-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathset.spec.ts
More file actions
463 lines (391 loc) · 12.9 KB
/
set.spec.ts
File metadata and controls
463 lines (391 loc) · 12.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
import * as util from "../../util";
test("set constructor", () => {
util.testFunction`
let myset = new Set();
return myset.size;
`.expectToMatchJsResult();
});
test("set iterable constructor", () => {
util.testFunction`
let myset = new Set(["a", "b"]);
return myset.has("a") || myset.has("b");
`.expectToMatchJsResult();
});
test("set iterable constructor set", () => {
util.testFunction`
let myset = new Set(new Set(["a", "b"]));
return myset.has("a") || myset.has("b");
`.expectToMatchJsResult();
});
test("set add", () => {
util.testFunction`
let myset = new Set();
myset.add("a");
return myset.has("a");
`.expectToMatchJsResult();
});
test("set clear", () => {
util.testFunction`
let myset = new Set(["a", "b"]);
myset.clear();
return { size: myset.size, has: !myset.has("a") && !myset.has("b") };
`.expectToMatchJsResult();
});
test("set delete", () => {
util.testFunction`
let myset = new Set(["a", "b"]);
myset.delete("a");
return myset.has("b") && !myset.has("a");
`.expectToMatchJsResult();
});
test("set entries", () => {
util.testFunction`
let myset = new Set([5, 6, 7]);
let count = 0;
for (const [key, value] of myset.entries()) { count += key + value; }
return count;
`.expectToMatchJsResult();
});
test("set foreach", () => {
util.testFunction`
let myset = new Set([2, 3, 4]);
let count = 0;
myset.forEach(i => { count += i; });
return count;
`.expectToMatchJsResult();
});
test("set foreach keys", () => {
util.testFunction`
let myset = new Set([2, 3, 4]);
let count = 0;
myset.forEach((value, key) => { count += key; });
return count;
`.expectToMatchJsResult();
});
test("set has", () => {
util.testFunction`
let myset = new Set(["a", "c"]);
return myset.has("a");
`.expectToMatchJsResult();
});
test("set has after deleting keys", () => {
util.testFunction`
let myset = new Set(["a", "c"]);
const results = [myset.has("c")];
myset.delete("c");
results.push(myset.has("c"));
myset.delete("a");
results.push(myset.has("a"))
return results;
`.expectToMatchJsResult();
});
test("set has false", () => {
util.testFunction`
let myset = new Set();
return myset.has("a");
`.expectToMatchJsResult();
});
test("set has null", () => {
util.testFunction`
let myset = new Set(["a", "c"]);
return myset.has(null);
`.expectToMatchJsResult();
});
test("set keys", () => {
util.testFunction`
let myset = new Set([5, 6, 7]);
let count = 0;
for (const key of myset.keys()) { count += key; }
return count;
`.expectToMatchJsResult();
});
test("set values", () => {
util.testFunction`
let myset = new Set([5, 6, 7]);
let count = 0;
for (const value of myset.values()) { count += value; }
return count;
`.expectToMatchJsResult();
});
test.each([
"let m = new Set()",
"let m = new Set(); m.add(1)",
"let m = new Set([1, 2])",
"let m = new Set([1, 2]); m.clear()",
"let m = new Set([1, 2]); m.delete(2)",
])("set size (%p)", code => {
util.testFunction`${code}; return m.size`.expectToMatchJsResult();
});
const iterationMethods = ["entries", "keys", "values"];
describe.each(iterationMethods)("set.%s() preserves insertion order", iterationMethod => {
test("basic", () => {
util.testFunction`
const myset = new Set();
myset.add("x");
myset.add("a");
myset.add(4);
myset.add("b");
myset.add(1);
myset.add("a");
myset.delete("b");
return [...myset.${iterationMethod}()];
`.expectToMatchJsResult();
});
test("after removing last", () => {
util.testFunction`
const myset = new Set();
myset.add("x");
myset.add("a");
myset.add(4);
myset.delete(4);
return [...myset.${iterationMethod}()];
`.expectToMatchJsResult();
});
test("after removing first", () => {
util.testFunction`
const myset = new Set();
myset.add("x");
myset.add("a");
myset.add(4);
myset.delete("x");
return [...myset.${iterationMethod}()];
`.expectToMatchJsResult();
});
test("after removing all", () => {
util.testFunction`
const myset = new Set();
myset.add("x");
myset.add("a");
myset.delete("a");
myset.delete("x");
return [...myset.${iterationMethod}()];
`.expectToMatchJsResult();
});
});
describe.each(iterationMethods)("set.%s() handles mutation", iterationMethod => {
test("iterator persists after delete", () => {
util.testFunction`
const set1 = new Set<string | number>();
set1.add(42);
set1.add("forty two");
const iterator1 = set1.${iterationMethod}();
set1.delete(42);
return iterator1.next().value;
`.expectToMatchJsResult();
});
test("iterator with delete and add between iterations", () => {
util.testFunction`
const set = new Set([1, 2, 3]);
const iter = set.${iterationMethod}();
iter.next(); // 1
set.delete(2);
set.add(4);
const results: IteratorResult<any>[] = [];
let r = iter.next();
while (!r.done) { results.push({ done: r.done, value: r.value }); r = iter.next(); }
return results;
`.expectToMatchJsResult();
});
test("iterator does not restart after exhaustion", () => {
util.testFunction`
const set = new Set([1, 2]);
const iter = set.${iterationMethod}();
const results: boolean[] = [];
results.push(iter.next().done!);
results.push(iter.next().done!);
results.push(iter.next().done!); // should be done
results.push(iter.next().done!); // should still be done, not restart
return results;
`.expectToMatchJsResult();
});
});
test("instanceof Set without creating set", () => {
util.testFunction`
const myset = 3 as any;
return myset instanceof Set;
`.expectToMatchJsResult();
});
describe("new ECMAScript Set methods", () => {
test("union", () => {
util.testFunction`
const set1 = new Set([1,2,3,4,5,6]);
const set2 = new Set([4,5,6,7,8,9]);
const intersection = set1.union(set2);
return [...intersection];
`.expectToEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
});
test("union with empty sets", () => {
util.testFunction`
const set1 = new Set([1,2,3,4,5,6]);
const set2 = new Set([]);
const intersection = set1.union(set2);
return [...intersection];
`.expectToEqual([1, 2, 3, 4, 5, 6]);
util.testFunction`
const set1 = new Set([]);
const set2 = new Set([4,5,6,7,8,9]);
const intersection = set1.union(set2);
return [...intersection];
`.expectToEqual([4, 5, 6, 7, 8, 9]);
});
test("intersection", () => {
util.testFunction`
const set1 = new Set([1,2,3,4,5,6]);
const set2 = new Set([4,5,6,7,8,9]);
const intersection = set1.intersection(set2);
return [...intersection];
`.expectToEqual([4, 5, 6]);
});
test("intersection with empty sets", () => {
util.testFunction`
const set1 = new Set([1,2,3,4,5,6]);
const set2 = new Set([]);
const intersection = set1.intersection(set2);
return [...intersection];
`.expectToEqual([]);
util.testFunction`
const set1 = new Set([]);
const set2 = new Set([4,5,6,7,8,9]);
const intersection = set1.intersection(set2);
return [...intersection];
`.expectToEqual([]);
});
test("difference", () => {
util.testFunction`
const set1 = new Set([1,2,3,4,5,6]);
const set2 = new Set([4,5,6,7,8,9]);
const intersection = set1.difference(set2);
return [...intersection];
`.expectToEqual([1, 2, 3]);
});
test("symmetricDifference", () => {
util.testFunction`
const set1 = new Set([1,2,3,4,5,6]);
const set2 = new Set([4,5,6,7,8,9]);
const intersection = set1.symmetricDifference(set2);
return [...intersection];
`.expectToEqual([1, 2, 3, 7, 8, 9]);
});
test("isSubsetOf", () => {
util.testFunction`
const set1 = new Set([3,4,5,6]);
const set2 = new Set([1,2,3,4,5,6,7,8,9]);
return {
set1SubsetOfSet2: set1.isSubsetOf(set2),
set2SubsetOfSet1: set2.isSubsetOf(set1),
};
`.expectToEqual({
set1SubsetOfSet2: true,
set2SubsetOfSet1: false,
});
});
test("isSubsetOf equal", () => {
util.testFunction`
const set1 = new Set([1,2,3,4,5,6,7,8,9]);
const set2 = new Set([1,2,3,4,5,6,7,8,9]);
return {
set1SubsetOfSet2: set1.isSubsetOf(set2),
set2SubsetOfSet1: set2.isSubsetOf(set1),
};
`.expectToEqual({
set1SubsetOfSet2: true,
set2SubsetOfSet1: true,
});
});
test("isSubsetOf empty", () => {
util.testFunction`
const set1 = new Set([]);
const set2 = new Set([1,2,3]);
return {
set1SubsetOfSet2: set1.isSubsetOf(set2),
set2SubsetOfSet1: set2.isSubsetOf(set1),
};
`.expectToEqual({
set1SubsetOfSet2: true,
set2SubsetOfSet1: false,
});
});
test("isSupersetOf", () => {
util.testFunction`
const set1 = new Set([3,4,5,6]);
const set2 = new Set([1,2,3,4,5,6,7,8,9]);
return {
set1SupersetOfSet2: set1.isSupersetOf(set2),
set2SupersetOfSet1: set2.isSupersetOf(set1),
};
`.expectToEqual({
set1SupersetOfSet2: false,
set2SupersetOfSet1: true,
});
});
test("isSupersetOf equal", () => {
util.testFunction`
const set1 = new Set([1,2,3,4,5,6,7,8,9]);
const set2 = new Set([1,2,3,4,5,6,7,8,9]);
return {
set1SupersetOfSet2: set1.isSupersetOf(set2),
set2SupersetOfSet1: set2.isSupersetOf(set1),
};
`.expectToEqual({
set1SupersetOfSet2: true,
set2SupersetOfSet1: true,
});
});
test("isSupersetOf empty", () => {
util.testFunction`
const set1 = new Set([]);
const set2 = new Set([1,2,3]);
return {
set1SupersetOfSet2: set1.isSupersetOf(set2),
set2SupersetOfSet1: set2.isSupersetOf(set1),
};
`.expectToEqual({
set1SupersetOfSet2: false,
set2SupersetOfSet1: true,
});
});
test("isDisjointFrom", () => {
util.testFunction`
const set1 = new Set([3,4,5,6]);
const set2 = new Set([7,8,9]);
const set3 = new Set([1,2,3,4]);
return {
set1DisjointFromSet2: set1.isDisjointFrom(set2),
set2DisjointFromSet1: set2.isDisjointFrom(set1),
set1DisjointFromSet3: set1.isDisjointFrom(set3),
set3DisjointFromSet1: set3.isDisjointFrom(set1),
};
`.expectToEqual({
set1DisjointFromSet2: true,
set2DisjointFromSet1: true,
set1DisjointFromSet3: false,
set3DisjointFromSet1: false,
});
});
test("isDisjointFrom equal", () => {
util.testFunction`
const set1 = new Set([1,2,3,4,5,6,7,8,9]);
const set2 = new Set([1,2,3,4,5,6,7,8,9]);
return {
set1DisjointFromSet2: set1.isDisjointFrom(set2),
set2DisjointFromSet1: set2.isDisjointFrom(set1),
};
`.expectToEqual({
set1DisjointFromSet2: false,
set2DisjointFromSet1: false,
});
});
test("isDisjointFrom empty", () => {
util.testFunction`
const set1 = new Set([]);
const set2 = new Set([1,2,3]);
return {
set1DisjointFromSet2: set1.isDisjointFrom(set2),
set2DisjointFromSet1: set2.isDisjointFrom(set1),
};
`.expectToEqual({
set1DisjointFromSet2: true,
set2DisjointFromSet1: true,
});
});
});