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