Skip to content

Commit 8ca15a5

Browse files
committed
test: write unit tests for timing function
1 parent acea915 commit 8ca15a5

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

lib/time.test.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const assert = require('node:assert/strict')
2+
const { describe, it, test } = require('node:test')
3+
const { pow } = Math
4+
5+
const { timeRepo } = require('./time')
6+
7+
const mockFun1 = (a, b, c) => a * a * b * b * c * c
8+
const mockFun2 = (a, b, c) => a ** 2 * b ** 2 * c ** 2
9+
const mockFun3 = (a, b, c) => pow(a, 2) * pow(b, 2) * pow(c, 2)
10+
11+
const mockAlg1 = { fun: mockFun1, id: 'mock function 1' }
12+
const mockAlg2 = { fun: mockFun2, id: 'mock function 2' }
13+
const mockAlg3 = { fun: mockFun3, id: 'mock function 3' }
14+
15+
describe('Mock algorithms', () => {
16+
test('return the same value', () => {
17+
const a = 4
18+
const b = -5
19+
const c = 7
20+
21+
const mock1 = mockAlg1.fun(a, b, c)
22+
const mock2 = mockAlg2.fun(a, b, c)
23+
const mock3 = mockAlg3.fun(a, b, c)
24+
25+
assert(mock1 === mock2 && mock2 === mock3)
26+
})
27+
})
28+
29+
describe('Timing function', () => {
30+
const algos = [mockAlg1, mockAlg2, mockAlg3]
31+
const args = [-3, 6, -9]
32+
33+
it('sorts results in ascending order', () => {
34+
const runs = 20
35+
const { sortedTimes } = timeRepo(algos, args, runs, 'unit test order')
36+
37+
for (let idx = 0; idx < sortedTimes.length - 1; idx++) {
38+
assert(sortedTimes[idx][1] <= sortedTimes[idx + 1][1])
39+
}
40+
})
41+
42+
it('throws if "runs" is less than 1', () => {
43+
const runs = 0
44+
assert.throws(
45+
() => timeRepo(algos, args, runs, 'unit test throw'),
46+
RangeError
47+
)
48+
})
49+
})

0 commit comments

Comments
 (0)