testtime_serialize.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright (c) 2002-2005 CrystalClear Software, Inc.
  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. * Author: Jeff Garland, Bart Garst
  6. */
  7. #include <boost/archive/text_oarchive.hpp>
  8. #include <boost/archive/text_iarchive.hpp>
  9. #include <boost/archive/xml_oarchive.hpp>
  10. #include <boost/archive/xml_iarchive.hpp>
  11. #include <boost/date_time/posix_time/posix_time.hpp>
  12. #include <boost/date_time/posix_time/time_serialize.hpp>
  13. #include "../testfrmwk.hpp"
  14. #include <sstream>
  15. using namespace boost;
  16. using namespace posix_time;
  17. using namespace gregorian;
  18. template<class archive_type, class temporal_type>
  19. void save_to(archive_type& ar, const temporal_type& tt)
  20. {
  21. ar << tt;
  22. }
  23. int main(){
  24. // originals
  25. date d(2002, Feb, 14);
  26. #if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
  27. time_duration td(12,13,52,123456789);
  28. #else
  29. time_duration td(12,13,52,123456);
  30. #endif
  31. ptime pt(d, td);
  32. time_period tp(pt, ptime(date(2002, Oct, 31), hours(19)));
  33. ptime sv_pt1(not_a_date_time);
  34. ptime sv_pt2(pos_infin);
  35. time_duration sv_td(neg_infin);
  36. // for loading in from archive
  37. date d2(not_a_date_time);
  38. time_duration td2(1,0,0);
  39. ptime pt2(d2, td2);
  40. time_period tp2(pt2, hours(1));
  41. ptime sv_pt3(min_date_time);
  42. ptime sv_pt4(min_date_time);
  43. time_duration sv_td2(0,0,0);
  44. std::ostringstream oss;
  45. {
  46. // NOTE: DATE_TIME_XML_SERIALIZE is only used in testing and is
  47. // defined in the testing Jamfile
  48. #if defined(DATE_TIME_XML_SERIALIZE)
  49. std::cout << "Running xml archive tests" << std::endl;
  50. archive::xml_oarchive oa(oss);
  51. #else
  52. std::cout << "Running text archive tests" << std::endl;
  53. archive::text_oarchive oa(oss);
  54. #endif // DATE_TIME_XML_SERIALIZE
  55. try{
  56. #if defined(DATE_TIME_XML_SERIALIZE)
  57. save_to(oa, BOOST_SERIALIZATION_NVP(pt));
  58. save_to(oa, BOOST_SERIALIZATION_NVP(sv_pt1));
  59. save_to(oa, BOOST_SERIALIZATION_NVP(sv_pt2));
  60. save_to(oa, BOOST_SERIALIZATION_NVP(tp));
  61. save_to(oa, BOOST_SERIALIZATION_NVP(td));
  62. save_to(oa, BOOST_SERIALIZATION_NVP(sv_td));
  63. #else
  64. save_to(oa, pt);
  65. save_to(oa, sv_pt1);
  66. save_to(oa, sv_pt2);
  67. save_to(oa, tp);
  68. save_to(oa, td);
  69. save_to(oa, sv_td);
  70. #endif // DATE_TIME_XML_SERIALIZE
  71. }catch(archive::archive_exception& ae){
  72. std::string s(ae.what());
  73. check("Error writing to archive: " + s + "\nWritten data: \"" + oss.str() + "\"", false);
  74. return printTestStats();
  75. }
  76. }
  77. {
  78. std::istringstream iss(oss.str());
  79. #if defined(DATE_TIME_XML_SERIALIZE)
  80. archive::xml_iarchive ia(iss);
  81. #else
  82. archive::text_iarchive ia(iss);
  83. #endif // DATE_TIME_XML_SERIALIZE
  84. try{
  85. #if defined(DATE_TIME_XML_SERIALIZE)
  86. ia >> BOOST_SERIALIZATION_NVP(pt2);
  87. ia >> BOOST_SERIALIZATION_NVP(sv_pt3);
  88. ia >> BOOST_SERIALIZATION_NVP(sv_pt4);
  89. ia >> BOOST_SERIALIZATION_NVP(tp2);
  90. ia >> BOOST_SERIALIZATION_NVP(td2);
  91. ia >> BOOST_SERIALIZATION_NVP(sv_td2);
  92. #else
  93. ia >> pt2;
  94. ia >> sv_pt3;
  95. ia >> sv_pt4;
  96. ia >> tp2;
  97. ia >> td2;
  98. ia >> sv_td2;
  99. #endif // DATE_TIME_XML_SERIALIZE
  100. }catch(archive::archive_exception& ae){
  101. std::string s(ae.what());
  102. check("Error readng from archive: " + s + "\nWritten data: \"" + oss.str() + "\"", false);
  103. return printTestStats();
  104. }
  105. }
  106. check("ptime", pt == pt2);
  107. check("special_values ptime (nadt)", sv_pt1 == sv_pt3);
  108. check("special_values ptime (pos_infin)", sv_pt2 == sv_pt4);
  109. check("time_period", tp == tp2);
  110. check("time_duration", td == td2);
  111. check("special_values time_duration (neg_infin)", sv_td == sv_td2);
  112. return printTestStats();
  113. }