C++ Utilities  4.6.1
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/timespan.h"
3 #include "../chrono/period.h"
4 #include "../chrono/format.h"
5 #include "../conversion/conversionexception.h"
6 
7 #include <cppunit/extensions/HelperMacros.h>
8 #include <cppunit/TestFixture.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 {
23  CPPUNIT_TEST_SUITE(ChronoTests);
24  CPPUNIT_TEST(testDateTime);
25  CPPUNIT_TEST(testTimeSpan);
26  CPPUNIT_TEST(testOperators);
27  CPPUNIT_TEST_SUITE_END();
28 
29 public:
30  void setUp() {}
31  void tearDown() {}
32 
33  void testDateTime();
34  void testTimeSpan();
35  void testOperators();
36 };
37 
39 
44 {
45  // test year(), month(), ...
46  const auto test1 = DateTime::fromDateAndTime(2012, 2, 29, 15, 34, 20, 33.0);
47  CPPUNIT_ASSERT_EQUAL(2012, test1.year());
48  CPPUNIT_ASSERT_EQUAL(2, test1.month());
49  CPPUNIT_ASSERT_EQUAL(29, test1.day());
50  CPPUNIT_ASSERT_EQUAL(34, test1.minute());
51  CPPUNIT_ASSERT_EQUAL(20, test1.second());
52  CPPUNIT_ASSERT_EQUAL(33, test1.millisecond());
53  CPPUNIT_ASSERT(test1.dayOfWeek() == DayOfWeek::Wednesday);
54  CPPUNIT_ASSERT_EQUAL((31 + 29), test1.dayOfYear());
55  CPPUNIT_ASSERT(test1.isLeapYear());
56  CPPUNIT_ASSERT_EQUAL("Wed 2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
57 
58  // test fromTimeStamp()
59  const auto test2 = DateTime::fromTimeStampGmt(1453840331);
60  CPPUNIT_ASSERT_EQUAL("Tue 2016-01-26 20:32:11"s, test2.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
61 
62  // test whether ConversionException() is thrown when invalid values are specified
63  CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2013, 2, 29, 15, 34, 20, 33), ConversionException);
64  CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 2, 29, 15, 61, 20, 33), ConversionException);
65  CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 4, 31, 15, 0, 20, 33), ConversionException);
66  CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 3, 31, 15, 0, 61, 33), ConversionException);
67 
68  // test fromString()/toString()
69  CPPUNIT_ASSERT_EQUAL(test1, DateTime::fromString("2012-02-29 15:34:20.033"));
70  CPPUNIT_ASSERT_EQUAL("2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateAndTime, false));
71  CPPUNIT_ASSERT_THROW(TimeSpan::fromString("2012-02-29 15:34:34:20.033"), ConversionException);
72  const auto test3 = DateTime::fromIsoString("2016-08-29T21:32:31.125+02:00");
73  CPPUNIT_ASSERT_EQUAL("2016-08-29T21:32:31.125+02:00"s, test3.first.toIsoString(test3.second));
74 }
75 
80 {
81  // test fromString(...), this should also test all other from...() methods and + operator
82  auto test1 = TimeSpan::fromString("2:34:53:2.5");
83  // test days(), hours(), ...
84  CPPUNIT_ASSERT_EQUAL(3, test1.days());
85  CPPUNIT_ASSERT_EQUAL(10, test1.hours());
86  CPPUNIT_ASSERT_EQUAL(53, test1.minutes());
87  CPPUNIT_ASSERT_EQUAL(2, test1.seconds());
88  CPPUNIT_ASSERT_EQUAL(500, test1.milliseconds());
89  CPPUNIT_ASSERT(test1.totalDays() > 3.0 && test1.totalDays() < 4.0);
90  CPPUNIT_ASSERT(test1.totalHours() > (2 * 24 + 34) && test1.totalHours() < (2 * 24 + 35));
91  CPPUNIT_ASSERT(test1.totalMinutes() > (2 * 24 * 60 + 34 * 60 + 53) && test1.totalHours() < (2 * 24 * 60 + 34 * 60 + 54));
92  CPPUNIT_ASSERT_EQUAL("3 d 10 h 53 min 2 s 500 ms"s, test1.toString(TimeSpanOutputFormat::WithMeasures, false));
93 
94  // test whether ConversionException() is thrown when invalid values are specified
95  CPPUNIT_ASSERT_THROW(TimeSpan::fromString("2:34a:53:32.5"), ConversionException);
96 }
97 
102 {
103  auto dateTime = DateTime::fromDateAndTime(1999, 1, 5, 4, 16);
104  CPPUNIT_ASSERT_EQUAL(7, (dateTime + TimeSpan::fromDays(2)).day());
105  CPPUNIT_ASSERT_EQUAL(6, (dateTime + TimeSpan::fromHours(24)).day());
106  CPPUNIT_ASSERT_EQUAL(3, (dateTime + TimeSpan::fromHours(24) + TimeSpan::fromHours(-1)).hour());
107  CPPUNIT_ASSERT_EQUAL(17, (dateTime + TimeSpan::fromHours(24) - TimeSpan::fromMinutes(-1)).minute());
108  dateTime += TimeSpan::fromDays(365);
109  CPPUNIT_ASSERT_EQUAL(2000, dateTime.year());
110  CPPUNIT_ASSERT_EQUAL(5, dateTime.day());
111  CPPUNIT_ASSERT_EQUAL(2, Period(dateTime, dateTime + TimeSpan::fromDays(62)).months());
112 }
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 testOperators()
Tests operators of DateTime / TimeSpan.
void setUp()
Definition: chronotests.cpp:30
void testDateTime()
Tests most important DateTime features.
Definition: chronotests.cpp:43
void testTimeSpan()
Tests most important TimeSpan features.
Definition: chronotests.cpp:79
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:31