-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiterable_weak_set.test.ts
180 lines (151 loc) · 4.31 KB
/
iterable_weak_set.test.ts
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
import { assert, assertEquals, assertFalse } from "@std/assert";
import { IterableWeakSet } from "./iterable_weak_set.ts";
function* iterate<T>(items: T[]): IterableIterator<T> {
for (const item of items) {
yield item;
}
}
Deno.test("IterableWeakSet, constructor", () => {
assertEquals(new IterableWeakSet().size, 0);
assertEquals(new IterableWeakSet(undefined).size, 0);
assertEquals(new IterableWeakSet(null).size, 0);
assertEquals(new IterableWeakSet([]).size, 0);
assertEquals(new IterableWeakSet(iterate([])).size, 0);
assertEquals(new IterableWeakSet([{}]).size, 1);
assertEquals(new IterableWeakSet(iterate([{}])).size, 1);
});
Deno.test("IterableWeakSet, comparison Set, WeakSet", () => {
const set = new Set();
const wset = new WeakSet();
const iwset = new IterableWeakSet();
assertEquals(set.size, 0);
// assertEquals(wset.size, 0);
assertEquals(iwset.size, 0);
const obj1 = {};
const obj2 = {};
assertEquals(set.add(obj1), set);
assertEquals(wset.add(obj1), wset);
assertEquals(iwset.add(obj1), iwset);
assertEquals(set.size, 1);
// assertEquals(wset.size, 0);
assertEquals(iwset.size, 1);
// add the same object again
assertEquals(set.add(obj1), set);
assertEquals(wset.add(obj1), wset);
assertEquals(iwset.add(obj1), iwset);
assertEquals(set.size, 1);
// assertEquals(wset.size, 0);
assertEquals(iwset.size, 1);
assertEquals(set.add(obj2), set);
assertEquals(wset.add(obj2), wset);
assertEquals(iwset.add(obj2), iwset);
assertEquals(set.size, 2);
// assertEquals(wset.size, 0);
assertEquals(iwset.size, 2);
assert(set.has(obj1));
assert(wset.has(obj1));
assert(iwset.has(obj1));
// delete the object
assert(set.delete(obj1));
assert(wset.delete(obj1));
assert(iwset.delete(obj1));
assertEquals(set.size, 1);
// assertEquals(wset.size, 0);
assertEquals(iwset.size, 1);
assertFalse(set.delete(obj1));
assertFalse(wset.delete(obj1));
assertFalse(iwset.delete(obj1));
assertEquals(set.size, 1);
// assertEquals(wset.size, 0);
assertEquals(iwset.size, 1);
assertFalse(set.has(obj1));
assertFalse(wset.has(obj1));
assertFalse(iwset.has(obj1));
assertFalse(set.clear());
// assertFalse(wset.clear());
assertFalse(iwset.clear());
assertEquals(set.size, 0);
// assertEquals(wset.size, 0);
assertEquals(iwset.size, 0);
assertEquals(set.toString(), "[object Set]");
assertEquals(wset.toString(), "[object WeakSet]");
assertEquals(iwset.toString(), "[object IterableWeakSet]");
});
Deno.test("IterableWeakSet, iterable", () => {
const objs = [{}, {}, {}];
const sets = [
new Set(objs),
new IterableWeakSet(objs),
];
for (const set of sets) {
assertEquals(set.size, 3);
{
let i = 0;
set.forEach(function (this: unknown, v1, v2, s) {
assert(this === undefined);
assert(v1 === objs[i]);
assert(v2 === objs[i]);
assert(s === set);
i++;
});
assertFalse(i === 0);
}
{
let i = 0;
set.forEach(function (this: unknown, v1, v2, s) {
assert(this === ":D");
assert(v1 === objs[i]);
assert(v2 === objs[i]);
assert(s === set);
i++;
}, ":D");
assertFalse(i === 0);
}
{
let i = 0;
for (const obj of set) {
assert(obj === objs[i++]);
}
assertFalse(i === 0);
}
{
let i = 0;
for (const obj of set.keys()) {
assert(obj === objs[i++]);
}
assertFalse(i === 0);
}
{
let i = 0;
for (const obj of set.values()) {
assert(obj === objs[i++]);
}
assertFalse(i === 0);
}
{
let i = 0;
for (const tuple of set.entries()) {
assert(tuple[0] === objs[i]);
assert(tuple[1] === objs[i++]);
}
assertFalse(i === 0);
}
}
});
Deno.test("IterableWeakSet, garbage collect", async () => {
let removedCount = 0;
let insertedCount = 0;
const register = new FinalizationRegistry(() => {
removedCount++;
});
const set = new IterableWeakSet();
for (let i = 0; removedCount < 100; i++) {
await new Promise((resolve) => setTimeout(resolve, 16));
const data = {};
set.add(data);
register.register(data, i);
insertedCount++;
}
assertEquals(insertedCount - removedCount, set.size);
assertEquals([...set].length, set.size);
});