Adding cereal lib
[platform/upstream/iotivity.git] / extlibs / cereal / unittests / tuple.cpp
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 #include "common.hpp"
28 #include <boost/test/unit_test.hpp>
29
30 template <class IArchive, class OArchive>
31 void test_tuple()
32 {
33   std::random_device rd;
34   std::mt19937 gen(rd());
35
36   auto rng = [&](){ return random_value<int>(gen); };
37
38   for(int ii=0; ii<100; ++ii)
39   {
40     auto o_podtuple = std::make_tuple( rng(), rng(), rng(), rng() );
41     auto o_isertuple = std::make_tuple( StructInternalSerialize( rng(), rng() ),
42         StructInternalSerialize( rng(), rng() ),
43         StructInternalSerialize( rng(), rng() ),
44         StructInternalSerialize( rng(), rng() ) );
45     auto o_ispltuple = std::make_tuple( StructInternalSplit( rng(), rng() ),
46         StructInternalSplit( rng(), rng() ),
47         StructInternalSplit( rng(), rng() ),
48         StructInternalSplit( rng(), rng() ) );
49     auto o_esertuple = std::make_tuple( StructExternalSerialize( rng(), rng() ),
50         StructExternalSerialize( rng(), rng() ),
51         StructExternalSerialize( rng(), rng() ),
52         StructExternalSerialize( rng(), rng() ) );
53     auto o_espltuple = std::make_tuple( StructExternalSerialize( rng(), rng() ),
54         StructExternalSerialize( rng(), rng() ),
55         StructExternalSerialize( rng(), rng() ),
56         StructExternalSerialize( rng(), rng() ) );
57
58     std::ostringstream os;
59     {
60       OArchive oar(os);
61
62       oar(o_podtuple);
63       oar(o_isertuple);
64       oar(o_ispltuple);
65       oar(o_esertuple);
66       oar(o_espltuple);
67     }
68
69     decltype( o_podtuple  ) i_podtuple;
70     decltype( o_isertuple ) i_isertuple;
71     decltype( o_ispltuple ) i_ispltuple;
72     decltype( o_esertuple ) i_esertuple;
73     decltype( o_espltuple ) i_espltuple;
74
75     std::istringstream is(os.str());
76     {
77       IArchive iar(is);
78
79       iar(i_podtuple);
80       iar(i_isertuple);
81       iar(i_ispltuple);
82       iar(i_esertuple);
83       iar(i_espltuple);
84     }
85
86     BOOST_CHECK_EQUAL( i_podtuple == o_podtuple, true );
87     BOOST_CHECK_EQUAL( i_isertuple == o_isertuple, true );
88     BOOST_CHECK_EQUAL( i_ispltuple == o_ispltuple, true );
89     BOOST_CHECK_EQUAL( i_esertuple == o_esertuple, true );
90     BOOST_CHECK_EQUAL( i_espltuple == o_espltuple, true );
91   }
92 }
93
94 BOOST_AUTO_TEST_CASE( binary_tuple )
95 {
96   test_tuple<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
97 }
98
99 BOOST_AUTO_TEST_CASE( portable_binary_tuple )
100 {
101   test_tuple<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
102 }
103
104 BOOST_AUTO_TEST_CASE( xml_tuple )
105 {
106   test_tuple<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
107 }
108
109 BOOST_AUTO_TEST_CASE( json_tuple )
110 {
111   test_tuple<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
112 }
113