-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsortedMap.test.ts
72 lines (62 loc) · 1.78 KB
/
sortedMap.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
import SortedMap from "./sortedMap.ts";
import { assertEquals } from "testing/asserts.ts";
Deno.test("SortedMap", () => {
const entries: [string, string][] = [
["a", "foo"],
["b", "bar"],
["c", "baz"],
];
const keys = () => entries.map(([k]) => k);
const values = () => entries.map(([, v]) => v);
assertEquals(
[
...new SortedMap(
(_va, _vb, a, b) => a.localeCompare(b),
[entries[1], entries[0], entries[2]],
),
],
entries,
);
const map = new SortedMap<string, string>((_va, _vb, a, b) =>
a.localeCompare(b)
);
map.set(...entries[1]);
map.set(...entries[0]);
map.set(...entries[2]);
assertEquals([...map], entries);
assertEquals([...map.entries()], entries);
assertEquals([...map.keys()], keys());
assertEquals([...map.values()], values());
assertEquals(
map.map((v) => v),
values(),
);
assertEquals(
map.map((_, k) => k),
keys(),
);
const fe: any[] = [];
map.forEach((v, k) => fe.push([k, v]));
assertEquals(fe, entries);
map.delete("b");
entries.splice(1, 1);
assertEquals([...map], entries);
assertEquals([...map.entries()], entries);
assertEquals([...map.keys()], keys());
assertEquals([...map.values()], values());
const nums = new SortedMap<string, { value: number }>(
(a, b) => a.value - b.value,
);
nums.set("a", { value: 2 });
nums.set("b", { value: 1 });
nums.set("c", { value: 3 });
assertEquals([...nums.keys()], ["b", "a", "c"]);
nums.set("a", { value: 0 });
assertEquals([...nums.keys()], ["a", "b", "c"]);
nums.get("c")!.value = -1;
assertEquals([...nums.keys()], ["a", "b", "c"]);
assertEquals(nums.update("c"), true);
nums.get("c")!.value = -2;
assertEquals(nums.update("c"), false);
assertEquals([...nums.keys()], ["c", "a", "b"]);
});