github.com/hdt3213/godis@v1.2.9/database/sortedset_test.go (about) 1 package database 2 3 import ( 4 "github.com/hdt3213/godis/lib/utils" 5 "github.com/hdt3213/godis/redis/protocol/asserts" 6 "math/rand" 7 "strconv" 8 "testing" 9 ) 10 11 func TestZAdd(t *testing.T) { 12 testDB.Flush() 13 size := 100 14 15 // add new members 16 key := utils.RandString(10) 17 members := make([]string, size) 18 scores := make([]float64, size) 19 setArgs := []string{key} 20 for i := 0; i < size; i++ { 21 members[i] = utils.RandString(10) 22 scores[i] = rand.Float64() 23 setArgs = append(setArgs, strconv.FormatFloat(scores[i], 'f', -1, 64), members[i]) 24 } 25 result := testDB.Exec(nil, utils.ToCmdLine2("zadd", setArgs...)) 26 asserts.AssertIntReply(t, result, size) 27 28 // test zscore and zrank 29 for i, member := range members { 30 result = testDB.Exec(nil, utils.ToCmdLine("ZScore", key, member)) 31 score := strconv.FormatFloat(scores[i], 'f', -1, 64) 32 asserts.AssertBulkReply(t, result, score) 33 } 34 35 // test zcard 36 result = testDB.Exec(nil, utils.ToCmdLine("zcard", key)) 37 asserts.AssertIntReply(t, result, size) 38 39 // update members 40 setArgs = []string{key} 41 for i := 0; i < size; i++ { 42 scores[i] = rand.Float64() + 100 43 setArgs = append(setArgs, strconv.FormatFloat(scores[i], 'f', -1, 64), members[i]) 44 } 45 result = testDB.Exec(nil, utils.ToCmdLine2("zadd", setArgs...)) 46 asserts.AssertIntReply(t, result, 0) // return number of new members 47 48 // test updated score 49 for i, member := range members { 50 result = testDB.Exec(nil, utils.ToCmdLine("zscore", key, member)) 51 score := strconv.FormatFloat(scores[i], 'f', -1, 64) 52 asserts.AssertBulkReply(t, result, score) 53 } 54 } 55 56 func TestZRank(t *testing.T) { 57 testDB.Flush() 58 size := 100 59 key := utils.RandString(10) 60 members := make([]string, size) 61 scores := make([]int, size) 62 setArgs := []string{key} 63 for i := 0; i < size; i++ { 64 members[i] = utils.RandString(10) 65 scores[i] = i 66 setArgs = append(setArgs, strconv.FormatInt(int64(scores[i]), 10), members[i]) 67 } 68 testDB.Exec(nil, utils.ToCmdLine2("zadd", setArgs...)) 69 70 // test zrank 71 for i, member := range members { 72 result := testDB.Exec(nil, utils.ToCmdLine("zrank", key, member)) 73 asserts.AssertIntReply(t, result, i) 74 result = testDB.Exec(nil, utils.ToCmdLine("ZRevRank", key, member)) 75 asserts.AssertIntReply(t, result, size-i-1) 76 } 77 } 78 79 func TestZRange(t *testing.T) { 80 // prepare 81 testDB.Flush() 82 size := 100 83 key := utils.RandString(10) 84 members := make([]string, size) 85 scores := make([]int, size) 86 setArgs := []string{key} 87 for i := 0; i < size; i++ { 88 members[i] = strconv.Itoa(i) //utils.RandString(10) 89 scores[i] = i 90 setArgs = append(setArgs, strconv.FormatInt(int64(scores[i]), 10), members[i]) 91 } 92 testDB.Exec(nil, utils.ToCmdLine2("zadd", setArgs...)) 93 reverseMembers := make([]string, size) 94 for i, v := range members { 95 reverseMembers[size-i-1] = v 96 } 97 98 start := "0" 99 end := "9" 100 result := testDB.Exec(nil, utils.ToCmdLine("ZRange", key, start, end)) 101 asserts.AssertMultiBulkReply(t, result, members[0:10]) 102 result = testDB.Exec(nil, utils.ToCmdLine("ZRange", key, start, end, "WITHSCORES")) 103 asserts.AssertMultiBulkReplySize(t, result, 20) 104 result = testDB.Exec(nil, utils.ToCmdLine("ZRevRange", key, start, end)) 105 asserts.AssertMultiBulkReply(t, result, reverseMembers[0:10]) 106 107 start = "0" 108 end = "200" 109 result = testDB.Exec(nil, utils.ToCmdLine("ZRange", key, start, end)) 110 asserts.AssertMultiBulkReply(t, result, members) 111 result = testDB.Exec(nil, utils.ToCmdLine("ZRevRange", key, start, end)) 112 asserts.AssertMultiBulkReply(t, result, reverseMembers) 113 114 start = "0" 115 end = "-10" 116 result = testDB.Exec(nil, utils.ToCmdLine("ZRange", key, start, end)) 117 asserts.AssertMultiBulkReply(t, result, members[0:size-10+1]) 118 result = testDB.Exec(nil, utils.ToCmdLine("ZRevRange", key, start, end)) 119 asserts.AssertMultiBulkReply(t, result, reverseMembers[0:size-10+1]) 120 121 start = "0" 122 end = "-200" 123 result = testDB.Exec(nil, utils.ToCmdLine("ZRange", key, start, end)) 124 asserts.AssertMultiBulkReply(t, result, members[0:0]) 125 result = testDB.Exec(nil, utils.ToCmdLine("ZRevRange", key, start, end)) 126 asserts.AssertMultiBulkReply(t, result, reverseMembers[0:0]) 127 128 start = "-10" 129 end = "-1" 130 result = testDB.Exec(nil, utils.ToCmdLine("ZRange", key, start, end)) 131 asserts.AssertMultiBulkReply(t, result, members[90:]) 132 result = testDB.Exec(nil, utils.ToCmdLine("ZRevRange", key, start, end)) 133 asserts.AssertMultiBulkReply(t, result, reverseMembers[90:]) 134 } 135 136 func reverse(src []string) []string { 137 result := make([]string, len(src)) 138 for i, v := range src { 139 result[len(src)-i-1] = v 140 } 141 return result 142 } 143 144 func TestZRangeByScore(t *testing.T) { 145 // prepare 146 testDB.Flush() 147 size := 100 148 key := utils.RandString(10) 149 members := make([]string, size) 150 scores := make([]int, size) 151 setArgs := []string{key} 152 for i := 0; i < size; i++ { 153 members[i] = strconv.FormatInt(int64(i), 10) 154 scores[i] = i 155 setArgs = append(setArgs, strconv.FormatInt(int64(scores[i]), 10), members[i]) 156 } 157 result := testDB.Exec(nil, utils.ToCmdLine2("zadd", setArgs...)) 158 asserts.AssertIntReply(t, result, size) 159 160 min := "20" 161 max := "30" 162 result = testDB.Exec(nil, utils.ToCmdLine("ZRangeByScore", key, min, max)) 163 asserts.AssertMultiBulkReply(t, result, members[20:31]) 164 result = testDB.Exec(nil, utils.ToCmdLine("ZRangeByScore", key, min, max, "WithScores")) 165 asserts.AssertMultiBulkReplySize(t, result, 22) 166 result = execZRevRangeByScore(testDB, utils.ToCmdLine(key, max, min)) 167 result = testDB.Exec(nil, utils.ToCmdLine("ZRevRangeByScore", key, max, min)) 168 asserts.AssertMultiBulkReply(t, result, reverse(members[20:31])) 169 170 min = "-10" 171 max = "10" 172 result = testDB.Exec(nil, utils.ToCmdLine("ZRangeByScore", key, min, max)) 173 asserts.AssertMultiBulkReply(t, result, members[0:11]) 174 result = testDB.Exec(nil, utils.ToCmdLine("ZRevRangeByScore", key, max, min)) 175 asserts.AssertMultiBulkReply(t, result, reverse(members[0:11])) 176 177 min = "90" 178 max = "110" 179 result = testDB.Exec(nil, utils.ToCmdLine("ZRangeByScore", key, min, max)) 180 asserts.AssertMultiBulkReply(t, result, members[90:]) 181 result = testDB.Exec(nil, utils.ToCmdLine("ZRevRangeByScore", key, max, min)) 182 asserts.AssertMultiBulkReply(t, result, reverse(members[90:])) 183 184 min = "(20" 185 max = "(30" 186 result = testDB.Exec(nil, utils.ToCmdLine("ZRangeByScore", key, min, max)) 187 asserts.AssertMultiBulkReply(t, result, members[21:30]) 188 result = testDB.Exec(nil, utils.ToCmdLine("ZRevRangeByScore", key, max, min)) 189 asserts.AssertMultiBulkReply(t, result, reverse(members[21:30])) 190 191 min = "20" 192 max = "40" 193 result = testDB.Exec(nil, utils.ToCmdLine("ZRangeByScore", key, min, max, "LIMIT", "5", "5")) 194 asserts.AssertMultiBulkReply(t, result, members[25:30]) 195 result = testDB.Exec(nil, utils.ToCmdLine("ZRevRangeByScore", key, max, min, "LIMIT", "5", "5")) 196 asserts.AssertMultiBulkReply(t, result, reverse(members[31:36])) 197 } 198 199 func TestZRem(t *testing.T) { 200 testDB.Flush() 201 size := 100 202 key := utils.RandString(10) 203 members := make([]string, size) 204 scores := make([]int, size) 205 setArgs := []string{key} 206 for i := 0; i < size; i++ { 207 members[i] = strconv.FormatInt(int64(i), 10) 208 scores[i] = i 209 setArgs = append(setArgs, strconv.FormatInt(int64(scores[i]), 10), members[i]) 210 } 211 testDB.Exec(nil, utils.ToCmdLine2("zadd", setArgs...)) 212 213 args := []string{key} 214 args = append(args, members[0:10]...) 215 result := testDB.Exec(nil, utils.ToCmdLine2("zrem", args...)) 216 asserts.AssertIntReply(t, result, 10) 217 result = testDB.Exec(nil, utils.ToCmdLine("zcard", key)) 218 asserts.AssertIntReply(t, result, size-10) 219 220 // test ZRemRangeByRank 221 testDB.Flush() 222 size = 100 223 key = utils.RandString(10) 224 members = make([]string, size) 225 scores = make([]int, size) 226 setArgs = []string{key} 227 for i := 0; i < size; i++ { 228 members[i] = strconv.FormatInt(int64(i), 10) 229 scores[i] = i 230 setArgs = append(setArgs, strconv.FormatInt(int64(scores[i]), 10), members[i]) 231 } 232 testDB.Exec(nil, utils.ToCmdLine2("zadd", setArgs...)) 233 234 result = testDB.Exec(nil, utils.ToCmdLine("ZRemRangeByRank", key, "0", "9")) 235 asserts.AssertIntReply(t, result, 10) 236 result = testDB.Exec(nil, utils.ToCmdLine("zcard", key)) 237 asserts.AssertIntReply(t, result, size-10) 238 239 // test ZRemRangeByScore 240 testDB.Flush() 241 size = 100 242 key = utils.RandString(10) 243 members = make([]string, size) 244 scores = make([]int, size) 245 setArgs = []string{key} 246 for i := 0; i < size; i++ { 247 members[i] = strconv.FormatInt(int64(i), 10) 248 scores[i] = i 249 setArgs = append(setArgs, strconv.FormatInt(int64(scores[i]), 10), members[i]) 250 } 251 testDB.Exec(nil, utils.ToCmdLine2("zadd", setArgs...)) 252 253 result = testDB.Exec(nil, utils.ToCmdLine("ZRemRangeByScore", key, "0", "9")) 254 asserts.AssertIntReply(t, result, 10) 255 result = testDB.Exec(nil, utils.ToCmdLine("zcard", key)) 256 asserts.AssertIntReply(t, result, size-10) 257 } 258 259 func TestZCount(t *testing.T) { 260 // prepare 261 testDB.Flush() 262 size := 100 263 key := utils.RandString(10) 264 members := make([]string, size) 265 scores := make([]int, size) 266 setArgs := []string{key} 267 for i := 0; i < size; i++ { 268 members[i] = strconv.FormatInt(int64(i), 10) 269 scores[i] = i 270 setArgs = append(setArgs, strconv.FormatInt(int64(scores[i]), 10), members[i]) 271 } 272 testDB.Exec(nil, utils.ToCmdLine2("zadd", setArgs...)) 273 274 min := "20" 275 max := "30" 276 result := testDB.Exec(nil, utils.ToCmdLine("zcount", key, min, max)) 277 asserts.AssertIntReply(t, result, 11) 278 279 min = "-10" 280 max = "10" 281 result = testDB.Exec(nil, utils.ToCmdLine("zcount", key, min, max)) 282 asserts.AssertIntReply(t, result, 11) 283 284 min = "90" 285 max = "110" 286 result = testDB.Exec(nil, utils.ToCmdLine("zcount", key, min, max)) 287 asserts.AssertIntReply(t, result, 10) 288 289 min = "(20" 290 max = "(30" 291 result = testDB.Exec(nil, utils.ToCmdLine("zcount", key, min, max)) 292 asserts.AssertIntReply(t, result, 9) 293 } 294 295 func TestZIncrBy(t *testing.T) { 296 testDB.Flush() 297 key := utils.RandString(10) 298 result := testDB.Exec(nil, utils.ToCmdLine("ZIncrBy", key, "10", "a")) 299 asserts.AssertBulkReply(t, result, "10") 300 result = testDB.Exec(nil, utils.ToCmdLine("ZIncrBy", key, "10", "a")) 301 asserts.AssertBulkReply(t, result, "20") 302 303 result = testDB.Exec(nil, utils.ToCmdLine("ZScore", key, "a")) 304 asserts.AssertBulkReply(t, result, "20") 305 } 306 307 func TestZPopMin(t *testing.T) { 308 testDB.Flush() 309 key := utils.RandString(10) 310 result := testDB.Exec(nil, utils.ToCmdLine("ZAdd", key, "1", "a", "1", "b", "2", "c")) 311 asserts.AssertNotError(t, result) 312 result = testDB.Exec(nil, utils.ToCmdLine("ZPopMin", key, "2")) 313 asserts.AssertMultiBulkReply(t, result, []string{"a", "1", "b", "1"}) 314 result = testDB.Exec(nil, utils.ToCmdLine("ZRange", key, "0", "-1")) 315 asserts.AssertMultiBulkReply(t, result, []string{"c"}) 316 317 result = testDB.Exec(nil, utils.ToCmdLine("ZPopMin", key+"1", "2")) 318 asserts.AssertMultiBulkReplySize(t, result, 0) 319 320 testDB.Exec(nil, utils.ToCmdLine("set", key+"2", "2")) 321 result = testDB.Exec(nil, utils.ToCmdLine("ZPopMin", key+"2", "2")) 322 asserts.AssertErrReply(t, result, "WRONGTYPE Operation against a key holding the wrong kind of value") 323 }