-
Notifications
You must be signed in to change notification settings - Fork 0
/
randset_test.go
162 lines (142 loc) · 4.11 KB
/
randset_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
package randset_test
import (
"fmt"
"math/rand"
"reflect"
"slices"
"testing"
"github.com/s3rj1k/go-randset"
)
const (
fnAdd = "Add"
fnClear = "Clear"
fnContains = "Contains"
fnContent = "Content"
fnIsEmpty = "IsEmpty"
fnLoadAndDelete = "LoadAndDelete"
fnRemove = "Remove"
fnSize = "Size"
)
func TestRandomizedSet(t *testing.T) {
tests := []struct {
name string
operations []string
values [][]int
expectedResults []any
}{
{
name: fnIsEmpty,
operations: []string{fnIsEmpty},
values: [][]int{{}},
expectedResults: []any{true},
},
{
name: fmt.Sprintf("%s and %s", fnAdd, fnContains),
operations: []string{fnAdd, fnContains},
values: [][]int{{1}, {1}},
expectedResults: []any{nil, true},
},
{
name: fmt.Sprintf("%s %s and %s", fnAdd, fnRemove, fnContains),
operations: []string{fnAdd, fnRemove, fnContains},
values: [][]int{{2}, {2}, {2}},
expectedResults: []any{nil, nil, false},
},
{
name: fmt.Sprintf("%s and %s", fnClear, fnIsEmpty),
operations: []string{fnAdd, fnContains, fnClear, fnIsEmpty},
values: [][]int{{1}, {1}, {}, {}},
expectedResults: []any{nil, true, nil, true},
},
{
name: fmt.Sprintf("%s and %s", fnContent, fnSize),
operations: []string{fnAdd, fnAdd, fnContent, fnSize},
values: [][]int{{1}, {2}, {}, {}},
expectedResults: []any{nil, nil, []int{1, 2}, 2},
},
{
name: fnLoadAndDelete,
operations: []string{fnAdd, fnLoadAndDelete, fnContains, fnIsEmpty},
values: [][]int{{1}, {}, {1}, {}},
expectedResults: []any{nil, true, false, true},
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
set := randset.New[int]()
for i, operation := range tt.operations {
switch operation {
case fnAdd:
set.Add(tt.values[i][0])
case fnClear:
set.Clear()
case fnContains:
got := set.Contains(tt.values[i][0])
if got != tt.expectedResults[i] {
t.Errorf("%s: %s(%d) = %v, want %v",
tt.name, operation, tt.values[i][0], got, tt.expectedResults[i],
)
}
case fnContent:
got := set.Content()
slices.Sort(got)
expected, ok := tt.expectedResults[i].([]int)
if !ok {
t.Fatalf("Unexpected value type: %T", expected)
}
slices.Sort(expected)
if !reflect.DeepEqual(got, expected) {
t.Errorf("%s: %s() = %v, want %v", tt.name, operation, got, expected)
}
case fnLoadAndDelete:
removedKey, removed := set.LoadAndDelete()
if !removed || set.Contains(removedKey) {
t.Errorf("%s: %s() failed", tt.name, operation)
}
case fnIsEmpty:
got := set.IsEmpty()
if got != tt.expectedResults[i] {
t.Errorf("%s: %s() = %v, want %v", tt.name, operation, got, tt.expectedResults[i])
}
case fnRemove:
set.Remove(tt.values[i][0])
case fnSize:
got := set.Size()
if got != tt.expectedResults[i] {
t.Errorf("%s: %s() = %v, want %v", tt.name, operation, got, tt.expectedResults[i])
}
}
}
})
}
}
const (
setSize = 500000
)
func TestLoadAndDelete(t *testing.T) {
set := randset.NewWithInitialSize[uint64](setSize)
for set.Size() < setSize {
set.Add(rand.Uint64()) //nolint:gosec // G404: Use of weak random number generator
}
if set.Size() != setSize {
t.Fatalf("Initial setup failed, set size = %d, expected %d", set.Size(), setSize)
}
for i := range setSize {
beforeSize := set.Size()
val, found := set.LoadAndDelete()
if !found {
t.Fatalf("%s() failed to return a value on iteration %d", fnLoadAndDelete, i)
}
if set.Contains(val) {
t.Errorf("Expected the value %d to be removed from set, but it was not", val)
}
afterSize := set.Size()
if beforeSize-1 != afterSize {
t.Errorf("Expected size to decrease by 1, but it went from %d to %d on iteration %d", beforeSize, afterSize, i)
}
}
if !set.IsEmpty() {
t.Errorf("Expected the set to be empty after %d removals, but it was not", setSize)
}
}