-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathtest_hashtable.py
More file actions
398 lines (255 loc) · 10.9 KB
/
Copy pathtest_hashtable.py
File metadata and controls
398 lines (255 loc) · 10.9 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# test_hashtable.py
from unittest.mock import patch
import pytest
from pytest_unordered import unordered
from hashtable import DELETED, HashTable
@pytest.fixture
def hash_table():
sample_data = HashTable(capacity=100)
sample_data["hola"] = "hello"
sample_data[98.6] = 37
sample_data[False] = True
return sample_data
def test_should_create_hashtable():
assert HashTable(capacity=100) is not None
def test_should_report_length_of_empty_hash_table():
assert len(HashTable(capacity=100)) == 0
def test_should_report_length(hash_table):
assert len(hash_table) == 3
def test_should_report_capacity_of_empty_hash_table():
assert HashTable(capacity=100).capacity == 100
def test_should_report_capacity(hash_table):
assert hash_table.capacity == 100
def test_should_create_empty_pair_slots():
assert HashTable(capacity=3)._slots == [None, None, None]
def test_should_insert_key_value_pairs():
hash_table = HashTable(capacity=100)
hash_table["hola"] = "hello"
hash_table[98.6] = 37
hash_table[False] = True
assert ("hola", "hello") in hash_table.pairs
assert (98.6, 37) in hash_table.pairs
assert (False, True) in hash_table.pairs
assert len(hash_table) == 3
def test_should_not_contain_none_value_when_created():
assert None not in HashTable(capacity=100).values
def test_should_insert_none_value():
hash_table = HashTable(capacity=100)
hash_table["key"] = None
assert ("key", None) in hash_table.pairs
def test_should_find_value_by_key(hash_table):
assert hash_table["hola"] == "hello"
assert hash_table[98.6] == 37
assert hash_table[False] is True
def test_should_raise_error_on_missing_key():
hash_table = HashTable(capacity=100)
with pytest.raises(KeyError) as exception_info:
hash_table["missing_key"]
assert exception_info.value.args[0] == "missing_key"
def test_should_find_key(hash_table):
assert "hola" in hash_table
def test_should_not_find_key(hash_table):
assert "missing_key" not in hash_table
def test_should_get_value(hash_table):
assert hash_table.get("hola") == "hello"
def test_should_get_none_when_missing_key(hash_table):
assert hash_table.get("missing_key") is None
def test_should_get_default_value_when_missing_key(hash_table):
assert hash_table.get("missing_key", "default") == "default"
def test_should_get_value_with_default(hash_table):
assert hash_table.get("hola", "default") == "hello"
def test_should_delete_key_value_pair(hash_table):
assert "hola" in hash_table
assert ("hola", "hello") in hash_table.pairs
assert len(hash_table) == 3
del hash_table["hola"]
assert "hola" not in hash_table
assert ("hola", "hello") not in hash_table.pairs
assert len(hash_table) == 2
def test_should_raise_key_error_when_deleting(hash_table):
with pytest.raises(KeyError) as exception_info:
del hash_table["missing_key"]
assert exception_info.value.args[0] == "missing_key"
def test_should_update_value(hash_table):
assert hash_table["hola"] == "hello"
hash_table["hola"] = "hallo"
assert hash_table["hola"] == "hallo"
assert hash_table[98.6] == 37
assert hash_table[False] is True
assert len(hash_table) == 3
def test_should_return_pairs(hash_table):
assert hash_table.pairs == {
("hola", "hello"),
(98.6, 37),
(False, True),
}
def test_should_get_pairs_of_empty_hash_table():
assert HashTable(capacity=100).pairs == set()
def test_should_return_copy_of_pairs(hash_table):
assert hash_table.pairs is not hash_table.pairs
def test_should_not_include_blank_pairs(hash_table):
assert None not in hash_table.pairs
def test_should_return_duplicate_values():
hash_table = HashTable(capacity=100)
hash_table["Alice"] = 24
hash_table["Bob"] = 42
hash_table["Joe"] = 42
assert [24, 42, 42] == sorted(hash_table.values)
def test_should_get_values(hash_table):
assert unordered(hash_table.values) == ["hello", 37, True]
def test_should_get_values_of_empty_hash_table():
assert HashTable(capacity=100).values == []
def test_should_return_copy_of_values(hash_table):
assert hash_table.values is not hash_table.values
def test_should_get_keys(hash_table):
assert hash_table.keys == {"hola", 98.6, False}
def test_should_get_keys_of_empty_hash_table():
assert HashTable(capacity=100).keys == set()
def test_should_return_copy_of_keys(hash_table):
assert hash_table.keys is not hash_table.keys
def test_should_convert_to_dict(hash_table):
dictionary = dict(hash_table.pairs)
assert set(dictionary.keys()) == hash_table.keys
assert set(dictionary.items()) == hash_table.pairs
assert list(dictionary.values()) == unordered(hash_table.values)
def test_should_not_create_hashtable_with_zero_capacity():
with pytest.raises(ValueError):
HashTable(capacity=0)
def test_should_not_create_hashtable_with_negative_capacity():
with pytest.raises(ValueError):
HashTable(capacity=-100)
def test_should_iterate_over_keys(hash_table):
for key in hash_table.keys:
assert key in ("hola", 98.6, False)
def test_should_iterate_over_values(hash_table):
for value in hash_table.values:
assert value in ("hello", 37, True)
def test_should_iterate_over_pairs(hash_table):
for key, value in hash_table.pairs:
assert key in hash_table.keys
assert value in hash_table.values
def test_should_iterate_over_instance(hash_table):
for key in hash_table:
assert key in ("hola", 98.6, False)
def test_should_use_dict_literal_for_str(hash_table):
assert str(hash_table) in {
"{'hola': 'hello', 98.6: 37, False: True}",
"{'hola': 'hello', False: True, 98.6: 37}",
"{98.6: 37, 'hola': 'hello', False: True}",
"{98.6: 37, False: True, 'hola': 'hello'}",
"{False: True, 'hola': 'hello', 98.6: 37}",
"{False: True, 98.6: 37, 'hola': 'hello'}",
}
def test_should_create_hashtable_from_dict():
dictionary = {"hola": "hello", 98.6: 37, False: True}
hash_table = HashTable.from_dict(dictionary)
assert hash_table.capacity == len(dictionary) * 10
assert hash_table.keys == set(dictionary.keys())
assert hash_table.pairs == set(dictionary.items())
assert unordered(hash_table.values) == list(dictionary.values())
def test_should_create_hashtable_from_dict_with_custom_capacity():
dictionary = {"hola": "hello", 98.6: 37, False: True}
hash_table = HashTable.from_dict(dictionary, capacity=100)
assert hash_table.capacity == 100
assert hash_table.keys == set(dictionary.keys())
assert hash_table.pairs == set(dictionary.items())
assert unordered(hash_table.values) == list(dictionary.values())
def test_should_have_canonical_string_representation(hash_table):
assert repr(hash_table) in {
"HashTable.from_dict({'hola': 'hello', 98.6: 37, False: True})",
"HashTable.from_dict({'hola': 'hello', False: True, 98.6: 37})",
"HashTable.from_dict({98.6: 37, 'hola': 'hello', False: True})",
"HashTable.from_dict({98.6: 37, False: True, 'hola': 'hello'})",
"HashTable.from_dict({False: True, 'hola': 'hello', 98.6: 37})",
"HashTable.from_dict({False: True, 98.6: 37, 'hola': 'hello'})",
}
def test_should_compare_equal_to_itself(hash_table):
assert hash_table == hash_table
def test_should_compare_equal_to_copy(hash_table):
assert hash_table is not hash_table.copy()
assert hash_table == hash_table.copy()
def test_should_compare_equal_different_key_value_order(hash_table):
h1 = HashTable.from_dict({"a": 1, "b": 2, "c": 3})
h2 = HashTable.from_dict({"b": 2, "a": 1, "c": 3})
assert h1 == h2
def test_should_compare_unequal(hash_table):
other = HashTable.from_dict({"different": "value"})
assert hash_table != other
def test_should_compare_unequal_another_data_type(hash_table):
assert hash_table != 42
def test_should_copy_keys_values_pairs_capacity(hash_table):
copy = hash_table.copy()
assert copy is not hash_table
assert set(hash_table.keys) == set(copy.keys)
assert set(hash_table.pairs) == set(copy.pairs)
assert unordered(hash_table.values) == copy.values
assert hash_table.capacity == copy.capacity
def test_should_compare_equal_different_capacity():
data = {"a": 1, "b": 2, "c": 3}
h1 = HashTable.from_dict(data, capacity=50)
h2 = HashTable.from_dict(data, capacity=100)
assert h1 == h2
@patch("builtins.hash", return_value=24)
def test_should_detect_and_resolve_hash_collisions(mock_hash):
hash_table = HashTable(capacity=100)
hash_table["hola"] = "hello"
hash_table[98.6] = 37
hash_table[False] = True
assert len(hash_table) == 3
assert hash_table._slots[24] == ("hola", "hello")
assert hash_table._slots[25] == (98.6, 37)
assert hash_table._slots[26] == (False, True)
@patch("builtins.hash", side_effect=[2, 1, 1])
def test_should_wrap_index_around_when_collides(mock_hash):
hash_table = HashTable(capacity=3)
hash_table["hola"] = "hello"
hash_table[98.6] = 37
hash_table[False] = True
assert len(hash_table) == 3
assert hash_table._slots[2] == ("hola", "hello")
assert hash_table._slots[1] == (98.6, 37)
assert hash_table._slots[0] == (False, True)
def test_should_not_overwrite_deleted(hash_table):
del hash_table["hola"]
deleted_slot = hash_table._slots.index(DELETED)
assert len(hash_table) == 2
assert DELETED in hash_table._slots
with patch("builtins.hash", return_value=deleted_slot):
hash_table["gracias"] = "thank you"
assert len(hash_table) == 3
assert DELETED in hash_table._slots
def test_should_raise_memory_error_when_not_enough_capacity():
hash_table = HashTable(capacity=3)
hash_table["hola"] = "hello"
hash_table[98.6] = 37
hash_table[False] = True
with pytest.raises(MemoryError) as exception_info:
hash_table["gracias"] = "thank you"
assert exception_info.value.args[0] == "Not enough capacity"
@patch("builtins.hash", return_value=24)
def test_should_get_collided_values(mock_hash):
hash_table = HashTable(capacity=3)
hash_table["hola"] = "hello"
hash_table[98.6] = 37
hash_table[False] = True
assert len(hash_table) == 3
assert hash_table["hola"] == "hello"
assert hash_table[98.6] == 37
assert hash_table[False] is True
@patch("builtins.hash", side_effect=[0, 1, 2, 0, 1, 0])
def test_should_not_get_deleted_values(mock_hash):
hash_table = HashTable(capacity=3)
hash_table["hola"] = "hello"
hash_table[98.6] = 37
hash_table[False] = True
del hash_table["hola"]
del hash_table[98.6]
assert hash_table[False] is True
def test_should_mark_as_deleted(hash_table):
index = hash_table._slots.index(("hola", "hello"))
del hash_table["hola"]
assert hash_table._slots[index] is DELETED
def test_pairs_should_not_contain_deleted(hash_table):
del hash_table["hola"]
del hash_table[98.6]
assert hash_table.pairs == {(False, True)}