-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestcaseGenerator.js
83 lines (70 loc) · 2.54 KB
/
TestcaseGenerator.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
class TestcaseGenerator {
#rowNumber;
#colNumber;
#targetLength;
#startPointNumber;
#grid;
#target;
#options;
#directions;
constructor({ rowNumber, colNumber, targetLength, startPointNumber, directions } = {}) {
this.#rowNumber = rowNumber;
this.#colNumber = colNumber;
this.#targetLength = targetLength;
this.#startPointNumber = startPointNumber;
this.#options = {
targetOverride: null, // in the form of "ABCDEFG" string
gridOverride: null, // in the form of "X" character
};
this.#directions = directions;
}
generate(overridedOptions = {}) {
const options = { ...this.#options, ...overridedOptions };
const grid = Array(this.#rowNumber);
if (options.targetOverride !== null && options.targetOverride.length !== this.#targetLength) {
throw new RangeError(`the length of targetOverride should be the same with targetLength: ${options.targetOverride.length} vs ${this.#targetLength}`);
}
for (let row = 0; row < this.#rowNumber; row += 1) {
grid[row] = Array(this.#colNumber);
for (let col = 0; col < this.#colNumber; col += 1) {
if (typeof options.gridOverride === "string") {
grid[row][col] = options.gridOverride;
} else {
grid[row][col] = String.fromCharCode(65 + Math.floor(Math.random() * 26));
}
}
}
let target = "";
if (typeof options.targetOverride === "string") {
target = options.targetOverride;
} else {
for (let i = 0; i < this.#targetLength; i += 1) {
target += String.fromCharCode(65 + Math.floor(Math.random() * 26));
}
}
for (let i = 0; i < this.#startPointNumber; i += 1) {
let currentRow = Math.floor(Math.random() * this.#rowNumber);
let currentCol = Math.floor(Math.random() * this.#colNumber);
const [deltaRow, deltaCol] = this.#directions[Math.floor(Math.random() * this.#directions.length)];
let targetRow = currentRow;
let targetCol = currentCol;
for (let k = 0; k < this.#targetLength; k += 1) {
const traversable = (0 <= targetRow && targetRow < this.#rowNumber) && (0 <= targetCol && targetCol < this.#colNumber);
if (!traversable) {
break;
}
grid[targetRow][targetCol] = target[k];
targetRow += deltaRow;
targetCol += deltaCol;
}
}
this.#grid = grid;
this.#target = target;
}
get grid() {
return this.#grid;
}
get target() {
return this.#target;
}
}