-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfilter-predicate_test.cc
235 lines (195 loc) · 6.22 KB
/
filter-predicate_test.cc
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
#include "filter-predicate.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
static int errors = 0;
void ERROR(const string& error) {
cout << "ERROR: " << error << endl;
;
++errors;
}
class TestObj {
public:
TestObj(int val) { val_ = val; }
int Val() { return val_; }
static int ValWrapper(TestObj* t) { return t->Val(); }
bool Alive() { return val_; }
static bool AliveWrapper(TestObj* t) { return t->Alive(); }
void SetStr(string str) { str_ = str; }
string Str() { return str_; }
static string StrWrapper(TestObj* t) { return t->Str(); }
private:
int val_;
string str_;
};
bool TestBooleanFilterPredicate() {
bool success = true;
cout << "Testing BooleanFilterPredicate" << endl;
// Create test objects
vector<TestObj*> test_objects;
for (int i = 0; i < 20; ++i) {
test_objects.push_back(new TestObj(i));
}
BooleanFilterPredicate<TestObj> bfp(TestObj::AliveWrapper);
vector<TestObj*> filtered = bfp.FilterVector(test_objects);
for (int i = 0; i < filtered.size(); ++i) {
if (!filtered[i]->Alive()) {
ERROR("All filtered items should have Alive() == true.");
success = false;
}
}
for (int i = 0; i < test_objects.size(); ++i) {
delete test_objects[i];
}
return success;
}
bool TestGTFilterPredicate() {
bool success = true;
cout << "Testing GTFilterPredicate" << endl;
// Create test objects
vector<TestObj*> test_objects;
for (int i = 0; i < 20; ++i) {
test_objects.push_back(new TestObj(i));
}
// Create a GTFilterPredicate to keep any objects > 11.
int val = 11;
GTFilterPredicate<TestObj, int> gtfp(val, TestObj::ValWrapper);
vector<TestObj*> filtered = gtfp.FilterVector(test_objects);
for (int i = 0; i < filtered.size(); ++i) {
cout << "Filtered[" << i << "] = " << filtered[i]->Val() << endl;
if (filtered[i]->Val() <= val) {
ERROR("filtered[i] wasn't > val.");
success = false;
}
}
for (int i = 0; i < test_objects.size(); ++i) {
delete test_objects[i];
}
return success;
}
bool TestLTFilterPredicate() {
bool success = true;
cout << "Testing LTFilterPredicate" << endl;
// Create test objects
vector<TestObj*> test_objects;
for (int i = 0; i < 20; ++i) {
test_objects.push_back(new TestObj(i));
}
// Create a LTFilterPredicate to keep any objects < 6
int val = 6;
LTFilterPredicate<TestObj, int> ltfp(val, TestObj::ValWrapper);
vector<TestObj*> filtered = ltfp.FilterVector(test_objects);
for (int i = 0; i < filtered.size(); ++i) {
cout << "Filtered[" << i << "] = " << filtered[i]->Val() << endl;
if (filtered[i]->Val() >= val) {
ERROR("filtered[i] wasn't < val.");
success = false;
}
}
for (int i = 0; i < test_objects.size(); ++i) {
delete test_objects[i];
}
return success;
}
bool TestORFilterPredicate() {
bool success = true;
cout << "Testing OrFilterPredicate" << endl;
// Create test objects
vector<TestObj*> test_objects;
for (int i = 0; i < 20; ++i) {
test_objects.push_back(new TestObj(i));
}
// Create the OR filter:
OrFilterPredicate<TestObj> or_filter;
// Create two children:
int val1 = 11;
int val2 = 4;
GTFilterPredicate<TestObj, int>* gtfp =
new GTFilterPredicate<TestObj, int>(val1, TestObj::ValWrapper);
LTFilterPredicate<TestObj, int>* ltfp =
new LTFilterPredicate<TestObj, int>(val2, TestObj::ValWrapper);
or_filter.AddChild(gtfp);
or_filter.AddChild(ltfp);
vector<TestObj*> filtered = or_filter.FilterVector(test_objects);
for (int i = 0; i < filtered.size(); ++i) {
cout << "Filtered[" << i << "] = " << filtered[i]->Val() << endl;
if (filtered[i]->Val() <= val1 && filtered[i]->Val() >= val2) {
ERROR("Filtered[i] wasn't > val1 or < val2.");
success = false;
}
}
for (int i = 0; i < test_objects.size(); ++i) {
delete test_objects[i];
}
return success;
}
bool TestANDFilterPredicate() {
bool success = true;
cout << "Testing AndFilterPredicate" << endl;
// Create test objects
vector<TestObj*> test_objects;
for (int i = 0; i < 20; ++i) {
test_objects.push_back(new TestObj(i));
}
// Create the AND filter:
AndFilterPredicate<TestObj> and_filter;
// Create two children:
int val1 = 11;
int val2 = 4;
GTFilterPredicate<TestObj, int>* gtfp =
new GTFilterPredicate<TestObj, int>(val1, TestObj::ValWrapper);
GTFilterPredicate<TestObj, int>* gtfp2 =
new GTFilterPredicate<TestObj, int>(val2, TestObj::ValWrapper);
and_filter.AddChild(gtfp2);
and_filter.AddChild(gtfp);
vector<TestObj*> filtered = and_filter.FilterVector(test_objects);
for (int i = 0; i < filtered.size(); ++i) {
cout << "Filtered[" << i << "] = " << filtered[i]->Val() << endl;
if (!(filtered[i]->Val() > val1 && filtered[i]->Val() > val2)) {
ERROR("Filtered[i] wasn't > val1 and > val2.");
success = false;
}
}
for (int i = 0; i < test_objects.size(); ++i) {
delete test_objects[i];
}
return success;
}
bool TestStringContainsFilterPredicate() {
bool success = true;
cout << "Testing StringContainsFilterPredicate" << endl;
// Create test objects
vector<TestObj*> test_objects;
for (int i = 0; i < 20; ++i) {
test_objects.push_back(new TestObj(i));
}
test_objects[2]->SetStr("hi gabe");
test_objects[3]->SetStr("gabe is cool");
test_objects[10]->SetStr("gabe gabe gabe");
test_objects[11]->SetStr("doesn't have the good word.");
// Make the filter
StringContainsFilterPredicate<TestObj> filter(string("gabe"),
TestObj::StrWrapper);
vector<TestObj*> filtered = filter.FilterVector(test_objects);
for (int i = 0; i < filtered.size(); ++i) {
string str = filtered[i]->Str();
cout << "Filtered[" << i << "] = " << str << endl;
if (str.find("gabe") == string::npos) {
ERROR("Filtered[i] didn't contain \"gabe\"");
success = false;
}
}
assert(filtered.size() == 3);
return success;
}
bool RunTests() {
return TestBooleanFilterPredicate() && TestGTFilterPredicate() &&
TestLTFilterPredicate() && TestORFilterPredicate() &&
TestANDFilterPredicate() && TestStringContainsFilterPredicate();
}
int main() {
bool success = RunTests();
cout << errors << " errors." << endl;
return success;
}