Repository navigation
Expand file tree
/
Copy pathcoverage_test.go
More file actions
443 lines (389 loc) · 12.8 KB
/
Copy pathcoverage_test.go
File metadata and controls
443 lines (389 loc) · 12.8 KB
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
package sets
import (
"cmp"
"encoding/json"
"math"
"slices"
"testing"
)
// plainSet delegates to an embedded Set but does not implement json.Marshaler,
// json.Unmarshaler, or Locker, exercising the wrapper types' error branches.
type plainSet[M comparable] struct {
Set[M]
}
// plainOrdered is the OrderedSet analog of plainSet.
type plainOrdered[M cmp.Ordered] struct {
OrderedSet[M]
}
// lyingSet reports a non-zero cardinality while iterating nothing, exercising
// Random's fallback return for misbehaving Set implementations.
type lyingSet struct {
Set[int]
}
func (lyingSet) Cardinality() int { return 1 }
func TestFilterTo(t *testing.T) {
t.Parallel()
src := NewWith(1, 2, 3, 4)
dst := New[int]()
FilterTo(src, dst, func(i int) bool { return i%2 == 0 })
if !Equal(dst, NewWith(2, 4)) {
t.Fatalf("FilterTo yielded %v, want [2 4]", Elements(dst))
}
}
func TestString(t *testing.T) {
t.Parallel()
if got, want := NewLockedWith(1).String(), "LockedSet[int]([1])"; got != want {
t.Fatalf("Locked.String() = %q, want %q", got, want)
}
if got, want := NewLockedOrderedWith(1).String(), "LockedOrderedSet[int]([1])"; got != want {
t.Fatalf("LockedOrdered.String() = %q, want %q", got, want)
}
if got, want := NewSyncMapWith(1).String(), "SyncSet[int]([1])"; got != want {
t.Fatalf("SyncMap.String() = %q, want %q", got, want)
}
}
func TestLockedOrdered_OrderedAccessors(t *testing.T) {
t.Parallel()
s := NewLockedOrderedWith(3, 1, 2)
var idx, vals []int
s.Ordered(func(i, v int) bool {
idx = append(idx, i)
vals = append(vals, v)
return true
})
if !slices.Equal(idx, []int{0, 1, 2}) || !slices.Equal(vals, []int{3, 1, 2}) {
t.Fatalf("Ordered yielded idx=%v vals=%v", idx, vals)
}
vals = nil
s.Ordered(func(_, v int) bool {
vals = append(vals, v)
return false
})
if !slices.Equal(vals, []int{3}) {
t.Fatalf("Ordered early stop yielded %v", vals)
}
idx, vals = nil, nil
s.Backwards(func(i, v int) bool {
idx = append(idx, i)
vals = append(vals, v)
return true
})
if !slices.Equal(idx, []int{2, 1, 0}) || !slices.Equal(vals, []int{2, 1, 3}) {
t.Fatalf("Backwards yielded idx=%v vals=%v", idx, vals)
}
vals = nil
s.Backwards(func(_, v int) bool {
vals = append(vals, v)
return false
})
if !slices.Equal(vals, []int{2}) {
t.Fatalf("Backwards early stop yielded %v", vals)
}
if got := s.Index(1); got != 1 {
t.Fatalf("Index(1) = %d, want 1", got)
}
if got := s.Index(99); got != -1 {
t.Fatalf("Index(99) = %d, want -1", got)
}
s.Sort()
if got := slices.Collect(s.Iterator); !slices.Equal(got, []int{1, 2, 3}) {
t.Fatalf("after Sort: Iterator yielded %v", got)
}
}
func TestWrappingAlreadyLocked(t *testing.T) {
t.Parallel()
l := NewLocked[int]()
if got := NewLockedWrapping(l); got != Set[int](l) {
t.Fatalf("NewLockedWrapping returned %T, want the original locked set", got)
}
lo := NewLockedOrdered[int]()
if got := NewLockedOrderedWrapping(lo); got != OrderedSet[int](lo) {
t.Fatalf("NewLockedOrderedWrapping returned %T, want the original locked set", got)
}
}
func TestNilReceiverCardinality(t *testing.T) {
t.Parallel()
var m *Map[int]
var o *Ordered[int]
var sm *SyncMap[int]
var l *Locked[int]
var lo *LockedOrdered[int]
var ss *SortedSet[int]
for i, c := range []int{
m.Cardinality(), o.Cardinality(), sm.Cardinality(),
l.Cardinality(), lo.Cardinality(), ss.Cardinality(),
} {
if c != 0 {
t.Fatalf("nil receiver Cardinality() #%d = %d, want 0", i, c)
}
}
}
// TestNilReceiverMaxMin pins the nil-safety of the Maxer/Minner fast paths: a typed-nil receiver
// must report ok=false (sending the package-level Max/Min down the generic path to the intended
// "empty set" panic) rather than dereference its backing storage.
func TestNilReceiverMaxMin(t *testing.T) {
t.Parallel()
var ss *SortedSet[int]
var bs *BitSet[int]
if _, ok := ss.Max(); ok {
t.Fatal("nil SortedSet Max() reported ok=true")
}
if _, ok := ss.Min(); ok {
t.Fatal("nil SortedSet Min() reported ok=true")
}
if _, ok := bs.Max(); ok {
t.Fatal("nil BitSet Max() reported ok=true")
}
if _, ok := bs.Min(); ok {
t.Fatal("nil BitSet Min() reported ok=true")
}
}
func TestZeroValueWrappers(t *testing.T) {
t.Parallel()
var m Map[int]
if c := m.Clone(); c.Cardinality() != 0 || !c.Add(1) {
t.Fatal("zero value Map.Clone() is not an empty usable set")
}
var l Locked[int]
if c := l.Clone(); c.Cardinality() != 0 {
t.Fatalf("zero value Locked.Clone() has %d elements", c.Cardinality())
}
if e := l.NewEmpty(); e.Cardinality() != 0 {
t.Fatalf("zero value Locked.NewEmpty() has %d elements", e.Cardinality())
}
var l2 Locked[int]
if err := l2.UnmarshalJSON([]byte(`[1,2]`)); err != nil {
t.Fatalf("zero value Locked.UnmarshalJSON error: %v", err)
}
if l2.Cardinality() != 2 {
t.Fatalf("zero value Locked after UnmarshalJSON has %d elements", l2.Cardinality())
}
var l3 Locked[int]
if err := l3.Scan([]byte(`[1]`)); err != nil {
t.Fatalf("zero value Locked.Scan error: %v", err)
}
var lo LockedOrdered[int]
if c := lo.Clone(); c.Cardinality() != 0 {
t.Fatalf("zero value LockedOrdered.Clone() has %d elements", c.Cardinality())
}
if e := lo.NewEmptyOrdered(); e.Cardinality() != 0 {
t.Fatalf("zero value LockedOrdered.NewEmptyOrdered() has %d elements", e.Cardinality())
}
var lo2 LockedOrdered[int]
if err := lo2.UnmarshalJSON([]byte(`[1,2]`)); err != nil {
t.Fatalf("zero value LockedOrdered.UnmarshalJSON error: %v", err)
}
var lo3 LockedOrdered[int]
if err := lo3.Scan([]byte(`[1]`)); err != nil {
t.Fatalf("zero value LockedOrdered.Scan error: %v", err)
}
}
func TestEqualSubsetSameCardinality(t *testing.T) {
t.Parallel()
if Equal(NewWith(1, 2), NewWith(1, 3)) {
t.Fatal("Equal on same-cardinality different sets returned true")
}
if Subset(NewWith(1, 5), NewWith(1, 2, 3)) {
t.Fatal("Subset returned true for a non-subset of larger cardinality")
}
}
func TestIter2EarlyStop(t *testing.T) {
t.Parallel()
var seen []int
for i := range Iter2(NewOrderedWith(1, 2, 3).Iterator) {
seen = append(seen, i)
break
}
if !slices.Equal(seen, []int{0}) {
t.Fatalf("Iter2 early stop yielded %v", seen)
}
}
func TestMinMaxPanicOnEmpty(t *testing.T) {
t.Parallel()
for name, fn := range map[string]func(){
"Max": func() { Max(New[int]()) },
"Min": func() { Min(New[int]()) },
} {
func() {
defer func() {
if recover() == nil {
t.Fatalf("%s on empty set did not panic", name)
}
}()
fn()
}()
}
}
func TestChunkPanicAndEarlyStop(t *testing.T) {
t.Parallel()
func() {
defer func() {
if recover() == nil {
t.Fatal("Chunk with n=0 did not panic")
}
}()
Chunk(New[int](), 0)
}()
var chunks int
for range Chunk(NewOrderedWith(1, 2, 3, 4, 5), 2) {
chunks++
break
}
if chunks != 1 {
t.Fatalf("Chunk early stop yielded %d chunks", chunks)
}
}
func TestOrderedBackwardsEarlyStop(t *testing.T) {
t.Parallel()
var vals []int
NewOrderedWith(1, 2, 3).Backwards(func(_, v int) bool {
vals = append(vals, v)
return false
})
if !slices.Equal(vals, []int{3}) {
t.Fatalf("Backwards early stop yielded %v", vals)
}
}
func TestMarshalJSONErrors(t *testing.T) {
t.Parallel()
if _, err := NewOrderedWith(math.NaN()).MarshalJSON(); err == nil {
t.Fatal("Ordered.MarshalJSON(NaN) did not error")
}
if _, err := NewLockedWith(math.NaN()).MarshalJSON(); err == nil {
t.Fatal("Locked.MarshalJSON(NaN) did not error")
}
if _, err := NewLockedOrderedWith(math.NaN()).MarshalJSON(); err == nil {
t.Fatal("LockedOrdered.MarshalJSON(NaN) did not error")
}
}
func TestLockedWrappingNonJSONSet(t *testing.T) {
t.Parallel()
w := NewLockedWrapping(plainSet[int]{New[int]()})
if _, err := w.(json.Marshaler).MarshalJSON(); err == nil {
t.Fatal("MarshalJSON of a wrapped non-marshaler set did not error")
}
if err := w.(json.Unmarshaler).UnmarshalJSON([]byte(`[1]`)); err == nil {
t.Fatal("UnmarshalJSON of a wrapped non-unmarshaler set did not error")
}
if err := w.(interface{ Scan(any) error }).Scan([]byte(`[1]`)); err == nil {
t.Fatal("Scan of a wrapped non-unmarshaler set did not error")
}
wo := NewLockedOrderedWrapping(plainOrdered[int]{NewOrdered[int]()})
if _, err := wo.(json.Marshaler).MarshalJSON(); err == nil {
t.Fatal("MarshalJSON of a wrapped non-marshaler ordered set did not error")
}
if err := wo.(json.Unmarshaler).UnmarshalJSON([]byte(`[1]`)); err == nil {
t.Fatal("UnmarshalJSON of a wrapped non-unmarshaler ordered set did not error")
}
if err := wo.(interface{ Scan(any) error }).Scan([]byte(`[1]`)); err == nil {
t.Fatal("Scan of a wrapped non-unmarshaler ordered set did not error")
}
}
func TestRandomMisbehavingSet(t *testing.T) {
t.Parallel()
if _, ok := Random(lyingSet{New[int]()}); ok {
t.Fatal("Random on a set that iterates nothing returned ok=true")
}
}
// maxMinStub is a minimal third-party-style Set implementing Maxer and Minner with methods that
// return sentinels, proving the package-level Max/Min honor implementations outside this package's
// concrete types when they report true and fall back to the generic iteration when they report
// false. The embedded Set provides the rest of the interface.
type maxMinStub struct {
Set[int]
optimize bool
}
func (m *maxMinStub) Max() (int, bool) { return 999, m.optimize }
func (m *maxMinStub) Min() (int, bool) { return -999, m.optimize }
func TestMinMaxOptionalInterfaces(t *testing.T) {
t.Parallel()
inner := NewWith(1, 2, 3)
if got := Max(&maxMinStub{Set: inner, optimize: true}); got != 999 {
t.Fatalf("Max = %d, want the Maxer sentinel 999", got)
}
if got := Min(&maxMinStub{Set: inner, optimize: true}); got != -999 {
t.Fatalf("Min = %d, want the Minner sentinel -999", got)
}
if got := Max(&maxMinStub{Set: inner, optimize: false}); got != 3 {
t.Fatalf("Max fallback = %d, want 3", got)
}
if got := Min(&maxMinStub{Set: inner, optimize: false}); got != 1 {
t.Fatalf("Min fallback = %d, want 1", got)
}
}
// predicateStub is a minimal third-party-style Set implementing Equaler, Disjointer, and
// Subsetter with methods that return sentinel answers the generic path would never produce,
// proving the package-level Equal/Disjoint/Subset/Superset honor implementations outside this
// package's concrete types when they report handled and fall back to the generic path when they
// decline. The embedded Set provides the rest of the interface.
type predicateStub struct {
Set[int]
optimize bool
}
func (p *predicateStub) Equal(Set[int]) (bool, bool) { return true, p.optimize }
func (p *predicateStub) Disjoint(Set[int]) (bool, bool) { return true, p.optimize }
func (p *predicateStub) Subset(Set[int]) (bool, bool) { return true, p.optimize }
func TestPredicateOptionalInterfaces(t *testing.T) {
t.Parallel()
// the stub holds {1,2,3}; against {1,2,4} every generic predicate answers false, so a true
// result can only have come from the stub's sentinel
other := NewWith(1, 2, 4)
opt := &predicateStub{Set: NewWith(1, 2, 3), optimize: true}
if !Equal[int](opt, other) {
t.Fatal("Equal ignored the Equaler sentinel")
}
if !Disjoint[int](opt, other) {
t.Fatal("Disjoint ignored the Disjointer sentinel")
}
if !Subset[int](opt, other) {
t.Fatal("Subset ignored the Subsetter sentinel")
}
if !Superset[int](other, opt) { // Superset consults its second operand's Subsetter
t.Fatal("Superset ignored the second operand's Subsetter sentinel")
}
fallback := &predicateStub{Set: NewWith(1, 2, 3), optimize: false}
if Equal[int](fallback, other) {
t.Fatal("Equal did not fall back on decline")
}
if Disjoint[int](fallback, other) {
t.Fatal("Disjoint did not fall back on decline")
}
if Subset[int](fallback, other) {
t.Fatal("Subset did not fall back on decline")
}
if Superset[int](other, fallback) {
t.Fatal("Superset did not fall back on decline")
}
}
// TestNilReceiverPredicates pins the nil-safety of the predicate fast paths: a typed-nil receiver
// or operand must decline (handled=false) rather than dereference backing storage, sending the
// package-level functions down the generic path.
func TestNilReceiverPredicates(t *testing.T) {
t.Parallel()
var ss *SortedSet[int]
var bs *BitSet[int]
if _, ok := ss.Equal(NewSortedSetWith(1)); ok {
t.Fatal("nil SortedSet Equal() reported handled")
}
if _, ok := ss.Disjoint(NewSortedSetWith(1)); ok {
t.Fatal("nil SortedSet Disjoint() reported handled")
}
if _, ok := ss.Subset(NewSortedSetWith(1)); ok {
t.Fatal("nil SortedSet Subset() reported handled")
}
if _, ok := NewSortedSetWith(1).Equal(ss); ok {
t.Fatal("SortedSet Equal(nil) reported handled")
}
if _, ok := bs.Equal(NewBitSetWith(1)); ok {
t.Fatal("nil BitSet Equal() reported handled")
}
if _, ok := bs.Disjoint(NewBitSetWith(1)); ok {
t.Fatal("nil BitSet Disjoint() reported handled")
}
if _, ok := bs.Subset(NewBitSetWith(1)); ok {
t.Fatal("nil BitSet Subset() reported handled")
}
if _, ok := NewBitSetWith(1).Subset(bs); ok {
t.Fatal("BitSet Subset(nil) reported handled")
}
}