From 02391f3f02ba4725d2cf2d94e5fef3d8761364d9 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Sat, 23 Apr 2005 09:03:10 +0000 Subject: [PATCH] Added unit tests for date comparison + fixed date comparison (again...). --- src/dateTime.cpp | 48 +++++++++++++++++------------------ tests/parser/datetimeTest.cpp | 37 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/src/dateTime.cpp b/src/dateTime.cpp index d2f0d5b6..d2807a2a 100644 --- a/src/dateTime.cpp +++ b/src/dateTime.cpp @@ -793,12 +793,12 @@ const bool datetime::operator<(const datetime& other) const const datetime ut1 = utility::datetimeUtils::localTimeToUniversalTime(*this); const datetime ut2 = utility::datetimeUtils::localTimeToUniversalTime(other); - return ((ut1.m_year < ut2.m_year) || - (ut1.m_year == ut2.m_year && ut1.m_month < ut2.m_month) || - (ut1.m_month == ut2.m_month && ut1.m_day < ut2.m_day) || - (ut1.m_day == ut2.m_day && ut1.m_hour < ut2.m_hour) || - (ut1.m_hour == ut2.m_hour && ut1.m_minute < ut2.m_minute) || - (ut1.m_minute == ut2.m_minute && ut1.m_second < ut2.m_second)); + return ((ut1.m_year < ut2.m_year) || + ((ut1.m_year == ut2.m_year) && ((ut1.m_month < ut2.m_month) || + ((ut1.m_month == ut2.m_month) && ((ut1.m_day < ut2.m_day) || + ((ut1.m_day == ut2.m_day) && ((ut1.m_hour < ut2.m_hour) || + ((ut1.m_hour == ut2.m_hour) && ((ut1.m_minute < ut2.m_minute) || + ((ut1.m_minute == ut2.m_minute) && ((ut1.m_second < ut2.m_second)))))))))))); } @@ -807,12 +807,12 @@ const bool datetime::operator<=(const datetime& other) const const datetime ut1 = utility::datetimeUtils::localTimeToUniversalTime(*this); const datetime ut2 = utility::datetimeUtils::localTimeToUniversalTime(other); - return ((ut1.m_year < ut2.m_year) || - (ut1.m_year == ut2.m_year && ut1.m_month < ut2.m_month) || - (ut1.m_month == ut2.m_month && ut1.m_day < ut2.m_day) || - (ut1.m_day == ut2.m_day && ut1.m_hour < ut2.m_hour) || - (ut1.m_hour == ut2.m_hour && ut1.m_minute < ut2.m_minute) || - (ut1.m_minute == ut2.m_minute && ut1.m_second <= ut2.m_second)); + return ((ut1.m_year < ut2.m_year) || + ((ut1.m_year == ut2.m_year) && ((ut1.m_month < ut2.m_month) || + ((ut1.m_month == ut2.m_month) && ((ut1.m_day < ut2.m_day) || + ((ut1.m_day == ut2.m_day) && ((ut1.m_hour < ut2.m_hour) || + ((ut1.m_hour == ut2.m_hour) && ((ut1.m_minute < ut2.m_minute) || + ((ut1.m_minute == ut2.m_minute) && ((ut1.m_second <= ut2.m_second)))))))))))); } @@ -821,12 +821,12 @@ const bool datetime::operator>(const datetime& other) const const datetime ut1 = utility::datetimeUtils::localTimeToUniversalTime(*this); const datetime ut2 = utility::datetimeUtils::localTimeToUniversalTime(other); - return ((ut1.m_year > ut2.m_year) || - (ut1.m_year == ut2.m_year && ut1.m_month > ut2.m_month) || - (ut1.m_month == ut2.m_month && ut1.m_day > ut2.m_day) || - (ut1.m_day == ut2.m_day && ut1.m_hour > ut2.m_hour) || - (ut1.m_hour == ut2.m_hour && ut1.m_minute > ut2.m_minute) || - (ut1.m_minute == ut2.m_minute && ut1.m_second > ut2.m_second)); + return ((ut1.m_year > ut2.m_year) || + ((ut1.m_year == ut2.m_year) && ((ut1.m_month > ut2.m_month) || + ((ut1.m_month == ut2.m_month) && ((ut1.m_day > ut2.m_day) || + ((ut1.m_day == ut2.m_day) && ((ut1.m_hour > ut2.m_hour) || + ((ut1.m_hour == ut2.m_hour) && ((ut1.m_minute > ut2.m_minute) || + ((ut1.m_minute == ut2.m_minute) && (ut1.m_second > ut2.m_second))))))))))); } @@ -835,12 +835,12 @@ const bool datetime::operator>=(const datetime& other) const const datetime ut1 = utility::datetimeUtils::localTimeToUniversalTime(*this); const datetime ut2 = utility::datetimeUtils::localTimeToUniversalTime(other); - return ((ut1.m_year > ut2.m_year) || - (ut1.m_year == ut2.m_year && ut1.m_month > ut2.m_month) || - (ut1.m_month == ut2.m_month && ut1.m_day > ut2.m_day) || - (ut1.m_day == ut2.m_day && ut1.m_hour > ut2.m_hour) || - (ut1.m_hour == ut2.m_hour && ut1.m_minute > ut2.m_minute) || - (ut1.m_minute == ut2.m_minute && ut1.m_second >= ut2.m_second)); + return ((ut1.m_year > ut2.m_year) || + ((ut1.m_year == ut2.m_year) && ((ut1.m_month > ut2.m_month) || + ((ut1.m_month == ut2.m_month) && ((ut1.m_day > ut2.m_day) || + ((ut1.m_day == ut2.m_day) && ((ut1.m_hour > ut2.m_hour) || + ((ut1.m_hour == ut2.m_hour) && ((ut1.m_minute > ut2.m_minute) || + ((ut1.m_minute == ut2.m_minute) && (ut1.m_second >= ut2.m_second))))))))))); } diff --git a/tests/parser/datetimeTest.cpp b/tests/parser/datetimeTest.cpp index 09dc7852..d372d685 100644 --- a/tests/parser/datetimeTest.cpp +++ b/tests/parser/datetimeTest.cpp @@ -86,6 +86,42 @@ namespace assert_eq("1", "Fri, 8 Jul 2005 04:05:06 +0123", d1.generate()); } + void testCompare() + { + // Date1 = Date2 + vmime::datetime d1(2005, 4, 22, 14, 6, 0, vmime::datetime::GMT2); + vmime::datetime d2(2005, 4, 22, 10, 6, 0, vmime::datetime::GMT_2); + + assert_eq("1.1", true, d1 == d2); + assert_eq("1.2", false, d1 != d2); + assert_eq("1.3", true, d1 <= d2); + assert_eq("1.4", false, d1 < d2); + assert_eq("1.5", true, d1 >= d2); + assert_eq("1.6", false, d1 > d2); + + // Date1 < Date2 + vmime::datetime d3(2005, 4, 22, 14, 6, 0); + vmime::datetime d4(2005, 4, 22, 15, 6, 0); + + assert_eq("2.1", false, d3 == d4); + assert_eq("2.2", true, d3 != d4); + assert_eq("2.3", true, d3 <= d4); + assert_eq("2.4", true, d3 < d4); + assert_eq("2.5", false, d3 >= d4); + assert_eq("2.6", false, d3 > d4); + + // Date1 > Date2 + vmime::datetime d5(2005, 4, 22, 15, 6, 0); + vmime::datetime d6(2005, 4, 22, 14, 6, 0); + + assert_eq("3.1", false, d5 == d6); + assert_eq("3.2", true, d5 != d6); + assert_eq("3.3", false, d5 <= d6); + assert_eq("3.4", false, d5 < d6); + assert_eq("3.5", true, d5 >= d6); + assert_eq("3.6", true, d5 > d6); + } + public: datetimeTest() : suite("vmime::datetime") @@ -95,6 +131,7 @@ namespace add("Parse", testcase(this, "Parse", &datetimeTest::testParse)); add("Generate", testcase(this, "Generate", &datetimeTest::testGenerate)); + add("Compare", testcase(this, "Compare", &datetimeTest::testCompare)); suite::main().add("vmime::datetime", this); }