]> git.saurik.com Git - redis.git/blob - tests/support/util.tcl
a084df90c2106c60a59763a69fece6349addae9b
[redis.git] / tests / support / util.tcl
1 proc randstring {min max {type binary}} {
2 set len [expr {$min+int(rand()*($max-$min+1))}]
3 set output {}
4 if {$type eq {binary}} {
5 set minval 0
6 set maxval 255
7 } elseif {$type eq {alpha}} {
8 set minval 48
9 set maxval 122
10 } elseif {$type eq {compr}} {
11 set minval 48
12 set maxval 52
13 }
14 while {$len} {
15 append output [format "%c" [expr {$minval+int(rand()*($maxval-$minval+1))}]]
16 incr len -1
17 }
18 return $output
19 }
20
21 # Useful for some test
22 proc zlistAlikeSort {a b} {
23 if {[lindex $a 0] > [lindex $b 0]} {return 1}
24 if {[lindex $a 0] < [lindex $b 0]} {return -1}
25 string compare [lindex $a 1] [lindex $b 1]
26 }
27
28 # Return value for INFO property
29 proc status {r property} {
30 if {[regexp "\r\n$property:(.*?)\r\n" [$r info] _ value]} {
31 set _ $value
32 }
33 }
34
35 proc waitForBgsave r {
36 while 1 {
37 if {[status r bgsave_in_progress] eq 1} {
38 puts -nonewline "\nWaiting for background save to finish... "
39 flush stdout
40 after 1000
41 } else {
42 break
43 }
44 }
45 }
46
47 proc waitForBgrewriteaof r {
48 while 1 {
49 if {[status r bgrewriteaof_in_progress] eq 1} {
50 puts -nonewline "\nWaiting for background AOF rewrite to finish... "
51 flush stdout
52 after 1000
53 } else {
54 break
55 }
56 }
57 }
58
59 proc randomInt {max} {
60 expr {int(rand()*$max)}
61 }
62
63 proc randpath args {
64 set path [expr {int(rand()*[llength $args])}]
65 uplevel 1 [lindex $args $path]
66 }
67
68 proc randomValue {} {
69 randpath {
70 # Small enough to likely collide
71 randomInt 1000
72 } {
73 # 32 bit compressible signed/unsigned
74 randpath {randomInt 2000000000} {randomInt 4000000000}
75 } {
76 # 64 bit
77 randpath {randomInt 1000000000000}
78 } {
79 # Random string
80 randpath {randstring 0 256 alpha} \
81 {randstring 0 256 compr} \
82 {randstring 0 256 binary}
83 }
84 }
85
86 proc randomKey {} {
87 randpath {
88 # Small enough to likely collide
89 randomInt 1000
90 } {
91 # 32 bit compressible signed/unsigned
92 randpath {randomInt 2000000000} {randomInt 4000000000}
93 } {
94 # 64 bit
95 randpath {randomInt 1000000000000}
96 } {
97 # Random string
98 randpath {randstring 1 256 alpha} \
99 {randstring 1 256 compr}
100 }
101 }
102
103 proc createComplexDataset {r ops} {
104 for {set j 0} {$j < $ops} {incr j} {
105 set k [randomKey]
106 set f [randomValue]
107 set v [randomValue]
108 randpath {
109 set d [expr {rand()}]
110 } {
111 set d [expr {rand()}]
112 } {
113 set d [expr {rand()}]
114 } {
115 set d [expr {rand()}]
116 } {
117 set d [expr {rand()}]
118 } {
119 randpath {set d +inf} {set d -inf}
120 }
121 set t [$r type $k]
122
123 if {$t eq {none}} {
124 randpath {
125 $r set $k $v
126 } {
127 $r lpush $k $v
128 } {
129 $r sadd $k $v
130 } {
131 $r zadd $k $d $v
132 } {
133 $r hset $k $f $v
134 }
135 set t [$r type $k]
136 }
137
138 switch $t {
139 {string} {
140 # Nothing to do
141 }
142 {list} {
143 randpath {$r lpush $k $v} \
144 {$r rpush $k $v} \
145 {$r lrem $k 0 $v} \
146 {$r rpop $k} \
147 {$r lpop $k}
148 }
149 {set} {
150 randpath {$r sadd $k $v} \
151 {$r srem $k $v}
152 }
153 {zset} {
154 randpath {$r zadd $k $d $v} \
155 {$r zrem $k $v}
156 }
157 {hash} {
158 randpath {$r hset $k $f $v} \
159 {$r hdel $k $f}
160 }
161 }
162 }
163 }