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
|
package test_core_container
import hm "core:container/handle_map"
import "core:container/xar"
import "core:testing"
Item :: struct {
v: int,
p: ^Item,
handle: hm.Handle32,
my_idx: u16,
}
@test
test_dynamic_handle_map :: proc(t: ^testing.T) {
dhm: hm.Dynamic_Handle_Map(Item, hm.Handle32)
hm.dynamic_init(&dhm, context.allocator)
defer hm.dynamic_destroy(&dhm)
items: [dynamic]Item
defer delete(items)
N :: 512
for i in 1..=N {
h, add_err := hm.dynamic_add(&dhm, Item{v = i * 10 + 1})
assert(add_err == nil)
item := hm.dynamic_get(&dhm, h)
item.handle = h
item.p = item
item.my_idx = h.idx
append(&items, item^)
}
testing.expect(t, hm.dynamic_len(dhm) == N)
testing.expect(t, hm.dynamic_cap(dhm) >= N)
for v in items {
item := hm.dynamic_get(&dhm, v.handle)
assert(item^ == v)
// Remove half of the items
if item.handle.idx & 1 == 0 {
found, found_err := hm.dynamic_remove(&dhm, v.handle)
assert(found && found_err == nil)
// These removed handles should no longer be valid
assert(!hm.dynamic_is_valid(&dhm, v.handle))
} else {
// Non-removed handles should still be valid
assert(hm.dynamic_is_valid(&dhm, v.handle))
}
}
testing.expect(t, hm.dynamic_len(dhm) == N / 2)
testing.expect(t, hm.dynamic_cap(dhm) >= N / 2)
testing.expect(t, xar.len(dhm.unused_items) == N / 2)
it := hm.dynamic_iterator_make(&dhm)
for v, handle in hm.iterate(&it) {
assert(v.handle.idx & 1 == 1)
assert(hm.dynamic_is_valid(&dhm, handle))
item := hm.dynamic_get(&dhm, handle)
assert(item.my_idx == v.handle.idx)
}
for i in 1..=N / 2 {
h, add_err := hm.dynamic_add(&dhm, Item{v = i * 10 + 1})
assert(add_err == nil)
assert(h.gen == 2)
}
hm.dynamic_clear(&dhm)
testing.expect(t, hm.dynamic_len(dhm) == 0)
testing.expect(t, hm.dynamic_cap(dhm) >= N)
}
test_static_handle_map :: proc(t: ^testing.T) {
N :: 512
shm: hm.Static_Handle_Map(N, Item, hm.Handle32)
items: [dynamic]Item
defer delete(items)
for i in 1..=N {
h, add_ok := hm.static_add(&shm, Item{v = i * 10 + 1})
assert(add_ok)
item := hm.static_get(&shm, h)
item.handle = h
item.p = item
item.my_idx = h.idx
append(&items, item^)
}
testing.expect(t, hm.static_len(shm) == N)
testing.expect(t, hm.static_cap(shm) >= N)
for v in items {
item := hm.static_get(&shm, v.handle)
assert(item^ == v)
// Remove half of the items
if item.handle.idx & 1 == 0 {
assert(hm.static_remove(&shm, v.handle))
// These removed handles should no longer be valid
assert(!hm.static_is_valid(shm, v.handle))
} else {
// Non-removed handles should still be valid
assert(hm.static_is_valid(shm, v.handle))
}
}
testing.expect(t, hm.static_len(shm) == N / 2)
testing.expect(t, hm.static_cap(shm) >= N / 2)
it := hm.static_iterator_make(&shm)
for v, handle in hm.iterate(&it) {
assert(v.handle.idx & 1 == 1)
assert(hm.static_is_valid(shm, handle))
item := hm.static_get(&shm, handle)
assert(item.my_idx == v.handle.idx)
}
for i in 1..=N / 2 {
h, add_ok := hm.static_add(&shm, Item{v = i * 10 + 1})
assert(add_ok)
assert(h.gen == 2)
}
hm.static_clear(&shm)
testing.expect(t, hm.static_len(shm) == 0)
testing.expect(t, hm.static_cap(shm) == N)
}
|