Repository navigation
Expand file tree
/
Copy pathmap-tests.c3
More file actions
136 lines (115 loc) · 3.08 KB
/
Copy pathmap-tests.c3
File metadata and controls
136 lines (115 loc) · 3.08 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
module test;
import std::collections;
import unordered_map;
import std::io;
fn void test_unordered_map_basic() @test {
test_map_basic(UnorderedMap{String, usz});
}
fn void test_unordered_map_two_hang() @test {
test_map_two_hang(UnorderedMap{String, usz});
}
macro debug_cells(map) {
foreach (idx, cell : map.cells) {
io::printfn("idx %d", idx);
io::printfn("key %s", cell.entry.key);
io::printfn("value %d", cell.entry.value);
io::printfn("used %s", cell.used);
}
}
// Repro of example that hangs.
// 2 is only capacity where items can == capacity.
// A result is that has_key will never hit an empty
// slot. As currently written, it will continue
// to loop.
macro test_map_two_hang($MapImpl) {
$MapImpl map;
map.init(tmem, capacity: 2);
map.set("AC", 1);
map.set("AC", 2);
map.set("ACGT", 3);
assert(map.has_key("A"));
}
macro test_map_basic($MapImpl) {
$MapImpl map;
map.init(tmem, capacity: 2);
map.set("A", 0);
map.set("AC", 1);
map.set("AC", 2);
map.set("ACGT", 3);
assert(map.has_key("A"));
assert(map.has_key("AC"));
assert(map.has_key("ACGT"));
assert(!map.has_key("ZZZZ"));
assert(map.get("A")!! == 0);
assert(map.get("AC")!! == 2);
assert(map.get("ACGT")!! == 3);
usz total = 0;
map.@each(; String k, usz v) {
total += 1;
};
assert(total == 3);
assert(map.len() == 3, "Found %d, expected 3", map.len());
map.remove("A");
assert(map.len() == 2);
assert(!map.has_key("A"));
}
//fn void test_unordered_map_copy() @test {
// test_map_copy(UnorderedMap{String, usz});
//}
macro test_map_copy($MapImpl) {
$MapImpl old_map;
old_map.init(tmem, capacity: 2);
old_map.set("A", 0);
old_map.set("AC", 1);
old_map.set("ACGT", 2);
$MapImpl new_map = old_map.copy(tmem);
assert(new_map.has_key("A"));
assert(new_map.has_key("AC"));
assert(new_map.has_key("ACGT"));
assert(new_map.get("A")!! == 0);
assert(new_map.get("AC")!! == 1);
assert(new_map.get("ACGT")!! == 2);
assert(new_map.len() == 3, "Found %d, expected 3", new_map.len());
}
alias IntList = List{int};
fn void test_unordered_map_get_ref_or_default() @test {
test_map_get_ref_or_default(UnorderedMap{String, IntList});
}
macro test_map_get_ref_or_default($MapImpl) {
$MapImpl map;
map.init(tmem, capacity: 2);
IntList intlist;
intlist.tinit();
map.get_ref_or_default("A", intlist);
assert(map.has_key("A"));
assert(map.get("A")!!.len() == 0);
map.get_ref("A")!!.push(1);
assert(map.get("A")!![0] == 1);
}
fn void test_unordered_map_clear() @test {
test_map_clear(UnorderedMap{int, int});
}
macro test_map_clear($MapImpl) {
$MapImpl map;
map.init(tmem, capacity: 2);
map.set(0, 10);
map.set(1, 11);
assert(map.get(0)!! == 10);
assert(map.get(1)!! == 11);
assert(map.len() == 2);
map.clear();
assert(map.len() == 0);
map.set(2, 12);
map.set(3, 13);
map.set(4, 14);
assert(map.get(2)!! == 12);
assert(map.get(3)!! == 13);
assert(map.get(4)!! == 14);
assert(map.len() == 3);
}
fn void test_fingerprint() @test {
ulong hash = ulong.max;
char fingerprint = unordered_map::take_fingerprint{int, int}(hash);
assert(fingerprint == char.max >> 1);
assert(fingerprint == (char)(hash >> (64 - 7)));
}