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>
test_chrono()31 void test_chrono()
32 {
33 for(int ii=0; ii<100; ++ii)
34 {
35 auto o_timePoint1 = std::chrono::system_clock::now();
36 #ifndef CEREAL_OLDER_GCC
37 auto o_timePoint2 = std::chrono::steady_clock::now();
38 #endif // CEREAL_OLDER_GCC
39 auto o_timePoint3 = std::chrono::high_resolution_clock::now();
40
41 auto o_duration1 = std::chrono::system_clock::now() - o_timePoint1;
42 #ifndef CEREAL_OLDER_GCC
43 auto o_duration2 = std::chrono::steady_clock::now() - o_timePoint2;
44 #endif // CEREAL_OLDER_GCC
45 auto o_duration3 = std::chrono::high_resolution_clock::now() - o_timePoint3;
46
47 std::ostringstream os;
48 {
49 OArchive oar(os);
50
51 oar(o_timePoint1);
52 #ifndef CEREAL_OLDER_GCC
53 oar(o_timePoint2);
54 #endif // CEREAL_OLDER_GCC
55 oar(o_timePoint3);
56 oar(o_duration1);
57 #ifndef CEREAL_OLDER_GCC
58 oar(o_duration2);
59 #endif // CEREAL_OLDER_GCC
60 oar(o_duration3);
61 }
62
63 decltype(o_timePoint1) i_timePoint1;
64 #ifndef CEREAL_OLDER_GCC
65 decltype(o_timePoint2) i_timePoint2;
66 #endif // CEREAL_OLDER_GCC
67 decltype(o_timePoint3) i_timePoint3;
68 decltype(o_duration1) i_duration1;
69 #ifndef CEREAL_OLDER_GCC
70 decltype(o_duration2) i_duration2;
71 #endif // CEREAL_OLDER_GCC
72 decltype(o_duration3) i_duration3;
73
74 std::istringstream is(os.str());
75 {
76 IArchive iar(is);
77
78 iar(i_timePoint1);
79 #ifndef CEREAL_OLDER_GCC
80 iar(i_timePoint2);
81 #endif // CEREAL_OLDER_GCC
82 iar(i_timePoint3);
83 iar(i_duration1);
84 #ifndef CEREAL_OLDER_GCC
85 iar(i_duration2);
86 #endif // CEREAL_OLDER_GCC
87 iar(i_duration3);
88 }
89
90 BOOST_CHECK( o_timePoint1 == i_timePoint1 );
91 #ifndef CEREAL_OLDER_GCC
92 BOOST_CHECK( o_timePoint2 == i_timePoint2 );
93 #endif // CEREAL_OLDER_GCC
94 BOOST_CHECK( o_timePoint3 == i_timePoint3 );
95 BOOST_CHECK( o_duration1 == i_duration1 );
96 #ifndef CEREAL_OLDER_GCC
97 BOOST_CHECK( o_duration2 == i_duration2 );
98 #endif // CEREAL_OLDER_GCC
99 BOOST_CHECK( o_duration3 == i_duration3 );
100 }
101 }
102
BOOST_AUTO_TEST_CASE(binary_chrono)103 BOOST_AUTO_TEST_CASE( binary_chrono )
104 {
105 test_chrono<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
106 }
107
BOOST_AUTO_TEST_CASE(portable_binary_chrono)108 BOOST_AUTO_TEST_CASE( portable_binary_chrono )
109 {
110 test_chrono<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
111 }
112
BOOST_AUTO_TEST_CASE(xml_chrono)113 BOOST_AUTO_TEST_CASE( xml_chrono )
114 {
115 test_chrono<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
116 }
117
BOOST_AUTO_TEST_CASE(json_chrono)118 BOOST_AUTO_TEST_CASE( json_chrono )
119 {
120 test_chrono<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
121 }
122
123
124