]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/date_time/test/gregorian/testdate.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / date_time / test / gregorian / testdate.cpp
CommitLineData
7c673cae
FG
1/* Copyright (c) 2002,2003 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
7c673cae
FG
8#include <boost/cstdint.hpp>
9#include "boost/date_time/gregorian/gregorian.hpp"
10#include "../testfrmwk.hpp"
11fdf7f2
TL
11#include <iostream>
12#include <sstream>
13
14void test_yearlimit(int yr, bool allowed)
15{
16 std::stringstream sdesc;
17 sdesc << "should" << (allowed ? "" : " not") << " be able to make a date in year " << yr;
18
19 try {
20 boost::gregorian::date chkyr(yr, 1, 1);
21 check(sdesc.str(), allowed);
22 }
23 catch (std::out_of_range&) { check(sdesc.str(), !allowed); }
24}
7c673cae
FG
25
26int
27main()
28{
29
30 using namespace boost::gregorian;
31
32 //various constructors
33#if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
34 date def;
35 check("Default constructor", def == date(not_a_date_time));
36#endif
11fdf7f2 37
7c673cae 38 date d1(1900,1,1);
11fdf7f2 39 date d2 = date(2000,1,1);
7c673cae
FG
40 date d3(d2);
41 check("Copy constructor", d3 == d2);
42 date d4(2000,12,31);
43 date d4a(2000,Dec,31);
44 //d4a.print(std::cout); std::cout << std::endl;
45 check("month_rep constructor", d4 == d4a);
46 //std::cout << d3 << std::endl;
47 //retrieval functions
48 check_equal("1900-01-01 day is 01", d1.day(), 1);
49 check_equal("1900-01-01 month is 01", d1.month(), 1);
50 check_equal("1900-01-01 year is 1900", d1.year(), 1900);
51 check_equal("2000-12-31 day is 31", d4.day(), 31);
52 check_equal("2000-12-31 month is 12", d4.month(), 12);
53 check_equal("2000-12-31 year is 2000", d4.year(), 2000);
54 //operator<
55 check("1900-01-01 is less than 2000-01-01", d1 < d2);
56 check("2000-01-01 is NOT less than 2000-01-01", !(d1 < d1));
57 //operator<=
58 check("2000-01-01 is less equal than 2000-01-01", d1 <= d1);
59 //operator>
60 check("2000-01-01 is greater than 1900-01-01", d2 > d1);
61 check("2000-01-01 is NOT greater than 2000-01-01", !(d1 < d1));
62 //operator>=
63 check("2000-01-01 is greater equal than 2000-01-01", d1 >= d1);
64 //operator!=
65 check("2000-01-01 is NOT equal to 1900-01-01", d2 != d1);
66 //operator==
67 check_equal("2000-01-01 is equal 2000-01-01", d3, d2);
68 check("2000-01-01 is greater equal 2000-01-01", d3 >= d2);
69 check("2000-01-01 is greater equal 2000-01-01", d3 <= d2);
70
71 date::ymd_type ymd = d1.year_month_day();
72 check_equal("ymd year", ymd.year, 1900);
73 check_equal("ymd month", ymd.month, 1);
74 check_equal("ymd day", ymd.day, 1);
75
76 //The max function will not compile with Borland 5.5
77 //Complains about must specialize basic_data<limits> ???
78// std::cout << "Max date is " << (date::max)() << std::endl;
79// //std::cout << "Max date is " << (basic_date< date_limits<unsigned int,1900> >::max)() << std::endl;
80// //std::cout << "Max date is " << (date_limits<unsigned int, 1900>::max)() << std::endl;
81
82 const date answers[] = {date(1900,Jan,1),date(1900,Jan,4),date(1900,Jan,7),
83 date(1900,Jan,10),date(1900,Jan,13)};
84 date_duration off(3);
85 date d5(1900,1,1);
86 for (int i=0; i < 5; ++i) {
87 //std::cout << d5 << " ";
88 check(" addition ", d5 == answers[i]);
89 d5 = d5 + off;
90 }
91 std::cout << std::endl;
92
93 const date answers1[] = {date(2000,2,26),date(2000,2,28),date(2000,Mar,1)};
94 date d8(2000,Feb,26);
95 for (int j=0; j < 3; ++j) {
96 //std::cout << d8 << " ";
97 check(" more addition ", d8 == answers1[j]);
98 d8 = d8 + days(2);
99 }
100 // std::cout << std::endl;
101
102 date d6(2000,2,28);
103 date d7(2000,3,1);
104 date_duration twoDays(2);
105 date_duration negtwoDays(-2);
106 date_duration zeroDays(0);
107 check_equal("2000-03-01 - 2000-02-28 == 2 days", twoDays, (d7-d6));
108 check_equal("2000-02-28 - 2000-03-01 == - 2 days", negtwoDays, (d6-d7));
109 check_equal("2000-02-28 - 2000-02-28 == 0 days", zeroDays, (d6-d6));
110 check_equal("2000-02-28 + 2 days == 2000-03-01 ", d6 + twoDays, d7);
111 check_equal("2000-03-01 - 2 days == 2000-02-28 ", d7 - twoDays, d6);
112 check_equal("Add duration to date", date(1999,1,1) + date_duration(365), date(2000,1,1));
113 check_equal("Add zero days", date(1999,1,1) + zeroDays, date(1999,1,1));
114 //can't do this...
115 //check("Add date to duration", date_duration(365) + date(1999,1,1) == date(2000,1,1));
116
117 {
118 date d(2003,Oct,31);
119 date_duration dd(55);
120 d += dd;
121 check("date += date_duration", d == date(2003,Dec,25));
122 d -= dd;
123 check("date -= date_duration", d == date(2003,Oct,31));
124 /* special_values is more thoroughly tested later,
125 * this is just a test of += & -= with special values */
126 d += date_duration(pos_infin);
127 check("date += inf_dur", d == date(pos_infin));
128 d -= dd;
129 check("inf_date -= dur", d == date(pos_infin));
130 }
131 {
132 date d(2003,Oct,31);
133 date_duration dd1(pos_infin), dd2(neg_infin), dd3(not_a_date_time);
134 check_equal("date + inf_dur", d + dd1, date(pos_infin));
135 check_equal("date + inf_dur", d + dd2, date(neg_infin));
136 check_equal("date + nan_dur", d + dd3, date(not_a_date_time));
137 check_equal("date - inf_dur", d - dd1, date(neg_infin));
138 check_equal("date - inf_dur", d - dd2, date(pos_infin));
139 check_equal("date - nan_dur", d - dd3, date(not_a_date_time));
140 check_equal("inf_date + inf_dur", date(pos_infin) + dd1, date(pos_infin));
141 check_equal("inf_date - inf_dur", date(pos_infin) - dd1, date(not_a_date_time));
142 check_equal("inf_date + inf_dur", date(neg_infin) + dd1, date(not_a_date_time));
143 check_equal("inf_date - inf_dur", date(neg_infin) - dd1, date(neg_infin));
144 }
145
146
147 try {
148 date d9(2000, Jan, 32);
149 check("day out of range", false);
150 //never reached if working -- but stops compiler warnings :-)
151 std::cout << "Oops: " << to_iso_string(d9) << std::endl;
152 }
153 catch (bad_day_of_month&) {
154 check("day out of range", true);
155 }
156 try {
157 date d9(2000, Jan, 0);
158 check("day out of range", false);
159 //never reached if working -- but stops compiler warnings :-)
160 std::cout << "Oops: " << to_iso_string(d9) << std::endl;
161 }
162 catch (bad_day_of_month&) {
163 check("day out of range", true);
164 }
165
166 try {
167 date d20(2000, Feb, 31);
168 check("day out of range", false);
169 //never reached if working -- but stops compiler warnings :-)
170 std::cout << "Oops: " << to_iso_string(d20) << std::endl;
171 }
172 catch (bad_day_of_month&) {
173 check("day out of range", true);
174 }
175
176 //more subtle -- one day past in a leap year
177 try {
178 date d21(2000, Feb, 30);
179 check("day out of range", false);
180 //never reached if working -- but stops compiler warnings :-)
181 std::cout << "Oops: " << to_iso_string(d21) << std::endl;
182 }
183 catch (bad_day_of_month&) {
184 check("day out of range", true);
185 }
186
187 //more subtle -- one day past in a leap year
188 try {
189 date d22(2000, Feb, 29);
190 check("last day of month ok", true);
191 std::cout << to_iso_string(d22) << std::endl; //stop compiler warning
192 }
193 catch (bad_day_of_month&) {
194 check("last day of month -- oops bad exception", false);
195 }
196
197 //Not a leap year -- now Feb 29 is bad
198 try {
199 date d23(1999, Feb, 29);
200 check("day out of range", false);
201 //never reached if working -- but stops compiler warnings :-)
202 std::cout << "Oops: " << to_iso_string(d23) << std::endl;
203 }
204 catch (bad_day_of_month&) {
205 check("day out of range", true);
206 }
207
208 //check out some special values
209 check("check not a date - false", !d7.is_not_a_date());
210 check("check positive infinity - false", !d7.is_pos_infinity());
211 check("check negative infinity - false", !d7.is_neg_infinity());
212
213 date d10(neg_infin);
214 check("check negative infinity - true", d10.is_infinity());
215 d10 = d10 + twoDays; //still neg infinity
216 check("check negative infinity - true", d10.is_neg_infinity());
217
218 date d11(pos_infin);
219 check("check positive infinity - true", d11.is_infinity());
220 d11 = d11 + twoDays;
221 check("check positive infinity add - true", d11.is_pos_infinity());
222
223 date d12(not_a_date_time);
224 check("check not a date", d12.is_not_a_date());
225 check("check infinity compare ", d10 != d11);
226 check("check infinity compare ", d10 < d11);
227 check("check infinity nad compare ", d12 != d11);
228 date d13(max_date_time);
229 check("check infinity - max compare ", d13 < d11);
230 check_equal("max date_time value ", d13, date(9999,Dec, 31));
231 std::cout << to_simple_string(d13) << std::endl;
232 date d14(min_date_time);
233 check("check infinity - min compare ", d14 > d10);
234 std::cout << to_simple_string(d14) << std::endl;
235 check_equal("min date_time value ", d14, date(1400,Jan, 1));
236
237
238 date d15(1400,1,1);
239 std::cout << d15.day_of_week().as_long_string() << std::endl;
240 check("check infinity - min compare ", d10 < d15);
241
242 // most of this testing is in the gregorian_calendar tests
243 std::cout << d15.julian_day() << std::endl;
244 check_equal("check julian day ", d15.julian_day(),
245 static_cast<boost::uint32_t>(2232400));
246 check_equal("check modjulian day ", d15.modjulian_day(), -167601);
247 date d16(2004,2,29);
248 check_equal("check julian day ", d16.julian_day(),
249 static_cast<boost::uint32_t>(2453065));
250 check_equal("check modjulian day ", d16.modjulian_day(),
251 static_cast<boost::uint32_t>(53064));
252
253 // most of this testing is in the gregorian_calendar tests
254 date d31(2000, Jun, 1);
255 check_equal("check iso week number ", d31.week_number(), 22);
256 date d32(2000, Aug, 1);
257 check_equal("check iso week number ", d32.week_number(), 31);
258 date d33(2000, Oct, 1);
259 check_equal("check iso week number ", d33.week_number(), 39);
260 date d34(2000, Dec, 1);
261 check_equal("check iso week number ", d34.week_number(), 48);
262 date d35(2000, Dec, 24);
263 check_equal("check iso week number ", d35.week_number(), 51);
264 date d36(2000, Dec, 25);
265 check_equal("check iso week number ", d36.week_number(), 52);
266 date d37(2000, Dec, 31);
267 check_equal("check iso week number ", d37.week_number(), 52);
268 date d38(2001, Jan, 1);
269 check_equal("check iso week number ", d38.week_number(), 1);
270
271 try {
272 int dayofyear1 = d38.day_of_year();
273 check_equal("check day of year number", dayofyear1, 1);
274 check_equal("check day of year number", d37.day_of_year(), 366);
275 date d39(2001,Dec,31);
276 check_equal("check day of year number", d39.day_of_year(), 365);
277 date d40(2000,Feb,29);
278 check_equal("check day of year number", d40.day_of_year(), 60);
279 date d41(1400,Jan,1);
280 check_equal("check day of year number", d41.day_of_year(), 1);
281 date d42(1400,Jan,1);
282 check_equal("check day of year number", d42.day_of_year(), 1);
283 date d43(2002,Nov,17);
284 check_equal("check day of year number", d43.day_of_year(), 321);
285 }
286 catch(std::exception& e) {
287 std::cout << e.what() << std::endl;
288 check("check day of year number", false);
289 }
290
291 //converts to date and back -- should get same result
11fdf7f2 292 check_equal("tm conversion functions 2000-1-1", date_from_tm(to_tm(d2)), d2);
7c673cae
FG
293 check_equal("tm conversion functions 1900-1-1", date_from_tm(to_tm(d1)), d1);
294 check_equal("tm conversion functions min date 1400-1-1 ", date_from_tm(to_tm(d14)), d14);
295 check_equal("tm conversion functions max date 9999-12-31", date_from_tm(to_tm(d13)), d13);
296
297 try{
298 date d(neg_infin);
299 tm d_tm = to_tm(d);
300 check("Exception not thrown (special_value to_tm)", false);
301 std::cout << d_tm.tm_sec << std::endl; //does nothing useful but stops compiler from complaining about unused d_tm
b32b8144 302 }catch(std::out_of_range&){
7c673cae
FG
303 check("Caught expected exception (special_value to_tm)", true);
304 }catch(...){
305 check("Caught un-expected exception (special_value to_tm)", false);
306 }
307
11fdf7f2
TL
308 // trac-13159
309 test_yearlimit( 0, false);
310 test_yearlimit( 1399, false);
311 test_yearlimit( 1400, true);
312 test_yearlimit( 1401, true);
313 test_yearlimit( 9999, true);
314 test_yearlimit(10000, false);
315 test_yearlimit(10001, false);
7c673cae 316
11fdf7f2 317 return printTestStats();
7c673cae
FG
318}
319