/
test_table.py
179 lines (129 loc) · 5.2 KB
/
test_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
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
"Test BBHashTable."
import random
import tempfile
import os.path
from collections import defaultdict
import pytest
from bbhash_table import BBHashTable
def test_create():
# try creating and using a BBHashTable to store hashes and associated vals.
all_hashes = [ random.randint(100, 2**32) for i in range(100) ]
print(len(all_hashes))
table = BBHashTable()
table.initialize(all_hashes)
for hashval, i in zip(all_hashes, range(100, 200)):
table[hashval] = i
for hashval, i in zip(all_hashes, range(100, 200)):
assert table[hashval] == i
def test_create_fill_default():
# default value should be int32 max
all_hashes = [ random.randint(100, 2**32) for i in range(100) ]
print(len(all_hashes))
table = BBHashTable()
# don't specify default value -
table.initialize(all_hashes)
# retrieve - what do we get?
for hashval, i in zip(all_hashes, range(100, 200)):
assert table[hashval] == 2**32 - 1
def test_create_fill_specify():
# test specifying a default value
all_hashes = [ random.randint(100, 2**32) for i in range(100) ]
print(len(all_hashes))
table = BBHashTable()
# specify a default value...
table.initialize(all_hashes, fill=5)
# retrieve - what do we get?
for hashval, i in zip(all_hashes, range(100, 200)):
assert table[hashval] == 5
def test_get_unique_values():
# test the 'get_unique_values' functionality.
all_hashes = [ random.randint(100, 2**32) for i in range(100) ]
print(len(all_hashes))
table = BBHashTable()
table.initialize(all_hashes)
for hashval, value in zip(all_hashes, [1, 2, 3, 4, 5]*20):
table[hashval] = value
for hashval, value in zip(all_hashes, [1, 2, 3, 4, 5]*20):
assert table[hashval] == value
value_count = table.get_unique_values(all_hashes)
assert value_count[1] == 20
assert value_count[2] == 20
assert value_count[3] == 20
assert value_count[4] == 20
assert value_count[5] == 20
def test_get_unique_values_noexist():
# check to see what happens when we add in hashes that don't exist.
all_hashes = [ random.randint(100, 2**32) for i in range(100) ]
print(len(all_hashes))
table = BBHashTable()
table.initialize(all_hashes)
for hashval, value in zip(all_hashes, [1, 2, 3, 4, 5]*20):
table[hashval] = value
for hashval, value in zip(all_hashes, [1, 2, 3, 4, 5]*20):
assert table[hashval] == value
# since we're using random, we have to make sure the non-existent hashes
# are not present in all_hashes. Convoluted, yes... but saves us from
# that one in a bajillion chance of collision making the test fail! :)
noexist_hashes = set([ random.randint(100, 2**32) for i in range(100) ])
noexist_hashes -= set(all_hashes)
all_hashes += list(noexist_hashes)
value_counts = table.get_unique_values(all_hashes)
assert value_counts[1] == 20
assert value_counts[2] == 20
assert value_counts[3] == 20
assert value_counts[4] == 20
assert value_counts[5] == 20
assert len(list(value_counts)) == 5
# compare get_unique_values with boring old for loop
value_counts = defaultdict(int)
for hashval in all_hashes:
value = table[hashval]
value_counts[value] += 1
assert value_counts[None] == len(noexist_hashes)
assert value_counts[1] == 20
assert value_counts[2] == 20
assert value_counts[3] == 20
assert value_counts[4] == 20
assert value_counts[5] == 20
def test_get_unique_values_noexist_fail():
# test requirement that hashes exist
all_hashes = [ random.randint(100, 2**32) for i in range(100) ]
print(len(all_hashes))
table = BBHashTable()
table.initialize(all_hashes)
for hashval, value in zip(all_hashes, [1, 2, 3, 4, 5]*20):
table[hashval] = value
noexist_hash = all_hashes[0] + 1
while noexist_hash in all_hashes:
noexist_hash += 1
value_counts = table.get_unique_values([ noexist_hash ])
assert not value_counts
with pytest.raises(ValueError) as exc:
value_counts = table.get_unique_values([ noexist_hash ],
require_exist=True)
print(str(exc))
def test_get_unique_values_set():
# try passing in a set, instead of list
all_hashes = [ random.randint(100, 2**32) for i in range(100) ]
print(len(all_hashes))
table = BBHashTable()
table.initialize(all_hashes)
for hashval, value in zip(all_hashes, [1, 2, 3, 4, 5]*20):
table[hashval] = value
hashvals_set = set(all_hashes)
value_counts = table.get_unique_values(hashvals_set)
assert value_counts
def test_save_load(tmpdir):
# test save & load!
all_hashes = [ random.randint(100, 2**32) for i in range(100) ]
print(len(all_hashes))
table = BBHashTable()
table.initialize(all_hashes)
for hashval, i in zip(all_hashes, range(100, 200)):
table[hashval] = i
mphf_filename = os.path.join(tmpdir, 'table.mphf')
array_filename = os.path.join(tmpdir, 'table.array')
table.save(mphf_filename, array_filename)
table2 = BBHashTable.load(mphf_filename, array_filename)
for hashval, i in zip(all_hashes, range(100, 200)):
assert table2[hashval] == i