Commit | Line | Data |
---|---|---|
aba955e1 | 1 | const { expect } = require('expect') |
997bbcba | 2 | const { CircularArray } = require('../lib/circular-array') |
afe0d5bf JB |
3 | const { |
4 | availableParallelism, | |
78ac1a90 | 5 | isAsyncFunction, |
59317253 | 6 | isKillBehavior, |
afe0d5bf JB |
7 | isPlainObject, |
8 | median, | |
a91f7b35 JB |
9 | round, |
10 | updateMeasurementStatistics | |
afe0d5bf | 11 | } = require('../lib/utils') |
59317253 | 12 | const { KillBehaviors } = require('../lib/worker/worker-options') |
aba955e1 JB |
13 | |
14 | describe('Utils test suite', () => { | |
afe0d5bf JB |
15 | it('Verify availableParallelism() behavior', () => { |
16 | expect(typeof availableParallelism() === 'number').toBe(true) | |
51c90525 JB |
17 | expect(availableParallelism()).toBeGreaterThan(0) |
18 | expect(Number.isSafeInteger(availableParallelism())).toBe(true) | |
afe0d5bf JB |
19 | }) |
20 | ||
bb615bd0 | 21 | it('Verify median() computation', () => { |
4a45e8d2 | 22 | expect(median([])).toBe(0) |
76845835 JB |
23 | expect(median([0.08])).toBe(0.08) |
24 | expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05) | |
25 | expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535) | |
aba955e1 JB |
26 | }) |
27 | ||
afe0d5bf JB |
28 | it('Verify round() behavior', () => { |
29 | expect(round(0)).toBe(0) | |
30 | expect(round(0.5, 0)).toBe(1) | |
31 | expect(round(0.5)).toBe(0.5) | |
32 | expect(round(-0.5, 0)).toBe(-1) | |
33 | expect(round(-0.5)).toBe(-0.5) | |
34 | expect(round(1.005)).toBe(1.01) | |
35 | expect(round(2.175)).toBe(2.18) | |
36 | expect(round(5.015)).toBe(5.02) | |
37 | expect(round(-1.005)).toBe(-1.01) | |
38 | expect(round(-2.175)).toBe(-2.18) | |
39 | expect(round(-5.015)).toBe(-5.02) | |
40 | }) | |
41 | ||
aba955e1 JB |
42 | it('Verify isPlainObject() behavior', () => { |
43 | expect(isPlainObject(null)).toBe(false) | |
44 | expect(isPlainObject(undefined)).toBe(false) | |
45 | expect(isPlainObject(true)).toBe(false) | |
46 | expect(isPlainObject(false)).toBe(false) | |
47 | expect(isPlainObject(0)).toBe(false) | |
48 | expect(isPlainObject('')).toBe(false) | |
49 | expect(isPlainObject([])).toBe(false) | |
50 | expect(isPlainObject(() => {})).toBe(false) | |
51 | expect(isPlainObject(new Date())).toBe(false) | |
52 | expect(isPlainObject(new RegExp())).toBe(false) | |
53 | expect(isPlainObject(new Error())).toBe(false) | |
54 | expect(isPlainObject(new Map())).toBe(false) | |
55 | expect(isPlainObject(new Set())).toBe(false) | |
56 | expect(isPlainObject(new WeakMap())).toBe(false) | |
57 | expect(isPlainObject(new WeakSet())).toBe(false) | |
58 | expect(isPlainObject(new Int8Array())).toBe(false) | |
59 | expect(isPlainObject(new Uint8Array())).toBe(false) | |
60 | expect(isPlainObject(new Uint8ClampedArray())).toBe(false) | |
61 | expect(isPlainObject(new Int16Array())).toBe(false) | |
62 | expect(isPlainObject(new Uint16Array())).toBe(false) | |
63 | expect(isPlainObject(new Int32Array())).toBe(false) | |
64 | expect(isPlainObject(new Uint32Array())).toBe(false) | |
65 | expect(isPlainObject(new Float32Array())).toBe(false) | |
66 | expect(isPlainObject(new Float64Array())).toBe(false) | |
67 | expect(isPlainObject(new BigInt64Array())).toBe(false) | |
68 | expect(isPlainObject(new BigUint64Array())).toBe(false) | |
69 | expect(isPlainObject(new Promise(() => {}))).toBe(false) | |
70 | expect(isPlainObject(new WeakRef({}))).toBe(false) | |
71 | expect(isPlainObject(new FinalizationRegistry(() => {}))).toBe(false) | |
72 | expect(isPlainObject(new ArrayBuffer())).toBe(false) | |
73 | expect(isPlainObject(new SharedArrayBuffer())).toBe(false) | |
74 | expect(isPlainObject(new DataView(new ArrayBuffer()))).toBe(false) | |
75 | expect(isPlainObject({})).toBe(true) | |
76 | expect(isPlainObject({ a: 1 })).toBe(true) | |
77 | }) | |
47aacbaa JB |
78 | |
79 | it('Verify isKillBehavior() behavior', () => { | |
80 | expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.SOFT)).toBe(true) | |
81 | expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.HARD)).toBe(false) | |
82 | expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.HARD)).toBe(true) | |
83 | expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.SOFT)).toBe(false) | |
84 | expect(isKillBehavior(KillBehaviors.SOFT)).toBe(false) | |
85 | expect(isKillBehavior(KillBehaviors.HARD)).toBe(false) | |
86 | expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false) | |
78ac1a90 | 87 | expect(isKillBehavior(KillBehaviors.HARD, undefined)).toBe(false) |
47aacbaa JB |
88 | expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false) |
89 | }) | |
78ac1a90 JB |
90 | |
91 | it('Verify isAsyncFunction() behavior', () => { | |
92 | expect(isAsyncFunction(null)).toBe(false) | |
93 | expect(isAsyncFunction(undefined)).toBe(false) | |
94 | expect(isAsyncFunction(true)).toBe(false) | |
95 | expect(isAsyncFunction(false)).toBe(false) | |
96 | expect(isAsyncFunction(0)).toBe(false) | |
97 | expect(isAsyncFunction('')).toBe(false) | |
98 | expect(isAsyncFunction([])).toBe(false) | |
99 | expect(isAsyncFunction(new Date())).toBe(false) | |
100 | expect(isAsyncFunction(new RegExp())).toBe(false) | |
101 | expect(isAsyncFunction(new Error())).toBe(false) | |
102 | expect(isAsyncFunction(new Map())).toBe(false) | |
103 | expect(isAsyncFunction(new Set())).toBe(false) | |
104 | expect(isAsyncFunction(new WeakMap())).toBe(false) | |
105 | expect(isAsyncFunction(new WeakSet())).toBe(false) | |
106 | expect(isAsyncFunction(new Int8Array())).toBe(false) | |
107 | expect(isAsyncFunction(new Uint8Array())).toBe(false) | |
108 | expect(isAsyncFunction(new Uint8ClampedArray())).toBe(false) | |
109 | expect(isAsyncFunction(new Int16Array())).toBe(false) | |
110 | expect(isAsyncFunction(new Uint16Array())).toBe(false) | |
111 | expect(isAsyncFunction(new Int32Array())).toBe(false) | |
112 | expect(isAsyncFunction(new Uint32Array())).toBe(false) | |
113 | expect(isAsyncFunction(new Float32Array())).toBe(false) | |
114 | expect(isAsyncFunction(new Float64Array())).toBe(false) | |
115 | expect(isAsyncFunction(new BigInt64Array())).toBe(false) | |
116 | expect(isAsyncFunction(new BigUint64Array())).toBe(false) | |
117 | expect(isAsyncFunction(new Promise(() => {}))).toBe(false) | |
118 | expect(isAsyncFunction(new WeakRef({}))).toBe(false) | |
119 | expect(isAsyncFunction(new FinalizationRegistry(() => {}))).toBe(false) | |
120 | expect(isAsyncFunction(new ArrayBuffer())).toBe(false) | |
121 | expect(isAsyncFunction(new SharedArrayBuffer())).toBe(false) | |
122 | expect(isAsyncFunction(new DataView(new ArrayBuffer()))).toBe(false) | |
123 | expect(isAsyncFunction({})).toBe(false) | |
124 | expect(isAsyncFunction({ a: 1 })).toBe(false) | |
125 | expect(isAsyncFunction(() => {})).toBe(false) | |
126 | expect(isAsyncFunction(function () {})).toBe(false) | |
127 | expect(isAsyncFunction(function named () {})).toBe(false) | |
128 | expect(isAsyncFunction(async () => {})).toBe(true) | |
129 | expect(isAsyncFunction(async function () {})).toBe(true) | |
130 | expect(isAsyncFunction(async function named () {})).toBe(true) | |
131 | }) | |
a91f7b35 JB |
132 | |
133 | it('Verify updateMeasurementStatistics() behavior', () => { | |
997bbcba JB |
134 | const measurementStatistics = { |
135 | history: new CircularArray() | |
136 | } | |
a91f7b35 JB |
137 | updateMeasurementStatistics( |
138 | measurementStatistics, | |
139 | { aggregate: true, average: false, median: false }, | |
140 | 0.01, | |
141 | 1 | |
142 | ) | |
997bbcba | 143 | expect(measurementStatistics).toStrictEqual({ |
a91f7b35 JB |
144 | aggregate: 0.01, |
145 | maximum: 0.01, | |
997bbcba JB |
146 | minimum: 0.01, |
147 | history: expect.any(CircularArray) | |
a91f7b35 JB |
148 | }) |
149 | updateMeasurementStatistics( | |
150 | measurementStatistics, | |
151 | { aggregate: true, average: false, median: false }, | |
152 | 0.02, | |
153 | 2 | |
154 | ) | |
997bbcba | 155 | expect(measurementStatistics).toStrictEqual({ |
a91f7b35 JB |
156 | aggregate: 0.03, |
157 | maximum: 0.02, | |
997bbcba JB |
158 | minimum: 0.01, |
159 | history: expect.any(CircularArray) | |
a91f7b35 JB |
160 | }) |
161 | updateMeasurementStatistics( | |
162 | measurementStatistics, | |
163 | { aggregate: true, average: true, median: false }, | |
164 | 0.001, | |
165 | 3 | |
166 | ) | |
997bbcba | 167 | expect(measurementStatistics).toStrictEqual({ |
a91f7b35 JB |
168 | aggregate: 0.031, |
169 | maximum: 0.02, | |
170 | minimum: 0.001, | |
997bbcba JB |
171 | average: 0.010333333333333333, |
172 | history: expect.any(CircularArray) | |
a91f7b35 JB |
173 | }) |
174 | }) | |
aba955e1 | 175 | }) |