set out {}
for {set x 0} {$x < $maxlen} {incr x} {
set bit [string range $b(0) $x $x]
+ if {$op eq {not}} {set bit [expr {!$bit}]}
for {set j 1} {$j < $count} {incr j} {
set bit2 [string range $b($j) $x $x]
switch $op {
and {set bit [expr {$bit & $bit2}]}
or {set bit [expr {$bit | $bit2}]}
xor {set bit [expr {$bit ^ $bit2}]}
- not {set bit [expr {!$bit}]}
}
}
append out $bit
} 0
catch {unset num}
- foreach vec [list \
- "" "\xaa" "\x00\x00\xff" "foobar" \
- [randstring 2000 3000] [randstring 2000 3000] \
- [randstring 2000 3000] \
- ] {
+ foreach vec [list "" "\xaa" "\x00\x00\xff" "foobar" "123"] {
incr num
test "BITCOUNT against test vector #$num" {
r set str $vec
}
}
+ test {BITCOUNT fuzzing} {
+ for {set j 0} {$j < 100} {incr j} {
+ set str [randstring 0 3000]
+ r set str $str
+ assert {[r bitcount str] == [count_bits $str]}
+ }
+ }
+
test {BITCOUNT with start, end} {
r set s "foobar"
assert_equal [r bitcount s 0 -1] [count_bits "foobar"]
set e
} {ERR*syntax*}
+ test {BITCOUNT regression test for github issue #582} {
+ r del str
+ r setbit foo 0 1
+ if {[catch {r bitcount foo 0 4294967296} e]} {
+ assert_match {*ERR*out of range*} $e
+ set _ 1
+ } else {
+ set e
+ }
+ } {1}
+
test {BITOP NOT (empty string)} {
r set s ""
r bitop not dest s
foreach op {and or xor} {
test "BITOP $op fuzzing" {
- set vec {}
- set veckeys {}
- set numvec [expr {[randomInt 10]+1}]
- for {set j 0} {$j < $numvec} {incr j} {
- set str [randstring 0 1000]
- lappend vec $str
- lappend veckeys vector_$j
- r set vector_$j $str
+ for {set i 0} {$i < 10} {incr i} {
+ r flushall
+ set vec {}
+ set veckeys {}
+ set numvec [expr {[randomInt 10]+1}]
+ for {set j 0} {$j < $numvec} {incr j} {
+ set str [randstring 0 1000]
+ lappend vec $str
+ lappend veckeys vector_$j
+ r set vector_$j $str
+ }
+ r bitop $op target {*}$veckeys
+ assert_equal [r get target] [simulate_bit_op $op {*}$vec]
}
- r bitop $op target {*}$veckeys
- assert_equal [r get target] [simulate_bit_op $op {*}$vec]
}
}
+
+ test {BITOP NOT fuzzing} {
+ for {set i 0} {$i < 10} {incr i} {
+ r flushall
+ set str [randstring 0 1000]
+ r set str $str
+ r bitop not target str
+ assert_equal [r get target] [simulate_bit_op not $str]
+ }
+ }
+
+ test {BITOP with integer encoded source objects} {
+ r set a 1
+ r set b 2
+ r bitop xor dest a b a
+ r get dest
+ } {2}
+
+ test {BITOP with non string source key} {
+ r del c
+ r set a 1
+ r set b 2
+ r lpush c foo
+ catch {r bitop xor dest a b c d} e
+ set e
+ } {WRONGTYPE*}
+
+ test {BITOP with empty string after non empty string (issue #529)} {
+ r flushdb
+ 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"
+ r bitop or x a b
+ } {32}
}