-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcritbit-test.cpp
149 lines (129 loc) · 3.45 KB
/
critbit-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
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
#include "critbit.hpp"
#include <string>
#include <stdlib.h>
#include <set>
#include <vector>
#define CYBOZU_TEST_DISABLE_AUTO_RUN
#include <cybozu/test.hpp>
#include <cybozu/benchmark.hpp>
#include <cybozu/mmap.hpp>
#include <cybozu/unordered_set.hpp>
typedef std::vector<std::string> StrVec;
struct StrSet : std::set<std::string> {
bool has(const std::string& str) const
{
return this->find(str) != this->end();
}
};
struct StrHash : CYBOZU_NAMESPACE_STD::unordered_set<std::string> {
bool has(const std::string& str) const
{
return this->find(str) != this->end();
}
};
CYBOZU_TEST_AUTO(has)
{
critbit::StrSet crit;
static const char* elems[] = {"a", "aa", "b", "bb", "ab", "ba", "aba", "bab", NULL};
for (size_t i = 0; elems[i]; ++i) {
crit.insert(elems[i]);
}
CYBOZU_TEST_EQUAL(crit.size(), 8);
for (size_t i = 0; elems[i]; ++i) {
CYBOZU_TEST_ASSERT(crit.has(elems[i]));
}
}
CYBOZU_TEST_AUTO(remove)
{
static const char* elems[] = {"a", "aa", "b", "bb", "ab", "ba", "aba", "bab", NULL};
for (size_t i = 1; elems[i]; ++i) {
critbit::StrSet crit;
for (size_t j = 0; j < i; ++j) crit.insert(elems[j]);
CYBOZU_TEST_EQUAL(crit.size(), i);
for (size_t j = 0; j < i; ++j) {
CYBOZU_TEST_ASSERT(crit.has(elems[j]));
}
for (size_t j = 0; j < i; ++j) {
CYBOZU_TEST_ASSERT(crit.remove(elems[j]));
}
CYBOZU_TEST_EQUAL(crit.size(), 0);
for (size_t j = 0; j < i; ++j) {
CYBOZU_TEST_ASSERT(!crit.has(elems[j]));
}
}
}
static bool handler(void *s, const char* elem)
{
((StrSet*)s)->insert(elem);
return true;
}
CYBOZU_TEST_AUTO(traverse)
{
critbit::StrSet crit;
static const char* elems[] = {"a", "aa", "aaz", "abz", "bba", "bbc", "bbd", NULL};
for (size_t i = 0; elems[i]; ++i) crit.insert(elems[i]);
CYBOZU_TEST_EQUAL(crit.size(), 7);
StrSet s;
crit.traverse("a", &s, handler);
CYBOZU_TEST_EQUAL(s.size(), 4);
CYBOZU_TEST_ASSERT(s.find("a") != s.end());
CYBOZU_TEST_ASSERT(s.find("aa") != s.end());
CYBOZU_TEST_ASSERT(s.find("aaz") != s.end());
CYBOZU_TEST_ASSERT(s.find("abz") != s.end());
s.clear();
crit.traverse("aa", &s, handler);
CYBOZU_TEST_EQUAL(s.size(), 2);
CYBOZU_TEST_ASSERT(s.find("aa") != s.end());
CYBOZU_TEST_ASSERT(s.find("aaz") != s.end());
}
template<class Set>
int findAll(const Set& s, const StrVec& sv)
{
int c = 0;
for (size_t i = 0; i < sv.size(); i++) {
c += s.has(sv[i]);
}
return c;
}
template<class Set>
void bench(const char *msg, const std::string& text)
{
printf("bench:%s -------------------\n", msg);
StrVec words;
Set set;
std::istringstream iss(text);
std::string word;
while (iss >> word) {
set.insert(word);
words.push_back(word);
}
printf("word num %d\n", (int)set.size());
for (size_t i = 0; i < set.size(); i++) {
const std::string w = words[i];
const char *msg = words[i].c_str();
CYBOZU_BENCH(msg, set.has, w);
if (i == 9) break;
}
const std::string str = "maybe-not-found-string";
CYBOZU_BENCH(str.c_str(), set.has, str);
int c = 0;
CYBOZU_BENCH_C("findAll", 1, c = findAll, set, words);
printf("count=%d\n", c);
}
int main(int argc, char *argv[])
try
{
if (argc == 2) {
const std::string file = argv[1];
printf("load file %s\n", file.c_str());
cybozu::Mmap m(file);
const std::string text(m.get(), m.size());
bench<StrSet>("std::set", text);
bench<StrHash>("std::unordered_set", text);
bench<critbit::StrSet>("critbit::set", text);
}
return cybozu::test::autoRun.run(argc, argv);
} catch (std::exception& e) {
printf("ERR %s\n", e.what());
return 1;
}