File: set_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (537 lines) | stat: -rw-r--r-- 11,240 bytes parent folder | download
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright ©2014 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package set

import (
	"reflect"
	"testing"
)

type node int64

func (n node) ID() int64 { return int64(n) }

// TestSameInts tests the assumption that pointer equality via unsafe conversion
// of a map[int]struct{} to uintptr is a valid test for perfect identity between
// set values. If any of the tests in TestSame fail, the package is broken and same
// must be reimplemented to conform to the runtime map implementation. The relevant
// code to look at (at least for gc) is the hmap type in runtime/map.go.

func TestSameInts(t *testing.T) {
	var (
		a = make(Ints[int64])
		b = make(Ints[int64])
		c = a
	)

	if intsSame(a, b) {
		t.Error("Independently created sets test as same")
	}
	if !intsSame(a, c) {
		t.Error("Ints copy and original test as not same.")
	}
	a.Add(1)
	if !intsSame(a, c) {
		t.Error("Ints copy and original test as not same after addition.")
	}
	if !intsSame[int64](nil, nil) {
		t.Error("nil sets test as not same.")
	}
	if intsSame(b, nil) {
		t.Error("nil and empty sets test as same.")
	}
}

func TestAddInts(t *testing.T) {
	s := make(Ints[int64])
	if s.Count() != 0 {
		t.Error("Ints somehow contains new elements upon creation")
	}

	s.Add(1)
	s.Add(3)
	s.Add(5)

	if s.Count() != 3 {
		t.Error("Incorrect number of set elements after adding")
	}

	if !s.Has(1) || !s.Has(3) || !s.Has(5) {
		t.Error("Ints doesn't contain element that was added")
	}

	s.Add(1)

	if s.Count() > 3 {
		t.Error("Ints double-adds element (element not unique)")
	} else if s.Count() < 3 {
		t.Error("Ints double-add lowered len")
	}

	if !s.Has(1) {
		t.Error("Ints doesn't contain double-added element")
	}

	if !s.Has(3) || !s.Has(5) {
		t.Error("Ints removes element on double-add")
	}
}

func TestRemoveInts(t *testing.T) {
	s := make(Ints[int64])

	s.Add(1)
	s.Add(3)
	s.Add(5)

	s.Remove(1)

	if s.Count() != 2 {
		t.Error("Incorrect number of set elements after removing an element")
	}

	if s.Has(1) {
		t.Error("Element present after removal")
	}

	if !s.Has(3) || !s.Has(5) {
		t.Error("Ints remove removed wrong element")
	}

	s.Remove(1)

	if s.Count() != 2 || s.Has(1) {
		t.Error("Double set remove does something strange")
	}

	s.Add(1)

	if s.Count() != 3 || !s.Has(1) {
		t.Error("Cannot add element after removal")
	}
}

func TestSelfEqualInts(t *testing.T) {
	s := make(Ints[int64])

	if !IntsEqual(s, s) {
		t.Error("Ints is not equal to itself")
	}

	s.Add(1)

	if !IntsEqual(s, s) {
		t.Error("Ints ceases self equality after adding element")
	}
}

func TestEqualInts(t *testing.T) {
	a := make(Ints[int64])
	b := make(Ints[int64])

	if !IntsEqual(a, b) {
		t.Error("Two different empty sets not equal")
	}

	a.Add(1)
	if IntsEqual(a, b) {
		t.Error("Two different sets with different sizes equal")
	}

	b.Add(1)
	if !IntsEqual(a, b) {
		t.Error("Two sets with same element not equal")
	}

	b.Remove(1)
	b.Add(2)
	if IntsEqual(a, b) {
		t.Error("Two different sets with different elements equal")
	}
}

func TestSameNodes(t *testing.T) {
	var (
		a = NewNodes()
		b = NewNodes()
		c = a
	)

	if same(a, b) {
		t.Error("Independently created sets test as same")
	}
	if !same(a, c) {
		t.Error("Ints copy and original test as not same.")
	}
	a.Add(node(1))
	if !same(a, c) {
		t.Error("Ints copy and original test as not same after addition.")
	}
	if !same(nil, nil) {
		t.Error("nil sets test as not same.")
	}
	if same(b, nil) {
		t.Error("nil and empty sets test as same.")
	}
}

