]> git.saurik.com Git - redis.git/blame - tests/unit/sort.tcl
change ltrim tests to cover all min/max cases and add stronger stresser
[redis.git] / tests / unit / sort.tcl
CommitLineData
7f7499ee 1start_server {tags {"sort"}} {
98578b57
PN
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
7f7499ee 11 tags {"slow"} {
7a6ae0a2 12 set res {}
7f7499ee
PN
13 test {Create a random list and a random set} {
14 set tosort {}
15 array set seenrand {}
16 for {set i 0} {$i < 10000} {incr i} {
17 while 1 {
18 # Make sure all the weights are different because
19 # Redis does not use a stable sort but Tcl does.
20 randpath {
21 set rint [expr int(rand()*1000000)]
22 } {
23 set rint [expr rand()]
24 }
25 if {![info exists seenrand($rint)]} break
98578b57 26 }
7f7499ee
PN
27 set seenrand($rint) x
28 r lpush tosort $i
29 r sadd tosort-set $i
30 r set weight_$i $rint
31 r hset wobj_$i weight $rint
32 lappend tosort [list $i $rint]
98578b57 33 }
7f7499ee 34 set sorted [lsort -index 1 -real $tosort]
7f7499ee
PN
35 for {set i 0} {$i < 10000} {incr i} {
36 lappend res [lindex $sorted $i 0]
37 }
38 format {}
39 } {}
98578b57 40
7f7499ee
PN
41 test {SORT with BY against the newly created list} {
42 r sort tosort {BY weight_*}
43 } $res
98578b57 44
7f7499ee
PN
45 test {SORT with BY (hash field) against the newly created list} {
46 r sort tosort {BY wobj_*->weight}
47 } $res
98578b57 48
7f7499ee
PN
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 }
98578b57 63 }
7f7499ee
PN
64 set _ $err
65 } {}
98578b57 66
7f7499ee
PN
67 test {SORT with BY, but against the newly created set} {
68 r sort tosort-set {BY weight_*}
69 } $res
98578b57 70
7f7499ee
PN
71 test {SORT with BY (hash field), but against the newly created set} {
72 r sort tosort-set {BY wobj_*->weight}
73 } $res
98578b57 74
7f7499ee
PN
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
98578b57 79
7f7499ee
PN
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
98578b57 84
7f7499ee
PN
85 test {SORT direct, numeric, against the newly created list} {
86 r sort tosort
87 } [lsort -integer $res]
98578b57 88
7f7499ee
PN
89 test {SORT decreasing sort} {
90 r sort tosort {DESC}
91 } [lsort -decreasing -integer $res]
98578b57 92
7f7499ee
PN
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 } {}
98578b57 103
7f7499ee
PN
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 } {}
98578b57 114
7f7499ee
PN
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 } {}
98578b57 125
7f7499ee
PN
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 }
98578b57
PN
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}