Brian Silverman | 8867871 | 2018-08-04 23:56:48 -0700 | [diff] [blame^] | 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 | |
| 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_exec_monitor.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(BOOST_DEDUCED_TYPENAME 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<BOOST_DEDUCED_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(std::abs( boost::math::float_distance(w, rhs.w)) > 1) |
| 249 | return false; |
| 250 | if(std::abs( boost::math::float_distance(x, rhs.x)) > 1) |
| 251 | return false; |
| 252 | if(0 != y.compare(rhs.y)) |
| 253 | return false; |
| 254 | #ifndef BOOST_NO_STD_WSTRING |
| 255 | if(0 != z.compare(rhs.z)) |
| 256 | return false; |
| 257 | #endif |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | inline bool A::operator!=(const A &rhs) const |
| 262 | { |
| 263 | return ! (*this == rhs); |
| 264 | } |
| 265 | |
| 266 | inline bool A::operator<(const A &rhs) const |
| 267 | { |
| 268 | if(b != rhs.b) |
| 269 | return b < rhs.b; |
| 270 | #ifndef BOOST_NO_INT64_T |
| 271 | if(f != rhs.f) |
| 272 | return f < rhs.f; |
| 273 | if(g != rhs.g) |
| 274 | return g < rhs.g; |
| 275 | #endif |
| 276 | if(l != rhs.l ) |
| 277 | return l < rhs.l; |
| 278 | if(m != rhs.m ) |
| 279 | return m < rhs.m; |
| 280 | if(n != rhs.n ) |
| 281 | return n < rhs.n; |
| 282 | if(o != rhs.o ) |
| 283 | return o < rhs.o; |
| 284 | if(p != rhs.p ) |
| 285 | return p < rhs.p; |
| 286 | if(q != rhs.q ) |
| 287 | return q < rhs.q; |
| 288 | #ifndef BOOST_NO_CWCHAR |
| 289 | if(r != rhs.r ) |
| 290 | return r < rhs.r; |
| 291 | #endif |
| 292 | if(c != rhs.c ) |
| 293 | return c < rhs.c; |
| 294 | if(s != rhs.s ) |
| 295 | return s < rhs.s; |
| 296 | if(t != rhs.t ) |
| 297 | return t < rhs.t; |
| 298 | if(u != rhs.u ) |
| 299 | return u < rhs.u; |
| 300 | if(v != rhs.v ) |
| 301 | return v < rhs.v; |
| 302 | if(w != rhs.w ) |
| 303 | return w < rhs.w; |
| 304 | if(x != rhs.x ) |
| 305 | return x < rhs.x; |
| 306 | int i = y.compare(rhs.y); |
| 307 | if(i != 0 ) |
| 308 | return i < 0; |
| 309 | #ifndef BOOST_NO_STD_WSTRING |
| 310 | int j = z.compare(rhs.z); |
| 311 | if(j != 0 ) |
| 312 | return j < 0; |
| 313 | #endif |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | #endif // BOOST_SERIALIZATION_TEST_A_HPP |