-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_hash_table.py
53 lines (41 loc) · 2.12 KB
/
test_hash_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Tests basic functionality of the hash table methods, such as statistics.
"""
from hash_table import LinearProbeTable
import unittest
__author__ = "Jackson Goerner"
FIX_TABLESIZE = 19
def silly_hash(key):
return (ord(key[0]) % FIX_TABLESIZE)
class TestHashTable(unittest.TestCase):
""" Testing Hash Table functionality. """
def test_initialisation(self):
table = LinearProbeTable(10, tablesize_override=FIX_TABLESIZE)
table.hash = silly_hash
for name in "Eva, Amy, Tim, Ron, Jan, Kim, Dot, Ann, Jim, Jon".split(", "):
table[name] = name + "-value"
conflict, probe_total, probe_max, rehash = table.statistics()
self.assertEqual(conflict, 4) # Tim, Ann, Jim, Jon
self.assertEqual(probe_total, 8) # Tim: 1, Ann: 2, Jim: 2, Jon: 3
self.assertEqual(probe_max, 3) # Jon: 3
self.assertEqual(rehash, 0) # No rehash
self.assertEqual(table["Tim"], "Tim-value")
self.assertRaises(KeyError, lambda: table["Joe"])
def test_rehash(self):
table = LinearProbeTable(10, tablesize_override=FIX_TABLESIZE)
table.hash = silly_hash
for name in "Eva, Amy, Tim, Ron, Jan, Kim, Dot, Ann, Jim, Jon".split(", "):
table[name] = name + "-value"
# Rehash should be checked before the item is actually inserted - So the 10th/19th insert doesn't trigger.
self.assertEqual(len(table.table), FIX_TABLESIZE, "Table rehashing too early.")
table["Joe"] = "Joe-value"
# But the 11th/19th does.
self.assertGreater(len(table.table), FIX_TABLESIZE, "Table not being rehashed.")
conflict, probe_total, probe_max, rehash = table.statistics()
self.assertGreaterEqual(conflict, 4) # Tim, Ann, Jim, Jon + Whatever rehash caused
self.assertGreaterEqual(probe_total, 8) # Tim: 1, Ann: 2, Jim: 2, Jon: 3 + Whatever rehash caused
self.assertGreaterEqual(probe_max, 3) # Jon: 3 + Whatever rehash caused
self.assertEqual(rehash, 1) # 1 rehash
if __name__ == '__main__':
# running all the tests
unittest.main()