Skip to content

Commit

Permalink
fix:bug in bitpos function for the clear bit mode
Browse files Browse the repository at this point in the history
- fakeredis returns the number of bits instead of -1 if it doesn't find 0.
- It returns 0 if the key doesn't exist.
  • Loading branch information
Diskein committed Oct 9, 2024
1 parent 7d44d1c commit 3cdcac6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
8 changes: 8 additions & 0 deletions fakeredis/_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,14 @@ def delete_keys(*keys: CommandItem) -> int:
ans += 1
return ans

def positive_range(start: int, end: int, length: int) -> Tuple[int, int]:
# Redis handles negative slightly differently for zrange
if start < 0:
start = max(0, start + length)
if end < 0:
end += length
end = min(end, length - 1)
return start, end + 1

def fix_range(start: int, end: int, length: int) -> Tuple[int, int]:
# Redis handles negative slightly differently for zrange
Expand Down
24 changes: 20 additions & 4 deletions fakeredis/commands_mixins/bitmap_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
BitValue,
fix_range_string,
fix_range,
positive_range,
CommandItem,
)
from fakeredis._helpers import SimpleError, casematch
Expand Down Expand Up @@ -54,23 +55,38 @@ def bitpos(self, key: CommandItem, bit: int, *args: bytes) -> int:
bit_mode = casematch(args[2], b"bit")
if not bit_mode and not casematch(args[2], b"byte"):
raise SimpleError(msgs.SYNTAX_ERROR_MSG)

# Redis treats non-existent key as an infinite array of 0 bits.
# If the user is looking for the first clear bit return 0,
# If the user is looking for the first set bit, return -1.
if not key.value:
return -1 if bit == 1 else 0

start = 0 if len(args) == 0 else Int.decode(args[0])
bit_chr = str(bit)
key_value = key.value if key.value else b""

if bit_mode:
value = self._bytes_as_bin_string(key_value)
end = len(value) if len(args) <= 1 else Int.decode(args[1])
start, end = fix_range(start, end, len(value))
value = value[start:end]
length = len(value)
else:
end = len(key_value) if len(args) <= 1 else Int.decode(args[1])
start, end = fix_range(start, end, len(key_value))
value = self._bytes_as_bin_string(key_value[start:end])
length = len(key_value)

start, end = positive_range(start, end, length)

if start > end or start >= length:
return -1

value = value[start:end] if bit_mode else self._bytes_as_bin_string(key_value[start:end])
result = value.find(bit_chr)
if result != -1:
result += start if bit_mode else (start * 8)
# Redis treats the value as padded with zero bytes to an infinity
# if the user is looking for the first clear bit and no end is set.
elif bit == 0 and len(args) <= 1:
result = len(key_value) * 8
return result

@command(name="BITCOUNT", fixed=(Key(bytes),), repeat=(bytes,), flags=msgs.FLAG_DO_NOT_CREATE)
Expand Down
13 changes: 13 additions & 0 deletions test/test_mixins/test_bitmap_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,20 @@ def test_bitpos(r: redis.Redis):
assert r.bitpos(key, 1, 1) == 8
r.set(key, b"\x00\x00\x00")
assert r.bitpos(key, 1) == -1
r.set(key, b"\xff\xff\xff")
assert r.bitpos(key, 1) == 0
assert r.bitpos(key, 1, 1) == 8
assert r.bitpos(key, 1, 3) == -1
assert r.bitpos(key, 0) == 24
assert r.bitpos(key, 0, 1) == 24
assert r.bitpos(key, 0, 3) == -1
assert r.bitpos(key, 0, 0, -1) == -1
r.set(key, b"\xff\xf0\x00")
assert r.bitpos("nokey:bitpos", 0) == 0
assert r.bitpos("nokey:bitpos", 1) == -1
assert r.bitpos("nokey:bitpos", 0, 1) == 0
assert r.bitpos("nokey:bitpos", 1, 1) == -1



@pytest.mark.min_server("7")
Expand Down

0 comments on commit 3cdcac6

Please sign in to comment.