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