-
Notifications
You must be signed in to change notification settings - Fork 23
/
TestNumberPlaceGame.js
278 lines (249 loc) · 9.27 KB
/
TestNumberPlaceGame.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
/*=============== Utils Test ===============*/
QUnit.module("Class Utils", {
before: () => {
this.board = new Board();
for(let i = 0; i < 9; i++) {
this.board.grids[i].setValue(i+1);
}
}
// beforeEach: assert => {
// },
// afterEach: assert => {
// }
});
QUnit.test("isGridsValid", (assert) => {
let grids = new Array(9);
for (let i = 0; i < 9; i++) {
grids[i] = (i+1);
}
let valid = Utils.isGridsValueValid(grids);
assert.equal( valid, true, "grids value is valid!");
let i = Math.floor( Math.random() * 9 )
grids[i] = 10;
valid = Utils.isGridsValueValid(grids);
assert.equal( valid, false, "grids value is not valid!");
});
QUnit.test("randomArray", assert => {
let basic = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let values = Utils.randomArray( basic )
assert.equal(values.length, 9, `Randomized the input array, result: ${values}`);
});
QUnit.test("distinctArray", assert => {
let basic, values, result;
result = true;
basic = [1, 3, 1, 2, 3, 6, 8];
values = Utils.distinctArray( basic );
values.sort( (a, b) => {
return a > b;
})
for(let i = 0; i < values.length - 1; i++) {
result &= (values[i] < values[i+1]);
}
assert.ok( result, `Array is distincted: ${values}` )
basic = [1, 2, 3, 6, 8];
values = Utils.distinctArray( basic );
result = true;
values.sort( (a, b) => {
return a > b;
})
for(let i = 0; i < values.length - 1; i++) {
result &= (values[i] < values[i+1]);
}
assert.ok( result, `Array is distincted: ${values}` )
});
QUnit.test("getRandomValue", (assert) => {
let values = Utils.getRandomValue();
let result = Utils.isGridsValueValid(values);
assert.ok( result, `All array values are expected to not equal, array is ${values}` );
let exclude = [1, 3, 6, 8];
let remainValues = Utils.getRandomValue({exclude});
result = Utils.isGridsValueValid(remainValues);
assert.equal( remainValues.length, 5, "Count of emained elements of value array is right" )
assert.equal( result, false, `All array values are expected to not equal, array is ${remainValues}` );
exclude = [1, 3, 3, 6, 8];
remainValues = Utils.getRandomValue({exclude});
result = Utils.isGridsValueValid(remainValues);
assert.equal( remainValues.length, 5, "Count of emained elements of value array is right" );
assert.equal( result, false, `All array values are expected to not equal, array is ${remainValues}` );
});
/*=============== Utils Test ===============*/
/*=============== Grid Test ===============*/
QUnit.module("Class Grid", {
before: () => {
this.grid = new Grid(0, 0);
this.times = 10;
}
});
QUnit.test("isRight", assert => {
let uvalue = 0;
let value = 0;
for(let i = 0; i < this.times; i++) {
uvalue = Math.floor( Math.random() * 9 );
value = Math.floor( Math.random() * 9 );
this.grid.setValue(value);
this.grid.placeValue(uvalue);
assert.equal(this.grid.value, value, `Set Grid Value(${value}) is OK`);
assert.equal(this.grid.userValue, uvalue, `Set Grid userValue(${uvalue}) is OK`);
assert.equal(this.grid.isRight(), uvalue === value, `User Place Value (${uvalue} === ${value}) set Right`);
}
});
QUnit.test("isValidValue", assert => {
let v = 0;
let valid;
let number = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for(let i = 0; i < this.times*3; i++) {
v = Math.floor( Math.random() * this.times*3 );
valid = number.includes(v);
assert.equal( Grid.isValidValue(v), valid, `Random Value ${v} valid: ${valid}`);
}
});
QUnit.test("blockBelonged", assert => {
let bRow, bCol, rowOff, colOff, grid, block, result;
for(let i = 0; i < this.times; i++) {
bRow = Math.floor( Math.random() * 3 );
bCol = Math.floor( Math.random() * 3 );
rowOff = Math.floor( Math.random() * 3 );
colOff = Math.floor( Math.random() * 3 );
grid = new Grid(bRow*3 + rowOff, bCol*3 + colOff);
block = Grid.blockBelonged(grid);
result = ( block.row === bRow ) && ( block.col == bCol );
assert.ok( result, `Get Right Block: ( ${block.row} === ${bRow} ) && ( ${block.col} == ${bCol} )`);
}
});
/*=============== Grid Test ===============*/
/*=============== Board Test ===============*/
QUnit.module("Class Board", {
before: () => {
this.board = new Board();
this.board.init();
this.times = 10;
}
})
QUnit.test("getRowGrids", assert => {
let row, grids;
for(let i = 0; i < this.times; i++) {
row = Math.floor( Math.random() * 9 );
grids = this.board.getRowGrids(row);
for (let i = 0; i < 9; i++) {
const grid = grids[i];
assert.equal(grid.row, row, `Grids (${row}, ${i}) row is right!`);
assert.equal(grid.col, i, `Grids (${row}, ${i}) col is right!`);
}
}
});
QUnit.test("getColumnGrids", assert => {
let col, grids;
for(let i = 0; i < this.times; i++) {
col = Math.floor( Math.random() * 9 );
grids = this.board.getColumnGrids(col);
for (let i = 0; i < 9; i++) {
const grid = grids[i];
assert.equal(grid.row, i, `Grids (${i}, ${col}) row is right!`);
assert.equal(grid.col, col, `Grids (${i}, ${col}) col is right!`);
}
}
});
QUnit.test("getBlockGrids", assert => {
let row, col, grids;
let right, block;
for(let i = 0; i < this.times; i++) {
row = Math.floor( Math.random() * 3 );
col = Math.floor( Math.random() * 3 );
grids = this.board.getBlockGrids(row, col);
assert.equal(grids.length, 9, `Block(${row}, ${col}) size is right!`);
grids.forEach(grid => {
assert.ok(grid instanceof Object, `Each ${grid} is an Object` )
block = Grid.blockBelonged(grid)
right = ( block.row === row ) && ( block.col === col );
assert.ok(right, "Block is Right");
});
}
});
QUnit.test("getGrids", assert => {
let row, col, grids;
for(let i = 0; i < this.times; i++) {
row = Math.floor( Math.random() * 9 );
grids = this.board.getGrids({mode: "row", row: row});
for (let i = 0; i < 9; i++) {
const grid = grids[i];
assert.ok( ( grid.row === row && grid.col === i ), `Grids (${row}, ${i}) row is right!`);
}
col = Math.floor( Math.random() * 9 );
grids = this.board.getGrids({mode: "column", col: col});
for (let i = 0; i < 9; i++) {
const grid = grids[i];
assert.ok( ( grid.row === i && grid.col === col ), `Grids (${i}, ${col}) col is right!`);
}
}
for(let i = 0; i < this.times; i++) {
row = Math.floor( Math.random() * 3 );
col = Math.floor( Math.random() * 3 );
grids = this.board.getGrids({mode: "block", row: row, col: col});
assert.equal(grids.length, 9, "Block size is right!");
grids.forEach(grid => {
assert.ok(grid instanceof Object, `Each ${grid} is an Object` )
});
}
grids = this.board.getGrids({mode: "none"});
assert.equal(grids.length, 0, "Get Empty Grids");
});
QUnit.test("getPartialGrids", assert => {
let rowStart, colStart, rowEnd, colEnd;
let pGrids, rGrids, cGrids, bGrids, result;
rowStart = 0, colStart = 0;
rowEnd = 1, colEnd = 9;
pGrids = this.board.getPartialGrids({rowStart, colStart, rowEnd, colEnd});
rGrids = this.board.getRowGrids(rowStart);
result = true;
pGrids.forEach( (grid, index) => {
result &= (grid === rGrids[index]);
});
assert.ok( result, `Partial Grids is Right`);
rowStart = 0, colStart = 3;
rowEnd = 9, colEnd = 4;
pGrids = this.board.getPartialGrids({rowStart, colStart, rowEnd, colEnd});
cGrids = this.board.getColumnGrids(colStart);
result = true;
pGrids.forEach( (grid, index) => {
result &= (grid === cGrids[index]);
});
assert.ok( result, `Partial Grids is Right`);
rowStart = 3, colStart = 3;
rowEnd = 6, colEnd = 6;
pGrids = this.board.getPartialGrids({rowStart, colStart, rowEnd, colEnd});
bGrids = this.board.getBlockGrids(1, 1);
result = true;
pGrids.forEach( (grid, index) => {
result &= (grid === bGrids[index]);
});
assert.ok( result, `Partial Grids is Right`);
});
QUnit.test("getUsedValueArrayAt", assert => {
let row, col;
for(let i = 0; i < this.times; i++) {
row = Math.floor( Math.random() * 9 );
col = Math.floor( Math.random() * 9 );
let usedValue = this.board.getUsedValueArrayAt({row, col});
assert.equal( usedValue.length, 9, "Used Value Array length is right");
}
});
/*=============== Board Test ===============*/
QUnit.module("Performance", {
before: () => {
this.times = 100;
}
});
QUnit.test("Board Init", assert => {
let valid;
let validCount = 0;
console.time("init board")
for (let i = 0; i < this.times; i++) {
let g = new Game(0);
Utils.printAll(g.board);
valid = g.board.isValid();
if( valid )
validCount++;
}
console.timeEnd("init board")
assert.equal(validCount, this.times, `Initialized new Valid Games ${validCount} times`);
});