-
Notifications
You must be signed in to change notification settings - Fork 6
/
set_test.go
263 lines (253 loc) · 6.8 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package rbtree_test
import (
"sort"
"testing"
"github.com/cdongyang/library/algorithm"
"github.com/cdongyang/library/randint"
"github.com/cdongyang/rbtree"
)
func NewSet(unique bool) *rbtree.Set {
if unique {
return rbtree.NewSet(int(0), rbtree.CompareInt)
}
return rbtree.NewMultiSet(int(0), rbtree.CompareInt)
//fmt.Printf("header: %p header: %+v\n", header, header)
}
func testSet(t *testing.T, length int, unique bool) {
// init
var rand = randint.Rand{First: 23456, Add: 12345, Mod: 1000}
var max = rand.Int()%length + 1
var intSlice1K = make([]int, length)
for i := range intSlice1K {
intSlice1K[i] = (rand.Int() % max) + 1
}
max++
//fmt.Println("offsetNode:", offsetNode)
var tree = NewSet(unique)
var count = make(map[int]int, len(intSlice1K))
// test empty tree and empty tree Begin and End
if !tree.Empty() {
t.Fatal("empty")
}
//fmt.Println(tree.Begin(), tree.End())
if tree.Begin() != tree.End() {
t.Fatal("empty tree begin and end error")
}
var iter = tree.End()
var index int
//test empty tree LowerBound method
iter = tree.LowerBound(intSlice1K[0])
if iter != tree.End() {
t.Fatal("empty tree LowerBound error")
}
//test empty tree UpperBound method
iter = tree.UpperBound(intSlice1K[0])
if iter != tree.End() {
t.Fatal("empty tree UpperBound error")
}
//test Insert,Begin,End method
var maxVal, minVal = intSlice1K[0], intSlice1K[0]
for _, val := range intSlice1K {
if val > maxVal {
maxVal = val
}
if val < minVal {
minVal = val
}
//fmt.Println("insert", val)
_, ok := tree.Insert(val)
if !tree.Unique() && !ok || tree.Unique() && ok == (count[val] != 0) {
t.Fatal("insert error", ok, count[val], val)
}
if tree.Begin().GetData() != minVal || tree.Most(0) != tree.Leftmost() {
t.Fatal("leftmost error", tree.Begin().GetData(), minVal)
}
if tree.End().Last().GetData() != maxVal || tree.Most(1) != tree.Rightmost() {
t.Fatal("rightmost error", tree.End().Last().GetData(), maxVal)
}
if tree.Unique() {
count[val] = 1
} else {
count[val]++
}
if tree.Count(val) != count[val] {
t.Fatal("count error", tree.Count(val), count[val])
}
_, size := tree.Check()
if size != tree.Size() {
t.Fatal("size error", size, tree.Size())
}
}
// test Compare
if tree.Begin().GetData() != minVal {
t.Fatal("Compare error")
}
var sortSlice = make([]int, len(intSlice1K))
copy(sortSlice, intSlice1K)
sort.IntSlice(sortSlice).Sort()
//test LowerBound method
iter = tree.LowerBound(intSlice1K[0])
index = algorithm.LowerBound(algorithm.SearchIntSlice{sortSlice, intSlice1K[0]})
if iter == tree.End() && index == len(sortSlice) {
//ok
} else if iter.GetData() == intSlice1K[0] {
//ok
} else {
t.Fatal("LowerBound error")
}
//test UpperBound method
iter = tree.UpperBound(intSlice1K[0])
index = algorithm.UpperBound(algorithm.SearchIntSlice{sortSlice, intSlice1K[0]})
if iter == tree.End() && index == len(sortSlice) {
//ok
} else if iter.GetData() == intSlice1K[0] {
}
//test Begin and EndNode method
if tree.Begin().GetData() != sortSlice[0] {
t.Fatal("begin error", tree.Begin().GetData(), sortSlice[0])
}
if tree.End().Last().GetData() != sortSlice[len(sortSlice)-1] {
t.Fatal("endNode error", tree.Begin().GetData(), sortSlice[len(sortSlice)-1])
}
//test Begin and End and Next method
var i int
for it := tree.Begin(); it != tree.End(); it = it.Next() {
if it.GetData() != sortSlice[i] {
t.Fatal("go through error", it.GetData(), sortSlice[i])
}
if tree.Unique() {
for i+1 < len(sortSlice) && sortSlice[i+1] == sortSlice[i] {
i++
}
}
i++
}
//test End and Last method
i = len(sortSlice) - 1
for it := tree.End().Last(); ; it = it.Last() {
if it.GetData() != sortSlice[i] {
t.Fatal("go back tree error", it.GetData(), sortSlice[i])
}
if tree.Unique() {
for i > 0 && sortSlice[i-1] == sortSlice[i] {
i--
}
}
i--
if tree.Begin() == it {
break
}
}
//test Find method
iter = tree.Find(intSlice1K[0])
index = sort.SearchInts(sortSlice, intSlice1K[0])
if iter == tree.End() && index == len(sortSlice) {
//ok
} else if iter.GetData() == sortSlice[index] {
//ok
} else {
t.Fatal("Find error")
}
if tree.Find(max) != tree.End() {
t.Fatal("find max error", max)
}
//unique sortslice
var uniqueN = 1
for i := range sortSlice {
if i > 0 && sortSlice[i] != sortSlice[i-1] {
sortSlice[uniqueN] = sortSlice[i]
uniqueN++
}
}
sortSlice = sortSlice[:uniqueN]
//test Erase method
for _, val := range intSlice1K {
if tree.Most(0) != tree.Leftmost() {
t.Fatalf("tree most error,%v %v %v %d\n", tree.Most(0), tree.Leftmost(), tree.End(), tree.Size())
}
if tree.Most(1) != tree.Rightmost() {
t.Fatalf("tree most error,%v %v %v %d\n", tree.Most(1), tree.Rightmost(), tree.End(), tree.Size())
}
//fmt.Println("erase", val)
num := tree.Erase(val)
if num != count[val] {
t.Fatal("erase error", num, count[val], val)
}
delete(count, val)
if tree.Count(val) != count[val] {
t.Fatal("count error", tree.Count(val), count[val])
}
_, size := tree.Check()
if size != tree.Size() {
t.Fatal("size error")
}
}
//test Empty method
if !tree.Empty() {
t.Fatal("empty error")
}
count = make(map[int]int)
for _, val := range intSlice1K {
_, ok := tree.Insert(val)
if !tree.Unique() && !ok || tree.Unique() && ok == (count[val] != 0) {
t.Fatal("insert error", ok, count[val], val)
}
if tree.Unique() {
count[val] = 1
} else {
count[val]++
}
if tree.Count(val) != count[val] {
t.Fatal("count error", tree.Count(val), count[val])
}
_, size := tree.Check()
if size != tree.Size() {
t.Fatal("size error", size, tree.Size())
}
}
//test EqualRange and EraseNodeRange
for _, val := range intSlice1K {
if tree.Most(0) != tree.Leftmost() {
t.Fatalf("tree most error,%v %v %v %d\n", tree.Most(0), tree.Leftmost(), tree.End(), tree.Size())
}
if tree.Most(1) != tree.Rightmost() {
t.Fatalf("tree most error,%v %v %v %d\n", tree.Most(1), tree.Rightmost(), tree.End(), tree.Size())
}
//fmt.Println("erase", val)
beg, end := tree.EqualRange(val)
num := tree.EraseNodeRange(beg, end)
if num != count[val] {
t.Fatal("erase error", num, count[val], val)
}
delete(count, val)
if tree.Count(val) != count[val] {
t.Fatal("count error", tree.Count(val), count[val])
}
_, size := tree.Check()
if size != tree.Size() {
t.Fatal("size error")
}
}
if tree.Size() != 0 || !tree.Empty() {
t.Fatal("clear error,size != 0 or not empty")
}
}
func TestSet(t *testing.T) {
var testN = 500
t.Run("unique", func(t *testing.T) {
for i := 0; i < testN; i++ {
testSet(t, i+1, true)
}
})
t.Run("not unique", func(t *testing.T) {
for i := 0; i < testN; i++ {
testSet(t, i+1, false)
}
})
t.Run("unique 1e4", func(t *testing.T) {
testSet(t, 1e4, true)
})
t.Run("not unique 1e4", func(t *testing.T) {
testSet(t, 1e4, false)
})
}