1 import { assert } from 'chai';
2 import { CircularList } from './CircularList';
4 describe('CircularList', () => {
5 describe('push', () => {
6 it('should push values onto the array', () => {
7 const list = new CircularList<string>(5);
13 assert.equal(list.get(0), '1');
14 assert.equal(list.get(1), '2');
15 assert.equal(list.get(2), '3');
16 assert.equal(list.get(3), '4');
17 assert.equal(list.get(4), '5');
20 it('should push old values from the start out of the array when max length is reached', () => {
21 const list = new CircularList<string>(2);
24 assert.equal(list.get(0), '1');
25 assert.equal(list.get(1), '2');
27 assert.equal(list.get(0), '2');
28 assert.equal(list.get(1), '3');
30 assert.equal(list.get(0), '3');
31 assert.equal(list.get(1), '4');
35 describe('maxLength', () => {
36 it('should increase the size of the list', () => {
37 const list = new CircularList<string>(2);
40 assert.equal(list.get(0), '1');
41 assert.equal(list.get(1), '2');
45 assert.equal(list.get(0), '1');
46 assert.equal(list.get(1), '2');
47 assert.equal(list.get(2), '3');
48 assert.equal(list.get(3), '4');
50 assert.equal(list.get(0), '2');
51 assert.equal(list.get(1), '3');
52 assert.equal(list.get(2), '4');
53 assert.equal(list.get(3), 'wrapped');
56 it('should return the maximum length of the list', () => {
57 const list = new CircularList<string>(2);
58 assert.equal(list.maxLength, 2);
61 assert.equal(list.maxLength, 2);
63 assert.equal(list.maxLength, 2);
65 assert.equal(list.maxLength, 4);
69 describe('length', () => {
70 it('should return the current length of the list, capped at the maximum length', () => {
71 const list = new CircularList<string>(2);
72 assert.equal(list.length, 0);
74 assert.equal(list.length, 1);
76 assert.equal(list.length, 2);
78 assert.equal(list.length, 2);
82 describe('splice', () => {
83 it('should delete items', () => {
84 const list = new CircularList<string>(2);
88 assert.equal(list.length, 1);
89 assert.equal(list.get(0), '2');
92 assert.equal(list.length, 1);
93 assert.equal(list.get(0), '2');
96 it('should insert items', () => {
97 const list = new CircularList<string>(2);
99 list.splice(0, 0, '2');
100 assert.equal(list.length, 2);
101 assert.equal(list.get(0), '2');
102 assert.equal(list.get(1), '1');
103 list.splice(1, 0, '3');
104 assert.equal(list.length, 2);
105 assert.equal(list.get(0), '3');
106 assert.equal(list.get(1), '1');
109 it('should delete items then insert items', () => {
110 const list = new CircularList<string>(3);
113 list.splice(0, 1, '3', '4');
114 assert.equal(list.length, 3);
115 assert.equal(list.get(0), '3');
116 assert.equal(list.get(1), '4');
117 assert.equal(list.get(2), '2');
120 it('should wrap the array correctly when more items are inserted than deleted', () => {
121 const list = new CircularList<string>(3);
124 list.splice(1, 0, '3', '4');
125 assert.equal(list.length, 3);
126 assert.equal(list.get(0), '3');
127 assert.equal(list.get(1), '4');
128 assert.equal(list.get(2), '2');
132 describe('trimStart', () => {
133 it('should remove items from the beginning of the list', () => {
134 const list = new CircularList<string>(5);
141 assert.equal(list.length, 4);
142 assert.deepEqual(list.get(0), '2');
143 assert.deepEqual(list.get(1), '3');
144 assert.deepEqual(list.get(2), '4');
145 assert.deepEqual(list.get(3), '5');
147 assert.equal(list.length, 2);
148 assert.deepEqual(list.get(0), '4');
149 assert.deepEqual(list.get(1), '5');
152 it('should remove all items if the requested trim amount is larger than the list\'s length', () => {
153 const list = new CircularList<string>(5);
156 assert.equal(list.length, 0);
160 describe('shiftElements', () => {
161 it('should not mutate the list when count is 0', () => {
162 const list = new CircularList<number>(5);
165 list.shiftElements(0, 0, 1);
166 assert.equal(list.length, 2);
167 assert.equal(list.get(0), 1);
168 assert.equal(list.get(1), 2);
171 it('should throw for invalid args', () => {
172 const list = new CircularList<number>(5);
174 assert.throws(() => list.shiftElements(-1, 1, 1), 'start argument out of range');
175 assert.throws(() => list.shiftElements(1, 1, 1), 'start argument out of range');
176 assert.throws(() => list.shiftElements(0, 1, -1), 'Cannot shift elements in list beyond index 0');
179 it('should shift an element forward', () => {
180 const list = new CircularList<number>(5);
183 list.shiftElements(0, 1, 1);
184 assert.equal(list.length, 2);
185 assert.equal(list.get(0), 1);
186 assert.equal(list.get(1), 1);
189 it('should shift elements forward', () => {
190 const list = new CircularList<number>(5);
195 list.shiftElements(0, 2, 2);
196 assert.equal(list.length, 4);
197 assert.equal(list.get(0), 1);
198 assert.equal(list.get(1), 2);
199 assert.equal(list.get(2), 1);
200 assert.equal(list.get(3), 2);
203 it('should shift elements forward, expanding the list if needed', () => {
204 const list = new CircularList<number>(5);
207 list.shiftElements(0, 2, 2);
208 assert.equal(list.length, 4);
209 assert.equal(list.get(0), 1);
210 assert.equal(list.get(1), 2);
211 assert.equal(list.get(2), 1);
212 assert.equal(list.get(3), 2);
215 it('should shift elements forward, wrapping the list if needed', () => {
216 const list = new CircularList<number>(5);
222 list.shiftElements(2, 2, 3);
223 assert.equal(list.length, 5);
224 assert.equal(list.get(0), 3);
225 assert.equal(list.get(1), 4);
226 assert.equal(list.get(2), 5);
227 assert.equal(list.get(3), 3);
228 assert.equal(list.get(4), 4);
231 it('should shift an element backwards', () => {
232 const list = new CircularList<number>(5);
235 list.shiftElements(1, 1, -1);
236 assert.equal(list.length, 2);
237 assert.equal(list.get(0), 2);
238 assert.equal(list.get(1), 2);
241 it('should shift elements backwards', () => {
242 const list = new CircularList<number>(5);
247 list.shiftElements(2, 2, -2);
248 assert.equal(list.length, 4);
249 assert.equal(list.get(0), 3);
250 assert.equal(list.get(1), 4);
251 assert.equal(list.get(2), 3);
252 assert.equal(list.get(3), 4);