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