-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestutils.cpp
355 lines (285 loc) · 8.56 KB
/
testutils.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
Testbed for empirical evaluation of KP-ABE schemes, according to Crampton, Pinto (CSF2014).
Code by: Alexandre Miranda Pinto
This file holds tests for the main functions of utils.h.
*/
#ifndef DEF_UTILS
#include "utils.h"
#endif
int testContains() {
vector<int> set;
vector<std::string> sset;
int errors = 0;
for (int i = 0; i < 5; i++) {
set.push_back(5+i);
sset.push_back(convertIntToStr(5+i));
}
int n = contains(set, 8);
test_diagnosis("Test Contains - element is present", (n == 3), errors);
n = contains(set, 10);
test_diagnosis("Test Contains - missing element", (n == -1), errors);
n = contains(set, 5);
test_diagnosis("Test Contains - bounds elements: first", (n == 0), errors);
n = contains(set, 9);
test_diagnosis("Test Contains - bounds elements: last", (n == 4), errors);
n = contains(sset, std::string("8"));
test_diagnosis("String Test Contains - element is present", (n == 3), errors);
n = contains(sset, std::string("10"));
test_diagnosis("String Test Contains - missing element", (n == -1), errors);
n = contains(sset, std::string("5"));
test_diagnosis("String Test Contains - bounds elements: first", (n == 0), errors);
n = contains(sset, std::string("9"));
test_diagnosis("String Test Contains - bounds elements: last", (n == 4), errors);
return errors;
}
int testAddVector() {
int errors = 0;
unsigned int nbase = 10;
unsigned int nadd = 5;
vector<int> base_vecint;
vector<int> add_vecint;
std::vector<std::string> base_vecstr;
std::vector<std::string> add_vecstr;
base_vecstr.push_back("abc");
base_vecstr.push_back("bcd");
base_vecstr.push_back("cde");
base_vecstr.push_back("def");
add_vecstr.push_back("ghi");
add_vecstr.push_back("jkl");
add_vecstr.push_back("mno");
add_vecstr.push_back("pqr");
add_vecstr.push_back("stu");
add_vecstr.push_back("vwx");
unsigned int nbase_s = base_vecstr.size();
unsigned int nadd_s = add_vecstr.size();
int j;
j = 0;
// j is used to add a signed int to the vector, not an unsigned one
for (unsigned int i = 0; i < nbase; i++,j++) {
base_vecint.push_back(j);
}
for (unsigned int i = 0; i < nadd; i++) {
add_vecint.push_back(j);
}
addVector<int>(base_vecint, add_vecint);
addVector<std::string>(base_vecstr, add_vecstr);
DEBUG("base_vecint size: " << base_vecint.size());
DEBUG("nbase + nadd: " << nbase + nadd);
test_diagnosis("Test AddVector - resulting integer vector should have 15 elements", base_vecint.size() == nbase + nadd, errors);
test_diagnosis("Test AddVector - added integer vector should have 5 elements", add_vecint.size() == nadd, errors);
test_diagnosis("Test AddVector - resulting string vector should have 10 elements", base_vecstr.size() == nbase_s + nadd_s, errors);
test_diagnosis("Test AddVector - added integer vector should have 5 elements", add_vecstr.size() == nadd_s, errors);
stringstream ss;
j = 0;
for (unsigned int i = 0; i < nbase; i++,j++) {
ss.str("");
ss << "Test AddVector - comparison final vector and base: pos " << i;
test_diagnosis(ss.str(), base_vecint[i] == j, errors); // extra variable j is because the contents of the vector are not necessarily unsigned,
// and I want to avoid casts to make sure I'm not hiding any error. j must be unsigned, so can not be declared inside the for
}
for (unsigned int i = 0; i < nadd; i++) {
ss.str("");
ss << "Test AddVector - comparison final vector and add: pos " << i + nbase;
test_diagnosis(ss.str(), base_vecint[nbase + i] == add_vecint[i], errors);
}
return errors;
}
int testConvertIntToStr() {
int errors = 0;
std::string s;
std::string base = "Test ConvertIntToStr - ";
int n = 23;
s = convertIntToStr(n);
test_diagnosis(base + "23", s == "23", errors);
n = 142;
s = convertIntToStr(n);
test_diagnosis(base + "142", s == "142", errors);
n = -218;
s = convertIntToStr(n);
test_diagnosis(base + "-218", s == "-218", errors);
return errors;
}
int testConvertStrToInt() {
int errors = 0;
int n;
std::string ss;
std::string base = "Test ConvertStrToInt - ";
std::string s;
s = "145";
ss = "";
ss = base + s;
try {
n = convertStrToInt(s);
test_diagnosis(ss, n == 145, errors);
} catch (std::exception &e) {
}
s = "145a";
ss = base + s;
try {
n = convertStrToInt(s);
test_diagnosis(ss, false, errors);
} catch (std::exception &e) {
test_diagnosis(ss, true, errors);
}
s = "abc";
ss = base + s;
try {
n = convertStrToInt(s);
test_diagnosis(ss, false, errors);
} catch (std::exception &e) {
test_diagnosis(ss, true, errors);
}
s = "b637";
ss = base + s;
try {
n = convertStrToInt(s);
test_diagnosis(ss, false, errors);
} catch (std::exception &e) {
test_diagnosis(ss, true, errors);
}
s = "124.24";
ss = base + s;
try {
n = convertStrToInt(s);
test_diagnosis(ss, false, errors);
} catch (std::exception &e) {
test_diagnosis(ss, true, errors);
}
s = "-234";
ss = base + s;
try {
n = convertStrToInt(s);
test_diagnosis(ss, n == -234, errors);
} catch (std::exception &e) {
}
s = "0";
ss = base + s;
try {
n = convertStrToInt(s);
test_diagnosis(ss, n == 0, errors);
} catch (std::exception &e) {
}
s = "14546377";
ss = base + s;
try {
n = convertStrToInt(s);
test_diagnosis(ss, n == 14546377, errors);
} catch (std::exception &e) {
}
return errors;
}
int testExprTokenize() {
int errors = 0;
vector<std::string> s;
vector< vector<std::string> > result_tokens;
std::string ss;
ss = "a1,a2,a3";
s.push_back(ss);
vector<std::string> tokens;
tokens.push_back("a1");
tokens.push_back("a2");
tokens.push_back("a3");
result_tokens.push_back(tokens);
ss = " a4 \v,\ra5 ";
s.push_back(ss);
tokens.clear();
tokens.push_back("a4");
tokens.push_back("a5");
result_tokens.push_back(tokens);
ss = ",a2,a4,,";
s.push_back(ss);
tokens.clear();
tokens.push_back("");
tokens.push_back("a2");
tokens.push_back("a4");
tokens.push_back("");
tokens.push_back("");
result_tokens.push_back(tokens);
std::string temp = op_AND + "(a7)";
ss = "a4,a5," + temp;
s.push_back(ss);
tokens.clear();
tokens.push_back("a4");
tokens.push_back("a5");
tokens.push_back(temp);
result_tokens.push_back(tokens);
std::string temp1 = op_AND + "(a1 ,a2 ,a3)";
std::string temp2 = op_OR + "(a1\t,a2)";
ss = "a1 , a2, " + temp1 + "\t , a4 , " + temp2 + ", a5";
s.push_back(ss);
tokens.clear();
tokens.push_back("a1");
tokens.push_back("a2");
tokens.push_back(temp1);
tokens.push_back("a4");
tokens.push_back(temp2);
tokens.push_back("a5");
result_tokens.push_back(tokens);
std::string base = "Test ExprTokenize - ";
vector<std::string> result;
for (unsigned int i = 0; i < s.size(); i++) {
ss = base + s[i];
exprTokenize(s[i], result, ",", "(", ")");
test_diagnosis(ss, result == result_tokens[i], errors);
}
return errors;
}
int testTrim() {
int errors = 0;
vector<std::string> s;
vector<std::string> r;
std::string temp;
stringstream ss;
std::string base = "Test Trim - ";
s.push_back(" \t\f\r\v \t\n ");
r.push_back("");
temp = "abcd";
s.push_back(temp);
r.push_back(temp);
temp = "ab \t\n\r cd";
s.push_back(temp);
r.push_back(temp);
s.push_back(" \t abcd \v ");
r.push_back("abcd");
s.push_back("\r abcd");
r.push_back("abcd");
s.push_back("abcd \n ");
r.push_back("abcd");
s.push_back(" \t\n\rab cd \t\n\r");
r.push_back("ab cd");
for (unsigned int i = 0; i < s.size(); i++) {
ss.str("");
ss << base << "\"" << s[i] << "\"";
test_diagnosis(ss.str(), trim(s[i]) == r[i], errors);
}
return errors;
}
int testIsSuffix(){
int errors = 0;
std::string s1 = "abcdef";
std::string s2 = "abcdefghij";
std::string s3 = "abcdjklefghij";
std::string s4 = "xafd";
test_diagnosis("testIsSuffix: " + s1 + "; " + s2, isSuffix(s1, s2), errors);
test_diagnosis("testIsSuffix: " + s1 + "; " + s3, ! isSuffix(s1, s3), errors);
test_diagnosis("testIsSuffix: " + s1 + "; " + s4, ! isSuffix(s1, s4), errors);
test_diagnosis("testIsSuffix: " + s2 + "; " + s1, ! isSuffix(s2, s1), errors);
test_diagnosis("testIsSuffix: " + s1 + "; " + s1, isSuffix(s1, s1), errors);
return errors;
}
int runTests() {
int errors = 0;
errors += testContains();
errors += testAddVector();
errors += testConvertStrToInt();
errors += testExprTokenize();
errors += testTrim();
errors += testIsSuffix();
return errors;
}
int main() {
std::string test_name = "Test Utils";
int result = runTests();
print_test_result(result,test_name);
return 0;
}