Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:bug in bitpos function for the clear bit mode #337

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions fakeredis/commands_mixins/bitmap_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init(self, *args: Any, **kwargs: Any) -> None:
def _bytes_as_bin_string(value: bytes) -> str:
return "".join([bin(i).lstrip("0b").rjust(8, "0") for i in value])

@command((Key(bytes), Int), (bytes,))
@command((Key(bytes), Int), (bytes,), flags=msgs.FLAG_DO_NOT_CREATE)
def bitpos(self, key: CommandItem, bit: int, *args: bytes) -> int:
if bit != 0 and bit != 1:
raise SimpleError(msgs.BIT_ARG_MUST_BE_ZERO_OR_ONE)
Expand All @@ -54,23 +54,27 @@ 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)
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]
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])
if key.value is None:
# The first clear bit is at 0, the first set bit is not found (-1).
return -1 if bit == 1 else 0

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

result = value.find(bit_chr)
result = source_value.find(str(bit))
if result != -1:
result += start if bit_mode else (start * 8)
elif bit == 0 and len(args) <= 1:
# 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.
result = len(key.value) * 8
return result

@command(name="BITCOUNT", fixed=(Key(bytes),), repeat=(bytes,), flags=msgs.FLAG_DO_NOT_CREATE)
Expand Down
12 changes: 12 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,19 @@ 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
Loading