-
Notifications
You must be signed in to change notification settings - Fork 1
/
sortedset_test.go
164 lines (126 loc) · 4.5 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
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 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
nodes := sortedset.GetByRankRange(1, -1, false)
checkOrder(t, nodes, []string{"d", "h", "a", "e", "f", "g", "c"})
// get & remove the 2nd/3rd nodes in reserve order
nodes = sortedset.GetByRankRange(-2, -3, true)
checkOrder(t, nodes, []string{"g", "f"})
// get all nodes since the last one to first one
nodes = sortedset.GetByRankRange(-1, 1, false)
checkOrder(t, nodes, []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"})
}