-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.cpp
481 lines (402 loc) · 11.1 KB
/
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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include <vector>
#include <sstream>
#include <string>
#include <iostream>
#include <functional>
#include "dumpable.h"
using namespace std;
using namespace dumpable;
string currentTestName;
vector<pair<string,function<void()>>> allTests;
namespace dumpable
{
template class dvector<int>;
template class dmap<int, int>;
template class dbasic_string<char>;
}
bool failed = false;
void setup()
{
failed = false;
}
void fail(string msg)
{
failed = true;
cerr << "TEST FAIL" << endl;;
cerr << msg << endl;
}
#define ASSERT_EQUAL(a,b) if (a != b) \
{ \
ostringstream os; \
os << "Assert error: expected " #b " (" << b << ") but " #a " (" << a << ")"; \
fail(os.str()); \
}
#define ASSERT_NOT_EQUAL(a,b) if (a == b) \
{ \
ostringstream os; \
os << "Assert error: not expected " #b " (" << b << ") but " #a " (" << a << ")"; \
fail(os.str()); \
}
#define TEST(x) \
void test_##x##_();\
struct TestAppendHelper ## x {TestAppendHelper ## x(){allTests.push_back(pair<string,function<void()>>(#x,test_##x##_));}} test_append_helper_## x; \
void test_##x##_()
#define IGNORED_TEST(x) \
void test_##x##_()
TEST(simple_example)
{
// example data structure
struct student
{
dwstring name;
int score;
dvector<int> emptyVectorForTest;
student(){}
student(const wstring& name, int score) : name(name), score(score) {}
};
struct classroom
{
dvector<int> emptyVectorForTest;
dstring class_name;
dvector<student> students;
};
// building sample data
classroom c;
c.class_name = "1001";
c.students.push_back(student(L"Alice",2));
c.students.push_back(student(L"Bob",5));
c.students.push_back(student(L"\ud55c\uae00",13));
// Dump to stringstream
// NOTE: No need to write serialization code for struct student and struct classroom.
ostringstream os;
dumpable::write(c, os);
// Destroy the contents of sample data.
c.students.clear();
c.class_name = "invalid";
// read from dumped data
// NOTE: from_dumped_buffer is just a constant time operation!
// (see implementation in dumpable.h)
// You can even use a pointer from mmap.
string buffer = os.str();
const classroom* c2 = dumpable::from_dumped_buffer<classroom>(buffer.data());
// Check contents of c2
// You should keep the buffer given to from_dumped_buffer function.
// These data are only valid when the buffer is valid.
ASSERT_EQUAL("1001", c2->class_name);
ASSERT_EQUAL(L"Alice", c2->students[0].name);
ASSERT_EQUAL(2, c2->students[0].score);
ASSERT_EQUAL(L"Bob", c2->students[1].name);
ASSERT_EQUAL(5, c2->students[1].score);
ASSERT_EQUAL(L"\ud55c\uae00", c2->students[2].name);
ASSERT_EQUAL(13, c2->students[2].score);
}
TEST(linked_list)
{
struct node
{
int d;
dptr<node> n;
};
// building sample linked list
node n1, n2, n3;
n1.d = 1;
n1.n = &n2;
n2.d = 2;
n2.n = &n3;
n3.d = 3;
ASSERT_EQUAL(0, n3.n);
// dptr<node> can be used as node*
int sum = 0;
const node* p = &n1;
ASSERT_EQUAL(1, p->d);
ASSERT_EQUAL(&n2, p->n);
while(p)
{
sum += p->d;
p = p->n;
}
ASSERT_EQUAL(6, sum);
// dump linked list to stringstream
ostringstream os;
dumpable::write(n1, os);
// destroy original data
n3.d = n2.d = n1.d = -100;
n3.n = n2.n = n1.n = nullptr;
// read from dumped data
string buffer = os.str();
p = dumpable::from_dumped_buffer<node>(buffer.data());
// recheck data
sum = 0;
ASSERT_EQUAL(1, p->d);
while(p)
{
sum += p->d;
p = p->n;
}
ASSERT_EQUAL(6, sum);
}
TEST(vector)
{
vector<int> v;
dvector<int> v2, v3;
v.push_back(100);
v.push_back(300);
v.push_back(200);
ASSERT_EQUAL(100, v.front());
ASSERT_EQUAL(200, v.back());
v2 = v;
v2 = v2;
v3 = v2;
dvector<int> v4(v2);
ASSERT_EQUAL(100, v4.front());
ASSERT_EQUAL(200, v4.back());
ASSERT_EQUAL(3, v4.size());
ASSERT_EQUAL(false, v4.empty());
v4.resize(0);
ASSERT_EQUAL(true, v4.empty());
ASSERT_EQUAL(3, v3.size());
ASSERT_EQUAL(300, v3[1]);
ASSERT_EQUAL(300, v3.at(1));
v3.clear();
ASSERT_EQUAL(0, v3.size());
v2 = std::move(v2);
ASSERT_EQUAL(3, v2.size());
v3 = std::move(v2);
ASSERT_EQUAL(3, v3.size());
ASSERT_EQUAL(0, v2.size());
ASSERT_EQUAL(300, v3[1]);
int sum = 0;
for(auto it = begin(v3); it != end(v3); ++it)
sum += *it;
ASSERT_EQUAL(600, sum);
for(int i = 0; i < 20; i ++)
v3.push_back(10);
int x = 2000;
v3.push_back(x);
sum = 0;
for(auto it = v3.cbegin(); it != v3.cend(); ++it)
sum += *it;
ASSERT_EQUAL(2800, sum);
v3.resize(3);
sum = 0;
for(auto it = v3.cbegin(); it != v3.cend(); ++it)
sum += *it;
ASSERT_EQUAL(600, sum);
v3.resize(200);
ASSERT_EQUAL(100, v3.front());
ASSERT_EQUAL(0, v3.back());
v3.push_back(1000);
ASSERT_EQUAL(1000, v3.back());
sum = 0;
for(auto it = v3.cbegin(); it != v3.cend(); ++it)
sum += *it;
ASSERT_EQUAL(1600, sum);
ASSERT_EQUAL(0, v3[3]);
ASSERT_EQUAL(0, v3[4]);
}
TEST(empty_encoding)
{
struct A
{
int a;
dstring b;
int c;
dvector<int> d;
int e;
};
A data;
data.a = 1;
data.c = 3;
data.e = 5;
ostringstream os;
dumpable::write(data, os);
string buffer = os.str();
A* stored = (A*)buffer.data();
ASSERT_EQUAL(1, stored->a);
ASSERT_EQUAL(3, stored->c);
ASSERT_EQUAL(5, stored->e);
}
TEST(string)
{
dstring dumpable_empty("");
string stl_empty("");
ASSERT_EQUAL(dumpable_empty, stl_empty);
ASSERT_NOT_EQUAL(dumpable_empty, "Hello World");
ASSERT_EQUAL(dumpable_empty, "");
ASSERT_NOT_EQUAL("Hello World", dumpable_empty);
ASSERT_EQUAL("", dumpable_empty);
dstring dumpable_abc("abc");
dstring dumpable_abc2("abc");
string stl_abc("abc");
dstring dumpable_def("def");
string stl_def("def");
ASSERT_EQUAL(dumpable_abc,dumpable_abc);
dumpable_abc = dumpable_abc;
ASSERT_EQUAL(dumpable_abc,dumpable_abc);
dumpable_abc = std::move(dumpable_abc);
ASSERT_EQUAL(dumpable_abc,dumpable_abc);
ASSERT_EQUAL(dumpable_abc,dstring("abc"));
ASSERT_NOT_EQUAL(dumpable_abc,dstring("abcd"));
ASSERT_EQUAL(dumpable_abc,dumpable_abc2);
ASSERT_EQUAL(dumpable_abc,stl_abc);
ASSERT_EQUAL(dumpable_def,stl_def);
ASSERT_EQUAL(dumpable_abc2, dumpable_abc);
ASSERT_EQUAL(stl_abc, dumpable_abc);
ASSERT_EQUAL(stl_def, dumpable_def);
ASSERT_NOT_EQUAL(dumpable_abc,stl_def);
ASSERT_NOT_EQUAL(dumpable_def,stl_abc);
ASSERT_NOT_EQUAL(dumpable_abc,string("abcd"));
ASSERT_NOT_EQUAL(string("efgh"),dumpable_def);
dstring moved_abc(std::move(dumpable_abc2));
ASSERT_EQUAL(moved_abc, dumpable_abc);
ASSERT_EQUAL(0, dumpable_abc2.size());
dstring moved_abc2;
moved_abc2 = std::move(moved_abc);
ASSERT_EQUAL(moved_abc2, dumpable_abc);
ASSERT_EQUAL(0, moved_abc.size());
string testAppend;
for(auto it = dumpable_abc.begin(); it != dumpable_abc.end(); ++it)
{
testAppend += *it;
}
ASSERT_EQUAL(testAppend, dumpable_abc);
ASSERT_EQUAL('a', dumpable_abc.front());
ASSERT_EQUAL('c', dumpable_abc.back());
ostringstream os;
os << dumpable_abc;
ASSERT_EQUAL(os.str(), dumpable_abc);
ASSERT_EQUAL(os.str().c_str(), dumpable_abc);
}
TEST(map)
{
map<int, dstring> original;
original.insert(make_pair(1, "one"));
original.insert(make_pair(2, "two"));
original.insert(make_pair(5, "five"));
dmap<int, dstring> data2(original);
dmap<int, dstring> data(std::move(data2));
dmap<int, dstring> data3(data);
ASSERT_EQUAL(true, data2.empty());
ASSERT_EQUAL(0, data2.size());
ASSERT_EQUAL(0, data2.count(1));
ASSERT_EQUAL(3, data.size());
ASSERT_EQUAL("one", data.find(1)->second);
ASSERT_EQUAL("two", data.find(2)->second);
ASSERT_EQUAL("five", data.find(5)->second);
ASSERT_EQUAL(false, data3.empty());
ASSERT_EQUAL(3, data3.size());
ASSERT_EQUAL("one", data3.find(1)->second);
ASSERT_EQUAL("two", data3.find(2)->second);
ASSERT_EQUAL("five", data3.find(5)->second);
ASSERT_EQUAL(0, data.count(3));
ASSERT_EQUAL(1, data.count(1));
ASSERT_EQUAL(3, data.size());
ostringstream os;
dumpable::write(data, os);
data.clear();
ASSERT_EQUAL(0, data.count(1));
ASSERT_EQUAL(0, data.count(2));
ASSERT_EQUAL(0, data.count(5));
ASSERT_EQUAL(0, data.size());
string buffer = os.str();
const decltype(data)* p = dumpable::from_dumped_buffer<const decltype(data)>(buffer.data());
{
const dmap<int, dstring>& data = *p;
ASSERT_EQUAL("one", data.find(1)->second);
ASSERT_EQUAL("two", data.find(2)->second);
ASSERT_EQUAL("five", data.find(5)->second);
ASSERT_EQUAL(0, data.count(3));
ASSERT_EQUAL(1, data.count(1));
ASSERT_EQUAL(3, data.size());
dmap<int, dstring> data2(data);
ASSERT_EQUAL(3, data2.size());
dmap<int, dstring> data3;
ASSERT_EQUAL(3, data2.size());
data3 = std::move(data2);
ASSERT_EQUAL(0, data2.size());
ASSERT_EQUAL(3, data3.size());
ASSERT_EQUAL(true, data3.value_comp()(*data3.find(1), *data3.find(2)));
}
}
TEST(not_dump)
{
struct example
{
int a;
not_dump<string> b;
dstring c;
not_dump<shared_ptr<int>> p;
};
example data;
data.a = 3;
data.b = "hello";
data.b += "there";
data.c = "world";
data.p = make_shared<int>(10);
ASSERT_EQUAL("hellothere", data.b);
ASSERT_EQUAL(10, *data.p);
ostringstream os;
dumpable::write(data, os);
string buffer = os.str();
example* stored = (example*)buffer.c_str();
ASSERT_EQUAL(3, stored->a);
ASSERT_EQUAL("world", stored->c);
ASSERT_EQUAL("", stored->b);
ASSERT_EQUAL(true, !stored->p);
}
TEST(basic_implementation)
{
struct embedded_empty
{
// intentionally empty
};
struct embedded
{
int x;
dptr<embedded_empty> y;
};
struct data
{
int a;
dptr<embedded> b;
int c;
};
data d;
d.a = 1;
d.b = new embedded;
d.b->x = 2;
d.b->y = new embedded_empty;
d.c = 3;
ostringstream os;
dumpable::write(d, os);
string buffer = os.str();
data* e = dumpable::from_dumped_buffer<data>(&buffer[0]);
ASSERT_EQUAL(1, e->a);
ASSERT_EQUAL(2, e->b->x);
ASSERT_EQUAL(3, e->c);
}
int testmain()
{
bool isAnyTestFailed = false;
for(auto it = allTests.begin(); it != allTests.end(); ++it)
{
setup();
currentTestName = it->first;
it->second();
if (failed)
{
isAnyTestFailed = true;
cerr << "F";
}
else
{
cerr << ".";
}
}
cerr << endl;
return isAnyTestFailed ? -1 : 0;
}
int main()
{
return testmain();
}