forked from ascv/HyperLogLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
213 lines (163 loc) · 6.37 KB
/
test.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
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
from HLL import HyperLogLog
from random import randint
import pickle
import unittest
import sys
class TestAdd(unittest.TestCase):
def setUp(self):
self.hll = HyperLogLog(5)
def test_add_string(self):
try:
self.hll.add('asdf')
except Exception as ex:
self.fail('failed to add string: %s' % ex)
@unittest.skipIf(sys.version_info[0] > 2, 'buffer is deprecated in python 3.x')
def test_add_buffer(self):
try:
self.hll.add(buffer('asdf'))
except Exception as ex:
self.fail('failed to add buffer: %s' % ex)
def test_add_bytes(self):
try:
self.hll.add(b'asdf')
except Exception as ex:
self.fail('failed to add bytes: %s' % ex)
class TestCardinalityEstimation(unittest.TestCase):
def setUp(self):
self.hll = HyperLogLog(5)
def test_small_range_correction_all_registers_set_to_zero(self):
self.assertEqual(self.hll.cardinality(), 0.0)
def test_small_range_correction_not_all_registers_set_to_zero(self):
self.hll.set_register(0, 1)
c = self.hll.cardinality()
correction= 1.46571806761 <= c and c <= 1.46571806762
self.assertTrue(correction)
def test_medium_range_no_correction(self):
for i in range(32):
self.hll.set_register(i, 2)
c = self.hll.cardinality()
no_correction = 89.216 <= c and c <= 89.217
self.assertTrue(no_correction)
@unittest.skip("correction value needs to be re-computed")
def test_large_range_correction(self):
hll = HyperLogLog(16)
for i in range(hll.size() - 1):
hll.set_register(i, 16)
c = hll.cardinality()
correction = 7916284520 <= c and c <= 7916284521
self.assertTrue(correction)
class TestHyperLogLogConstructor(unittest.TestCase):
def test_one_is_invalid_size(self):
with self.assertRaises(ValueError):
HyperLogLog(0)
def test_negative_size_is_invalid(self):
with self.assertRaises(ValueError):
HyperLogLog(-1)
def test_minimum_size_is_valid(self):
try:
HyperLogLog(2)
except Exception:
self.fail()
def test_maximum_size_is_valid(self):
try:
HyperLogLog(16)
except Exception:
self.fail()
def test_greater_than_the_maximum_size_is_invalid(self):
with self.assertRaises(ValueError):
HyperLogLog(17)
def test_all_registers_initialized_to_zero(self):
hll = HyperLogLog(5)
registers = hll.registers()
for register in registers:
self.assertEqual(register, 0)
def test_k_param_determines_the_number_of_registers(self):
hll = HyperLogLog(5)
self.assertEqual(len(hll.registers()), 32)
self.assertEqual(hll.size(), 32)
def test_seed_parameter_sets_seed(self):
hll = HyperLogLog(5, seed=4)
self.assertEqual(hll.seed(), 4)
hll2 = HyperLogLog(5, seed=2)
self.assertNotEqual(hll.murmur3_hash('test'), hll2.murmur3_hash('test'))
class TestMerging(unittest.TestCase):
def test_only_same_size_HyperLogLogs_can_be_merged(self):
hll = HyperLogLog(4)
hll2 = HyperLogLog(5)
with self.assertRaises(ValueError):
hll.merge(hll2)
def test_merge(self):
expected = bytearray(4)
expected[0] = 1
expected[3] = 1
hll = HyperLogLog(2)
hll2 = HyperLogLog(2)
hll.set_register(0, 1)
hll2.set_register(3, 1)
hll.merge(hll2)
self.assertEqual(hll.registers(), expected)
class TestPickling(unittest.TestCase):
def setUp(self):
hlls = [HyperLogLog(x, randint(1, 1000)) for x in range(4, 16)]
cardinalities = [x**5 for x in range(1, 16)]
for hll, n in zip(hlls, cardinalities):
for i in range(1, n):
hll.add(str(i))
self.hlls = hlls
def test_pickled_cardinality(self):
for hll in self.hlls:
expected = hll.cardinality()
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(expected, hll2.cardinality())
def test_pickled_seed(self):
for hll in self.hlls:
expected = hll.seed()
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(expected, hll2.seed())
def test_pickled_registers(self):
for hll in self.hlls:
expected = hll.registers()
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(expected, hll2.registers())
def test_pickled_size(self):
for hll in self.hlls:
expected = hll.size()
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(expected, hll2.size())
class TestRegisterFunctions(unittest.TestCase):
def setUp(self):
self.k = 5
self.hll = HyperLogLog(5)
def test_set_last_register(self):
self.hll.set_register(self.k - 1, 1)
self.assertTrue(self.hll.registers()[self.k - 1] == 1)
def test_set_first_register(self):
self.hll.set_register(0, 1)
self.assertTrue(self.hll.registers()[0] == 1)
def test_set_register_with_negative_value_fails(self):
with self.assertRaises(ValueError):
self.hll.set_register(0, -1)
def test_set_register_with_greater_than_max_rank_fails(self):
with self.assertRaises(ValueError):
self.hll.set_register(0, 33)
def test_set_register_with_index_out_of_bounds(self):
with self.assertRaises(IndexError):
self.hll.set_register(32, 1)
def test_set_register_with_negative_index_fails(self):
with self.assertRaises(ValueError):
self.hll.set_register(0, -1)
def test_bytesarray_has_correct_values(self):
expected = bytearray(32)
for i in range(31):
expected[i] = randint(0, 16)
for i in range(31):
self.hll.set_register(i, expected[i])
registers = self.hll.registers()
for i in range(31):
self.assertEqual(expected[i], registers[i])
def test_registers_returns_bytesarray(self):
self.assertTrue(type(self.hll.registers()) is bytearray)
def test_bytesarray_has_correct_length(self):
self.assertTrue(len(self.hll.registers()) == pow(2, self.k))
if __name__ == '__main__':
unittest.main()