-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestClass.java
637 lines (587 loc) · 16.9 KB
/
TestClass.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Tests for Trivia Maze Game
*
* @author Ilya Kozorezov
* @author Rin Pham
* @version 1.0
* @see Maze
* @see Graph
* @since 1.0
*/
class TestClass {
/**
* Add a new vertex on empty graph
*
* @result Graph will have a new vertex and only 1 vertex
*/
@Test
void testAddVertex_on_BlankGraph() {
Graph graph = new Graph();
graph.addVertex(1);
assertEquals(graph.toString(), "1: \n");
}
/**
* Add a new vertex to an existing graph
*
* @result Graph will have 2 vertices that are unlinked
*/
@Test
void testAddVertex_on_UsedGraph() {
Graph graph = new Graph();
graph.addVertex(1);
graph.addVertex(2);
assertEquals(graph.toString(), """
1:\s
2:\s
""");
}
/**
* Attempt to create a new vertex on an existing vertex
*
* @result throws IllegalArgumentException because the vertex already exists
*/
@Test
void testAddVertex_duplicateVertexes() {
Graph graph = new Graph();
graph.addVertex(1);
assertThrows(IllegalArgumentException.class, () -> graph.addVertex(1));
}
/**
* Create a new edge between 2 nonexistent vertices
*
* @result Creates 2 new vertices and links them with an edge
*/
@Test
void testAddEdge_on_BlankGraph() {
Graph graph = new Graph();
graph.addEdge(1, 2);
assertEquals(graph.toString(), """
1: 2\s
2: 1\s
""");
}
/**
* Create a new edge between 2 existent vertices
*
* @result Creates a new edge between 2 existent vertices
*/
@Test
void testAddEdge_on_UsedGraph() {
Graph graph = new Graph();
graph.addVertex(1);
graph.addVertex(2);
graph.addEdge(1, 2);
assertEquals(graph.toString(), """
1: 2\s
2: 1\s
""");
}
/**
* Try to duplicate an existing edge
*
* @result Nothing should change
*/
@Test
void testAddEdge_duplicateEdges() {
Graph graph = new Graph();
graph.addEdge(1, 2);
graph.addEdge(1, 2);
assertEquals(graph.toString(), """
1: 2\s
2: 1\s
""");
}
/**
* Remove the edge between 2 vertices
*
* @result removes the edge between 2 vertices
*/
@Test
void testRemoveEdge() {
Graph graph = new Graph();
graph.addEdge(1, 2);
graph.removeEdge(1, 2);
assertEquals(graph.toString(), """
1:\s
2:\s
""");
}
/**
* Attempt to remove an edge that does not exist
*
* @result throws IllegalArgumentException because edge does not exist
*/
@Test
void testRemoveEdge_thatDoesNotExist() {
Graph graph = new Graph();
graph.addEdge(1, 2);
assertThrows(IllegalArgumentException.class, () -> graph.removeEdge(3, 2));
}
/**
* Iterate through a connected graph using a depth first search
*
* @result true because the whole graph is iterated through
*/
@Test
void testDepthFirstSearch_onConnectedGraph() {
Graph graph = new Graph();
graph.addEdge(0, 1);
graph.addEdge(1, 2);
graph.addEdge(2, 3);
assertTrue(graph.depthFirstSearch(0, 4));
}
/**
* Iterated through an unconnected graph using depth first search
*
* @result false because the graph is unconnected
*/
@Test
void testDepthFirstSearch_onUnconnectedGraph() {
Graph graph = new Graph();
graph.addEdge(0, 1);
graph.addEdge(2, 3);
assertFalse(graph.depthFirstSearch(0, 4));
}
/**
* Attempt to iterate through a graph with input of size being invalid
*
* @result throws IllegalArgumentException because size is not valid
*/
@Test
void testDepthFirstSearch_IllegalSize() {
Graph graph = new Graph();
graph.addEdge(0, 1);
assertThrows(IllegalArgumentException.class, () -> graph.depthFirstSearch(0, -1));
}
/**
* Attempt to iterate through a graph starting from a source not in the graph
*
* @result throws IllegalArgumentException because the source is not in the graph
*/
@Test
void testDepthFirstSearch_IllegalSource() {
Graph graph = new Graph();
graph.addEdge(0, 1);
assertThrows(IllegalArgumentException.class, () -> graph.depthFirstSearch(2, 2));
}
/**
* See if vertex 0 has an edge to vertex 1
*
* @result true
*/
@Test
void testHasEdge_HasEdge() {
Graph graph = new Graph();
graph.addEdge(0, 1);
assertTrue(graph.hasEdge(0, 1));
}
/**
* See if vertex 0 has an edge to vertex 1
*
* @result false
*/
@Test
void testHasEdge_NoEdge() {
Graph graph = new Graph();
graph.addEdge(0, 1);
assertFalse(graph.hasEdge(0, 2));
}
/**
* Create a new Maze with size 16
*
* @result Creates a new maze with the size 16
*/
@Test
void testCreate4x4_Maze() {
Maze maze = new Maze(16);
assertEquals(maze.toString(), """
0: 1 4\s
1: 0 2 5\s
2: 1 3 6\s
3: 2 7\s
4: 0 5 8\s
5: 1 4 6 9\s
6: 2 5 7 10\s
7: 3 6 11\s
8: 4 9 12\s
9: 5 8 10 13\s
10: 6 9 11 14\s
11: 7 10 15\s
12: 8 13\s
13: 9 12 14\s
14: 10 13 15\s
15: 11 14\s
""");
}
/**
* Create a new Maze with size 25
*
* @result Creates a new maze with the size 25
*/
@Test
void testCreate5x5_Maze() {
Maze maze = new Maze(25);
assertEquals(maze.toString(), """
0: 1 5\s
1: 0 2 6\s
2: 1 3 7\s
3: 2 4 8\s
4: 3 9\s
5: 0 6 10\s
6: 1 5 7 11\s
7: 2 6 8 12\s
8: 3 7 9 13\s
9: 4 8 14\s
10: 5 11 15\s
11: 16 6 10 12\s
12: 17 7 11 13\s
13: 18 8 12 14\s
14: 19 9 13\s
15: 16 20 10\s
16: 17 21 11 15\s
17: 16 18 22 12\s
18: 17 19 23 13\s
19: 18 24 14\s
20: 21 15\s
21: 16 20 22\s
22: 17 21 23\s
23: 18 22 24\s
24: 19 23\s
""");
}
/**
* Create a new Maze with size 36
*
* @result Creates a new maze with the size 36
*/
@Test
void testCreate6x6_Maze() {
Maze maze = new Maze(36);
assertEquals(maze.toString(), """
0: 1 6\s
1: 0 2 7\s
2: 1 3 8\s
3: 2 4 9\s
4: 3 5 10\s
5: 4 11\s
6: 0 7 12\s
7: 1 6 8 13\s
8: 2 7 9 14\s
9: 3 8 10 15\s
10: 16 4 9 11\s
11: 17 5 10\s
12: 18 6 13\s
13: 19 7 12 14\s
14: 20 8 13 15\s
15: 16 21 9 14\s
16: 17 22 10 15\s
17: 16 23 11\s
18: 19 24 12\s
19: 18 20 25 13\s
20: 19 21 26 14\s
21: 20 22 27 15\s
22: 16 21 23 28\s
23: 17 22 29\s
24: 18 25 30\s
25: 19 24 26 31\s
26: 32 20 25 27\s
27: 33 21 26 28\s
28: 34 22 27 29\s
29: 35 23 28\s
30: 24 31\s
31: 32 25 30\s
32: 33 26 31\s
33: 32 34 27\s
34: 33 35 28\s
35: 34 29\s
""");
}
/**
* Attempt to create a maze with size that is less than 8
*
* @result throws IllegalArgumentException because size is invalid
*/
@Test
void testCreateMaze_IllegalSmallSize() {
assertThrows(IllegalArgumentException.class, () -> new Maze(4));
}
/**
* Attempt to create a maze with a size that is not a perfect square
*
* @result throws IllegalArgumentException because size is invalid
*/
@Test
void testCreateMaze_IllegalNotSquare() {
assertThrows(IllegalArgumentException.class, () -> new Maze(10));
}
/**
* Close a door between 2 rooms on the maze
*
* @result Have the door be closed between the 2 rooms
*/
@Test
void testCloseDoor() {
Maze maze = new Maze(9);
maze.closeDoor(1, 2);
assertEquals(maze.toString(), """
0: 1 3\s
1: 0 4\s
2: 5\s
3: 0 4 6\s
4: 1 3 5 7\s
5: 2 4 8\s
6: 3 7\s
7: 4 6 8\s
8: 5 7\s
""");
}
/**
* Close a door between 2 rooms that is already closed
*
* @result Nothing since the door is already closed
*/
@Test
void testCloseDoor_AlreadyClosed() {
Maze maze = new Maze(9);
maze.closeDoor(1, 2);
maze.closeDoor(1, 2);
assertEquals(maze.toString(), """
0: 1 3\s
1: 0 4\s
2: 5\s
3: 0 4 6\s
4: 1 3 5 7\s
5: 2 4 8\s
6: 3 7\s
7: 4 6 8\s
8: 5 7\s
""");
}
/**
* Close a door but the maze still can be finished
*
* @result true because the maze is still possible to complete
*/
@Test
void testCloseDoor_mazeStillConnected() {
Maze maze = new Maze(9);
assertTrue(maze.closeDoor(0, 1));
}
/**
* Close doors so that the maze is not possible to finish
*
* @result false because the maze is not possible to complete
*/
@Test
void testCloseDoor_mazeNowUnconnected() {
Maze maze = new Maze(9);
maze.closeDoor(0, 3);
assertFalse(maze.closeDoor(0, 1));
}
/**
* Close a door that does not exist
*
* @result Nothing because the door does not exist
*/
@Test
void testCloseDoor_nonExistingDoor() {
Maze maze = new Maze(9);
assertTrue(maze.closeDoor(0, 8));
assertEquals(maze.toString(), """
0: 1 3\s
1: 0 2 4\s
2: 1 5\s
3: 0 4 6\s
4: 1 3 5 7\s
5: 2 4 8\s
6: 3 7\s
7: 4 6 8\s
8: 5 7\s
""");
}
@Test
void getLevel() {
Player.createPlayer("Player1", Difficulty.Hard);
assertEquals(Player.getPlayer().getLevel(), Difficulty.Hard);
Player.deletePlayer();
}
@Test
void getName() {
Player.createPlayer("Player2", Difficulty.Hard);
assertEquals("Player2", Player.getPlayer().getName());
Player.deletePlayer();
}
@Test
void getPlayerPosition() {
Player.createPlayer("player3", Difficulty.Hard);
assertEquals(0, Player.getPlayer().getPlayerPosition());
Player.deletePlayer();
}
/**
* Move Player.getPlayer() right 1 position
*
* @result Player.getPlayer() moves from room 0 to 1
*/
@Test
void testMovePlayer_Right_valid() {
Player.createPlayer("Bill", Difficulty.Test);
Player.getPlayer().movePlayer(Directions.RIGHT);
assertEquals(Player.getPlayer().getPlayerPosition(), 1);
Player.deletePlayer();
}
/**
* Move Player.getPlayer() right 1 position when there is no position on the right
*
* @result Player.getPlayer() stays in the same room
*/
@Test
void testMovePlayer_Right_invalid() {
Player.createPlayer("Bill", Difficulty.Test);
Player.getPlayer().setPlayerPosition(2);
Player.getPlayer().movePlayer(Directions.RIGHT);
assertEquals(Player.getPlayer().getPlayerPosition(), 2);
Player.deletePlayer();
}
/**
* Move Player.getPlayer() left 1 position
*
* @result Player.getPlayer() moves from room 1 to room 0
*/
@Test
void testMovePlayer_Left_valid() {
Player.createPlayer("Bill", Difficulty.Test);
Player.getPlayer().setPlayerPosition(1);
Player.getPlayer().movePlayer(Directions.LEFT);
assertEquals(Player.getPlayer().getPlayerPosition(), 0);
Player.deletePlayer();
}
/**
* Move Player.getPlayer() left 1 position when there is no position on the right
*
* @result Player.getPlayer() stays in the same room
*/
@Test
void testMovePlayer_Left_invalid() {
Player.createPlayer("Bill", Difficulty.Test);
Player.getPlayer().movePlayer(Directions.LEFT);
assertEquals(Player.getPlayer().getPlayerPosition(), 0);
Player.deletePlayer();
}
/**
* Move Player.getPlayer() up 1 position
*
* @result Player.getPlayer() moves from room 3 to room 0
*/
@Test
void testMovePlayer_Up_valid() {
Player.createPlayer("Bill", Difficulty.Test);
Player.getPlayer().setPlayerPosition(3);
Player.getPlayer().movePlayer(Directions.UP);
assertEquals(Player.getPlayer().getPlayerPosition(), 0);
Player.deletePlayer();
}
/**
* Move Player.getPlayer() up 1 position when there is no position on the right
*
* @result Player.getPlayer() stays in the same room
*/
@Test
void testMovePlayer_Up_invalid() {
Player.createPlayer("Bill", Difficulty.Test);
Player.getPlayer().movePlayer(Directions.UP);
assertEquals(Player.getPlayer().getPlayerPosition(), 0);
Player.deletePlayer();
}
/**
* Move Player.getPlayer() down 1 position
*
* @result Player.getPlayer() moves from room 0 to 3
*/
@Test
void testMovePlayer_Down_valid() {
Player.createPlayer("Bill", Difficulty.Test);
Player.getPlayer().movePlayer(Directions.DOWN);
assertEquals(Player.getPlayer().getPlayerPosition(), 3);
Player.deletePlayer();
}
/**
* Move Player.getPlayer() right 1 position when there is no position on the right
*
* @result Player.getPlayer() stays in the same room
*/
@Test
void testMovePlayer_Down_invalid() {
Player.createPlayer("Bill", Difficulty.Test);
Player.getPlayer().setPlayerPosition(6);
Player.getPlayer().movePlayer(Directions.DOWN);
assertEquals(Player.getPlayer().getPlayerPosition(), 6);
Player.deletePlayer();
}
/**
* Create an instance of the player
*
* @result creates a new player
*/
@Test
void testPlayer_createPlayer() {
Player.createPlayer("Bill", Difficulty.Test);
assertEquals(Player.getPlayer().getLevel(), Difficulty.Test);
Player.deletePlayer();
}
/**
* Create an instance of player and get it
*
* @result get the instance of the player
*/
@Test
void testPlayer_getPlayer() {
Player.createPlayer("Bill", Difficulty.Test);
assertEquals(Player.getPlayer().getLevel(), Difficulty.Test);
Player.deletePlayer();
}
/**
* Delete the instance of the player
*
* @result player will be null
*/
@Test
void testPlayer_deletePlayer() {
Player.createPlayer("Bill", Difficulty.Test);
Player.deletePlayer();
assertNull(Player.getPlayer());
}
/**
* Creates a new game with a clean player
*/
@Test
void testNewGame() {
Player.createPlayer("Bill", Difficulty.Test);
Player.getPlayer().setPlayerPosition(4);
GameState.saveGame();
GameState.newGame("Bill", Difficulty.Easy);
assertEquals(Player.getPlayer().getLevel(), Difficulty.Easy);
Player.deletePlayer();
}
/**
* Save and load the game
*
* @result saves and loads the player
*/
@Test
void testSave_and_LoadGame() {
Player.createPlayer("Bill", Difficulty.Test);
Player.getPlayer().setPlayerPosition(4);
GameState.saveGame();
GameState.endGame();
assertNull(Player.getPlayer());
GameState.loadGame();
assertEquals(Player.getPlayer().getLevel(), Difficulty.Test);
Player.deletePlayer();
}
}