]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/utils/CircularList.test.ts
Merge pull request #440 from Tyriar/439_wide_char_class
[mirror_xterm.js.git] / src / utils / CircularList.test.ts
CommitLineData
49b76baf
DI
1import { assert } from 'chai';
2import { CircularList } from './CircularList';
3
4describe('CircularList', () => {
5 describe('push', () => {
6 it('should push values onto the array', () => {
7 const list = new CircularList<string>(5);
8 list.push('1');
9 list.push('2');
10 list.push('3');
11 list.push('4');
12 list.push('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');
18 });
19
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);
22 list.push('1');
23 list.push('2');
24 assert.equal(list.get(0), '1');
25 assert.equal(list.get(1), '2');
26 list.push('3');
27 assert.equal(list.get(0), '2');
28 assert.equal(list.get(1), '3');
29 list.push('4');
30 assert.equal(list.get(0), '3');
31 assert.equal(list.get(1), '4');
32 });
33 });
34
35 describe('maxLength', () => {
36 it('should increase the size of the list', () => {
37 const list = new CircularList<string>(2);
38 list.push('1');
39 list.push('2');
40 assert.equal(list.get(0), '1');
41 assert.equal(list.get(1), '2');
42 list.maxLength = 4;
43 list.push('3');
44 list.push('4');
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');
49 list.push('wrapped');
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');
54 });
55
56 it('should return the maximum length of the list', () => {
57 const list = new CircularList<string>(2);
58 assert.equal(list.maxLength, 2);
59 list.push('1');
60 list.push('2');
61 assert.equal(list.maxLength, 2);
62 list.push('3');
63 assert.equal(list.maxLength, 2);
64 list.maxLength = 4;
65 assert.equal(list.maxLength, 4);
66 });
67 });
68
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);
73 list.push('1');
74 assert.equal(list.length, 1);
75 list.push('2');
76 assert.equal(list.length, 2);
77 list.push('3');
78 assert.equal(list.length, 2);
79 });
80 });
61fbbb00
DI
81
82 describe('splice', () => {
83 it('should delete items', () => {
84 const list = new CircularList<string>(2);
85 list.push('1');
86 list.push('2');
87 list.splice(0, 1);
88 assert.equal(list.length, 1);
89 assert.equal(list.get(0), '2');
90 list.push('3');
91 list.splice(1, 1);
92 assert.equal(list.length, 1);
93 assert.equal(list.get(0), '2');
94 });
95
96 it('should insert items', () => {
97 const list = new CircularList<string>(2);
98 list.push('1');
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');
107 });
108
109 it('should delete items then insert items', () => {
110 const list = new CircularList<string>(3);
111 list.push('1');
112 list.push('2');
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');
118 });
119
120 it('should wrap the array correctly when more items are inserted than deleted', () => {
121 const list = new CircularList<string>(3);
122 list.push('1');
123 list.push('2');
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');
129 });
130 });
bfaf9cd6
DI
131
132 describe('trimStart', () => {
133 it('should remove items from the beginning of the list', () => {
134 const list = new CircularList<string>(5);
135 list.push('1');
136 list.push('2');
137 list.push('3');
138 list.push('4');
139 list.push('5');
140 list.trimStart(1);
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');
146 list.trimStart(2);
147 assert.equal(list.length, 2);
148 assert.deepEqual(list.get(0), '4');
149 assert.deepEqual(list.get(1), '5');
150 });
151
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);
154 list.push('1');
155 list.trimStart(2);
156 assert.equal(list.length, 0);
157 });
158 });
969a2e95
DI
159
160 describe('shiftElements', () => {
161 it('should not mutate the list when count is 0', () => {
162 const list = new CircularList<number>(5);
163 list.push(1);
164 list.push(2);
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);
169 });
170
171 it('should throw for invalid args', () => {
172 const list = new CircularList<number>(5);
173 list.push(1);
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');
177 });
178
179 it('should shift an element forward', () => {
180 const list = new CircularList<number>(5);
181 list.push(1);
182 list.push(2);
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);
187 });
188
189 it('should shift elements forward', () => {
190 const list = new CircularList<number>(5);
191 list.push(1);
192 list.push(2);
193 list.push(3);
194 list.push(4);
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);
201 });
202
203 it('should shift elements forward, expanding the list if needed', () => {
204 const list = new CircularList<number>(5);
205 list.push(1);
206 list.push(2);
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);
213 });
214
215 it('should shift elements forward, wrapping the list if needed', () => {
216 const list = new CircularList<number>(5);
217 list.push(1);
218 list.push(2);
219 list.push(3);
220 list.push(4);
221 list.push(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);
229 });
230
231 it('should shift an element backwards', () => {
232 const list = new CircularList<number>(5);
233 list.push(1);
234 list.push(2);
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);
239 });
240
241 it('should shift elements backwards', () => {
242 const list = new CircularList<number>(5);
243 list.push(1);
244 list.push(2);
245 list.push(3);
246 list.push(4);
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);
253 });
254 });
49b76baf 255});