-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerateTests.js
90 lines (82 loc) · 2.5 KB
/
generateTests.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
const fs = require('fs');
const stream = fs.createWriteStream("index.test.js");
stream.write('const findBestSpot = require("./index.js");\n\n');
function createRandomTest(title) {
const a = getRandomInt(100);
const b = getRandomInt(100);
const x = getRandomInt(a);
const y = getRandomInt(b);
const n = getRandomInt(a*b);
createPrebuildTest(a,b,x,y,n, `random test ${id}`);
}
let count = 0;
function createPrebuildTest(a, b, x, y, n, title) {
let list = n
if(!Array.isArray(n)) {
list = []
const map = [];
for(i=0; i<n; i++) {
const coord = {x: getRandomInt(a), y: getRandomInt(b)};
if(!map[coord.x]) map[coord.x] = [];
if(!map[coord.x][coord.y]) {
map[coord.x][coord.y] = true;
list.push(coord);
}
}
}
const result = resolve(a,b,x,y,list);
stream.write(`
describe('test ${++count}',() => {
test('${title}', () => {
expect(findBestSpot(${a}, ${b}, ${x}, ${y}, ${JSON.stringify(list)})).toEqual(${JSON.stringify(result)});
});
});
`);
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function resolve(a, b, x, y, list) {
const map = [];
list.forEach(mine => {
map[mine.x] = map[mine.x] || [];
map[mine.x][mine.y] = true;
});
let best = {
coordinates: {x: 0, y: 0},
goldMines: 0,
}
for(i=0; i<=a-x; i++) {
for(j=0; j<=b-y; j++) {
const result = {
coordinates : {x: i, y: j},
goldMines : 0,
}
for(k=0; k<x; k++) {
for(l=0; l<y; l++) {
if(map[k+i] && map[k+i][l+j]) result.goldMines++;
}
}
if(result.goldMines>best.goldMines) best = result;
}
}
return best;
}
createPrebuildTest(1, 1, 1, 1, 1, 'one space, one mine');
createPrebuildTest(1, 1, 1, 1, 0, 'one space, no mine');
createPrebuildTest(1, 1, 0, 0, 1, 'no money');
createPrebuildTest(1, 1, 1, 0, 1, 'no money 2');
createPrebuildTest(1, 1, 0, 1, 1, 'no money 3');
createPrebuildTest(0, 0, 0, 0, 0, 'no land');
createPrebuildTest(0, 1, 1, 1, 1, 'no land 2');
createPrebuildTest(1, 0, 1, 1, 1, 'no land 3');
createPrebuildTest(1, 2, 1, 2, 1, 'two spaces, one mine');
createPrebuildTest(2, 2, 1, 1, 8, 'multiple options');
createPrebuildTest(1, 10, 1, 3, 10, 'one row');
createPrebuildTest(10, 1, 3, 1, 10, 'one column');
createPrebuildTest(100, 100, 25, 25, 100, 'large numbers');
createPrebuildTest(5, 5, 3, 3, [{x: 2, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}, {x: 3, y: 2}], 'star');
for(id=0;id<20;id++) {
createRandomTest(id);
}
stream.close()