-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathskipfilter_test.go
199 lines (196 loc) · 4.67 KB
/
skipfilter_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
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
package skipfilter
import (
"fmt"
"testing"
)
func TestSkipFilter(t *testing.T) {
t.Run("New", func(t *testing.T) {
var sf *SkipFilter
t.Run("success", func(t *testing.T) {
test := func(value interface{}, filter interface{}) bool {
return true
}
for i, n := range []int{-1, 0, 10} {
t.Run(fmt.Sprintf("size %d", n), func(t *testing.T) {
sf = New(test, n)
if sf == nil {
t.Fatalf("New returned nil (%d)", i)
}
})
}
})
})
modTest := func(value interface{}, filter interface{}) bool {
// value passes filter if value is multiple of filter
return value.(int)%filter.(int) == 0
}
t.Run("Add", func(t *testing.T) {
sf := New(modTest, 10)
t.Run("success", func(t *testing.T) {
for i := 0; i < 10; i++ {
sf.Add(i)
}
})
})
t.Run("Remove", func(t *testing.T) {
sf := New(modTest, 10)
t.Run("success", func(t *testing.T) {
for i := 0; i < 10; i++ {
sf.Add(i)
}
sf.Remove(0)
sf.Remove(11)
})
})
t.Run("MatchAny", func(t *testing.T) {
sf := New(modTest, 10)
for i := 0; i < 10; i++ {
sf.Add(i)
}
t.Run("success", func(t *testing.T) {
res := sf.MatchAny(1)
if len(res) != 10 {
t.Fatalf("Expected 10 results, received (%d)", len(res))
}
res = sf.MatchAny(2)
if len(res) != 5 {
t.Fatalf("Expected 5 results, received (%d)", len(res))
}
res = sf.MatchAny(2)
if len(res) != 5 {
t.Fatalf("Expected 5 results, received (%d)", len(res))
}
})
t.Run("removal", func(t *testing.T) {
sf.Remove(0)
res := sf.MatchAny(1)
if len(res) != 9 {
t.Fatalf("Expected 9 results, received (%d)", len(res))
}
res = sf.MatchAny(2)
if len(res) != 4 {
t.Fatalf("Expected 4 results, received (%d)", len(res))
}
})
t.Run("remove all", func(t *testing.T) {
for i := 1; i < 10; i++ {
sf.Remove(i)
}
res := sf.MatchAny(1)
if len(res) != 0 {
t.Fatalf("Expected 0 results, received (%d)", len(res))
}
res = sf.MatchAny(2)
if len(res) != 0 {
t.Fatalf("Expected 0 results, received (%d)", len(res))
}
})
})
t.Run("Walk", func(t *testing.T) {
sf := New(modTest, 10)
for i := 0; i < 10; i++ {
sf.Add(i)
}
t.Run("success", func(t *testing.T) {
var n uint64
id := sf.Walk(5, func(i interface{}) bool {
n++
return n < 5
})
if n != 5 {
t.Fatalf("Expected WalkAll to execute callback 5 times, received (%d)", n)
}
if id != 10 {
t.Fatalf(`Expected WalkAll to return cursor "10", received (%d)`, id)
}
})
t.Run("removal", func(t *testing.T) {
sf.Remove(0)
var n uint64
id := sf.Walk(5, func(i interface{}) bool {
n++
return n < 5
})
if n != 5 {
t.Fatalf("Expected WalkAll to execute callback 5 times, received (%d)", n)
}
if id != 10 {
t.Fatalf(`Expected WalkAll to return cursor "10", received (%d)`, id)
}
})
})
t.Run("Walk all", func(t *testing.T) {
sf := New(modTest, 10)
for i := 0; i < 10; i++ {
sf.Add(i)
}
t.Run("success", func(t *testing.T) {
var n uint64
id := sf.Walk(0, func(i interface{}) bool {
n++
return true
})
if n != 10 {
t.Fatalf("Expected WalkAll to execute callback 10 times, received (%d)", n)
}
if id != 10 {
t.Fatalf(`Expected WalkAll to return cursor "10", received (%d)`, id)
}
})
t.Run("removal", func(t *testing.T) {
sf.Remove(0)
var n uint64
id := sf.Walk(0, func(i interface{}) bool {
n++
return true
})
if n != 9 {
t.Fatalf("Expected WalkAll to execute callback 9 times, received (%d)", n)
}
if id != 10 {
t.Fatalf(`Expected WalkAll to return cursor "10", received (%d)`, id)
}
})
sf = New(modTest, 10)
t.Run("empty", func(t *testing.T) {
var n uint64
id := sf.Walk(0, func(i interface{}) bool {
n++
return true
})
if n != 0 {
t.Fatalf("Expected WalkAll to execute callback 0 times, received (%d)", n)
}
if id != 0 {
t.Fatalf(`Expected WalkAll to return cursor "0", received (%d)`, id)
}
})
t.Run("one", func(t *testing.T) {
sf.Add(0)
var n uint64
id := sf.Walk(0, func(i interface{}) bool {
n++
return true
})
if n != 1 {
t.Fatalf("Expected WalkAll to execute callback 1 time, received (%d)", n)
}
if id != 1 {
t.Fatalf(`Expected WalkAll to return cursor "1", received (%d)`, id)
}
})
})
}
func TestEntry(t *testing.T) {
e := &entry{123, "test"}
t.Run("ExtractKey", func(t *testing.T) {
if e.ExtractKey() != float64(123) {
t.Fatalf(`Expected ExtractKey to return 123, received (%f)`, e.ExtractKey())
}
})
t.Run("String", func(t *testing.T) {
if e.String() != " 7b" {
t.Fatalf(`Expected ExtractKey to return " 7b", received (%s)`, e.String())
}
})
}