-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathproblems.js
97 lines (78 loc) · 2.91 KB
/
problems.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
var assert = require('assert');
var fs = require('fs');
var karel = require('../js/karel.js');
var DOMParser = require('xmldom').DOMParser;
var util = require('../js/util.js');
describe('problems', function () {
var problems = fs.readdirSync('test/problems');
problems.forEach(function (problem) {
it(problem, function () {
// 10s timeout per problem.
this.timeout(10000);
var problemDir = 'test/problems/' + problem + '/';
var file = fs.readFileSync(problemDir + 'sol.txt', { encoding: 'utf-8' });
var compiled = karel.compile(file);
fs.readdirSync(problemDir + 'cases').forEach(function (casename) {
if (!casename.endsWith('.in')) return;
var inPath = problemDir + 'cases/' + casename;
var outPath = inPath.slice(0, -3) + '.out';
var worldXml = new DOMParser().parseFromString(
fs.readFileSync(inPath, { encoding: 'utf-8' }),
'text/xml',
);
var world = new karel.World(100, 100);
world.load(worldXml);
world.runtime.load(compiled);
while (world.runtime.step()) {
// Keep going...
}
var output = world.output().replace(/\s+/g, '');
var expectedOutput = fs
.readFileSync(outPath, { encoding: 'utf-8' })
.replace(/\s+/g, '');
assert.equal(output, expectedOutput);
});
});
});
});
describe('draw worlds', function () {
var problems = fs.readdirSync('test/problems');
problems.forEach(function (problem) {
it(problem, function () {
// 10s timeout per problem.
this.timeout(10000);
var problemDir = 'test/problems/' + problem + '/';
var solutionPath = problemDir + 'sol.txt';
var allDoneWriting = fs
.readdirSync(problemDir + 'cases')
.slice(0, 1) // only test one case
.map(function (casename) {
if (!casename.endsWith('.in')) return;
var inPath = problemDir + 'cases/' + casename;
var pngPath = inPath.slice(0, -3) + '.png';
var world = fs.readFileSync(inPath, { encoding: 'utf-8' });
return util
.Draw(world, pngPath, { run: solutionPath })
.finally(() => assert(fs.existsSync(pngPath)));
});
return Promise.all(allDoneWriting);
});
});
});
describe('import old mdo+kec', function () {
var oldCases = fs.readdirSync('test/mdokec');
oldCases.forEach(function (casename) {
if (!casename.endsWith('.in')) return;
it(casename.slice(0, -3), function () {
var inPath = 'test/mdokec/' + casename;
var mdoPath = inPath.slice(0, -3) + '.mdo';
var kecPath = inPath.slice(0, -3) + '.kec';
var world = util.ImportMdoKec(mdoPath, kecPath);
var output = world.save().replace(/\s+/g, '');
var expectedOutput = fs
.readFileSync(inPath, { encoding: 'utf-8' })
.replace(/\s+/g, '');
assert.equal(output, expectedOutput);
});
});
});