-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShareableArray.spec.ts
More file actions
666 lines (527 loc) · 22.9 KB
/
ShareableArray.spec.ts
File metadata and controls
666 lines (527 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
import {ShareableArray} from "../ShareableArray";
describe("ShareableArray", () => {
const randomItemsAmount = 50000;
const generatedData: string[] = [];
for (let i = 0; i < randomItemsAmount; i++) {
generatedData.push(generateRandomString());
}
beforeAll(() => {
const { TextEncoder, TextDecoder } = require("util");
globalThis.TextEncoder = TextEncoder;
globalThis.TextDecoder = TextDecoder;
});
it("should correctly add items", () => {
const array = new ShareableArray<string>();
for (const item of generatedData) {
array.push(item);
}
expect(array.length).toEqual(randomItemsAmount);
});
it("should correctly return the items that have been added before", () => {
const array = new ShareableArray<string>();
for (const item of generatedData) {
array.push(item);
}
for (let idx = 0; idx < randomItemsAmount; idx++) {
expect(array.at(idx)).toEqual(generatedData[idx]);
}
});
it("should correctly store and retrieve simple objects", () => {
const array = new ShareableArray<{ id: number, name: string }>();
const testObjects = [
{id: 1, name: "first"},
{id: 2, name: "second"},
{id: 3, name: "third"}
];
testObjects.forEach(obj => array.push(obj));
for (let i = 0; i < testObjects.length; i++) {
expect(array.at(i)).toEqual(testObjects[i]);
}
});
it("should correctly store and retrieve nested objects", () => {
const array = new ShareableArray<{ user: { id: number, details: { name: string } } }>();
const testObjects = [
{user: {id: 1, details: {name: "John"}}},
{user: {id: 2, details: {name: "Jane"}}}
];
testObjects.forEach(obj => array.push(obj));
for (let i = 0; i < testObjects.length; i++) {
expect(array.at(i)).toEqual(testObjects[i]);
}
});
it("should correctly store and retrieve objects with array properties", () => {
const array = new ShareableArray<{ id: number, tags: string[] }>();
const testObjects = [
{id: 1, tags: ["tag1", "tag2"]},
{id: 2, tags: ["tag3", "tag4", "tag5"]}
];
testObjects.forEach(obj => array.push(obj));
for (let i = 0; i < testObjects.length; i++) {
expect(array.at(i)).toEqual(testObjects[i]);
}
});
it("should correctly store and retrieve complex nested objects", () => {
type ComplexObject = {
id: number,
metadata: {
created: string,
tags: string[]
},
data: {
user: {
details: {
name: string,
contacts: {
email: string,
phone?: string
}[]
}
}
}
};
const array = new ShareableArray<ComplexObject>();
const testObjects: ComplexObject[] = [
{
id: 1,
metadata: {
created: '2025-01-01',
tags: ["important", "user"]
},
data: {
user: {
details: {
name: "John Doe",
contacts: [
{email: "john@example.com", phone: "1234567"},
{email: "john.doe@example.com"}
]
}
}
}
},
{
id: 2,
metadata: {
created: '2025-01-02',
tags: ["regular"]
},
data: {
user: {
details: {
name: "Jane Doe",
contacts: [
{email: "jane@example.com"}
]
}
}
}
}
];
testObjects.forEach(obj => array.push(obj));
for (let i = 0; i < testObjects.length; i++) {
expect(array.at(i)).toEqual(testObjects[i]);
}
});
it("should correctly concatenate two ShareableArrays", () => {
const arrA = new ShareableArray<string>();
const arrB = new ShareableArray<string>();
const dummyData = ["a", "b", "c", "d", "e", "1", "2", "3", "4", "5"];
// Fill the first array
for (const item of dummyData.slice(0, 5)) {
arrA.push(item);
}
// Fill the second array
for (const item of dummyData.slice(5, 10)) {
arrB.push(item);
}
const concatenated = arrA.concat(arrB);
expect(concatenated.length).toEqual(dummyData.length);
// Check if all items from the dummy data are present in the concatenated arrays
for (const [idx, dummyItem] of dummyData.entries()) {
expect(concatenated.at(idx)).toEqual(dummyItem);
}
});
it("should correctly concatenate a ShareableArray with a normal array", () => {
const arrA = new ShareableArray<string>();
const arrB = [];
const dummyData = ["a", "b", "c", "d", "e", "1", "2", "3", "4", "5"];
// Fill the first array
for (const item of dummyData.slice(0, 5)) {
arrA.push(item);
}
// Fill the second array
for (const item of dummyData.slice(5, 10)) {
arrB.push(item);
}
const concatenated = arrA.concat(arrB);
expect(concatenated.length).toEqual(dummyData.length);
// Check if all items from the dummy data are present in the concatenated arrays
for (const [idx, dummyItem] of dummyData.entries()) {
expect(concatenated.at(idx)).toEqual(dummyItem);
}
});
it("should correctly check if all elements satisfy a condition using every()", () => {
const array = new ShareableArray<number>();
[2, 4, 6, 8, 10].forEach(n => array.push(n));
expect(array.every(num => num! % 2 === 0)).toBeTruthy();
expect(array.every(num => num! > 5)).toBeFalsy();
});
it("should correctly filter elements using filter()", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].forEach(n => array.push(n));
const filtered = array.filter(num => num! % 2 === 0);
expect(filtered.length).toBe(5);
expect([...filtered].every(num => num % 2 === 0)).toBeTruthy();
});
it("should correctly execute forEach() on all elements", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
const result: number[] = [];
array.forEach(num => result.push(num! * 2));
expect(result).toEqual([2, 4, 6, 8, 10]);
});
it("should correctly find element index using indexOf()", () => {
const array = new ShareableArray<string>();
["apple", "banana", "orange", "banana", "grape"].forEach(item => array.push(item));
expect(array.indexOf("banana")).toBe(1);
expect(array.indexOf("grape")).toBe(4);
expect(array.indexOf("mango")).toBe(-1);
expect(array.indexOf("banana", 2)).toBe(3);
});
it("should correctly join elements with different delimiters", () => {
const emptyArray = new ShareableArray<string>();
const singleArray = new ShareableArray<string>();
const multiArray = new ShareableArray<string>();
singleArray.push("apple");
["apple", "banana", "orange"].forEach(item => multiArray.push(item));
// Empty array
expect(emptyArray.join()).toBe("");
expect(emptyArray.join("|")).toBe("");
expect(emptyArray.join(" and ")).toBe("");
expect(emptyArray.join("")).toBe("");
// Single element array
expect(singleArray.join()).toBe("apple");
expect(singleArray.join("|")).toBe("apple");
expect(singleArray.join(" and ")).toBe("apple");
expect(singleArray.join("")).toBe("apple");
// Multiple elements array
expect(multiArray.join()).toBe("apple,banana,orange");
expect(multiArray.join("|")).toBe("apple|banana|orange");
expect(multiArray.join(" and ")).toBe("apple and banana and orange");
expect(multiArray.join("")).toBe("applebananaorange");
});
it("should correctly find last element index using lastIndexOf()", () => {
const array = new ShareableArray<string>();
["apple", "banana", "orange", "banana", "grape"].forEach(item => array.push(item));
expect(array.lastIndexOf("banana")).toBe(3);
expect(array.lastIndexOf("grape")).toBe(4);
expect(array.lastIndexOf("mango")).toBe(-1);
expect(array.lastIndexOf("banana", 2)).toBe(1);
});
it("should correctly pop elements from a non-empty array", () => {
const array = new ShareableArray<string>();
["apple", "banana", "orange"].forEach(item => array.push(item));
expect(array.pop()).toBe("orange");
expect(array.pop()).toBe("banana");
expect(array.pop()).toBe("apple");
});
it("should return undefined when popping from an empty array", () => {
const array = new ShareableArray<string>();
expect(array.pop()).toBeUndefined();
});
it("should correctly update length after pop operations", () => {
const array = new ShareableArray<string>();
["apple", "banana", "orange"].forEach(item => array.push(item));
expect(array.length).toBe(3);
array.pop();
expect(array.length).toBe(2);
array.pop();
expect(array.length).toBe(1);
array.pop();
expect(array.length).toBe(0);
});
it("should correctly reduce array of numbers", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
const sum = array.reduce((acc, curr) => acc! + curr!, 0);
expect(sum).toBe(15);
const emptyArray = new ShareableArray<number>();
expect(emptyArray.reduce((acc, curr) => acc! + curr!, 0)).toBe(0);
});
it("should correctly reduce array of strings", () => {
const array = new ShareableArray<string>();
["Hello", " ", "World", "!"].forEach(s => array.push(s));
const concatenated = array.reduce((acc, curr) => acc! + curr!);
expect(concatenated).toBe("Hello World!");
const emptyArray = new ShareableArray<string>();
expect(() => emptyArray.reduce((acc, curr) => acc! + curr!)).toThrow();
});
it("should correctly reduce with initial value", () => {
const array = new ShareableArray<number>();
[1, 2, 3].forEach(n => array.push(n));
const sumPlusTen = array.reduce((acc, curr) => acc + curr!, 10);
expect(sumPlusTen).toBe(16);
const emptyArray = new ShareableArray<number>();
expect(emptyArray.reduce((acc, curr) => acc + curr!, 10)).toBe(10);
});
it("should correctly reduce right array of numbers", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
const result = array.reduceRight((acc, curr) => acc! - curr!);
expect(result).toBe(-5);
const emptyArray = new ShareableArray<number>();
expect(() => emptyArray.reduceRight((acc, curr) => acc! - curr!)).toThrow();
});
it("should correctly reduce right with initial value", () => {
const array = new ShareableArray<number>();
[1, 2, 3].forEach(n => array.push(n));
const result = array.reduceRight((acc, curr) => acc - curr!, 10);
expect(result).toBe(4);
const emptyArray = new ShareableArray<number>();
expect(emptyArray.reduceRight((acc, curr) => acc - curr!, 10)).toBe(10);
});
it("should correctly reverse non-empty array", () => {
const array = new ShareableArray<string>();
["a", "b", "c", "d"].forEach(item => array.push(item));
array.reverse();
expect([...array]).toEqual(["d", "c", "b", "a"]);
});
it("should handle reverse on empty array", () => {
const array = new ShareableArray<string>();
array.reverse();
expect(array.length).toBe(0);
});
it("should correctly shift elements from non-empty array", () => {
const array = new ShareableArray<string>();
["apple", "banana", "orange"].forEach(item => array.push(item));
expect(array.shift()).toBe("apple");
expect(array.shift()).toBe("banana");
expect(array.shift()).toBe("orange");
});
it("should return undefined when shifting from empty array", () => {
const array = new ShareableArray<string>();
expect(array.shift()).toBeUndefined();
});
it("should correctly update length after shift operations", () => {
const array = new ShareableArray<string>();
["apple", "banana", "orange"].forEach(item => array.push(item));
expect(array.length).toBe(3);
array.shift();
expect(array.length).toBe(2);
array.shift();
expect(array.length).toBe(1);
array.shift();
expect(array.length).toBe(0);
});
it("should correctly fill array with a number value", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
array.fill(0);
expect([...array]).toEqual([0, 0, 0, 0, 0]);
});
it("should correctly fill array with a string value", () => {
const array = new ShareableArray<string>();
["a", "b", "c"].forEach(s => array.push(s));
array.fill("x");
expect([...array]).toEqual(["x", "x", "x"]);
});
it("should correctly fill array with an object value", () => {
const array = new ShareableArray<object>();
[{id: 1}, {id: 2}, {id: 3}].forEach(obj => array.push(obj));
const fillObj = {test: true};
array.fill(fillObj);
expect([...array]).toEqual([fillObj, fillObj, fillObj]);
});
it("should correctly fill array with start and end indexes", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
array.fill(0, 1, 4);
expect([...array]).toEqual([1, 0, 0, 0, 5]);
});
it("should handle filling empty array", () => {
const array = new ShareableArray<number>();
array.fill(1);
expect([...array]).toEqual([]);
});
it("should handle negative indexes when filling", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
array.fill(0, -3, -1);
expect([...array]).toEqual([1, 2, 0, 0, 5]);
});
it("should handle out of bounds indexes when filling", () => {
const array = new ShareableArray<number>();
[1, 2, 3].forEach(n => array.push(n));
array.fill(0, -5, 10);
expect([...array]).toEqual([0, 0, 0]);
});
it("should correctly find element using find()", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
const found = array.find(num => num! > 3);
expect(found).toBe(4);
const evenNumber = array.find(num => num! % 2 === 0);
expect(evenNumber).toBe(2);
});
it("should return undefined when no element is found using find()", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
const notFound = array.find(num => num! > 10);
expect(notFound).toBeUndefined();
});
it("should correctly find element index using findIndex()", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
const foundIndex = array.findIndex(num => num! > 3);
expect(foundIndex).toBe(3);
const evenNumberIndex = array.findIndex(num => num! % 2 === 0);
expect(evenNumberIndex).toBe(1);
});
it("should return -1 when no element index is found using findIndex()", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
const notFoundIndex = array.findIndex(num => num! > 10);
expect(notFoundIndex).toBe(-1);
});
it("should correctly find last element using findLast()", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5, 2, 4].forEach(n => array.push(n));
const found = array.findLast(num => num! > 3);
expect(found).toBe(4);
const evenNumber = array.findLast(num => num! % 2 === 0);
expect(evenNumber).toBe(4);
});
it("should return undefined when no element is found using findLast()", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
const notFound = array.findLast(num => num! > 10);
expect(notFound).toBeUndefined();
});
it("should correctly find last element index using findLastIndex()", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5, 2, 4].forEach(n => array.push(n));
const foundIndex = array.findLastIndex(num => num! > 3);
expect(foundIndex).toBe(6);
const evenNumberIndex = array.findLastIndex(num => num! % 2 === 0);
expect(evenNumberIndex).toBe(6);
});
it("should return -1 when no element index is found using findLastIndex()", () => {
const array = new ShareableArray<number>();
[1, 2, 3, 4, 5].forEach(n => array.push(n));
const notFoundIndex = array.findLastIndex(num => num! > 10);
expect(notFoundIndex).toBe(-1);
});
it("should correctly unshift elements to an empty array", () => {
const array = new ShareableArray<string>();
array.unshift("first");
expect(array.length).toBe(1);
expect(array.at(0)).toBe("first");
});
it("should correctly unshift multiple elements in sequence", () => {
const array = new ShareableArray<string>();
array.unshift("third");
array.unshift("second");
array.unshift("first");
expect(array.length).toBe(3);
expect([...array]).toEqual(["first", "second", "third"]);
});
it("should correctly unshift different data types", () => {
const array = new ShareableArray<any>();
array.unshift(42);
array.unshift("test");
array.unshift({key: "value"});
array.unshift([1, 2, 3]);
expect(array.length).toBe(4);
expect(array.at(0)).toEqual([1, 2, 3]);
expect(array.at(1)).toEqual({key: "value"});
expect(array.at(2)).toBe("test");
expect(array.at(3)).toBe(42);
});
it("should correctly update length after unshift operations", () => {
const array = new ShareableArray<number>();
expect(array.length).toBe(0);
array.unshift(1);
expect(array.length).toBe(1);
array.unshift(2);
expect(array.length).toBe(2);
array.unshift(3);
expect(array.length).toBe(3);
});
it("should correctly handle multiple items in a single unshift operation", () => {
const array = new ShareableArray<number>();
array.unshift(1, 2, 3, 4);
expect(array.length).toBe(4);
expect([...array]).toEqual([1, 2, 3, 4]);
array.unshift(5, 6);
expect(array.length).toBe(6);
expect([...array]).toEqual([5, 6, 1, 2, 3, 4]);
});
it("should correctly check if element exists using includes()", () => {
const array = new ShareableArray<string>();
["apple", "banana", "orange"].forEach(item => array.push(item));
expect(array.includes("banana")).toBeTruthy();
expect(array.includes("grape")).toBeFalsy();
// `fromIndex >= array.length` => The array should not be searched.
expect(array.includes("banana", 2)).toBeFalsy();
// Start searching after "apple" (which should thus not be found).
expect(array.includes("apple", 1)).toBeFalsy();
});
it("should correctly return iterator for array keys()", () => {
const array = new ShareableArray<string>();
["a", "b", "c"].forEach(item => array.push(item));
const keys = [...array.keys()];
expect(keys).toEqual([0, 1, 2]);
});
it("should correctly flatMap array elements", () => {
const array = new ShareableArray<number>();
[1, 2, 3].forEach(n => array.push(n));
const result = array.flatMap(x => [x!, x! * 2]);
expect([...result]).toEqual([1, 2, 2, 4, 3, 6]);
expect(result).toBeInstanceOf(ShareableArray);
});
it("should correctly reverse array without modifying original using toReversed()", () => {
const array = new ShareableArray<string>();
["a", "b", "c", "d"].forEach(item => array.push(item));
const reversed = array.toReversed();
expect([...reversed]).toEqual(["d", "c", "b", "a"]);
expect([...array]).toEqual(["a", "b", "c", "d"]);
expect(reversed).toBeInstanceOf(ShareableArray);
});
it("should correctly sort array without modifying original using toSorted()", () => {
const array = new ShareableArray<number>();
[3, 1, 4, 1, 5, 9, 2, 6].forEach(n => array.push(n));
const sorted = array.toSorted();
const customSorted = array.toSorted((a, b) => b! - a!);
expect([...sorted]).toEqual([1, 1, 2, 3, 4, 5, 6, 9]);
expect([...customSorted]).toEqual([9, 6, 5, 4, 3, 2, 1, 1]);
expect([...array]).toEqual([3, 1, 4, 1, 5, 9, 2, 6]);
expect(sorted).toBeInstanceOf(ShareableArray);
expect(customSorted).toBeInstanceOf(ShareableArray);
});
it("should correctly defragment the array", () => {
const array = new ShareableArray<string>();
const items = ["item1", "item2", "item3", "item4", "item5"];
items.forEach(item => array.push(item));
// Delete some items to create gaps
array.pop(); // delete last
(array as any).deleteItem(1); // delete index 1 ("item2")
// Array should have [item1, item3, item4]
// Indices: 0->item1, 1->item3, 2->item4
expect(array.length).toBe(3);
expect(array.at(0)).toBe("item1");
expect(array.at(1)).toBe("item3");
expect(array.at(2)).toBe("item4");
// Call defragment
(array as any).defragment();
// Verify data is still intact
expect(array.length).toBe(3);
expect(array.at(0)).toBe("item1");
expect(array.at(1)).toBe("item3");
expect(array.at(2)).toBe("item4");
// We can't easily verify memory layout without inspecting private internals,
// but if data retrieval works, it means defrag didn't corrupt pointers.
});
});
function generateRandomString() {
return Math.random().toString(36).substring(7);
}