From 8b8c1cd4c2a40a4a11dce3d072e3e53d8d89a1b1 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 27 Feb 2014 15:27:05 +0100 Subject: [PATCH] BITPOS fuzzy testing. --- tests/unit/bitops.tcl | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/unit/bitops.tcl b/tests/unit/bitops.tcl index a32ce26c..89631098 100644 --- a/tests/unit/bitops.tcl +++ b/tests/unit/bitops.tcl @@ -303,6 +303,39 @@ start_server {tags {"bitops"}} { test {BITPOS bit=0 changes behavior if end is given} { r set str "\xff\xff\xff" assert {[r bitpos str 0] == 24} + assert {[r bitpos str 0 0] == 24} assert {[r bitpos str 0 0 -1] == -1} } + + test {BITPOS bit=1 fuzzy testing using SETBIT} { + r del str + set max 524288; # 64k + set first_one_pos -1 + for {set j 0} {$j < 1000} {incr j} { + assert {[r bitpos str 1] == $first_one_pos} + set pos [randomInt $max] + r setbit str $pos 1 + if {$first_one_pos == -1 || $first_one_pos > $pos} { + # Update the position of the first 1 bit in the array + # if the bit we set is on the left of the previous one. + set first_one_pos $pos + } + } + } + + test {BITPOS bit=0 fuzzy testing using SETBIT} { + set max 524288; # 64k + set first_zero_pos $max + r set str [string repeat "\xff" [expr $max/8]] + for {set j 0} {$j < 1000} {incr j} { + assert {[r bitpos str 0] == $first_zero_pos} + set pos [randomInt $max] + r setbit str $pos 0 + if {$first_zero_pos > $pos} { + # Update the position of the first 0 bit in the array + # if the bit we clear is on the left of the previous one. + set first_zero_pos $pos + } + } + } }