This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
tests.js
88 lines (65 loc) · 2.28 KB
/
tests.js
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
var HyperLogLog = require('./hyperloglog');
var hash = HyperLogLog.hash;
var vows = require('vows');
var assert = require('assert');
vows.describe('HyperLogLog').addBatch({
'empty hll has cardinality of zero': function () {
assert.equal(HyperLogLog(8).count(), 0);
},
'counts unique things': function () {
var thing1 = hash('thing1');
var thing2 = hash('thing2');
var thing3 = hash('thing3');
var hll = HyperLogLog(8);
hll.add(thing1);
assert.equal(hll.count(), 1);
for (var i = 0; i < 100; ++i) {
hll.add(thing1);
hll.add(thing2);
hll.add(thing3);
}
assert.equal(hll.count(), 3);
},
'merges overlapping counts': function () {
var hll = HyperLogLog(15);
var hll2 = HyperLogLog(15);
for (var i = 0; i < 100; ++i) {
hll.add(hash('Just hll ' + i));
hll2.add(hash('Just hll2 ' + i));
var both = hash('Both ' + i);
hll.add(both);
hll2.add(both);
}
assert(Math.abs(hll.count() - 200) <= 2);
assert(Math.abs(hll2.count() - 200) <= 2);
hll.merge(hll2.output());
assert(Math.abs(hll.count() - 300) <= 3);
},
'merges bigger HLL into a smaller one': function () {
var hll = HyperLogLog(8);
var hll2 = HyperLogLog(14);
var original_relative_error = hll.relative_error();
hll.add(hash('Just hll'));
hll2.add(hash('Just hll2'));
var both = hash('both');
hll.add(both);
hll2.add(both);
hll.merge(hll2.output());
assert.equal(hll.count(), 3);
assert(hll.relative_error() == original_relative_error);
},
'merges a smaller HLL into a bigger one': function () {
// The result is the same size as the smaller one.
var hll = HyperLogLog(14);
var hll2 = HyperLogLog(8);
var original_relative_error = hll.relative_error();
hll.add(hash('Just hll'));
hll2.add(hash('Just hll2'));
var both = hash('both');
hll.add(both);
hll2.add(both);
hll.merge(hll2.output());
assert.equal(hll.count(), 3);
assert(hll.relative_error() > original_relative_error);
}
}).export(module);