-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted_set_test.go
216 lines (177 loc) · 3.95 KB
/
sorted_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
package sset
import (
"testing"
)
func TestSortedSetWrite(t *testing.T) {
t.Run("SortedSetAdd", func(t *testing.T) {
s := SortedSet{}
s.Init()
result := s.Get(5)
if len(result) != 0 {
t.Errorf("Result length should not be greater than zero for Get call")
return
}
result = s.GetRange(5, 10)
if len(result) != 0 {
t.Errorf("Result length should not be greater than zero for GetRange call")
return
}
s.Add("Hello", 5)
s.Add("World", 5)
s.Add("World2", 6)
result = s.Get(5)
if result[0] != "Hello" && result[1] != "Hello" {
t.Errorf("Hello not present in result.")
return
}
if result[0] != "World" && result[1] != "World" {
t.Errorf("World not present in result")
return
}
if s.Add("World2", 7) {
t.Errorf("World2 should not have been added")
return
}
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("Negative Rank addition did not panic")
return
}
}()
s.Add("NegativeRank", -1)
}()
})
t.Run("SortedSetRemove", func(t *testing.T) {
s := SortedSet{}
s.Init()
if s.Remove("Hello") {
t.Errorf("Removal of non existant key should return false")
return
}
s.Add("Hello", 5)
s.Add("World", 5)
if !s.Remove("Hello") {
t.Errorf("Removal of existing key should return true")
return
}
result := s.Get(5)
if len(result) != 1 {
t.Errorf("Length of result should be: 1, got: %d", len(result))
return
}
if result[0] != "World" {
t.Errorf("World not pesent in the result")
return
}
})
}
func TestSortedSetRead(t *testing.T) {
t.Run("SortedSetGet", func(t *testing.T) {
s := SortedSet{}
s.Init()
s.Add("Member1", 5)
s.Add("Member2", 5)
s.Add("Member3", 6)
result := s.Get(5)
if len(result) != 2 {
t.Errorf("Expected length of result to be 2, actual is: %d", len(result))
return
}
if result[0] != "Member1" && result[1] != "Member1" {
t.Errorf("Member1 is not present in the result")
return
}
if result[0] != "Member2" && result[1] != "Member2" {
t.Errorf("Member2 is not present in the result")
return
}
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("Negative Rank searching did not panic")
return
}
}()
s.Get(-1)
}()
})
t.Run("SortedSetGetRange", func(t *testing.T) {
s := SortedSet{}
s.Init()
result := s.GetRange(5, 10)
if len(result) != 0 {
t.Errorf("Result length should not be greater than zero for GetRange call")
return
}
s.Add("Hello", 5)
s.Add("World", 5)
s.Add("World2", 6)
result = s.GetRange(4, 6)
if len(result) != 2 {
t.Errorf("Result length for GetRange expected: %d, got: %d", 3, len(result))
return
}
if result[0] != "Hello" && result[1] != "Hello" {
t.Errorf("Hello not present in result.")
return
}
if result[0] != "World" && result[1] != "World" {
t.Errorf("World not present in result")
return
}
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("Negative Rank searching did not panic")
return
}
}()
s.GetRange(-1, 5)
}()
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("Invalid Range Rank searching did not panic")
return
}
}()
s.GetRange(5, 5)
}()
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("Invalid Range Rank searching did not panic")
return
}
}()
s.GetRange(5, 1)
}()
})
t.Run("SortedSetExists", func(t *testing.T) {
s := SortedSet{}
s.Init()
if s.Exists("Hello") {
t.Errorf("Returned True for non existant member")
return
}
s.Add("World", 5)
if !s.Exists("World") {
t.Errorf("Returned False for existing key")
return
}
})
t.Run("SortedSetGetRank", func(t *testing.T) {
s := SortedSet{}
s.Init()
if s.GetRank("Hello") != -1 {
t.Errorf("Returned rank for non existant member")
return
}
s.Add("World", 5)
if s.GetRank("World") != 5 {
t.Errorf("Returned Wrong rank for member")
return
}
})
}