-
Notifications
You must be signed in to change notification settings - Fork 3
/
seti_test.go
226 lines (198 loc) · 4.56 KB
/
seti_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
package maps
import (
"bytes"
"encoding/gob"
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)
type makeSetF func(sources ...string) SetI[string]
func makeSetI[M any](sources ...string) SetI[string] {
var m any
m = new(M)
i := m.(SetI[string])
for _, s := range sources {
i.Add(s)
}
return i
}
func runSetITests[M any](t *testing.T, f makeSetF) {
testSetClear(t, f)
testSetLen(t, f)
testSetMerge(t, f)
testSetHas(t, f)
testSetRange(t, f)
testSetAdd(t, f)
testSetValues(t, f)
testSetEqual(t, f)
testSetBinaryMarshal[M](t, f)
testSetMarshalJSON(t, f)
testSetUnmarshalJSON[M](t, f)
testSetDelete(t, f)
}
func testSetClear(t *testing.T, f makeSetF) {
tests := []struct {
name string
m setTI
}{
{"empty", f()},
{"1 item", f("a")},
{"2 items", f("a", "b")},
}
for _, tt := range tests {
t.Run("Clear "+tt.name, func(t *testing.T) {
tt.m.Clear()
if tt.m.Len() != 0 {
t.Errorf("SetI not cleared")
}
})
}
}
func testSetLen(t *testing.T, f makeSetF) {
assert.Equal(t, 0, f().Len())
assert.Equal(t, 2, f("a", "b").Len())
}
func testSetMerge(t *testing.T, f makeSetF) {
tests := []struct {
name string
m1 setTI
m2 setTI
expected setTI
}{
{"1 to 1", f("a"), f("b"), f("a", "b")},
{"overwrite", f("a"), f("a", "b"), f("a", "b")},
{"to empty", f(), f("a", "b"), f("a", "b")},
{"from nil", f("a"), nil, f("a")},
{"from empty", f("a"), f(), f("a")},
}
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 testSetHas(t *testing.T, f makeSetF) {
m := f("a", "b")
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")
}
})
}
func testSetRange(t *testing.T, f makeSetF) {
tests := []struct {
name string
m setTI
expected int
}{
{"0", f(), 0},
{"1", f("a"), 1},
{"2", f("a", "b"), 2},
{"3", f("a", "b", "c"), 2},
}
for _, tt := range tests {
t.Run("Range "+tt.name, func(t *testing.T) {
count := 0
tt.m.Range(func(k string) bool {
count++
if count > 1 {
return false
}
return true
})
if count != tt.expected {
t.Errorf("Expected %d, got %d", tt.expected, count)
}
})
}
}
func testSetAdd(t *testing.T, f makeSetF) {
t.Run("Set", func(t *testing.T) {
a := f()
a.Add("a")
a.Add("b")
a.Add("b")
assert.True(t, a.Has("b"))
})
}
func testSetValues(t *testing.T, f makeSetF) {
t.Run("Values", func(t *testing.T) {
m := f("a", "b", "c")
values := m.Values()
assert.Len(t, values, 3)
assert.Contains(t, values, "c")
})
}
func testSetEqual(t *testing.T, f makeSetF) {
tests := []struct {
name string
m setTI
m2 setTI
want bool
}{
{"equal", f("a"), f("a"), true},
{"empty", f(), f(), true},
{"dif len", f("a"), f(), false},
{"dif len 1", f("a"), f("a", "b"), false},
{"dif value", f("a"), f("b"), 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 testSetBinaryMarshal[M any](t *testing.T, f makeSetF) {
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("a", "b", "c")
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.(SetI[string])
assert.True(t, m2.Has("a"))
assert.True(t, m2.Has("c"))
})
}
func testSetMarshalJSON(t *testing.T, f makeSetF) {
t.Run("MarshalJSON", func(t *testing.T) {
m := f("a", "b", "c")
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.Contains(t, string(s), `"a"`)
})
}
func testSetUnmarshalJSON[M any](t *testing.T, f makeSetF) {
b := []byte(`["a","b","c"]`)
var m M
json.Unmarshal(b, &m)
var i interface{}
i = &m
m2 := i.(SetI[string])
assert.True(t, m2.Has("c"))
}
func testSetDelete(t *testing.T, f makeSetF) {
t.Run("Delete", func(t *testing.T) {
m := f("a", "b")
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
})
}