-
Notifications
You must be signed in to change notification settings - Fork 11
/
tests.js
380 lines (273 loc) · 8.43 KB
/
tests.js
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
if (typeof require != "undefined") {
var chai = require('chai');
var Draughts = require('./draughts').Draughts;
}
var assert = chai.assert;
describe("Perft", function() {
var perfts = [
];
perfts.forEach(function(perft) {
var draughts = new Draughts();
draughts.load(perft.fen);
it(perft.fen, function() {
var nodes = draughts.perft(perft.depth);
assert(nodes == perft.nodes);
});
});
});
describe("Single Square Move Generation", function() {
var positions = [
];
positions.forEach(function(position) {
var draughts = new Draughts();
draughts.load(position.fen);
it(position.fen + ' ' + position.square, function() {
var moves = draughts.moves({square: position.square, verbose: position.verbose});
var passed = position.moves.length == moves.length;
for (var j = 0; j < moves.length; j++) {
if (!position.verbose) {
passed = passed && moves[j] == position.moves[j];
} else {
for (var k in moves[j]) {
passed = passed && moves[j][k] == position.moves[j][k];
}
}
}
assert(passed);
});
});
});
describe("Insufficient Material", function() {
var positions = [
];
positions.forEach(function(position) {
var draughts = new Draughts();
draughts.load(position.fen);
it(position.fen, function() {
if (position.draw) {
assert(draughts.insufficient_material() && draughts.in_draw());
} else {
assert(!draughts.insufficient_material() && !draughts.in_draw());
}
});
});
});
describe("Threefold Repetition", function() {
var positions = [
];
positions.forEach(function(position) {
var draughts = new Draughts();
draughts.load(position.fen);
it(position.fen, function() {
var passed = true;
for (var j = 0; j < position.moves.length; j++) {
if (draughts.in_threefold_repetition()) {
passed = false;
break;
}
draughts.move(position.moves[j]);
}
assert(passed && draughts.in_threefold_repetition() && draughts.in_draw());
});
});
});
describe("Get/Put/Remove", function() {
var draughts = new Draughts();
var passed = true;
var positions = [
];
positions.forEach(function(position) {
passed = true;
draughts.clear();
it("position should pass - " + position.should_pass, function() {
/* places the pieces */
for (var square in position.pieces) {
passed &= draughts.put(position.pieces[square], square);
}
/* iterate over every square to make sure get returns the proper
* piece values/color
*/
for (var j = 0; j < draughts.SQUARES.length; j++) {
var square = draughts.SQUARES[j];
if (!(square in position.pieces)) {
if (draughts.get(square)) {
passed = false;
break;
}
} else {
var piece = draughts.get(square);
if (!(piece &&
piece.type == position.pieces[square].type &&
piece.color == position.pieces[square].color)) {
passed = false;
break;
}
}
}
if (passed) {
/* remove the pieces */
for (var j = 0; j < draughts.SQUARES.length; j++) {
var square = draughts.SQUARES[j];
var piece = draughts.remove(square);
if ((!(square in position.pieces)) && piece) {
passed = false;
break;
}
if (piece &&
(position.pieces[square].type != piece.type ||
position.pieces[square].color != piece.color)) {
passed = false;
break;
}
}
}
/* finally, check for an empty board */
passed = passed && (draughts.fen() == '8/8/8/8/8/8/8/8 w - - 0 1');
/* some tests should fail, so make sure we're supposed to pass/fail each
* test
*/
passed = (passed == position.should_pass);
assert(passed);
});
});
});
describe("FEN", function() {
var positions = [
];
positions.forEach(function(position) {
var draughts = new Draughts();
it(position.fen + ' (' + position.should_pass + ')', function() {
draughts.load(position.fen);
assert(draughts.fen() == position.fen == position.should_pass);
});
});
});
describe("PDN", function() {
var passed = true;
var error_message;
var positions = [
];
positions.forEach(function(position, i) {
it(i, function() {
var draughts = ("starting_position" in position) ? new Draughts(position.starting_position) : new Draughts();
passed = true;
error_message = "";
for (var j = 0; j < position.moves.length; j++) {
if (draughts.move(position.moves[j]) === null) {
error_message = "move() did not accept " + position.moves[j] + " : ";
break;
}
}
draughts.header.apply(null, position.header);
var pdn = draughts.pdn({max_width:position.max_width, newline_char:position.newline_char});
var fen = draughts.fen();
passed = pdn === position.pdn && fen === position.fen;
assert(passed && error_message.length == 0);
});
});
});
describe("Load PDN", function() {
var draughts = new Draughts();
var tests = [
];
var newline_chars = ['\n', '<br />', '\r\n', 'BLAH'];
tests.forEach(function(t, i) {
newline_chars.forEach(function(newline, j) {
it(i + String.fromCharCode(97 + j), function() {
var result = draughts.load_pdn(t.pdn.join(newline), { newline_char: newline });
var should_pass = t.expect;
/* some tests are expected to fail */
if (should_pass) {
/* some pdn's tests contain comments which are stripped during parsing,
* so we'll need compare the results of the load against a FEN string
* (instead of the reconstructed pdn [e.g. test.pdn.join(newline)])
*/
if ('fen' in t) {
assert(result && draughts.fen() == t.fen);
} else {
assert(result && draughts.pdn({ max_width: 65, newline_char: newline }) == t.pdn.join(newline));
}
} else {
/* this test should fail, so make sure it does */
assert(result == should_pass);
}
});
});
});
// special case dirty file containing a mix of \n and \r\n
it('dirty pdn', function() {
var pdn;
var result = draughts.load_pdn(pdn, { newline_char: '\r?\n' });
assert(result);
assert(draughts.load_pdn(pdn));
assert(draughts.pdn().match(/^\[\[/) === null);
});
});
describe("Make Move", function() {
var positions = [
];
positions.forEach(function(position) {
var draughts = new Draughts();
draughts.load(position.fen);
it(position.fen + ' (' + position.move + ' ' + position.legal + ')', function() {
var result = draughts.move(position.move);
if (position.legal) {
assert(result
&& draughts.fen() == position.next
&& result.captured == position.captured);
} else {
assert(!result);
}
});
});
});
describe("Validate FEN", function() {
var draughts = new Draughts();
var positions = [
];
positions.forEach(function(position) {
it(position.fen + ' (valid: ' + (position.error_number == 0) + ')', function() {
var result = draughts.validate_fen(position.fen);
assert(result.error_number == position.error_number, result.error_number);
});
});
});
describe("History", function() {
var draughts = new Draughts();
var tests = [
];
tests.forEach(function(t, i) {
var passed = true;
it(i, function() {
draughts.reset();
for (var j = 0; j < t.moves.length; j++) {
draughts.move(t.moves[j])
}
var history = draughts.history({verbose: t.verbose});
if (t.fen != draughts.fen()) {
passed = false;
} else if (history.length != t.moves.length) {
passed = false;
} else {
for (var j = 0; j < t.moves.length; j++) {
if (!t.verbose) {
if (history[j] != t.moves[j]) {
passed = false;
break;
}
} else {
for (var key in history[j]) {
if (history[j][key] != t.moves[j][key]) {
passed = false;
break;
}
}
}
}
}
assert(passed);
});
});
});
describe('Regression Tests', function() {
});