Skip to content

Commit

Permalink
bitarray: additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust committed Jan 1, 2024
1 parent 92ad74d commit a72a8a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions probables/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class Bitarray:
"""Simplified, pure python bitarray implementation using as little memory as possible"""

def __init__(self, size: int):
if not isinstance(size, int):
raise TypeError(f"Bitarray size must be an int; {type(size)} was provided")
if size <= 0:
raise ValueError(f"Bitarray size must be larger than 1; {size} was provided")
self._size_bytes = math.ceil(size / 8)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ def test_resolve_path(self):
def test_bitarray(self):
"""test bit array basic operations"""
ba = Bitarray(100)

self.assertEqual(ba.size, 100)
self.assertEqual(ba.size_bytes, 13)
for i in range(ba.size_bytes):
self.assertEqual(0, ba.bitarray[i])

# test setting bits
for i in range(33):
ba.set_bit(i * 3)

Expand All @@ -124,6 +131,7 @@ def test_bitarray(self):
self.assertEqual(ba[0], 1)
self.assertEqual(ba[1], 0)

# test clearing bits
for i in range(33):
ba.clear_bit(i * 3)

Expand Down Expand Up @@ -153,6 +161,8 @@ def test_bitarray(self):

def test_bitarray_invalid_idx(self):
"""use an invalid type in a jaccard index"""
self.assertRaises(TypeError, lambda: Bitarray("100"))
self.assertRaises(ValueError, lambda: Bitarray(-100))
ba = Bitarray(10)
self.assertRaises(IndexError, lambda: ba.set_bit(12))
self.assertRaises(IndexError, lambda: ba.set_bit(-1))
Expand Down

0 comments on commit a72a8a8

Please sign in to comment.