2
0

bitops.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. # Compare Redis commands against Tcl implementations of the same commands.
  2. proc count_bits s {
  3. binary scan $s b* bits
  4. string length [regsub -all {0} $bits {}]
  5. }
  6. proc simulate_bit_op {op args} {
  7. set maxlen 0
  8. set j 0
  9. set count [llength $args]
  10. foreach a $args {
  11. binary scan $a b* bits
  12. set b($j) $bits
  13. if {[string length $bits] > $maxlen} {
  14. set maxlen [string length $bits]
  15. }
  16. incr j
  17. }
  18. for {set j 0} {$j < $count} {incr j} {
  19. if {[string length $b($j)] < $maxlen} {
  20. append b($j) [string repeat 0 [expr $maxlen-[string length $b($j)]]]
  21. }
  22. }
  23. set out {}
  24. for {set x 0} {$x < $maxlen} {incr x} {
  25. set bit [string range $b(0) $x $x]
  26. if {$op eq {not}} {set bit [expr {!$bit}]}
  27. for {set j 1} {$j < $count} {incr j} {
  28. set bit2 [string range $b($j) $x $x]
  29. switch $op {
  30. and {set bit [expr {$bit & $bit2}]}
  31. or {set bit [expr {$bit | $bit2}]}
  32. xor {set bit [expr {$bit ^ $bit2}]}
  33. }
  34. }
  35. append out $bit
  36. }
  37. binary format b* $out
  38. }
  39. start_server {tags {"bitops"}} {
  40. test {BITCOUNT returns 0 against non existing key} {
  41. r bitcount no-key
  42. } 0
  43. test {BITCOUNT returns 0 with out of range indexes} {
  44. r set str "xxxx"
  45. r bitcount str 4 10
  46. } 0
  47. test {BITCOUNT returns 0 with negative indexes where start > end} {
  48. r set str "xxxx"
  49. r bitcount str -6 -7
  50. } 0
  51. catch {unset num}
  52. foreach vec [list "" "\xaa" "\x00\x00\xff" "foobar" "123"] {
  53. incr num
  54. test "BITCOUNT against test vector #$num" {
  55. r set str $vec
  56. assert {[r bitcount str] == [count_bits $vec]}
  57. }
  58. }
  59. test {BITCOUNT fuzzing without start/end} {
  60. for {set j 0} {$j < 100} {incr j} {
  61. set str [randstring 0 3000]
  62. r set str $str
  63. assert {[r bitcount str] == [count_bits $str]}
  64. }
  65. }
  66. test {BITCOUNT fuzzing with start/end} {
  67. for {set j 0} {$j < 100} {incr j} {
  68. set str [randstring 0 3000]
  69. r set str $str
  70. set l [string length $str]
  71. set start [randomInt $l]
  72. set end [randomInt $l]
  73. if {$start > $end} {
  74. lassign [list $end $start] start end
  75. }
  76. assert {[r bitcount str $start $end] == [count_bits [string range $str $start $end]]}
  77. }
  78. }
  79. test {BITCOUNT with start, end} {
  80. r set s "foobar"
  81. assert_equal [r bitcount s 0 -1] [count_bits "foobar"]
  82. assert_equal [r bitcount s 1 -2] [count_bits "ooba"]
  83. assert_equal [r bitcount s -2 1] [count_bits ""]
  84. assert_equal [r bitcount s 0 1000] [count_bits "foobar"]
  85. }
  86. test {BITCOUNT syntax error #1} {
  87. catch {r bitcount s 0} e
  88. set e
  89. } {ERR*syntax*}
  90. test {BITCOUNT regression test for github issue #582} {
  91. r del foo
  92. r setbit foo 0 1
  93. if {[catch {r bitcount foo 0 4294967296} e]} {
  94. assert_match {*ERR*out of range*} $e
  95. set _ 1
  96. } else {
  97. set e
  98. }
  99. } {1}
  100. test {BITCOUNT misaligned prefix} {
  101. r del str
  102. r set str ab
  103. r bitcount str 1 -1
  104. } {3}
  105. test {BITCOUNT misaligned prefix + full words + remainder} {
  106. r del str
  107. r set str __PPxxxxxxxxxxxxxxxxRR__
  108. r bitcount str 2 -3
  109. } {74}
  110. test {BITOP NOT (empty string)} {
  111. r set s ""
  112. r bitop not dest s
  113. r get dest
  114. } {}
  115. test {BITOP NOT (known string)} {
  116. r set s "\xaa\x00\xff\x55"
  117. r bitop not dest s
  118. r get dest
  119. } "\x55\xff\x00\xaa"
  120. test {BITOP where dest and target are the same key} {
  121. r set s "\xaa\x00\xff\x55"
  122. r bitop not s s
  123. r get s
  124. } "\x55\xff\x00\xaa"
  125. test {BITOP AND|OR|XOR don't change the string with single input key} {
  126. r set a "\x01\x02\xff"
  127. r bitop and res1 a
  128. r bitop or res2 a
  129. r bitop xor res3 a
  130. list [r get res1] [r get res2] [r get res3]
  131. } [list "\x01\x02\xff" "\x01\x02\xff" "\x01\x02\xff"]
  132. test {BITOP missing key is considered a stream of zero} {
  133. r set a "\x01\x02\xff"
  134. r bitop and res1 no-suck-key a
  135. r bitop or res2 no-suck-key a no-such-key
  136. r bitop xor res3 no-such-key a
  137. list [r get res1] [r get res2] [r get res3]
  138. } [list "\x00\x00\x00" "\x01\x02\xff" "\x01\x02\xff"]
  139. test {BITOP shorter keys are zero-padded to the key with max length} {
  140. r set a "\x01\x02\xff\xff"
  141. r set b "\x01\x02\xff"
  142. r bitop and res1 a b
  143. r bitop or res2 a b
  144. r bitop xor res3 a b
  145. list [r get res1] [r get res2] [r get res3]
  146. } [list "\x01\x02\xff\x00" "\x01\x02\xff\xff" "\x00\x00\x00\xff"]
  147. foreach op {and or xor} {
  148. test "BITOP $op fuzzing" {
  149. for {set i 0} {$i < 10} {incr i} {
  150. r flushall
  151. set vec {}
  152. set veckeys {}
  153. set numvec [expr {[randomInt 10]+1}]
  154. for {set j 0} {$j < $numvec} {incr j} {
  155. set str [randstring 0 1000]
  156. lappend vec $str
  157. lappend veckeys vector_$j
  158. r set vector_$j $str
  159. }
  160. r bitop $op target {*}$veckeys
  161. assert_equal [r get target] [simulate_bit_op $op {*}$vec]
  162. }
  163. }
  164. }
  165. test {BITOP NOT fuzzing} {
  166. for {set i 0} {$i < 10} {incr i} {
  167. r flushall
  168. set str [randstring 0 1000]
  169. r set str $str
  170. r bitop not target str
  171. assert_equal [r get target] [simulate_bit_op not $str]
  172. }
  173. }
  174. test {BITOP with integer encoded source objects} {
  175. r set a 1
  176. r set b 2
  177. r bitop xor dest a b a
  178. r get dest
  179. } {2}
  180. test {BITOP with non string source key} {
  181. r del c
  182. r set a 1
  183. r set b 2
  184. r lpush c foo
  185. catch {r bitop xor dest a b c d} e
  186. set e
  187. } {WRONGTYPE*}
  188. test {BITOP with empty string after non empty string (issue #529)} {
  189. r flushdb
  190. r set a "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  191. r bitop or x a b
  192. } {32}
  193. test {BITPOS bit=0 with empty key returns 0} {
  194. r del str
  195. r bitpos str 0
  196. } {0}
  197. test {BITPOS bit=1 with empty key returns -1} {
  198. r del str
  199. r bitpos str 1
  200. } {-1}
  201. test {BITPOS bit=0 with string less than 1 word works} {
  202. r set str "\xff\xf0\x00"
  203. r bitpos str 0
  204. } {12}
  205. test {BITPOS bit=1 with string less than 1 word works} {
  206. r set str "\x00\x0f\x00"
  207. r bitpos str 1
  208. } {12}
  209. test {BITPOS bit=0 starting at unaligned address} {
  210. r set str "\xff\xf0\x00"
  211. r bitpos str 0 1
  212. } {12}
  213. test {BITPOS bit=1 starting at unaligned address} {
  214. r set str "\x00\x0f\xff"
  215. r bitpos str 1 1
  216. } {12}
  217. test {BITPOS bit=0 unaligned+full word+reminder} {
  218. r del str
  219. r set str "\xff\xff\xff" ; # Prefix
  220. # Followed by two (or four in 32 bit systems) full words
  221. r append str "\xff\xff\xff\xff\xff\xff\xff\xff"
  222. r append str "\xff\xff\xff\xff\xff\xff\xff\xff"
  223. r append str "\xff\xff\xff\xff\xff\xff\xff\xff"
  224. # First zero bit.
  225. r append str "\x0f"
  226. assert {[r bitpos str 0] == 216}
  227. assert {[r bitpos str 0 1] == 216}
  228. assert {[r bitpos str 0 2] == 216}
  229. assert {[r bitpos str 0 3] == 216}
  230. assert {[r bitpos str 0 4] == 216}
  231. assert {[r bitpos str 0 5] == 216}
  232. assert {[r bitpos str 0 6] == 216}
  233. assert {[r bitpos str 0 7] == 216}
  234. assert {[r bitpos str 0 8] == 216}
  235. }
  236. test {BITPOS bit=1 unaligned+full word+reminder} {
  237. r del str
  238. r set str "\x00\x00\x00" ; # Prefix
  239. # Followed by two (or four in 32 bit systems) full words
  240. r append str "\x00\x00\x00\x00\x00\x00\x00\x00"
  241. r append str "\x00\x00\x00\x00\x00\x00\x00\x00"
  242. r append str "\x00\x00\x00\x00\x00\x00\x00\x00"
  243. # First zero bit.
  244. r append str "\xf0"
  245. assert {[r bitpos str 1] == 216}
  246. assert {[r bitpos str 1 1] == 216}
  247. assert {[r bitpos str 1 2] == 216}
  248. assert {[r bitpos str 1 3] == 216}
  249. assert {[r bitpos str 1 4] == 216}
  250. assert {[r bitpos str 1 5] == 216}
  251. assert {[r bitpos str 1 6] == 216}
  252. assert {[r bitpos str 1 7] == 216}
  253. assert {[r bitpos str 1 8] == 216}
  254. }
  255. test {BITPOS bit=1 returns -1 if string is all 0 bits} {
  256. r set str ""
  257. for {set j 0} {$j < 20} {incr j} {
  258. assert {[r bitpos str 1] == -1}
  259. r append str "\x00"
  260. }
  261. }
  262. test {BITPOS bit=0 works with intervals} {
  263. r set str "\x00\xff\x00"
  264. assert {[r bitpos str 0 0 -1] == 0}
  265. assert {[r bitpos str 0 1 -1] == 16}
  266. assert {[r bitpos str 0 2 -1] == 16}
  267. assert {[r bitpos str 0 2 200] == 16}
  268. assert {[r bitpos str 0 1 1] == -1}
  269. }
  270. test {BITPOS bit=1 works with intervals} {
  271. r set str "\x00\xff\x00"
  272. assert {[r bitpos str 1 0 -1] == 8}
  273. assert {[r bitpos str 1 1 -1] == 8}
  274. assert {[r bitpos str 1 2 -1] == -1}
  275. assert {[r bitpos str 1 2 200] == -1}
  276. assert {[r bitpos str 1 1 1] == 8}
  277. }
  278. test {BITPOS bit=0 changes behavior if end is given} {
  279. r set str "\xff\xff\xff"
  280. assert {[r bitpos str 0] == 24}
  281. assert {[r bitpos str 0 0] == 24}
  282. assert {[r bitpos str 0 0 -1] == -1}
  283. }
  284. test {BITPOS bit=1 fuzzy testing using SETBIT} {
  285. r del str
  286. set max 524288; # 64k
  287. set first_one_pos -1
  288. for {set j 0} {$j < 1000} {incr j} {
  289. assert {[r bitpos str 1] == $first_one_pos}
  290. set pos [randomInt $max]
  291. r setbit str $pos 1
  292. if {$first_one_pos == -1 || $first_one_pos > $pos} {
  293. # Update the position of the first 1 bit in the array
  294. # if the bit we set is on the left of the previous one.
  295. set first_one_pos $pos
  296. }
  297. }
  298. }
  299. test {BITPOS bit=0 fuzzy testing using SETBIT} {
  300. set max 524288; # 64k
  301. set first_zero_pos $max
  302. r set str [string repeat "\xff" [expr $max/8]]
  303. for {set j 0} {$j < 1000} {incr j} {
  304. assert {[r bitpos str 0] == $first_zero_pos}
  305. set pos [randomInt $max]
  306. r setbit str $pos 0
  307. if {$first_zero_pos > $pos} {
  308. # Update the position of the first 0 bit in the array
  309. # if the bit we clear is on the left of the previous one.
  310. set first_zero_pos $pos
  311. }
  312. }
  313. }
  314. }