-
Notifications
You must be signed in to change notification settings - Fork 15
/
count_min_sketch_test.cpp
59 lines (51 loc) · 1.66 KB
/
count_min_sketch_test.cpp
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
# include <iostream>
# include <map>
# include <cstdlib>
# include <cmath>
# include "count_min_sketch.hpp"
using namespace std;
// run basic tests on CountMinSketch
int main(int argc, char **argv) {
// Count for ar_str[i] is i1+i2+...
// where i's are the positions where ar_str[i] occurs
const char *ar_str[] = {
"hello", "some", "one", "hello", "alice",
"one", "lady", "let", "us", "lady",
"alice", "in", "wonderland", "us", "lady",
"lady", "some", "hello", "none", "pie"
};
CountMinSketch c(0.01, 0.1);
unsigned int i, total = 0;
map<const char *, int> mapitems;
map<const char *, int>::const_iterator it;
for (i = 0; i < 15; i++) {
if ((it = mapitems.find(ar_str[i]))!=mapitems.end()) {
mapitems[ar_str[i]] += i;
} else {
mapitems[ar_str[i]] = i;
}
c.update(ar_str[i], i);
total += i;
}
// 1. test for items in ar_str
// note: since probablistic (and not deterministic)
// might not be accurate sometimes
for (it = mapitems.begin(); it != mapitems.end(); it++) {
if (c.estimate(it->first) != mapitems[it->first]) {
cout << "Incorrect count for " << it->first <<
";error: " << abs(int(c.estimate(it->first)-mapitems[it->first]))
<< endl;
} else {
cout << "Correct count for '" << it->first <<
"' ;count: " << c.estimate(it->first) << endl;
}
}
cout << "c.totalcount()==total? "
<< (c.totalcount() == total ? "True" : "False")
<< "Sketch Total: " << c.totalcount() << endl;
// 2. test for items not in ar_str
cout << "Testing for strings not in ar_str..." << endl;
cout << c.estimate("blabla") << endl;
cout << c.estimate("yoyo!") << endl;
return 0;
}