]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* Copyright (c) 2002-2004 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 | * $Date$ | |
7 | */ | |
8 | ||
9 | #include "boost/date_time/posix_time/posix_time_duration.hpp" | |
10 | #include "boost/date_time/compiler_config.hpp" | |
11 | #if defined(BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS) | |
12 | #include "boost/date_time/posix_time/time_formatters_limited.hpp" | |
13 | #else | |
14 | #include "boost/date_time/posix_time/time_formatters.hpp" | |
15 | #endif | |
16 | #include "../testfrmwk.hpp" | |
17 | ||
18 | ||
19 | int | |
20 | main() | |
21 | { | |
22 | using namespace boost::posix_time; | |
23 | ||
24 | ||
25 | // std::cout << "Default limits: SECOND " << std::endl; | |
26 | { | |
27 | time_duration td; | |
28 | check("default construction -- 0 ticks", td.ticks() == 0); | |
29 | // check("default construction -- 0 secs", td.seconds() == 0); | |
30 | // check("default construction -- 0 min", td.minutes() == 0); | |
31 | } | |
32 | ||
33 | // construct from components | |
34 | time_duration td1(1,25,0); | |
35 | time_duration td3(td1.hours(),td1.minutes(),td1.seconds()); | |
36 | check("total up elements", td1 == td3); | |
37 | td1 = -td1; // td1 == "-1:25:00" | |
92f5a8d4 TL |
38 | check("invert_sign",td3 == td1.invert_sign()); |
39 | check("invert_sign",td1 == td3.invert_sign()); | |
40 | check("abs",td3 == td1.abs()); | |
41 | check("abs",td3 == td3.abs()); | |
42 | check("is_positive",td3.is_positive()); | |
43 | check("is_positive",!td1.is_positive()); | |
44 | check("is_negative",td1.is_negative()); | |
45 | check("is_negative",!td3.is_negative()); | |
46 | check("is_zero",!td1.is_zero()); | |
47 | check("is_zero",(td1 - td1).is_zero()); | |
7c673cae | 48 | td3 = time_duration(td1.hours(),td1.minutes(),td1.seconds()); |
92f5a8d4 | 49 | check("total up elements-inverted sign", td1 == td3); |
7c673cae FG |
50 | |
51 | ||
52 | time_duration t_1(0,1,40); | |
53 | time_duration t_2(0,1,41); | |
54 | check("less test", !(t_2 < t_2)); | |
55 | check("equal test", t_1 == t_1); | |
56 | check("greater equal - equal", t_1 >= t_1); | |
57 | check("greater equal - greater", t_2 >= t_1); | |
58 | check("less equal - equal ", t_2 <= t_2); | |
59 | check("greater ", t_2 > t_1); | |
60 | check("greater - not ", !(t_1 > t_2)); | |
61 | time_duration t_3(t_2); | |
62 | check("copy constructor ", t_2 == t_3); | |
63 | time_duration t_4 = t_3; | |
64 | check("assignment operator ", t_2 == t_4); | |
65 | ||
66 | time_duration t_5(1,30,20,10); // 1hr, 30min, 20sec, 10 frac sec | |
67 | t_5 /= 2; | |
68 | check("divide equal", (t_5.hours() == 0 && | |
69 | t_5.minutes() == 45 && | |
70 | t_5.seconds() == 10 && | |
71 | t_5.fractional_seconds() == 5)); | |
72 | t_5 = time_duration(3,15,8,0) / 2; | |
73 | check("divide int", t_5 == time_duration(1,37,34,0)); | |
74 | { | |
75 | time_duration td = hours(5); | |
76 | td *= 5; | |
77 | check("mult-equals int", time_duration(25,0,0,0) == td); | |
78 | } | |
79 | ||
80 | ||
81 | t_5 = t_2 + t_1; | |
82 | //VC6 goes ambiguous on the next line... | |
83 | #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) | |
84 | //sorry ticks() doesn't work on VC6 | |
85 | #else | |
86 | std::cout << t_5.ticks() << std::endl; | |
87 | #endif | |
88 | check("add", t_5 == time_duration(0,3,21)); | |
89 | time_duration td_a(5,5,5,5); | |
90 | time_duration td_b(4,4,4,4); | |
91 | time_duration td_c(2,2,2,2); | |
92 | td_a += td_b; | |
93 | check("add equal", td_a == time_duration(9,9,9,9)); | |
94 | time_duration td_d = td_b - td_c; | |
95 | check("subtract", td_d == time_duration(2,2,2,2)); | |
96 | td_d -= td_b; | |
97 | check("subtract equal (neg result)", td_d == td_c - td_b); | |
98 | ||
99 | time_duration utd(1,2,3,4); | |
100 | time_duration utd2 = -utd; | |
101 | //std::cout << td_d << '\n' << utd2 << std::endl; | |
102 | check("unary-", ((utd2.hours() == -1) && | |
103 | (utd2.minutes() == -2) && | |
104 | (utd2.seconds() == -3) && | |
105 | (utd2.fractional_seconds() == -4)) ); | |
106 | utd2 = -hours(5); | |
107 | check("unary-", utd2.hours() == -5); | |
108 | utd2 = -utd2; | |
109 | check("unary-", utd2.hours() == 5); | |
110 | ||
111 | time_duration t_6(5,4,3); //05:04:03 | |
112 | check("h-m-s 5-4-3 hours", t_6.hours() == 5); | |
113 | check("h-m-s 5-4-3 minutes", t_6.minutes() == 4); | |
114 | check("h-m-s 5-4-3 seconds", t_6.seconds() == 3); | |
115 | std::cout << t_6.total_seconds() << std::endl; | |
116 | check("h-m-s 5-4-3 total_seconds", t_6.total_seconds() == 18243); | |
117 | ||
118 | hours tenhours(10); | |
119 | minutes fivemin(5); | |
120 | time_duration t7 = time_duration(1,2,3) + tenhours + fivemin; | |
121 | check("short hand durations add", t7 == time_duration(11,7,3)); | |
122 | time_duration t8 = tenhours + time_duration(1,2,3) + fivemin; | |
123 | check("short hand durations add", t8 == time_duration(11,7,3)); | |
124 | ||
125 | if (time_duration::resolution() >= boost::date_time::micro) { | |
126 | time_duration t_9(5,4,3,9876); //05:04:03.09876 | |
127 | check("h-m-s 5-4-3.21 hours", t_9.hours() == 5); | |
128 | check("h-m-s 5-4-3.21 min ", t_9.minutes() == 4); | |
129 | check("h-m-s 5-4-3.21 sec ", t_9.seconds() == 3); | |
130 | check("h-m-s 5-4-3.21 fs ", t_9.fractional_seconds() == 9876); | |
131 | check("h-m-s 5-4-3.21 total_seconds", t_9.total_seconds() == 18243); | |
132 | // check("h-m-s 5-4-3.21 fs ", t_9.fs_as_double() == 0.9876); | |
133 | //std::cout << t_9.fs_as_double() << std::endl; | |
134 | std::cout << to_simple_string(t_9) << std::endl; | |
135 | } | |
136 | ||
137 | if (time_duration::resolution() >= boost::date_time::tenth) { | |
138 | time_duration t_10(5,4,3,9); //05:04:03.00001 | |
139 | check("h-m-s 5-4-3.9 hours", t_10.hours() == 5); | |
140 | check("h-m-s 5-4-3.9 min ", t_10.minutes() == 4); | |
141 | check("h-m-s 5-4-3.9 sec ", t_10.seconds() == 3); | |
142 | check("h-m-s 5-4-3.9 fs ", t_10.fractional_seconds() == 9); | |
143 | check("h-m-s 5-4-3.9 total_seconds", t_10.total_seconds() == 18243); | |
144 | std::cout << to_simple_string(t_10) << std::endl; | |
145 | } | |
146 | ||
147 | if (time_duration::resolution() >= boost::date_time::milli) { | |
148 | millisec ms(9); | |
149 | // time_duration t_10(0,0,0,); //00:00:00.009 | |
150 | std::cout << "time_resolution: " << time_duration::resolution() << std::endl; | |
151 | #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) | |
152 | //sorry res_adjust() doesn't work on VC6 | |
153 | #else | |
154 | std::cout << "res_adjust " << time_res_traits::res_adjust() << std::endl; | |
155 | #endif | |
156 | if (time_duration::resolution() == boost::date_time::nano) { | |
157 | check("millisec", ms.fractional_seconds() == 9000000); | |
158 | check("total_seconds - nofrac", ms.total_seconds() == 0); | |
159 | check("total_millisec", ms.total_milliseconds() == 9); | |
160 | check("ticks per second", time_duration::ticks_per_second() == 1000000000); | |
161 | } | |
162 | else { | |
163 | check("millisec 9000", ms.fractional_seconds() == 9000); | |
164 | check("total_seconds - nofrac", ms.total_seconds() == 0); | |
165 | check("total_millisec", ms.total_milliseconds() == 9); | |
166 | } | |
167 | } | |
168 | ||
169 | #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS | |
170 | if (time_duration::resolution() >= boost::date_time::nano) { | |
171 | nanosec ns(9); | |
172 | // time_duration t_10(0,0,0,); //00:00:00.00009 | |
173 | check("nanosec", ns.fractional_seconds() == 9); | |
174 | check("total nanosec", ns.total_nanoseconds() == 9); | |
175 | check("total microsec - truncated", ns.total_microseconds() == 0); | |
176 | std::cout << to_simple_string(ns) << std::endl; | |
177 | time_duration ns18 = ns + ns; | |
178 | check("nanosec", ns18.fractional_seconds() == 18); | |
179 | std::cout << to_simple_string(ns18) << std::endl; | |
180 | nanosec ns2(1000000000); //one second | |
181 | check("nano to second compare", ns2 == seconds(1)); | |
182 | check("check total seconds", ns2.total_seconds() == 1); | |
183 | std::cout << to_simple_string(ns2) << std::endl; | |
184 | check("division of nanoseconds", (nanosec(3)/2) == nanosec(1)); | |
185 | check("multiplication of nanosec", nanosec(3)*1000 == microsec(3)); | |
186 | } | |
187 | #endif | |
188 | ||
189 | // Test for overflows (ticket #3471) | |
190 | { | |
191 | ptime start(boost::gregorian::date(2000, 1, 1)); | |
192 | ptime end(boost::gregorian::date(2000, 5, 1)); | |
193 | time_duration td = end - start; | |
194 | ptime end2 = start + microseconds(td.total_microseconds()); | |
195 | check("microseconds constructor overflow", end == end2); | |
196 | } | |
197 | ||
198 | time_duration t_11(3600,0,0); | |
199 | check("3600 hours ", t_11.hours() == 3600); | |
200 | check("total seconds 3600 hours", t_11.total_seconds() == 12960000); | |
201 | ||
202 | time_duration td_12(1,2,3,10); | |
203 | std::cout << td_12.total_seconds() << std::endl; | |
204 | check("total seconds 3723 hours", td_12.total_seconds() == 3723); | |
205 | ||
206 | // time_duration t_11a = t_11/time_duration(0,3,0); | |
207 | ||
208 | check("division", (hours(2)/2) == hours(1)); | |
209 | check("division", (hours(3)/2) == time_duration(1,30,0)); | |
210 | check("division", (hours(3)/3) == hours(1)); | |
211 | check("multiplication", time_duration(3,0,0)*2 == hours(6)); | |
11fdf7f2 | 212 | check("multiplication", hours(360)*1000 == hours(360000)); |
7c673cae FG |
213 | |
214 | // special_values operations | |
215 | time_duration pi_dur(pos_infin), ni_dur(neg_infin), ndt_dur(not_a_date_time); | |
216 | check("+infin + -infin", pi_dur + ni_dur == ndt_dur); | |
217 | check("infin / int", pi_dur / 3 == pi_dur); | |
218 | check("infin + duration", pi_dur + td_12 == pi_dur); | |
219 | check("infin - duration", pi_dur - td_12 == pi_dur); | |
220 | check("unary-", -pi_dur == ni_dur); | |
221 | check("-infin less than +infin", ni_dur < pi_dur); | |
222 | check("-infin less than duration", ni_dur < td_12); | |
223 | check("+infin greater than duration", pi_dur > td_12); | |
224 | std::string result(""), answer("+infinity"); | |
225 | result = to_simple_string(pi_dur); | |
226 | check("to string +infin", result==answer); | |
227 | result = to_simple_string(ni_dur); | |
228 | answer = "-infinity"; | |
229 | check("to string +infin", result==answer); | |
230 | result = to_simple_string(ndt_dur); | |
231 | //answer = "not-a-number"; | |
232 | answer = "not-a-date-time"; | |
233 | check("to string +infin", result==answer); | |
234 | ||
235 | using namespace boost::gregorian; | |
236 | ptime t1(date(2001,7,14)); | |
237 | ptime t2(date(2002,7,14)); | |
238 | check("One year of hours: 365*24=8760", 365*24 == ((t2-t1).hours())); | |
239 | check("Total seconds in a year", 365*24*3600 == ((t2-t1).total_seconds())); | |
240 | ||
241 | std::cout << to_simple_string(time_duration(20000 * 24, 0, 0, 0)) << std::endl; | |
242 | std::cout << to_simple_string(time_duration(24855 * 24, 0, 0, 0)) << std::endl; | |
243 | std::cout << to_simple_string(time_duration(24856 * 24, 0, 0, 0)) << std::endl; | |
244 | std::cout << to_simple_string(time_duration(25000 * 24, 0, 0, 0)) << std::endl; | |
245 | time_duration tdl1(25000*24, 0, 0, 0); | |
246 | check("600000 hours", tdl1.hours() == 600000); | |
247 | time_duration tdl2(2000000, 0, 0, 0); | |
248 | check("2000000 hours", tdl2.hours() == 2000000); | |
249 | ||
250 | check("total milliseconds", seconds(1).total_milliseconds() == 1000); | |
251 | check("total microseconds", seconds(1).total_microseconds() == 1000000); | |
252 | check("total nanoseconds", seconds(1).total_nanoseconds() == 1000000000); | |
253 | check("total milliseconds", hours(1).total_milliseconds() == 3600*1000); | |
254 | boost::int64_t tms = static_cast<boost::int64_t>(3600)*1000000*1001; //ms per sec | |
255 | check("total microseconds 1000 hours", hours(1001).total_microseconds() == tms); | |
256 | tms = static_cast<boost::int64_t>(3600)*365*24*1000; | |
257 | check("total milliseconds - one year", (t2-t1).total_milliseconds() == tms); | |
258 | tms = 3600*365*24*static_cast<boost::int64_t>(1000000000); | |
259 | check("total nanoseconds - one year", (t2-t1).total_nanoseconds() == tms); | |
260 | #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) | |
261 | #else | |
262 | std::cout << "tms: " << (t2-t1).total_milliseconds() << std::endl; | |
263 | std::cout << "nano per year: " << (t2-t1).total_nanoseconds() << std::endl; | |
264 | #endif | |
265 | // make it into a double | |
266 | double d1 = microseconds(25).ticks()/(double)time_duration::ticks_per_second(); | |
267 | std::cout << d1 << std::endl; | |
268 | ||
269 | //Following causes errors on several compilers about value to large for | |
270 | //type. So it is commented out for now. Strangely works on gcc 3.3 | |
271 | //eg: integer constant out of range | |
272 | // long sec_in_200k_hours(7200000000); | |
273 | // check("total sec in 2000000 hours", | |
274 | // tdl2.total_seconds() == sec_in_200k_hours); | |
275 | // std::cout << to_simple_string(time_duration(2000000, 0, 0, 0)) << std::endl; | |
276 | ||
277 | ||
278 | return printTestStats(); | |
279 | ||
280 | } | |
281 | ||
282 |