1 | #include "lib.h" |
---|
2 | #include "map.h" |
---|
3 | |
---|
4 | struct KeyMapper { |
---|
5 | Str *operator()(Int key) { |
---|
6 | return ToStr(key); |
---|
7 | } |
---|
8 | } keymapper; |
---|
9 | struct ValMapper { |
---|
10 | Int operator()(Str *val) { |
---|
11 | return atoi(val->c_str()); |
---|
12 | } |
---|
13 | } valmapper; |
---|
14 | void Main() { |
---|
15 | Map<Str *, Int> *to_i = new Map<Str *, Int>(); |
---|
16 | Map<Int, Str *> *to_s = new Map<Int, Str *>(); |
---|
17 | const int n = 1000; |
---|
18 | for (int i = 0; i < n; i++) { |
---|
19 | char buf[sizeof(int) * 3 + 1]; |
---|
20 | sprintf(buf, "%d", i); |
---|
21 | Str *s = S(buf); |
---|
22 | to_i->add(s, i); |
---|
23 | to_s->add(i, s); |
---|
24 | } |
---|
25 | Check(to_i->count() == n && to_s->count() == n, "map sizes"); |
---|
26 | to_i->union_in_place(to_i); |
---|
27 | Check(to_i->count() == n, "map union"); |
---|
28 | to_i->intersect_in_place(to_i); |
---|
29 | Check(to_i->count() == n, "map intersection"); |
---|
30 | Map<Str *, Int> *aux = to_i->diff_with(to_i); |
---|
31 | Check(aux->count() == 0, "map difference"); |
---|
32 | Check(to_i->keys()->len() == to_i->values()->len(), "keys & values"); |
---|
33 | int mismatch = 0; |
---|
34 | for (int i = 0; i < n; i++) { |
---|
35 | if (!to_s->contains(i)) |
---|
36 | mismatch++; |
---|
37 | Str *s = to_s->get(i, NULL); |
---|
38 | if (!s || to_i->get(s, -1) == -1) |
---|
39 | mismatch++; |
---|
40 | } |
---|
41 | Check(mismatch == 0, "map indexing"); |
---|
42 | Map<Str *, Int> *r |
---|
43 | = to_s->map_pairs(new Map<Str *, Int>(), keymapper, valmapper); |
---|
44 | Check(r->count() == n, "map pair mapping"); |
---|
45 | Dict *dict = to_i->map_values(new Dict(), keymapper); |
---|
46 | Check(dict->count() == n, "map value mapping"); |
---|
47 | } |
---|