tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / tools / qa / cppunit / test_duration.cxx
blobfb5e779d9030b3227713551dc902df183e3c1a7b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <cppunit/TestFixture.h>
11 #include <cppunit/extensions/HelperMacros.h>
13 #include <tools/duration.hxx>
14 #include <tools/datetime.hxx>
16 namespace tools
18 class DurationTest : public CppUnit::TestFixture
20 public:
21 void testDuration();
23 CPPUNIT_TEST_SUITE(DurationTest);
24 CPPUNIT_TEST(testDuration);
25 CPPUNIT_TEST_SUITE_END();
28 void DurationTest::testDuration()
31 const Duration aD(Time(Time::EMPTY), Time(12, 0, 0));
32 CPPUNIT_ASSERT_EQUAL(0.5, aD.GetInDays());
35 const Duration aD(Time(24, 0, 0), Time(12, 0, 0));
36 CPPUNIT_ASSERT_EQUAL(-0.5, aD.GetInDays());
39 const DateTime aS(Date(23, 11, 1999), Time(6, 0, 0));
40 const DateTime aE(Date(24, 11, 1999), Time(18, 0, 0));
41 const Duration aD(aS, aE);
42 CPPUNIT_ASSERT_EQUAL(1.5, aD.GetInDays());
43 DateTime aDT1(aS);
44 const DateTime aDT2 = aDT1 + aD;
45 CPPUNIT_ASSERT_EQUAL(aE, aDT2);
46 aDT1 += aD;
47 CPPUNIT_ASSERT_EQUAL(aE, aDT1);
48 aDT1 += aD;
49 CPPUNIT_ASSERT_EQUAL(DateTime(Date(26, 11, 1999), Time(6, 0, 0)), aDT1);
52 const DateTime aS(Date(23, 11, 1999), Time(18, 0, 0));
53 const DateTime aE(Date(24, 11, 1999), Time(6, 0, 0));
54 const Duration aD(aS, aE);
55 CPPUNIT_ASSERT_EQUAL(0.5, aD.GetInDays());
56 DateTime aDT1(aS);
57 const DateTime aDT2 = aDT1 + aD;
58 CPPUNIT_ASSERT_EQUAL(aE, aDT2);
59 aDT1 += aD;
60 CPPUNIT_ASSERT_EQUAL(aE, aDT1);
61 aDT1 += aD;
62 CPPUNIT_ASSERT_EQUAL(DateTime(Date(24, 11, 1999), Time(18, 0, 0)), aDT1);
65 const DateTime aS(Date(24, 11, 1999), Time(18, 0, 0));
66 const DateTime aE(Date(23, 11, 1999), Time(6, 0, 0));
67 const Duration aD(aS, aE);
68 CPPUNIT_ASSERT_EQUAL(-1.5, aD.GetInDays());
69 DateTime aDT1(aS);
70 const DateTime aDT2 = aDT1 + aD;
71 CPPUNIT_ASSERT_EQUAL(aE, aDT2);
72 aDT1 += aD;
73 CPPUNIT_ASSERT_EQUAL(aE, aDT1);
74 aDT1 += aD;
75 CPPUNIT_ASSERT_EQUAL(DateTime(Date(21, 11, 1999), Time(18, 0, 0)), aDT1);
78 const DateTime aS(Date(24, 11, 1999), Time(6, 0, 0));
79 const DateTime aE(Date(23, 11, 1999), Time(18, 0, 0));
80 const Duration aD(aS, aE);
81 CPPUNIT_ASSERT_EQUAL(-0.5, aD.GetInDays());
82 DateTime aDT1(aS);
83 const DateTime aDT2 = aDT1 + aD;
84 CPPUNIT_ASSERT_EQUAL(aE, aDT2);
85 aDT1 += aD;
86 CPPUNIT_ASSERT_EQUAL(aE, aDT1);
87 aDT1 += aD;
88 CPPUNIT_ASSERT_EQUAL(DateTime(Date(23, 11, 1999), Time(6, 0, 0)), aDT1);
91 const Duration aD(1.5);
92 CPPUNIT_ASSERT_EQUAL(1.5, aD.GetInDays());
93 CPPUNIT_ASSERT_EQUAL(DateTime(Date(24, 11, 1999), Time(18, 0, 0)),
94 DateTime(Date(23, 11, 1999), Time(6, 0, 0)) + aD);
97 const Duration aD(-1.5);
98 CPPUNIT_ASSERT_EQUAL(-1.5, aD.GetInDays());
99 CPPUNIT_ASSERT_EQUAL(DateTime(Date(23, 11, 1999), Time(6, 0, 0)),
100 DateTime(Date(24, 11, 1999), Time(18, 0, 0)) + aD);
103 const Duration aD(-1.5);
104 const Duration aN = -aD;
105 CPPUNIT_ASSERT_EQUAL(1.5, aN.GetInDays());
108 const Duration aD(1, Time(2, 3, 4, 5));
109 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aD.GetDays());
110 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(2), aD.GetTime().GetHour());
111 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(3), aD.GetTime().GetMin());
112 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(4), aD.GetTime().GetSec());
113 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(5), aD.GetTime().GetNanoSec());
116 // 235929599 seconds == SAL_MAX_UINT16 hours + 59 minutes + 59 seconds
117 const Duration aD(0, Time(0, 0, 235929599, Time::nanoSecPerSec - 1));
118 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2730), aD.GetDays());
119 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(15), aD.GetTime().GetHour());
120 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aD.GetTime().GetMin());
121 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aD.GetTime().GetSec());
122 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(999999999), aD.GetTime().GetNanoSec());
125 // 235929599 seconds == SAL_MAX_UINT16 hours + 59 minutes + 59 seconds
126 const Duration aD(0, 0, 0, 235929599, Time::nanoSecPerSec - 1);
127 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2730), aD.GetDays());
128 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(15), aD.GetTime().GetHour());
129 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aD.GetTime().GetMin());
130 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aD.GetTime().GetSec());
131 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(999999999), aD.GetTime().GetNanoSec());
134 const Duration aD(1, 2, 3, 4, 5);
135 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aD.GetDays());
136 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(2), aD.GetTime().GetHour());
137 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(3), aD.GetTime().GetMin());
138 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(4), aD.GetTime().GetSec());
139 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(5), aD.GetTime().GetNanoSec());
142 const Duration aD(-1, 2, 3, 4, 5);
143 CPPUNIT_ASSERT(aD.IsNegative());
144 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(-1), aD.GetDays());
145 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(2), aD.GetTime().GetHour());
146 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(3), aD.GetTime().GetMin());
147 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(4), aD.GetTime().GetSec());
148 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(5), aD.GetTime().GetNanoSec());
151 const Duration aD(1, SAL_MAX_UINT32, SAL_MAX_UINT32, SAL_MAX_UINT32, SAL_MAX_UINT64);
152 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(182202802), aD.GetDays());
153 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(1), aD.GetTime().GetHour());
154 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(17), aD.GetTime().GetMin());
155 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(48), aD.GetTime().GetSec());
156 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(709551615), aD.GetTime().GetNanoSec());
159 const Duration aD(-1, SAL_MAX_UINT32, SAL_MAX_UINT32, SAL_MAX_UINT32, SAL_MAX_UINT64);
160 CPPUNIT_ASSERT(aD.IsNegative());
161 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(-182202802), aD.GetDays());
162 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(1), aD.GetTime().GetHour());
163 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(17), aD.GetTime().GetMin());
164 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(48), aD.GetTime().GetSec());
165 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(709551615), aD.GetTime().GetNanoSec());
167 { // Maximum days with all max possible.
168 const Duration aD(1965280846, SAL_MAX_UINT32, SAL_MAX_UINT32, SAL_MAX_UINT32,
169 SAL_MAX_UINT64);
170 CPPUNIT_ASSERT_EQUAL(SAL_MAX_INT32, aD.GetDays());
171 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(1), aD.GetTime().GetHour());
172 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(17), aD.GetTime().GetMin());
173 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(48), aD.GetTime().GetSec());
174 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(709551615), aD.GetTime().GetNanoSec());
176 { // Maximum negative days with all max possible.
177 const Duration aD(-1965280847, SAL_MAX_UINT32, SAL_MAX_UINT32, SAL_MAX_UINT32,
178 SAL_MAX_UINT64);
179 CPPUNIT_ASSERT_EQUAL(SAL_MIN_INT32, aD.GetDays());
180 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(1), aD.GetTime().GetHour());
181 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(17), aD.GetTime().GetMin());
182 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(48), aD.GetTime().GetSec());
183 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(709551615), aD.GetTime().GetNanoSec());
185 { // Add()
186 const DateTime aS(Date(23, 11, 1999), Time(0, 0, 0));
187 const DateTime aE(Date(23, 11, 1999), Time(1, 23, 45));
188 const Duration aD(aS, aE);
189 Duration aV = aD;
190 bool bOverflow = true;
191 aV.Add(aD, bOverflow);
192 CPPUNIT_ASSERT(!bOverflow);
193 CPPUNIT_ASSERT_EQUAL(DateTime(Date(23, 11, 1999), Time(2, 47, 30)),
194 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV);
195 for (int i = 0; i < 20; ++i)
196 aV.Add(aD, bOverflow);
197 CPPUNIT_ASSERT(!bOverflow);
198 CPPUNIT_ASSERT_EQUAL(DateTime(Date(24, 11, 1999), Time(6, 42, 30)),
199 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV);
200 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aV.GetDays());
201 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(6), aV.GetTime().GetHour());
202 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(42), aV.GetTime().GetMin());
203 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(30), aV.GetTime().GetSec());
204 CPPUNIT_ASSERT(aV.GetTime().GetTime() > 0);
205 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.27951388888889, aV.GetInDays(), 1E-14);
206 // Negative duration.
207 const Duration aN(aE, aS);
208 aV = aN;
209 aV.Add(aN, bOverflow);
210 CPPUNIT_ASSERT(!bOverflow);
211 CPPUNIT_ASSERT_EQUAL(DateTime(Date(22, 11, 1999), Time(21, 12, 30)),
212 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV);
213 for (int i = 0; i < 20; ++i)
214 aV.Add(aN, bOverflow);
215 CPPUNIT_ASSERT(!bOverflow);
216 CPPUNIT_ASSERT_EQUAL(DateTime(Date(21, 11, 1999), Time(17, 17, 30)),
217 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV);
218 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(-1), aV.GetDays());
219 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(6), aV.GetTime().GetHour());
220 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(42), aV.GetTime().GetMin());
221 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(30), aV.GetTime().GetSec());
222 CPPUNIT_ASSERT(aV.GetTime().GetTime() < 0);
223 CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.27951388888889, aV.GetInDays(), 1E-14);
225 { // Mult()
226 const DateTime aS(Date(23, 11, 1999), Time(0, 0, 0));
227 const DateTime aE(Date(23, 11, 1999), Time(1, 23, 45));
228 const Duration aD(aS, aE);
229 bool bOverflow = true;
230 Duration aV = aD.Mult(22, bOverflow);
231 CPPUNIT_ASSERT(!bOverflow);
232 CPPUNIT_ASSERT_EQUAL(DateTime(Date(24, 11, 1999), Time(6, 42, 30)),
233 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV);
234 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), aV.GetDays());
235 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(6), aV.GetTime().GetHour());
236 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(42), aV.GetTime().GetMin());
237 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(30), aV.GetTime().GetSec());
238 CPPUNIT_ASSERT(aV.GetTime().GetTime() > 0);
239 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.27951388888889, aV.GetInDays(), 1E-14);
240 // Negative duration.
241 const Duration aN(aE, aS);
242 bOverflow = true;
243 aV = aN.Mult(22, bOverflow);
244 CPPUNIT_ASSERT(!bOverflow);
245 CPPUNIT_ASSERT_EQUAL(DateTime(Date(21, 11, 1999), Time(17, 17, 30)),
246 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV);
247 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(-1), aV.GetDays());
248 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(6), aV.GetTime().GetHour());
249 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(42), aV.GetTime().GetMin());
250 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(30), aV.GetTime().GetSec());
251 CPPUNIT_ASSERT(aV.GetTime().GetTime() < 0);
252 CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.27951388888889, aV.GetInDays(), 1E-14);
254 { // Mult() including days.
255 const Duration aD(1.5);
256 bool bOverflow = true;
257 Duration aV = aD.Mult(10, bOverflow);
258 CPPUNIT_ASSERT(!bOverflow);
259 CPPUNIT_ASSERT_DOUBLES_EQUAL(15.0, aV.GetInDays(), 0.0);
261 { // Mult() including days.
262 const Duration aD(-1.5);
263 bool bOverflow = true;
264 Duration aV = aD.Mult(10, bOverflow);
265 CPPUNIT_ASSERT(!bOverflow);
266 CPPUNIT_ASSERT_DOUBLES_EQUAL(-15.0, aV.GetInDays(), 0.0);
268 { // Mult() including days.
269 const Duration aD(1.5);
270 bool bOverflow = true;
271 Duration aV = aD.Mult(-10, bOverflow);
272 CPPUNIT_ASSERT(!bOverflow);
273 CPPUNIT_ASSERT_DOUBLES_EQUAL(-15.0, aV.GetInDays(), 0.0);
275 { // Mult() including days.
276 const Duration aD(-1.5);
277 bool bOverflow = true;
278 Duration aV = aD.Mult(-10, bOverflow);
279 CPPUNIT_ASSERT(!bOverflow);
280 CPPUNIT_ASSERT_DOUBLES_EQUAL(15.0, aV.GetInDays(), 0.0);
282 { // Mult() with overflow.
283 const Duration aD(SAL_MAX_INT32);
284 bool bOverflow = false;
285 Duration aV = aD.Mult(2, bOverflow);
286 CPPUNIT_ASSERT(bOverflow);
287 CPPUNIT_ASSERT_EQUAL(SAL_MAX_INT32, aV.GetDays());
288 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(23), aV.GetTime().GetHour());
289 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aV.GetTime().GetMin());
290 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aV.GetTime().GetSec());
291 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(Time::nanoSecPerSec - 1),
292 aV.GetTime().GetNanoSec());
294 { // Mult() with overflow.
295 const Duration aD(SAL_MIN_INT32);
296 bool bOverflow = false;
297 Duration aV = aD.Mult(2, bOverflow);
298 CPPUNIT_ASSERT(bOverflow);
299 CPPUNIT_ASSERT_EQUAL(SAL_MIN_INT32, aV.GetDays());
300 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(23), aV.GetTime().GetHour());
301 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aV.GetTime().GetMin());
302 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aV.GetTime().GetSec());
303 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(Time::nanoSecPerSec - 1),
304 aV.GetTime().GetNanoSec());
306 { // Mult() with overflow.
307 const Duration aD(SAL_MAX_INT32);
308 bool bOverflow = false;
309 Duration aV = aD.Mult(-2, bOverflow);
310 CPPUNIT_ASSERT(bOverflow);
311 CPPUNIT_ASSERT_EQUAL(SAL_MIN_INT32, aV.GetDays());
312 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(23), aV.GetTime().GetHour());
313 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aV.GetTime().GetMin());
314 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aV.GetTime().GetSec());
315 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(Time::nanoSecPerSec - 1),
316 aV.GetTime().GetNanoSec());
318 { // Mult() with overflow.
319 const Duration aD(SAL_MIN_INT32);
320 bool bOverflow = false;
321 Duration aV = aD.Mult(-2, bOverflow);
322 CPPUNIT_ASSERT(bOverflow);
323 CPPUNIT_ASSERT_EQUAL(SAL_MAX_INT32, aV.GetDays());
324 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(23), aV.GetTime().GetHour());
325 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aV.GetTime().GetMin());
326 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(59), aV.GetTime().GetSec());
327 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(Time::nanoSecPerSec - 1),
328 aV.GetTime().GetNanoSec());
330 { // Inaccurate double yielding exact duration.
331 const Time aS(15, 0, 0);
332 const Time aE(16, 0, 0);
333 const Duration aD(aE.GetTimeInDays() - aS.GetTimeInDays());
334 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), aD.GetDays());
335 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(1), aD.GetTime().GetHour());
336 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(0), aD.GetTime().GetMin());
337 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(0), aD.GetTime().GetSec());
338 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(0), aD.GetTime().GetNanoSec());
340 { // Inaccurate double yielding exact duration, negative.
341 const Time aS(15, 0, 0);
342 const Time aE(16, 0, 0);
343 const Duration aD(aS.GetTimeInDays() - aE.GetTimeInDays());
344 CPPUNIT_ASSERT(aD.IsNegative());
345 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), aD.GetDays());
346 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(1), aD.GetTime().GetHour());
347 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(0), aD.GetTime().GetMin());
348 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(0), aD.GetTime().GetSec());
349 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(0), aD.GetTime().GetNanoSec());
353 CPPUNIT_TEST_SUITE_REGISTRATION(DurationTest);
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */