Skip to content

Commit

Permalink
add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust committed Jan 4, 2024
1 parent 1ffdb86 commit 581d8f3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion probables/quotientfilter/quotientfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class QuotientFilter:
def __init__(self, quotient: int = 20, hash_function: HashFuncT = None): # needs to be parameterized
if quotient < 3 or quotient > 31:
raise ValueError(
f"Invalid q setting for Quotient filter; q must be between 3 and 31; {quotient} was provided"
f"Quotient filter: Invalid quotient setting; quotient must be between 3 and 31; {quotient} was provided"
)
self._q = quotient
self._r = 32 - quotient
Expand Down
32 changes: 32 additions & 0 deletions tests/quotientfilter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def test_qf_init(self):
self.assertEqual(qf.elements_added, 0)
self.assertEqual(qf.num_elements, 256) # 2**qf.quotient

qf = QuotientFilter(quotient=24)

self.assertEqual(qf.bits_per_elm, 8)
self.assertEqual(qf.quotient, 24)
self.assertEqual(qf.remainder, 8)
self.assertEqual(qf.elements_added, 0)
self.assertEqual(qf.num_elements, 16777216) # 2**qf.quotient

def test_qf_add_check(self):
"test that the qf is able to add and check elements"
qf = QuotientFilter(quotient=8)
Expand All @@ -59,3 +67,27 @@ def test_qf_add_check(self):
self.assertFalse(qf.contains(str(i)))

self.assertEqual(qf.elements_added, 100)

def test_qf_add_check_in(self):
"test that the qf is able to add and check elements using `in`"
qf = QuotientFilter(quotient=8)

for i in range(0, 200, 2):
qf.add(str(i))
self.assertEqual(qf.elements_added, 100)

found_no = False
for i in range(0, 200, 2):
if str(i) not in qf:
found_no = True
self.assertFalse(found_no)

for i in range(1, 200, 2):
print(i)
self.assertFalse(str(i) in qf)

self.assertEqual(qf.elements_added, 100)

def test_qf_errors(self):
self.assertRaises(ValueError, lambda: QuotientFilter(quotient=2))
self.assertRaises(ValueError, lambda: QuotientFilter(quotient=32))

0 comments on commit 581d8f3

Please sign in to comment.