1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <cppunit/TestFixture.h>
11 #include <cppunit/extensions/HelperMacros.h>
13 #include <tools/duration.hxx>
14 #include <tools/datetime.hxx>
18 class DurationTest
: public CppUnit::TestFixture
23 CPPUNIT_TEST_SUITE(DurationTest
);
24 CPPUNIT_TEST(testDuration
);
25 CPPUNIT_TEST_SUITE_END();
28 void DurationTest::testDuration()
31 const Duration
aD(Time(0), 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());
44 const DateTime aDT2
= aDT1
+ aD
;
45 CPPUNIT_ASSERT_EQUAL(aE
, aDT2
);
47 CPPUNIT_ASSERT_EQUAL(aE
, aDT1
);
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());
57 const DateTime aDT2
= aDT1
+ aD
;
58 CPPUNIT_ASSERT_EQUAL(aE
, aDT2
);
60 CPPUNIT_ASSERT_EQUAL(aE
, aDT1
);
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());
70 const DateTime aDT2
= aDT1
+ aD
;
71 CPPUNIT_ASSERT_EQUAL(aE
, aDT2
);
73 CPPUNIT_ASSERT_EQUAL(aE
, aDT1
);
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());
83 const DateTime aDT2
= aDT1
+ aD
;
84 CPPUNIT_ASSERT_EQUAL(aE
, aDT2
);
86 CPPUNIT_ASSERT_EQUAL(aE
, aDT1
);
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 DateTime
aS(Date(23, 11, 1999), Time(0, 0, 0));
109 const DateTime
aE(Date(23, 11, 1999), Time(1, 23, 45));
110 const Duration
aD(aS
, aE
);
112 bool bOverflow
= true;
113 aV
.Add(aD
, bOverflow
);
114 CPPUNIT_ASSERT(!bOverflow
);
115 CPPUNIT_ASSERT_EQUAL(DateTime(Date(23, 11, 1999), Time(2, 47, 30)),
116 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV
);
117 for (int i
= 0; i
< 20; ++i
)
118 aV
.Add(aD
, bOverflow
);
119 CPPUNIT_ASSERT(!bOverflow
);
120 CPPUNIT_ASSERT_EQUAL(DateTime(Date(24, 11, 1999), Time(6, 42, 30)),
121 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV
);
122 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), aV
.GetDays());
123 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(6), aV
.GetTime().GetHour());
124 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(42), aV
.GetTime().GetMin());
125 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(30), aV
.GetTime().GetSec());
126 CPPUNIT_ASSERT(aV
.GetTime().GetTime() > 0);
127 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.27951388888889, aV
.GetInDays(), 1E-14);
128 // Negative duration.
129 const Duration
aN(aE
, aS
);
131 aV
.Add(aN
, bOverflow
);
132 CPPUNIT_ASSERT(!bOverflow
);
133 CPPUNIT_ASSERT_EQUAL(DateTime(Date(22, 11, 1999), Time(21, 12, 30)),
134 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV
);
135 for (int i
= 0; i
< 20; ++i
)
136 aV
.Add(aN
, bOverflow
);
137 CPPUNIT_ASSERT(!bOverflow
);
138 CPPUNIT_ASSERT_EQUAL(DateTime(Date(21, 11, 1999), Time(17, 17, 30)),
139 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV
);
140 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(-1), aV
.GetDays());
141 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(6), aV
.GetTime().GetHour());
142 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(42), aV
.GetTime().GetMin());
143 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(30), aV
.GetTime().GetSec());
144 CPPUNIT_ASSERT(aV
.GetTime().GetTime() < 0);
145 CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.27951388888889, aV
.GetInDays(), 1E-14);
148 const DateTime
aS(Date(23, 11, 1999), Time(0, 0, 0));
149 const DateTime
aE(Date(23, 11, 1999), Time(1, 23, 45));
150 const Duration
aD(aS
, aE
);
151 bool bOverflow
= true;
152 Duration aV
= aD
.Mult(22, bOverflow
);
153 CPPUNIT_ASSERT(!bOverflow
);
154 CPPUNIT_ASSERT_EQUAL(DateTime(Date(24, 11, 1999), Time(6, 42, 30)),
155 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV
);
156 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), aV
.GetDays());
157 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(6), aV
.GetTime().GetHour());
158 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(42), aV
.GetTime().GetMin());
159 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(30), aV
.GetTime().GetSec());
160 CPPUNIT_ASSERT(aV
.GetTime().GetTime() > 0);
161 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.27951388888889, aV
.GetInDays(), 1E-14);
162 // Negative duration.
163 const Duration
aN(aE
, aS
);
165 aV
= aN
.Mult(22, bOverflow
);
166 CPPUNIT_ASSERT(!bOverflow
);
167 CPPUNIT_ASSERT_EQUAL(DateTime(Date(21, 11, 1999), Time(17, 17, 30)),
168 DateTime(Date(23, 11, 1999), Time(0, 0, 0)) + aV
);
169 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(-1), aV
.GetDays());
170 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(6), aV
.GetTime().GetHour());
171 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(42), aV
.GetTime().GetMin());
172 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(30), aV
.GetTime().GetSec());
173 CPPUNIT_ASSERT(aV
.GetTime().GetTime() < 0);
174 CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.27951388888889, aV
.GetInDays(), 1E-14);
176 { // Mult() including days.
177 const Duration
aD(1.5);
178 bool bOverflow
= true;
179 Duration aV
= aD
.Mult(10, bOverflow
);
180 CPPUNIT_ASSERT(!bOverflow
);
181 CPPUNIT_ASSERT_DOUBLES_EQUAL(15.0, aV
.GetInDays(), 0.0);
183 { // Mult() including days.
184 const Duration
aD(-1.5);
185 bool bOverflow
= true;
186 Duration aV
= aD
.Mult(10, bOverflow
);
187 CPPUNIT_ASSERT(!bOverflow
);
188 CPPUNIT_ASSERT_DOUBLES_EQUAL(-15.0, aV
.GetInDays(), 0.0);
190 { // Mult() including days.
191 const Duration
aD(1.5);
192 bool bOverflow
= true;
193 Duration aV
= aD
.Mult(-10, bOverflow
);
194 CPPUNIT_ASSERT(!bOverflow
);
195 CPPUNIT_ASSERT_DOUBLES_EQUAL(-15.0, aV
.GetInDays(), 0.0);
197 { // Mult() including days.
198 const Duration
aD(-1.5);
199 bool bOverflow
= true;
200 Duration aV
= aD
.Mult(-10, bOverflow
);
201 CPPUNIT_ASSERT(!bOverflow
);
202 CPPUNIT_ASSERT_DOUBLES_EQUAL(15.0, aV
.GetInDays(), 0.0);
204 { // Mult() with overflow.
205 const Duration
aD(SAL_MAX_INT32
);
206 bool bOverflow
= false;
207 Duration aV
= aD
.Mult(2, bOverflow
);
208 CPPUNIT_ASSERT(bOverflow
);
209 CPPUNIT_ASSERT_EQUAL(SAL_MAX_INT32
, aV
.GetDays());
210 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(23), aV
.GetTime().GetHour());
211 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(59), aV
.GetTime().GetMin());
212 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(59), aV
.GetTime().GetSec());
213 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32
>(Time::nanoSecPerSec
- 1),
214 aV
.GetTime().GetNanoSec());
216 { // Mult() with overflow.
217 const Duration
aD(SAL_MIN_INT32
);
218 bool bOverflow
= false;
219 Duration aV
= aD
.Mult(2, bOverflow
);
220 CPPUNIT_ASSERT(bOverflow
);
221 CPPUNIT_ASSERT_EQUAL(SAL_MIN_INT32
, aV
.GetDays());
222 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(23), aV
.GetTime().GetHour());
223 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(59), aV
.GetTime().GetMin());
224 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(59), aV
.GetTime().GetSec());
225 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32
>(Time::nanoSecPerSec
- 1),
226 aV
.GetTime().GetNanoSec());
228 { // Mult() with overflow.
229 const Duration
aD(SAL_MAX_INT32
);
230 bool bOverflow
= false;
231 Duration aV
= aD
.Mult(-2, bOverflow
);
232 CPPUNIT_ASSERT(bOverflow
);
233 CPPUNIT_ASSERT_EQUAL(SAL_MIN_INT32
, aV
.GetDays());
234 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(23), aV
.GetTime().GetHour());
235 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(59), aV
.GetTime().GetMin());
236 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(59), aV
.GetTime().GetSec());
237 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32
>(Time::nanoSecPerSec
- 1),
238 aV
.GetTime().GetNanoSec());
240 { // Mult() with overflow.
241 const Duration
aD(SAL_MIN_INT32
);
242 bool bOverflow
= false;
243 Duration aV
= aD
.Mult(-2, bOverflow
);
244 CPPUNIT_ASSERT(bOverflow
);
245 CPPUNIT_ASSERT_EQUAL(SAL_MAX_INT32
, aV
.GetDays());
246 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(23), aV
.GetTime().GetHour());
247 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(59), aV
.GetTime().GetMin());
248 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(59), aV
.GetTime().GetSec());
249 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32
>(Time::nanoSecPerSec
- 1),
250 aV
.GetTime().GetNanoSec());
252 { // Inaccurate double yielding exact duration.
253 const Time
aS(15, 0, 0);
254 const Time
aE(16, 0, 0);
255 const Duration
aD(aE
.GetTimeInDays() - aS
.GetTimeInDays());
256 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), aD
.GetDays());
257 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(1), aD
.GetTime().GetHour());
258 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(0), aD
.GetTime().GetMin());
259 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(0), aD
.GetTime().GetSec());
260 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32
>(0), aD
.GetTime().GetNanoSec());
262 { // Inaccurate double yielding exact duration, negative.
263 const Time
aS(15, 0, 0);
264 const Time
aE(16, 0, 0);
265 const Duration
aD(aS
.GetTimeInDays() - aE
.GetTimeInDays());
266 CPPUNIT_ASSERT(aD
.IsNegative());
267 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), aD
.GetDays());
268 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(1), aD
.GetTime().GetHour());
269 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(0), aD
.GetTime().GetMin());
270 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16
>(0), aD
.GetTime().GetSec());
271 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32
>(0), aD
.GetTime().GetNanoSec());
275 CPPUNIT_TEST_SUITE_REGISTRATION(DurationTest
);
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */