]> git.saurik.com Git - redis.git/blob - tests/unit/sort.tcl
554477d1efe70bd515b035d75d0fa6a982cd4ddd
[redis.git] / tests / unit / sort.tcl
1 start_server {
2 tags {"sort"}
3 overrides {
4 "list-max-ziplist-value" 16
5 "list-max-ziplist-entries" 32
6 }
7 } {
8 proc create_random_dataset {num cmd} {
9 set tosort {}
10 set result {}
11 array set seenrand {}
12 r del tosort
13 for {set i 0} {$i < $num} {incr i} {
14 # Make sure all the weights are different because
15 # Redis does not use a stable sort but Tcl does.
16 while 1 {
17 randpath {
18 set rint [expr int(rand()*1000000)]
19 } {
20 set rint [expr rand()]
21 }
22 if {![info exists seenrand($rint)]} break
23 }
24 set seenrand($rint) x
25 r $cmd tosort $i
26 r set weight_$i $rint
27 r hset wobj_$i weight $rint
28 lappend tosort [list $i $rint]
29 }
30 set sorted [lsort -index 1 -real $tosort]
31 for {set i 0} {$i < $num} {incr i} {
32 lappend result [lindex $sorted $i 0]
33 }
34 set _ $result
35 }
36
37 set result [create_random_dataset 16 lpush]
38 test "SORT BY key" {
39 assert_equal $result [r sort tosort {BY weight_*}]
40 }
41
42 test "SORT BY hash field" {
43 assert_equal $result [r sort tosort {BY wobj_*->weight}]
44 }
45
46 test "SORT GET #" {
47 assert_equal [lsort -integer $result] [r sort tosort GET #]
48 }
49
50 test "SORT GET <const>" {
51 r del foo
52 set res [r sort tosort GET foo]
53 assert_equal 16 [llength $res]
54 foreach item $res { assert_equal {} $item }
55 }
56
57 test "SORT GET (key and hash) with sanity check" {
58 set l1 [r sort tosort GET # GET weight_*]
59 set l2 [r sort tosort GET # GET wobj_*->weight]
60 foreach {id1 w1} $l1 {id2 w2} $l2 {
61 assert_equal $id1 $id2
62 assert_equal $w1 [r get weight_$id1]
63 assert_equal $w2 [r get weight_$id1]
64 }
65 }
66
67 test "SORT BY key STORE" {
68 r sort tosort {BY weight_*} store sort-res
69 assert_equal $result [r lrange sort-res 0 -1]
70 assert_equal 16 [r llen sort-res]
71 assert_encoding ziplist sort-res
72 }
73
74 test "SORT BY hash field STORE" {
75 r sort tosort {BY wobj_*->weight} store sort-res
76 assert_equal $result [r lrange sort-res 0 -1]
77 assert_equal 16 [r llen sort-res]
78 assert_encoding ziplist sort-res
79 }
80
81 test "SORT DESC" {
82 assert_equal [lsort -decreasing -integer $result] [r sort tosort {DESC}]
83 }
84
85 test "SORT ALPHA against integer encoded strings" {
86 r del mylist
87 r lpush mylist 2
88 r lpush mylist 1
89 r lpush mylist 3
90 r lpush mylist 10
91 r sort mylist alpha
92 } {1 10 2 3}
93
94 test "SORT sorted set" {
95 r del zset
96 r zadd zset 1 a
97 r zadd zset 5 b
98 r zadd zset 2 c
99 r zadd zset 10 d
100 r zadd zset 3 e
101 r sort zset alpha desc
102 } {e d c b a}
103
104 test "SORT sorted set: +inf and -inf handling" {
105 r del zset
106 r zadd zset -100 a
107 r zadd zset 200 b
108 r zadd zset -300 c
109 r zadd zset 1000000 d
110 r zadd zset +inf max
111 r zadd zset -inf min
112 r zrange zset 0 -1
113 } {min c a b d max}
114
115 test "SORT regression for issue #19, sorting floats" {
116 r flushdb
117 set floats {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}
118 foreach x $floats {
119 r lpush mylist $x
120 }
121 assert_equal [lsort -real $floats] [r sort mylist]
122 }
123
124 tags {"slow"} {
125 set num 100
126 set res [create_random_dataset $num lpush]
127
128 test "SORT speed, $num element list BY key, 100 times" {
129 set start [clock clicks -milliseconds]
130 for {set i 0} {$i < 100} {incr i} {
131 set sorted [r sort tosort {BY weight_* LIMIT 0 10}]
132 }
133 set elapsed [expr [clock clicks -milliseconds]-$start]
134 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
135 flush stdout
136 }
137
138 test "SORT speed, $num element list BY hash field, 100 times" {
139 set start [clock clicks -milliseconds]
140 for {set i 0} {$i < 100} {incr i} {
141 set sorted [r sort tosort {BY wobj_*->weight LIMIT 0 10}]
142 }
143 set elapsed [expr [clock clicks -milliseconds]-$start]
144 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
145 flush stdout
146 }
147
148 test "SORT speed, $num element list directly, 100 times" {
149 set start [clock clicks -milliseconds]
150 for {set i 0} {$i < 100} {incr i} {
151 set sorted [r sort tosort {LIMIT 0 10}]
152 }
153 set elapsed [expr [clock clicks -milliseconds]-$start]
154 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
155 flush stdout
156 }
157
158 test "SORT speed, $num element list BY <const>, 100 times" {
159 set start [clock clicks -milliseconds]
160 for {set i 0} {$i < 100} {incr i} {
161 set sorted [r sort tosort {BY nokey LIMIT 0 10}]
162 }
163 set elapsed [expr [clock clicks -milliseconds]-$start]
164 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
165 flush stdout
166 }
167 }
168 }