-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_test.go
233 lines (211 loc) · 5.23 KB
/
list_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
package nodis
import (
"os"
"testing"
"time"
)
func TestList_LPush(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPush("list", []byte("value"))
n.LPush("list", []byte("value1"))
n.LPush("list", []byte("value2"))
n.LPush("list", []byte("value3"))
if string(n.LPop("list", 1)[0]) != "value3" {
t.Error("LPush failed")
}
if n.LLen("list") != 3 {
t.Error("LPush failed")
}
v := n.LRange("list", 0, -1)
if string(v[0]) != "value2" || string(v[1]) != "value1" || string(v[2]) != "value" {
t.Error("LPush failed")
}
}
func TestList_RPush(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.RPush("list", []byte("value"))
if string(n.RPop("list", 1)[0]) != "value" {
t.Error("RPush failed")
}
}
func TestList_LPop(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPush("list", []byte("value"))
if string(n.LPop("list", 1)[0]) != "value" {
t.Error("LPop failed")
}
}
func TestList_RPop(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.RPush("list", []byte("value"))
if string(n.RPop("list", 1)[0]) != "value" {
t.Error("RPop failed")
}
}
func TestList_LLen(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPush("list", []byte("value"))
if n.LLen("list") != 1 {
t.Error("LLen failed")
}
}
func TestList_LRange(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPush("list", []byte("value"))
if string(n.LRange("list", 0, -1)[0]) != "value" {
t.Error("LRange failed")
}
}
func TestList_LIndex(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPush("list", []byte("value"))
if string(n.LIndex("list", 0)) != "value" {
t.Error("LIndex failed")
}
}
func TestList_BLPopNoPush(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
k, _ := n.BLPop(time.Second, "list")
if k != "" {
t.Error("BLPop failed excepted value got nil")
}
}
func TestList_BLPop(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
go func() {
time.Sleep(time.Second)
n.LPush("list", []byte("value"))
}()
_, v := n.BLPop(time.Second*2, "list")
if string(v) != "value" {
t.Error("BLPop failed excepted value got nil")
}
}
func TestList_BRPopNoPush(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
k, _ := n.BRPop(time.Second, "list")
if k != "" {
t.Error("BRPop failed")
}
}
func TestList_BRPop(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
go func() {
n.RPush("list", []byte("value"))
}()
_, v := n.BRPop(time.Second, "list")
if string(v) != "value" {
t.Error("BRPop failed")
}
}
func TestList_LSet(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPush("list", []byte("value"))
n.LSet("list", 0, []byte("new"))
if string(n.LIndex("list", 0)) != "new" {
t.Error("LSet failed")
}
}
func TestList_LTrim(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPush("list", []byte("value"))
n.LTrim("list", 0, 0)
if string(n.LIndex("list", 0)) != "value" {
t.Error("LTrim failed")
}
}
func TestList_LRem(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPush("list", []byte("value"))
n.LRem("list", []byte("value"), 0)
if n.LLen("list") != 0 {
t.Error("LRem failed")
}
}
func TestList_LInsert(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPush("list", []byte("value"))
n.LInsert("list", []byte("value"), []byte("new"), true)
if string(n.LIndex("list", 0)) != "new" {
t.Error("LInsert failed")
}
}
func TestList_LPushX(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPushX("list", []byte("value"))
v := n.LPop("list", 1)
if len(v) > 0 && string(v[0]) == "value" {
t.Error("LPushX failed excepted value nil")
}
}
func TestList_RPushX(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.RPushX("list", []byte("value"))
v := n.RPop("list", 1)
if len(v) > 0 && string(v[0]) == "value" {
t.Error("RPushX failed excepted value nil")
}
}
func TestList_LPopRPush(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPush("list", []byte("value"))
n.LPush("list2", []byte("value2"))
if string(n.LPopRPush("list", "list2")) != "value" {
t.Error("LPopRPush failed")
}
if n.LLen("list2") != 2 {
t.Error("LPopRPush failed expected list length 2 got 1")
}
}
func TestList_RPopLPush(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.RPush("list", []byte("value"))
n.RPush("list2", []byte("value"))
if string(n.RPopLPush("list", "list2")) != "value" {
t.Error("RPopLPush failed")
}
if n.LLen("list2") != 2 {
t.Error("LPopRPush failed expected list length 2 got 1")
}
}
func TestList_LPopRPush2(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.LPush("list", []byte("value"))
if string(n.LPopRPush("list", "list2")) != "value" {
t.Error("LPopRPush failed")
}
if n.LLen("list2") != 1 {
t.Error("LPopRPush failed expected list length 1")
}
}
func TestList_RPopLPush2(t *testing.T) {
_ = os.RemoveAll("testdata")
n := Open(&Options{})
n.RPush("list", []byte("value"))
if string(n.RPopLPush("list", "list2")) != "value" {
t.Error("RPopLPush failed")
}
if n.LLen("list2") != 1 {
t.Error("LPopRPush failed expected list length 1")
}
}