- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathtest_hash_map.py
97 lines (74 loc) · 2.23 KB
/
test_hash_map.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
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
fromoperatorimportdelitem, getitem, setitem
importpytest
fromdata_structures.hashing.hash_mapimportHashMap
def_get(k):
returngetitem, k
def_set(k, v):
returnsetitem, k, v
def_del(k):
returndelitem, k
def_run_operation(obj, fun, *args):
try:
returnfun(obj, *args), None
exceptExceptionase:
returnNone, e
_add_items= (
_set("key_a", "val_a"),
_set("key_b", "val_b"),
)
_overwrite_items= [
_set("key_a", "val_a"),
_set("key_a", "val_b"),
]
_delete_items= [
_set("key_a", "val_a"),
_set("key_b", "val_b"),
_del("key_a"),
_del("key_b"),
_set("key_a", "val_a"),
_del("key_a"),
]
_access_absent_items= [
_get("key_a"),
_del("key_a"),
_set("key_a", "val_a"),
_del("key_a"),
_del("key_a"),
_get("key_a"),
]
_add_with_resize_up= [
*[_set(x, x) forxinrange(5)], # guaranteed upsize
]
_add_with_resize_down= [
*[_set(x, x) forxinrange(5)], # guaranteed upsize
*[_del(x) forxinrange(5)],
_set("key_a", "val_b"),
]
@pytest.mark.parametrize(
"operations",
[
pytest.param(_add_items, id="add items"),
pytest.param(_overwrite_items, id="overwrite items"),
pytest.param(_delete_items, id="delete items"),
pytest.param(_access_absent_items, id="access absent items"),
pytest.param(_add_with_resize_up, id="add with resize up"),
pytest.param(_add_with_resize_down, id="add with resize down"),
],
)
deftest_hash_map_is_the_same_as_dict(operations):
my=HashMap(initial_block_size=4)
py= {}
for_, (fun, *args) inenumerate(operations):
my_res, my_exc=_run_operation(my, fun, *args)
py_res, py_exc=_run_operation(py, fun, *args)
assertmy_res==py_res
assertstr(my_exc) ==str(py_exc)
assertset(py) ==set(my)
assertlen(py) ==len(my)
assertset(my.items()) ==set(py.items())
deftest_no_new_methods_was_added_to_api():
defis_public(name: str) ->bool:
returnnotname.startswith("_")
dict_public_names= {namefornameindir({}) ifis_public(name)}
hash_public_names= {namefornameindir(HashMap()) ifis_public(name)}
assertdict_public_names>hash_public_names