func TestAddNodes(t *testing.T) {
	s := NewNodes()
	if s == nil {
		t.Fatal("Ints cannot be created successfully")
	}

	if s.Count() != 0 {
		t.Error("Ints somehow contains new elements upon creation")
	}

	s.Add(node(1))
	s.Add(node(3))
	s.Add(node(5))

	if s.Count() != 3 {
		t.Error("Incorrect number of set elements after adding")
	}

	if !s.Has(node(1)) || !s.Has(node(3)) || !s.Has(node(5)) {
		t.Error("Ints doesn't contain element that was added")
	}

	s.Add(node(1))

	if s.Count() > 3 {
		t.Error("Ints double-adds element (element not unique)")
	} else if s.Count() < 3 {
		t.Error("Ints double-add lowered len")
	}

	if !s.Has(node(1)) {
		t.Error("Ints doesn't contain double-added element")
	}

	if !s.Has(node(3)) || !s.Has(node(5)) {
		t.Error("Ints removes element on double-add")
	}

	for e, n := range s {
		if e != n.ID() {
			t.Errorf("Element ID did not match key: %d != %d", e, n.ID())
		}
	}
}

func TestRemoveNodes(t *testing.T) {
	s := NewNodes()

	s.Add(node(1))
	s.Add(node(3))
	s.Add(node(5))

	s.Remove(node(1))

	if s.Count() != 2 {
		t.Error("Incorrect number of set elements after removing an element")
	}

	if s.Has(node(1)) {
		t.Error("Element present after removal")
	}

	if !s.Has(node(3)) || !s.Has(node(5)) {
		t.Error("Ints remove removed wrong element")
	}

	s.Remove(node(1))

	if s.Count() != 2 || s.Has(node(1)) {
		t.Error("Double set remove does something strange")
	}

	s.Add(node(1))

	if s.Count() != 3 || !s.Has(node(1)) {
		t.Error("Cannot add element after removal")
	}
}

func TestSelfEqualNodes(t *testing.T) {
	s := NewNodes()

	if !Equal(s, s) {
		t.Error("Ints is not equal to itself")
	}

	s.Add(node(1))

	if !Equal(s, s) {
		t.Error("Ints ceases self equality after adding element")
	}
}

func TestEqualNodes(t *testing.T) {
	a := NewNodes()
	b := NewNodes()

	if !Equal(a, b) {
		t.Error("Two different empty sets not equal")
	}

	a.Add(node(1))
	if Equal(a, b) {
		t.Error("Two different sets with different sizes equal")
	}

	b.Add(node(1))
	if !Equal(a, b) {
		t.Error("Two sets with same element not equal")
	}

	b.Remove(node(1))
	b.Add(node(2))
	if Equal(a, b) {
		t.Error("Two different sets with different elements equal")
	}
}

func TestCopyNodes(t *testing.T) {
	a := NewNodes()

	a.Add(node(1))
	a.Add(node(2))
	a.Add(node(3))

	b := CloneNodes(a)

	if !Equal(a, b) {
		t.Fatalf("Two sets not equal after copy: %v != %v", a, b)
	}

	b.Remove(node(1))

	if Equal(a, b) {
		t.Errorf("Mutating one set mutated another after copy: %v == %v", a, b)
	}
}

func TestUnionSameNodes(t *testing.T) {
	a := NewNodes()
	b := NewNodes()

	a.Add(node(1))
	a.Add(node(2))

	b.Add(node(1))
	b.Add(node(2))

	c := UnionOfNodes(a, b)

	if c.Count() != 2 {
		t.Error("Union of same sets yields set with wrong len")
	}

	if !c.Has(node(1)) || !c.Has(node(2)) {
		t.Error("Union of same sets yields wrong elements")
	}

	for i, s := range []Nodes{a, b, c} {
		for e, n := range s {
			if e != n.ID() {
				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
			}
		}
	}
}

func TestUnionDiffNodes(t *testing.T) {
	a := NewNodes()
	b := NewNodes()

	a.Add(node(1))
	a.Add(node(2))

	b.Add(node(3))

	c := UnionOfNodes(a, b)

	if c.Count() != 3 {
		t.Error("Union of different sets yields set with wrong len")
	}

	if !c.Has(node(1)) || !c.Has(node(2)) || !c.Has(node(3)) {
		t.Error("Union of different sets yields set with wrong elements")
	}

	if a.Has(node(3)) || !a.Has(node(2)) || !a.Has(node(1)) || a.Count() != 2 {
		t.Error("Union of sets mutates non-destination set (argument 1)")
	}

	if !b.Has(node(3)) || b.Has(node(1)) || b.Has(node(2)) || b.Count() != 1 {
		t.Error("Union of sets mutates non-destination set (argument 2)")
	}

	for i, s := range []Nodes{a, b, c} {
		for e, n := range s {
			if e != n.ID() {
				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
			}
		}
	}

	c = UnionOfNodes(a, a)
	if !reflect.DeepEqual(c, a) {
		t.Errorf("Union of equal sets not equal to sets: %v != %v", c, a)
	}
}

