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