/** * This is part of the Problem Set 0: Introduction for 6.170 Fall 2005. */ package ps0; import java.util.Iterator; import java.util.LinkedList; import java.util.Random; import junit.framework.TestCase; /** * BoxTest is a glassbox test of the Box class. * * Recall that like a BallContainer, the Box is a container for Balls and you * can only put a Ball into a Box once. After you put the Ball into * the Box, further attempts to do so will fail, since the Ball is * already in the Box! Similarly, you cannot expect to remove a Ball * from a Box if it is not inside the Box. * * In addition, a Box differs from a ballcontainer because it only has a finite * volume. As Balls get put into a Box, it gets filled up. Once a Box * is full, further attempts to put Balls into the Box will also fail. * * @see ps0.Ball * @see ps0.BallContainer * @see ps0.Box */ public class BoxTest extends TestCase { private Box box = null; private int NUM_BALLS_TO_TEST = 5; private int BOX_CAPACITY = NUM_BALLS_TO_TEST - 1; private Ball[] b = null; private double BALL_UNIT_VOLUME = 10.0; private double JUNIT_DOUBLE_DELTA = 0.0001; private int TRIES_FOR_BALLS_TEST = 3; protected void setUp() throws Exception { assertEquals("Test case error, you must test at least 1 Ball!!", true, NUM_BALLS_TO_TEST > 0); assertEquals("This test case is set up assuming that the box cannot contain all the balls, please check and change parameters!", true, NUM_BALLS_TO_TEST > BOX_CAPACITY); double box_volume = 0; // Let's create the balls we need. b = new Ball[NUM_BALLS_TO_TEST]; for (int i=0; i list = new LinkedList(); for (int i=0; i it = box.getBallsFromSmallest(); int count = 0; while (it.hasNext() && count < BOX_CAPACITY) { Ball ball = it.next(); assertEquals("Balls are not returned by Box.getBallsFromSmallest() iterator in the correct order", true, b[count] == ball); if (b[count] != ball) { break; } count++; } assertEquals("Box.getBallsFromSmallest() did not return all the balls", BOX_CAPACITY, count); } } /** * Test to check that Box.remove(Ball) is implemented * correctly. Depending on how getBallsFromSmallest() * is implemented, remove() might have to be overridden and this * test helps ensure that remove() is not broken in the process. */ public void testRemove() { box.clear(); assertEquals("Box.remove(Ball) should fail because box is empty, but it didn't!", false, box.remove(b[0])); for (int i=0; i