1 /*
2   Copyright (c) 2014, Randolph Voorhies, Shane Grant
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7       * Redistributions of source code must retain the above copyright
8         notice, this list of conditions and the following disclaimer.
9       * Redistributions in binary form must reproduce the above copyright
10         notice, this list of conditions and the following disclaimer in the
11         documentation and/or other materials provided with the distribution.
12       * Neither the name of cereal nor the
13         names of its contributors may be used to endorse or promote products
14         derived from this software without specific prior written permission.
15 
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES AND SHANE GRANT BE LIABLE FOR ANY
20   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #ifndef CEREAL_TEST_UNORDERED_LOADS_H_
28 #define CEREAL_TEST_UNORDERED_LOADS_H_
29 #include "common.hpp"
30 
31 struct unordered_naming
32 {
33   int x;
34   int xx;
35   int y;
36   int z;
37 
38   template <class Archive>
saveunordered_naming39   void save( Archive & ar ) const
40   {
41     ar( CEREAL_NVP(x),
42         CEREAL_NVP(z),
43         CEREAL_NVP(y),
44         CEREAL_NVP(xx) );
45   }
46 
47   template <class Archive>
loadunordered_naming48   void load( Archive & ar )
49   {
50     ar( x,
51         CEREAL_NVP(y),
52         CEREAL_NVP(z),
53         CEREAL_NVP(xx) );
54   }
55 
operator ==unordered_naming56   bool operator==( unordered_naming const & other ) const
57   {
58     return x == other.x && xx == other.xx && y == other.y && z == other.z;
59   }
60 };
61 
operator <<(std::ostream & os,unordered_naming const & s)62 std::ostream& operator<<(std::ostream& os, unordered_naming const & s)
63 {
64   os << "[x: " << s.x << " xx: " << s.xx << " y: " << s.y << " z: " << s.z << "]";
65   return os;
66 }
67 
68 template <class IArchive, class OArchive> inline
test_unordered_loads()69 void test_unordered_loads()
70 {
71   std::random_device rd;
72   std::mt19937 gen(rd());
73 
74   auto rngB = [&](){ return random_value<int>( gen ) % 2 == 0; };
75   auto rngI = [&](){ return random_value<int>( gen ); };
76   auto rngF = [&](){ return random_value<float>( gen ); };
77   auto rngD = [&](){ return random_value<double>( gen ); };
78   auto rngS = [&](){ return random_basic_string<char>( gen ); };
79 
80   for(int ii=0; ii<100; ++ii)
81   {
82     auto const name1 = rngS();
83     auto const name2 = rngS();
84     auto const name3 = rngS();
85     auto const name4 = rngS();
86     auto const name5 = rngS();
87     auto const name6 = rngS();
88     auto const name7 = rngS();
89 
90     int o_int1 = rngI();
91     double o_double2 = rngD();
92     std::vector<bool> o_vecbool3 = { rngB(), rngB(), rngB(), rngB(), rngB() };
93     int o_int4 = rngI();
94     int o_int5 = rngI();
95     int o_int6 = rngI();
96     std::pair<float, unordered_naming> o_un7;
97     o_un7.first = rngF();
98     o_un7.second.x = rngI();
99     o_un7.second.xx = rngI();
100     o_un7.second.y = rngI();
101     o_un7.second.z = rngI();
102 
103     std::ostringstream os;
104     {
105       OArchive oar(os);
106 
107       oar( cereal::make_nvp( name1, o_int1 ),
108            cereal::make_nvp( name2, o_double2 ),
109            cereal::make_nvp( name3, o_vecbool3 ),
110            cereal::make_nvp( name4, o_int4 ),
111            cereal::make_nvp( name5, o_int5 ),
112            cereal::make_nvp( name6, o_int6 ),
113            cereal::make_nvp( name7, o_un7 ) );
114     }
115 
116     decltype(o_int1) i_int1;
117     decltype(o_double2) i_double2;
118     decltype(o_vecbool3) i_vecbool3;
119     decltype(o_int4) i_int4;
120     decltype(o_int5) i_int5;
121     decltype(o_int6) i_int6;
122     decltype(o_un7) i_un7;
123 
124     std::istringstream is(os.str());
125     {
126       IArchive iar(is);
127 
128       iar( cereal::make_nvp( name7, i_un7 ),
129            cereal::make_nvp( name2, i_double2 ),
130            cereal::make_nvp( name4, i_int4 ),
131            cereal::make_nvp( name3, i_vecbool3 ),
132            cereal::make_nvp( name1, i_int1 ),
133            cereal::make_nvp( name5, i_int5 ),
134            i_int6 );
135     }
136 
137     CHECK_EQ(o_int1, i_int1);
138     CHECK_EQ(o_double2, doctest::Approx(o_double2).epsilon(1e-5));
139     CHECK_EQ(o_vecbool3.size(), i_vecbool3.size());
140     check_collection(i_vecbool3, o_vecbool3);
141     CHECK_EQ(o_int4, i_int4);
142     CHECK_EQ(o_int5, i_int5);
143     CHECK_EQ(o_int6, i_int6);
144     CHECK_EQ(o_un7.first, i_un7.first);
145     CHECK_EQ(o_un7.second, i_un7.second);
146   }
147 }
148 
149 #endif // CEREAL_TEST_UNORDERED_LOADS_H_
150