forked from wasilibs/nottinygc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
intmap_test.go
75 lines (65 loc) · 1.33 KB
/
intmap_test.go
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
// Copyright wasilibs authors
// SPDX-License-Identifier: MIT
package nottinygc
import "testing"
func TestIntMapBasic(t *testing.T) {
m := newIntMap()
_, ok := m.get(5)
if ok {
t.Fatal("expected not ok in empty map")
}
m.put(5, 10)
v, ok := m.get(5)
if !ok {
t.Fatal("expected ok in map")
}
if v != 10 {
t.Fatal("expected 10 in map")
}
}
func TestIntMapNoResize(t *testing.T) {
top := int(0.75 * 512)
m := newIntMap()
for i := 0; i < top; i++ {
m.put(uintptr(i), uintptr(i))
}
if len(m.buckets) != 512 {
t.Fatal("expected 512 buckets")
}
for i := 0; i < top; i++ {
v, ok := m.get(uintptr(i))
if !ok {
t.Fatalf("expected %d to be in map", i)
}
if v != uintptr(i) {
t.Fatalf("expected %d to have value %d in map, got %d", i, i, v)
}
}
_, ok := m.get(uintptr(top))
if ok {
t.Fatal("expected not ok in map")
}
}
func TestIntMapResize(t *testing.T) {
top := 512
m := newIntMap()
for i := 0; i < top; i++ {
m.put(uintptr(i), uintptr(i))
}
if len(m.buckets) != 1024 {
t.Fatal("expected 1024 buckets")
}
for i := 0; i < top; i++ {
v, ok := m.get(uintptr(i))
if !ok {
t.Fatalf("expected %d to be in map", i)
}
if v != uintptr(i) {
t.Fatalf("expected %d to have value %d in map, got %d", i, i, v)
}
}
_, ok := m.get(uintptr(top))
if ok {
t.Fatal("expected not ok in map")
}
}