-
Notifications
You must be signed in to change notification settings - Fork 10
/
set_test.go
167 lines (148 loc) · 2.69 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
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
package hashmap
import (
"math/rand"
"reflect"
"sort"
"sync"
"testing"
"time"
)
func TestSet(t *testing.T) {
var s Set[int]
keys := rand.Perm(1000000)
for i := 0; i < len(keys); i++ {
s.Insert(keys[i])
if s.Len() != i+1 {
t.Fatalf("expected %d got %d", i+1, s.Len())
}
}
for i := 0; i < len(keys); i++ {
ok := s.Contains(keys[i])
if !ok {
t.Fatalf("expected true")
}
}
var skeys []int
s.Scan(func(key int) bool {
skeys = append(skeys, key)
return true
})
if len(skeys) != s.Len() {
t.Fatalf("expected %d got %d", len(skeys), s.Len())
}
for i := 0; i < len(keys); i++ {
s.Delete(keys[i])
if s.Len() != len(keys)-i-1 {
t.Fatalf("expected %d got %d", len(keys)-i-1, s.Len())
}
}
}
func TestSetKeys(t *testing.T) {
var s Set[string]
s.Insert("key")
expect := []string{"key"}
got := s.Keys()
if !reflect.DeepEqual(got, expect) {
t.Fatal("expected Keys equal")
}
}
func copySetEntries(m *Set[int]) []int {
all := m.Keys()
sort.Ints(all)
return all
}
func setEntriesEqual(a, b []int) bool {
return reflect.DeepEqual(a, b)
}
func copySetTest(N int, s1 *Set[int], e11 []int, deep bool) {
e12 := copySetEntries(s1)
if !setEntriesEqual(e11, e12) {
panic("!")
}
// Make a copy and compare the values
s2 := s1.Copy()
e21 := copySetEntries(s1)
if !setEntriesEqual(e21, e12) {
panic("!")
}
// Delete every other key
var e22 []int
for i, j := range rand.Perm(N) {
if i&1 == 0 {
e22 = append(e22, e21[j])
} else {
s2.Delete(e21[j])
}
}
if s2.Len() != N/2 {
panic("!")
}
sort.Ints(e22)
e23 := copySetEntries(s2)
if !setEntriesEqual(e23, e22) {
panic("!")
}
if !deep {
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
copySetTest(N/2, s2, e23, true)
}()
go func() {
defer wg.Done()
copySetTest(N/2, s2, e23, true)
}()
wg.Wait()
}
e24 := copySetEntries(s2)
if !setEntriesEqual(e24, e23) {
panic("!")
}
}
func TestSetCopy(t *testing.T) {
N := 1_000
// create the initial map
s1 := new(Set[int])
for s1.Len() < N {
s1.Insert(rand.Int())
}
e11 := copySetEntries(s1)
dur := time.Second * 2
var wg sync.WaitGroup
for i := 0; i < 16; i++ {
wg.Add(1)
go func() {
defer wg.Done()
start := time.Now()
for time.Since(start) < dur {
copySetTest(N, s1, e11, false)
}
}()
}
wg.Wait()
e12 := copySetEntries(s1)
if !setEntriesEqual(e11, e12) {
panic("!")
}
}
func TestSetGetPos(t *testing.T) {
var m Set[int]
if _, ok := m.GetPos(100); ok {
t.Fatal()
}
for i := 0; i < 1000; i++ {
m.Insert(i)
}
m2 := make(map[int]int)
for i := 0; i < 10000; i++ {
key, ok := m.GetPos(uint64(i))
if !ok {
t.Fatal()
}
m2[key] = key
}
if len(m2) != m.Len() {
t.Fatal()
}
}