-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_hashtable.py
More file actions
95 lines (70 loc) · 2.45 KB
/
Copy pathtest_hashtable.py
File metadata and controls
95 lines (70 loc) · 2.45 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from hashtable.hashtable import HashTable
def test_can_instantiate_hashtable():
new_hashtable = HashTable()
assert new_hashtable
assert new_hashtable.size == 1024
assert len(new_hashtable.bucket) == 1024
def test_hash_function_returns_valid_index():
hashtable = HashTable()
actual_index = hashtable.hash("app")
expected = 791
assert actual_index == expected
def test_hash_function_will_return_a_value_within_range():
hashtable = HashTable()
actual = hashtable.hash("test")
assert actual <= 1024
assert actual >= 0
def test_hash_function_will_return_similar_value_for_similar_word():
hashtable = HashTable()
key_a = "abcd"
key_b = "dcba"
assert hashtable.hash(key_a) == hashtable.hash(key_b)
def test_hash_function_will_produce_different_values_for_different_words():
hashtable = HashTable()
key_a = "abcd"
key_b = "efgh"
assert hashtable.hash(key_a) != hashtable.hash(key_b)
def test_add_works_correctly():
hashtable = HashTable()
index = hashtable.hash("spam")
assert hashtable.bucket[index] is None
hashtable.add("spam", "eggs")
bucket = hashtable.bucket[index]
assert bucket
def test_add_method_will_add_to_linked_list_and_handle_collisions():
hashtable = HashTable()
hashtable.add("abcd", 10)
hashtable.add("dcba", 20)
index = hashtable.hash("abcd")
count = 0
current = hashtable.bucket[index].head
while current:
count += 1
current = current.next
assert count == 2
def test_get_method_returns_value_if_key_present():
hashtable = HashTable()
hashtable.add("abcd", 10)
assert hashtable.get("abcd") == 10
def test_get_method_returns_None_for_value_not_present():
hashtable = HashTable()
hashtable.add("abcd", 10)
assert hashtable.get("xyz") is None
def test_get_method_will_account_for_linked_list_with_multiple_values():
hashtable = HashTable()
hashtable.add("abcd", 10)
hashtable.add("dcba", 20)
hashtable.add("cbad", 30)
assert hashtable.get("dcba") == 20
def test_contains_returns_true_if_key_is_present():
hashtable = HashTable()
hashtable.add("abcd", 10)
hashtable.add("bcda", 20)
hashtable.add("abdc", 30)
assert hashtable.contains("abcd")
def test_contains_returns_false_if_key_is_not_present():
hashtable = HashTable()
hashtable.add("abcd", 10)
hashtable.add("bcda", 20)
hashtable.add("abdc", 30)
assert hashtable.contains("xyz") == False