Ticket #5212: simple_test.cc

File simple_test.cc, 3.5 KB (added by ipapadop@…, 12 years ago)

Test that stresses writing-reading from text archives

Line 
1#include <fstream>
2#include <iostream>
3#include <boost/archive/text_oarchive.hpp>
4#include <boost/archive/text_iarchive.hpp>
5
6#include <boost/serialization/vector.hpp>
7
8void test_vector()
9{
10 typedef std::vector<int> container_type;
11 typedef container_type::value_type value_type;
12
13 container_type c;
14
15 c.push_back(value_type(1));
16 c.push_back(value_type(2));
17 c.push_back(value_type(3));
18 c.push_back(value_type(3));
19
20 for (container_type::const_iterator it=c.begin(); it!=c.end(); it++) {
21 std::cout << *it << std::endl;
22 }
23
24 {
25 std::ofstream ofs("vfilename");
26 // save data to archive
27 boost::archive::text_oarchive oa(ofs);
28 // write class instance to archive
29 oa << c;
30 // archive and stream closed when destructors are called
31 }
32
33 // ... some time later restore the class instance to its orginal state
34 container_type c2;
35 {
36 // create and open an archive for input
37 std::ifstream ifs("vfilename");
38 boost::archive::text_iarchive ia(ifs);
39 // read class state from archive
40 ia >> c2;
41 // archive and stream closed when destructors are called
42 }
43
44 for (container_type::const_iterator it=c2.begin(); it!=c2.end(); it++) {
45 std::cout << *it << std::endl;
46 }
47}
48
49#include <boost/serialization/set.hpp>
50
51void test_set()
52{
53 typedef std::set<int> container_type;
54 typedef container_type::value_type value_type;
55
56 container_type c;
57
58 c.insert(value_type(1));
59 c.insert(value_type(2));
60 c.insert(value_type(3));
61 c.insert(value_type(3));
62
63 for (container_type::const_iterator it=c.begin(); it!=c.end(); it++) {
64 std::cout << *it << std::endl;
65 }
66
67 {
68 std::ofstream ofs("sfilename");
69 // save data to archive
70 boost::archive::text_oarchive oa(ofs);
71 // write class instance to archive
72 oa << c;
73 // archive and stream closed when destructors are called
74 }
75
76 // ... some time later restore the class instance to its orginal state
77 container_type c2;
78 {
79 // create and open an archive for input
80 std::ifstream ifs("sfilename");
81 boost::archive::text_iarchive ia(ifs);
82 // read class state from archive
83 ia >> c2;
84 // archive and stream closed when destructors are called
85 }
86
87 for (container_type::const_iterator it=c2.begin(); it!=c2.end(); it++) {
88 std::cout << *it << std::endl;
89 }
90}
91
92#include <boost/serialization/map.hpp>
93
94void test_map()
95{
96 // typedef std::map<int,int> container_type;
97 typedef std::multimap<int,int> container_type;
98
99 typedef container_type::value_type value_type;
100
101 container_type c;
102
103 c.insert(value_type(1,10));
104 c.insert(value_type(2,20));
105 c.insert(value_type(3,50));
106 c.insert(value_type(3,150));
107
108 for (container_type::const_iterator it=c.begin(); it!=c.end(); it++) {
109 std::cout << (*it).first << " => " << (*it).second << std::endl;
110 }
111
112 {
113 std::ofstream ofs("mfilename");
114 // save data to archive
115 boost::archive::text_oarchive oa(ofs);
116 // write class instance to archive
117 oa << c;
118 // archive and stream closed when destructors are called
119 }
120
121 // ... some time later restore the class instance to its orginal state
122 container_type c2;
123 {
124 // create and open an archive for input
125 std::ifstream ifs("mfilename");
126 boost::archive::text_iarchive ia(ifs);
127 // read class state from archive
128 ia >> c2;
129 // archive and stream closed when destructors are called
130 }
131
132 for (container_type::const_iterator it=c2.begin(); it!=c2.end(); it++) {
133 std::cout << (*it).first << " => " << (*it).second << std::endl;
134 }
135}
136
137int main()
138{
139 test_vector();
140 test_set();
141 test_map();
142 return 0;
143}