-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsubstrings_test.cc
More file actions
132 lines (95 loc) · 3.58 KB
/
Copy pathsubstrings_test.cc
File metadata and controls
132 lines (95 loc) · 3.58 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
#include <algorithm>
#include <set>
#include <string>
#include "substrings.h"
namespace {
std::set<std::string> unique_strings;
void CollectUnique(size_t input0_count, size_t input1_count, double log_odds,
const ev::StringRef& string) {
if (input1_count) return;
unique_strings.emplace(string.str());
}
void CompareSets(const std::string& input0, const std::string& input1,
const std::set<std::string>& expected,
const std::set<std::string>& got) {
bool ok = true;
std::set<std::string> missing;
std::set_difference(expected.begin(), expected.end(), got.begin(), got.end(),
std::inserter(missing, missing.end()));
for (const std::string& s : missing) {
fprintf(stderr, "Missing output string: \"%.*s\"\n",
static_cast<int>(s.size()), s.data());
ok = false;
}
std::set<std::string> unexpected;
std::set_difference(unique_strings.begin(), unique_strings.end(),
expected.begin(), expected.end(),
std::inserter(unexpected, unexpected.end()));
for (const std::string& s : unexpected) {
fprintf(stderr, "Unexpected output string: \"%.*s\" (%zu bytes)\n",
static_cast<int>(s.size()), s.data(), s.size());
ok = false;
}
if (!ok) {
fprintf(stderr, "Input 0 was: \"%.*s\"\n", static_cast<int>(input0.size()),
input0.data());
fprintf(stderr, "Input 1 was: \"%.*s\"\n", static_cast<int>(input1.size()),
input1.data());
abort();
}
}
void TestUniqueStrings(const std::string& input0, const std::string& input1,
const std::set<std::string>& expected) {
CommonSubstringFinder csf;
csf.input0 = input0.data();
csf.input0_size = input0.size();
csf.input1 = input1.data();
csf.input1_size = input1.size();
csf.threshold_count = 1;
csf.filter_redundant_features = 0;
csf.output = CollectUnique;
unique_strings.clear();
csf.FindSubstringFrequencies();
CompareSets(input0, input1, expected, unique_strings);
}
std::string MakeDocuments(const std::string& input, char sep) {
std::string result;
for (char ch : input) {
if (ch == sep)
result.push_back(0);
else
result.push_back(ch);
}
return result;
}
void TestDocuments(const std::string& input0, const std::string& input1,
const std::set<std::string>& expected) {
CommonSubstringFinder csf;
csf.input0 = input0.data();
csf.input0_size = input0.size();
csf.input1 = input1.data();
csf.input1_size = input1.size();
csf.threshold_count = 1;
csf.output = CollectUnique;
unique_strings.clear();
csf.FindSubstringFrequencies();
CompareSets(input0, input1, expected, unique_strings);
}
} // namespace
int main(int argc, char** argv) {
TestUniqueStrings("aa aaz", "", {"a", "aa"});
TestUniqueStrings("aa aa", "", {"a", "aa"});
TestUniqueStrings("aa aa", "xyz", {"a", "aa"});
TestUniqueStrings("aa aa", "a", {"aa"});
TestUniqueStrings("cccAcccBcccCccc", "ccd dcc ccd dcc dcd", {"ccc"});
TestUniqueStrings("cccAcccBcccCccc", "cccAcccBcccCccc", {});
TestDocuments(MakeDocuments("ccc|ccc|ccc|ccc", '|'),
MakeDocuments("ccd|dcc|ccd|dcc|dcd", '|'), {"ccc"});
TestDocuments(MakeDocuments("ccc|ccc|ccc|ccc", '|'),
MakeDocuments("ccc|ccc|ccc|ccc|ccc", '|'), {});
TestDocuments(MakeDocuments("ccc|ccc|ccc|ccc", '|'),
MakeDocuments("ccc|ccc|ccc|ccc|", '|'), {});
TestUniqueStrings("abcabc", "", {"a", "ab", "abc", "bc", "b", "c"});
TestUniqueStrings("abcabc", "abx", {"abc", "bc", "c"});
return EXIT_SUCCESS;
}