The Battle for Wesnoth  1.15.3+dev
TimeTest.cpp
Go to the documentation of this file.
1 //
2 // M A R I A D B + +
3 //
4 // Copyright The ViaDuck Project 2016 - 2020.
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 #include "TimeTest.h"
10 
11 #define EXPECT_INVALID(t, x...) do { \
12  t d(x); \
13  EXPECT_FALSE(d.is_valid()); \
14 } while(false)
15 
16 TEST_F(TimeTest, testConstructors) {
17  // valid times
19  mariadb::time b(13, 37, 42, 000);
20  mariadb::time c(b);
21 
22  // invalid times
23  EXPECT_INVALID(mariadb::time, static_cast<u8>(24));
24  EXPECT_INVALID(mariadb::time, static_cast<u8>(24));
25  EXPECT_INVALID(mariadb::time, 23u, 60u);
26  EXPECT_INVALID(mariadb::time, 23u, 59u, 62u);
27  EXPECT_INVALID(mariadb::time, 23u, 59u, 59u, 1000u);
28 
29  // test time.h constructors
30  tm sometime;
31  sometime.tm_sec = 42;
32  sometime.tm_min = 37;
33  sometime.tm_hour = 13;
34 
35  tm invtime;
36  invtime.tm_hour = 41;
37 
38  time_t someothertime = ::time(nullptr);
39 
40  mariadb::time e(sometime);
41  EXPECT_INVALID(mariadb::time, invtime);
42 
43  mariadb::time f(someothertime);
44 
45  // test string conversion
46  mariadb::time g("00:00:00.000");
47  mariadb::time h("23:59:59.999");
48  mariadb::time i("13:37:42.000");
49  mariadb::time j("18-59.59?0");
50  mariadb::time k("8:9:5-01");
51  mariadb::time l("8:59:59.0");
52  EXPECT_ANY_THROW(mariadb::time d("24"));
53  EXPECT_ANY_THROW(mariadb::time d("23:60"));
54  EXPECT_ANY_THROW(mariadb::time d("23:59:62"));
55  EXPECT_ANY_THROW(mariadb::time d("23:59:a59.1000"));
56  EXPECT_ANY_THROW(mariadb::time m("859"));
57  EXPECT_INVALID(mariadb::time, "23:59:59.1000");
58 
59  EXPECT_NE(a, b);
60  EXPECT_EQ(b, c);
61  EXPECT_EQ(c, e);
62  EXPECT_NE(e, f);
63  EXPECT_EQ(a, g);
64  EXPECT_NE(g, h);
65  EXPECT_EQ(e, i);
66 
67  EXPECT_EQ("00:00:00.000", g.str_time(true));
68  EXPECT_EQ("23:59:59.999", h.str_time(true));
69  EXPECT_EQ("13:37:42.000", i.str_time(true));
70  EXPECT_EQ("18:59:59.000", j.str_time(true));
71  EXPECT_EQ("08:09:05.001", k.str_time(true));
72  EXPECT_EQ("08:59:59", l.str_time());
73 }
74 
75 TEST_F(TimeTest, testArithm) {
76  // test ms
77  mariadb::time a(13, 37);
78  mariadb::time b(13, 36, 59, 999);
80 
81  ASSERT_EQ(a, c);
82  ASSERT_NE(a, b);
83 
84  // test day overflow with ms
85  mariadb::time d(23, 59, 59, 999);
87  ASSERT_EQ(mariadb::time(), e);
88 
89  // test add second
90  mariadb::time f(13, 36, 59);
92  ASSERT_EQ(a, g);
93 
94  // test day overflow with second
96  ASSERT_EQ(mariadb::time(0, 0, 0, 999), h);
97 
98  // test add minute
99  mariadb::time i(13, 36);
100  mariadb::time j = i.add_minutes(1);
101  ASSERT_EQ(a, j);
102 
103  // test day overflow with minute
104  mariadb::time k = d.add_minutes(1);
105  ASSERT_EQ(mariadb::time(0, 0, 59, 999), k);
106 
107  // test add hour
108  mariadb::time l(12, 37);
109  mariadb::time m = l.add_hours(1);
110  ASSERT_EQ(a, m);
111 
112  // test day overflow with hour
113  mariadb::time n = d.add_hours(1);
114  ASSERT_EQ(mariadb::time(0, 59, 59, 999), n);
115 
116  // test subtraction
117  ASSERT_EQ(d, n.add_hours(-1));
118  ASSERT_EQ(d, k.add_minutes(-1));
119  ASSERT_EQ(d, h.add_seconds(-1));
120  ASSERT_EQ(d, e.add_milliseconds(-1));
121 }
122 
123 TEST_F(TimeTest, testDiff) {
124  mariadb::time a(13, 37, 42, 007);
125  mariadb::time b(12, 37, 42, 007);
126  mariadb::time c(23, 37, 42, 007);
127 
128  mariadb::time_span s12 = a.time_between(b);
129  mariadb::time_span s21 = b.time_between(a);
130 
131  mariadb::time_span s23 = b.time_between(c);
133 
134  mariadb::time_span s31 = c.time_between(a);
135  mariadb::time_span s13 = a.time_between(c);
136 
137  ASSERT_EQ(1, s12.hours());
138  ASSERT_EQ(s12.hours(), s21.hours());
139  ASSERT_NE(s12.negative(), s21.negative());
140 
141  ASSERT_EQ(11, s23.hours());
142  ASSERT_EQ(s23.hours(), s32.hours());
143  ASSERT_NE(s23.negative(), s32.negative());
144 
145  ASSERT_EQ(10, s31.hours());
146  ASSERT_EQ(s31.hours(), s13.hours());
147  ASSERT_NE(s31.negative(), s13.negative());
148 }
149 
150 TEST_F(TimeTest, testNow) {
151  mariadb::time n1 = time::now();
152  mariadb::time n2 = time::now();
153  mariadb::time n3 = time::now();
154 
155  mariadb::time un1 = time::now_utc();
156  mariadb::time un2 = time::now_utc();
157  mariadb::time un3 = time::now_utc();
158 
159  // make sure n4 and un4 are actually different
160  sleep(1);
161  mariadb::time n4 = time::now();
162  mariadb::time un4 = time::now_utc();
163 
164  ASSERT_GE(n4, n3);
165  ASSERT_GE(n3, n2);
166  ASSERT_GE(n2, n1);
167 
168  ASSERT_GE(un4, un3);
169  ASSERT_GE(un3, un2);
170  ASSERT_GE(un2, un1);
171 
172  ASSERT_NE(n1, n4);
173  ASSERT_NE(un1, un4);
174 }
175 
176 TEST_F(TimeTest, testSpan) {
177  time_span a;
178  time_span b(1, 3, 37, 42, 007);
179  time_span c(0, 3, 37, 42, 007, true);
180  EXPECT_ANY_THROW(time_span d(0, 33, 37, 42, 007, true));
181  EXPECT_ANY_THROW(time_span d(0, 33, 37, 42, 007, true));
182  EXPECT_ANY_THROW(time_span d(0, 3, 66, 42, 007));
183  EXPECT_ANY_THROW(time_span d(0, 3, 37, 100, 007));
184  EXPECT_ANY_THROW(time_span d(0, 3, 37, 42, 1001));
185  time_span e(b);
186 
187  EXPECT_EQ(b, e);
188  EXPECT_TRUE(a.zero());
189  EXPECT_FALSE(a.negative());
190 
191  EXPECT_NE(a, b);
192  EXPECT_NE(b, c);
193 
194  // equal days
195  c.days(1);
196  EXPECT_NE(b, c);
197 
198  // all equal
199  c.negative(false);
200  EXPECT_EQ(b, c);
201 
202  // original das
203  c.days(0);
204  ASSERT_EQ(b.total_hours(), c.total_hours() + 24);
205  ASSERT_EQ(b.total_minutes(), c.total_minutes() + 24 * 60);
206  ASSERT_EQ(b.total_seconds(), c.total_seconds() + 24 * 60 * 60);
207  ASSERT_EQ(b.total_milliseconds(), c.total_milliseconds() + 24 * 60 * 60 * 1000);
208 }
209 
210 void readdTest(date_time smaller, date_time bigger) {
211  ASSERT_EQ(bigger, smaller.add(bigger.time_between(smaller)));
212 }
213 
214 TEST_F(TimeTest, testDateTime) {
215  date_time a;
216  date_time b(2012, 12, 21, 13, 37, 42, 007);
217  date_time before(2012, 12, 19, 13, 37, 42, 007);
218  date_time c(2008, 2, 29);
219  EXPECT_INVALID(date_time, 2009, 2, 29);
220  EXPECT_INVALID(date_time, 2009, 1, 0);
221  EXPECT_INVALID(date_time, 2009, 4, 31);
222  date_time e(c);
223 
224  date_time f(1900, 1, 1, 13, 37, 42);
225  mariadb::time ta(13, 37, 42);
226  date_time g(ta);
227 
228  EXPECT_EQ(b, before.add_days(2));
229  EXPECT_EQ(e, c);
230  EXPECT_NE(a, b);
231  EXPECT_EQ(f, g);
232 
233  // test time.h constructors
234  tm sometime;
235  sometime.tm_year = 112;
236  sometime.tm_mon = 11;
237  sometime.tm_mday = 21;
238  sometime.tm_hour = 13;
239  sometime.tm_min = 37;
240  sometime.tm_sec = 42;
241 
242  tm invtime;
243  invtime.tm_year = 110;
244  invtime.tm_mon = 12;
245  invtime.tm_mday = 1;
246 
247  time_t someothertime = ::time(nullptr);
248 
249  date_time h(sometime);
250  EXPECT_INVALID(date_time, invtime);
251 
252  date_time i(someothertime);
253 
254  EXPECT_EQ(h.add_milliseconds(007), b);
255  EXPECT_NE(a, i);
256 
257  date_time aa("2012-12-21 13:37:42.007");
258  date_time ba("2008-02-29 13:37:42.007");
259  EXPECT_ANY_THROW(date_time d("2007-02-29 a13:37:42.007"));
260  EXPECT_ANY_THROW(date_time d("2007-a02-29 13:37:42.007"));
261  EXPECT_ANY_THROW(date_time d("2007-02-29 13:37:a42.007"));
262  EXPECT_ANY_THROW(date_time d("2007-02-29 113:37:42.007"));
263  EXPECT_ANY_THROW(date_time d("2007-02-29 13:37:42.a007"));
264  EXPECT_INVALID(date_time, "2007-02-29 13:37:42.007");
265  EXPECT_INVALID(date_time, "2007-02-29 13:37:42.007");
266 
267  EXPECT_EQ(b, aa);
268 
269  u16 doy = aa.day_of_year();
270  date_time da = date_time::reverse_day_of_year(aa.year(), doy);
271  EXPECT_EQ(da, aa.date());
272 
273  readdTest(before, b);
274 
275  // leap year
276  readdTest(date_time(2000, 1, 1), date_time(2000, 2, 1));
277 
278  readdTest(date_time(2000, 9, 1), date_time(2010, 2, 1));
279  readdTest(date_time(2000, 2, 1), date_time(2010, 9, 1));
280 
281  // non-leap
282  readdTest(date_time(2001, 1, 1), date_time(2001, 2, 1));
283 
284  readdTest(date_time(2001, 9, 1), date_time(2010, 2, 1));
285  readdTest(date_time(2001, 2, 1), date_time(2010, 9, 1));
286 
287  EXPECT_EQ("2012-12-21 13:37:42.007", aa.str(true));
288  EXPECT_EQ("2012-12-21 13:37:42", aa.str(false));
289  EXPECT_EQ("2012-12-21", aa.str_date());
290 
291  EXPECT_EQ("2008-02-29 13:37:42.007", ba.str(true));
292  EXPECT_EQ("2008-02-29 13:37:42", ba.str(false));
293  EXPECT_EQ("2008-02-29", ba.str_date());
294 }
signed int s32
Definition: types.hpp:25
void readdTest(date_time smaller, date_time bigger)
Definition: TimeTest.cpp:210
u16 day_of_year() const
Calculates the day of year from current date.
Definition: date_time.cpp:152
Class representing SQL time.
Definition: time.hpp:23
#define a
#define h
#define EXPECT_INVALID(t, x...)
Definition: TimeTest.cpp:11
#define d
u64 total_hours() const
Converts the time_span to minutes.
Definition: time_span.cpp:122
time add_milliseconds(s32 milliseconds) const
Adds a certain amount of milliseconds to the current time.
Definition: time.cpp:181
#define b
u32 days() const
Get number of days.
Definition: time_span.cpp:82
const std::string str_time(bool with_millisecond=false) const
Converts the time to a string with the format hh:mm:ss[.nnn].
Definition: time.cpp:357
u16 year() const
Get currently set year.
Definition: date_time.cpp:108
time add_minutes(s32 minutes) const
Adds a certain amount of minutes to the current time.
Definition: time.cpp:143
TEST_F(TimeTest, testConstructors)
Definition: TimeTest.cpp:16
u64 total_milliseconds() const
Converts the time_span to milliseconds.
Definition: time_span.cpp:128
date_time add(const time_span &dur) const
Adds a timespan to the date_time.
Definition: date_time.cpp:316
const std::string str_date() const
Converts only the date part of this date_time to ISO 8601 date string yyyy-mm-dd. ...
Definition: date_time.cpp:573
if(m_stmt_data) return mariadb return date_time(std::string(m_row[index], column_size(index))).date()
unsigned short u16
Definition: types.hpp:21
date_time add_milliseconds(s32 milliseconds) const
Add milliseconds to current date with second wrapping.
Definition: date_time.cpp:285
date_time add_days(s32 days) const
Add days to current date with month wrapping.
Definition: date_time.cpp:195
u64 total_minutes() const
Converts the time_span to minutes.
Definition: time_span.cpp:124
std::size_t i
Definition: function.cpp:933
time add_seconds(s32 seconds) const
Adds a certain amount of seconds to the current time.
Definition: time.cpp:162
Class used to represent SQL date_time.
Definition: date_time.hpp:20
time_span time_between(const date_time &dt) const
Calculates the time_span between this date_time and dt.
Definition: date_time.cpp:348
double g
Definition: astarsearch.cpp:64
time add_hours(s32 hours) const
Adds a certain amount of hours to the current time.
Definition: time.cpp:132
date_time date() const
Gets only the date part of this date_time.
Definition: date_time.cpp:505
#define f
bool negative() const
Indicates whether this time_span is negative.
Definition: time_span.cpp:118
time_span time_between(const time &t) const
Calculates the timespan between the current time instance and given instance t.
Definition: time.cpp:221
#define e
u64 total_seconds() const
Converts the time_span to seconds.
Definition: time_span.cpp:126
mock_char c
static map_location::DIRECTION n
u8 hours() const
Get number of hours.
Definition: time_span.cpp:86
const std::string str(bool with_millisecond=false) const
Converts the date and time to ISO 8601 string yyyy-mm-dd hh:mm:ss[.nnn].
Definition: date_time.cpp:565
bool zero() const
Indicates whether this time_span is zero.
Definition: time_span.cpp:78