-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap-tests-std.c3
More file actions
95 lines (84 loc) · 1.78 KB
/
Copy pathmap-tests-std.c3
File metadata and controls
95 lines (84 loc) · 1.78 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
module test_std @test;
import std::collections::list;
import std::collections::map;
import std::sort;
import std::io;
import unordered_map;
alias TestHashMap = UnorderedMap{String, usz};
struct MapTest
{
String key;
usz value;
}
alias ListMap = List{MapTest};
fn void map()
{
TestHashMap m;
assert(!m.is_initialized());
m.tinit();
assert(m.is_initialized());
assert(m.is_empty());
assert(m.len() == 0);
m.set("a", 1);
assert(!m.is_empty());
assert(m.len() == 1);
m.remove("a");
assert(m.is_empty());
MapTest[] tcases = { {"key1", 0}, {"key2", 1}, {"key3", 2} };
foreach (tc : tcases)
{
m.set(tc.key, tc.value);
}
assert(m.len() == tcases.len);
foreach (tc : tcases)
{
usz v = m.get(tc.key)!!;
assert(tc.value == v);
}
ListMap list;
list.tinit();
m.@each(;String key, usz value)
{
list.push({key, value});
};
assert(list.len() == tcases.len);
quicksort(&list, fn int (MapTest a, MapTest b) => (int)(a.value - b.value));
foreach (i, tc : tcases)
{
assert(tc.key == list[i].key);
assert(tc.value == list[i].value);
}
}
alias FooMap = UnorderedMap{char, Foobar};
enum Foobar : inline char
{
FOO,
BAR,
BAZ
}
enum Foobar2 : const inline int
{
ABC = 3,
DEF = 5,
}
fn void map_inline_enum()
{
FooMap x;
x[Foobar.BAZ] = FOO;
x[Foobar2.ABC] = BAR;
// test should be fixed; depends on order
//test::eq(string::tformat("%s", x), "{ 2: FOO, 3: BAR }");
test::eq(x.len(), 2);
test::eq(x.get(Foobar.BAZ)!!, Foobar.FOO);
test::eq(x.get(Foobar2.ABC)!!, Foobar.BAR);
x.free();
}
fn void map_remove()
{
TestHashMap m;
assert(!@ok(m.remove("A")));
m.tinit();
assert(!@ok(m.remove("A")));
m.set("A", 0);
assert(@ok(m.remove("A")));
}