1 | // |
2 | // TimespanTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "TimespanTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Timespan.h" |
15 | |
16 | |
17 | using Poco::Timespan; |
18 | |
19 | |
20 | TimespanTest::TimespanTest(const std::string& rName): CppUnit::TestCase(rName) |
21 | { |
22 | } |
23 | |
24 | |
25 | TimespanTest::~TimespanTest() |
26 | { |
27 | } |
28 | |
29 | |
30 | void TimespanTest::testConversions() |
31 | { |
32 | Timespan ts; |
33 | assertTrue (ts.totalMicroseconds() == 0); |
34 | ts = Timespan::DAYS; |
35 | assertTrue (ts.totalMicroseconds() == Timespan::DAYS); |
36 | assertTrue (ts.totalMilliseconds() == 86400000); |
37 | assertTrue (ts.totalSeconds() == 86400); |
38 | assertTrue (ts.totalMinutes() == 60*24); |
39 | assertTrue (ts.totalHours() == 24); |
40 | assertTrue (ts.days() == 1); |
41 | |
42 | assertTrue (ts.microseconds() == 0); |
43 | assertTrue (ts.milliseconds() == 0); |
44 | assertTrue (ts.seconds() == 0); |
45 | assertTrue (ts.minutes() == 0); |
46 | assertTrue (ts.hours() == 0); |
47 | |
48 | ts.assign(2, 12, 30, 10, 123456); |
49 | assertTrue (ts.microseconds() == 456); |
50 | assertTrue (ts.milliseconds() == 123); |
51 | assertTrue (ts.seconds() == 10); |
52 | assertTrue (ts.minutes() == 30); |
53 | assertTrue (ts.hours() == 12); |
54 | assertTrue (ts.days() == 2); |
55 | |
56 | ts.assign(0, 36, 30, 10, 123456); |
57 | assertTrue (ts.microseconds() == 456); |
58 | assertTrue (ts.milliseconds() == 123); |
59 | assertTrue (ts.useconds() == 123456); |
60 | assertTrue (ts.seconds() == 10); |
61 | assertTrue (ts.minutes() == 30); |
62 | assertTrue (ts.hours() == 12); |
63 | assertTrue (ts.days() == 1); |
64 | |
65 | ts.assign(0, 0, 2190, 10, 123456); |
66 | assertTrue (ts.microseconds() == 456); |
67 | assertTrue (ts.milliseconds() == 123); |
68 | assertTrue (ts.useconds() == 123456); |
69 | assertTrue (ts.seconds() == 10); |
70 | assertTrue (ts.minutes() == 30); |
71 | assertTrue (ts.hours() == 12); |
72 | assertTrue (ts.days() == 1); |
73 | } |
74 | |
75 | |
76 | void TimespanTest::testComparisons() |
77 | { |
78 | Timespan ts1(10000000); |
79 | Timespan ts2(20000000); |
80 | Timespan ts3(20000000); |
81 | |
82 | assertTrue (ts1 != ts2); |
83 | assertTrue (!(ts1 == ts2)); |
84 | assertTrue (ts1 <= ts2); |
85 | assertTrue (ts1 < ts2); |
86 | assertTrue (ts2 > ts1); |
87 | assertTrue (ts2 >= ts1); |
88 | |
89 | assertTrue (ts2 == ts3); |
90 | assertTrue (!(ts2 != ts3)); |
91 | assertTrue (ts2 >= ts3); |
92 | assertTrue (ts2 <= ts3); |
93 | assertTrue (!(ts2 > ts3)); |
94 | assertTrue (!(ts2 < ts3)); |
95 | |
96 | assertTrue (ts1 == 10000000); |
97 | assertTrue (ts1 != 20000000); |
98 | assertTrue (ts1 <= 10000000); |
99 | assertTrue (ts1 <= 20000000); |
100 | assertTrue (ts1 >= 10000000); |
101 | assertTrue (ts1 >= 5000000); |
102 | assertTrue (ts1 < 20000000); |
103 | assertTrue (ts1 > 5000000); |
104 | } |
105 | |
106 | |
107 | void TimespanTest::testArithmetics() |
108 | { |
109 | Timespan ts1(100000000); |
110 | Timespan ts2(50000000); |
111 | Timespan ts3; |
112 | ts3 = ts1 + ts2; |
113 | assertTrue (ts3 == 150000000); |
114 | ts3 = ts1 + 30000000; |
115 | assertTrue (ts3 == 130000000); |
116 | ts3 = ts1 - ts2; |
117 | assertTrue (ts3 == 50000000); |
118 | ts3 = ts1 - 20000000; |
119 | assertTrue (ts3 == 80000000); |
120 | ts3 += 20000000; |
121 | assertTrue (ts3 == ts1); |
122 | ts3 -= ts2; |
123 | assertTrue (ts3 == ts2); |
124 | } |
125 | |
126 | |
127 | void TimespanTest::testSwap() |
128 | { |
129 | Timespan ts1(10000000); |
130 | Timespan ts2(50000000); |
131 | |
132 | assertTrue (ts1 < ts2); |
133 | ts1.swap(ts2); |
134 | assertTrue (ts2 < ts1); |
135 | } |
136 | |
137 | |
138 | void TimespanTest::setUp() |
139 | { |
140 | } |
141 | |
142 | |
143 | void TimespanTest::tearDown() |
144 | { |
145 | } |
146 | |
147 | |
148 | CppUnit::Test* TimespanTest::suite() |
149 | { |
150 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TimespanTest" ); |
151 | |
152 | CppUnit_addTest(pSuite, TimespanTest, testConversions); |
153 | CppUnit_addTest(pSuite, TimespanTest, testComparisons); |
154 | CppUnit_addTest(pSuite, TimespanTest, testArithmetics); |
155 | CppUnit_addTest(pSuite, TimespanTest, testSwap); |
156 | |
157 | return pSuite; |
158 | } |
159 | |