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/date.hxx>
18 class DateTest
: public CppUnit::TestFixture
23 void testGetDaysInYear();
24 void testValidGregorianDate();
27 void testGetDayOfWeek();
28 void testGetDaysInMonth();
31 CPPUNIT_TEST_SUITE(DateTest
);
32 CPPUNIT_TEST(testDate
);
33 CPPUNIT_TEST(testLeapYear
);
34 CPPUNIT_TEST(testGetDaysInYear
);
35 CPPUNIT_TEST(testValidGregorianDate
);
36 CPPUNIT_TEST(testValidDate
);
37 CPPUNIT_TEST(testNormalize
);
38 CPPUNIT_TEST(testGetDayOfWeek
);
39 CPPUNIT_TEST(testGetDaysInMonth
);
40 CPPUNIT_TEST(testIsBetween
);
41 CPPUNIT_TEST_SUITE_END();
44 void DateTest::testDate()
46 const Date
aCE(1,1,1); // first day CE
47 const Date
aBCE(31,12,-1); // last day BCE
48 const Date
aMin(1,1,-32768); // minimum date
49 const Date
aMax(31,12,32767); // maximum date
50 Date
aDate(Date::EMPTY
);
51 const sal_Int32 kMinDays
= -11968265;
52 const sal_Int32 kMaxDays
= 11967900;
54 // Last day BCE to first day CE is 1 day difference.
55 CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32
>(1), aCE
- aBCE
);
56 CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32
>(-1), aBCE
- aCE
);
59 CPPUNIT_ASSERT_EQUAL( aCE
.GetDate(), aDate
.GetDate());
62 CPPUNIT_ASSERT_EQUAL( aBCE
.GetDate(), aDate
.GetDate());
64 // The entire BCE and CE ranges cover that many days. Day 0 is -0001-12-31
65 CPPUNIT_ASSERT_EQUAL( kMaxDays
, aMax
- aBCE
);
66 CPPUNIT_ASSERT_EQUAL( kMinDays
, aMin
- aBCE
);
68 // Truncate at limits, not under-/overflow or wrap.
71 CPPUNIT_ASSERT_EQUAL( aMin
.GetDate(), aDate
.GetDate());
74 CPPUNIT_ASSERT_EQUAL( aMax
.GetDate(), aDate
.GetDate());
76 aDate
.AddDays(kMinDays
-10);
77 CPPUNIT_ASSERT_EQUAL( aMin
.GetDate(), aDate
.GetDate());
79 aDate
.AddDays(kMaxDays
+10);
80 CPPUNIT_ASSERT_EQUAL( aMax
.GetDate(), aDate
.GetDate());
84 CPPUNIT_ASSERT_EQUAL( aMax
.GetDate(), aDate
.GetDate());
85 CPPUNIT_ASSERT(!aDate
.IsEmpty());
87 // 0001-00-x normalized to -0001-12-x
92 CPPUNIT_ASSERT_EQUAL( Date(22,12,-1).GetDate(), aDate
.GetDate());
94 sal_uInt32 nExpected
= 11222;
95 CPPUNIT_ASSERT_EQUAL(nExpected
, aDate
.GetDateUnsigned());
96 // 1999-02-32 normalized to 1999-03-04
101 CPPUNIT_ASSERT_EQUAL( Date(4,3,1999).GetDate(), aDate
.GetDate());
103 // Empty date is not normalized and stays empty date.
104 aDate
= Date( Date::EMPTY
);
106 CPPUNIT_ASSERT_EQUAL( Date(Date::EMPTY
).GetDate(), aDate
.GetDate());
107 CPPUNIT_ASSERT( !aDate
.IsValidDate()); // GetDate() also shall have no normalizing side effect
109 // 0000-01-00 normalized to -0001-12-31
110 // SetYear(0) asserts, use empty date to force.
111 aDate
= Date( Date::EMPTY
);
115 CPPUNIT_ASSERT_EQUAL( Date(31,12,-1).GetDate(), aDate
.GetDate());
117 // 1999-00-00 normalized to 1998-12-31 (not 1998-11-30, or otherwise
118 // also 0001-00-00 should be -0001-11-30 which it should not, should it?)
123 CPPUNIT_ASSERT_EQUAL( Date(31,12,1998).GetDate(), aDate
.GetDate());
125 // 0001-00-00 normalized to -0001-12-31
130 CPPUNIT_ASSERT_EQUAL( Date(31,12,-1).GetDate(), aDate
.GetDate());
133 void DateTest::testLeapYear()
136 Date
aDate(1, 1, 2000);
137 CPPUNIT_ASSERT(aDate
.IsLeapYear());
141 Date
aDate(1, 1, 1900);
142 CPPUNIT_ASSERT(!aDate
.IsLeapYear());
146 Date
aDate(1, 1, 1999);
147 CPPUNIT_ASSERT(!aDate
.IsLeapYear());
151 Date
aDate(1, 1, 2004);
152 CPPUNIT_ASSERT(aDate
.IsLeapYear());
156 Date
aDate(1, 1, 400);
157 CPPUNIT_ASSERT(aDate
.IsLeapYear());
161 // Year -1 is a leap year.
162 Date
aDate (28,2,-1);
164 CPPUNIT_ASSERT(aDate
.IsLeapYear());
165 CPPUNIT_ASSERT_EQUAL( Date(29,2,-1).GetDate(), aDate
.GetDate());
171 CPPUNIT_ASSERT(aDate
.IsLeapYear());
172 CPPUNIT_ASSERT_EQUAL( Date(29,2,-1).GetDate(), aDate
.GetDate());
176 // Year -5 is a leap year.
179 CPPUNIT_ASSERT(aDate
.IsLeapYear());
180 CPPUNIT_ASSERT_EQUAL( Date(29,2,-5).GetDate(), aDate
.GetDate());
186 CPPUNIT_ASSERT(aDate
.IsLeapYear());
187 CPPUNIT_ASSERT_EQUAL( Date(29,2,-5).GetDate(), aDate
.GetDate());
191 void DateTest::testGetDaysInYear()
194 Date
aDate(1, 1, 2000);
195 CPPUNIT_ASSERT_EQUAL(sal_uInt16(366), aDate
.GetDaysInYear());
199 Date
aDate(1, 1, 1900);
200 CPPUNIT_ASSERT_EQUAL(sal_uInt16(365), aDate
.GetDaysInYear());
204 Date
aDate(1, 1, 1999);
205 CPPUNIT_ASSERT_EQUAL(sal_uInt16(365), aDate
.GetDaysInYear());
209 Date
aDate(1, 1, 2004);
210 CPPUNIT_ASSERT_EQUAL(sal_uInt16(366), aDate
.GetDaysInYear());
214 Date
aDate(1, 1, 400);
215 CPPUNIT_ASSERT_EQUAL(sal_uInt16(366), aDate
.GetDaysInYear());
219 void DateTest::testValidGregorianDate()
222 Date
aDate(1, 0, 2000);
223 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
227 Date
aDate(1, 13, 2000);
228 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
232 Date
aDate(1, 1, 1581);
233 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
237 Date
aDate(1, 9, 1582);
238 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
242 Date
aDate(1, 10, 1582);
243 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
247 Date
aDate(32, 1, 2000);
248 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
252 Date
aDate(29, 2, 2000);
253 CPPUNIT_ASSERT(aDate
.IsValidAndGregorian());
257 Date
aDate(29, 2, 2001);
258 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
262 Date
aDate(32, 3, 2000);
263 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
267 Date
aDate(31, 4, 2000);
268 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
272 Date
aDate(32, 5, 2000);
273 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
277 Date
aDate(31, 6, 2000);
278 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
282 Date
aDate(32, 7, 2000);
283 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
287 Date
aDate(32, 8, 2000);
288 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
292 Date
aDate(31, 9, 2000);
293 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
297 Date
aDate(32, 10, 2000);
298 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
302 Date
aDate(31, 11, 2000);
303 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
307 Date
aDate(32, 12, 2000);
308 CPPUNIT_ASSERT(!aDate
.IsValidAndGregorian());
312 void DateTest::testValidDate()
315 // Empty date is not a valid date.
316 Date
aDate(Date::EMPTY
);
317 CPPUNIT_ASSERT(aDate
.IsEmpty());
318 CPPUNIT_ASSERT(!aDate
.IsValidDate());
322 Date
aDate(32, 1, 2000);
323 CPPUNIT_ASSERT(!aDate
.IsValidDate());
327 Date
aDate(29, 2, 2000);
328 CPPUNIT_ASSERT(aDate
.IsValidDate());
332 Date
aDate(29, 2, 2001);
333 CPPUNIT_ASSERT(!aDate
.IsValidDate());
337 Date
aDate(32, 3, 2000);
338 CPPUNIT_ASSERT(!aDate
.IsValidDate());
342 Date
aDate(31, 4, 2000);
343 CPPUNIT_ASSERT(!aDate
.IsValidDate());
347 Date
aDate(32, 5, 2000);
348 CPPUNIT_ASSERT(!aDate
.IsValidDate());
352 Date
aDate(31, 6, 2000);
353 CPPUNIT_ASSERT(!aDate
.IsValidDate());
357 Date
aDate(32, 7, 2000);
358 CPPUNIT_ASSERT(!aDate
.IsValidDate());
362 Date
aDate(32, 8, 2000);
363 CPPUNIT_ASSERT(!aDate
.IsValidDate());
367 Date
aDate(31, 9, 2000);
368 CPPUNIT_ASSERT(!aDate
.IsValidDate());
372 Date
aDate(32, 10, 2000);
373 CPPUNIT_ASSERT(!aDate
.IsValidDate());
377 Date
aDate(31, 11, 2000);
378 CPPUNIT_ASSERT(!aDate
.IsValidDate());
382 Date
aDate(32, 12, 2000);
383 CPPUNIT_ASSERT(!aDate
.IsValidDate());
387 void DateTest::testNormalize()
390 Date
aDate(32, 2, 1999);
392 Date
aExpectedDate(4, 3, 1999);
393 CPPUNIT_ASSERT_EQUAL(aExpectedDate
, aDate
);
397 Date
aDate(1, 13, 1999);
399 Date
aExpectedDate(1, 1, 2000);
400 CPPUNIT_ASSERT_EQUAL(aExpectedDate
, aDate
);
404 Date
aDate(42, 13, 1999);
406 Date
aExpectedDate(11, 2, 2000);
407 CPPUNIT_ASSERT_EQUAL(aExpectedDate
, aDate
);
413 Date
aExpectedDate(1, 12, -1);
414 CPPUNIT_ASSERT_EQUAL(aExpectedDate
, aDate
);
418 void DateTest::testGetDayOfWeek()
421 DayOfWeek eExpectedDay
= DayOfWeek::MONDAY
;
422 Date
aDate(30, 4, 2018);
423 CPPUNIT_ASSERT_EQUAL(eExpectedDay
, aDate
.GetDayOfWeek());
427 DayOfWeek eExpectedDay
= DayOfWeek::TUESDAY
;
428 Date
aDate(1, 5, 2018);
429 CPPUNIT_ASSERT_EQUAL(eExpectedDay
, aDate
.GetDayOfWeek());
433 DayOfWeek eExpectedDay
= DayOfWeek::WEDNESDAY
;
434 Date
aDate(2, 5, 2018);
435 CPPUNIT_ASSERT_EQUAL(eExpectedDay
, aDate
.GetDayOfWeek());
439 DayOfWeek eExpectedDay
= DayOfWeek::THURSDAY
;
440 Date
aDate(3, 5, 2018);
441 CPPUNIT_ASSERT_EQUAL(eExpectedDay
, aDate
.GetDayOfWeek());
445 DayOfWeek eExpectedDay
= DayOfWeek::FRIDAY
;
446 Date
aDate(4, 5, 2018);
447 CPPUNIT_ASSERT_EQUAL(eExpectedDay
, aDate
.GetDayOfWeek());
451 DayOfWeek eExpectedDay
= DayOfWeek::SATURDAY
;
452 Date
aDate(5, 5, 2018);
453 CPPUNIT_ASSERT_EQUAL(eExpectedDay
, aDate
.GetDayOfWeek());
457 DayOfWeek eExpectedDay
= DayOfWeek::SUNDAY
;
458 Date
aDate(6, 5, 2018);
459 CPPUNIT_ASSERT_EQUAL(eExpectedDay
, aDate
.GetDayOfWeek());
463 void DateTest::testGetDaysInMonth()
466 Date
aDate(1, 1, 2000);
467 CPPUNIT_ASSERT_EQUAL(sal_uInt16(31), aDate
.GetDaysInMonth());
471 Date
aDate(1, 2, 2000);
472 CPPUNIT_ASSERT_EQUAL(sal_uInt16(29), aDate
.GetDaysInMonth());
476 Date
aDate(1, 2, 1999);
477 CPPUNIT_ASSERT_EQUAL(sal_uInt16(28), aDate
.GetDaysInMonth());
481 Date
aDate(1, 3, 2000);
482 CPPUNIT_ASSERT_EQUAL(sal_uInt16(31), aDate
.GetDaysInMonth());
486 Date
aDate(1, 4, 2000);
487 CPPUNIT_ASSERT_EQUAL(sal_uInt16(30), aDate
.GetDaysInMonth());
491 Date
aDate(1, 5, 2000);
492 CPPUNIT_ASSERT_EQUAL(sal_uInt16(31), aDate
.GetDaysInMonth());
496 Date
aDate(1, 6, 2000);
497 CPPUNIT_ASSERT_EQUAL(sal_uInt16(30), aDate
.GetDaysInMonth());
501 Date
aDate(1, 7, 2000);
502 CPPUNIT_ASSERT_EQUAL(sal_uInt16(31), aDate
.GetDaysInMonth());
506 Date
aDate(1, 8, 2000);
507 CPPUNIT_ASSERT_EQUAL(sal_uInt16(31), aDate
.GetDaysInMonth());
511 Date
aDate(1, 9, 2000);
512 CPPUNIT_ASSERT_EQUAL(sal_uInt16(30), aDate
.GetDaysInMonth());
516 Date
aDate(1, 10, 2000);
517 CPPUNIT_ASSERT_EQUAL(sal_uInt16(31), aDate
.GetDaysInMonth());
521 Date
aDate(1, 11, 2000);
522 CPPUNIT_ASSERT_EQUAL(sal_uInt16(30), aDate
.GetDaysInMonth());
526 Date
aDate(1, 12, 2000);
527 CPPUNIT_ASSERT_EQUAL(sal_uInt16(31), aDate
.GetDaysInMonth());
531 void DateTest::testIsBetween()
533 Date
aDate(6, 4, 2018);
534 CPPUNIT_ASSERT(aDate
.IsBetween(Date(1, 1, 2018), Date(1, 12, 2018)));
537 CPPUNIT_TEST_SUITE_REGISTRATION(DateTest
);
541 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */