1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
2 | // test_simple_class.cpp |
3 | |
4 | // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . |
5 | // Use, modification and distribution is subject to the Boost Software |
6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
7 | // http://www.boost.org/LICENSE_1_0.txt) |
8 | |
9 | // should pass compilation and execution |
10 | |
11 | #include <cstdlib> // for rand(), NULL, size_t |
12 | |
13 | #include <fstream> |
14 | #include <boost/config.hpp> |
15 | |
16 | #include <cstdio> // remove |
17 | #if defined(BOOST_NO_STDC_NAMESPACE) |
18 | namespace std{ |
19 | using ::rand; |
20 | using ::remove; |
21 | } |
22 | #endif |
23 | |
24 | #include "test_tools.hpp" |
25 | |
26 | #include <boost/serialization/nvp.hpp> |
27 | #include <boost/serialization/binary_object.hpp> |
28 | |
29 | class A { |
30 | friend class boost::serialization::access; |
31 | char data[150]; |
32 | // note: from an aesthetic perspective, I would much prefer to have this |
33 | // defined out of line. Unfortunately, this trips a bug in the VC 6.0 |
34 | // compiler. So hold our nose and put it her to permit running of tests. |
35 | template<class Archive> |
36 | void serialize(Archive & ar, const unsigned int /* file_version */){ |
37 | ar & boost::serialization::make_nvp( |
38 | n: "data" , |
39 | v: boost::serialization::make_binary_object(t: data, size: sizeof(data)) |
40 | ); |
41 | } |
42 | |
43 | public: |
44 | A(); |
45 | bool operator==(const A & rhs) const; |
46 | }; |
47 | |
48 | A::A(){ |
49 | int i = sizeof(data); |
50 | while(i-- > 0) |
51 | data[i] = static_cast<char>(0xff & std::rand()); |
52 | } |
53 | |
54 | bool A::operator==(const A & rhs) const { |
55 | int i = sizeof(data); |
56 | while(i-- > 0) |
57 | if(data[i] != rhs.data[i]) |
58 | return false; |
59 | return true; |
60 | } |
61 | |
62 | int test_main( int /* argc */, char* /* argv */[] ) |
63 | { |
64 | const char * testfile = boost::archive::tmpnam(NULL); |
65 | BOOST_REQUIRE(NULL != testfile); |
66 | |
67 | const A a; |
68 | char s1[] = "a" ; |
69 | char s2[] = "ab" ; |
70 | char s3[] = "abc" ; |
71 | char s4[] = "abcd" ; |
72 | const int i = 12345; |
73 | |
74 | A a1; |
75 | char s1_1[10]; |
76 | char s1_2[10]; |
77 | char s1_3[10]; |
78 | char s1_4[10]; |
79 | int i1 = 34790; |
80 | |
81 | std::memset(s: s1_1, c: '\0', n: sizeof(s1_1)); |
82 | std::memset(s: s1_2, c: '\0', n: sizeof(s1_2)); |
83 | std::memset(s: s1_3, c: '\0', n: sizeof(s1_3)); |
84 | std::memset(s: s1_4, c: '\0', n: sizeof(s1_4)); |
85 | { |
86 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
87 | test_oarchive oa(os, TEST_ARCHIVE_FLAGS); |
88 | oa << boost::serialization::make_nvp( |
89 | n: "s1" , |
90 | v: boost::serialization::make_binary_object( |
91 | t: s1, |
92 | size: sizeof(s1) |
93 | ) |
94 | ); |
95 | oa << boost::serialization::make_nvp( |
96 | n: "s2" , |
97 | v: boost::serialization::make_binary_object( |
98 | t: s2, |
99 | size: sizeof(s2) |
100 | ) |
101 | ); |
102 | oa << boost::serialization::make_nvp( |
103 | n: "s3" , |
104 | v: boost::serialization::make_binary_object( |
105 | t: s3, |
106 | size: sizeof(s3) |
107 | ) |
108 | ); |
109 | oa << boost::serialization::make_nvp( |
110 | n: "s4" , |
111 | v: boost::serialization::make_binary_object( |
112 | t: s4, |
113 | size: sizeof(s4) |
114 | ) |
115 | ); |
116 | oa << BOOST_SERIALIZATION_NVP(a); |
117 | // note: add a little bit on the end of the archive to detect |
118 | // failure of text mode binary. |
119 | oa << BOOST_SERIALIZATION_NVP(i); |
120 | } |
121 | { |
122 | test_istream is(testfile, TEST_STREAM_FLAGS); |
123 | test_iarchive ia(is, TEST_ARCHIVE_FLAGS); |
124 | ia >> boost::serialization::make_nvp( |
125 | n: "s1" , |
126 | v: boost::serialization::make_binary_object( |
127 | t: s1_1, |
128 | size: sizeof(s1) |
129 | ) |
130 | ); |
131 | ia >> boost::serialization::make_nvp( |
132 | n: "s2" , |
133 | v: boost::serialization::make_binary_object( |
134 | t: s1_2, |
135 | size: sizeof(s2) |
136 | ) |
137 | ); |
138 | ia >> boost::serialization::make_nvp( |
139 | n: "s3" , |
140 | v: boost::serialization::make_binary_object( |
141 | t: s1_3, |
142 | size: sizeof(s3) |
143 | ) |
144 | ); |
145 | ia >> boost::serialization::make_nvp( |
146 | n: "s4" , |
147 | v: boost::serialization::make_binary_object( |
148 | t: s1_4, |
149 | size: sizeof(s4) |
150 | ) |
151 | ); |
152 | ia >> BOOST_SERIALIZATION_NVP(a1); |
153 | // note: add a little bit on the end of the archive to detect |
154 | // failure of text mode binary. |
155 | ia >> BOOST_SERIALIZATION_NVP(i1); |
156 | } |
157 | BOOST_CHECK(0 == std::strcmp(s1, s1_1)); |
158 | BOOST_CHECK(0 == std::strcmp(s2, s1_2)); |
159 | BOOST_CHECK(0 == std::strcmp(s3, s1_3)); |
160 | BOOST_CHECK(0 == std::strcmp(s4, s1_4)); |
161 | BOOST_CHECK(a == a1); |
162 | BOOST_CHECK(i == i1); |
163 | std::remove(filename: testfile); |
164 | return EXIT_SUCCESS; |
165 | } |
166 | |
167 | // EOF |
168 | |