-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest.py
More file actions
447 lines (355 loc) · 14 KB
/
test.py
File metadata and controls
447 lines (355 loc) · 14 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import pickle
import random
import unittest
from HLL import HyperLogLog
from random import randint
class TestAdd(unittest.TestCase):
def setUp(self):
self.hll = HyperLogLog(5)
def test_add_string(self):
try:
self.hll.add('my string')
except Exception as ex:
self.fail('failed to add string: %s' % ex)
def test_add_bytes(self):
try:
self.hll.add(b'some other characters')
except Exception as ex:
self.fail('failed to add bytes: %s' % ex)
def test_return_value_indicates_register_update(self):
# Dense representation returns change status
hll = HyperLogLog(5, sparse=False)
changed = hll.add('asdf')
self.assertTrue(changed)
changed = hll.add('asdf')
self.assertFalse(changed)
changed = hll.add('otherdata')
self.assertTrue(changed)
# Sparse representation does not
hll = HyperLogLog(5, sparse=True)
changed = hll.add('asdf')
self.assertFalse(changed)
class TestHyperLogLogConstructor(unittest.TestCase):
def test_size_lower_bound(self):
for i in range(-1, 2):
with self.assertRaises(ValueError):
HyperLogLog(i)
def test_size_upper_bound(self):
with self.assertRaises(ValueError):
HyperLogLog(64)
def test_registers_initialized_to_zero(self):
hll = HyperLogLog(randint(2, 16))
for i in range(hll.size()):
self.assertEqual(hll.get_register(i), 0)
def test_histogram_initialized_with_correct_counts(self):
k = randint(2, 10)
hll = HyperLogLog(k)
hist = hll._histogram()
self.assertEqual(sum(hist), 2**k)
def test_p_sets_size(self):
for i in range(2, 6):
hll = HyperLogLog(i)
self.assertEqual(hll.size(), 2**i)
def test_setting_a_seed(self):
hll = HyperLogLog(5, seed=4)
self.assertEqual(hll.seed(), 4)
hll2 = HyperLogLog(5, seed=20000)
self.assertNotEqual(hll.hash('test'), hll2.hash('test'))
class TestMerging(unittest.TestCase):
def test_only_same_size_can_be_merged(self):
with self.assertRaises(Exception):
hll = HyperLogLog(4)
hll.merge(HyperLogLog(5))
def test_merge_type_check(self):
hll = HyperLogLog(4)
with self.assertRaises(TypeError):
hll.merge("hello")
with self.assertRaises(TypeError):
hll.merge(123)
with self.assertRaises(TypeError):
hll.merge(None)
def test_sparse_x_dense_merge(self):
k = 8
hll_a = HyperLogLog(k, max_sparse_list_size=128)
hll_b = HyperLogLog(k, sparse=False)
hll_c = HyperLogLog(k)
for i in range(32):
hll_a.add(str(randint(1, 1024)))
for i in range(1024):
hll_b.add(str(randint(1, 1024)))
hll_c.merge(hll_a)
hll_c.merge(hll_b)
self.assertFalse(hll_c._get_meta()['is_sparse'])
for i in range(2**k):
max_fsb = max(hll_a.get_register(i), hll_b.get_register(i))
self.assertEqual(max_fsb, hll_c.get_register(i))
def test_sparse_x_sparse_merge(self):
k = 12
hll_a = HyperLogLog(k)
hll_b = HyperLogLog(k)
hll_c = HyperLogLog(k)
for i in range(32):
hll_a.add(str(randint(1, 1024)))
hll_b.add(str(randint(1, 1024)))
hll_c.merge(hll_a)
hll_c.merge(hll_b)
self.assertTrue(hll_a._get_meta()['is_sparse'])
self.assertTrue(hll_b._get_meta()['is_sparse'])
self.assertTrue(hll_c._get_meta()['is_sparse'])
for i in range(2**k):
max_fsb = max(hll_a.get_register(i), hll_b.get_register(i))
self.assertEqual(max_fsb, hll_c.get_register(i))
def test_dense_x_dense_merge(self):
k = randint(3, 8)
hll_a = HyperLogLog(k, sparse=False)
hll_b = HyperLogLog(k, sparse=False)
hll_c = HyperLogLog(k, sparse=False)
for i in range(randint(100, 1000)):
hll_a.add(str(randint(0, 1024)))
hll_b.add(str(randint(0, 1024)))
hll_c.merge(hll_a)
hll_c.merge(hll_b)
for i in range(2**k):
max_fsb = max(hll_a.get_register(i), hll_b.get_register(i))
self.assertEqual(max_fsb, hll_c.get_register(i))
class TestGetMeta(unittest.TestCase):
def test_max_buffer_size_reports_correctly(self):
hll = HyperLogLog(8, max_sparse_list_size=1000, max_sparse_buffer_size=500)
meta = hll._get_meta()
self.assertEqual(meta['max_list_size'], 1000)
self.assertEqual(meta['max_buffer_size'], 500)
def test_max_buffer_size_default(self):
hll = HyperLogLog(8, max_sparse_list_size=100)
meta = hll._get_meta()
self.assertEqual(meta['max_list_size'], 100)
# Default buffer size is max_list_size / 2
self.assertEqual(meta['max_buffer_size'], 50)
class TestPickling(unittest.TestCase):
def setUp(self):
hlls = [HyperLogLog(x, randint(1, 10**6), sparse=False) 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.dense_hlls = hlls
hlls = [HyperLogLog(x, randint(1, 10**6), sparse=True) 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.sparse_hlls = hlls
def test_dense_pickled_cardinality(self):
for hll in self.dense_hlls:
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(hll.cardinality(), hll2.cardinality())
def test_dense_pickle_merge_workflow(self):
"""
Test repeated pickle dumps, pickle loads, and merging for dense HyperLogLogs.
See:
https://github.com/ascv/HyperLogLog/issues/46
"""
random.seed(0)
p = 8
seed = 0
merge_hll = HyperLogLog(p=p, seed=seed, sparse=False)
control_hll = HyperLogLog(p=p, seed=seed, sparse=False)
merge_count = 1024
possible_values = [str(i) for i in range(2**16)]
chosen_values = set()
for _ in range(merge_count):
tmp_hll = HyperLogLog(p=p, seed=seed, sparse=False)
random_values = random.sample(possible_values, k=random.randint(0, 2**8))
chosen_values.update(random_values)
for val in random_values:
tmp_hll.add(val)
control_hll.add(val)
tmp_hll = pickle.loads(pickle.dumps(tmp_hll))
merge_hll.merge(tmp_hll)
random.seed(None)
self.assertEqual(merge_hll.cardinality(), control_hll.cardinality())
def test_dense_pickled_seed(self):
for hll in self.dense_hlls:
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(hll.seed(), hll2.seed())
def test_dense_pickled_register_histogram(self):
for hll in self.dense_hlls:
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(hll._histogram(), hll2._histogram())
def test_dense_pickled_registers(self):
for hll in self.dense_hlls:
hll2 = pickle.loads(pickle.dumps(hll))
for i in range(hll.size()):
self.assertEqual(hll.get_register(i), hll2.get_register(i))
def test_dense_pickled_size(self):
for hll in self.dense_hlls:
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(hll.size(), hll2.size())
def test_sparse_pickled_cardinality(self):
for hll in self.sparse_hlls:
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(hll.cardinality(), hll2.cardinality())
def test_sparse_pickle_merge_workflow(self):
"""
Test repeated pickle dumps, pickle loads, and merging for sparse HyperLogLogs.
See:
https://github.com/ascv/HyperLogLog/issues/46
"""
random.seed(0)
p = 8
seed = 0
merge_hll = HyperLogLog(p=p, seed=seed, sparse=True)
control_hll = HyperLogLog(p=p, seed=seed, sparse=True)
merge_count = 1024
possible_values = [str(i) for i in range(2**16)]
chosen_values = set()
for _ in range(merge_count):
tmp_hll = HyperLogLog(p=p, seed=seed, sparse=True)
random_values = random.sample(possible_values, k=random.randint(0, 2**8))
chosen_values.update(random_values)
for val in random_values:
tmp_hll.add(val)
control_hll.add(val)
tmp_hll = pickle.loads(pickle.dumps(tmp_hll))
merge_hll.merge(tmp_hll)
random.seed(None)
self.assertEqual(merge_hll.cardinality(), control_hll.cardinality())
def test_sparse_pickled_seed(self):
for hll in self.sparse_hlls:
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(hll.seed(), hll2.seed())
def test_sparse_pickled_register_histogram(self):
for hll in self.sparse_hlls:
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(hll._histogram(), hll2._histogram())
def test_sparse_pickled_registers(self):
for hll in self.sparse_hlls:
hll2 = pickle.loads(pickle.dumps(hll))
for i in range(hll.size()):
self.assertEqual(hll.get_register(i), hll2.get_register(i))
def test_sparse_pickled_size(self):
for hll in self.sparse_hlls:
hll2 = pickle.loads(pickle.dumps(hll))
self.assertEqual(hll.size(), hll2.size())
class TestIntersectionCardinality(unittest.TestCase):
def test_wrong_type_raises_type_error(self):
hll = HyperLogLog(8)
with self.assertRaises(TypeError):
hll.intersection_cardinality("hello")
with self.assertRaises(TypeError):
hll.intersection_cardinality(123)
with self.assertRaises(TypeError):
hll.intersection_cardinality(None)
def test_mismatched_p_raises_value_error(self):
a = HyperLogLog(8)
b = HyperLogLog(10)
with self.assertRaises(ValueError):
a.intersection_cardinality(b)
def test_empty_hlls(self):
a = HyperLogLog(12)
b = HyperLogLog(12)
self.assertEqual(a.intersection_cardinality(b), 0)
def test_one_empty(self):
a = HyperLogLog(12)
b = HyperLogLog(12)
for i in range(1000):
a.add(str(i))
self.assertEqual(a.intersection_cardinality(b), 0)
def test_disjoint_sets(self):
a = HyperLogLog(12)
b = HyperLogLog(12)
for i in range(10000):
a.add(str(i))
for i in range(10000, 20000):
b.add(str(i))
result = a.intersection_cardinality(b)
# Disjoint: expect near zero (may not be exactly zero due to estimation)
self.assertLess(result, 2000)
def test_identical_sets(self):
a = HyperLogLog(12)
b = HyperLogLog(12)
for i in range(50000):
a.add(str(i))
b.add(str(i))
card = a.cardinality()
result = a.intersection_cardinality(b)
# Should be close to the cardinality of the set
self.assertAlmostEqual(result, card, delta=card * 0.15)
def test_subset(self):
a = HyperLogLog(12)
b = HyperLogLog(12)
# A is a subset of B
for i in range(10000):
a.add(str(i))
b.add(str(i))
for i in range(10000, 50000):
b.add(str(i))
result = a.intersection_cardinality(b)
expected = a.cardinality()
self.assertAlmostEqual(result, expected, delta=expected * 0.20)
def test_partial_overlap(self):
a = HyperLogLog(12)
b = HyperLogLog(12)
n_shared = 50000
n_unique = 50000
for i in range(n_shared):
a.add(str(i))
b.add(str(i))
for i in range(n_shared, n_shared + n_unique):
a.add(str(i))
for i in range(n_shared + n_unique, n_shared + 2 * n_unique):
b.add(str(i))
result = a.intersection_cardinality(b)
self.assertAlmostEqual(result, n_shared, delta=n_shared * 0.15)
def test_symmetry(self):
a = HyperLogLog(12)
b = HyperLogLog(12)
for i in range(30000):
a.add(str(i))
b.add(str(i))
for i in range(30000, 60000):
a.add(str(i))
for i in range(60000, 90000):
b.add(str(i))
self.assertEqual(a.intersection_cardinality(b),
b.intersection_cardinality(a))
def test_sparse_x_sparse(self):
a = HyperLogLog(12, sparse=True)
b = HyperLogLog(12, sparse=True)
for i in range(200):
a.add(str(i))
b.add(str(i))
for i in range(200, 400):
a.add(str(i))
result = a.intersection_cardinality(b)
self.assertAlmostEqual(result, 200, delta=100)
def test_dense_x_dense(self):
a = HyperLogLog(12, sparse=False)
b = HyperLogLog(12, sparse=False)
for i in range(20000):
a.add(str(i))
b.add(str(i))
for i in range(20000, 40000):
a.add(str(i))
result = a.intersection_cardinality(b)
self.assertAlmostEqual(result, 20000, delta=20000 * 0.15)
def test_sparse_x_dense(self):
a = HyperLogLog(12, sparse=True)
b = HyperLogLog(12, sparse=False)
for i in range(200):
a.add(str(i))
b.add(str(i))
for i in range(200, 400):
b.add(str(i))
result = a.intersection_cardinality(b)
self.assertAlmostEqual(result, 200, delta=100)
def test_single_element(self):
a = HyperLogLog(12)
b = HyperLogLog(12)
a.add('x')
b.add('x')
result = a.intersection_cardinality(b)
# Should be 1 or close to it
self.assertLessEqual(result, 5)
self.assertGreaterEqual(result, 0)
if __name__ == '__main__':
unittest.main()