-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathset_test.go
95 lines (83 loc) · 1.71 KB
/
set_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
package mapset_test
import (
"fmt"
"math/rand"
"testing"
"github.com/zyedidia/generic/mapset"
)
func checkeq[K comparable](set mapset.Set[K], get func(k K) bool, t *testing.T) {
set.Each(func(key K) {
if !get(key) {
t.Fatalf("value %v should be in the set", key)
}
})
}
func TestCrossCheck(t *testing.T) {
stdm := make(map[int]bool)
set := mapset.New[int]()
const nops = 1000
for i := 0; i < nops; i++ {
op := rand.Intn(2)
switch op {
case 0:
key := rand.Int()
stdm[key] = true
set.Put(key)
case 1:
var del int
for k := range stdm {
del = k
break
}
delete(stdm, del)
set.Remove(del)
}
checkeq(set, func(k int) bool {
_, ok := stdm[int(k)]
return ok
}, t)
}
}
func TestOf(t *testing.T) {
testcases := []struct {
name string
input []string
}{
{"init with several items", []string{"foo", "bar", "baz"}},
{"init without values", []string{}},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
set := mapset.Of[string](tc.input...)
if len(tc.input) != set.Size() {
t.Fatalf("expected %d elements in set, got %d", len(tc.input), set.Size())
}
for _, val := range tc.input {
if !set.Has(val) {
t.Fatalf("expected to find val '%s' in set but did not", val)
}
}
})
}
}
func Example() {
set := mapset.New[string]()
set.Put("foo")
set.Put("bar")
set.Put("baz")
fmt.Println("foo", set.Has("foo"))
fmt.Println("quux", set.Has("quux"))
set.Remove("foo")
fmt.Println("foo", set.Has("foo"))
fmt.Println("bar", set.Has("bar"))
set.Clear()
fmt.Println("foo", set.Has("foo"))
fmt.Println("bar", set.Has("bar"))
// Output:
// foo true
// quux false
// foo false
// bar true
// foo false
// bar false
}