private static boolean testTriangleArch() { boolean pass = true; int test = 1; int cnt; Triangle tri; Class cl; Class[] temp; System.out.println("Triangle architecture tests..."); Point a = new Point(0, 0); Point b = new Point(3, 0); Point c = new Point(0, 4); tri = new Triangle(a, b, c, Color.cyan, false); cl = tri.getClass(); pass &= test(cl.getConstructors().length == 1, test++); pass &= test((temp = cl.getInterfaces()).length == 1, test++); pass &= test(temp[0].getName().equals("Shape"), test++); pass &= test(verifyEqualsMethodSignature(cl), test++); cnt = countModifiers(cl.getDeclaredMethods(), Modifier.PUBLIC); pass &= test(cnt == 13, test++); cnt = cl.getFields().length; pass &= test(cnt == 0, test++); cnt = countModifiers(cl.getDeclaredFields(), Modifier.PROTECTED); pass &= test(cnt == 0, test++); cnt = countModifiers(cl.getDeclaredFields(), Modifier.PRIVATE); pass &= test(cnt == 5, test++); // Count and test number of of PACKAGE fields cnt = cl.getDeclaredFields().length - countModifiers(cl.getDeclaredFields(), Modifier.PRIVATE) - countModifiers(cl.getDeclaredFields(), Modifier.PROTECTED) - countModifiers(cl.getDeclaredFields(), Modifier.PUBLIC); pass &= test(cnt == 0, test++); return pass; }
private static boolean testTriangle() { boolean pass = true; int test = 1; Triangle tri; Point a = new Point(4, 0); Point b = new Point(-2, -1); Point c = new Point(1, 4); System.out.println("Triangle tests..."); tri = new Triangle(a, b, c, Color.cyan, false); pass &= test(approx(tri.getArea(), 13.5, 0.000001), test++); pass &= test(tri.getColor().equals(Color.cyan), test++); tri.setColor(Color.black); pass &= test(tri.getColor().equals(Color.black), test++); pass &= test(!tri.getFilled(), test++); tri.setFilled(true); pass &= test(tri.getFilled(), test++); pass &= test(tri.getVertexA().equals(new Point(4, 0)), test++); pass &= test(tri.getVertexB().equals(new Point(-2, -1)), test++); pass &= test(tri.getVertexC().equals(new Point(1, 4)), test++); a = new Point(7, -3); tri.setVertexA(a); pass &= test(tri.getVertexA().equals(new Point(7, -3)), test++); b = new Point(13, 56); tri.setVertexB(b); pass &= test(tri.getVertexB().equals(new Point(13, 56)), test++); c = new Point(-3, 23); tri.setVertexC(c); pass &= test(tri.getVertexC().equals(new Point(-3, 23)), test++); pass &= test(approx(tri.getArea(), 373.0, 0.000001), test++); tri.move(new Point(-5, -7)); pass &= test(tri.getVertexA().equals(new Point(2, -10)), test++); pass &= test(tri.getVertexB().equals(new Point(8, 49)), test++); pass &= test(tri.getVertexC().equals(new Point(-8, 16)), test++); a = new Point(2, -10); b = new Point(8, 49); c = new Point(-8, 16); Triangle tri2 = new Triangle(a, b, c, Color.black, true); pass &= test(tri.equals(tri2), test++); Triangle tri3 = new Triangle(new Point(3, -9), b, c, Color.black, false); pass &= test(!tri2.equals(tri3), test++); tri3 = new Triangle(a, new Point(7, 48), c, Color.black, false); pass &= test(!tri2.equals(tri3), test++); tri3 = new Triangle(a, b, new Point(-7, 17), Color.black, false); pass &= test(!tri2.equals(tri3), test++); tri3 = new Triangle(a, b, c, Color.red, false); pass &= test(!tri2.equals(tri3), test++); pass &= test(!tri2.equals(null), test++); pass &= test(!tri2.equals(new String("Whatever")), test++); return pass; }