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/type/decimal.h>
25
#include <drizzled/temporal.h>
26
#include <drizzled/temporal_format.h>
28
#include "temporal_generator.h"
30
using namespace drizzled;
32
template <typename TemporalType>
33
class DateTestCompareOperators : public ::testing::Test
39
TemporalType identical_with_sample_date, before_sample_date, after_sample_date;
41
void initBeforeIdenticalAfter();
45
TemporalGenerator::DateGen::make_date(&sample_date, 2010, 9, 8);
46
initBeforeIdenticalAfter();
50
template<> void DateTestCompareOperators<Date>::initBeforeIdenticalAfter()
52
TemporalGenerator::DateGen::make_date(&before_sample_date, 1980, 1, 1);
53
TemporalGenerator::DateGen::make_date(&identical_with_sample_date, 2010, 9, 8);
54
TemporalGenerator::DateGen::make_date(&after_sample_date, 2019, 5, 30);
57
template<> void DateTestCompareOperators<DateTime>::initBeforeIdenticalAfter()
59
TemporalGenerator::DateTimeGen::make_datetime(&before_sample_date, 1990, 12, 31, 12, 12, 30);
60
TemporalGenerator::DateTimeGen::make_datetime(&identical_with_sample_date, 2010, 9, 8, 0, 0, 0);
61
TemporalGenerator::DateTimeGen::make_datetime(&after_sample_date, 2020, 4, 4, 4, 4, 4);
64
template<> void DateTestCompareOperators<Timestamp>::initBeforeIdenticalAfter()
66
TemporalGenerator::TimestampGen::make_timestamp(&before_sample_date, 1980, 1, 1, 13, 56, 41);
67
TemporalGenerator::TimestampGen::make_timestamp(&identical_with_sample_date, 2010, 9, 8, 0, 0, 0);
68
TemporalGenerator::TimestampGen::make_timestamp(&after_sample_date, 2019, 5, 30, 9, 10, 13);
71
typedef ::testing::Types<Date, DateTime, Timestamp> typesForDateTestCompareOperators;
72
TYPED_TEST_CASE(DateTestCompareOperators, typesForDateTestCompareOperators);
74
TYPED_TEST(DateTestCompareOperators, operatorEqual_ComparingWithIdencticalTemporal_ShouldReturn_True)
76
this->result= (this->sample_date == this->identical_with_sample_date);
78
ASSERT_TRUE(this->result);
81
TYPED_TEST(DateTestCompareOperators, operatorEqual_ComparingWithDifferentTemporal_ShouldReturn_False)
83
this->result= (this->sample_date == this->before_sample_date);
85
ASSERT_FALSE(this->result);
88
TYPED_TEST(DateTestCompareOperators, operatorNotEqual_ComparingWithIdencticalTemporal_ShouldReturn_False)
90
this->result= (this->sample_date != this->identical_with_sample_date);
92
ASSERT_FALSE(this->result);
95
TYPED_TEST(DateTestCompareOperators, operatorNotEqual_ComparingWithDifferentTemporal_ShouldReturn_True)
97
this->result= (this->sample_date != this->before_sample_date);
99
ASSERT_TRUE(this->result);
102
TYPED_TEST(DateTestCompareOperators, operatorGreaterThan_ComparingWithIdenticalTemporal_ShouldReturn_False)
104
this->result= (this->sample_date > this->identical_with_sample_date);
106
ASSERT_FALSE(this->result);
109
TYPED_TEST(DateTestCompareOperators, operatorGreaterThan_ComparingWithLaterTemporal_ShouldReturn_False)
111
this->result= (this->sample_date > this->after_sample_date);
113
ASSERT_FALSE(this->result);
116
TYPED_TEST(DateTestCompareOperators, operatorGreaterThan_ComparingWithEarlierTemporal_ShouldReturn_True)
118
this->result= (this->sample_date > this->before_sample_date);
120
ASSERT_TRUE(this->result);
123
TYPED_TEST(DateTestCompareOperators, operatorGreaterThanOrEqual_ComparingWithIdenticalTemporal_ShouldReturn_True)
125
this->result= (this->sample_date >= this->identical_with_sample_date);
127
ASSERT_TRUE(this->result);
130
TYPED_TEST(DateTestCompareOperators, operatorGreaterThanOrEqual_ComparingWithLaterTemporal_ShouldReturn_False)
132
this->result= (this->sample_date >= this->after_sample_date);
134
ASSERT_FALSE(this->result);
137
TYPED_TEST(DateTestCompareOperators, operatorGreaterThanOrEqual_ComparingWithEarlierTemporal_ShouldReturn_True)
139
this->result= (this->sample_date >= this->before_sample_date);
141
ASSERT_TRUE(this->result);
144
TYPED_TEST(DateTestCompareOperators, operatorLessThan_ComparingWithIdenticalTemporal_ShouldReturn_False)
146
this->result= (this->sample_date < this->identical_with_sample_date);
148
ASSERT_FALSE(this->result);
151
TYPED_TEST(DateTestCompareOperators, operatorLessThan_ComparingWithLaterTemporal_ShouldReturn_True)
153
this->result= (this->sample_date < this->after_sample_date);
155
ASSERT_TRUE(this->result);
158
TYPED_TEST(DateTestCompareOperators, operatorLessThan_ComparingWithEarlierTemporal_ShouldReturn_False)
160
this->result= (this->sample_date < this->before_sample_date);
162
ASSERT_FALSE(this->result);
165
TYPED_TEST(DateTestCompareOperators, operatorLessThanOrEqual_ComparingWithIdenticalTemporal_ShouldReturn_True)
167
this->result= (this->sample_date <= this->identical_with_sample_date);
169
ASSERT_TRUE(this->result);
172
TYPED_TEST(DateTestCompareOperators, operatorLessThanOrEqual_ComparingWithLaterTemporal_ShouldReturn_True)
174
this->result= (this->sample_date <= this->after_sample_date);
176
ASSERT_TRUE(this->result);
179
TYPED_TEST(DateTestCompareOperators, operatorLessThanOrEqual_ComparingWithEarlierTemporal_ShouldReturn_False)
181
this->result= (this->sample_date <= this->before_sample_date);
183
ASSERT_FALSE(this->result);
186
class DateTest : public ::testing::Test
194
TemporalGenerator::DateGen::make_valid_date(&date);
198
TEST_F(DateTest, operatorAssign_shouldCopyDateRelatadComponents)
202
EXPECT_EQ(date.years(), copy.years());
203
EXPECT_EQ(date.months(), copy.months());
204
EXPECT_EQ(date.days(), copy.days());
207
TEST_F(DateTest, is_valid_onValidDate_shouldReturn_True)
209
result= date.is_valid();
213
TEST_F(DateTest, is_valid_onInvalidDateWithYearBelowMinimum_shouldReturn_False)
215
date.set_years(DRIZZLE_MIN_YEARS_SQL - 1);
217
result= date.is_valid();
219
ASSERT_FALSE(result);
222
TEST_F(DateTest, is_valid_onInvalidDateWithYearAboveMaximum_shouldReturn_False)
224
date.set_years(DRIZZLE_MAX_YEARS_SQL + 1);
226
result= date.is_valid();
228
ASSERT_FALSE(result);
231
TEST_F(DateTest, is_valid_onInvalidDateWithMonthSetToZero_shouldReturn_False)
235
result= date.is_valid();
237
ASSERT_FALSE(result);
241
TEST_F(DateTest, is_valid_onInvalidDateWithMonthAboveMaximum_shouldReturn_False)
245
result= date.is_valid();
247
ASSERT_FALSE(result);
250
TEST_F(DateTest, is_valid_onInvalidDateWithDaySetToZero_shouldReturn_False)
254
result= date.is_valid();
256
ASSERT_FALSE(result);
259
TEST_F(DateTest, is_valid_onInvalidDateWithDayAboveDaysInMonth_shouldReturn_False)
263
result= date.is_valid();
265
ASSERT_FALSE(result);
268
TEST_F(DateTest, is_valid_onInvalidDateWithLeapDayInNonLeapYear_shouldReturn_False)
270
TemporalGenerator::TemporalGen::leap_day_in_non_leap_year(&date);
272
result= date.is_valid();
274
ASSERT_FALSE(result);
277
TEST_F(DateTest, is_valid_onValidDateWithLeapDayInLeapYear_shouldReturn_True)
279
TemporalGenerator::TemporalGen::leap_day_in_leap_year(&date);
281
result= date.is_valid();
286
TEST_F(DateTest, to_string_shouldProduce_hyphenSeperatedDateElements)
288
char expected[Date::MAX_STRING_LENGTH]= "2010-05-01";
289
char returned[Date::MAX_STRING_LENGTH];
290
TemporalGenerator::DateGen::make_date(&date, 2010, 5, 1);
292
date.to_string(returned, Date::MAX_STRING_LENGTH);
294
ASSERT_STREQ(expected, returned);
297
TEST_F(DateTest, to_string_nullBuffer_shouldReturnProperLengthAnyway)
299
int length= date.to_string(NULL, 0);
301
ASSERT_EQ(Date::MAX_STRING_LENGTH - 1, length);
304
TEST_F(DateTest, from_string_validString_shouldPopulateCorrectly)
306
char valid_string[Date::MAX_STRING_LENGTH]= "2010-05-01";
307
uint32_t years, months, days;
309
init_temporal_formats();
311
result= date.from_string(valid_string, Date::MAX_STRING_LENGTH - 1);
315
months= date.months();
318
deinit_temporal_formats();
320
EXPECT_EQ(2010, years);
321
EXPECT_EQ(5, months);
325
TEST_F(DateTest, from_string_invalidString_shouldReturn_False)
327
char valid_string[Date::MAX_STRING_LENGTH]= "2x10-05-01";
329
init_temporal_formats();
330
result= date.from_string(valid_string, Date::MAX_STRING_LENGTH - 1);
331
deinit_temporal_formats();
333
ASSERT_FALSE(result);
336
TEST_F(DateTest, to_int64_t)
338
TemporalGenerator::DateGen::make_date(&date, 2030, 8, 17);
339
int64_t representation;
341
date.to_int64_t(&representation);
343
ASSERT_EQ(20300817, representation);
346
TEST_F(DateTest, to_int32_t)
348
TemporalGenerator::DateGen::make_date(&date, 2030, 8, 17);
349
int32_t representation;
351
date.to_int32_t(&representation);
353
ASSERT_EQ(20300817, representation);
356
TEST_F(DateTest, from_int32_t_shouldPopulateDateCorrectly)
358
uint32_t decoded_years, decoded_months, decoded_days;
360
date.from_int32_t(20300817);
362
decoded_years= date.years();
363
decoded_months= date.months();
364
decoded_days= date.days();
366
EXPECT_EQ(2030, decoded_years);
367
EXPECT_EQ(8, decoded_months);
368
EXPECT_EQ(17, decoded_days);
371
TEST_F(DateTest, to_julian_day_number)
374
TemporalGenerator::DateGen::make_date(&date, 1999, 12, 31);
376
date.to_julian_day_number(&julian_day);
378
ASSERT_EQ(2451544, julian_day);
381
TEST_F(DateTest, from_julian_day_number)
383
int64_t julian_day= 2451544;
384
uint32_t years, months, days;
386
date.from_julian_day_number(julian_day);
389
months= date.months();
392
EXPECT_EQ(1999, years);
393
EXPECT_EQ(12, months);
397
TEST_F(DateTest, DISABLED_to_tm)
399
uint32_t years= 2030, months= 8, days= 17;
400
TemporalGenerator::DateGen::make_date(&date, years, months, days);
405
EXPECT_EQ(130, filled.tm_year);
406
EXPECT_EQ(7, filled.tm_mon);
407
EXPECT_EQ(17, filled.tm_mday);
408
EXPECT_EQ(0, filled.tm_hour);
409
EXPECT_EQ(0, filled.tm_min);
410
EXPECT_EQ(0, filled.tm_sec);
412
/* TODO:these fail, shouldn't they also be set properly? */
413
EXPECT_EQ(228, filled.tm_yday);
414
EXPECT_EQ(6, filled.tm_wday);
415
EXPECT_EQ(-1, filled.tm_isdst);
418
TEST_F(DateTest, from_tm)
420
uint32_t years, months, days;
422
from.tm_year= 1956 - 1900;
429
months= date.months();
432
EXPECT_EQ(1956, years);
433
EXPECT_EQ(3, months);
437
TEST_F(DateTest, to_time_t)
440
TemporalGenerator::DateGen::make_date(&date, 1990, 9, 9);
442
date.to_time_t(time);
444
ASSERT_EQ(652838400, time);
447
TEST_F(DateTest, from_time_t)
449
uint32_t years, months, days;
451
date.from_time_t(652838400);
454
months= date.months();
457
EXPECT_EQ(1990, years);
458
EXPECT_EQ(9, months);
462
TEST_F(DateTest, to_decimal)
464
drizzled::type::Decimal to;
465
TemporalGenerator::DateGen::make_date(&date, 1987, 5, 6);
467
date.to_decimal(&to);
469
ASSERT_EQ(19870506, to.buf[0]);
472
class DateFromStringTest: public ::testing::TestWithParam<const char*>
477
uint32_t years, months, days;
481
init_temporal_formats();
484
virtual void TearDown()
486
deinit_temporal_formats();
489
void assign_date_values()
492
months= date.months();
497
TEST_P(DateFromStringTest, from_string)
499
const char *valid_string= GetParam();
501
result= date.from_string(valid_string, strlen(valid_string));
504
assign_date_values();
506
EXPECT_EQ(2010, years);
507
EXPECT_EQ(6, months);
511
/* TODO:for some reason this was not declared by the macro, needs clarification*/
512
testing::internal::ParamGenerator<const char*> gtest_ValidStringDateFromStringTest_EvalGenerator_();
514
INSTANTIATE_TEST_CASE_P(ValidString, DateFromStringTest,
515
::testing::Values("20100607", /* YYYYMMDD */
516
"06/07/2010",/* MM[-/.]DD[-/.]YYYY (US common format)*/
517
"10.06.07",/* YY[-/.]MM[-/.]DD */
518
"10/6/7",/* YY[-/.][M]M[-/.][D]D */
519
"2010-6-7"/* YYYY[-/.][M]M[-/.][D]D */));