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 | |
13 | #include <boost/date_time/gregorian/gregorian.hpp> |
14 | #include <boost/date_time/gregorian/greg_serialize.hpp> |
15 | #include "../testfrmwk.hpp" |
16 | #include <sstream> |
17 | |
18 | using namespace boost; |
19 | using namespace gregorian; |
20 | |
21 | template<class archive_type, class temporal_type> |
22 | void save_to(archive_type& ar, const temporal_type& tt) |
23 | { |
24 | ar << tt; |
25 | } |
26 | |
27 | int main(){ |
28 | date d(2002,Feb,12); |
29 | date sv_d1(not_a_date_time); |
30 | date sv_d2(pos_infin); |
31 | date_duration dd(11); |
32 | date_duration sv_dd(neg_infin); |
33 | date_period dp(d,dd); |
34 | greg_year gy(1959); |
35 | greg_month gm(Feb); |
36 | greg_day gd(14); |
37 | greg_weekday gwd(Friday); |
38 | partial_date pd(26,Jul); |
39 | nth_kday_of_month nkd(nth_kday_of_month::second,Tuesday,Mar); |
40 | first_kday_of_month fkd(Saturday,Apr); |
41 | last_kday_of_month lkd(Saturday,Apr); |
42 | first_kday_before fkdb(Thursday); |
43 | first_kday_after fkda(Thursday); |
44 | |
45 | std::ostringstream oss; |
46 | |
47 | { |
48 | |
49 | // NOTE: DATE_TIME_XML_SERIALIZE is only used in testing and is |
50 | // defined in the testing Jamfile |
51 | #if defined(DATE_TIME_XML_SERIALIZE) |
52 | std::cout << "Running xml archive tests" << std::endl; |
53 | archive::xml_oarchive oa(oss); |
54 | #else |
55 | std::cout << "Running text archive tests" << std::endl; |
56 | archive::text_oarchive oa(oss); |
57 | #endif |
58 | |
59 | // load up the archive |
60 | try{ |
61 | #if defined(DATE_TIME_XML_SERIALIZE) |
62 | save_to(oa, BOOST_SERIALIZATION_NVP(d)); |
63 | save_to(oa, BOOST_SERIALIZATION_NVP(sv_d1)); |
64 | save_to(oa, BOOST_SERIALIZATION_NVP(sv_d2)); |
65 | save_to(oa, BOOST_SERIALIZATION_NVP(dd)); |
66 | save_to(oa, BOOST_SERIALIZATION_NVP(sv_dd)); |
67 | save_to(oa, BOOST_SERIALIZATION_NVP(dp)); |
68 | save_to(oa, BOOST_SERIALIZATION_NVP(gy)); |
69 | save_to(oa, BOOST_SERIALIZATION_NVP(gm)); |
70 | save_to(oa, BOOST_SERIALIZATION_NVP(gd)); |
71 | save_to(oa, BOOST_SERIALIZATION_NVP(gwd)); |
72 | save_to(oa, BOOST_SERIALIZATION_NVP(pd)); |
73 | save_to(oa, BOOST_SERIALIZATION_NVP(nkd)); |
74 | save_to(oa, BOOST_SERIALIZATION_NVP(fkd)); |
75 | save_to(oa, BOOST_SERIALIZATION_NVP(lkd)); |
76 | save_to(oa, BOOST_SERIALIZATION_NVP(fkdb)); |
77 | save_to(oa, BOOST_SERIALIZATION_NVP(fkda)); |
78 | #else |
79 | save_to(ar&: oa, tt: d); |
80 | save_to(ar&: oa, tt: sv_d1); |
81 | save_to(ar&: oa, tt: sv_d2); |
82 | save_to(ar&: oa, tt: dd); |
83 | save_to(ar&: oa, tt: sv_dd); |
84 | save_to(ar&: oa, tt: dp); |
85 | save_to(ar&: oa, tt: gy); |
86 | save_to(ar&: oa, tt: gm); |
87 | save_to(ar&: oa, tt: gd); |
88 | save_to(ar&: oa, tt: gwd); |
89 | save_to(ar&: oa, tt: pd); |
90 | save_to(ar&: oa, tt: nkd); |
91 | save_to(ar&: oa, tt: fkd); |
92 | save_to(ar&: oa, tt: lkd); |
93 | save_to(ar&: oa, tt: fkdb); |
94 | save_to(ar&: oa, tt: fkda); |
95 | #endif |
96 | }catch(archive::archive_exception& ae){ |
97 | std::string s(ae.what()); |
98 | check(testname: "Error writing to archive: " + s + "\nWritten data: \"" + oss.str() + "\"" , testcond: false); |
99 | return printTestStats(); |
100 | } |
101 | } |
102 | |
103 | // read from the archive |
104 | date d2(not_a_date_time); |
105 | date sv_d3(min_date_time); |
106 | date sv_d4(min_date_time); |
107 | date_duration dd2(not_a_date_time); |
108 | date_duration sv_dd2(0); |
109 | date_period dp2(date(2000,Jan,1),date_duration(1)); |
110 | greg_year gy2(1960); |
111 | greg_month gm2(Jan); |
112 | greg_day gd2(1); |
113 | greg_weekday gwd2(Monday); |
114 | partial_date pd2(1); |
115 | nth_kday_of_month nkd2(nth_kday_of_month::first,Monday,Jan); |
116 | first_kday_of_month fkd2(Monday,Jan); |
117 | last_kday_of_month lkd2(Monday,Jan); |
118 | first_kday_before fkdb2(Monday); |
119 | first_kday_after fkda2(Monday); |
120 | |
121 | { |
122 | std::istringstream iss(oss.str()); |
123 | #if defined(DATE_TIME_XML_SERIALIZE) |
124 | archive::xml_iarchive ia(iss); |
125 | #else |
126 | archive::text_iarchive ia(iss); |
127 | #endif |
128 | |
129 | try{ |
130 | #if defined(DATE_TIME_XML_SERIALIZE) |
131 | ia >> BOOST_SERIALIZATION_NVP(d2); |
132 | ia >> BOOST_SERIALIZATION_NVP(sv_d3); |
133 | ia >> BOOST_SERIALIZATION_NVP(sv_d4); |
134 | ia >> BOOST_SERIALIZATION_NVP(dd2); |
135 | ia >> BOOST_SERIALIZATION_NVP(sv_dd2); |
136 | ia >> BOOST_SERIALIZATION_NVP(dp2); |
137 | ia >> BOOST_SERIALIZATION_NVP(gy2); |
138 | ia >> BOOST_SERIALIZATION_NVP(gm2); |
139 | ia >> BOOST_SERIALIZATION_NVP(gd2); |
140 | ia >> BOOST_SERIALIZATION_NVP(gwd2); |
141 | ia >> BOOST_SERIALIZATION_NVP(pd2); |
142 | ia >> BOOST_SERIALIZATION_NVP(nkd2); |
143 | ia >> BOOST_SERIALIZATION_NVP(fkd2); |
144 | ia >> BOOST_SERIALIZATION_NVP(lkd2); |
145 | ia >> BOOST_SERIALIZATION_NVP(fkdb2); |
146 | ia >> BOOST_SERIALIZATION_NVP(fkda2); |
147 | #else |
148 | ia >> d2; |
149 | ia >> sv_d3; |
150 | ia >> sv_d4; |
151 | ia >> dd2; |
152 | ia >> sv_dd2; |
153 | ia >> dp2; |
154 | ia >> gy2; |
155 | ia >> gm2; |
156 | ia >> gd2; |
157 | ia >> gwd2; |
158 | ia >> pd2; |
159 | ia >> nkd2; |
160 | ia >> fkd2; |
161 | ia >> lkd2; |
162 | ia >> fkdb2; |
163 | ia >> fkda2; |
164 | #endif |
165 | }catch(archive::archive_exception& ae){ |
166 | std::string s(ae.what()); |
167 | check(testname: "Error reading from archive: " + s + "\nWritten data: \"" + oss.str() + "\"" , testcond: false); |
168 | return printTestStats(); |
169 | } |
170 | } |
171 | |
172 | check(testname: "date" , testcond: d == d2); |
173 | check(testname: "special_value date (nadt)" , testcond: sv_d1 == sv_d3); |
174 | check(testname: "special_value date (pos_infin)" , testcond: sv_d2 == sv_d4); |
175 | check(testname: "date_duration" , testcond: dd == dd2); |
176 | check(testname: "special_value date_duration (neg_infin)" , testcond: sv_dd == sv_dd2); |
177 | check(testname: "date_period" , testcond: dp == dp2); |
178 | check(testname: "greg_year" , testcond: gy == gy2); |
179 | check(testname: "greg_month" , testcond: gm == gm2); |
180 | check(testname: "greg_day" , testcond: gd == gd2); |
181 | check(testname: "greg_weekday" , testcond: gwd == gwd2); |
182 | check(testname: "date_generator: partial_date" , testcond: pd == pd2); |
183 | check(testname: "date_generator: nth_kday_of_month" , testcond: nkd.get_date(y: 2002) == nkd2.get_date(y: 2002)); // no operator== for nth_kday_of_week - yet |
184 | check(testname: "date_generator: first_kday_of_month" , testcond: fkd.get_date(year: 2002) == fkd2.get_date(year: 2002)); // no operator== for first_kday_of_week - yet |
185 | check(testname: "date_generator: last_kday_of_month" , testcond: lkd.get_date(year: 2002) == lkd2.get_date(year: 2002)); // no operator== for last_kday_of_week - yet |
186 | check(testname: "date_generator: first_kday_before" , testcond: fkdb.get_date(start_day: d) == fkdb2.get_date(start_day: d)); // no operator== for first_kday_before - yet |
187 | check(testname: "date_generator: first_kday_after" , testcond: fkda.get_date(start_day: d) == fkda2.get_date(start_day: d)); // no operator== for first_kday_after - yet |
188 | |
189 | return printTestStats(); |
190 | } |
191 | |