1 /* Copyright (c) 2017 James E. King III
2 * Use, modification and distribution is subject to the
3 * Boost Software License, Version 1.0. (See accompanying
4 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
5 */
6
7 #include <boost/archive/binary_oarchive.hpp>
8 #include <boost/archive/binary_iarchive.hpp>
9 #include <boost/date_time/posix_time/posix_time.hpp>
10 #include <boost/date_time/posix_time/time_serialize.hpp>
11 #include <boost/lexical_cast.hpp>
12 #include "../testfrmwk.hpp"
13 #include <fstream>
14
15 using namespace boost;
16 using namespace posix_time;
17
check_filesize(const std::string & filename,std::ifstream::pos_type expectedSize)18 void check_filesize(const std::string& filename, std::ifstream::pos_type expectedSize)
19 {
20 std::ifstream in(filename.c_str(), std::ifstream::ate | std::ifstream::binary);
21 check_equal("check file size is " + boost::lexical_cast<std::string>(expectedSize), in.tellg(), expectedSize);
22 }
23
get_fname(int version)24 std::string get_fname(int version)
25 {
26 return "time_duration_serialization." +
27 std::string((sizeof(size_t) == 4) ? "x32" : "x64") +
28 ".v" + boost::lexical_cast<std::string>(version);
29 }
30
main()31 int main() {
32 time_duration td(12, 13, 52, 123456);
33
34 #if BOOST_DATE_TIME_POSIX_TIME_DURATION_VERSION == 0
35 std::ofstream ofs(get_fname(0).c_str(), std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
36 boost::archive::binary_oarchive oa(ofs);
37 oa << td;
38 ofs.close();
39
40 #if defined(_MSC_VER)
41 check_filesize(get_fname(0), 58 + sizeof(size_t));
42 #endif
43
44 #else // BOOST_DATE_TIME_POSIX_TIME_DURATION_VERSION > 0
45 std::ifstream ifs(get_fname(0).c_str(), std::ios_base::binary | std::ios_base::in);
46 boost::archive::binary_iarchive ia(ifs);
47 time_duration tmp;
48 ia >> tmp;
49 ifs.close();
50 check_equal("read older version structure ok", td, tmp);
51
52 std::ofstream ofs(get_fname(1).c_str(), std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
53 boost::archive::binary_oarchive oa(ofs);
54 oa << td;
55 ofs.close();
56
57 #if defined(_MSC_VER)
58 check_filesize(get_fname(1), 70 + sizeof(size_t));
59 #endif
60
61 #endif // BOOST_DATE_TIME_POSIX_TIME_DURATION_VERSION
62
63 return printTestStats();
64 }
65