cpp-utilities/tests/chronotests.cpp

144 lines
5.5 KiB
C++
Raw Normal View History

2016-01-27 01:01:43 +01:00
#include "../chrono/datetime.h"
#include "../chrono/format.h"
2017-05-04 22:44:00 +02:00
#include "../chrono/period.h"
#include "../chrono/timespan.h"
2016-01-27 01:01:43 +01:00
#include "../conversion/conversionexception.h"
2017-05-30 23:55:46 +02:00
#include "../tests/testutils.h"
2016-01-27 01:01:43 +01:00
#include <cppunit/TestFixture.h>
2017-05-04 22:44:00 +02:00
#include <cppunit/extensions/HelperMacros.h>
2016-01-27 01:01:43 +01:00
#include <iostream>
using namespace std;
using namespace ConversionUtilities;
using namespace ChronoUtilities;
2017-05-30 23:55:46 +02:00
using namespace TestUtilities::Literals;
2016-01-27 01:01:43 +01:00
using namespace CPPUNIT_NS;
/*!
* \brief The ChronoTests class tests classes and methods of the ChronoUtilities namespace.
*/
2017-05-04 22:44:00 +02:00
class ChronoTests : public TestFixture {
2016-01-27 01:01:43 +01:00
CPPUNIT_TEST_SUITE(ChronoTests);
CPPUNIT_TEST(testDateTime);
CPPUNIT_TEST(testTimeSpan);
CPPUNIT_TEST(testOperators);
2016-11-26 13:07:05 +01:00
CPPUNIT_TEST(testHashing);
2016-01-27 01:01:43 +01:00
CPPUNIT_TEST_SUITE_END();
public:
2017-05-04 22:44:00 +02:00
void setUp()
{
}
void tearDown()
{
}
2016-01-27 01:01:43 +01:00
void testDateTime();
void testTimeSpan();
void testOperators();
2016-11-26 13:07:05 +01:00
void testHashing();
2016-01-27 01:01:43 +01:00
};
CPPUNIT_TEST_SUITE_REGISTRATION(ChronoTests);
/*!
* \brief Tests most important DateTime features.
*/
void ChronoTests::testDateTime()
{
// test year(), month(), ...
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(34, test1.minute());
CPPUNIT_ASSERT_EQUAL(20, test1.second());
CPPUNIT_ASSERT_EQUAL(33, test1.millisecond());
2016-01-27 01:01:43 +01:00
CPPUNIT_ASSERT(test1.dayOfWeek() == DayOfWeek::Wednesday);
CPPUNIT_ASSERT_EQUAL((31 + 29), test1.dayOfYear());
2016-01-27 01:01:43 +01:00
CPPUNIT_ASSERT(test1.isLeapYear());
2017-02-06 18:23:59 +01:00
CPPUNIT_ASSERT_EQUAL("Wed 2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
2016-01-27 01:01:43 +01:00
// test fromTimeStamp()
const auto test2 = DateTime::fromTimeStampGmt(1453840331);
2017-02-06 18:23:59 +01:00
CPPUNIT_ASSERT_EQUAL("Tue 2016-01-26 20:32:11"s, test2.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
2016-01-27 01:01:43 +01:00
// test whether ConversionException() is thrown when invalid values are specified
CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2013, 2, 29, 15, 34, 20, 33), ConversionException);
CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 2, 29, 15, 61, 20, 33), ConversionException);
CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 4, 31, 15, 0, 20, 33), ConversionException);
CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 3, 31, 15, 0, 61, 33), ConversionException);
// test fromString()/toString()
CPPUNIT_ASSERT_EQUAL(test1, DateTime::fromString("2012-02-29 15:34:20.033"));
2017-02-06 18:23:59 +01:00
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");
2017-02-06 18:23:59 +01:00
CPPUNIT_ASSERT_EQUAL("2016-08-29T21:32:31.125+02:00"s, test3.first.toIsoString(test3.second));
2017-05-04 22:44:00 +02:00
// test now() and exactNow() (or at least whether both behave the same)
#if defined(PLATFORM_UNIX)
const auto delta = DateTime::gmtNow() - DateTime::exactGmtNow();
CPPUNIT_ASSERT(delta < TimeSpan::fromSeconds(2) && delta > TimeSpan::fromSeconds(-2));
#endif
2016-01-27 01:01:43 +01:00
}
/*!
* \brief Tests most important TimeSpan features.
*/
void ChronoTests::testTimeSpan()
{
// test fromString(...), this should also test all other from...() methods and + operator
auto test1 = TimeSpan::fromString("2:34:53:2.5");
// test days(), hours(), ...
CPPUNIT_ASSERT_EQUAL(3, test1.days());
CPPUNIT_ASSERT_EQUAL(10, test1.hours());
CPPUNIT_ASSERT_EQUAL(53, test1.minutes());
CPPUNIT_ASSERT_EQUAL(2, test1.seconds());
CPPUNIT_ASSERT_EQUAL(500, test1.milliseconds());
2016-01-27 01:01:43 +01:00
CPPUNIT_ASSERT(test1.totalDays() > 3.0 && test1.totalDays() < 4.0);
CPPUNIT_ASSERT(test1.totalHours() > (2 * 24 + 34) && test1.totalHours() < (2 * 24 + 35));
CPPUNIT_ASSERT(test1.totalMinutes() > (2 * 24 * 60 + 34 * 60 + 53) && test1.totalHours() < (2 * 24 * 60 + 34 * 60 + 54));
2017-02-06 18:23:59 +01:00
CPPUNIT_ASSERT_EQUAL("3 d 10 h 53 min 2 s 500 ms"s, test1.toString(TimeSpanOutputFormat::WithMeasures, false));
2016-01-27 01:01:43 +01:00
// test whether ConversionException() is thrown when invalid values are specified
CPPUNIT_ASSERT_THROW(TimeSpan::fromString("2:34a:53:32.5"), ConversionException);
}
/*!
* \brief Tests operators of DateTime / TimeSpan.
*/
void ChronoTests::testOperators()
{
auto dateTime = DateTime::fromDateAndTime(1999, 1, 5, 4, 16);
CPPUNIT_ASSERT_EQUAL(7, (dateTime + TimeSpan::fromDays(2)).day());
CPPUNIT_ASSERT_EQUAL(6, (dateTime + TimeSpan::fromHours(24)).day());
CPPUNIT_ASSERT_EQUAL(3, (dateTime + TimeSpan::fromHours(24) + TimeSpan::fromHours(-1)).hour());
CPPUNIT_ASSERT_EQUAL(17, (dateTime + TimeSpan::fromHours(24) - TimeSpan::fromMinutes(-1)).minute());
2016-01-27 01:01:43 +01:00
dateTime += TimeSpan::fromDays(365);
CPPUNIT_ASSERT_EQUAL(2000, dateTime.year());
CPPUNIT_ASSERT_EQUAL(5, dateTime.day());
CPPUNIT_ASSERT_EQUAL(2, Period(dateTime, dateTime + TimeSpan::fromDays(62)).months());
2016-01-27 01:01:43 +01:00
}
2016-11-26 13:07:05 +01:00
/*!
* \brief Tests hashing DateTime / TimeSpan by using in a set.
*/
void ChronoTests::testHashing()
{
set<DateTime> dateTimes;
dateTimes.emplace(DateTime::fromDate(2500, 2, 1));
dateTimes.emplace(DateTime::fromDate(2500, 2, 2));
dateTimes.emplace(DateTime::fromDate(2500, 2, 1));
2017-05-30 23:55:46 +02:00
CPPUNIT_ASSERT_EQUAL(2_st, dateTimes.size());
2016-11-26 13:07:05 +01:00
set<TimeSpan> timeSpans;
timeSpans.emplace(TimeSpan::fromDays(5));
timeSpans.emplace(TimeSpan::fromDays(10));
timeSpans.emplace(TimeSpan::fromDays(5));
2017-05-30 23:55:46 +02:00
CPPUNIT_ASSERT_EQUAL(2_st, timeSpans.size());
2016-11-26 13:07:05 +01:00
}