Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d67247b

Browse files
committedJul 30, 2018
Fix bug with range methods
1 parent b32e95c commit d67247b

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed
 

‎pyroaring/abstract_bitmap.pxi

+4
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ cdef class AbstractBitMap:
148148
>>> bm.contains_range(8, 12)
149149
False
150150
"""
151+
if range_end <= range_start or range_end == 0 or range_start >= 2**32:
152+
return True # empty range
153+
if range_end >= 2**32:
154+
range_end = 2**32
151155
return croaring.roaring_bitmap_contains_range(self._c_bitmap, range_start, range_end)
152156

153157
cdef compute_hash(self):

‎pyroaring/bitmap.pxi

+8
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ cdef class BitMap(AbstractBitMap):
138138
>>> bm
139139
BitMap([5, 6, 7, 8])
140140
"""
141+
if range_end <= range_start or range_end == 0 or range_start >= 2**32:
142+
return
143+
if range_end >= 2**32:
144+
range_end = 2**32
141145
croaring.roaring_bitmap_add_range(self._c_bitmap, range_start, range_end)
142146

143147
def remove_range(self, uint64_t range_start, uint64_t range_end):
@@ -149,4 +153,8 @@ cdef class BitMap(AbstractBitMap):
149153
>>> bm
150154
BitMap([5, 9, 10])
151155
"""
156+
if range_end <= range_start or range_end == 0 or range_start >= 2**32:
157+
return
158+
if range_end >= 2**32:
159+
range_end = 2**32
152160
croaring.roaring_bitmap_remove_range(self._c_bitmap, range_start, range_end)

‎test.py

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
uint18 = st.integers(min_value=0, max_value=2**18)
3535
uint32 = st.integers(min_value=0, max_value=2**32-1)
36+
uint64 = st.integers(min_value=0, max_value=2**64-1)
3637
integer = st.integers(min_value=0, max_value=2**31-1)
3738

3839
range_max_size = 2**18
@@ -448,6 +449,17 @@ def test_add_remove_range(self, values, cow, start, end):
448449
self.assertFalse(bm.contains_range(start, end))
449450
self.assertEqual(bm.intersection_cardinality(BitMap(range(start, end), copy_on_write=cow)), 0)
450451

452+
@given(hyp_collection, st.booleans(), uint64, uint64)
453+
def test_large_values(self, values, cow, start, end):
454+
st.assume(start >= 2**32 and end >= 2**32)
455+
bm = BitMap(values, copy_on_write=cow)
456+
original = BitMap(bm)
457+
bm.add_range(start, end)
458+
self.assertEqual(bm, original)
459+
bm.remove_range(start, end)
460+
self.assertEqual(bm, original)
461+
self.assertTrue(bm.contains_range(start, end))
462+
451463

452464
class CardinalityTest(Util):
453465

0 commit comments

Comments
 (0)
Please sign in to comment.