-
Notifications
You must be signed in to change notification settings - Fork 13
/
map_test.go
161 lines (136 loc) · 3.32 KB
/
map_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package hamt
import (
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewMap(t *testing.T) {
NewMap[entryInt, string]()
}
func TestMapInsert(t *testing.T) {
m := NewMap[entryInt, string]()
for i := 0; i < iterations; i++ {
e := entryInt(rand.Int31())
m = m.Insert(e, "value")
assert.Equal(t, "value", *m.Find(e))
}
}
func TestMapOperations(t *testing.T) {
m := NewMap[entryInt, string]()
for i := 0; i < iterations; i++ {
k := entryInt(rand.Int31() % 256)
var mm Map[entryInt, string]
if rand.Int()%2 == 0 {
mm = m.Insert(k, "value")
assert.Equal(t, "value", *mm.Find(k))
if m.Include(k) {
assert.Equal(t, m.Size(), mm.Size())
} else {
assert.Equal(t, m.Size()+1, mm.Size())
}
} else {
mm = m.Delete(k)
assert.Nil(t, mm.Find(k))
if m.Include(k) {
assert.Equal(t, m.Size()-1, mm.Size())
} else {
assert.Equal(t, m.Size(), mm.Size())
}
}
m = mm
}
}
func TestMapFirstRest(t *testing.T) {
m := NewMap[entryInt, string]()
k, v, mm := m.FirstRest()
assert.Nil(t, k)
assert.Nil(t, v)
assert.Equal(t, 0, mm.Size())
m = m.Insert(entryInt(42), "value")
k, v, mm = m.FirstRest()
assert.Equal(t, entryInt(42), *k)
assert.Equal(t, "value", *v)
assert.Equal(t, 0, mm.Size())
m = m.Insert(entryInt(2049), "value")
s := m.Size()
for i := 0; i < s; i++ {
k, v, m = m.FirstRest()
assert.NotNil(t, k)
assert.Equal(t, "value", *v)
assert.Equal(t, 1-i, m.Size())
}
}
func TestMapForEach(t *testing.T) {
m := NewMap[entryInt, string]()
err := m.ForEach(func(key entryInt, val string) error {
assert.Fail(t, "for-each callback called on empty set")
return nil
})
assert.NoError(t, err)
m = m.Insert(entryInt(42), "value")
kvs := make([]keyValue[entryInt, string], 0)
want := []keyValue[entryInt, string]{
{
key: entryInt(42),
value: "value",
},
}
err = m.ForEach(func(key entryInt, val string) error {
kvs = append(kvs, keyValue[entryInt, string]{
key: key,
value: val,
})
return nil
})
assert.NoError(t, err)
assert.Equal(t, want, kvs)
m = m.Insert(entryInt(2049), "value2")
kvs = make([]keyValue[entryInt, string], 0)
want = []keyValue[entryInt, string]{
{
key: entryInt(42),
value: "value",
},
{
key: entryInt(2049),
value: "value2",
},
}
err = m.ForEach(func(key entryInt, val string) error {
kvs = append(kvs, keyValue[entryInt, string]{
key: key,
value: val,
})
return nil
})
assert.NoError(t, err)
assert.ElementsMatch(t, want, kvs)
}
func TestMapMerge(t *testing.T) {
for _, ms := range [][3]Map[entryInt, string]{
{
NewMap[entryInt, string](),
NewMap[entryInt, string](),
NewMap[entryInt, string](),
},
{
NewMap[entryInt, string]().Insert(entryInt(1), "foo"),
NewMap[entryInt, string](),
NewMap[entryInt, string]().Insert(entryInt(1), "foo"),
},
{
NewMap[entryInt, string](),
NewMap[entryInt, string]().Insert(entryInt(1), "foo"),
NewMap[entryInt, string]().Insert(entryInt(1), "foo"),
},
{
NewMap[entryInt, string]().Insert(entryInt(2), "foo"),
NewMap[entryInt, string]().Insert(entryInt(1), "foo"),
NewMap[entryInt, string]().Insert(entryInt(1), "foo").Insert(entryInt(2), "foo")},
} {
assert.Equal(t, ms[2], ms[0].Merge(ms[1]))
}
}
func TestMapSize(t *testing.T) {
assert.Equal(t, 0, NewMap[entryInt, string]().Size())
}