-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzz_compat_test.go
513 lines (448 loc) · 12.3 KB
/
zz_compat_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Code generated by update_compat_test_helpers.sh. DO NOT EDIT.
//go:generate hack/update_compat_test_helpers.sh
//
// Tests for backwards compatibility of the API surface with genericset.
// vim: syntax=go
package set
import (
"fmt"
"math/rand"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func init() {
rand.Seed(time.Now().Unix())
}
// Assert existence of item getter function for use in tests. If your tests
// fail here, you need to define the function NewItem and have
// it return a new instance of the item type interface{} in your set, so the
// tests have content to manipulate in the Set.
var _ = NewItem
func getSetItems(n int) []interface{} {
items := make([]interface{}, n)
for i := 0; i < n; i++ {
items[i] = NewItem()
}
return items
}
func AssertSetElementsMatch(t assert.TestingT, expected []interface{}, set Set) (ok bool) {
if h, ok := t.(interface {
Helper()
}); ok {
h.Helper()
}
return assert.ElementsMatch(t, expected, set.Slice())
}
func RequireSetElementsMatch(t require.TestingT, expected []interface{}, set Set) {
if h, ok := t.(interface {
Helper()
}); ok {
h.Helper()
}
require.ElementsMatch(t, expected, set.Slice())
}
func TestSetNew(t *testing.T) {
t.Run("New creates an empty set", func(t *testing.T) {
s := New()
assert.Equal(t, 0, s.Count())
})
t.Run("Items passed to New are added to the Set", func(t *testing.T) {
items := getSetItems(3)
s := New(items...)
require.Equal(t, 3, s.Count())
assert.True(t, s.Has(items[0]))
assert.True(t, s.Has(items[1]))
assert.True(t, s.Has(items[2]))
})
}
func TestSetAdd(t *testing.T) {
t.Run("Add adds items to an existing Set", func(t *testing.T) {
s := New()
items := getSetItems(3)
s.Add(items[0])
s.Add(items[1:]...)
require.Equal(t, 3, s.Count())
assert.True(t, s.Has(items[0]))
assert.True(t, s.Has(items[1]))
assert.True(t, s.Has(items[2]))
AssertSetElementsMatch(t, items, s)
})
t.Run("Add initialises an uninitialised Set", func(t *testing.T) {
var s Set
items := getSetItems(3)
s.Add(items[0])
s.Add(items[1])
s.Add(items[2])
require.Equal(t, 3, s.Count())
assert.True(t, s.Has(items[0]))
assert.True(t, s.Has(items[1]))
assert.True(t, s.Has(items[2]))
AssertSetElementsMatch(t, items, s)
})
}
func TestSetCount(t *testing.T) {
t.Run("Count returns 0 on an empty uninitialised Set", func(t *testing.T) {
var s Set
require.Equal(t, 0, s.Count())
})
t.Run("Count returns the number of items in the Set", func(t *testing.T) {
var s Set
n := rand.Intn(2048) + 100
for i := 0; i < n; i++ {
assert.Equal(t, i, s.Count())
s.Add(getSetItems(1)[0])
assert.Equal(t, i+1, s.Count())
}
require.Equal(t, n, s.Count())
})
}
func TestSetHas(t *testing.T) {
t.Run("Has returns false when called on an uninitialised Set", func(t *testing.T) {
var s Set
require.False(t, s.Has(getSetItems(1)[0]))
})
t.Run("Has returns true for an item previously added to the Set and false otherwise", func(t *testing.T) {
var s Set
type setTestItem struct {
dt interface{}
include bool
}
// Generate some random items (integers) to add to the set and
// others not to add but use in checking
n := rand.Intn(2048) + 100
items := make([]setTestItem, n)
for i := 0; i < n; i++ {
items[i] = setTestItem{
dt: getSetItems(1)[0],
include: rand.Int31()&0x01 == 0x01,
}
}
// add the items to the set
for _, item := range items {
if item.include {
s.Add(item.dt)
}
}
for _, item := range items {
assert.Equal(t, item.include, s.Has(item.dt))
}
})
}
func TestSetRemove(t *testing.T) {
t.Run("Remove returns cleanly on an uninitialised Set", func(t *testing.T) {
var s Set
s.Remove(getSetItems(1)[0])
require.Equal(t, 0, s.Count())
AssertSetElementsMatch(t, nil, s)
})
t.Run("Remove removes an item previously added to the Set s.t. Has(item) returns false", func(t *testing.T) {
var s Set
items := getSetItems(5)
s.Add(items...)
require.Equal(t, 5, s.Count())
s.Remove(items[2])
require.Equal(t, 4, s.Count())
require.False(t, s.Has(items[2]))
expectItems := append(items[:2], items[3:]...)
for _, expectItem := range expectItems {
require.True(t, s.Has(expectItem))
}
RequireSetElementsMatch(t, expectItems, s)
})
t.Run("Remove is a no-op for an element not in the set", func(t *testing.T) {
var s Set
items := getSetItems(3)
s.Add(items...)
require.Equal(t, 3, s.Count())
s.Remove(getSetItems(1)[0])
require.Equal(t, 3, s.Count())
RequireSetElementsMatch(t, items, s)
})
}
func TestSetSlice(t *testing.T) {
t.Run("Slice on an uninitialised Set returns slice of length 0", func(t *testing.T) {
var s Set
require.Equal(t, 0, len(s.Slice()))
})
t.Run("Slice returns a list of items in the Set", func(t *testing.T) {
var s Set
items := getSetItems(5)
s.Add(items...)
sl := s.Slice()
assert.ElementsMatch(t, items, sl)
})
}
func TestSetString(t *testing.T) {
tcs := [][]interface{}{
{},
getSetItems(5),
getSetItems(1),
}
const offset = len(`Set`)
for _, tc := range tcs {
s := strings.TrimSpace(New(tc...).String())
assert.Equal(t, "Set{", s[:offset+1])
assert.Equal(t, '}', rune(s[len(s)-1]))
contents := s[offset+1 : len(s)-1]
var parts []string
if contents != "" {
parts = strings.Split(contents, ", ")
}
strElts := make([]string, 0, len(tc))
for _, elt := range tc {
strElts = append(strElts, fmt.Sprintf("%v", elt))
}
assert.ElementsMatch(t, strElts, parts)
}
}
type testSetCase struct {
leftElements, rightElements, expect []interface{}
}
func appendTestSetCase(testCases []testSetCase, left, right, expect []interface{}) []testSetCase {
return append(testCases, testSetCase{
leftElements: append(left[:0:0], left...),
rightElements: append(right[:0:0], right...),
expect: append(expect[:0:0], expect...),
})
}
func intersectTestCasesSet() []testSetCase {
var testCases []testSetCase
app := appendTestSetCase
n := getSetItems
testCases = append(testCases, []testSetCase{{
leftElements: nil,
rightElements: nil,
expect: nil,
}, {
leftElements: n(5),
rightElements: nil,
expect: nil,
}, {
leftElements: nil,
rightElements: n(5),
expect: nil,
}, {
leftElements: []interface{}{},
rightElements: n(1),
expect: []interface{}{},
}, {
leftElements: n(1),
rightElements: []interface{}{},
expect: []interface{}{},
}, {
leftElements: []interface{}{},
rightElements: []interface{}{},
expect: []interface{}{},
}}...)
// We require persistent values in the following, so they require more computation
// to avoid any randomness in the new item factory
{
onlyOne := n(1)
testCases = app(testCases, onlyOne, onlyOne, onlyOne)
}
{
moreLeft := n(3)
thanRight := moreLeft[:2]
testCases = app(testCases, moreLeft, thanRight, thanRight)
}
{
// More than one, same left-right
sameMultipleLeftRight := n(2)
testCases = app(testCases, sameMultipleLeftRight, sameMultipleLeftRight, sameMultipleLeftRight)
}
{
twoLeft := n(2)
oneRight := twoLeft[1:]
testCases = app(testCases, twoLeft, oneRight, oneRight)
}
return testCases
}
func unionTestCasesSet() []testSetCase {
var testCases []testSetCase
app := appendTestSetCase
n := getSetItems
testCases = append(testCases, []testSetCase{{
leftElements: nil,
rightElements: nil,
expect: nil,
}, {
leftElements: []interface{}{},
rightElements: []interface{}{},
expect: []interface{}{},
}}...)
// We require access to the same generated values in both arms of the
// test in the following, so they require computation to workaround
// randomness in the new item generator
{
onlyLeft := n(5)
testCases = app(testCases, onlyLeft, nil, onlyLeft)
}
{
onlyRight := n(5)
testCases = app(testCases, nil, onlyRight, onlyRight)
}
{
onlyOneLeft := n(1)
testCases = app(testCases, onlyOneLeft, nil, onlyOneLeft)
}
{
onlyOneRight := n(1)
testCases = app(testCases, nil, onlyOneRight, onlyOneRight)
}
{
multipleLeft := n(3)
emptyRight := n(0)
testCases = app(testCases, multipleLeft, emptyRight, multipleLeft)
}
{
sameLeftRight := n(5)
testCases = app(testCases, sameLeftRight, sameLeftRight, sameLeftRight)
}
{
emptyLeft := n(0)
multipleRight := n(3)
testCases = app(testCases, emptyLeft, multipleRight, multipleRight)
}
{
onlyOneLeftRight := n(1)
testCases = app(testCases, onlyOneLeftRight, onlyOneLeftRight, onlyOneLeftRight)
}
{
differentLeft := n(5)
right := n(5)
testCases = app(testCases, differentLeft, right, append(differentLeft, right...))
}
{
someLeft := n(5)
overlapRight := append(someLeft[3:], n(2)...)
testCases = app(testCases, someLeft, overlapRight, append(someLeft[0:3], overlapRight...))
}
return testCases
}
func subtractTestCasesSet() []testSetCase {
var testCases []testSetCase
app := appendTestSetCase
n := getSetItems
testCases = append(testCases, []testSetCase{{
leftElements: nil,
rightElements: nil,
expect: nil,
}, {
leftElements: []interface{}{},
rightElements: []interface{}{},
expect: []interface{}{},
}, {
leftElements: nil,
rightElements: n(5),
expect: nil,
}, {
leftElements: []interface{}{},
rightElements: n(5),
expect: []interface{}{},
}}...)
// We require access to the same generated values in both arms of the
// test in the following, so they require computation to workaround
// randomness in the new item generator
{
onlyLeft := n(5)
testCases = app(testCases, onlyLeft, nil, onlyLeft)
}
{
onlyOneLeft := n(1)
testCases = app(testCases, onlyOneLeft, nil, onlyOneLeft)
}
{
multipleLeft := n(3)
emptyRight := n(0)
testCases = app(testCases, multipleLeft, emptyRight, multipleLeft)
}
{
sameLeftRight := n(5)
testCases = app(testCases, sameLeftRight, sameLeftRight, []interface{}{})
}
{
onlyOneLeftRight := n(1)
testCases = app(testCases, onlyOneLeftRight, onlyOneLeftRight, []interface{}{})
}
{
differentLeft := n(5)
right := n(4)
testCases = app(testCases, differentLeft, right, differentLeft)
}
{
someLeft := n(5)
overlapRight := make([]interface{}, 0, 6)
overlapRight = append(overlapRight, someLeft[4], someLeft[3], someLeft[2])
overlapRight = append(overlapRight, n(3)...)
testCases = app(testCases, someLeft, overlapRight, someLeft[:2])
}
return testCases
}
// makeSet initialises and returns a Set with the items provided in xs. If xs
// is nil, the Set is returned uninitialised.
func makeSetForTest(t *testing.T, xs []interface{}) Set {
var s Set
if xs != nil {
s.Add(xs...)
}
require.Equal(t, len(xs), s.Count())
return s
}
func testSetOperation(t *testing.T, cases []testSetCase,
op func(s1 *Set, s2 Set),
) {
t.Helper()
for i, tc := range cases {
t.Logf("testing case %d: %s", i, tc)
s1 := makeSetForTest(t, tc.leftElements)
s2 := makeSetForTest(t, tc.rightElements)
op(&s1, s2)
// s2 is unmodified
RequireSetElementsMatch(t, tc.rightElements, s2)
// s1 has been modified appropriately
RequireSetElementsMatch(t, tc.expect, s1)
}
}
func TestSetIntersection(t *testing.T) {
testSetOperation(t, intersectTestCasesSet(), func(s1 *Set, s2 Set) {
s1.Intersect(s2)
})
}
func TestSetUnion(t *testing.T) {
testSetOperation(t, unionTestCasesSet(), func(s1 *Set, s2 Set) {
s1.Union(s2)
})
}
func TestSetSubtract(t *testing.T) {
testSetOperation(t, subtractTestCasesSet(), func(s1 *Set, s2 Set) {
s1.Subtract(s2)
})
}
func testNonMutatingSetOperation(cases []testSetCase, op func(s1, s2 Set) Set) func(*testing.T) {
return func(t *testing.T) {
for _, tc := range cases {
s1 := makeSetForTest(t, tc.leftElements)
s2 := makeSetForTest(t, tc.rightElements)
out := op(s1, s2)
// The input sets are not modified
require.Equal(t, len(tc.leftElements), s1.Count())
require.ElementsMatch(t, tc.leftElements, s1.Slice())
require.Equal(t, len(tc.rightElements), s2.Count())
require.ElementsMatch(t, tc.rightElements, s2.Slice())
require.Equal(t, len(tc.expect), out.Count())
require.ElementsMatch(t, tc.expect, out.Slice())
}
}
}
func TestIntersectionSet(t *testing.T) {
testNonMutatingSetOperation(intersectTestCasesSet(), Intersect)(t)
}
func TestUnionSet(t *testing.T) {
testNonMutatingSetOperation(unionTestCasesSet(), Union)(t)
}
func TestSubtractSet(t *testing.T) {
testNonMutatingSetOperation(subtractTestCasesSet(), Subtract)(t)
}