1
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Pawel Blokus
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <gtest/gtest.h>
24
#include <drizzled/temporal.h>
26
#include "generator.h"
28
using namespace drizzled;
30
class DateTimeTest: public ::testing::Test
38
Generator::DateTimeGen::make_valid_datetime(&datetime);
42
TEST_F(DateTimeTest, in_unix_epoch_onFirstDateTimeInUnixEpoch_shouldReturn_True)
44
Generator::DateTimeGen::make_datetime(&datetime, 1970, 1, 1, 0, 0, 0);
46
result= datetime.is_valid();
51
TEST_F(DateTimeTest, in_unix_epoch_onLastDateTimeInUnixEpoch_shouldReturn_True)
53
Generator::DateTimeGen::make_datetime(&datetime, 2038, 1, 19, 3, 14, 7);
55
result= datetime.is_valid();
60
TEST_F(DateTimeTest, in_unix_epoch_onLastDateTimeBeforeUnixEpoch_shouldReturn_False)
62
Generator::DateTimeGen::make_datetime(&datetime, 1969, 12, 31, 23, 59, 59);
64
result= datetime.is_valid();
69
TEST_F(DateTimeTest, in_unix_epoch_onFirstDateTimeAfterUnixEpoch_shouldReturn_False)
71
Generator::DateTimeGen::make_datetime(&datetime, 1969, 1, 19, 3, 14, 8);
73
result= datetime.is_valid();
78
TEST_F(DateTimeTest, is_valid_onValidDateTime_shouldReturn_True)
80
result= datetime.is_valid();
84
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithYearBelowMinimum_shouldReturn_False)
86
datetime.set_years(DRIZZLE_MIN_YEARS_SQL - 1);
88
result= datetime.is_valid();
93
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithYearAboveMaximum_shouldReturn_False)
95
datetime.set_years(DRIZZLE_MAX_YEARS_SQL + 1);
97
result= datetime.is_valid();
102
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMonthSetToZero_shouldReturn_False)
104
datetime.set_months(0);
106
result= datetime.is_valid();
108
ASSERT_FALSE(result);
112
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMonthAboveMaximum_shouldReturn_False)
114
datetime.set_months(13);
116
result= datetime.is_valid();
118
ASSERT_FALSE(result);
121
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithDaySetToZero_shouldReturn_False)
123
datetime.set_days(0);
125
result= datetime.is_valid();
127
ASSERT_FALSE(result);
130
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithDayAboveDaysInMonth_shouldReturn_False)
132
datetime.set_days(32);
134
result= datetime.is_valid();
136
ASSERT_FALSE(result);
139
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithLeapDayInNonLeapYear_shouldReturn_False)
141
Generator::TemporalGen::leap_day_in_non_leap_year(&datetime);
143
result= datetime.is_valid();
145
ASSERT_FALSE(result);
148
TEST_F(DateTimeTest, is_valid_onValidDateTimeWithLeapDayInLeapYear_shouldReturn_True)
150
Generator::TemporalGen::leap_day_in_leap_year(&datetime);
152
result= datetime.is_valid();
157
TEST_F(DateTimeTest, is_valid_onValidMinimalTime_shouldReturn_True)
159
Generator::TemporalGen::make_min_time(&datetime);
161
result= datetime.is_valid();
166
TEST_F(DateTimeTest, is_valid_onValidMaximalTime_shouldReturn_True)
168
Generator::TemporalGen::make_max_time(&datetime);
170
result= datetime.is_valid();
175
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithHourAboveMaximum23_shouldReturn_False)
177
datetime.set_hours(24);
179
result= datetime.is_valid();
181
ASSERT_FALSE(result);
184
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMinutesAboveMaximum59_shouldReturn_False)
186
datetime.set_minutes(60);
188
result= datetime.is_valid();
190
ASSERT_FALSE(result);
193
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithSecondsAboveMaximum59_shouldReturn_False)
195
datetime.set_seconds(60);
197
result= datetime.is_valid();
199
ASSERT_FALSE(result);
202
TEST_F(DateTimeTest, to_string_shouldProduce_hyphenSeperatedDateElements_and_colonSeperatedTimeElements)
204
char expected[DateTime::MAX_STRING_LENGTH]= "2010-05-01 08:07:06";
205
char returned[DateTime::MAX_STRING_LENGTH];
206
Generator::DateTimeGen::make_datetime(&datetime, 2010, 5, 1, 8, 7, 6);
208
datetime.to_string(returned, DateTime::MAX_STRING_LENGTH);
210
ASSERT_STREQ(expected, returned);
213
TEST_F(DateTimeTest, to_string_nullBuffer_shouldReturnProperLengthAnyway)
215
int length= datetime.to_string(NULL, 0);
217
ASSERT_EQ(DateTime::MAX_STRING_LENGTH - 1, length);
220
TEST_F(DateTimeTest, from_string_validString_shouldPopulateCorrectly)
222
char valid_string[DateTime::MAX_STRING_LENGTH]= "2010-05-01 08:07:06";
223
uint32_t years, months, days, hours, minutes, seconds;
225
result = datetime.from_string(valid_string, DateTime::MAX_STRING_LENGTH);
228
years = datetime.years();
229
months = datetime.months();
230
days = datetime.days();
231
hours = datetime.hours();
232
minutes = datetime.minutes();
233
seconds = datetime.seconds();
235
EXPECT_EQ(2010, years);
236
EXPECT_EQ(5, months);
239
EXPECT_EQ(7, minutes);
240
EXPECT_EQ(6, seconds);
243
TEST_F(DateTimeTest, from_int32_t_onValueCreatedBy_to_int32_t_shouldProduceOriginalDate)
245
uint32_t years = 2030, months = 8, days = 17, hours = 14, minutes = 45, seconds = 13;
246
Generator::DateTimeGen::make_datetime(&datetime, years, months, days, hours, minutes, seconds);
247
uint32_t decoded_years, decoded_months, decoded_days;
248
uint32_t decoded_hours, decoded_minutes, decoded_seconds;
249
int64_t representation;
250
DateTime decoded_datetime;
252
datetime.to_int64_t(&representation);
253
decoded_datetime.from_int64_t(representation);
255
decoded_years = decoded_datetime.years();
256
decoded_months = decoded_datetime.months();
257
decoded_days = decoded_datetime.days();
258
decoded_hours = decoded_datetime.hours();
259
decoded_minutes = decoded_datetime.minutes();
260
decoded_seconds = decoded_datetime.seconds();
262
EXPECT_EQ(years, decoded_years);
263
EXPECT_EQ(months, decoded_months);
264
EXPECT_EQ(days, decoded_days);
265
EXPECT_EQ(hours, decoded_hours);
266
EXPECT_EQ(minutes, decoded_minutes);
267
EXPECT_EQ(seconds, decoded_seconds);
270
TEST_F(DateTimeTest, to_tm)
272
uint32_t years = 2030, months = 8, days = 17, hours = 14, minutes = 45, seconds = 13;
273
Generator::DateTimeGen::make_datetime(&datetime, years, months, days, hours, minutes, seconds);
276
datetime.to_tm(&filled);
278
EXPECT_EQ(130, filled.tm_year);
279
EXPECT_EQ(7, filled.tm_mon);
280
EXPECT_EQ(17, filled.tm_mday);
281
EXPECT_EQ(14, filled.tm_hour);
282
EXPECT_EQ(45, filled.tm_min);
283
EXPECT_EQ(13, filled.tm_sec);
284
EXPECT_EQ(228, filled.tm_yday);
285
EXPECT_EQ(6, filled.tm_wday);
286
EXPECT_GT(0, filled.tm_isdst);