/ RosterTest.java
RosterTest.java
  1  import org.junit.Test;
  2  import static org.junit.Assert.assertEquals;
  3  
  4  /// Tests
  5  public class RosterTest {
  6  
  7  	/// Dummy unused constructor to silence javadoc
  8  	public RosterTest() { }
  9  
 10  	/// Constructor
 11  	/// JRE entry point
 12  	///
 13  	/// @param args  command-line arguments (ignored)
 14  	public static void main(final String[] args) {
 15  		RosterTest test = new RosterTest();
 16  
 17  		test.testSize();
 18  		test.testPopulation();
 19  
 20  		System.out.println("All tests passed!");
 21  	}
 22  
 23  	/// Test size
 24  	@Test
 25  	private void testSize() {
 26  		Roster roster;
 27  
 28  		// 1D
 29  // TODO: test exception thrown at mismatching dimensions
 30  //		roster = new Roster(new int[] {-9}, new int[] {3});
 31  //		assertEquals(1, roster.BOTTOM.length);
 32  //		assertEquals(1, roster.TOP.length);
 33  //		assertEquals(-9, roster.BOTTOM[0]);
 34  //		assertEquals(3, roster.TOP[0]);
 35  //		assertEquals(12, roster.LEN[0]);
 36  
 37  		// 2D
 38  		roster = new Roster(new int[] {-9, 0}, new int[] {3, 9});
 39  		assertEquals(2, roster.BOTTOM.length);
 40  		assertEquals(2, roster.TOP.length);
 41  		assertEquals(-9, roster.BOTTOM[0]);
 42  		assertEquals(3, roster.TOP[0]);
 43  		assertEquals(13, roster.LEN[0]);
 44  		assertEquals(0, roster.BOTTOM[1]);
 45  		assertEquals(9, roster.TOP[1]);
 46  		assertEquals(10, roster.LEN[1]);
 47  
 48  		// 3D
 49  // TODO: test exception thrown at mismatching dimensions
 50  //		roster = new Roster(new int[] {-9, 0, 43}, new int[] {3, 9, 8});
 51  //		assertEquals(3, roster.BOTTOM.length);
 52  //		assertEquals(3, roster.TOP.length);
 53  //		assertEquals(-9, roster.BOTTOM[0]);
 54  //		assertEquals(3, roster.TOP[0]);
 55  //		assertEquals(12, roster.LEN[0]);
 56  //		assertEquals(0, roster.BOTTOM[1]);
 57  //		assertEquals(9, roster.TOP[1]);
 58  //		assertEquals(9, roster.LEN[1]);
 59  //		assertEquals(43, roster.BOTTOM[2]);
 60  //		assertEquals(8, roster.TOP[2]);
 61  //		assertEquals(-35, roster.LEN[2]);
 62  	}
 63  
 64  	/// Test populating
 65  	@Test
 66  	private void testPopulation() {
 67  		Roster roster;
 68  		Player player = new Player(Bearing.NORTH);
 69  		Thing treasure = new Treasure();
 70  		Thing trap = new Trap();
 71  		Thing obstacle = new Obstacle();
 72  
 73  		// sample positioned things mirrored from assignment
 74  		roster = new Roster(new int[] {-9, 0}, new int[] {0, 9});
 75  		assertEquals(true, roster.add(player, new int[] {0, 0}));
 76  		assertEquals(false, roster.add(player, new int[] {0, 0}));
 77  		assertEquals(true, roster.add(treasure, new int[] {0, 1}));
 78  		assertEquals(false, roster.add(obstacle, new int[] {0, 1}));
 79  		assertEquals(true, roster.add(obstacle, new int[] {0, 4}));
 80  		assertEquals(true, roster.add(trap, new int[] {1, 1}));
 81  		assertEquals(true, roster.add(trap, new int[] {1, 7}));
 82  		assertEquals("""
 83  ^ T . . O . . . . .
 84  . X . . . . . X . .
 85  . . . . . . . . . .
 86  . . . . . . . . . .
 87  . . . . . . . . . .
 88  . . . . . . . . . .
 89  . . . . . . . . . .
 90  . . . . . . . . . .
 91  . . . . . . . . . .
 92  . . . . . . . . . .""", roster.map());
 93  
 94  		// random placement
 95  		assertEquals(true, roster.add(treasure));
 96  		assertEquals(true, roster.add(treasure));
 97  		assertEquals(true, roster.add(treasure));
 98  		assertEquals(true, roster.add(treasure));
 99  		assertEquals(true, roster.add(trap));
100  		assertEquals(true, roster.add(trap));
101  		assertEquals(true, roster.add(trap));
102  		assertEquals(true, roster.add(obstacle));
103  		assertEquals(true, roster.add(obstacle));
104  		assertEquals(true, roster.add(obstacle));
105  		assertEquals(true, roster.add(obstacle));
106  		System.err.println(roster.map()
107  			.replace("\\n", System.lineSeparator()));
108  
109  		// agency
110  		int[] pos;
111  		assertEquals(Bearing.NORTH, player.getHeading());
112  		pos = roster.virtualMove(player, new int[] {0, 0});
113  		assertEquals(0, pos[0]);
114  		assertEquals(1, pos[1]);
115  		player.turnRight();
116  		assertEquals(Bearing.EAST, player.getHeading());
117  		pos = roster.virtualMove(player, new int[] {0, 0});
118  		assertEquals(1, pos[0]);
119  		assertEquals(0, pos[1]);
120  	}
121  }