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