refactor: cleanup cluster ESM issue workaround
[poolifier.git] / tests / circular-array.test.js
... / ...
CommitLineData
1const { expect } = require('expect')
2const { CircularArray } = require('../lib/circular-array')
3
4describe('Circular array test suite', () => {
5 it('Verify that circular array can be instantiated', () => {
6 const circularArray = new CircularArray()
7 expect(circularArray).toBeInstanceOf(CircularArray)
8 })
9
10 it('Verify circular array default size at instance creation', () => {
11 const circularArray = new CircularArray()
12 expect(circularArray.size).toBe(1024)
13 })
14
15 it('Verify that circular array size can be set at instance creation', () => {
16 const circularArray = new CircularArray(1000)
17 expect(circularArray.size).toBe(1000)
18 })
19
20 it('Verify that circular array size and items can be set at instance creation', () => {
21 let circularArray = new CircularArray(1000, 1, 2, 3, 4, 5)
22 expect(circularArray.size).toBe(1000)
23 expect(circularArray.length).toBe(5)
24 circularArray = new CircularArray(4, 1, 2, 3, 4, 5)
25 expect(circularArray.size).toBe(4)
26 expect(circularArray.length).toBe(4)
27 })
28
29 it('Verify that circular array size is valid at instance creation', () => {
30 expect(() => new CircularArray(0.25)).toThrowError(
31 new TypeError('Invalid circular array size: 0.25 is not a safe integer')
32 )
33 expect(() => new CircularArray(-1)).toThrowError(
34 new RangeError('Invalid circular array size: -1 < 0')
35 )
36 expect(() => new CircularArray(Number.MAX_SAFE_INTEGER + 1)).toThrowError(
37 new TypeError(
38 `Invalid circular array size: ${
39 Number.MAX_SAFE_INTEGER + 1
40 } is not a safe integer`
41 )
42 )
43 })
44
45 it('Verify that circular array empty works as intended', () => {
46 const circularArray = new CircularArray()
47 expect(circularArray.empty()).toBe(true)
48 })
49
50 it('Verify that circular array full works as intended', () => {
51 const circularArray = new CircularArray(5, 1, 2, 3, 4, 5)
52 expect(circularArray.full()).toBe(true)
53 })
54
55 it('Verify that circular array push works as intended', () => {
56 let circularArray = new CircularArray(4)
57 let arrayLength = circularArray.push(1, 2, 3, 4, 5)
58 expect(arrayLength).toBe(circularArray.size)
59 expect(circularArray.length).toBe(circularArray.size)
60 expect(circularArray).toStrictEqual(new CircularArray(4, 2, 3, 4, 5))
61 arrayLength = circularArray.push(6, 7)
62 expect(arrayLength).toBe(circularArray.size)
63 expect(circularArray.length).toBe(circularArray.size)
64 expect(circularArray).toStrictEqual(new CircularArray(4, 4, 5, 6, 7))
65 circularArray = new CircularArray(100)
66 arrayLength = circularArray.push(1, 2, 3, 4, 5)
67 expect(arrayLength).toBe(5)
68 expect(circularArray.size).toBe(100)
69 expect(circularArray.length).toBe(5)
70 expect(circularArray).toStrictEqual(new CircularArray(100, 1, 2, 3, 4, 5))
71 })
72
73 it('Verify that circular array splice works as intended', () => {
74 let circularArray = new CircularArray(1000, 1, 2, 3, 4, 5)
75 let deletedItems = circularArray.splice(2)
76 expect(deletedItems).toStrictEqual(new CircularArray(3, 3, 4, 5))
77 expect(circularArray.length).toBe(2)
78 expect(circularArray).toStrictEqual(new CircularArray(1000, 1, 2))
79 circularArray = new CircularArray(1000, 1, 2, 3, 4, 5)
80 deletedItems = circularArray.splice(2, 1)
81 expect(deletedItems).toStrictEqual(new CircularArray(1, 3))
82 expect(circularArray.length).toBe(4)
83 expect(circularArray).toStrictEqual(new CircularArray(1000, 1, 2, 4, 5))
84 circularArray = new CircularArray(4, 1, 2, 3, 4)
85 deletedItems = circularArray.splice(2, 1, 5, 6)
86 expect(deletedItems).toStrictEqual(new CircularArray(1, 3))
87 expect(circularArray.length).toBe(4)
88 expect(circularArray).toStrictEqual(new CircularArray(4, 2, 4, 5, 6))
89 })
90
91 it('Verify that circular array concat works as intended', () => {
92 let circularArray = new CircularArray(5, 1, 2, 3, 4, 5)
93 circularArray = circularArray.concat(6, 7)
94 expect(circularArray.length).toBe(5)
95 expect(circularArray).toStrictEqual(new CircularArray(5, 3, 4, 5, 6, 7))
96 circularArray = new CircularArray(1)
97 circularArray = circularArray.concat(6, 7)
98 expect(circularArray.length).toBe(1)
99 expect(circularArray).toStrictEqual(new CircularArray(1, 7))
100 })
101
102 it('Verify that circular array unshift works as intended', () => {
103 let circularArray = new CircularArray(5, 1, 2, 3, 4, 5)
104 let arrayLength = circularArray.unshift(6, 7)
105 expect(arrayLength).toBe(5)
106 expect(circularArray.length).toBe(5)
107 expect(circularArray).toStrictEqual(new CircularArray(5, 6, 7, 1, 2, 3))
108 circularArray = new CircularArray(1)
109 arrayLength = circularArray.unshift(6, 7)
110 expect(arrayLength).toBe(1)
111 expect(circularArray.length).toBe(1)
112 expect(circularArray).toStrictEqual(new CircularArray(1, 6))
113 })
114
115 it('Verify that circular array resize works as intended', () => {
116 expect(() => new CircularArray().resize(0.25)).toThrowError(
117 new TypeError('Invalid circular array size: 0.25 is not a safe integer')
118 )
119 expect(() => new CircularArray().resize(-1)).toThrowError(
120 new RangeError('Invalid circular array size: -1 < 0')
121 )
122 expect(() =>
123 new CircularArray().resize(Number.MAX_SAFE_INTEGER + 1)
124 ).toThrowError(
125 new TypeError(
126 `Invalid circular array size: ${
127 Number.MAX_SAFE_INTEGER + 1
128 } is not a safe integer`
129 )
130 )
131 let circularArray = new CircularArray(5, 1, 2, 3, 4, 5)
132 circularArray.resize(0)
133 expect(circularArray.size).toBe(0)
134 expect(circularArray).toStrictEqual(new CircularArray(0))
135 circularArray = new CircularArray(5, 1, 2, 3, 4, 5)
136 circularArray.resize(3)
137 expect(circularArray.size).toBe(3)
138 expect(circularArray).toStrictEqual(new CircularArray(3, 1, 2, 3))
139 circularArray = new CircularArray(5, 1, 2, 3, 4, 5)
140 circularArray.resize(8)
141 expect(circularArray.size).toBe(8)
142 expect(circularArray).toStrictEqual(new CircularArray(8, 1, 2, 3, 4, 5))
143 })
144})