4 "list-max-ziplist-value" 16
5 "list-max-ziplist-entries" 32
6 "set-max-intset-entries" 32
9 proc create_random_dataset
{num cmd
} {
14 for {set i
0} {$i < $num} {incr i
} {
15 # Make sure all the weights are different because
16 # Redis does not use a stable sort but Tcl does.
19 set rint
[expr int
(rand
()*1000000)]
21 set rint
[expr rand
()]
23 if {![info exists seenrand
($rint)]} break
28 r hset wobj_
$i weight
$rint
29 lappend tosort
[list $i $rint]
31 set sorted
[lsort -index 1 -real $tosort]
32 for {set i
0} {$i < $num} {incr i
} {
33 lappend result
[lindex $sorted $i 0]
38 foreach {num cmd enc title
} {
39 16 lpush ziplist
"Ziplist"
40 1000 lpush linkedlist
"Linked list"
41 10000 lpush linkedlist
"Big Linked list"
42 16 sadd intset
"Intset"
43 1000 sadd hashtable
"Hash table"
44 10000 sadd hashtable
"Big Hash table"
46 set result
[create_random_dataset
$num $cmd]
47 assert_encoding
$enc tosort
49 test
"$title: SORT BY key" {
50 assert_equal
$result [r sort tosort BY weight_
*]
53 test
"$title: SORT BY key with limit" {
54 assert_equal
[lrange $result 5 9] [r sort tosort BY weight_
* LIMIT
5 5]
57 test
"$title: SORT BY hash field" {
58 assert_equal
$result [r sort tosort BY wobj_
*->weight
]
62 set result
[create_random_dataset
16 lpush
]
64 assert_equal
[lsort -integer $result] [r sort tosort GET
#]
67 test
"SORT GET <const>" {
69 set res
[r sort tosort GET foo
]
70 assert_equal
16 [llength $res]
71 foreach item
$res { assert_equal
{} $item }
74 test
"SORT GET (key and hash) with sanity check" {
75 set l1
[r sort tosort GET
# GET weight_*]
76 set l2
[r sort tosort GET
# GET wobj_*->weight]
77 foreach {id1 w1
} $l1 {id2 w2
} $l2 {
78 assert_equal
$id1 $id2
79 assert_equal
$w1 [r get weight_
$id1]
80 assert_equal
$w2 [r get weight_
$id1]
84 test
"SORT BY key STORE" {
85 r sort tosort BY weight_
* store sort-res
86 assert_equal
$result [r
lrange sort-res
0 -1]
87 assert_equal
16 [r llen sort-res
]
88 assert_encoding ziplist sort-res
91 test
"SORT BY hash field STORE" {
92 r sort tosort BY wobj_
*->weight store sort-res
93 assert_equal
$result [r
lrange sort-res
0 -1]
94 assert_equal
16 [r llen sort-res
]
95 assert_encoding ziplist sort-res
99 assert_equal
[lsort -decreasing -integer $result] [r sort tosort DESC
]
102 test
"SORT ALPHA against integer encoded strings" {
111 test
"SORT sorted set" {
118 r sort zset alpha desc
121 test
"SORT sorted set BY nosort should retain ordering" {
129 r sort zset by nosort asc
130 r sort zset by nosort desc
132 } {{a c e b d
} {d b e c a
}}
134 test
"SORT sorted set BY nosort + LIMIT" {
141 assert_equal
[r sort zset by nosort asc limit
0 1] {a
}
142 assert_equal
[r sort zset by nosort desc limit
0 1] {d
}
143 assert_equal
[r sort zset by nosort asc limit
0 2] {a c
}
144 assert_equal
[r sort zset by nosort desc limit
0 2] {d b
}
145 assert_equal
[r sort zset by nosort limit
5 10] {}
146 assert_equal
[r sort zset by nosort limit
-10 100] {a c e b d
}
149 test
"SORT sorted set BY nosort works as expected from scripts" {
157 return {redis.call
('sort'
,'zset'
,'by'
,'nosort'
,'asc'
),
158 redis.call
('sort'
,'zset'
,'by'
,'nosort'
,'desc'
)}
160 } {{a c e b d
} {d b e c a
}}
162 test
"SORT sorted set: +inf and -inf handling" {
167 r zadd zset
1000000 d
173 test
"SORT regression for issue #19, sorting floats" {
175 set floats
{1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}
179 assert_equal
[lsort -real $floats] [r sort mylist
]
182 test
"SORT with STORE returns zero if result is empty (github isse 224)" {
187 test
"SORT with STORE does not create empty lists (github issue 224)" {
190 r sort foo alpha limit
10 10 store zap
194 test
"SORT with STORE removes key if result is empty (github issue 227)" {
197 r sort emptylist store foo
201 test
"SORT with BY <constant> and STORE should still order output" {
203 r sadd myset a b c d e f g h i l m n o p q r s t u v z aa aaa azz
204 r sort myset alpha by _ store mylist
206 } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z
}
208 test
"SORT will complain with numerical sorting and bad doubles (1)" {
210 r sadd myset
1 2 3 4 not-a-double
212 catch {r sort myset
} e
216 test
"SORT will complain with numerical sorting and bad doubles (2)" {
219 r mset score
:1 10 score
:2 20 score
:3 30 score
:4 not-a-double
221 catch {r sort myset by score
:*} e
225 test
"SORT BY sub-sorts lexicographically if score is the same" {
227 r sadd myset a b c d e f g h i l m n o p q r s t u v z aa aaa azz
228 foreach ele
{a aa aaa azz b c d e f g h i l m n o p q r s t u v z
} {
231 r sort myset by score
:*
232 } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z
}
234 test
"SORT GET with pattern ending with just -> does not get hash field" {
238 r sort mylist by num get x
:*->
243 set res
[create_random_dataset
$num lpush
]
245 test
"SORT speed, $num element list BY key, 100 times" {
246 set start
[clock clicks
-milliseconds]
247 for {set i
0} {$i < 100} {incr i
} {
248 set sorted
[r sort tosort BY weight_
* LIMIT
0 10]
250 set elapsed
[expr [clock clicks
-milliseconds]-$start]
252 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
257 test
"SORT speed, $num element list BY hash field, 100 times" {
258 set start
[clock clicks
-milliseconds]
259 for {set i
0} {$i < 100} {incr i
} {
260 set sorted
[r sort tosort BY wobj_
*->weight LIMIT
0 10]
262 set elapsed
[expr [clock clicks
-milliseconds]-$start]
264 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
269 test
"SORT speed, $num element list directly, 100 times" {
270 set start
[clock clicks
-milliseconds]
271 for {set i
0} {$i < 100} {incr i
} {
272 set sorted
[r sort tosort LIMIT
0 10]
274 set elapsed
[expr [clock clicks
-milliseconds]-$start]
276 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
281 test
"SORT speed, $num element list BY <const>, 100 times" {
282 set start
[clock clicks
-milliseconds]
283 for {set i
0} {$i < 100} {incr i
} {
284 set sorted
[r sort tosort BY nokey LIMIT
0 10]
286 set elapsed
[expr [clock clicks
-milliseconds]-$start]
288 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "