import junit.framework.TestCase; /** * A JUnit test case class. * Every method starting with the word "test" will be called when running * the test with JUnit. */ public class Test_IShape extends TestCase { double _tol = 1e-15; // tolerance value for assertEquals using doubles /** * Test the Square class */ public void test_Square() { Square s1 = new Square(5); assertEquals("Square(5).getArea()", 25.0, s1.getArea(), _tol); // Note that we don't have to assign an object to a variable to use it: assertEquals("Square(1.3).getArea()", 1.69, (new Square(1.3)).getArea(), _tol); assertEquals("Square(0).getArea()", 0, (new Square(0)).getArea(), _tol); } /** * Test the Circle class */ public void test_Circle() { assertEquals("Circle(5).getArea()", Math.PI*25.0, (new Circle(5)).getArea(), _tol); assertEquals("Circle(1.3).getArea()", Math.PI*1.69, (new Circle(1.3)).getArea(), _tol); assertEquals("Circle(0).getArea()", 0, (new Circle(0)).getArea(), _tol); } /** * Test the Rectangle class */ public void test_Rectangle() { assertEquals("Rectangle(5, 7).getArea()", 35.0, (new Rectangle(5, 7)).getArea(), _tol); assertEquals("Rectangle(1.3, 2.7).getArea()", 3.51, (new Rectangle(1.3, 2.7)).getArea(), _tol); assertEquals("Rectangle(0, 0).getArea()", 0, (new Rectangle(0, 0)).getArea(), _tol); assertEquals("Rectangle(0, Math.PI).getArea()", 0, (new Rectangle(0, Math.PI)).getArea(), _tol); assertEquals("Rectangle(Math.PI, 0).getArea()", 0, (new Rectangle(Math.PI, 0)).getArea(), _tol); } /** * Test the Triangle class */ public void test_Triangle() { assertEquals("Triangle(5, 7).getArea()", 17.5, (new Triangle(5, 7)).getArea(), _tol); assertEquals("Triangle(1.3, 2.7).getArea()", 1.755, (new Triangle(1.3, 2.7)).getArea(), _tol); assertEquals("Triangle(0, 0).getArea()", 0, (new Triangle(0, 0)).getArea(), _tol); assertEquals("Triangle(0, Math.PI).getArea()", 0, (new Triangle(0, Math.PI)).getArea(), _tol); assertEquals("Triangle(Math.PI, 0).getArea()", 0, (new Triangle(Math.PI, 0)).getArea(), _tol); } /** * Test the IShape interface */ public void test_IShape() { IShape s; // s is an abstract shape // Notice how all the calculations below use the abstract IShape.getArea(), // not the getArea() from the concrete subclasses! s = new Square(5); // s refers to a square now assertEquals("Square(5): IShape.getArea()",25.0, s.getArea(), _tol); s = new Circle(5); // s refers to a circle now assertEquals("Circle(5): IShape.getArea()",Math.PI*25.0, s.getArea(), _tol); s = new Rectangle(5, 7); // s refers to a rectangle now assertEquals("Rectangle(5, 7): IShape.getArea()",35.0, s.getArea(), _tol); s = new Triangle(5, 7); // s refers to a triangle now assertEquals("Triangle(5, 7): IShape.getArea()",17.5, s.getArea(), _tol); } }