[Libreoffice-commits] core.git: include/tools tools/qa tools/source
Chris Sherlock
chris.sherlock79 at gmail.com
Mon May 7 06:30:49 UTC 2018
include/tools/date.hxx | 8
tools/qa/cppunit/test_date.cxx | 459 ++++++++++++++++++++++++++++++--
tools/source/datetime/datetimeutils.cxx | 6
3 files changed, 454 insertions(+), 19 deletions(-)
New commits:
commit e2b72039c619b64235fc7cbf12ac40b6b968f984
Author: Chris Sherlock <chris.sherlock79 at gmail.com>
Date: Sun May 6 06:07:18 2018 +1000
tools: date unit tests
Change-Id: I2b3eaf74173f7f456f04c734dfb7c05c95802809
Reviewed-on: https://gerrit.libreoffice.org/53895
Tested-by: Jenkins <ci at libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>
diff --git a/include/tools/date.hxx b/include/tools/date.hxx
index c70d3450399c..69d9e3d03ea3 100644
--- a/include/tools/date.hxx
+++ b/include/tools/date.hxx
@@ -19,10 +19,14 @@
#ifndef INCLUDED_TOOLS_DATE_HXX
#define INCLUDED_TOOLS_DATE_HXX
+#include <sal/log.hxx>
+
#include <tools/toolsdllapi.h>
+
+#include <ostream>
+
#include <com/sun/star/util/Date.hpp>
#include <com/sun/star/util/DateTime.hpp>
-#include <sal/log.hxx>
enum DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY, SUNDAY };
@@ -244,6 +248,8 @@ public:
sal_Int32 GetAsNormalizedDays() const;
};
+TOOLS_DLLPUBLIC std::ostream& operator<<(std::ostream& os, const Date& rDate);
+
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/tools/qa/cppunit/test_date.cxx b/tools/qa/cppunit/test_date.cxx
index 74d37efe5201..7e1119e93b68 100644
--- a/tools/qa/cppunit/test_date.cxx
+++ b/tools/qa/cppunit/test_date.cxx
@@ -19,9 +19,25 @@ class DateTest : public CppUnit::TestFixture
{
public:
void testDate();
+ void testLeapYear();
+ void testGetDaysInYear();
+ void testValidGregorianDate();
+ void testValidDate();
+ void testNormalize();
+ void testGetDayOfWeek();
+ void testGetDaysInMonth();
+ void testIsBetween();
CPPUNIT_TEST_SUITE(DateTest);
CPPUNIT_TEST(testDate);
+ CPPUNIT_TEST(testLeapYear);
+ CPPUNIT_TEST(testGetDaysInYear);
+ CPPUNIT_TEST(testValidGregorianDate);
+ CPPUNIT_TEST(testValidDate);
+ CPPUNIT_TEST(testNormalize);
+ CPPUNIT_TEST(testGetDayOfWeek);
+ CPPUNIT_TEST(testGetDaysInMonth);
+ CPPUNIT_TEST(testIsBetween);
CPPUNIT_TEST_SUITE_END();
};
@@ -66,10 +82,7 @@ void DateTest::testDate()
aDate.SetDay(32);
aDate.Normalize();
CPPUNIT_ASSERT_EQUAL( aMax.GetDate(), aDate.GetDate());
-
- // Empty date is not a valid date.
- aDate = Date( Date::EMPTY );
- CPPUNIT_ASSERT( !aDate.IsValidDate());
+ CPPUNIT_ASSERT(!aDate.IsEmpty());
// 0001-00-x normalized to -0001-12-x
aDate.SetYear(1);
@@ -77,6 +90,9 @@ void DateTest::testDate()
aDate.SetDay(22);
aDate.Normalize();
CPPUNIT_ASSERT_EQUAL( Date(22,12,-1).GetDate(), aDate.GetDate());
+
+ sal_uInt32 nExpected = 11222;
+ CPPUNIT_ASSERT_EQUAL(nExpected, aDate.GetDateUnsigned());
// 1999-02-32 normalized to 1999-03-04
aDate.SetYear(1999);
aDate.SetMonth(2);
@@ -112,21 +128,428 @@ void DateTest::testDate()
aDate.SetDay(0);
aDate.Normalize();
CPPUNIT_ASSERT_EQUAL( Date(31,12,-1).GetDate(), aDate.GetDate());
+}
- // Year -1 is a leap year.
- aDate = Date(28,2,-1);
- aDate.AddDays(1);
- CPPUNIT_ASSERT_EQUAL( Date(29,2,-1).GetDate(), aDate.GetDate());
- aDate = Date(1,3,-1);
- aDate.AddDays(-1);
- CPPUNIT_ASSERT_EQUAL( Date(29,2,-1).GetDate(), aDate.GetDate());
- // Year -5 is a leap year.
- aDate = Date(28,2,-5);
- aDate.AddDays(1);
- CPPUNIT_ASSERT_EQUAL( Date(29,2,-5).GetDate(), aDate.GetDate());
- aDate = Date(1,3,-5);
- aDate.AddDays(-1);
- CPPUNIT_ASSERT_EQUAL( Date(29,2,-5).GetDate(), aDate.GetDate());
+void DateTest::testLeapYear()
+{
+ {
+ Date aDate(1, 1, 2000);
+ CPPUNIT_ASSERT(aDate.IsLeapYear());
+ }
+
+ {
+ Date aDate(1, 1, 1900);
+ CPPUNIT_ASSERT(!aDate.IsLeapYear());
+ }
+
+ {
+ Date aDate(1, 1, 1999);
+ CPPUNIT_ASSERT(!aDate.IsLeapYear());
+ }
+
+ {
+ Date aDate(1, 1, 2004);
+ CPPUNIT_ASSERT(aDate.IsLeapYear());
+ }
+
+ {
+ Date aDate(1, 1, 400);
+ CPPUNIT_ASSERT(aDate.IsLeapYear());
+ }
+
+ {
+ // Year -1 is a leap year.
+ Date aDate (28,2,-1);
+ aDate.AddDays(1);
+ CPPUNIT_ASSERT(aDate.IsLeapYear());
+ CPPUNIT_ASSERT_EQUAL( Date(29,2,-1).GetDate(), aDate.GetDate());
+ }
+
+ {
+ Date aDate(1,3,-1);
+ aDate.AddDays(-1);
+ CPPUNIT_ASSERT(aDate.IsLeapYear());
+ CPPUNIT_ASSERT_EQUAL( Date(29,2,-1).GetDate(), aDate.GetDate());
+ }
+
+ {
+ // Year -5 is a leap year.
+ Date aDate(28,2,-5);
+ aDate.AddDays(1);
+ CPPUNIT_ASSERT(aDate.IsLeapYear());
+ CPPUNIT_ASSERT_EQUAL( Date(29,2,-5).GetDate(), aDate.GetDate());
+ }
+
+ {
+ Date aDate(1,3,-5);
+ aDate.AddDays(-1);
+ CPPUNIT_ASSERT(aDate.IsLeapYear());
+ CPPUNIT_ASSERT_EQUAL( Date(29,2,-5).GetDate(), aDate.GetDate());
+ }
+}
+
+void DateTest::testGetDaysInYear()
+{
+ {
+ Date aDate(1, 1, 2000);
+ sal_uInt16 nExpectedDays = 366;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInYear());
+ }
+
+ {
+ Date aDate(1, 1, 1900);
+ sal_uInt16 nExpectedDays = 365;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInYear());
+ }
+
+ {
+ Date aDate(1, 1, 1999);
+ sal_uInt16 nExpectedDays = 365;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInYear());
+ }
+
+ {
+ Date aDate(1, 1, 2004);
+ sal_uInt16 nExpectedDays = 366;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInYear());
+ }
+
+ {
+ Date aDate(1, 1, 400);
+ sal_uInt16 nExpectedDays = 366;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInYear());
+ }
+}
+
+void DateTest::testValidGregorianDate()
+{
+ {
+ Date aDate(1, 0, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(1, 13, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(1, 1, 1581);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(1, 9, 1582);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(1, 10, 1582);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(32, 1, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(29, 2, 2000);
+ CPPUNIT_ASSERT(aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(29, 2, 2001);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(32, 3, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(31, 4, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(32, 5, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(31, 6, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(32, 7, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(32, 8, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(31, 9, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(32, 10, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(31, 11, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+
+ {
+ Date aDate(32, 12, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidAndGregorian());
+ }
+}
+
+void DateTest::testValidDate()
+{
+ {
+ // Empty date is not a valid date.
+ Date aDate(Date::EMPTY);
+ CPPUNIT_ASSERT(aDate.IsEmpty());
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(32, 1, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(29, 2, 2000);
+ CPPUNIT_ASSERT(aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(29, 2, 2001);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(32, 3, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(31, 4, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(32, 5, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(31, 6, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(32, 7, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(32, 8, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(31, 9, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(32, 10, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(31, 11, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+
+ {
+ Date aDate(32, 12, 2000);
+ CPPUNIT_ASSERT(!aDate.IsValidDate());
+ }
+}
+
+void DateTest::testNormalize()
+{
+ {
+ Date aDate(32, 2, 1999);
+ aDate.Normalize();
+ Date aExpectedDate(4, 3, 1999);
+ CPPUNIT_ASSERT_EQUAL(aExpectedDate, aDate);
+ }
+
+ {
+ Date aDate(1, 13, 1999);
+ aDate.Normalize();
+ Date aExpectedDate(1, 1, 2000);
+ CPPUNIT_ASSERT_EQUAL(aExpectedDate, aDate);
+ }
+
+ {
+ Date aDate(42, 13, 1999);
+ aDate.Normalize();
+ Date aExpectedDate(11, 2, 2000);
+ CPPUNIT_ASSERT_EQUAL(aExpectedDate, aDate);
+ }
+
+ {
+ Date aDate(1, 0, 1);
+ aDate.Normalize();
+ Date aExpectedDate(1, 12, -1);
+ CPPUNIT_ASSERT_EQUAL(aExpectedDate, aDate);
+ }
+}
+
+void DateTest::testGetDayOfWeek()
+{
+ {
+ DayOfWeek eExpectedDay = DayOfWeek::MONDAY;
+ Date aDate(30, 4, 2018);
+ CPPUNIT_ASSERT_EQUAL(eExpectedDay, aDate.GetDayOfWeek());
+ }
+
+ {
+ DayOfWeek eExpectedDay = DayOfWeek::TUESDAY;
+ Date aDate(1, 5, 2018);
+ CPPUNIT_ASSERT_EQUAL(eExpectedDay, aDate.GetDayOfWeek());
+ }
+
+ {
+ DayOfWeek eExpectedDay = DayOfWeek::WEDNESDAY;
+ Date aDate(2, 5, 2018);
+ CPPUNIT_ASSERT_EQUAL(eExpectedDay, aDate.GetDayOfWeek());
+ }
+
+ {
+ DayOfWeek eExpectedDay = DayOfWeek::THURSDAY;
+ Date aDate(3, 5, 2018);
+ CPPUNIT_ASSERT_EQUAL(eExpectedDay, aDate.GetDayOfWeek());
+ }
+
+ {
+ DayOfWeek eExpectedDay = DayOfWeek::FRIDAY;
+ Date aDate(4, 5, 2018);
+ CPPUNIT_ASSERT_EQUAL(eExpectedDay, aDate.GetDayOfWeek());
+ }
+
+ {
+ DayOfWeek eExpectedDay = DayOfWeek::SATURDAY;
+ Date aDate(5, 5, 2018);
+ CPPUNIT_ASSERT_EQUAL(eExpectedDay, aDate.GetDayOfWeek());
+ }
+
+ {
+ DayOfWeek eExpectedDay = DayOfWeek::SUNDAY;
+ Date aDate(6, 5, 2018);
+ CPPUNIT_ASSERT_EQUAL(eExpectedDay, aDate.GetDayOfWeek());
+ }
+}
+
+void DateTest::testGetDaysInMonth()
+{
+ {
+ Date aDate(1, 1, 2000);
+ sal_uInt16 nExpectedDays = 31;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 2, 2000);
+ sal_uInt16 nExpectedDays = 29;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 2, 1999);
+ sal_uInt16 nExpectedDays = 28;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 3, 2000);
+ sal_uInt16 nExpectedDays = 31;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 4, 2000);
+ sal_uInt16 nExpectedDays = 30;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 5, 2000);
+ sal_uInt16 nExpectedDays = 31;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 6, 2000);
+ sal_uInt16 nExpectedDays = 30;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 7, 2000);
+ sal_uInt16 nExpectedDays = 31;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 8, 2000);
+ sal_uInt16 nExpectedDays = 31;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 9, 2000);
+ sal_uInt16 nExpectedDays = 30;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 10, 2000);
+ sal_uInt16 nExpectedDays = 31;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 11, 2000);
+ sal_uInt16 nExpectedDays = 30;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+
+ {
+ Date aDate(1, 12, 2000);
+ sal_uInt16 nExpectedDays = 31;
+ CPPUNIT_ASSERT_EQUAL(nExpectedDays, aDate.GetDaysInMonth());
+ }
+}
+
+void DateTest::testIsBetween()
+{
+ Date aDate(6, 4, 2018);
+ CPPUNIT_ASSERT(aDate.IsBetween(Date(1, 1, 2018), Date(1, 12, 2018)));
}
CPPUNIT_TEST_SUITE_REGISTRATION(DateTest);
diff --git a/tools/source/datetime/datetimeutils.cxx b/tools/source/datetime/datetimeutils.cxx
index bc1747e670a1..4c3b28d49dc6 100644
--- a/tools/source/datetime/datetimeutils.cxx
+++ b/tools/source/datetime/datetimeutils.cxx
@@ -74,3 +74,9 @@ OString DateToDDMMYYYYOString( const Date& rDate )
return aBuffer.makeStringAndClear();
}
+
+std::ostream& operator<<(std::ostream& os, const Date& rDate)
+{
+ os << rDate.GetYear() << "-" << rDate.GetMonth() << "-" << rDate.GetDay();
+ return os;
+}
More information about the Libreoffice-commits
mailing list