1 import { expect } from 'expect'
5 DEFAULT_CIRCULAR_ARRAY_SIZE
6 } from '../lib/circular-array.cjs'
8 describe('Circular array test suite', () => {
9 it('Verify that circular array can be instantiated', () => {
10 const circularArray = new CircularArray()
11 expect(circularArray).toBeInstanceOf(CircularArray)
14 it('Verify circular array default size at instance creation', () => {
15 const circularArray = new CircularArray()
16 expect(circularArray.size).toBe(DEFAULT_CIRCULAR_ARRAY_SIZE)
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)
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)
33 it('Verify that circular array size is valid at instance creation', () => {
34 expect(() => new CircularArray(0.25)).toThrow(
35 new TypeError('Invalid circular array size: 0.25 is not a safe integer')
37 expect(() => new CircularArray(-1)).toThrow(
38 new RangeError('Invalid circular array size: -1 < 0')
40 expect(() => new CircularArray(Number.MAX_SAFE_INTEGER + 1)).toThrow(
42 `Invalid circular array size: ${
43 Number.MAX_SAFE_INTEGER + 1
44 } is not a safe integer`
49 it('Verify that circular array empty works as intended', () => {
50 const circularArray = new CircularArray()
51 expect(circularArray.empty()).toBe(true)
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)
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))
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)
90 expect(deletedItems).toStrictEqual(new CircularArray(2, 3, 1))
91 expect(circularArray.length).toBe(4)
92 expect(circularArray).toStrictEqual(new CircularArray(4, 2, 5, 6, 4))
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))
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))
119 it('Verify that circular array resize works as intended', () => {
120 expect(() => new CircularArray().resize(0.25)).toThrow(
121 new TypeError('Invalid circular array size: 0.25 is not a safe integer')
123 expect(() => new CircularArray().resize(-1)).toThrow(
124 new RangeError('Invalid circular array size: -1 < 0')
127 new CircularArray().resize(Number.MAX_SAFE_INTEGER + 1)
130 `Invalid circular array size: ${
131 Number.MAX_SAFE_INTEGER + 1
132 } is not a safe integer`
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)
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)
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))