Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/map.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { BidirectionalMap, LinkedMap, LRUCache, mapsStrictEqualIgnoreOrder, MRUCache, NKeyMap, ResourceMap, SetMap, Touch } from '../../common/map.js';
8
import { extUriIgnorePathCase } from '../../common/resources.js';
9
import { URI } from '../../common/uri.js';
10
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
11
12
suite('Map', () => {
13
14
ensureNoDisposablesAreLeakedInTestSuite();
15
16
test('LinkedMap - Simple', () => {
17
const map = new LinkedMap<string, string>();
18
map.set('ak', 'av');
19
map.set('bk', 'bv');
20
assert.deepStrictEqual([...map.keys()], ['ak', 'bk']);
21
assert.deepStrictEqual([...map.values()], ['av', 'bv']);
22
assert.strictEqual(map.first, 'av');
23
assert.strictEqual(map.last, 'bv');
24
});
25
26
test('LinkedMap - Touch Old one', () => {
27
const map = new LinkedMap<string, string>();
28
map.set('ak', 'av');
29
map.set('ak', 'av', Touch.AsOld);
30
assert.deepStrictEqual([...map.keys()], ['ak']);
31
assert.deepStrictEqual([...map.values()], ['av']);
32
});
33
34
test('LinkedMap - Touch New one', () => {
35
const map = new LinkedMap<string, string>();
36
map.set('ak', 'av');
37
map.set('ak', 'av', Touch.AsNew);
38
assert.deepStrictEqual([...map.keys()], ['ak']);
39
assert.deepStrictEqual([...map.values()], ['av']);
40
});
41
42
test('LinkedMap - Touch Old two', () => {
43
const map = new LinkedMap<string, string>();
44
map.set('ak', 'av');
45
map.set('bk', 'bv');
46
map.set('bk', 'bv', Touch.AsOld);
47
assert.deepStrictEqual([...map.keys()], ['bk', 'ak']);
48
assert.deepStrictEqual([...map.values()], ['bv', 'av']);
49
});
50
51
test('LinkedMap - Touch New two', () => {
52
const map = new LinkedMap<string, string>();
53
map.set('ak', 'av');
54
map.set('bk', 'bv');
55
map.set('ak', 'av', Touch.AsNew);
56
assert.deepStrictEqual([...map.keys()], ['bk', 'ak']);
57
assert.deepStrictEqual([...map.values()], ['bv', 'av']);
58
});
59
60
test('LinkedMap - Touch Old from middle', () => {
61
const map = new LinkedMap<string, string>();
62
map.set('ak', 'av');
63
map.set('bk', 'bv');
64
map.set('ck', 'cv');
65
map.set('bk', 'bv', Touch.AsOld);
66
assert.deepStrictEqual([...map.keys()], ['bk', 'ak', 'ck']);
67
assert.deepStrictEqual([...map.values()], ['bv', 'av', 'cv']);
68
});
69
70
test('LinkedMap - Touch New from middle', () => {
71
const map = new LinkedMap<string, string>();
72
map.set('ak', 'av');
73
map.set('bk', 'bv');
74
map.set('ck', 'cv');
75
map.set('bk', 'bv', Touch.AsNew);
76
assert.deepStrictEqual([...map.keys()], ['ak', 'ck', 'bk']);
77
assert.deepStrictEqual([...map.values()], ['av', 'cv', 'bv']);
78
});
79
80
test('LinkedMap - basics', function () {
81
const map = new LinkedMap<string, any>();
82
83
assert.strictEqual(map.size, 0);
84
85
map.set('1', 1);
86
map.set('2', '2');
87
map.set('3', true);
88
89
const obj = Object.create(null);
90
map.set('4', obj);
91
92
const date = Date.now();
93
map.set('5', date);
94
95
assert.strictEqual(map.size, 5);
96
assert.strictEqual(map.get('1'), 1);
97
assert.strictEqual(map.get('2'), '2');
98
assert.strictEqual(map.get('3'), true);
99
assert.strictEqual(map.get('4'), obj);
100
assert.strictEqual(map.get('5'), date);
101
assert.ok(!map.get('6'));
102
103
map.delete('6');
104
assert.strictEqual(map.size, 5);
105
assert.strictEqual(map.delete('1'), true);
106
assert.strictEqual(map.delete('2'), true);
107
assert.strictEqual(map.delete('3'), true);
108
assert.strictEqual(map.delete('4'), true);
109
assert.strictEqual(map.delete('5'), true);
110
111
assert.strictEqual(map.size, 0);
112
assert.ok(!map.get('5'));
113
assert.ok(!map.get('4'));
114
assert.ok(!map.get('3'));
115
assert.ok(!map.get('2'));
116
assert.ok(!map.get('1'));
117
118
map.set('1', 1);
119
map.set('2', '2');
120
map.set('3', true);
121
122
assert.ok(map.has('1'));
123
assert.strictEqual(map.get('1'), 1);
124
assert.strictEqual(map.get('2'), '2');
125
assert.strictEqual(map.get('3'), true);
126
127
map.clear();
128
129
assert.strictEqual(map.size, 0);
130
assert.ok(!map.get('1'));
131
assert.ok(!map.get('2'));
132
assert.ok(!map.get('3'));
133
assert.ok(!map.has('1'));
134
});
135
136
test('LinkedMap - Iterators', () => {
137
const map = new LinkedMap<number, any>();
138
map.set(1, 1);
139
map.set(2, 2);
140
map.set(3, 3);
141
142
for (const elem of map.keys()) {
143
assert.ok(elem);
144
}
145
146
for (const elem of map.values()) {
147
assert.ok(elem);
148
}
149
150
for (const elem of map.entries()) {
151
assert.ok(elem);
152
}
153
154
{
155
const keys = map.keys();
156
const values = map.values();
157
const entries = map.entries();
158
map.get(1);
159
keys.next();
160
values.next();
161
entries.next();
162
}
163
164
{
165
const keys = map.keys();
166
const values = map.values();
167
const entries = map.entries();
168
map.get(1, Touch.AsNew);
169
170
let exceptions: number = 0;
171
try {
172
keys.next();
173
} catch (err) {
174
exceptions++;
175
}
176
try {
177
values.next();
178
} catch (err) {
179
exceptions++;
180
}
181
try {
182
entries.next();
183
} catch (err) {
184
exceptions++;
185
}
186
187
assert.strictEqual(exceptions, 3);
188
}
189
});
190
191
test('LinkedMap - LRU Cache simple', () => {
192
const cache = new LRUCache<number, number>(5);
193
194
[1, 2, 3, 4, 5].forEach(value => cache.set(value, value));
195
assert.strictEqual(cache.size, 5);
196
cache.set(6, 6);
197
assert.strictEqual(cache.size, 5);
198
assert.deepStrictEqual([...cache.keys()], [2, 3, 4, 5, 6]);
199
cache.set(7, 7);
200
assert.strictEqual(cache.size, 5);
201
assert.deepStrictEqual([...cache.keys()], [3, 4, 5, 6, 7]);
202
const values: number[] = [];
203
[3, 4, 5, 6, 7].forEach(key => values.push(cache.get(key)!));
204
assert.deepStrictEqual(values, [3, 4, 5, 6, 7]);
205
});
206
207
test('LinkedMap - LRU Cache get', () => {
208
const cache = new LRUCache<number, number>(5);
209
210
[1, 2, 3, 4, 5].forEach(value => cache.set(value, value));
211
assert.strictEqual(cache.size, 5);
212
assert.deepStrictEqual([...cache.keys()], [1, 2, 3, 4, 5]);
213
cache.get(3);
214
assert.deepStrictEqual([...cache.keys()], [1, 2, 4, 5, 3]);
215
cache.peek(4);
216
assert.deepStrictEqual([...cache.keys()], [1, 2, 4, 5, 3]);
217
const values: number[] = [];
218
[1, 2, 3, 4, 5].forEach(key => values.push(cache.get(key)!));
219
assert.deepStrictEqual(values, [1, 2, 3, 4, 5]);
220
});
221
222
test('LinkedMap - LRU Cache limit', () => {
223
const cache = new LRUCache<number, number>(10);
224
225
for (let i = 1; i <= 10; i++) {
226
cache.set(i, i);
227
}
228
assert.strictEqual(cache.size, 10);
229
cache.limit = 5;
230
assert.strictEqual(cache.size, 5);
231
assert.deepStrictEqual([...cache.keys()], [6, 7, 8, 9, 10]);
232
cache.limit = 20;
233
assert.strictEqual(cache.size, 5);
234
for (let i = 11; i <= 20; i++) {
235
cache.set(i, i);
236
}
237
assert.deepStrictEqual(cache.size, 15);
238
const values: number[] = [];
239
for (let i = 6; i <= 20; i++) {
240
values.push(cache.get(i)!);
241
assert.strictEqual(cache.get(i), i);
242
}
243
assert.deepStrictEqual([...cache.values()], values);
244
});
245
246
test('LinkedMap - LRU Cache limit with ratio', () => {
247
const cache = new LRUCache<number, number>(10, 0.5);
248
249
for (let i = 1; i <= 10; i++) {
250
cache.set(i, i);
251
}
252
assert.strictEqual(cache.size, 10);
253
cache.set(11, 11);
254
assert.strictEqual(cache.size, 5);
255
assert.deepStrictEqual([...cache.keys()], [7, 8, 9, 10, 11]);
256
const values: number[] = [];
257
[...cache.keys()].forEach(key => values.push(cache.get(key)!));
258
assert.deepStrictEqual(values, [7, 8, 9, 10, 11]);
259
assert.deepStrictEqual([...cache.values()], values);
260
});
261
262
test('LinkedMap - MRU Cache simple', () => {
263
const cache = new MRUCache<number, number>(5);
264
265
[1, 2, 3, 4, 5].forEach(value => cache.set(value, value));
266
assert.strictEqual(cache.size, 5);
267
cache.set(6, 6);
268
assert.strictEqual(cache.size, 5);
269
assert.deepStrictEqual([...cache.keys()], [1, 2, 3, 4, 6]);
270
cache.set(7, 7);
271
assert.strictEqual(cache.size, 5);
272
assert.deepStrictEqual([...cache.keys()], [1, 2, 3, 4, 7]);
273
const values: number[] = [];
274
[1, 2, 3, 4, 7].forEach(key => values.push(cache.get(key)!));
275
assert.deepStrictEqual(values, [1, 2, 3, 4, 7]);
276
});
277
278
test('LinkedMap - MRU Cache get', () => {
279
const cache = new MRUCache<number, number>(5);
280
281
[1, 2, 3, 4, 5].forEach(value => cache.set(value, value));
282
assert.strictEqual(cache.size, 5);
283
assert.deepStrictEqual([...cache.keys()], [1, 2, 3, 4, 5]);
284
cache.get(3);
285
assert.deepStrictEqual([...cache.keys()], [1, 2, 4, 5, 3]);
286
cache.peek(4);
287
assert.deepStrictEqual([...cache.keys()], [1, 2, 4, 5, 3]);
288
const values: number[] = [];
289
[1, 2, 3, 4, 5].forEach(key => values.push(cache.get(key)!));
290
assert.deepStrictEqual(values, [1, 2, 3, 4, 5]);
291
});
292
293
test('LinkedMap - MRU Cache limit with ratio', () => {
294
const cache = new MRUCache<number, number>(10, 0.5);
295
296
for (let i = 1; i <= 10; i++) {
297
cache.set(i, i);
298
}
299
assert.strictEqual(cache.size, 10);
300
cache.set(11, 11);
301
assert.strictEqual(cache.size, 5);
302
assert.deepStrictEqual([...cache.keys()], [1, 2, 3, 4, 11]);
303
const values: number[] = [];
304
[...cache.keys()].forEach(key => values.push(cache.get(key)!));
305
assert.deepStrictEqual(values, [1, 2, 3, 4, 11]);
306
assert.deepStrictEqual([...cache.values()], values);
307
});
308
309
test('LinkedMap - toJSON / fromJSON', () => {
310
let map = new LinkedMap<string, string>();
311
map.set('ak', 'av');
312
map.set('bk', 'bv');
313
map.set('ck', 'cv');
314
315
const json = map.toJSON();
316
map = new LinkedMap<string, string>();
317
map.fromJSON(json);
318
319
let i = 0;
320
map.forEach((value, key) => {
321
if (i === 0) {
322
assert.strictEqual(key, 'ak');
323
assert.strictEqual(value, 'av');
324
} else if (i === 1) {
325
assert.strictEqual(key, 'bk');
326
assert.strictEqual(value, 'bv');
327
} else if (i === 2) {
328
assert.strictEqual(key, 'ck');
329
assert.strictEqual(value, 'cv');
330
}
331
i++;
332
});
333
});
334
335
test('LinkedMap - delete Head and Tail', function () {
336
const map = new LinkedMap<string, number>();
337
338
assert.strictEqual(map.size, 0);
339
340
map.set('1', 1);
341
assert.strictEqual(map.size, 1);
342
map.delete('1');
343
assert.strictEqual(map.get('1'), undefined);
344
assert.strictEqual(map.size, 0);
345
assert.strictEqual([...map.keys()].length, 0);
346
});
347
348
test('LinkedMap - delete Head', function () {
349
const map = new LinkedMap<string, number>();
350
351
assert.strictEqual(map.size, 0);
352
353
map.set('1', 1);
354
map.set('2', 2);
355
assert.strictEqual(map.size, 2);
356
map.delete('1');
357
assert.strictEqual(map.get('2'), 2);
358
assert.strictEqual(map.size, 1);
359
assert.strictEqual([...map.keys()].length, 1);
360
assert.strictEqual([...map.keys()][0], '2');
361
});
362
363
test('LinkedMap - delete Tail', function () {
364
const map = new LinkedMap<string, number>();
365
366
assert.strictEqual(map.size, 0);
367
368
map.set('1', 1);
369
map.set('2', 2);
370
assert.strictEqual(map.size, 2);
371
map.delete('2');
372
assert.strictEqual(map.get('1'), 1);
373
assert.strictEqual(map.size, 1);
374
assert.strictEqual([...map.keys()].length, 1);
375
assert.strictEqual([...map.keys()][0], '1');
376
});
377
378
test('ResourceMap - basics', function () {
379
const map = new ResourceMap<any>();
380
381
const resource1 = URI.parse('some://1');
382
const resource2 = URI.parse('some://2');
383
const resource3 = URI.parse('some://3');
384
const resource4 = URI.parse('some://4');
385
const resource5 = URI.parse('some://5');
386
const resource6 = URI.parse('some://6');
387
388
assert.strictEqual(map.size, 0);
389
390
const res = map.set(resource1, 1);
391
assert.ok(res === map);
392
map.set(resource2, '2');
393
map.set(resource3, true);
394
395
const values = [...map.values()];
396
assert.strictEqual(values[0], 1);
397
assert.strictEqual(values[1], '2');
398
assert.strictEqual(values[2], true);
399
400
let counter = 0;
401
map.forEach((value, key, mapObj) => {
402
assert.strictEqual(value, values[counter++]);
403
assert.ok(URI.isUri(key));
404
assert.ok(map === mapObj);
405
});
406
407
const obj = Object.create(null);
408
map.set(resource4, obj);
409
410
const date = Date.now();
411
map.set(resource5, date);
412
413
assert.strictEqual(map.size, 5);
414
assert.strictEqual(map.get(resource1), 1);
415
assert.strictEqual(map.get(resource2), '2');
416
assert.strictEqual(map.get(resource3), true);
417
assert.strictEqual(map.get(resource4), obj);
418
assert.strictEqual(map.get(resource5), date);
419
assert.ok(!map.get(resource6));
420
421
map.delete(resource6);
422
assert.strictEqual(map.size, 5);
423
assert.ok(map.delete(resource1));
424
assert.ok(map.delete(resource2));
425
assert.ok(map.delete(resource3));
426
assert.ok(map.delete(resource4));
427
assert.ok(map.delete(resource5));
428
429
assert.strictEqual(map.size, 0);
430
assert.ok(!map.get(resource5));
431
assert.ok(!map.get(resource4));
432
assert.ok(!map.get(resource3));
433
assert.ok(!map.get(resource2));
434
assert.ok(!map.get(resource1));
435
436
map.set(resource1, 1);
437
map.set(resource2, '2');
438
map.set(resource3, true);
439
440
assert.ok(map.has(resource1));
441
assert.strictEqual(map.get(resource1), 1);
442
assert.strictEqual(map.get(resource2), '2');
443
assert.strictEqual(map.get(resource3), true);
444
445
map.clear();
446
447
assert.strictEqual(map.size, 0);
448
assert.ok(!map.get(resource1));
449
assert.ok(!map.get(resource2));
450
assert.ok(!map.get(resource3));
451
assert.ok(!map.has(resource1));
452
453
map.set(resource1, false);
454
map.set(resource2, 0);
455
456
assert.ok(map.has(resource1));
457
assert.ok(map.has(resource2));
458
});
459
460
test('ResourceMap - files (do NOT ignorecase)', function () {
461
const map = new ResourceMap<any>();
462
463
const fileA = URI.parse('file://some/filea');
464
const fileB = URI.parse('some://some/other/fileb');
465
const fileAUpper = URI.parse('file://SOME/FILEA');
466
467
map.set(fileA, 'true');
468
assert.strictEqual(map.get(fileA), 'true');
469
470
assert.ok(!map.get(fileAUpper));
471
472
assert.ok(!map.get(fileB));
473
474
map.set(fileAUpper, 'false');
475
assert.strictEqual(map.get(fileAUpper), 'false');
476
477
assert.strictEqual(map.get(fileA), 'true');
478
479
const windowsFile = URI.file('c:\\test with %25\\c#code');
480
const uncFile = URI.file('\\\\shäres\\path\\c#\\plugin.json');
481
482
map.set(windowsFile, 'true');
483
map.set(uncFile, 'true');
484
485
assert.strictEqual(map.get(windowsFile), 'true');
486
assert.strictEqual(map.get(uncFile), 'true');
487
});
488
489
test('ResourceMap - files (ignorecase)', function () {
490
const map = new ResourceMap<any>(uri => extUriIgnorePathCase.getComparisonKey(uri));
491
492
const fileA = URI.parse('file://some/filea');
493
const fileB = URI.parse('some://some/other/fileb');
494
const fileAUpper = URI.parse('file://SOME/FILEA');
495
496
map.set(fileA, 'true');
497
assert.strictEqual(map.get(fileA), 'true');
498
499
assert.strictEqual(map.get(fileAUpper), 'true');
500
501
assert.ok(!map.get(fileB));
502
503
map.set(fileAUpper, 'false');
504
assert.strictEqual(map.get(fileAUpper), 'false');
505
506
assert.strictEqual(map.get(fileA), 'false');
507
508
const windowsFile = URI.file('c:\\test with %25\\c#code');
509
const uncFile = URI.file('\\\\shäres\\path\\c#\\plugin.json');
510
511
map.set(windowsFile, 'true');
512
map.set(uncFile, 'true');
513
514
assert.strictEqual(map.get(windowsFile), 'true');
515
assert.strictEqual(map.get(uncFile), 'true');
516
});
517
518
test('ResourceMap - files (ignorecase, BUT preservecase)', function () {
519
const map = new ResourceMap<number>(uri => extUriIgnorePathCase.getComparisonKey(uri));
520
521
const fileA = URI.parse('file://some/filea');
522
const fileAUpper = URI.parse('file://SOME/FILEA');
523
524
map.set(fileA, 1);
525
assert.strictEqual(map.get(fileA), 1);
526
assert.strictEqual(map.get(fileAUpper), 1);
527
assert.deepStrictEqual(Array.from(map.keys()).map(String), [fileA].map(String));
528
assert.deepStrictEqual(Array.from(map), [[fileA, 1]]);
529
530
map.set(fileAUpper, 1);
531
assert.strictEqual(map.get(fileA), 1);
532
assert.strictEqual(map.get(fileAUpper), 1);
533
assert.deepStrictEqual(Array.from(map.keys()).map(String), [fileAUpper].map(String));
534
assert.deepStrictEqual(Array.from(map), [[fileAUpper, 1]]);
535
});
536
537
test('mapsStrictEqualIgnoreOrder', () => {
538
const map1 = new Map();
539
const map2 = new Map();
540
541
assert.strictEqual(mapsStrictEqualIgnoreOrder(map1, map2), true);
542
543
map1.set('foo', 'bar');
544
assert.strictEqual(mapsStrictEqualIgnoreOrder(map1, map2), false);
545
546
map2.set('foo', 'bar');
547
assert.strictEqual(mapsStrictEqualIgnoreOrder(map1, map2), true);
548
549
map2.set('bar', 'foo');
550
assert.strictEqual(mapsStrictEqualIgnoreOrder(map1, map2), false);
551
552
map1.set('bar', 'foo');
553
assert.strictEqual(mapsStrictEqualIgnoreOrder(map1, map2), true);
554
});
555
});
556
557
suite('BidirectionalMap', () => {
558
ensureNoDisposablesAreLeakedInTestSuite();
559
560
test('should set and get values correctly', () => {
561
const map = new BidirectionalMap<string, number>();
562
map.set('one', 1);
563
map.set('two', 2);
564
map.set('three', 3);
565
566
assert.strictEqual(map.get('one'), 1);
567
assert.strictEqual(map.get('two'), 2);
568
assert.strictEqual(map.get('three'), 3);
569
});
570
571
test('should get keys by value correctly', () => {
572
const map = new BidirectionalMap<string, number>();
573
map.set('one', 1);
574
map.set('two', 2);
575
map.set('three', 3);
576
577
assert.strictEqual(map.getKey(1), 'one');
578
assert.strictEqual(map.getKey(2), 'two');
579
assert.strictEqual(map.getKey(3), 'three');
580
});
581
582
test('should delete values correctly', () => {
583
const map = new BidirectionalMap<string, number>();
584
map.set('one', 1);
585
map.set('two', 2);
586
map.set('three', 3);
587
588
assert.strictEqual(map.delete('one'), true);
589
assert.strictEqual(map.get('one'), undefined);
590
assert.strictEqual(map.getKey(1), undefined);
591
592
assert.strictEqual(map.delete('two'), true);
593
assert.strictEqual(map.get('two'), undefined);
594
assert.strictEqual(map.getKey(2), undefined);
595
596
assert.strictEqual(map.delete('three'), true);
597
assert.strictEqual(map.get('three'), undefined);
598
assert.strictEqual(map.getKey(3), undefined);
599
});
600
601
test('should handle non-existent keys correctly', () => {
602
const map = new BidirectionalMap<string, number>();
603
map.set('one', 1);
604
map.set('two', 2);
605
map.set('three', 3);
606
607
assert.strictEqual(map.get('four'), undefined);
608
assert.strictEqual(map.getKey(4), undefined);
609
assert.strictEqual(map.delete('four'), false);
610
});
611
612
test('should handle forEach correctly', () => {
613
const map = new BidirectionalMap<string, number>();
614
map.set('one', 1);
615
map.set('two', 2);
616
map.set('three', 3);
617
618
const keys: string[] = [];
619
const values: number[] = [];
620
map.forEach((value, key) => {
621
keys.push(key);
622
values.push(value);
623
});
624
625
assert.deepStrictEqual(keys, ['one', 'two', 'three']);
626
assert.deepStrictEqual(values, [1, 2, 3]);
627
});
628
629
test('should handle clear correctly', () => {
630
const map = new BidirectionalMap<string, number>();
631
map.set('one', 1);
632
map.set('two', 2);
633
map.set('three', 3);
634
635
map.clear();
636
637
assert.strictEqual(map.get('one'), undefined);
638
assert.strictEqual(map.get('two'), undefined);
639
assert.strictEqual(map.get('three'), undefined);
640
assert.strictEqual(map.getKey(1), undefined);
641
assert.strictEqual(map.getKey(2), undefined);
642
assert.strictEqual(map.getKey(3), undefined);
643
});
644
});
645
646
suite('SetMap', () => {
647
648
ensureNoDisposablesAreLeakedInTestSuite();
649
650
test('add and get', () => {
651
const setMap = new SetMap<string, number>();
652
setMap.add('a', 1);
653
setMap.add('a', 2);
654
setMap.add('b', 3);
655
assert.deepStrictEqual([...setMap.get('a')], [1, 2]);
656
assert.deepStrictEqual([...setMap.get('b')], [3]);
657
});
658
659
test('delete', () => {
660
const setMap = new SetMap<string, number>();
661
setMap.add('a', 1);
662
setMap.add('a', 2);
663
setMap.add('b', 3);
664
setMap.delete('a', 1);
665
assert.deepStrictEqual([...setMap.get('a')], [2]);
666
setMap.delete('a', 2);
667
assert.deepStrictEqual([...setMap.get('a')], []);
668
});
669
670
test('forEach', () => {
671
const setMap = new SetMap<string, number>();
672
setMap.add('a', 1);
673
setMap.add('a', 2);
674
setMap.add('b', 3);
675
let sum = 0;
676
setMap.forEach('a', value => sum += value);
677
assert.strictEqual(sum, 3);
678
});
679
680
test('get empty set', () => {
681
const setMap = new SetMap<string, number>();
682
assert.deepStrictEqual([...setMap.get('a')], []);
683
});
684
});
685
686
suite('NKeyMap', () => {
687
ensureNoDisposablesAreLeakedInTestSuite();
688
689
test('set and get', () => {
690
const map = new NKeyMap<number, [string, string, string, string]>();
691
map.set(1, 'a', 'b', 'c', 'd');
692
map.set(2, 'a', 'c', 'c', 'd');
693
map.set(3, 'b', 'e', 'f', 'g');
694
assert.strictEqual(map.get('a', 'b', 'c', 'd'), 1);
695
assert.strictEqual(map.get('a', 'c', 'c', 'd'), 2);
696
assert.strictEqual(map.get('b', 'e', 'f', 'g'), 3);
697
assert.strictEqual(map.get('a', 'b', 'c', 'a'), undefined);
698
});
699
700
test('clear', () => {
701
const map = new NKeyMap<number, [string, string, string, string]>();
702
map.set(1, 'a', 'b', 'c', 'd');
703
map.set(2, 'a', 'c', 'c', 'd');
704
map.set(3, 'b', 'e', 'f', 'g');
705
map.clear();
706
assert.strictEqual(map.get('a', 'b', 'c', 'd'), undefined);
707
assert.strictEqual(map.get('a', 'c', 'c', 'd'), undefined);
708
assert.strictEqual(map.get('b', 'e', 'f', 'g'), undefined);
709
});
710
711
test('values', () => {
712
const map = new NKeyMap<number, [string, string, string, string]>();
713
map.set(1, 'a', 'b', 'c', 'd');
714
map.set(2, 'a', 'c', 'c', 'd');
715
map.set(3, 'b', 'e', 'f', 'g');
716
assert.deepStrictEqual(Array.from(map.values()), [1, 2, 3]);
717
});
718
719
test('toString', () => {
720
const map = new NKeyMap<number, [string, string, string]>();
721
map.set(1, 'f', 'o', 'o');
722
map.set(2, 'b', 'a', 'r');
723
map.set(3, 'b', 'a', 'z');
724
map.set(3, 'b', 'o', 'o');
725
assert.strictEqual(map.toString(), [
726
'f: ',
727
' o: ',
728
' o: 1',
729
'b: ',
730
' a: ',
731
' r: 2',
732
' z: 3',
733
' o: ',
734
' o: 3',
735
'',
736
].join('\n'));
737
});
738
});
739
740