-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtestutils.js
95 lines (81 loc) · 2.44 KB
/
testutils.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
const { StringArrayFilterer } = require("../index")
const legacy = require("fuzzaldrin-plus")
let performance = null
try {
performance = require("perf_hooks").performance
} catch {
performance = window.performance
}
/*function areArraysEqual2(a, b) {
if (a.length != b.length)
return false
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i])
return false
}
return true
}*/
function areArraysEqual(a, b) {
a = new Set(a)
b = new Set(b)
if (a.size !== b.size) {
return false
}
for (const a_item of a) {
if (!b.has(a_item)) {
return false
}
}
return true
}
function start_timer() {
return performance.now()
}
exports.start_timer = start_timer
function elapsed_time(timer_start_time, testName, decimals = 2) {
const time_final = performance.now()
const elapsed = (time_final - timer_start_time).toFixed(decimals)
if (testName) {
console.log(`${testName} took ${" ".repeat(80 - testName.length)} ${elapsed} ms`)
}
return parseFloat(elapsed)
}
exports.elapsed_time = elapsed_time
function doFilterTest(test_name, lines, query, params) {
console.log(`====== Running test - query:${query} ======`)
const strArrFilterer = new StringArrayFilterer() // We exclude the class construction time
const timer_start_time = start_timer()
strArrFilterer.setCandidates(lines)
const res_actual = strArrFilterer.filter(query, params)
const elapsed = elapsed_time(timer_start_time)
const timer_start_time_legacy = start_timer()
const res_expected = legacy.filter(lines, query)
const elapsed_legacy = elapsed_time(timer_start_time_legacy)
if (res_actual.length !== res_expected.length) {
console.error(`Results count changed! ${res_actual.length} instead of ${res_expected.length}`)
process.exit(1)
}
if (!areArraysEqual(res_actual, res_expected)) {
console.error(`Results different`)
console.error(` counts: ${res_actual.length}`)
process.exit(1)
}
if (test_name) {
console.log(test_name)
}
console.log(`zadeh vs. legacy: ${" ".repeat(50)} ${elapsed} ms | ${elapsed_legacy} ms`)
console.log(`length of the result: ${res_actual.length}, length of the lines: ${lines.length}`)
if (elapsed > elapsed_legacy) {
console.error(`${" ".repeat(75)} zadeh is SLOWER`)
}
console.log("")
}
function averageArray(nums) {
return nums.reduce((a, b) => a + b, 0) / nums.length || 0
}
module.exports = {
start_timer,
elapsed_time,
doFilterTest,
averageArray,
}