C++ Utilities  4.8.0
Common C++ classes and routines used by my applications such as argument parser, IO and conversion utilities
chronotests.cpp
Go to the documentation of this file.
1 #include "../chrono/datetime.h"
2 #include "../chrono/format.h"
3 #include "../chrono/period.h"
4 #include "../chrono/timespan.h"
5 #include "../conversion/conversionexception.h"
6 #include "../tests/testutils.h"
7 
8 #include <cppunit/TestFixture.h>
9 #include <cppunit/extensions/HelperMacros.h>
10 
11 #include <iostream>
12 
13 using namespace std;
14 using namespace ConversionUtilities;
15 using namespace ChronoUtilities;
16 using namespace TestUtilities::Literals;
17 
18 using namespace CPPUNIT_NS;
19 
23 class ChronoTests : public TestFixture {
24  CPPUNIT_TEST_SUITE(ChronoTests);
25  CPPUNIT_TEST(testDateTime);
26  CPPUNIT_TEST(testTimeSpan);
27  CPPUNIT_TEST(testOperators);
28  CPPUNIT_TEST(testHashing);
29  CPPUNIT_TEST_SUITE_END();
30 
31 public:
32  void setUp()
33  {
34  }
35  void tearDown()
36  {
37  }
38 
39  void testDateTime();
40  void testTimeSpan();
41  void testOperators();
42  void testHashing();
43 };
44 
46 
51 {
52  // test year(), month(), ...
53  const auto test1 = DateTime::fromDateAndTime(2012, 2, 29, 15, 34, 20, 33.0);
54  CPPUNIT_ASSERT_EQUAL(2012, test1.year());
55  CPPUNIT_ASSERT_EQUAL(2, test1.month());
56  CPPUNIT_ASSERT_EQUAL(29, test1.day());
57  CPPUNIT_ASSERT_EQUAL(34, test1.minute());
58  CPPUNIT_ASSERT_EQUAL(20, test1.second());
59  CPPUNIT_ASSERT_EQUAL(33, test1.millisecond());
60  CPPUNIT_ASSERT(test1.dayOfWeek() == DayOfWeek::Wednesday);
61  CPPUNIT_ASSERT_EQUAL((31 + 29), test1.dayOfYear());
62  CPPUNIT_ASSERT(test1.isLeapYear());
63  CPPUNIT_ASSERT_EQUAL("Wed 2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
64 
65  // test fromTimeStamp()
66  const auto test2 = DateTime::fromTimeStampGmt(1453840331);
67  CPPUNIT_ASSERT_EQUAL("Tue 2016-01-26 20:32:11"s, test2.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
68 
69  // test whether ConversionException() is thrown when invalid values are specified
70  CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2013, 2, 29, 15, 34, 20, 33), ConversionException);
71  CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 2, 29, 15, 61, 20, 33), ConversionException);
72  CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 4, 31, 15, 0, 20, 33), ConversionException);
73  CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 3, 31, 15, 0, 61, 33), ConversionException);
74 
75  // test fromString()/toString()
76  CPPUNIT_ASSERT_EQUAL(test1, DateTime::fromString("2012-02-29 15:34:20.033"));
77  CPPUNIT_ASSERT_EQUAL("2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateAndTime, false));
78  CPPUNIT_ASSERT_THROW(TimeSpan::fromString("2012-02-29 15:34:34:20.033"), ConversionException);
79  const auto test3 = DateTime::fromIsoString("2016-08-29T21:32:31.125+02:00");
80  CPPUNIT_ASSERT_EQUAL("2016-08-29T21:32:31.125+02:00"s, test3.first.toIsoString(test3.second));
81 
82 // test now() and exactNow() (or at least whether both behave the same)
83 #if defined(PLATFORM_UNIX)
84  const auto delta = DateTime::gmtNow() - DateTime::exactGmtNow();
85  CPPUNIT_ASSERT(delta < TimeSpan::fromSeconds(2) && delta > TimeSpan::fromSeconds(-2));
86 #endif
87 }
88 
93 {
94  // test fromString(...), this should also test all other from...() methods and + operator
95  auto test1 = TimeSpan::fromString("2:34:53:2.5");
96  // test days(), hours(), ...
97  CPPUNIT_ASSERT_EQUAL(3, test1.days());
98  CPPUNIT_ASSERT_EQUAL(10, test1.hours());
99  CPPUNIT_ASSERT_EQUAL(53, test1.minutes());
100  CPPUNIT_ASSERT_EQUAL(2, test1.seconds());
101  CPPUNIT_ASSERT_EQUAL(500, test1.milliseconds());
102  CPPUNIT_ASSERT(test1.totalDays() > 3.0 && test1.totalDays() < 4.0);
103  CPPUNIT_ASSERT(test1.totalHours() > (2 * 24 + 34) && test1.totalHours() < (2 * 24 + 35));
104  CPPUNIT_ASSERT(test1.totalMinutes() > (2 * 24 * 60 + 34 * 60 + 53) && test1.totalHours() < (2 * 24 * 60 + 34 * 60 + 54));
105  CPPUNIT_ASSERT_EQUAL("3 d 10 h 53 min 2 s 500 ms"s, test1.toString(TimeSpanOutputFormat::WithMeasures, false));
106 
107  // test whether ConversionException() is thrown when invalid values are specified
108  CPPUNIT_ASSERT_THROW(TimeSpan::fromString("2:34a:53:32.5"), ConversionException);
109 }
110 
115 {
116  auto dateTime = DateTime::fromDateAndTime(1999, 1, 5, 4, 16);
117  CPPUNIT_ASSERT_EQUAL(7, (dateTime + TimeSpan::fromDays(2)).day());
118  CPPUNIT_ASSERT_EQUAL(6, (dateTime + TimeSpan::fromHours(24)).day());
119  CPPUNIT_ASSERT_EQUAL(3, (dateTime + TimeSpan::fromHours(24) + TimeSpan::fromHours(-1)).hour());
120  CPPUNIT_ASSERT_EQUAL(17, (dateTime + TimeSpan::fromHours(24) - TimeSpan::fromMinutes(-1)).minute());
121  dateTime += TimeSpan::fromDays(365);
122  CPPUNIT_ASSERT_EQUAL(2000, dateTime.year());
123  CPPUNIT_ASSERT_EQUAL(5, dateTime.day());
124  CPPUNIT_ASSERT_EQUAL(2, Period(dateTime, dateTime + TimeSpan::fromDays(62)).months());
125 }
126 
131 {
132  set<DateTime> dateTimes;
133  dateTimes.emplace(DateTime::fromDate(2500, 2, 1));
134  dateTimes.emplace(DateTime::fromDate(2500, 2, 2));
135  dateTimes.emplace(DateTime::fromDate(2500, 2, 1));
136  CPPUNIT_ASSERT_EQUAL(2_st, dateTimes.size());
137 
138  set<TimeSpan> timeSpans;
139  timeSpans.emplace(TimeSpan::fromDays(5));
140  timeSpans.emplace(TimeSpan::fromDays(10));
141  timeSpans.emplace(TimeSpan::fromDays(5));
142  CPPUNIT_ASSERT_EQUAL(2_st, timeSpans.size());
143 }
CPPUNIT_TEST_SUITE_REGISTRATION(ChronoTests)
Contains classes providing a means for handling date and time information.
Definition: datetime.h:12
The ConversionException class is thrown by the various conversion functions of this library when a co...
STL namespace.
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:197
void testHashing()
Tests hashing DateTime / TimeSpan by using in a set.
void testOperators()
Tests operators of DateTime / TimeSpan.
void setUp()
Definition: chronotests.cpp:32
void testDateTime()
Tests most important DateTime features.
Definition: chronotests.cpp:50
void testTimeSpan()
Tests most important TimeSpan features.
Definition: chronotests.cpp:92
Represents a period of time.
Definition: period.h:8
The ChronoTests class tests classes and methods of the ChronoUtilities namespace. ...
Definition: chronotests.cpp:23
Contains several functions providing conversions between different data types.
void tearDown()
Definition: chronotests.cpp:35