refactor: move helpers to utils.ts file
[poolifier.git] / tests / utils.test.js
CommitLineData
aba955e1 1const { expect } = require('expect')
afe0d5bf
JB
2const {
3 availableParallelism,
59317253 4 isKillBehavior,
afe0d5bf
JB
5 isPlainObject,
6 median,
7 round
8} = require('../lib/utils')
59317253 9const { KillBehaviors } = require('../lib/worker/worker-options')
aba955e1
JB
10
11describe('Utils test suite', () => {
afe0d5bf
JB
12 it('Verify availableParallelism() behavior', () => {
13 expect(typeof availableParallelism() === 'number').toBe(true)
14 })
15
bb615bd0 16 it('Verify median() computation', () => {
4a45e8d2 17 expect(median([])).toBe(0)
76845835
JB
18 expect(median([0.08])).toBe(0.08)
19 expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05)
20 expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535)
aba955e1
JB
21 })
22
afe0d5bf
JB
23 it('Verify round() behavior', () => {
24 expect(round(0)).toBe(0)
25 expect(round(0.5, 0)).toBe(1)
26 expect(round(0.5)).toBe(0.5)
27 expect(round(-0.5, 0)).toBe(-1)
28 expect(round(-0.5)).toBe(-0.5)
29 expect(round(1.005)).toBe(1.01)
30 expect(round(2.175)).toBe(2.18)
31 expect(round(5.015)).toBe(5.02)
32 expect(round(-1.005)).toBe(-1.01)
33 expect(round(-2.175)).toBe(-2.18)
34 expect(round(-5.015)).toBe(-5.02)
35 })
36
aba955e1
JB
37 it('Verify isPlainObject() behavior', () => {
38 expect(isPlainObject(null)).toBe(false)
39 expect(isPlainObject(undefined)).toBe(false)
40 expect(isPlainObject(true)).toBe(false)
41 expect(isPlainObject(false)).toBe(false)
42 expect(isPlainObject(0)).toBe(false)
43 expect(isPlainObject('')).toBe(false)
44 expect(isPlainObject([])).toBe(false)
45 expect(isPlainObject(() => {})).toBe(false)
46 expect(isPlainObject(new Date())).toBe(false)
47 expect(isPlainObject(new RegExp())).toBe(false)
48 expect(isPlainObject(new Error())).toBe(false)
49 expect(isPlainObject(new Map())).toBe(false)
50 expect(isPlainObject(new Set())).toBe(false)
51 expect(isPlainObject(new WeakMap())).toBe(false)
52 expect(isPlainObject(new WeakSet())).toBe(false)
53 expect(isPlainObject(new Int8Array())).toBe(false)
54 expect(isPlainObject(new Uint8Array())).toBe(false)
55 expect(isPlainObject(new Uint8ClampedArray())).toBe(false)
56 expect(isPlainObject(new Int16Array())).toBe(false)
57 expect(isPlainObject(new Uint16Array())).toBe(false)
58 expect(isPlainObject(new Int32Array())).toBe(false)
59 expect(isPlainObject(new Uint32Array())).toBe(false)
60 expect(isPlainObject(new Float32Array())).toBe(false)
61 expect(isPlainObject(new Float64Array())).toBe(false)
62 expect(isPlainObject(new BigInt64Array())).toBe(false)
63 expect(isPlainObject(new BigUint64Array())).toBe(false)
64 expect(isPlainObject(new Promise(() => {}))).toBe(false)
65 expect(isPlainObject(new WeakRef({}))).toBe(false)
66 expect(isPlainObject(new FinalizationRegistry(() => {}))).toBe(false)
67 expect(isPlainObject(new ArrayBuffer())).toBe(false)
68 expect(isPlainObject(new SharedArrayBuffer())).toBe(false)
69 expect(isPlainObject(new DataView(new ArrayBuffer()))).toBe(false)
70 expect(isPlainObject({})).toBe(true)
71 expect(isPlainObject({ a: 1 })).toBe(true)
72 })
47aacbaa
JB
73
74 it('Verify isKillBehavior() behavior', () => {
75 expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.SOFT)).toBe(true)
76 expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.HARD)).toBe(false)
77 expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.HARD)).toBe(true)
78 expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.SOFT)).toBe(false)
79 expect(isKillBehavior(KillBehaviors.SOFT)).toBe(false)
80 expect(isKillBehavior(KillBehaviors.HARD)).toBe(false)
81 expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false)
82 expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false)
83 })
aba955e1 84})