-
Notifications
You must be signed in to change notification settings - Fork 3
/
mapi_test.go
256 lines (225 loc) · 5.51 KB
/
mapi_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package maps
import (
"bytes"
"encoding/gob"
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)
type makeF func(sources ...mapT) MapI[string, int]
func makeMapi[M any](sources ...mapT) MapI[string, int] {
var m any
m = new(M)
i := m.(MapI[string, int])
for _, s := range sources {
i.Merge(s)
}
return i
}
func runMapiTests[M any](t *testing.T, f makeF) {
testClear(t, f)
testLen(t, f)
testMerge(t, f)
testGetHasLoad(t, f)
testRange(t, f)
testSet(t, f)
testKeys(t, f)
testValues(t, f)
testEqual(t, f)
testBinaryMarshal[M](t, f)
testMarshalJSON(t, f)
testUnmarshalJSON[M](t, f)
testDelete(t, f)
}
func testClear(t *testing.T, f makeF) {
tests := []struct {
name string
m mapTI
}{
{"empty", f()},
{"1 item", f(mapT{"a": 1})},
{"2 items", f(mapT{"a": 1, "b": 2})},
}
for _, tt := range tests {
t.Run("Clear "+tt.name, func(t *testing.T) {
tt.m.Clear()
if tt.m.Len() != 0 {
t.Errorf("MapI not cleared")
}
})
}
}
func testLen(t *testing.T, f makeF) {
assert.Equal(t, 0, f().Len())
assert.Equal(t, 2, f(mapT{"a": 1, "b": 2}).Len())
}
func testMerge(t *testing.T, f makeF) {
tests := []struct {
name string
m1 mapTI
m2 mapT
expected mapT
}{
{"1 to 1", f(mapT{"a": 1}), mapT{"b": 2}, mapT{"a": 1, "b": 2}},
{"overwrite", f(mapT{"a": 1}), mapT{"a": 1, "b": 2}, mapT{"a": 1, "b": 2}},
{"to empty", f(), mapT{"a": 1, "b": 2}, mapT{"a": 1, "b": 2}},
{"from nil", f(mapT{"a": 1}), nil, mapT{"a": 1}},
{"from empty", f(mapT{"a": 1}), mapT{}, mapT{"a": 1}},
{"from cast map", f(mapT{"a": 1}), Cast(map[string]int{"b": 2}), mapT{"a": 1, "b": 2}},
}
for _, tt := range tests {
t.Run("Merge "+tt.name, func(t *testing.T) {
tt.m1.Merge(tt.m2)
if !tt.m1.Equal(tt.expected) {
t.Errorf("Merge error. Expected: %q, got %q", tt.expected, tt.m1)
}
})
}
}
func testGetHasLoad(t *testing.T, f makeF) {
m := f(mapT{"a": 1, "b": 2})
t.Run("Has", func(t *testing.T) {
if m.Has("c") {
t.Errorf("Expected false, got true")
}
if !m.Has("b") {
t.Errorf("Expected true, got false")
}
})
t.Run("Get", func(t *testing.T) {
if v := m.Get("b"); v != 2 {
t.Errorf("Expected 2, got %q", v)
}
if v := m.Get("c"); v != 0 {
t.Errorf("Expected 0, got %q", v)
}
})
t.Run("Load", func(t *testing.T) {
v, ok := m.Load("a")
if v != 1 {
t.Errorf("Expected 1, got %q", v)
}
if !ok {
t.Errorf("Expected true, got false")
}
})
}
func testRange(t *testing.T, f makeF) {
tests := []struct {
name string
m mapTI
expected int
}{
{"0", f(), 0},
{"1", f(mapT{"a": 1}), 1},
{"2", f(mapT{"a": 1, "b": 2}), 2},
{"3", f(mapT{"a": 1, "b": 2, "c": 3}), 2},
}
for _, tt := range tests {
t.Run("Range "+tt.name, func(t *testing.T) {
count := 0
tt.m.Range(func(k string, v int) bool {
count++
if count > 1 {
return false
}
return true
})
if count != tt.expected {
t.Errorf("Expected %d, got %d", tt.expected, count)
}
})
}
}
func testSet(t *testing.T, f makeF) {
t.Run("Set", func(t *testing.T) {
a := f()
a.Set("a", 1)
a.Set("b", 2)
assert.Equal(t, 2, a.Get("b"))
})
}
func testKeys(t *testing.T, f makeF) {
t.Run("Keys", func(t *testing.T) {
m := f(mapT{"a": 1, "b": 2, "c": 3})
keys := m.Keys()
assert.Len(t, keys, 3)
assert.Contains(t, keys, "c")
})
}
func testValues(t *testing.T, f makeF) {
t.Run("Values", func(t *testing.T) {
m := f(mapT{"a": 1, "b": 2, "c": 3})
values := m.Values()
assert.Len(t, values, 3)
assert.Contains(t, values, 3)
})
}
func testEqual(t *testing.T, f makeF) {
tests := []struct {
name string
m mapTI
m2 mapT
want bool
}{
{"equal", f(mapT{"a": 1}), mapT{"a": 1}, true},
{"empty", f(), mapT{}, true},
{"dif len", f(mapT{"a": 1}), mapT{}, false},
{"dif len 1", f(mapT{"a": 1}), mapT{"a": 1, "b": 2}, false},
{"dif value", f(mapT{"a": 1}), mapT{"a": 2}, false},
{"dif key", f(mapT{"a": 1}), mapT{"b": 1}, false},
}
for _, tt := range tests {
t.Run("Equal "+tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.m.Equal(tt.m2), "Equal(%v)", tt.m2)
})
}
}
func testBinaryMarshal[M any](t *testing.T, f makeF) {
t.Run("BinaryMarshal", func(t *testing.T) {
// You would rarely call MarshallBinary directly, but rather would use an encoder, like GOB for binary encoding
m := f(mapT{"a": 1, "b": 2, "c": 3})
var buf bytes.Buffer
enc := gob.NewEncoder(&buf) // Will write
dec := gob.NewDecoder(&buf) // Will read
var i any
i = m
err := enc.Encode(&i)
assert.NoError(t, err)
var i2 any
err = dec.Decode(&i2)
assert.NoError(t, err)
m2 := i2.(MapI[string, int])
assert.Equal(t, 1, m2.Get("a"))
assert.Equal(t, 3, m2.Get("c"))
})
}
func testMarshalJSON(t *testing.T, f makeF) {
t.Run("MarshalJSON", func(t *testing.T) {
m := f(mapT{"a": 1, "b": 2, "c": 3})
s, err := json.Marshal(m)
assert.NoError(t, err)
// Note: The below output is what is produced, but isn't guaranteed. go seems to currently be sorting keys
assert.Equal(t, `{"a":1,"b":2,"c":3}`, string(s))
})
}
func testUnmarshalJSON[M any](t *testing.T, f makeF) {
b := []byte(`{"a":1,"b":2,"c":3}`)
var m M
json.Unmarshal(b, &m)
var i interface{}
i = &m
m2 := i.(MapI[string, int])
assert.Equal(t, 3, m2.Get("c"))
}
func testDelete(t *testing.T, f makeF) {
t.Run("Delete", func(t *testing.T) {
m := f(mapT{"a": 1, "b": 2})
m.Delete("a")
assert.False(t, m.Has("a"))
assert.True(t, m.Has("b"))
m.Delete("b")
assert.False(t, m.Has("b"))
m.Delete("b") // make sure deleting from an empty map is a no-op
})
}