func TestUnionOverlappingNodes(t *testing.T) {
	a := NewNodes()
	b := NewNodes()

	a.Add(node(1))
	a.Add(node(2))

	b.Add(node(2))
	b.Add(node(3))

	c := UnionOfNodes(a, b)

	if c.Count() != 3 {
		t.Error("Union of overlapping sets yields set with wrong len")
	}

	if !c.Has(node(1)) || !c.Has(node(2)) || !c.Has(node(3)) {
		t.Error("Union of overlapping sets yields set with wrong elements")
	}

	if a.Has(node(3)) || !a.Has(node(2)) || !a.Has(node(1)) || a.Count() != 2 {
		t.Error("Union of sets mutates non-destination set (argument 1)")
	}

	if !b.Has(node(3)) || b.Has(node(1)) || !b.Has(node(2)) || b.Count() != 2 {
		t.Error("Union of sets mutates non-destination set (argument 2)")
	}

	for i, s := range []Nodes{a, b, c} {
		for e, n := range s {
			if e != n.ID() {
				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
			}
		}
	}

	c = IntersectionOfNodes(a, a)
	if !reflect.DeepEqual(c, a) {
		t.Errorf("Intersection of equal sets not equal to sets: %v != %v", c, a)
	}
}

func TestIntersectSameNodes(t *testing.T) {
	a := NewNodes()
	b := NewNodes()

	a.Add(node(2))
	a.Add(node(3))

	b.Add(node(2))
	b.Add(node(3))

	c := IntersectionOfNodes(a, b)

	if card := c.Count(); card != 2 {
		t.Errorf("Intersection of identical sets yields set of wrong len %d", card)
	}

	if !c.Has(node(2)) || !c.Has(node(3)) {
		t.Error("Intersection of identical sets yields set of wrong elements")
	}

	for i, s := range []Nodes{a, b, c} {
		for e, n := range s {
			if e != n.ID() {
				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
			}
		}
	}
}

func TestIntersectDiffNodes(t *testing.T) {
	a := NewNodes()
	b := NewNodes()

	a.Add(node(2))
	a.Add(node(3))

	b.Add(node(1))
	b.Add(node(4))

	c := IntersectionOfNodes(a, b)

	if card := c.Count(); card != 0 {
		t.Errorf("Intersection of different yields non-empty set %d", card)
	}

	if !a.Has(node(2)) || !a.Has(node(3)) || a.Has(node(1)) || a.Has(node(4)) || a.Count() != 2 {
		t.Error("Intersection of sets mutates non-destination set (argument 1)")
	}

	if b.Has(node(2)) || b.Has(node(3)) || !b.Has(node(1)) || !b.Has(node(4)) || b.Count() != 2 {
		t.Error("Intersection of sets mutates non-destination set (argument 1)")
	}

	for i, s := range []Nodes{a, b, c} {
		for e, n := range s {
			if e != n.ID() {
				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
			}
		}
	}
}

func TestIntersectOverlappingNodes(t *testing.T) {
	a := NewNodes()
	b := NewNodes()

	a.Add(node(2))
	a.Add(node(3))

	b.Add(node(3))
	b.Add(node(4))

	c := IntersectionOfNodes(a, b)

	if card := c.Count(); card != 1 {
		t.Errorf("Intersection of overlapping sets yields set of incorrect len %d", card)
	}

	if !c.Has(node(3)) {
		t.Errorf("Intersection of overlapping sets yields set with wrong element")
	}

	if !a.Has(node(2)) || !a.Has(node(3)) || a.Has(node(4)) || a.Count() != 2 {
		t.Error("Intersection of sets mutates non-destination set (argument 1)")
	}

	if b.Has(node(2)) || !b.Has(node(3)) || !b.Has(node(4)) || b.Count() != 2 {
		t.Error("Intersection of sets mutates non-destination set (argument 1)")
	}

	for i, s := range []Nodes{a, b, c} {
		for e, n := range s {
			if e != n.ID() {
				t.Errorf("Element ID did not match key in s%d: %d != %d", i+1, e, n.ID())
			}
		}
	}

	c = IntersectionOfNodes(c, a)
	want := Nodes{3: node(3)}
	if !reflect.DeepEqual(c, want) {
		t.Errorf("Intersection of sets with dst equal to a not equal: %v != %v", c, want)
	}
	c = IntersectionOfNodes(a, c)
	if !reflect.DeepEqual(c, want) {
		t.Errorf("Intersection of sets with dst equal to a not equal: %v != %v", c, want)
	}
}