-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-driver.js
515 lines (447 loc) · 16.6 KB
/
test-driver.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
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
//Exports test driver code used by various transfer methods.
//
// test(method, offsets, firsts, orders, limit, options = {})
// calls method(offsets, firsts, orders, limit, xfer)
// checks all calls to xfer to make sure racking/slack are obeyed (throws if not)
// checks that all stitches get to their destinations
// will throw if not,
// will also throw if first-marked stitch did not reach destination (unless options.ignoreFirsts)
// will also throw if stacked stitches are transferred (unless options.ignoreFirsts)
// will also throw if empty needles are transferred (unless options.ignoreEmpty)
// returns an object:
// ret = {
// log:["xfer f0 b0", ...], //list of xfer operations
// invalidFirsts:0, //number of invalid first stitches, 0 unless options.ignoreFirsts is set
// xferredStacks:0, //number of stacked stitches transferred, 0 unless options.ignoreStacks is set
// xferredEmpty:0, //number of empty needles transferred, 0 unless options.ignoreEmpty is set
// }
//
// runTests(method, options = {})
// reads tests specified on the command line
// if options.outDir is set, creates options.outDir if needed, then:
// writes test logs into options.outDir + '/' + test_case_name for successful tests
// if options.skipCables is set, does not attempt test cases with cables
// if options.skipLace is set, does not attempt test cases with lace (== stacking loops)
// if options.skipLong is set, does not attempt test cases with offsets outside of maxTransfer
// if options.skipFinished is set, does not attempt test cases with offsets of all zero
// if options.halfGauge is set, runs problem in half gauge. sets normal needles to even indices and slides to odd
// options.ignoreFirsts, options.ignoreStacks are passed to test
//
// returns an object:
// ret = {
// ran:52,
// passed:50,
// failed:2,
// skipped:105
// }
function test(method, offsets, firsts, orders, limit, options) {
//validate parameters:
//this lets you pass strings to test for offsets/firsts/orders, which is handy for by-hand-written test cases:
if (typeof(offsets) === "string") {
let parsed = [];
offsets.split(/\s+/).forEach(function(t){
if (t === '') return;
parsed.push(parseInt(t));
});
offsets = parsed;
}
if (typeof(firsts) === "string") {
let parsed = [];
firsts.split(/\s+/).forEach(function(t){
if (t === '') return;
else if (t === '*') parsed.push(true);
else if (t === '.') parsed.push(false);
else console.error("Unexpected 'firsts' character '" + t + "'");
});
firsts = parsed;
}
if (typeof(orders) === "string") {
let parsed = [];
orders.split(/\s+/).forEach(function(t){
if (t === '') return;
else if (t === '+') parsed.push(1);
else if (t === '.') parsed.push(0);
else if (t === '-') parsed.push(-1);
else console.error("Unexpected 'orders' character '" + t + "'");
});
orders = parsed;
}
//basic parameter validation:
console.assert(typeof(method) === 'function', "method parameter should be a function");
console.assert(Array.isArray(offsets), "offsets parameter should be an array");
console.assert(Array.isArray(firsts), "firsts parameter should be an array");
console.assert(Array.isArray(orders), "orders parameter should be an array");
console.assert(Number.isInteger(limit) && limit > 0, "limit should be a positive integer");
if (typeof(options) === 'undefined') options = {};
console.assert(typeof(options) === 'object', "options parameter should be an object");
console.assert(offsets.length === firsts.length, "offsets and firsts should be the same length");
console.assert(offsets.length === orders.length, "offsets and orders should be the same length");
const ignoreFirsts = Boolean(options.ignoreFirsts);
const ignoreStacks = Boolean(options.ignoreStacks);
const ignoreEmpty = Boolean(options.ignoreEmpty);
const halfGauge = Boolean(options.halfGauge)
//----------------------------------
function offsetsToString(offsets) {
let info = "";
for (let i = 0; i < offsets.length; ++i) {
if (i !== 0) info += " ";
if (offsets[i] < 0) info += offsets[i];
else if (offsets[i] > 0) info += "+" + offsets[i];
else info += " 0";
}
return info;
}
//will track positions of each loop by using this needles object:
let needles = {};
for (let i = 0; i < offsets.length; ++i) {
if (halfGauge) {
needles['f' + (2*i)] = [i];
} else {
needles['f' + i] = [i];
}
}
function dumpNeedles() {
let minNeedle = 0;
let maxNeedle = halfGauge ? 2*offsets.length-1: offsets.length-1;
for (let n in needles) {
let m = n.match(/^[fb]s?(-?\d+)$/);
console.assert(m);
let val = parseInt(m[1]);
minNeedle = Math.min(minNeedle, val);
maxNeedle = Math.max(maxNeedle, val);
}
[
['b', ' back:'],
['bs', ' backSl:'],
['fs', 'frontSl:'],
['f', ' front:'],
].forEach(function(bedName) {
const bed = bedName[0];
const name = bedName[1];
let layers = [];
for (let n = minNeedle; n <= maxNeedle; ++n) {
if ((bed + n) in needles) {
needles[bed + n].forEach(function(i, d){
while (d >= layers.length) layers.push("");
while (layers[d].length < (n - minNeedle) * 3) layers[d] += " ";
layers[d] += " ";
if (i < 10) layers[d] += " " + i;
else layers[d] += i;
});
}
}
for (let l = layers.length - 1; l >= 0; --l) {
console.log(name + layers[l]);
}
});
let infoI = "";
for (let n = minNeedle; n <= maxNeedle; ++n) {
if (n < -10) infoI += n.toString();
else if (n < 0) infoI += " " + n.toString();
else if (n === 0) infoI += " 0";
else if (n < 10) infoI += " " + n.toString();
else infoI += " " + n.toString();
}
console.log(" index:" + infoI);
}
let log = [];
let xferredStacks = 0;
let xferredEmpty = 0;
function xfer(fromBed, fromIndex, toBed, toIndex) {
let cmd = "xfer " + fromBed + fromIndex + " " + toBed + toIndex;
console.log(cmd);
if (halfGauge) {
fromIndex = fromBed.length === 1 ? 2*fromIndex : 2*fromIndex+1;
toIndex = toBed.length === 1 ? 2*toIndex : 2*toIndex+1;
fromBed = fromBed[0];
toBed = toBed[0];
}
cmd = "xfer " + fromBed + fromIndex + " " + toBed + toIndex;
console.log(cmd);
log.push(cmd);
console.assert(
["f>b","f>bs","fs>b","b>f","b>fs","bs>f"].indexOf(fromBed + '>' + toBed) !== -1,
"must xfer f[s] <=> b[s]");
//check for valid racking:
let at = [];
for (let i = 0; i < offsets.length; ++i) {
at.push(null);
}
for (var n in needles) {
let m = n.match(/^([fb]s?)(-?\d+)$/);
console.assert(m);
needles[n].forEach(function(s){
console.assert(at[s] === null, "each stitch can only be in one place");
at[s] = {bed:m[1], needle:parseInt(m[2])};
});
}
let minRacking = -Infinity;
let maxRacking = Infinity;
for (let i = 1; i < offsets.length; ++i) {
if (at[i-1].bed[0] === at[i].bed[0]) continue;
let slack = halfGauge ? Math.max(3, 1+2*Math.abs(1+offsets[i]-offsets[i-1])) : Math.max(1, Math.abs( i+offsets[i] - (i-1+offsets[i-1]) ));
let back = (at[i].bed[0] === 'b' ? at[i].needle : at[i-1].needle);
console.log('b'+back);
let front = (at[i].bed[0] === 'b' ? at[i-1].needle : at[i].needle);
console.log('f'+front);
//-slack <= back + racking - front <= slack
minRacking = Math.max(minRacking, -slack - back + front);
maxRacking = Math.min(maxRacking, slack - back + front);
}
console.assert(minRacking <= maxRacking, "there is a valid racking for this stitch configuration");
let racking = (fromBed[0] === 'f' ? fromIndex - toIndex : toIndex - fromIndex);
console.assert(minRacking <= racking && racking <= maxRacking, "required racking " + racking + " is outside [" + minRacking + ", " + maxRacking + "] valid range. (" + cmd + ")");
var from = needles[fromBed + fromIndex];
if (!((toBed + toIndex) in needles)) needles[toBed + toIndex] = [];
var to = needles[toBed + toIndex];
if (from.length === 0) {
++xferredEmpty;
if (!ignoreEmpty) {
console.assert(from.length !== 0, "no reason to xfer empty needle");
}
}
if (from.length > 1) {
++xferredStacks;
if (!ignoreStacks) {
console.assert(from.length === 1, "shouldn't ever xfer stack");
}
}
while (from.length) to.push(from.pop());
dumpNeedles(); //DEBUG
}
let infoI = "";
let infoO = "";
let infoF = "";
for (let i = 0; i < offsets.length; ++i) {
infoI += " ";
infoO += " ";
infoF += " ";
if (i < 10) infoI += " " + i;
else infoI += i;
if (offsets[i] < 0) infoO += offsets[i];
else if (offsets[i] > 0) infoO += "+" + offsets[i];
else infoO += " 0";
if (firsts[i]) {
infoF += " *";
} else {
infoF += " .";
}
}
console.log(" index:" + infoI);
console.log("offset:" + infoO);
console.log(" first:" + infoF);
method(offsets, firsts, orders, limit, xfer);
dumpNeedles();
let invalidFirsts = 0;
for (let i = 0; i < offsets.length; ++i) {
var n = halfGauge ? needles['f' + 2*(i + offsets[i])]: needles['f' + (i + offsets[i])];
if (n.indexOf(i) === -1) throw "loop on " + ('f' + i) + " did not reach " + ('f' + (i + offsets[i]));
//console.assert(n.indexOf(i) !== -1, "needle got to destination");
if (firsts[i]) {
if (n.indexOf(i) !== 0) {
invalidFirsts += 1;
if (!ignoreFirsts) {
throw "loop on " + ('f' + i) + " did not reach " + ('f' + (i + offsets[i])) + " first";
}
}
}
}
console.log(log.length + " transfers, avg " + (log.length/offsets.length) + " per needle.");
return {
invalidFirsts:invalidFirsts,
xferredStacks:xferredStacks,
xferredEmpty:xferredEmpty,
log:log,
};
}
function runTests(method, options) {
//check parameters:
console.assert(typeof(method) === 'function', "method parameter should be a function");
if (typeof(options) === 'undefined') options = {};
console.assert(typeof(options) === 'object', "options parameter should be an object or empty");
const path = require('path');
const fs = require('fs');
if ('outDir' in options) {
console.assert(typeof(options.outDir) === 'string', "options.outDir should be a string");
let dirs = options.outDir.split(path.sep);
for (let i = 1; i <= dirs.length; ++i) {
let sub = path.join(...dirs.slice(0,i));
try {
fs.mkdirSync(sub); //will this error if subdir exists?
} catch (e) {
if (e.code === 'EEXIST') {
//okay; directory already exists.
} else {
throw e;
}
}
}
}
const skipCables = ('skipCables' in options ? Boolean(options.skipCables) : false);
const skipLace = ('skipLace' in options ? Boolean(options.skipLace) : false);
const skipLong = ('skipLong' in options ? Boolean(options.skipLong) : false);
const skipFinished = ('skipFinished' in options ? Boolean(options.skipFinished) : false);
console.log("skipCables: " + skipCables); //DEBUG
const testOptions = {};
if (options.ignoreFirsts) testOptions.ignoreFirsts = true;
if (options.ignoreStacks) testOptions.ignoreStacks = true;
if (options.ignoreEmpty) testOptions.ignoreEmpty = true;
//------- actual testing code ------
let stats = {
total:0, //total test cases examined
ran:0, //test cases that were run
passed:0, //test cases that were run and passed
passedXferredStacks:0, //passed but had some xferred stacks
passedInvalidFirsts:0, //passed but had some invalid firsts
passedXferredEmpty:0, //passed but xferred some empty needles
failed:0, //test cases that were run and failed
skipped:0, //test cases that were skipped
skippedCables:0,
skippedLace:0,
skippedLong:0,
skippedFinished:0,
invalid:0 //test cases that were skipped because the file was malformed
};
let curr_arg = 2;
if (process.argv[curr_arg]=="-h") {
console.log("expand offsets to half gauge");
options.halfGauge = true;
++curr_arg;
}
for (let i = curr_arg; i < process.argv.length; ++i) {
let name = process.argv[i];
if (name.endsWith(path.sep)) name = name.substr(0,name.length-1);
const lstats = fs.lstatSync(name);
let files;
if (lstats.isDirectory()) {
files = [];
fs.readdirSync(name).forEach(function(filename){
files.push(name + "/" + filename);
});
} else {
files = [name];
}
files.forEach(function(filename){
stats.total += 1;
console.log(filename + " ...");
let data = null;
try {
data = JSON.parse(fs.readFileSync(filename));
if (!(Number.isInteger(data.transferMax) && data.transferMax >= 1)) throw "Invalid transferMax";
if (!Array.isArray(data.offsets)) throw "Invalid offsets";
if (!Array.isArray(data.firsts) || data.offsets.length !== data.firsts.length) throw "Invalid firsts";
if (!Array.isArray(data.orders) || data.offsets.length !== data.orders.length) throw "Invalid order";
for (let i = 0; i < data.offsets.length; ++i) {
if (!Number.isInteger(data.offsets[i])) throw "Invalid offset: " + data.offsets[i];
if (!Number.isInteger(data.orders[i])) throw "Invalid order: " + data.orders[i];
data.firsts[i] = Boolean(data.firsts[i]); //normalize firsts
}
} catch (e) {
console.log(e);
stats.skipped += 1;
stats.invalid += 1;
return;
}
//Check inputs:
let maxRacking = data.transferMax;
let hasLong = false; //stitches with |offset| > maxRacking
let hasCables = false; //stitches that cross other stitches
let hasLace = false; //stitches that stack on other stitches
let isFinished = true; //clear out examples that are all zero offset and thus already done
let targets = {}; //for hasLace checking
for (let i = 0; i < data.offsets.length; ++i) {
if (Math.abs(data.offsets[i]) > maxRacking) {
hasLong = true;
}
if (i > 0 && (i-1 + data.offsets[i-1] > i + data.offsets[i])) {
hasCables = true;
}
if (data.offsets[i] != 0) {
isFinished = false;
}
let t = (i + data.offsets[i]).toString();
if (!(t in targets)) targets[t] = [];
targets[t].push(i);
if (targets[t].length > 1) hasLace = true;
}
if (hasLace && skipLace) {
stats.skipped += 1;
stats.skippedLace += 1;
return;
}
if (hasCables && skipCables) {
stats.skipped += 1;
stats.skippedCables += 1;
return;
}
if (hasLong && skipLong) {
stats.skipped += 1;
stats.skippedLong += 1;
return;
}
if (isFinished && skipFinished) {
stats.skipped += 1;
stats.skippedFinished += 1;
return;
}
//-------------- actually do the test ------------
stats.ran += 1;
let test_log;
try {
test_log = test(method, data.offsets, data.firsts, data.orders, data.transferMax, testOptions);
if (test_log.xferredStacks > 0) {
stats.passedXferredStacks += 1;
}
if (test_log.invalidFirsts > 0) {
stats.passedInvalidFirsts += 1;
}
if (test_log.xferredEmpty > 0) {
stats.passedXferredEmpty += 1;
}
} catch (e) {
console.log(e);
stats.failed += 1;
if (options.rethrow) throw e;
return;
}
stats.passed += 1;
if (options.outDir) {
//generate transfers file
let transfer_name = path.basename(filename, '.xfers');
console.log(options.outDir);
let results_file = path.join(options.outDir, transfer_name + '.xout');
if (options.halfGauge) {
data.transferMax = (data.transferMax*2)+1;
for (let i = 0; i < data.orders.length; i++) {
data.offsets[i] = data.offsets[i]*2;
}
}
//NOTE: explicitly building an object to avoid leaking any extra fields through from data
let result_string = ";" + JSON.stringify({
offsets:data.offsets,
firsts:data.firsts,
orders:data.orders,
transferMax:data.transferMax,
xferredStacks:test_log.xferredStacks,
xferredEmpty:test_log.xferredEmpty
}) + '\n' + test_log.log.join('\n') + '\n';
fs.writeFileSync(results_file, result_string, 'utf8');
}
});
}
console.log("Of " + stats.total + " test cases:\n"
+ " Skipped " + stats.skipped + " tests, " + stats.invalid + " because of invalid file contents.\n"
+ (stats.skippedCables ? " (" + stats.skippedCables + " of which had cables)\n" : "")
+ (stats.skippedLace ? " (" + stats.skippedLace + " of which had lace)\n" : "")
+ (stats.skippedLong ? " (" + stats.skippedLong + " of which were long)\n" : "")
+ (stats.skippedFinished ? " (" + stats.skippedFinished + " of which were already done)\n" : "")
+ " Ran " + stats.ran + " tests, of which\n"
+ " " + stats.passed + " passed\n"
+ (stats.passedXferredStacks ? " (" + stats.passedXferredStacks + " of which transferred stacks)\n" : "")
+ (stats.passedXferredEmpty ? " (" + stats.passedXferredEmpty + " of which transferred empty needles)\n" : "")
+ (stats.passedInvalidFirsts ? " (" + stats.passedInvalidFirsts + " of which had invalid firsts)\n" : "")
+ " " + stats.failed + " failed.");
return stats;
}
module.exports.test = test;
module.exports.runTests = runTests;