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