-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ml
473 lines (425 loc) · 17.3 KB
/
test.ml
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
open State
open Command
open Cpu
open OUnit2
let pp_string s = "\"" ^ s ^ "\""
let interpret coord_from coord_to (state : State.state) =
try
let result = valid_move coord_from coord_to state in
match result with
| true -> "true"
| false -> "false"
with
| NoPiece -> "No Piece"
| FullSquare -> "Full Square"
| Backwards -> "Backwards"
| NotAdjacent -> "Not Adjacent"
let make_valid_move_test
(name : string)
(coord_from : State.coordinate)
(coord_to : State.coordinate)
(state : State.state)
(expected_output : string) : test =
name >:: (fun _ ->
assert_equal expected_output (interpret coord_from coord_to state)
~printer:(pp_string))
let empty_board = (true, [])
let two_piece_board : State.state =
(true, [(("f", "3"), {color = "red"; king = false});
(("g", "4"), {color = "white"; king = false})])
let one_king_board : State.state = (true, [(("f", "3"), {color = "red";
king = true})])
let valid_move_tests =
[
make_valid_move_test "No Piece" ("e", "2") ("d", "3") two_piece_board
"No Piece";
make_valid_move_test "Full Square" ("f", "3") ("g", "4") two_piece_board
"Full Square";
make_valid_move_test "Backwards" ("f", "3") ("e", "2") two_piece_board
"Backwards";
make_valid_move_test "King Backwards" ("f", "3") ("e", "2") one_king_board
"true";
make_valid_move_test "Not Adjacent Num" ("f", "3") ("f", "5")
two_piece_board "Not Adjacent";
make_valid_move_test "Not Adjacent Letter"("f", "3") ("g", "3")
two_piece_board "Not Adjacent";
make_valid_move_test "Move Wrong Color" ("g", "4") ("h", "3")
two_piece_board "No Piece";
make_valid_move_test "Valid Adjacent" ("f", "3") ("e", "4") two_piece_board
"true"
]
let make_move_test
(name : string)
(coord_from : State.coordinate)
(coord_to : State.coordinate)
(state : State.state)
(expected_output : State.state) : test =
name >:: (fun _ ->
assert_equal expected_output (move coord_from coord_to state))
let two_piece_board_2 : State.state =
(false, [(("e", "4"), {color = "red"; king = false});
(("g", "4"), {color = "white"; king = false})])
let one_king_board_2 : State.state = (false, [(("e", "4"), {color = "red";
king = true})])
let one_king_board_3 : State.state = (false, [(("e", "2"), {color = "red";
king = true})])
let full_board_1 : State.state =
(false, [(("b", "1"), {color = "red"; king = false});
(("d", "1"), {color = "red"; king = false});
(("f", "1"), {color = "red"; king = false});
(("h", "1"), {color = "red"; king = false});
(("a", "2"), {color = "red"; king = false});
(("c", "2"), {color = "red"; king = false});
(("e", "2"), {color = "red"; king = false});
(("g", "2"), {color = "red"; king = false});
(("b", "3"), {color = "red"; king = false});
(("d", "3"), {color = "red"; king = false});
(("f", "3"), {color = "red"; king = false});
(("g", "4"), {color = "red"; king = false});
(("a", "6"), {color = "white"; king = false});
(("c", "6"), {color = "white"; king = false});
(("e", "6"), {color = "white"; king = false});
(("g", "6"), {color = "white"; king = false});
(("b", "7"), {color = "white"; king = false});
(("d", "7"), {color = "white"; king = false});
(("f", "7"), {color = "white"; king = false});
(("h", "7"), {color = "white"; king = false});
(("a", "8"), {color = "white"; king = false});
(("c", "8"), {color = "white"; king = false});
(("e", "8"), {color = "white"; king = false});
(("g", "8"), {color = "white"; king = false});
])
let full_board_2 : State.state =
(true, [(("b", "1"), {color = "red"; king = false});
(("d", "1"), {color = "red"; king = false});
(("f", "1"), {color = "red"; king = false});
(("h", "1"), {color = "red"; king = false});
(("a", "2"), {color = "red"; king = false});
(("c", "2"), {color = "red"; king = false});
(("e", "2"), {color = "red"; king = false});
(("g", "2"), {color = "red"; king = false});
(("b", "3"), {color = "red"; king = false});
(("d", "3"), {color = "red"; king = false});
(("f", "3"), {color = "red"; king = false});
(("g", "4"), {color = "red"; king = false});
(("b", "5"), {color = "white"; king = false});
(("c", "6"), {color = "white"; king = false});
(("e", "6"), {color = "white"; king = false});
(("g", "6"), {color = "white"; king = false});
(("b", "7"), {color = "white"; king = false});
(("d", "7"), {color = "white"; king = false});
(("f", "7"), {color = "white"; king = false});
(("h", "7"), {color = "white"; king = false});
(("a", "8"), {color = "white"; king = false});
(("c", "8"), {color = "white"; king = false});
(("e", "8"), {color = "white"; king = false});
(("g", "8"), {color = "white"; king = false});
])
let make_king_board : State.state =
(true, [(("e", "7"), {color = "red"; king = false});
(("g", "4"), {color = "white"; king = false})])
let make_king_board_2 : State.state =
(false, [(("d", "8"), {color = "red"; king = true});
(("g", "4"), {color = "white"; king = false})])
let move_tests =
[
make_move_test "Two piece standard" ("f", "3") ("e", "4") two_piece_board
two_piece_board_2;
make_move_test "one king standard" ("f", "3") ("e", "4") one_king_board
one_king_board_2;
make_move_test "one king backwards" ("f", "3") ("e", "2") one_king_board
one_king_board_3;
make_move_test "full board" ("h", "3") ("g", "4") State.init_state
full_board_1;
make_move_test "full board 2" ("a", "6") ("b", "5") full_board_1
full_board_2;
make_move_test "make king" ("e", "7") ("d", "8") make_king_board
make_king_board_2
]
let interpretCap coord_from coord_to (state : State.state) =
try
let result = valid_capture coord_from coord_to state in
match result with
| true -> "true"
| false -> "false"
with
| NoPiece -> "No Piece"
| FullSquare -> "Full Square"
| Backwards -> "Backwards"
| NotAdjCapture -> "Not Adjacent Capture"
| NoPieceToCapture -> "No Piece to Capture"
| OwnPieceCap -> "Own Piece Capture"
let empty_board = (true, [])
let board1 : State.state =
(true, [(("f", "3"), {color = "red"; king = false});
(("g", "4"), {color = "white"; king = false});
(("e", "2"), {color = "white"; king = false});
(("g", "3"), {color = "white"; king = false})])
let board2 : State.state =
(true, [(("f", "3"), {color = "red"; king = false});
(("h", "5"), {color = "red"; king = false})])
let board3 : State.state =
(false, [(("f", "3"), {color = "red"; king = false});
(("g", "4"), {color = "white"; king = true});
(("e", "4"), {color = "white"; king = false})])
let board4 : State.state =
(true, [(("f", "3"), {color = "red"; king = false});
(("g", "4"), {color = "red"; king = false})])
let white_cap_board : State.state =
(false, [(("f", "6"), {color = "red"; king = false});
(("e", "7"), {color = "white"; king = false});
(("a", "8"), {color = "red"; king = false})])
let make_valid_capture_test
(name : string)
(coord_from : State.coordinate)
(coord_to : State.coordinate)
(state : State.state)
(expected_output : string) : test =
name >:: (fun _ ->
assert_equal expected_output (interpretCap coord_from coord_to state)
~printer:(pp_string))
let valid_capture_tests =
[
make_valid_capture_test "No Piece" ("e", "2") ("d", "3") board1 "No Piece";
make_valid_capture_test "Full Square" ("f", "3") ("h", "5") board2
"Full Square";
make_valid_capture_test "Backwards" ("f", "3") ("d", "1") board1 "Backwards";
make_valid_capture_test "Not Adjacent Num" ("f", "3") ("f", "5") board1
"Not Adjacent Capture";
make_valid_capture_test "Not Adjacent Letter" ("f", "3") ("h", "3") board1
"Not Adjacent Capture";
make_valid_capture_test "No Piece to Cap" ("f", "3") ("d", "5") board2
"No Piece to Capture";
make_valid_capture_test "Own Piece Cap" ("f", "3") ("h", "5") board4
"Own Piece Capture";
make_valid_capture_test "Valid Capture" ("f", "3") ("h", "5") board1 "true";
make_valid_capture_test "Valid King Backwards Capture" ("g", "4") ("e", "2")
board3 "true";
make_valid_capture_test "Regular Move" ("f", "3") ("e", "4") board2
"Not Adjacent Capture";
make_valid_capture_test "White Capture" ("e", "4") ("g", "2") board3 "true";
make_valid_capture_test "white2" ("e", "7") ("g", "5") white_cap_board "true"
]
let make_capture_test
(name : string)
(coord_from : State.coordinate)
(coord_to : State.coordinate)
(state : State.state)
(expected_output : State.state) : test =
name >:: (fun _ ->
assert_equal expected_output (capture coord_from coord_to state))
let three_piece_board : State.state =
(true, [(("f", "3"), {color = "red"; king = false});
(("g", "4"), {color = "white"; king = false});
(("a", "8"), {color = "white"; king = false})])
let three_piece_board_2 : State.state =
(false, [(("h", "5"), {color = "red"; king = false});
(("a", "8"), {color = "white"; king = false})])
let king_capture_board : State.state =
(true, [(("f", "3"), {color = "red"; king = true});
(("g", "4"), {color = "white"; king = true});
(("e", "2"), {color = "white"; king = true})])
let king_capture_board_2 : State.state =
(false, [(("d", "1"), {color = "red"; king = true});
(("g", "4"), {color = "white"; king = true})])
let make_king_cap_board : State.state =
(true, [(("f", "6"), {color = "red"; king = false});
(("e", "7"), {color = "white"; king = false});
(("a", "8"), {color = "white"; king = false})])
let make_king_cap_board_2 : State.state =
(false, [(("d", "8"), {color = "red"; king = true});
(("a", "8"), {color = "white"; king = false})])
let white_cap_board : State.state =
(false, [(("f", "6"), {color = "red"; king = false});
(("e", "7"), {color = "white"; king = false});
(("a", "8"), {color = "red"; king = false})])
let white_cap_board_2 : State.state =
(true, [(("g", "5"), {color = "white"; king = false});
(("a", "8"), {color = "red"; king = false})])
let white_cap_board_3 : State.state =
(false, [(("f", "6"), {color = "red"; king = false});
(("e", "7"), {color = "white"; king = false});
(("a", "8"), {color = "red"; king = false});
(("d", "6"), {color = "red"; king = false})])
let white_cap_board_4 : State.state =
(true, [(("f", "6"), {color = "red"; king = false});
(("c", "5"), {color = "white"; king = false});
(("a", "8"), {color = "red"; king = false})])
let capture_tests =
[
make_capture_test "standard" ("f", "3") ("h", "5") three_piece_board
three_piece_board_2;
make_capture_test "kings" ("f", "3") ("d", "1") king_capture_board
king_capture_board_2;
make_capture_test "make king" ("f", "6") ("d", "8") make_king_cap_board
make_king_cap_board_2;
make_capture_test "white cap1" ("e", "7") ("g", "5") white_cap_board
white_cap_board_2;
make_capture_test "white cap2" ("e", "7") ("c", "5") white_cap_board_3
white_cap_board_4;
]
let command_to_string command =
match command with
| Move (c1,c2) -> "move" ^ (fst c1) ^ (snd c1) ^ "to" ^ (fst c2) ^ (snd c2)
| Capture (c1,c2) -> "move" ^ (fst c1) ^ (snd c1) ^ "to" ^ (fst c2) ^(snd c2)
| Hint -> "hint"
| Board -> "board"
| Quit -> "quit"
| Help -> "help"
| Score -> "score"
let make_command_test
(name : string)
(input : string)
(expected_output : command) : test =
name >:: (fun _ ->
assert_equal expected_output (parse input) ~printer:command_to_string)
let command_tests =
[
make_command_test "move1" " move a3 to b4 "
(Move (("a","3"),("b","4")));
make_command_test "move2" "move c3 to b4 "
(Move (("c","3"),("b","4")));
make_command_test "cap1" " capture a3 to b4 "
(Capture (("a","3"),("b","4")));
make_command_test "hint1" " hint "
Hint;
make_command_test "board1" " board "
(Board);
make_command_test "quit1" " quit "
(Quit);
make_command_test "help1" " help "
(Help);
make_command_test "score1" " score "
(Score);
]
let bad_command_output str =
match parse str with
| exception Malformed -> "Malformed"
| exception Empty -> "Empty"
| _ -> "Uncaught"
let make_command_exception_test
(name : string)
(input : string)
(expected_output : string) : test =
name >:: (fun _ ->
assert_equal expected_output (bad_command_output input))
let command_exception_tests =
[
make_command_exception_test "exp1" "quit asdflk;sdf"
"Malformed";
make_command_exception_test "exp2" "quitt "
"Malformed";
make_command_exception_test "exp3" " "
"Empty";
make_command_exception_test "exp4" ""
"Empty";
make_command_exception_test "exp5" "move a a"
"Malformed";
make_command_exception_test "exp6" "move asdfka"
"Malformed";
make_command_exception_test "exp7" "move a a a"
"Malformed";
make_command_exception_test "exp8" "move a a a a"
"Malformed";
make_command_exception_test "exp9" "capture a a"
"Malformed";
make_command_exception_test "exp10" "capture a a a"
"Malformed";
make_command_exception_test "exp11" "capture"
"Malformed";
make_command_exception_test "exp12" "captur"
"Malformed";
make_command_exception_test "exp13" "hint a3"
"Malformed";
make_command_exception_test "exp14" "hint a b c"
"Malformed";
make_command_exception_test "exp15" "help a "
"Malformed";
make_command_exception_test "exp16" "score a b c"
"Malformed";
]
let cant_move_1 : State.state =
(true, [(("h", "1"), {color = "red"; king = false});
(("g", "2"), {color = "white"; king = false});
(("f", "3"), {color = "white"; king = false})])
let cant_move_2 : State.state =
(true, [(("e", "4"), {color = "red"; king = true});
(("c", "6"), {color = "white"; king = false});
(("g", "6"), {color = "white"; king = false});
(("d", "5"), {color = "white"; king = false});
(("f", "5"), {color = "white"; king = false});
(("d", "3"), {color = "white"; king = false});
(("f", "3"), {color = "white"; king = false});
(("c", "2"), {color = "white"; king = false});
(("g", "2"), {color = "white"; king = false});])
let can_move_1 : State.state =
(true, [(("h", "1"), {color = "red"; king = false});
(("g", "2"), {color = "white"; king = false})])
let can_move_2 : State.state =
(true, [(("h", "1"), {color = "red"; king = false});
(("f", "3"), {color = "white"; king = false})])
let make_cant_move_test
(name : string)
(state : State.state)
(expected_output : bool) : test =
name >:: (fun _ ->
assert_equal expected_output (cant_move state))
let cant_move_tests =
[
make_cant_move_test "cant move 1" cant_move_1 true;
make_cant_move_test "cant move 2" cant_move_2 true;
make_cant_move_test "can move 1" can_move_1 false;
make_cant_move_test "can move 2" can_move_2 false;
]
let can_capture : State.state =
(true, [(("f", "3"), {color = "red"; king = false});
(("e", "4"), {color = "white"; king = false})])
let can_move_no_cap : State.state =
(true, [(("c", "4"), {color = "red"; king = false})])
let can_move_no_cap2 : State.state =
(false, [(("b", "5"), {color = "red"; king = false})])
let must_cap coord_from coord_to state =
try
vmmc coord_from coord_to state
with MustCap -> state
let make_vmmc_test
(name : string)
(coord_from : coordinate)
(coord_to : coordinate)
(state : state)
(expected_output : state) : test =
name >:: (fun _ ->
assert_equal expected_output (vmmc coord_from coord_to state))
let vmmc_tests =
[
make_vmmc_test "can move no cap" ("c", "4") ("b", "5") can_move_no_cap
can_move_no_cap2;
]
let two_piece_board : State.state =
(false, [(("f", "3"), {color = "red"; king = false});
(("g", "4"), {color = "white"; king = false})])
let two_after : State.state =
(true, [(("e", "2"), {color = "white"; king = false})])
let make_easy_AI_test
(name : string)
(state : State.state)
(expected_output : State.state) : test =
name >:: (fun _ ->
assert_equal expected_output (easyAI state))
let easyAI_tests =
[
make_easy_AI_test "little" two_piece_board two_after
]
let suite =
"test suite for checkers" >::: List.flatten [
valid_move_tests;
move_tests;
valid_capture_tests;
capture_tests;
command_tests;
command_exception_tests;
cant_move_tests;
vmmc_tests;
easyAI_tests
]
let _ = run_test_tt_main suite