-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_binary.cpp
More file actions
103 lines (78 loc) · 3.45 KB
/
test_binary.cpp
File metadata and controls
103 lines (78 loc) · 3.45 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
/*
test_binary.cpp
Test cases for cpp-generic-serialize
*/
#include <cassert>
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <map>
#include <list>
#include <utility>
#include "skip.hpp"
#include "binary.hpp"
namespace usb = util::serialize::binary;
template<typename T>
void test1(std::string fn, const T & t) {
/* save */
std::ofstream os(fn, std::ios::out | std::ios::binary);
usb::save(os, t);
os.close();
/* load */
T t_in;
std::ifstream is(fn, std::ios::in | std::ios::binary);
usb::load(is, t_in);
is.close();
/* assert */
assert(t_in == t);
}
template<typename T>
void test2(std::string fn, const T & t) {
/* save */
std::ofstream os(fn, std::ios::out | std::ios::binary);
usb::save(os, t);
os.close();
/* load */
std::ifstream is(fn, std::ios::in | std::ios::binary);
auto t_in = usb::load<T>(is);
is.close();
/* assert */
assert(t_in == t);
}
int main() {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* T E S T S A V E / L O A D */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
test1("int.bin", int { -5 } );
test1("string.bin", std::string {"foobarbaz"} );
test1("map_1.bin", std::map<std::string, unsigned> {{"foo", 1}, {"bar", 2}} );
test1("list_1.bin", std::list<unsigned int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 } );
test1("list_map_1.bin", std::list<std::map<std::string, unsigned>> {{{"foo", 1}, {"bar", 2}}, {{"baz", 1}, {"quux", 2}}} );
// test("pair.bin", pair );
test2("int.bin", int { -5 } );
test2("string.bin", std::string {"foobarbaz"} );
test2("map_1.bin", std::map<std::string, unsigned> {{"foo", 1}, {"bar", 2}} );
test2("list_1.bin", std::list<unsigned int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 } );
test2("list_map_1.bin", std::list<std::map<std::string, unsigned>> {{{"foo", 1}, {"bar", 2}}, {{"baz", 1}, {"quux", 2}}} );
// test("pair.bin", pair );
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* T E S T S K I P */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
std::string s("foobar");
/* save */
std::ofstream os("skip.bin", std::ios::out | std::ios::binary);
usb::save(os, s);
auto pos_os = os.tellp();
os.close();
/* skip */
std::ifstream is("skip.bin", std::ios::in | std::ios::binary);
// usb::skip<decltype(s)>(is);
usb::skip<std::string>(is);
auto pos_is = is.tellg();
/* assert */
assert(pos_os == pos_is);
assert(is.good());
std::cout << "All tests run successful." << std::endl;
return 0;
}