-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCR_BoardInitTests.java
More file actions
148 lines (134 loc) · 5.06 KB
/
Copy pathCR_BoardInitTests.java
File metadata and controls
148 lines (134 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package clueTests;
// Doing a static import allows me to write assertEquals rather than
// Assert.assertEquals
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.*;
import java.io.FileNotFoundException;
import java.util.Map;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import clueGame.BadConfigFormatException;
import clueGame.Board;
import clueGame.BoardCell;
import clueGame.RoomCell;
public class CR_BoardInitTests {
// I made this static because I only want to set it up one
// time (using @BeforeClass), no need to do setup before each test
private static Board board;
public static final int NUM_ROOMS = 11;
public static final int NUM_ROWS = 22;
public static final int NUM_COLUMNS = 23;
@BeforeClass
public static void setUp() {
board = new Board("ClueLayout.csv", "ClueLegend.txt");
board.loadConfigFiles();
}
@Test
public void testRooms() {
Map<Character, String> rooms = board.getRooms();
// Ensure we read the correct number of rooms
assertEquals(NUM_ROOMS, rooms.size());
// Test retrieving a few from the hash, including the first
// and last in the file and a few others
assertEquals("Conservatory", rooms.get('C'));
assertEquals("Ballroom", rooms.get('B'));
assertEquals("Billiard room", rooms.get('R'));
assertEquals("Dining room", rooms.get('D'));
assertEquals("Walkway", rooms.get('W'));
}
@Test
public void testBoardDimensions() {
// Ensure we have the proper number of rows and columns
assertEquals(NUM_ROWS, board.getNumRows());
assertEquals(NUM_COLUMNS, board.getNumColumns());
}
// Test a doorway in each direction, plus two cells that are not
// a doorway.
// These cells are white on the planning spreadsheet
@Test
public void FourDoorDirections() {
// Test one each RIGHT/LEFT/UP/DOWN
RoomCell room = board.getRoomCellAt(4, 3);
assertTrue(room.isDoorway());
assertEquals(RoomCell.DoorDirection.RIGHT, room.getDoorDirection());
room = board.getRoomCellAt(4, 8);
assertTrue(room.isDoorway());
assertEquals(RoomCell.DoorDirection.DOWN, room.getDoorDirection());
room = board.getRoomCellAt(15, 18);
assertTrue(room.isDoorway());
assertEquals(RoomCell.DoorDirection.LEFT, room.getDoorDirection());
room = board.getRoomCellAt(14, 11);
assertTrue(room.isDoorway());
assertEquals(RoomCell.DoorDirection.UP, room.getDoorDirection());
// Test that room pieces that aren't doors know it
room = board.getRoomCellAt(14, 14);
assertFalse(room.isDoorway());
// Test that walkways are not doors
BoardCell cell = board.getCellAt(board.calcIndex(0, 6));
assertFalse(cell.isDoorway());
}
// Test that we have the correct number of doors
@Test
public void testNumberOfDoorways()
{
int numDoors = 0;
int totalCells = board.getNumColumns() * board.getNumRows();
Assert.assertEquals(506, totalCells);
for (int i=0; i<totalCells; i++)
{
BoardCell cell = board.getCellAt(i);
if (cell.isDoorway())
numDoors++;
}
Assert.assertEquals(16, numDoors);
}
@Test
public void testCalcIndex() {
// Test each corner of the board
assertEquals(0, board.calcIndex(0, 0));
assertEquals(NUM_COLUMNS-1, board.calcIndex(0, NUM_COLUMNS-1));
assertEquals(483, board.calcIndex(NUM_ROWS-1, 0));
assertEquals(505, board.calcIndex(NUM_ROWS-1, NUM_COLUMNS-1));
// Test a couple others
assertEquals(24, board.calcIndex(1, 1));
assertEquals(66, board.calcIndex(2, 20));
}
// Test a few room cells to ensure the room initial is
// correct.
@Test
public void testRoomInitials() {
assertEquals('C', board.getRoomCellAt(0, 0).getInitial());
assertEquals('R', board.getRoomCellAt(4, 8).getInitial());
assertEquals('B', board.getRoomCellAt(9, 0).getInitial());
assertEquals('O', board.getRoomCellAt(21, 22).getInitial());
assertEquals('K', board.getRoomCellAt(21, 0).getInitial());
}
// Test that an exception is thrown for a bad config file
@Test (expected = BadConfigFormatException.class)
public void testBadColumns() throws BadConfigFormatException, FileNotFoundException {
// overloaded Board ctor takes config file names
Board b = new Board("ClueLayoutBadColumns.csv", "ClueLegend.txt");
// You may change these calls if needed to match your function names
// My loadConfigFiles has a try/catch, so I can't call it directly to
// see test throwing the BadConfigFormatException
b.loadRoomConfig();
b.loadBoardConfig();
}
// Test that an exception is thrown for a bad config file
@Test (expected = BadConfigFormatException.class)
public void testBadRoom() throws BadConfigFormatException, FileNotFoundException {
// overloaded Board ctor takes config file name
Board b = new Board("ClueLayoutBadRoom.csv", "ClueLegend.txt");
b.loadRoomConfig();
b.loadBoardConfig();
}
// Test that an exception is thrown for a bad config file
@Test (expected = BadConfigFormatException.class)
public void testBadRoomFormat() throws BadConfigFormatException, FileNotFoundException {
// overloaded Board ctor takes config file name
Board b = new Board("ClueLayout.csv", "ClueLegendBadFormat.txt");
b.loadRoomConfig();
b.loadBoardConfig();
}
}