-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.js
More file actions
273 lines (241 loc) · 6.57 KB
/
Test.js
File metadata and controls
273 lines (241 loc) · 6.57 KB
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
exports.logArgs = true;
exports.logResult = true;
exports.test = function (fun, ...args) {
// console.log("fun:",);
console.log("test", fun);
// console.log({ logArgs: this.logArgs });
if (this.logArgs) {
console.log("args:", args);
}
console.time();
const result = fun(...args);
if (this.logResult) {
console.log("result:");
console.log(result);
}
console.timeEnd();
console.log("\n");
};
exports.repeatTest = function (times, fun, ...args) {
console.time();
while (--times > 0) {
fun(...args);
}
console.timeEnd();
console.log("\n");
}
exports.run = function (fun, ...args) {
// console.log("fun:",);
console.log("test", fun);
// console.log({ logArgs: this.logArgs });
if (this.logArgs) {
console.log("args:", args);
}
console.time();
const result = fun(...args);
if (this.logResult) {
console.log("result:");
console.log(result);
}
console.timeEnd();
console.log("\n");
return result;
};
exports.repeatRun = function (times, fun, ...args) {
console.time();
while (--times > 0) {
fun(...args);
}
console.timeEnd();
console.log("\n");
}
exports.assert = function (value, fun) {
console.log();
console.log();
// console.log(fun);
const result = fun();
if (value == result) {
console.log(true);
}
else {
console.error(false, `expected ${value}, but get ${result}`);
}
}
exports.testWithTestcase = function (fun, id) {
this.test(fun, ...this.testcase(id));
}
exports.testWithTestcaseV2 = async function (fun, id) {
var fs = require('fs');
const readline = require('readline');
const fileStream = fs.createReadStream("Testcases/" + id, 'utf-8');
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
const lines = [];
for await (const line of rl) {
lines.push(eval(line));
}
this.run(fun, ...lines);
}
// exports.testWithInstructions = function (ops, params) {
// const obj = eval(`new ${ops[0]}(...params[0])`);
exports.testWithInstructions = function (clazz, ops, params) {
console.log(clazz);
console.log(ops);
console.log(params);
console.time();
const obj = new clazz(...params[0]);
console.log("constructed:", obj);
// const obj = eval(`new ${ops[0]}(...params[0])`);
// console.log(obj);
for (let i = 1; i < ops.length; i++) {
console.log(ops[i], ...params[i]);
const result = obj[ops[i]].bind(obj)(...params[i]);
if (result != undefined) {
console.log("result:");
console.log(result);
}
// console.log({ result });
console.log();
}
console.timeEnd();
console.log("\n");
};
exports.runWithInstructions = function (clazz, ops, params) {
console.log(clazz);
console.log(ops);
console.log(params);
console.time();
const obj = new clazz(...params[0]);
console.log("constructed:", obj);
// const obj = eval(`new ${ops[0]}(...params[0])`);
// console.log(obj);
for (let i = 1; i < ops.length; i++) {
console.log(ops[i], ...params[i]);
const result = obj[ops[i]].bind(obj)(...params[i]);
if (result != undefined) {
console.log("result:");
console.log(result);
}
// console.log({ result });
console.log();
}
console.timeEnd();
console.log("\n");
};
exports.loopTest = function (times, fun, ...args) {
// console.log("fun:",);
console.log("test", fun);
console.log("args:", args);
console.time();
for (let i = 0; i < times; i++) {
fun(...args);
}
// const result = fun(...args);
// console.log("result:");
// console.log(result);
console.timeEnd();
console.log("\n");
};
exports.Test = class {
timeLabel = "time";
// logArgs = true;
// logResult = true;
argsLogger;
resultLogger;
constructor(fun, ...args) {
this.fun = fun;
this.args = args;
// this.logArgs = true;
this.logArgs = false;
this.logResult = true;
// this.logResult = false;
}
do() {
console.log("test", this.fun);
if (this.logArgs) {
console.log("args:");
if (this.argsLogger) {
this.argsLogger(this.args);
}
else {
console.log(this.args);
}
}
console.time(this.timeLabel);
const result = this.fun(...this.args);
if (this.logResult) {
console.log("result:");
if (this.resultLogger) {
this.resultLogger(result);
}
else {
console.log(result);
}
}
console.timeEnd(this.timeLabel);
console.log("\n");
}
}
exports.compareStr = function (str1, str2) {
let i;
// for (i = 0; i < str1.length; i++) {
for (i = 0; i < Math.min(str1.length, str2.length); i++) {
if (str1[i] != str2[i]) {
console.log("different from position " + i + ":");
console.log("..." + str1.slice(i));
console.log("..." + str2.slice(i));
break
}
}
if (i < str1.length) {
console.log(str1.slice(i));
}
if (i < str2.length) {
console.log(str2.slice(i));
}
console.log("comparing completed");
}
exports.isLogOn = true;
exports.log = function (...args) {
if (this.isLogOn) {
console.log(...args);
}
}
exports.condLog = function (cond, ...args) {
if (this.isLogOn && cond) {
console.log(...args);
}
}
exports.testcase = function (id) {
var fs = require('fs');
// var data = fs.readFileSync("Testcases/" + id, 'utf-8');
var data = fs.readFileSync("../res/Testcases/" + id, 'utf-8');
return eval(data);
}
exports.testcaseV2 = function (id) {
}
/*
exports.testcase = function (id) {
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('file.in')
});
lineReader.on('line', function (line) {
console.log('Line from file:', line);
});
}
*/
exports.wrongOutput = function (id) {
var fs = require('fs');
var data = fs.readFileSync("WrongOutputs/" + id, 'utf-8');
return eval(data);
}
exports.compareArray = function (arr1, arr2) {
// for (let i = 0; i < Math.min(arr1.length, arr2.length); i++) {
for (let i = 0; i < Math.max(arr1.length, arr2.length); i++) {
if (arr1[i] != arr2[i]) {
console.log([i, arr1[i], arr2[i]]);
}
}
}