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