docs: document availableParallelism() usage
[poolifier.git] / tests / utils.test.js
1 const { expect } = require('expect')
2 const { isPlainObject, median, availableParallelism } = require('../lib/utils')
3 const {
4 isKillBehavior,
5 KillBehaviors
6 } = require('../lib/worker/worker-options')
7
8 describe('Utils test suite', () => {
9 it('Verify median() computation', () => {
10 expect(median([])).toBe(0)
11 expect(median([0.08])).toBe(0.08)
12 expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05)
13 expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535)
14 })
15
16 it('Verify isPlainObject() behavior', () => {
17 expect(isPlainObject(null)).toBe(false)
18 expect(isPlainObject(undefined)).toBe(false)
19 expect(isPlainObject(true)).toBe(false)
20 expect(isPlainObject(false)).toBe(false)
21 expect(isPlainObject(0)).toBe(false)
22 expect(isPlainObject('')).toBe(false)
23 expect(isPlainObject([])).toBe(false)
24 expect(isPlainObject(() => {})).toBe(false)
25 expect(isPlainObject(new Date())).toBe(false)
26 expect(isPlainObject(new RegExp())).toBe(false)
27 expect(isPlainObject(new Error())).toBe(false)
28 expect(isPlainObject(new Map())).toBe(false)
29 expect(isPlainObject(new Set())).toBe(false)
30 expect(isPlainObject(new WeakMap())).toBe(false)
31 expect(isPlainObject(new WeakSet())).toBe(false)
32 expect(isPlainObject(new Int8Array())).toBe(false)
33 expect(isPlainObject(new Uint8Array())).toBe(false)
34 expect(isPlainObject(new Uint8ClampedArray())).toBe(false)
35 expect(isPlainObject(new Int16Array())).toBe(false)
36 expect(isPlainObject(new Uint16Array())).toBe(false)
37 expect(isPlainObject(new Int32Array())).toBe(false)
38 expect(isPlainObject(new Uint32Array())).toBe(false)
39 expect(isPlainObject(new Float32Array())).toBe(false)
40 expect(isPlainObject(new Float64Array())).toBe(false)
41 expect(isPlainObject(new BigInt64Array())).toBe(false)
42 expect(isPlainObject(new BigUint64Array())).toBe(false)
43 expect(isPlainObject(new Promise(() => {}))).toBe(false)
44 expect(isPlainObject(new WeakRef({}))).toBe(false)
45 expect(isPlainObject(new FinalizationRegistry(() => {}))).toBe(false)
46 expect(isPlainObject(new ArrayBuffer())).toBe(false)
47 expect(isPlainObject(new SharedArrayBuffer())).toBe(false)
48 expect(isPlainObject(new DataView(new ArrayBuffer()))).toBe(false)
49 expect(isPlainObject({})).toBe(true)
50 expect(isPlainObject({ a: 1 })).toBe(true)
51 })
52
53 it('Verify isKillBehavior() behavior', () => {
54 expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.SOFT)).toBe(true)
55 expect(isKillBehavior(KillBehaviors.SOFT, KillBehaviors.HARD)).toBe(false)
56 expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.HARD)).toBe(true)
57 expect(isKillBehavior(KillBehaviors.HARD, KillBehaviors.SOFT)).toBe(false)
58 expect(isKillBehavior(KillBehaviors.SOFT)).toBe(false)
59 expect(isKillBehavior(KillBehaviors.HARD)).toBe(false)
60 expect(isKillBehavior(KillBehaviors.HARD, null)).toBe(false)
61 expect(isKillBehavior(KillBehaviors.SOFT, 'unknown')).toBe(false)
62 })
63
64 it('Verify availableParallelism() behavior', () => {
65 expect(typeof availableParallelism() === 'number').toBe(true)
66 })
67 })