testtime_serialize_versioning.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include <boost/archive/binary_oarchive.hpp>
  7. #include <boost/archive/binary_iarchive.hpp>
  8. #include <boost/date_time/posix_time/posix_time.hpp>
  9. #include <boost/date_time/posix_time/time_serialize.hpp>
  10. #include <boost/lexical_cast.hpp>
  11. #include "../testfrmwk.hpp"
  12. #include <fstream>
  13. using namespace boost;
  14. using namespace posix_time;
  15. void check_filesize(const std::string& filename, std::ifstream::pos_type expectedSize)
  16. {
  17. std::ifstream in(filename.c_str(), std::ifstream::ate | std::ifstream::binary);
  18. check_equal("check file size is " + boost::lexical_cast<std::string>(expectedSize), in.tellg(), expectedSize);
  19. }
  20. std::string get_fname(int version)
  21. {
  22. return "time_duration_serialization." +
  23. std::string((sizeof(size_t) == 4) ? "x32" : "x64") +
  24. ".v" + boost::lexical_cast<std::string>(version);
  25. }
  26. int main() {
  27. time_duration td(12, 13, 52, 123456);
  28. #if BOOST_DATE_TIME_POSIX_TIME_DURATION_VERSION == 0
  29. std::ofstream ofs(get_fname(0).c_str(), std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
  30. boost::archive::binary_oarchive oa(ofs);
  31. oa << td;
  32. ofs.close();
  33. #if defined(_MSC_VER)
  34. check_filesize(get_fname(0), 58 + sizeof(size_t));
  35. #endif
  36. #else // BOOST_DATE_TIME_POSIX_TIME_DURATION_VERSION > 0
  37. std::ifstream ifs(get_fname(0).c_str(), std::ios_base::binary | std::ios_base::in);
  38. boost::archive::binary_iarchive ia(ifs);
  39. time_duration tmp;
  40. ia >> tmp;
  41. ifs.close();
  42. check_equal("read older version structure ok", td, tmp);
  43. std::ofstream ofs(get_fname(1).c_str(), std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
  44. boost::archive::binary_oarchive oa(ofs);
  45. oa << td;
  46. ofs.close();
  47. #if defined(_MSC_VER)
  48. check_filesize(get_fname(1), 70 + sizeof(size_t));
  49. #endif
  50. #endif // BOOST_DATE_TIME_POSIX_TIME_DURATION_VERSION
  51. return printTestStats();
  52. }