private static boolean testRectangle() {
    boolean pass = true;
    int test = 1;
    Rectangle rect;

    System.out.println("Rectangle tests...");

    rect = new Rectangle(4.3, 5.6, new Point(-99, 66), Color.cyan, false);
    pass &= test(approx(rect.getArea(), 4.3 * 5.6, 0.000001), test++);
    pass &= test(rect.getColor().equals(Color.cyan), test++);

    rect.setColor(Color.black);
    pass &= test(rect.getColor().equals(Color.black), test++);
    pass &= test(!rect.getFilled(), test++);

    rect.setFilled(true);
    pass &= test(rect.getFilled(), test++);
    pass &= test(rect.getWidth() == 4.3, test++);
    pass &= test(rect.getHeight() == 5.6, test++);

    rect.setWidth(4.321);
    pass &= test(rect.getWidth() == 4.321, test++);

    rect.setHeight(89.21);
    pass &= test(rect.getHeight() == 89.21, test++);
    pass &= test(approx(rect.getArea(), 4.321 * 89.21, 0.000001), test++);
    pass &= test(rect.getPosition().equals(new Point(-99, 66)), test++);

    rect.move(new Point(-5, -7));
    pass &= test(rect.getPosition().equals(new Point(-104, 59)), test++);

    Rectangle rect2 = new Rectangle(4.321, 89.21, new Point(-104, 59), Color.black, true);
    pass &= test(rect.equals(rect2), test++);

    Rectangle rect3 = new Rectangle(4.3219, 89.21, new Point(-104, 59), Color.black, false);
    pass &= test(!rect2.equals(rect3), test++);

    rect3 = new Rectangle(4.321, 89.219, new Point(-104, 59), Color.black, false);
    pass &= test(!rect2.equals(rect3), test++);

    rect3 = new Rectangle(4.321, 89.21, new Point(-104, 59), Color.red, false);
    pass &= test(!rect2.equals(rect3), test++);

    rect3 = new Rectangle(4.321, 89.21, new Point(104, 59), Color.black, false);
    pass &= test(!rect2.equals(rect3), test++);

    pass &= test(!rect2.equals(null), test++);
    pass &= test(!rect2.equals(new String("Whatever")), test++);

    return pass;
  }
示例#2
0
  public static Segment[] slice(Rectangle r) {
    Segment[] retval = new Segment[r.getLines()];
    for (int i = 0; i < retval.length; ++i)
      retval[i] = new Segment(r.getPosition().vertical(i), r.getCols());

    return retval;
  }