Skip to content

Commit

Permalink
Fixed a few things regarding the coverage of certain methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nbro committed Feb 14, 2017
1 parent 77d97b4 commit 1e9214f
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 66 deletions.
14 changes: 11 additions & 3 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@

[report]

omit =
omit =
*/python?.?/*
*/site-packages/*
*__init__*

exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover

@abstractmethod

raise NotImplementedError
def __repr__ # exclude __repr__ methods
def __str__ # exclude __repr__ methods

def __repr__

def __str__

def __fields

def __invariants
28 changes: 14 additions & 14 deletions ands/ds/LinearProbingHashTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,21 @@ def __init__(self, capacity: int = 11):
self._keys = [None] * self._n
self._values = [None] * self._n

def __invariants(self):
"""These conditions should always hold at the beginning and end of public methods!"""
assert len(self._keys) == len(self._values) == self._n
assert not has_duplicates_ignore_nones(self._keys)

@property
def size(self) -> int:
"""Returns the number of pairs key-value in this map."""
self.__invariants__()
self.__invariants()
return sum(k is not None for k in self._keys)

@property
def capacity(self) -> int:
"""Returns the size of the internal buffers that store the keys and the values."""
self.__invariants__()
self.__invariants()
return len(self._keys)

def put(self, key: object, value: object) -> None:
Expand All @@ -110,9 +115,9 @@ def put(self, key: object, value: object) -> None:
if key is None:
raise TypeError("key cannot be None.")

self.__invariants__()
self.__invariants()
self._put(key, value, self._n)
self.__invariants__()
self.__invariants()

def _put(self, key: object, value: object, size: int) -> None:
"""Helper method of `self.put` and thus it's considered PRIVATE."""
Expand Down Expand Up @@ -181,17 +186,17 @@ def get(self, key: object) -> object:
"""Returns the value associated with `key`.
If `key` is `None`, a `TypeError` is raised, because keys cannot be None."""
self.__invariants__()
self.__invariants()
if key is None:
raise TypeError("key cannot be None.")
value = LinearProbingHashTable._get(key, self._keys, self._values, self._n)
self.__invariants__()
self.__invariants()
return value

def delete(self, key: object) -> object:
"""Deletes the mapping between `key` and its corresponding associated value.
If there's no mapping, nothing is done."""
self.__invariants__()
self.__invariants()

if key is None:
raise TypeError("key cannot be None.")
Expand All @@ -202,10 +207,10 @@ def delete(self, key: object) -> object:
i = self._keys.index(key)
v = self._values[i]
self._keys[i] = self._values[i] = None
self.__invariants__()
self.__invariants()
return v
except ValueError:
self.__invariants__()
self.__invariants()
pass

def show(self) -> None:
Expand All @@ -230,11 +235,6 @@ def __str__(self):
def __repr__(self):
return self.__str__()

def __invariants__(self):
"""These conditions should always hold at the beginning and end of public methods!"""
assert len(self._keys) == len(self._values) == self._n
assert not has_duplicates_ignore_nones(self._keys)

@staticmethod
def _hash_code(key, size: int) -> int:
"""Returns a hash code (an int) between 0 and `size` (excluded).
Expand Down
10 changes: 5 additions & 5 deletions ands/ds/TST.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def __init__(self):
self._n = 0
self._root = None

def __invariants__(self) -> None:
def __invariants(self) -> None:
"""These propositions should always be true at the BEGINNING
and END of every PUBLIC method of this TST.
Expand Down Expand Up @@ -210,15 +210,15 @@ def insert(self, key: str, value: object) -> None:
which also represents how many times we follow the middle link,
and h is the number of left and right turns.
So a lower bound of the complexity would be &Omega(m);."""
self.__invariants__()
self.__invariants()
if not isinstance(key, str):
raise TypeError("key must be an instance of type str.")
if not key:
raise ValueError("key must be a string of length >= 1.")
if value is None:
raise ValueError("value cannot be None.")
self._root = self._insert(self._root, key, value, 0)
self.__invariants__()
self.__invariants()

def _insert(self, node: TSTNode, key: str, value: object, index: int):
"""Inserts `key` with `value` into self starting from `node`."""
Expand Down Expand Up @@ -370,7 +370,7 @@ def delete(self, key: str) -> TSTNode:
k is the number of "no more necessary" cleaned up
after deletion of the node associated with `key`.
Unnecessary nodes are nodes with no children and value equal to None."""
self.__invariants__()
self.__invariants()

if not isinstance(key, str):
raise TypeError("key must be an instance of type str.")
Expand All @@ -390,7 +390,7 @@ def delete(self, key: str) -> TSTNode:
else:
result = None

self.__invariants__()
self.__invariants()
return result

def _delete_fix(self, u: TSTNode) -> None:
Expand Down
7 changes: 0 additions & 7 deletions tests/ds/test_LinearProbingHashTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,3 @@ def test_show(self):
t.put(elem, choice(string.ascii_letters))
print()
t.show()

def test_repr(self):
t = LinearProbingHashTable()
ls = sample(range(5), 5)
for elem in ls:
t.put(elem, choice(string.ascii_letters))
print(repr(t))
8 changes: 0 additions & 8 deletions tests/ds/test_MaxHeap.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,3 @@ def test_is_on_odd_level(self):
self.assertFalse(h.is_on_odd_level(6))
self.assertTrue(h.is_on_odd_level(7))
self.assertTrue(h.is_on_odd_level(8))

def test_str(self):
h = MaxHeap([12, 14, 28, 6, 7, 18, 10, 3, 1])
print(h)

def test_repr(self):
h = MaxHeap([1, 2, 3, 4, 5])
print(repr(h))
8 changes: 0 additions & 8 deletions tests/ds/test_MinHeap.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,3 @@ def test_is_on_odd_level(self):
self.assertFalse(h.is_on_odd_level(6))
self.assertTrue(h.is_on_odd_level(7))
self.assertTrue(h.is_on_odd_level(8))

def test_str(self):
h = MinHeap([1, 2, 3, 4, 5])
print(h)

def test_repr(self):
h = MinHeap([1, 2, 3, 4, 5])
print(repr(h))
10 changes: 1 addition & 9 deletions tests/ds/test_MinMaxHeap.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,4 @@ def test_index_of_min_and_max(self):
self.assertEqual(h.index_of_min(7), -1)
self.assertEqual(h.index_of_max(7), -1)
self.assertEqual(h.index_of_min(8), -1)
self.assertEqual(h.index_of_max(8), -1)

def test_str(self):
h = MinMaxHeap([7, 2, 92, 67])
print(h)

def test_repr(self):
h = MinMaxHeap([7, 2, 92, 67])
print(repr(h))
self.assertEqual(h.index_of_max(8), -1)
5 changes: 1 addition & 4 deletions tests/ds/test_Queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,4 @@ def test_dequeue_many(self):
elem = q.dequeue()
self.assertEqual(elem, ls[i])

self.assertTrue(q.is_empty())

def test_str(self):
self.assertEqual(repr(Queue(["first", "second", "last"])), str(["first", "second", "last"]))
self.assertTrue(q.is_empty())
3 changes: 0 additions & 3 deletions tests/ds/test_Stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,3 @@ def test_pop_until_empty(self):
self.assertEqual(s.size(), len(ls) - (i + 1))

self.assertTrue(s.is_empty())

def test_print_stack(self):
print("\n", repr(Stack([11, 2, 3])), sep="")
10 changes: 5 additions & 5 deletions tests/ds/test_TST.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_creation(self):

def test_insert_key_not_string(self):
t = TST()
self.assertRaises(TypeError, t.insert, 3.14)
self.assertRaises(TypeError, t.insert, 10, 5)

def test_insert_key_empty_string(self):
t = TST()
Expand Down Expand Up @@ -117,14 +117,14 @@ def test_search_key_empty_string(self):
self.assertRaises(ValueError, t.search, "")
self.assertRaises(ValueError, t.search_iteratively, "")

def test_contains_empty_tst(self):
t = TST()
self.assertFalse(t.contains("contains in an empty tst"))

def test_contains_key_not_string(self):
t = TST()
self.assertRaises(TypeError, t.contains, 3.14)

def test_contains_empty_tst(self):
t = TST()
self.assertFalse(t.contains("contains in an empty tst"))

def test_contains_key_empty_string(self):
t = TST()
self.assertRaises(ValueError, t.contains, "")
Expand Down

0 comments on commit 1e9214f

Please sign in to comment.