Improve chrono tests

This commit is contained in:
Martchus 2018-01-29 16:24:17 +01:00
parent 368fc46e47
commit 9ed143e7d5
1 changed files with 71 additions and 3 deletions

View File

@ -18,6 +18,47 @@ using namespace TestUtilities::Literals;
using namespace CPPUNIT_NS;
// compile-time checks for DateTime class
static_assert(DateTime().isNull(), "isNull()");
static_assert(DateTime(1).totalTicks() == 1, "construction with ticks");
static_assert(DateTime(2) == DateTime(2), "operator ==");
static_assert(DateTime(2) != DateTime(3), "operator !=");
static_assert(DateTime(2) < DateTime(3), "operator <");
static_assert(DateTime(3) > DateTime(2), "operator >");
static_assert(DateTime::eternity().isEternity() && !DateTime().isEternity(), "isEternity()");
static constexpr auto dateFromUnixEpoch(
DateTime::unixEpochStart() + TimeSpan::fromHours(1) + TimeSpan::fromMinutes(2) + TimeSpan::fromSeconds(3.1256789));
static_assert(dateFromUnixEpoch.dayOfWeek() == DayOfWeek::Thursday, "dayOfWeek()");
static_assert(dateFromUnixEpoch.hour() == 1, "hour()");
static_assert(dateFromUnixEpoch.minute() == 2, "minute()");
static_assert(dateFromUnixEpoch.second() == 3, "second()");
static_assert(dateFromUnixEpoch.millisecond() == 125, "millisecond()");
static_assert(dateFromUnixEpoch.microsecond() == 678, "microsecond()");
static_assert(dateFromUnixEpoch.nanosecond() == 900, "nanosecond()");
static_assert(dateFromUnixEpoch.isSameDay(DateTime::unixEpochStart()), "isSameDay()");
static_assert(!dateFromUnixEpoch.isSameDay(DateTime::unixEpochStart() + TimeSpan::fromHours(24)), "!isSameDay()");
// compile-time checks for TimeSpan class
static_assert(TimeSpan().isNull(), "isNull()");
static_assert(TimeSpan(1).totalTicks() == 1, "construction with ticks");
static_assert(TimeSpan(-1).isNegative() && !TimeSpan(1).isNegative(), "isNegative()");
static_assert(TimeSpan::infinity().isInfinity() && !TimeSpan().isInfinity(), "isInfinity()");
static_assert(TimeSpan::negativeInfinity().isNegativeInfinity() && !TimeSpan().isNegativeInfinity(), "isNegativeInfinity()");
static_assert(TimeSpan::fromMilliseconds(1.0125).nanoseconds() == 500, "fromMilliseconds()/nanoseconds()");
static_assert(TimeSpan::fromMilliseconds(1.0125).microseconds() == 12, "fromMilliseconds()/microseconds()");
static_assert(TimeSpan::fromMilliseconds(1.0125).milliseconds() == 1, "fromMilliseconds()/milliseconds()");
static_assert(TimeSpan::fromSeconds(61).seconds() == 1, "fromSeconds()/seconds()");
static_assert(TimeSpan::fromSeconds(61).minutes() == 1, "fromSeconds()/minutes()");
static_assert(TimeSpan::fromMinutes(61).minutes() == 1, "fromMinutes()/minutes()");
static_assert(TimeSpan::fromHours(25).hours() == 1, "fromMinutes()/hours()");
static_assert(TimeSpan::fromDays(20.5).days() == 20, "fromDays()/days()");
static_assert(TimeSpan::fromMinutes(1.5).totalMicroseconds() == 90e6, "totalMicroseconds()");
static_assert(TimeSpan::fromMinutes(1.5).totalMilliseconds() == 90e3, "totalMilliseconds()");
static_assert(TimeSpan::fromMinutes(1.5).totalSeconds() == 90.0, "totalSeconds()");
static_assert(TimeSpan::fromHours(1.5).totalMinutes() == 90.0, "totalMinutes()");
static_assert(TimeSpan::fromDays(1.5).totalHours() == 36.0, "totalHours()");
static_assert(TimeSpan::fromDays(20.5).totalDays() == 20.5, "totalDays()");
/*!
* \brief The ChronoTests class tests classes and methods of the ChronoUtilities namespace.
* \remarks Before comitting any changes to this test, run with different timezones to prevent
@ -56,16 +97,22 @@ CPPUNIT_TEST_SUITE_REGISTRATION(ChronoTests);
void ChronoTests::testDateTime()
{
// test year(), month(), ...
CPPUNIT_ASSERT_EQUAL(DateTime::daysInMonth(2000, 2), 29);
CPPUNIT_ASSERT_EQUAL(DateTime::daysInMonth(2001, 2), 28);
CPPUNIT_ASSERT_EQUAL(DateTime::daysInMonth(2100, 2), 28);
const auto test1 = DateTime::fromDateAndTime(2012, 2, 29, 15, 34, 20, 33.0);
CPPUNIT_ASSERT_EQUAL(2012, test1.year());
CPPUNIT_ASSERT_EQUAL(2, test1.month());
CPPUNIT_ASSERT_EQUAL(29, test1.day());
CPPUNIT_ASSERT_EQUAL(15, test1.hour());
CPPUNIT_ASSERT_EQUAL(34, test1.minute());
CPPUNIT_ASSERT_EQUAL(20, test1.second());
CPPUNIT_ASSERT_EQUAL(33, test1.millisecond());
CPPUNIT_ASSERT(test1.dayOfWeek() == DayOfWeek::Wednesday);
CPPUNIT_ASSERT_EQUAL(DayOfWeek::Wednesday, test1.dayOfWeek());
CPPUNIT_ASSERT_EQUAL((31 + 29), test1.dayOfYear());
CPPUNIT_ASSERT(test1.isLeapYear());
CPPUNIT_ASSERT(test1.isSameDay(test1 + TimeSpan::fromHours(8)));
CPPUNIT_ASSERT(!test1.isSameDay(test1 + TimeSpan::fromHours(9)));
CPPUNIT_ASSERT_EQUAL("Wed 2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
// test fromTimeStamp()
@ -87,18 +134,39 @@ void ChronoTests::testDateTime()
// test fromString()/toString()
CPPUNIT_ASSERT_EQUAL(test1, DateTime::fromString("2012-02-29 15:34:20.033"));
CPPUNIT_ASSERT_EQUAL_MESSAGE("surplus parts ignored", test1, DateTime::fromString("2012-02-29 15:34:20.033:12"));
CPPUNIT_ASSERT_EQUAL("2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateAndTime, false));
CPPUNIT_ASSERT_THROW(TimeSpan::fromString("2012-02-29 15:34:34:20.033"), ConversionException);
const auto test3 = DateTime::fromIsoString("2016-08-29T21:32:31.125+02:00");
CPPUNIT_ASSERT_EQUAL("2016-08-29T21:32:31.125+02:00"s, test3.first.toIsoString(test3.second));
CPPUNIT_ASSERT_THROW(DateTime::fromString("#"), ConversionException);
// test accuracy (of 100 nanoseconds)
const auto test4 = DateTime::fromIsoString("2017-08-23T19:40:15.985077682+02:00");
const auto test4 = DateTime::fromIsoString("2017-08-23T19:40:15.985077682+02:30");
CPPUNIT_ASSERT_EQUAL(2.5, test4.second.totalHours());
CPPUNIT_ASSERT_EQUAL(15, test4.first.second());
CPPUNIT_ASSERT_EQUAL(985, test4.first.millisecond());
CPPUNIT_ASSERT_EQUAL(77, test4.first.microsecond());
CPPUNIT_ASSERT_EQUAL(600, test4.first.nanosecond());
CPPUNIT_ASSERT_EQUAL("2017-08-23T19:40:15.9850776+02:00"s, test4.first.toIsoString(test4.second));
CPPUNIT_ASSERT_EQUAL("2017-08-23T19:40:15.9850776+02:30"s, test4.first.toIsoString(test4.second));
// test negative delta
const auto test5 = DateTime::fromIsoString("2017-08-23T19:40:15.985077682-02:30");
CPPUNIT_ASSERT_EQUAL(-2.5, test5.second.totalHours());
CPPUNIT_ASSERT_EQUAL(15, test5.first.second());
CPPUNIT_ASSERT_EQUAL(985, test5.first.millisecond());
CPPUNIT_ASSERT_EQUAL(77, test5.first.microsecond());
CPPUNIT_ASSERT_EQUAL(600, test5.first.nanosecond());
CPPUNIT_ASSERT_EQUAL("2017-08-23T19:40:15.9850776-02:30"s, test5.first.toIsoString(test5.second));
// test further variants
CPPUNIT_ASSERT_EQUAL_MESSAGE("Zulu time", TimeSpan(), DateTime::fromIsoString("2017-08-23T19:40:15.985077682Z").second);
CPPUNIT_ASSERT_EQUAL_MESSAGE("no minutes", TimeSpan::fromHours(3), DateTime::fromIsoString("2017-08-23T19:40:15.985077682+03").second);
// test invalid characters
CPPUNIT_ASSERT_THROW_MESSAGE("digits after Z", DateTime::fromIsoString("2017-O8-23T19:40:15.985077682Z02:00"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid letter", DateTime::fromIsoString("2017-O8-23T19:40:15.985077682:+02:00"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid T", DateTime::fromIsoString("2017-08-23T19:T40:15.985077682+02:00"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid -", DateTime::fromIsoString("2017-08-23T19:40-15.985077682+02:00"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid .", DateTime::fromIsoString("2017-08.5-23T19:40:15.985077682+02:00"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid :", DateTime::fromIsoString("2017:08-23T19:40:15.985077682+02:00"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid :", DateTime::fromIsoString("2017-08-23T19:40:15:985077682+02:00"), ConversionException);
// test now() and exactNow() (or at least whether both behave the same)
#if defined(PLATFORM_UNIX)