Skip to content

Commit 8ab5c1c

Browse files
committed
Unit tests for #216
1 parent d2bfaa9 commit 8ab5c1c

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

unittests/load_construct.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,38 @@ std::ostream& operator<<(std::ostream& os, OneLA const & s)
5555
return os;
5656
}
5757

58+
struct OneLAVersioned
59+
{
60+
OneLAVersioned( int xx ) : x( xx ), v() {}
61+
OneLAVersioned( int xx, int vv ) : x( xx ), v( vv ) {}
62+
63+
int x;
64+
std::uint32_t v;
65+
66+
template <class Archive>
67+
void serialize( Archive & ar, const std::uint32_t version )
68+
{ ar( x ); v = version; }
69+
70+
template <class Archive>
71+
static void load_and_construct( Archive & ar, cereal::construct<OneLAVersioned> & construct, const std::uint32_t version )
72+
{
73+
int xx;
74+
ar( xx );
75+
construct( xx, version );
76+
}
77+
78+
bool operator==( OneLAVersioned const & other ) const
79+
{ return x == other.x; }
80+
};
81+
82+
std::ostream& operator<<(std::ostream& os, OneLAVersioned const & s)
83+
{
84+
os << "[" << s.x << "]";
85+
return os;
86+
}
87+
88+
CEREAL_CLASS_VERSION( OneLAVersioned, 13 )
89+
5890
struct TwoLA
5991
{
6092
TwoLA( int xx ) : x( xx ) {}
@@ -90,6 +122,45 @@ namespace cereal
90122
};
91123
}
92124

125+
struct TwoLAVersioned
126+
{
127+
TwoLAVersioned( int xx ) : x( xx ), v() {}
128+
TwoLAVersioned( int xx, int vv ) : x( xx ), v( vv ) {}
129+
130+
int x;
131+
std::uint32_t v;
132+
133+
template <class Archive>
134+
void serialize( Archive & ar, const std::uint32_t version )
135+
{ ar( x ); v = version; }
136+
137+
bool operator==( TwoLAVersioned const & other ) const
138+
{ return x == other.x; }
139+
};
140+
141+
std::ostream& operator<<(std::ostream& os, TwoLAVersioned const & s)
142+
{
143+
os << "[" << s.x << "]";
144+
return os;
145+
}
146+
147+
namespace cereal
148+
{
149+
template <>
150+
struct LoadAndConstruct<TwoLAVersioned>
151+
{
152+
template <class Archive>
153+
static void load_and_construct( Archive & ar, cereal::construct<TwoLAVersioned> & construct, const std::uint32_t version )
154+
{
155+
int xx;
156+
ar( xx );
157+
construct( xx, version );
158+
}
159+
};
160+
}
161+
162+
CEREAL_CLASS_VERSION( TwoLAVersioned, 1 )
163+
93164
struct ThreeLA : std::enable_shared_from_this<ThreeLA>
94165
{
95166
ThreeLA( int xx ) : x( xx ) {}
@@ -131,6 +202,8 @@ void test_memory_load_construct()
131202
std::unique_ptr<OneLA> o_unique1( new OneLA( random_value<int>(gen) ) );
132203
std::unique_ptr<TwoLA> o_unique2( new TwoLA( random_value<int>(gen) ) );
133204
auto o_shared3 = std::make_shared<ThreeLA>( random_value<int>(gen) );
205+
auto o_shared1v = std::make_shared<OneLAVersioned>( random_value<int>(gen) );
206+
auto o_shared2v = std::make_shared<TwoLAVersioned>( random_value<int>(gen) );
134207

135208
std::ostringstream os;
136209
{
@@ -141,6 +214,8 @@ void test_memory_load_construct()
141214
oar( o_unique1 );
142215
oar( o_unique2 );
143216
oar( o_shared3 );
217+
oar( o_shared1v );
218+
oar( o_shared2v );
144219
}
145220

146221
o_shared3->shared_from_this(); // tests github issue #68
@@ -150,6 +225,8 @@ void test_memory_load_construct()
150225
decltype(o_unique1) i_unique1;
151226
decltype(o_unique2) i_unique2;
152227
decltype(o_shared3) i_shared3;
228+
decltype(o_shared1v) i_shared1v;
229+
decltype(o_shared2v) i_shared2v;
153230

154231
std::istringstream is(os.str());
155232
{
@@ -160,13 +237,19 @@ void test_memory_load_construct()
160237
iar( i_unique1 );
161238
iar( i_unique2 );
162239
iar( i_shared3 );
240+
iar( i_shared1v );
241+
iar( i_shared2v );
163242
}
164243

165244
BOOST_CHECK_EQUAL( *o_shared1, *i_shared1 );
166245
BOOST_CHECK_EQUAL( *o_shared2, *i_shared2 );
167246
BOOST_CHECK_EQUAL( *o_unique1, *i_unique1 );
168247
BOOST_CHECK_EQUAL( *o_unique2, *i_unique2 );
169248
BOOST_CHECK_EQUAL( *o_shared3, *i_shared3 );
249+
BOOST_CHECK_EQUAL( *o_shared1v, *i_shared1v );
250+
BOOST_CHECK_EQUAL(i_shared1v->v, 13u);
251+
BOOST_CHECK_EQUAL( *o_shared2v, *i_shared2v );
252+
BOOST_CHECK_EQUAL(i_shared2v->v, 1u);
170253

171254
auto i_shared3_2 = i_shared3->shared_from_this();
172255
BOOST_CHECK_EQUAL( *o_shared3, *i_shared3_2 );

0 commit comments

Comments
 (0)