-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_hash_table.py
More file actions
302 lines (266 loc) · 8.99 KB
/
test_hash_table.py
File metadata and controls
302 lines (266 loc) · 8.99 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# DO NOT MODIFY THE TESTS IN THIS FILE
# Run me via: python3 -m unittest test_hash_table or
# py -m unittest test_hash_table
import unittest
import time
import random
from hash_table import HashTable
class TestHashTable(unittest.TestCase):
"""
Initialization
"""
def test_instantiation(self):
"""
Test 1: A HashTable exists.
"""
try:
HashTable()
except NameError:
self.fail("Could not instantiate HashTable.")
# def test_size(self):
# """
# Test 2: A default HashTable has a size attribute that is 100.
# """
# h = HashTable()
# self.assertEqual(100, h.capacity)
# def test_instantiation_with_size(self):
# """
# Test 3: A HashTable can be instantiated with an optional size.
# """
# h = HashTable(25)
# self.assertEqual(25, h.capacity)
# """
# Basic API
# """
# # Hint: Do the naive thing. You do NOT need data storage to pass this test.
# def test_simple_insertion(self):
# """
# Test 4: Insert a single key-value pair into to hash table
# """
# h = HashTable()
# try:
# h['spam'] = 'eggs'
# except TypeError:
# self.fail("HashTable has no __setitem__ implementation")
# #Hint: Do the naive thing. You do NOT need data storage to pass this test.
# def test_simple_retrieval(self):
# """
# Test 5: Retrive a value from the hash table. If key not present, raise KeyError
# """
# h = HashTable()
# try:
# _ = h['spam']
# self.fail("Did not raise KeyError: Missing key.")
# except KeyError:
# pass
# def test_hash(self):
# """
# Hash function returns hash no greater than its size - 1.
# """
# h = HashTable(23)
# self.assertEqual(0, h.hash(0))
# self.assertEqual(22, h.hash(22))
# self.assertEqual(0, h.hash(23))
# self.assertEqual(4, h.hash(27))
# self.assertEqual(hash("fake key") % 23, h.hash("fake key"))
# """
# Data Storage
# """
# def test_data(self):
# """
# Test 7: A HashTable has an internal array for storing k-v pairs.
# """
# h = HashTable(50)
# self.assertEqual(list, type(h.data))
# def test_data_contents(self):
# """
# Test 8: A HashTable data array contains empty lists.
# We need lists at each location in the array to store multiple
# key-value pairs in the event of collisions
# """
# h = HashTable(5)
# expected = [ [], [], [], [], [] ]
# self.assertEqual(expected, h.data)
# """
# Insertion Basics
# """
# def test_insert_one(self):
# """
# Test 9: Inserting a k-v pair stores it as a two-element array in the list at
# the right index.
# """
# h = HashTable(5)
# h[11] = 'eggs' # 11 is the key, not an index :)
# self.assertEqual([[11, 'eggs']], h.data[1])
# """
# Retrieval Basics
# """
# def test_retrieve_one(self):
# """
# Test 10: The value of an inserted k-v pair is retrievable.
# """
# h = HashTable(5)
# h['spam'] = 'eggs'
# self.assertEqual('eggs', h['spam'])
# """
# Insertion
# """
# def test_insert_two(self):
# """
# Test 11: Inserting two k-v pairs stores them as two-element arrays in the list
# at the right index.
# """
# h = HashTable(5)
# h[9] = 'spam' # Using numbers as keys for visibility.
# h[11] = 'eggs'
# self.assertEqual([[9, 'spam']], h.data[4])
# self.assertEqual([[11, 'eggs']], h.data[1])
# def test_insert_existing(self):
# """
# Test 12: Inserting a k-v pair where the key already exists overwrites the old value.
# """
# h = HashTable(5)
# h[9] = 'spam' # Using numbers as keys for visibility.
# h[9] = 'eggs'
# self.assertEqual([[9, 'eggs']], h.data[4])
# def test_insert_collision(self):
# """
# Test 13: Inserting a k-v pair where the key has the same hash as an existing key
# appends the new k-v pair to the list at the appropriate index.
# """
# h = HashTable(5)
# h[9] = 'spam'
# h[4] = 'eggs'
# self.assertEqual([[9, 'spam'], [4, 'eggs']], h.data[4])
# """
# Deletion
# """
# def test_delete(self):
# """
# Test 14: Deleting a key removes the k-v pair from the hash table.
# """
# h = HashTable(5)
# h['spam'] = 'eggs'
# h.delete('spam')
# try:
# _ = h['spam']
# self.fail("Did not raise KeyError: Missing key.")
# except KeyError:
# pass
# """
# Hash table length
# """
# def test_hash_table_length_empty(self):
# """
# Test 15: An empty hash table returns a length of 0
# """
# h = HashTable(5)
# self.assertEqual(0, len(h))
# def test_hash_table_length(self):
# """
# Test 16: An non-empty hash table returns the number of key-value pairs in the hash table
# """
# h = HashTable(50)
# for key in range(75):
# h[key] = fake_value()
# self.assertEqual(75, len(h))
# """
# Misc. Methods
# """
# def test_clear(self):
# """
# Test 17:A cleared HashTable has an empty data array.
# """
# h = HashTable(5)
# h['spam'] = 'eggs'
# h['needle'] = 'haystack'
# h['osu'] = 'beavers'
# h.clear()
# self.assertEqual([[], [], [], [], []], h.data)
# def test_initial_keys(self):
# """
# Test 18: A HashTable initially has no keys.
# """
# h = HashTable()
# self.assertEqual([], h.keys())
# def test_keys(self):
# """
# Test 19: A HashTable can produce a list of its keys.
# """
# h = HashTable()
# h['spam'] = 'eggs'
# h['osu'] = 'beavers'
# h['needle'] = 'haystack'
# keys = h.keys()
# keys.sort()
# self.assertEqual(['needle', 'osu', 'spam'], keys)
# def test_initial_data(self):
# """
# Test 20: A HashTable initially has no data.
# """
# h = HashTable(5)
# self.assertEqual([], h.values())
# def test_data(self):
# """
# Test 21: A HashTable can produce a list of its data.
# """
# h = HashTable()
# h['spam'] = 'eggs'
# h['osu'] = 'beavers'
# h['needle'] = 'haystack'
# data = h.values()
# data.sort()
# self.assertEqual(['beavers', 'eggs', 'haystack'], data)
# # """
# # Time complexity
# # """
# def test_retrieval_is_constant_time(self):
# """
# Test 22: Retrieving a value from a dictionary should take the same amount of time
# no matter how many k-v pairs it contains. It should be O(... what?)
# """
# time_samples = []
# key = fake_key()
# value = fake_value()
# small_table = HashTable()
# small_table[key] = value
# large_table = HashTable(20000)
# for _ in range(10000):
# large_table[fake_key()] = fake_value()
# large_table[key] = value
# for _ in range(9999):
# large_table[fake_key()] = fake_value()
# small_average_elapsed_time = average_retrieval_time(small_table, key)
# large_average_elapsed_time = average_retrieval_time(large_table, key)
# self.assertAlmostEqual(small_average_elapsed_time, large_average_elapsed_time, delta=(small_average_elapsed_time+1e-6)*2)
# def test_constant_retrieval_order(self):
# """
# Test 23: Retrieving a value using the first-used key and the most recently-used
# key should be in constant time.
# """
# h = HashTable(20000)
# first_key = fake_key()
# last_key = fake_key()
# h[first_key] = fake_value()
# for _ in range(19998):
# h[fake_key()] = fake_value()
# h[last_key] = fake_value()
# first_key_value_average_retrieval_time = average_retrieval_time(h, first_key)
# last_key_value_average_retrieval_time = average_retrieval_time(h, last_key)
# self.assertAlmostEqual(first_key_value_average_retrieval_time,\
# last_key_value_average_retrieval_time,\
# delta=(first_key_value_average_retrieval_time+1e-6)*2)
def fake_key():
return f"FAKE KEY {time.time()}"
def fake_value():
return f"FAKE VALUE {time.time()}"
def average_retrieval_time(table, key):
time_samples = []
for _ in range(1000):
start_time = time.time()
_ = table[key]
end_time = time.time()
time_samples.append(end_time - start_time)
return sum(time_samples) / float(len(time_samples))
if __name__ == '__main__':
unittest.main()