1 #ifndef BOOST_SERIALIZATION_TEST_A_HPP
2 #define BOOST_SERIALIZATION_TEST_A_HPP
3
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER)
6 # pragma once
7 #endif
8
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // A.hpp simple class test
11
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
13 // Use, modification and distribution is subject to the Boost Software
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
16
17 // See http://www.boost.org for updates, documentation, and revision history.
18
19 #include <cassert>
20 #include <cstdlib> // for rand()
21 #include <cmath> // for fabs()
22 #include <cstddef> // size_t
23 #include <boost/math/special_functions/next.hpp>
24
25 #include <boost/config.hpp>
26 #if defined(BOOST_NO_STDC_NAMESPACE)
27 namespace std{
28 using ::rand;
29 using ::fabs;
30 using ::size_t;
31 }
32 #endif
33
34 //#include <boost/test/test_exec_monitor.hpp>
35 #include <boost/limits.hpp>
36 #include <boost/cstdint.hpp>
37
38 #include <boost/detail/workaround.hpp>
39 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
40 #include <boost/archive/dinkumware.hpp>
41 #endif
42
43 #include <boost/serialization/nvp.hpp>
44 #include <boost/serialization/string.hpp>
45 #include <boost/serialization/access.hpp>
46
47 class A
48 {
49 private:
50 friend class boost::serialization::access;
51 // note: from an aesthetic perspective, I would much prefer to have this
52 // defined out of line. Unfortunately, this trips a bug in the VC 6.0
53 // compiler. So hold our nose and put it her to permit running of tests.
54 template<class Archive>
serialize(Archive & ar,const unsigned int)55 void serialize(
56 Archive &ar,
57 const unsigned int /* file_version */
58 ){
59 ar & BOOST_SERIALIZATION_NVP(b);
60 #ifndef BOOST_NO_INT64_T
61 ar & BOOST_SERIALIZATION_NVP(f);
62 ar & BOOST_SERIALIZATION_NVP(g);
63 #endif
64 #if BOOST_WORKAROUND(BOOST_BORLANDC, <= 0x551 )
65 int i;
66 if(BOOST_DEDUCED_TYPENAME Archive::is_saving::value){
67 i = l;
68 ar & BOOST_SERIALIZATION_NVP(i);
69 }
70 else{
71 ar & BOOST_SERIALIZATION_NVP(i);
72 l = i;
73 }
74 #else
75 ar & BOOST_SERIALIZATION_NVP(l);
76 #endif
77 ar & BOOST_SERIALIZATION_NVP(m);
78 ar & BOOST_SERIALIZATION_NVP(n);
79 ar & BOOST_SERIALIZATION_NVP(o);
80 ar & BOOST_SERIALIZATION_NVP(p);
81 ar & BOOST_SERIALIZATION_NVP(q);
82 #ifndef BOOST_NO_CWCHAR
83 ar & BOOST_SERIALIZATION_NVP(r);
84 #endif
85 ar & BOOST_SERIALIZATION_NVP(c);
86 ar & BOOST_SERIALIZATION_NVP(s);
87 ar & BOOST_SERIALIZATION_NVP(t);
88 ar & BOOST_SERIALIZATION_NVP(u);
89 ar & BOOST_SERIALIZATION_NVP(v);
90 ar & BOOST_SERIALIZATION_NVP(w);
91 ar & BOOST_SERIALIZATION_NVP(x);
92 ar & BOOST_SERIALIZATION_NVP(y);
93 #ifndef BOOST_NO_STD_WSTRING
94 ar & BOOST_SERIALIZATION_NVP(z);
95 #endif
96 }
97 bool b;
98 #ifndef BOOST_NO_INT64_T
99 boost::int64_t f;
100 boost::uint64_t g;
101 #endif
102 enum h {
103 i = 0,
104 j,
105 k
106 } l;
107 std::size_t m;
108 signed long n;
109 unsigned long o;
110 signed short p;
111 unsigned short q;
112 #ifndef BOOST_NO_CWCHAR
113 wchar_t r;
114 #endif
115 char c;
116 signed char s;
117 unsigned char t;
118 signed int u;
119 unsigned int v;
120 float w;
121 double x;
122 std::string y;
123 #ifndef BOOST_NO_STD_WSTRING
124 std::wstring z;
125 #endif
126 public:
127 A();
128 bool operator==(const A &rhs) const;
129 bool operator!=(const A &rhs) const;
130 bool operator<(const A &rhs) const; // used by less
131 // hash function for class A
132 operator std::size_t () const;
133 friend std::ostream & operator<<(std::ostream & os, A const & a);
134 friend std::istream & operator>>(std::istream & is, A & a);
135 };
136
137 //BOOST_TEST_DONT_PRINT_LOG_VALUE(A);
138
139 template<class S>
randomize(S & x)140 void randomize(S &x)
141 {
142 assert(0 == x.size());
143 for(;;){
144 unsigned int i = std::rand() % 27;
145 if(0 == i)
146 break;
147 x += static_cast<BOOST_DEDUCED_TYPENAME S::value_type>('a' - 1 + i);
148 }
149 }
150
151 template<class T>
accumulate(std::size_t & s,const T & t)152 void accumulate(std::size_t & s, const T & t){
153 const char * tptr = (const char *)(& t);
154 unsigned int count = sizeof(t);
155 while(count-- > 0){
156 s += *tptr++;
157 }
158 }
159
operator std::size_t() const160 A::operator std::size_t () const {
161 std::size_t retval = 0;
162 accumulate(retval, b);
163 #ifndef BOOST_NO_INT64_T
164 accumulate(retval, f);
165 accumulate(retval, g);
166 #endif
167 accumulate(retval, l);
168 accumulate(retval, m);
169 accumulate(retval, n);
170 accumulate(retval, o);
171 accumulate(retval, p);
172 accumulate(retval, q);
173 #ifndef BOOST_NO_CWCHAR
174 accumulate(retval, r);
175 #endif
176 accumulate(retval, c);
177 accumulate(retval, s);
178 accumulate(retval, t);
179 accumulate(retval, u);
180 accumulate(retval, v);
181 return retval;
182 }
183
A()184 inline A::A() :
185 b(true),
186 #ifndef BOOST_NO_INT64_T
187 f(std::rand() * std::rand()),
188 g(std::rand() * std::rand()),
189 #endif
190 l(static_cast<enum h>(std::rand() % 3)),
191 m(std::rand()),
192 n(std::rand()),
193 o(std::rand()),
194 p(std::rand()),
195 q(std::rand()),
196 #ifndef BOOST_NO_CWCHAR
197 r(std::rand()),
198 #endif
199 c(std::rand()),
200 s(std::rand()),
201 t(std::rand()),
202 u(std::rand()),
203 v(std::rand()),
204 w((float)std::rand()),
205 x((double)std::rand())
206 {
207 randomize(y);
208 #ifndef BOOST_NO_STD_WSTRING
209 randomize(z);
210 #endif
211 }
212
operator ==(const A & rhs) const213 inline bool A::operator==(const A &rhs) const
214 {
215 if(b != rhs.b)
216 return false;
217 if(l != rhs.l)
218 return false;
219 #ifndef BOOST_NO_INT64_T
220 if(f != rhs.f)
221 return false;
222 if(g != rhs.g)
223 return false;
224 #endif
225 if(m != rhs.m)
226 return false;
227 if(n != rhs.n)
228 return false;
229 if(o != rhs.o)
230 return false;
231 if(p != rhs.p)
232 return false;
233 if(q != rhs.q)
234 return false;
235 #ifndef BOOST_NO_CWCHAR
236 if(r != rhs.r)
237 return false;
238 #endif
239 if(c != rhs.c)
240 return false;
241 if(s != rhs.s)
242 return false;
243 if(t != rhs.t)
244 return false;
245 if(u != rhs.u)
246 return false;
247 if(v != rhs.v)
248 return false;
249 if(std::abs( boost::math::float_distance(w, rhs.w)) > 1)
250 return false;
251 if(std::abs( boost::math::float_distance(x, rhs.x)) > 1)
252 return false;
253 if(0 != y.compare(rhs.y))
254 return false;
255 #ifndef BOOST_NO_STD_WSTRING
256 if(0 != z.compare(rhs.z))
257 return false;
258 #endif
259 return true;
260 }
261
operator !=(const A & rhs) const262 inline bool A::operator!=(const A &rhs) const
263 {
264 return ! (*this == rhs);
265 }
266
operator <(const A & rhs) const267 inline bool A::operator<(const A &rhs) const
268 {
269 if(b != rhs.b)
270 return b < rhs.b;
271 #ifndef BOOST_NO_INT64_T
272 if(f != rhs.f)
273 return f < rhs.f;
274 if(g != rhs.g)
275 return g < rhs.g;
276 #endif
277 if(l != rhs.l )
278 return l < rhs.l;
279 if(m != rhs.m )
280 return m < rhs.m;
281 if(n != rhs.n )
282 return n < rhs.n;
283 if(o != rhs.o )
284 return o < rhs.o;
285 if(p != rhs.p )
286 return p < rhs.p;
287 if(q != rhs.q )
288 return q < rhs.q;
289 #ifndef BOOST_NO_CWCHAR
290 if(r != rhs.r )
291 return r < rhs.r;
292 #endif
293 if(c != rhs.c )
294 return c < rhs.c;
295 if(s != rhs.s )
296 return s < rhs.s;
297 if(t != rhs.t )
298 return t < rhs.t;
299 if(u != rhs.u )
300 return u < rhs.u;
301 if(v != rhs.v )
302 return v < rhs.v;
303 if(w != rhs.w )
304 return w < rhs.w;
305 if(x != rhs.x )
306 return x < rhs.x;
307 int i = y.compare(rhs.y);
308 if(i != 0 )
309 return i < 0;
310 #ifndef BOOST_NO_STD_WSTRING
311 int j = z.compare(rhs.z);
312 if(j != 0 )
313 return j < 0;
314 #endif
315 return false;
316 }
317
318 #endif // BOOST_SERIALIZATION_TEST_A_HPP
319