Bump version to 6.4-15
[LibreOffice.git] / tools / qa / cppunit / test_date.cxx
blobca6c39d841092297136960e15a7099c79827c7c0
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/date.hxx>
15 namespace tools
18 class DateTest : public CppUnit::TestFixture
20 public:
21 void testDate();
22 void testLeapYear();
23 void testGetDaysInYear();
24 void testValidGregorianDate();
25 void testValidDate();
26 void testNormalize();
27 void testGetDayOfWeek();
28 void testGetDaysInMonth();
29 void testIsBetween();
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);
57 aDate = aBCE;
58 aDate.AddDays(1);
59 CPPUNIT_ASSERT_EQUAL( aCE.GetDate(), aDate.GetDate());
60 aDate = aCE;
61 aDate.AddDays(-1);
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.
69 aDate = aMin;
70 aDate.AddDays(-1);
71 CPPUNIT_ASSERT_EQUAL( aMin.GetDate(), aDate.GetDate());
72 aDate = aMax;
73 aDate.AddDays(1);
74 CPPUNIT_ASSERT_EQUAL( aMax.GetDate(), aDate.GetDate());
75 aDate = aBCE;
76 aDate.AddDays(kMinDays-10);
77 CPPUNIT_ASSERT_EQUAL( aMin.GetDate(), aDate.GetDate());
78 aDate = aBCE;
79 aDate.AddDays(kMaxDays+10);
80 CPPUNIT_ASSERT_EQUAL( aMax.GetDate(), aDate.GetDate());
81 aDate = aMax;
82 aDate.SetDay(32);
83 aDate.Normalize();
84 CPPUNIT_ASSERT_EQUAL( aMax.GetDate(), aDate.GetDate());
85 CPPUNIT_ASSERT(!aDate.IsEmpty());
87 // 0001-00-x normalized to -0001-12-x
88 aDate.SetYear(1);
89 aDate.SetMonth(0);
90 aDate.SetDay(22);
91 aDate.Normalize();
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
97 aDate.SetYear(1999);
98 aDate.SetMonth(2);
99 aDate.SetDay(32);
100 aDate.Normalize();
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 );
105 aDate.Normalize();
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 );
112 aDate.SetMonth(1);
113 aDate.SetDay(0);
114 aDate.Normalize();
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?)
119 aDate.SetYear(1999);
120 aDate.SetMonth(0);
121 aDate.SetDay(0);
122 aDate.Normalize();
123 CPPUNIT_ASSERT_EQUAL( Date(31,12,1998).GetDate(), aDate.GetDate());
125 // 0001-00-00 normalized to -0001-12-31
126 aDate.SetYear(1);
127 aDate.SetMonth(0);
128 aDate.SetDay(0);
129 aDate.Normalize();
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);
163 aDate.AddDays(1);
164 CPPUNIT_ASSERT(aDate.IsLeapYear());
165 CPPUNIT_ASSERT_EQUAL( Date(29,2,-1).GetDate(), aDate.GetDate());
169 Date aDate(1,3,-1);
170 aDate.AddDays(-1);
171 CPPUNIT_ASSERT(aDate.IsLeapYear());
172 CPPUNIT_ASSERT_EQUAL( Date(29,2,-1).GetDate(), aDate.GetDate());
176 // Year -5 is a leap year.
177 Date aDate(28,2,-5);
178 aDate.AddDays(1);
179 CPPUNIT_ASSERT(aDate.IsLeapYear());
180 CPPUNIT_ASSERT_EQUAL( Date(29,2,-5).GetDate(), aDate.GetDate());
184 Date aDate(1,3,-5);
185 aDate.AddDays(-1);
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);
391 aDate.Normalize();
392 Date aExpectedDate(4, 3, 1999);
393 CPPUNIT_ASSERT_EQUAL(aExpectedDate, aDate);
397 Date aDate(1, 13, 1999);
398 aDate.Normalize();
399 Date aExpectedDate(1, 1, 2000);
400 CPPUNIT_ASSERT_EQUAL(aExpectedDate, aDate);
404 Date aDate(42, 13, 1999);
405 aDate.Normalize();
406 Date aExpectedDate(11, 2, 2000);
407 CPPUNIT_ASSERT_EQUAL(aExpectedDate, aDate);
411 Date aDate(1, 0, 1);
412 aDate.Normalize();
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: */