forked from zyedidia/generic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_test.go
98 lines (84 loc) · 1.77 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
96
97
98
package hashset_test
import (
"fmt"
"math/rand"
"testing"
g "github.com/zyedidia/generic"
"github.com/zyedidia/generic/hashset"
)
func checkeq[K any](set *hashset.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 := hashset.New[int](1, g.Equals[int], g.HashInt)
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 := hashset.Of[string](10, g.Equals[string], g.HashString, 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 := hashset.New[string](3, g.Equals[string], g.HashString)
set.Put("foo")
set.Put("bar")
set.Put("baz")
fmt.Println(set.Has("foo"))
fmt.Println(set.Has("quux"))
set.Remove("foo")
fmt.Println(set.Has("foo"))
fmt.Println(set.Has("bar"))
set.Clear()
fmt.Println(set.Has("foo"))
fmt.Println(set.Has("bar"))
// Output:
// true
// false
// false
// true
// false
// false
}