-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.c
702 lines (596 loc) · 15.3 KB
/
tests.c
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/* Travis Wolfe
* December 2009
*/
#include <stdio.h>
#include <stdlib.h>
#include "Board.h"
#include "Util.h"
#define QUIET 0
#define WIN 1
#define FAIL 0
extern int *rf_to_tlbr_start, *rf_to_tlbr_width, *rf_to_tlbr;
extern int *rf_to_bltr_start, *rf_to_bltr_width, *rf_to_bltr;
int test_to_play(Board *board) {
set_play(board, WHITE);
if(to_play(board) != WHITE) {
printf("[tests.test_to_play] failed to set the player\n");
return FAIL;
}
set_play(board, BLACK);
if(to_play(board) != BLACK) {
printf("[tests.test_to_play] failed to set the player\n");
return FAIL;
}
set_play(board, WHITE);
if(to_play(board) != WHITE) {
printf("[tests.test_to_play] failed to set the player\n");
return FAIL;
}
printf("[testing.to_play]\tpassed\n");
return WIN;
}
int test_ply(Board *board) {
set_ply(board, 11);
if(ply(board) != 11) {
printf("[tests.test_ply] failed to set ply\n");
return FAIL;
}
set_ply(board, 1);
if(ply(board) != 1) {
printf("[tests.test_ply] failed to set ply\n");
return FAIL;
}
set_ply(board, 21);
if(ply(board) != 21) {
printf("[tests.test_ply] failed to set ply\n");
return FAIL;
}
set_ply(board, 111);
if(ply(board) != 111) {
printf("[tests.test_ply] failed to set ply\n");
return FAIL;
}
set_ply(board, 21);
if(ply(board) != 21) {
printf("[tests.test_ply] failed to set ply\n");
return FAIL;
}
printf("[testing.ply]\t\tpassed\n");
return WIN;
}
void stupid_tests() {
unsigned long mask;
mask = SQUARE(12);
if(mask != (1L<<12))
printf("[main.stupid] (1)\tyou're failing the stupid tests!\n");
mask = SQUARE(8 + UP);
if(mask != (1L<<16)) {
printf("[main.stupid] (2)\tyou're failing the stupid tests!\n");
printf("up = %d\n", UP);
}
}
int test_file_attacks() {
unsigned long mask, expected;
int status, sq, file;
unsigned long *fattacks = make_file_attacks();
status = WIN;
/* lets assume we queen at D1 (3) and
* pieces at D3 (19) and D5 (35).
* this should leave a move to D2 (11)
* and D3 (19).
*/
sq = 3;
expected = SQUARE(sq+8) | SQUARE(sq+8*2);
file = (1<<2) | (1<<4) | 1;
mask = fattacks[sq*256 + file];
if(mask != expected) {
printf("[tests.file_attacks] (1) expected %d, but got %d, file = %d\n", (int) expected, (int) mask, file);
status = FAIL;
}
/* lets assume we rook at H3 (23) and
* pieces at H2 (15) and H4 (31).
* this should leave moves to H2 (15)
* and H4 (31)
*/
sq = 23;
expected = SQUARE(sq-8) | SQUARE(sq+8);
file = (1<<1) | (1<<2) | (1<<3);
mask = fattacks[sq*256 + file];
if(mask != expected) {
printf("[tests.file_attacks] (2) expected %d, but got %d, file = %d\n", (int) expected, (int) mask, file);
status = FAIL;
}
/* lets assume we rook at A8 (56) and
* a piece at A7 (48).
* this should leave a to A7 (48)
*/
sq = 56;
expected = SQUARE(sq-8);
file = (1<<7) | (1<<6);
mask = fattacks[sq*256 + file];
if(mask != expected) {
printf("[tests.file_attacks] (3) expected %i, but got %i, file = %d\n", (int) expected, (int) mask, file);
status = FAIL;
}
free(fattacks);
return status;
}
int test_rank_attacks() {
unsigned long mask, expected;
int status, sq, rank;
unsigned long *rattacks = make_rank_attacks();
status = WIN;
/* lets assume we queen at D1 (3) and
* pieces at E1 (4) and H1 (7).
* this should leave moves from A1 (0)
* through E1 (4).
*/
sq = 3;
expected = SQUARE(sq+1) | SQUARE(sq-1) | SQUARE(sq-2) | SQUARE(sq-3);
rank = (1<<3) | (1<<4) | (1<<7);
mask = rattacks[sq*256 + rank];
if(mask != expected) {
printf("[tests.rank_attacks] (1) expected %d, but got %d, rank = %d\n", (int) expected, (int) mask, rank);
status = FAIL;
}
/* lets assume we rook at H8 (63) and
* a piece at G8 (62)
* this should leave on move to G8 (62)
*/
sq = 63;
expected = SQUARE(sq-1);
rank = (1<<6) | (1<<7);
mask = rattacks[sq*256 + rank];
if(mask != expected) {
printf("[tests.rank_attacks] (1) expected %d, but got %d, rank = %d\n", (int) expected, (int) mask, rank);
status = FAIL;
}
/* lets assume we rook at C5 (42) and
* no other pieces in that rank
* this should give moves to A5 (40) through
* H5 (47) except C5
*/
sq = 42;
expected = SQUARE(sq-2) | SQUARE(sq-1) | SQUARE(sq+1) | SQUARE(sq+2) | SQUARE(sq+3) | SQUARE(sq+4) | SQUARE(sq+5);
rank = 0;
mask = rattacks[sq*256 + rank];
if(mask != expected) {
printf("[tests.rank_attacks] (1) expected %d, but got %d, rank = %d\n", (int) expected, (int) mask, rank);
status = FAIL;
}
return status;
}
int test_initf() {
/* TODO: test tl_br position maps */
/* TODO: test bl_tr position maps */
return FAIL;
}
int test_tl_br_attacks() {
/* TODO */
return FAIL;
}
int test_knight_attacks() {
/* TODO */
return FAIL;
}
int test_king_attacks() {
/* TODO */
return FAIL;
}
int test_moves() {
Board b;
Move *moves;
int status, num_moves, expected;
status = WIN;
/* on startup, there shouldn't be any king moves */
setup(&b);
moves = gen_moves(&b, &num_moves);
expected = 20;
if(num_moves != expected) {
if(!QUIET) {
if(to_play(&b) == WHITE)
printf("\t>> WHITE to play, ");
else
printf("\t>> BLACK to play, ");
printf("expected %d moves, but saw %d\n", expected, num_moves);
printb(&b);
}
status = FAIL;
}
free(moves);
/* try this config (black moves) */
initf(&b, "rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2");
moves = gen_moves(&b, &num_moves);
expected = 22;
if(num_moves != expected) {
if(!QUIET) {
if(to_play(&b) == WHITE)
printf("\t>> WHITE to play, ");
else
printf("\t>> BLACK to play, ");
printf("expected %d moves, but saw %d\n", expected, num_moves);
printb(&b);
}
status = FAIL;
}
free(moves);
/* try this config (white moves) */
initf(&b, "rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2");
set_play(&b, WHITE);
moves = gen_moves(&b, &num_moves);
expected = 13 + 7 + 1 + 1 + 5; /* pawns, knights, king, queen, bishop */
if(num_moves != expected) {
if(!QUIET) {
if(to_play(&b) == WHITE)
printf("\t>> WHITE to play, ");
else
printf("\t>> BLACK to play, ");
printf("expected %d moves, but saw %d\n", expected, num_moves);
printb(&b);
}
status = FAIL;
}
free(moves);
/* here is one with only rook moves */
initf(&b, "8/8/8/2r5/8/5R2/8/8 b KQkq - 1 2");
moves = gen_moves(&b, &num_moves);
expected = 14;
if(num_moves != expected) {
if(!QUIET) {
if(to_play(&b) == WHITE)
printf("\t>> WHITE to play, ");
else
printf("\t>> BLACK to play, ");
printf("expected %d moves, but saw %d\n", expected, num_moves);
printb(&b);
}
status = FAIL;
}
free(moves);
/* here is one with only rook moves */
initf(&b, "8/2p5/8/2r5/8/5R2/8/8 b KQkq - 1 2");
moves = gen_moves(&b, &num_moves);
expected = 13;
if(num_moves != expected) {
if(!QUIET) {
if(to_play(&b) == WHITE)
printf("\t>> WHITE to play, ");
else
printf("\t>> BLACK to play, ");
printf("expected %d moves, but saw %d\n", expected, num_moves);
printb(&b);
}
status = FAIL;
}
free(moves);
/* here is another one with only rook moves */
initf(&b, "8/2p5/8/2rp4/8/5R2/8/8 b KQkq - 1 2");
moves = gen_moves(&b, &num_moves);
expected = 9;
if(num_moves != expected) {
if(!QUIET) {
if(to_play(&b) == WHITE)
printf("\t>> WHITE to play, ");
else
printf("\t>> BLACK to play, ");
printf("expected %d moves, but saw %d\n", expected, num_moves);
printb(&b);
}
status = FAIL;
}
free(moves);
/* here is one last one with only rook moves */
initf(&b, "8/2p5/8/2rp4/8/2p2R2/8/8 b KQkq - 1 2");
moves = gen_moves(&b, &num_moves);
expected = 7;
if(num_moves != expected) {
if(!QUIET) {
if(to_play(&b) == WHITE)
printf("\t>> WHITE to play, ");
else
printf("\t>> BLACK to play, ");
printf("expected %d moves, but saw %d\n", expected, num_moves);
printb(&b);
}
status = FAIL;
}
free(moves);
/* and again for white... */
initf(&b, "8/2p5/8/2rp4/8/2p2R2/8/8 w KQkq - 1 2");
moves = gen_moves(&b, &num_moves);
expected = 12;
if(num_moves != expected) {
if(!QUIET) {
if(to_play(&b) == WHITE)
printf("\t>> WHITE to play, ");
else
printf("\t>> BLACK to play, ");
printf("expected %d moves, but saw %d\n", expected, num_moves);
printb(&b);
}
status = FAIL;
}
free(moves);
/* try to figure out bishop issue */
initf(&b, "8/3/8/2B5/8/8/8/8 w KQkq - 1 2");
moves = gen_moves(&b, &num_moves);
expected = 11;
if(num_moves != expected) {
if(!QUIET) {
if(to_play(&b) == WHITE)
printf("\t>> WHITE to play, ");
else
printf("\t>> BLACK to play, ");
printf("expected %d moves, but saw %d\n", expected, num_moves);
printb(&b);
}
status = FAIL;
}
free(moves);
return status;
}
int test_conversions() {
int status, expected, x, actual;
status = WIN;
/* test rf_to_tlbr *******************************************************/
x = 34;
actual = rf_to_tlbr[x];
expected = 17;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 35;
actual = rf_to_tlbr[x];
expected = 24;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 7;
actual = rf_to_tlbr[x];
expected = 63;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 56;
actual = rf_to_tlbr[x];
expected = 0;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 6;
actual = rf_to_tlbr[x];
expected = 61;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
/* test rf_to_tlbr_start *************************************************/
x = 56;
actual = rf_to_tlbr_start[x];
expected = 0;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 0;
actual = rf_to_tlbr_start[x];
expected = 28;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 16+2;
actual = rf_to_tlbr_start[x];
expected = 28;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 1;
actual = rf_to_tlbr_start[x];
expected = 36;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 2;
actual = rf_to_tlbr_start[x];
expected = 43;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 11;
actual = rf_to_tlbr_start[x];
expected = 43;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
/* test rf_to_tlbr_width *************************************************/
x = 11;
actual = rf_to_tlbr_width[x];
expected = 6;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 0;
actual = rf_to_tlbr_width[x];
expected = 8;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 1;
actual = rf_to_tlbr_width[x];
expected = 7;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 8;
actual = rf_to_tlbr_width[x];
expected = 7;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 17;
actual = rf_to_tlbr_width[x];
expected = 7;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
/* test tlbr_to_rf *******************************************************/
x = 0;
actual = tlbr_to_rf[x];
expected = 56;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 1;
actual = tlbr_to_rf[x];
expected = 48;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 2;
actual = tlbr_to_rf[x];
expected = 57;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 63;
actual = tlbr_to_rf[x];
expected = 7;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
x = 62;
actual = tlbr_to_rf[x];
expected = 15;
if(actual - expected) {
printf("expected %d, but got %d\n", expected, actual);
status = FAIL;
}
/*************************************************************************/
return status;
}
int test_apply_move() {
int status, num_moves, i;
Board board;
Move *moves;
status = WIN;
num_moves = 0;
setup(&board);
printb(&board);
moves = gen_moves(&board, &num_moves);
printf("found %d moves\n", num_moves);
for(i=0; i<35 && i<num_moves; i++) {
printf("move %d: %d is moving from %d to %d, capturing %d\n",
i, moving_type(moves[i]), moves[i].from,
moves[i].to, capturing_type(moves[i]));
printf("board after move %d looks like:\n", i+1);
apply_move(&board, moves[i]);
printb(&board);
undo_move(&board, moves[i]);
}
return status;
}
int test_search() {
Board b;
int status, depth, val;
status = WIN;
setup(&b);
depth = 4;
val = scout(&b, -20000, 20000, depth);
printf("negascout after depth %d came out as %d\n", depth, val);
return WIN;
}
int test_scout() {
Board b, orig;
Move *moves, *omoves;
int status, i, j, n, m;
status = WIN;
printf("testing scout\n");
setup(&b);
setup(&orig);
printb(&b);
moves = gen_moves(&b, &n);
for(i=0; i<n && i<5; i++) {
printf("=============================================\napplying move %d\n", i+1);
apply_move(&b, moves[i]);
omoves = gen_moves(&b, &m);
for(j=0; j<m && j<5; j++) {
printf("second move %d\n", j+1);
apply_move(&b, omoves[j]);
printb(&b);
printf("eval = %d\n", eval(&b));
undo_move(&b, omoves[j]);
}
free(omoves);
undo_move(&b, moves[i]);
}
free(moves);
return status;
}
int main(int argc, const char * argv[]) {
get_ready();
/*
stupid_tests();
printf("[tests.main]\tsanity check: sizeof(unsigned long) = %d\n",
(int) sizeof(unsigned long));
printf("[tests.main]\trunning tests....\n");
Board board;
setup(&board);
printf("\n");
printb(&board);
printf("\n");
test_to_play(&board);
test_ply(&board);
if(WIN == test_file_attacks())
printf("[tests.file_attacks]\tpassed\n");
else printf("[tests.file_attacks]\tFAILED!\n");
if(WIN == test_rank_attacks())
printf("[tests.rank_attacks]\tpassed\n");
else printf("[tests.rank_attacks]\tFAILED!\n");
if(WIN == test_initf())
printf("[tests.initf]\t\tpassed\n");
else printf("[tests.initf]\t\tFAILED!\n");
if(WIN == test_tl_br_attacks())
printf("[tests.tl_br_attacks]\tpassed\n");
else printf("[tests.tl_br_attacks]\tFAILED!\n");
if(WIN == test_knight_attacks())
printf("[tests.knight_attacks]\tpassed\n");
else printf("[tests.knight_attacks]\tFAILED!\n");
if(WIN == test_king_attacks())
printf("[tests.king_attacks]\tpassed\n");
else printf("[tests.king_attacks]\tFAILED!\n");
if(WIN == test_moves())
printf("[tests.moves]\t\tpassed\n");
else printf("[tests.moves]\t\tFAILED!\n");
if(WIN == test_conversions())
printf("[tests.conversions]\tpassed\n");
else printf("[tests.conversions]\tFAILED!\n");
if(WIN == test_apply_move())
printf("[tests.apply_move]\tpassed\n");
else printf("[tests.apply_move]\tFAILED!\n");
*/
if(WIN == test_search())
printf("[tests.search]\t\tpassed\n");
else printf("[tests.search]\t\tFAILED!\n");
/* test_scout(); */
clean_up();
printf("\n[tests.main]\t\ttests complete\n");
return EXIT_SUCCESS;
}