-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathsortedset_test.go
210 lines (166 loc) · 5.91 KB
/
sortedset_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
package sortedset
import (
"testing"
)
func checkOrder(t *testing.T, nodes []*SortedSetNode, expectedOrder []string) {
if len(expectedOrder) != len(nodes) {
t.Errorf("nodes does not contain %d elements", len(expectedOrder))
}
for i := 0; i < len(expectedOrder); i++ {
if nodes[i].Key() != expectedOrder[i] {
t.Errorf("nodes[%d] is %q, but the expected key is %q", i, nodes[i].Key(), expectedOrder[i])
}
}
}
func checkIterByRankRange(t *testing.T, sortedset *SortedSet, start int, end int, expectedOrder []string) {
var keys []string
// check nil callback should do nothing
sortedset.IterFuncByRankRange(start, end, nil)
sortedset.IterFuncByRankRange(start, end, func(key string, _ interface{}) bool {
keys = append(keys, key)
return true
})
if len(expectedOrder) != len(keys) {
t.Errorf("keys does not contain %d elements", len(expectedOrder))
}
for i := 0; i < len(expectedOrder); i++ {
if keys[i] != expectedOrder[i] {
t.Errorf("keys[%d] is %q, but the expected key is %q", i, keys[i], expectedOrder[i])
}
}
// check return early
if len(expectedOrder) < 1 {
return
}
// reset data
keys = []string{}
var i int
sortedset.IterFuncByRankRange(start, end, func(key string, _ interface{}) bool {
keys = append(keys, key)
i++
// return early
return i < len(expectedOrder)-1
})
if len(expectedOrder)-1 != len(keys) {
t.Errorf("keys does not contain %d elements", len(expectedOrder)-1)
}
for i := 0; i < len(expectedOrder)-1; i++ {
if keys[i] != expectedOrder[i] {
t.Errorf("keys[%d] is %q, but the expected key is %q", i, keys[i], expectedOrder[i])
}
}
}
func checkRankRangeIterAndOrder(t *testing.T, sortedset *SortedSet, start int, end int, remove bool, expectedOrder []string) {
checkIterByRankRange(t, sortedset, start, end, expectedOrder)
nodes := sortedset.GetByRankRange(start, end, remove)
checkOrder(t, nodes, expectedOrder)
}
func TestCase1(t *testing.T) {
sortedset := New()
sortedset.AddOrUpdate("a", 89, "Kelly")
sortedset.AddOrUpdate("b", 100, "Staley")
sortedset.AddOrUpdate("c", 100, "Jordon")
sortedset.AddOrUpdate("d", -321, "Park")
sortedset.AddOrUpdate("e", 101, "Albert")
sortedset.AddOrUpdate("f", 99, "Lyman")
sortedset.AddOrUpdate("g", 99, "Singleton")
sortedset.AddOrUpdate("h", 70, "Audrey")
sortedset.AddOrUpdate("e", 99, "ntrnrt")
sortedset.Remove("b")
node := sortedset.GetByRank(3, false)
if node == nil || node.Key() != "a" {
t.Error("GetByRank() does not return expected value `a`")
}
node = sortedset.GetByRank(-3, false)
if node == nil || node.Key() != "f" {
t.Error("GetByRank() does not return expected value `f`")
}
// get all nodes since the first one to last one
checkRankRangeIterAndOrder(t, sortedset, 1, -1, false, []string{"d", "h", "a", "e", "f", "g", "c"})
// get & remove the 2nd/3rd nodes in reserve order
checkRankRangeIterAndOrder(t, sortedset, -2, -3, true, []string{"g", "f"})
// get all nodes since the last one to first one
checkRankRangeIterAndOrder(t, sortedset, -1, 1, false, []string{"c", "e", "a", "h", "d"})
}
func TestCase2(t *testing.T) {
// create a new set
sortedset := New()
// fill in new node
sortedset.AddOrUpdate("a", 89, "Kelly")
sortedset.AddOrUpdate("b", 100, "Staley")
sortedset.AddOrUpdate("c", 100, "Jordon")
sortedset.AddOrUpdate("d", -321, "Park")
sortedset.AddOrUpdate("e", 101, "Albert")
sortedset.AddOrUpdate("f", 99, "Lyman")
sortedset.AddOrUpdate("g", 99, "Singleton")
sortedset.AddOrUpdate("h", 70, "Audrey")
// update an existing node
sortedset.AddOrUpdate("e", 99, "ntrnrt")
// remove node
sortedset.Remove("b")
nodes := sortedset.GetByScoreRange(-500, 500, nil)
checkOrder(t, nodes, []string{"d", "h", "a", "e", "f", "g", "c"})
nodes = sortedset.GetByScoreRange(500, -500, nil)
//t.Logf("%v", nodes)
checkOrder(t, nodes, []string{"c", "g", "f", "e", "a", "h", "d"})
nodes = sortedset.GetByScoreRange(600, 500, nil)
checkOrder(t, nodes, []string{})
nodes = sortedset.GetByScoreRange(500, 600, nil)
checkOrder(t, nodes, []string{})
rank := sortedset.FindRank("f")
if rank != 5 {
t.Error("FindRank() does not return expected value `5`")
}
rank = sortedset.FindRank("d")
if rank != 1 {
t.Error("FindRank() does not return expected value `1`")
}
nodes = sortedset.GetByScoreRange(99, 100, nil)
checkOrder(t, nodes, []string{"e", "f", "g", "c"})
nodes = sortedset.GetByScoreRange(90, 50, nil)
checkOrder(t, nodes, []string{"a", "h"})
nodes = sortedset.GetByScoreRange(99, 100, &GetByScoreRangeOptions{
ExcludeStart: true,
})
checkOrder(t, nodes, []string{"c"})
nodes = sortedset.GetByScoreRange(100, 99, &GetByScoreRangeOptions{
ExcludeStart: true,
})
checkOrder(t, nodes, []string{"g", "f", "e"})
nodes = sortedset.GetByScoreRange(99, 100, &GetByScoreRangeOptions{
ExcludeEnd: true,
})
checkOrder(t, nodes, []string{"e", "f", "g"})
nodes = sortedset.GetByScoreRange(100, 99, &GetByScoreRangeOptions{
ExcludeEnd: true,
})
checkOrder(t, nodes, []string{"c"})
nodes = sortedset.GetByScoreRange(50, 100, &GetByScoreRangeOptions{
Limit: 2,
})
checkOrder(t, nodes, []string{"h", "a"})
nodes = sortedset.GetByScoreRange(100, 50, &GetByScoreRangeOptions{
Limit: 2,
})
checkOrder(t, nodes, []string{"c", "g"})
minNode := sortedset.PeekMin()
if minNode == nil || minNode.Key() != "d" {
t.Error("PeekMin() does not return expected value `d`")
}
minNode = sortedset.PopMin()
if minNode == nil || minNode.Key() != "d" {
t.Error("PopMin() does not return expected value `d`")
}
nodes = sortedset.GetByScoreRange(-500, 500, nil)
checkOrder(t, nodes, []string{"h", "a", "e", "f", "g", "c"})
maxNode := sortedset.PeekMax()
if maxNode == nil || maxNode.Key() != "c" {
t.Error("PeekMax() does not return expected value `c`")
}
maxNode = sortedset.PopMax()
if maxNode == nil || maxNode.Key() != "c" {
t.Error("PopMax() does not return expected value `c`")
}
nodes = sortedset.GetByScoreRange(500, -500, nil)
checkOrder(t, nodes, []string{"g", "f", "e", "a", "h"})
}