-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathtester.js
More file actions
executable file
·188 lines (160 loc) · 4.86 KB
/
tester.js
File metadata and controls
executable file
·188 lines (160 loc) · 4.86 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
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
#!/usr/bin/env node
//var params = getParams({booleans: ["no-multi-threading"]});
var params = require("util").parseArgs({strict: false, allowPositionals: false}).values;
var score;
var depthFound = 0;
var enginePath = params.enginePath ?? require("path").join(__dirname, "src", "stockfish.js");
var execPath = params.execPath ?? process.execPath;
var minDepth = Number((params.minDepth || params["min-depth"]) ?? 20);
var minScore = Number((params.minScore || params["min-score"]) ?? 500);
var delayModifier = Number((params.delayModifier || params["delay-modifier"]) ?? 1); /// Give the tests more (or less) time
var testCompletedCorrectly = false;
var spawnArgs = [];
if (enginePath) {
spawnArgs.push(enginePath);
}
highlight("Starting: " + execPath + (spawnArgs.length ? " " + spawnArgs.join(" ") : ""));
var stockfish = require("child_process").spawn(execPath, spawnArgs, {stdio: "pipe", env: process.env, encoding: "utf8", detached: true, shell: true});
function getParams(options, argv)
{
var i,
params = {_: []},
last,
len,
match;
if (Array.isArray(options)) {
args = options;
options = {};
}
options = options || {};
if (!options.booleans) {
options.booleans = [];
}
argv = argv || process.argv;
len = argv.length;
for (i = 2; i < len; i += 1) {
if (argv[i][0] === "-") {
if (argv[i][1] === "-") {
last = argv[i].substr(2);
match = last.match(/([^=]*)=(.*)/);
if (match) {
last = match[1];
params[last] = match[2];
last = "";
} else {
params[last] = true;
}
} else {
/// E.g., -hav should indicate h, a, and v as TRUE.
argv[i].split("").slice(1).forEach(function oneach(letter)
{
params[letter] = true;
last = letter;
});
}
} else if (last) {
params[last] = argv[i];
last = "";
} else {
params._.push(argv[i]);
last = "";
}
/// Handle booleans.
if (last && options.booleans.indexOf(last) > -1) {
last = "";
}
}
return params;
}
function good(mixed)
{
console.log("\u001B[32m" + mixed + "\u001B[0m");
}
function highlight(mixed)
{
console.log("\u001B[33m" + mixed + "\u001B[0m");
}
function error(mixed)
{
console.error("\u001B[31m" + mixed + "\u001B[0m");
throw new Error(mixed);
}
function write(str)
{
highlight("STDIN: " + str);
stockfish.stdin.write(str + "\n");
}
stockfish.on("error", function (err)
{
throw err;
});
stockfish.stdout.on("data", function onstdout(data)
{
if (typeof data !== "string") {
data = data.toString();
}
process.stdout.write(data);
if (data.indexOf("uciok") > -1) {
good("**Found uciok**");
write("ucinewgame");
write("isready");
write("position startpos moves f2f4 e7e5 g1f3 d8h4");
write("eval");
write("d");
if (!params["no-multi-threading"]) {
write("setoption name Threads value 5");
}
}
if (data.indexOf("score cp ") > -1) {
score = Number(data.match(/score cp ([-\d.]+)/)[1]);
}
if (data.indexOf("depth ") > -1) {
/// When calling stop, the engine may output a shallower depth.
depthFound = Math.max(Number(data.match(/depth ([\d]+)/)[1]), depthFound);
}
if (data.indexOf("Checkers:") > -1) {
if (data.indexOf("Checkers: h4") > -1) {
good("**Found Checkers**");
/// Make sure ponder works.
write("go ponder");
setTimeout(function ()
{
write("stop");
}, 3000 * delayModifier);
} else {
error("Cannot find valid legal uci moves.");
}
}
if (data.indexOf("bestmove") > -1) {
if (data.indexOf("bestmove f3h4") > -1) {
good("**Found bestmove**");
if (score < minScore) {
error("Bad score. Got " + score + "; expected >= " + minScore);
}
if (depthFound < minDepth) {
error("Bad depth. Got " + depthFound + "; expected >= " + minDepth);
}
write("quit");
testCompletedCorrectly = true;
} else {
error("Did not find expected move (h4).");
}
}
});
stockfish.on("exit", function (code)
{
if (code) {
error("Exited with code: " + code);
}
});
setTimeout(function ()
{
error("Timeout");
}, 1000 * 5 * delayModifier).unref();
process.on("exit", function ()
{
if (!testCompletedCorrectly) {
error("Test did not complete correctly.");
}
});
write("uci");