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 "temporal_generator.h"
28
using namespace drizzled;
30
template <typename TemporalType>
31
class TimestampTestCompareOperators : public ::testing::Test
34
Timestamp sample_timestamp;
37
TemporalType identical_with_sample_timestamp, before_sample_timestamp, after_sample_timestamp;
39
void initBeforeIdenticalAfter();
43
TemporalGenerator::TimestampGen::make_timestamp(&sample_timestamp, 2010, 9, 8, 0, 0, 0);
44
initBeforeIdenticalAfter();
48
template<> void TimestampTestCompareOperators<Date>::initBeforeIdenticalAfter()
50
TemporalGenerator::DateGen::make_date(&before_sample_timestamp, 1980, 1, 1);
51
TemporalGenerator::DateGen::make_date(&identical_with_sample_timestamp, 2010, 9, 8);
52
TemporalGenerator::DateGen::make_date(&after_sample_timestamp, 2019, 5, 30);
55
template<> void TimestampTestCompareOperators<DateTime>::initBeforeIdenticalAfter()
57
TemporalGenerator::DateTimeGen::make_datetime(&before_sample_timestamp, 1990, 12, 31, 12, 12, 30);
58
TemporalGenerator::DateTimeGen::make_datetime(&identical_with_sample_timestamp, 2010, 9, 8, 0, 0, 0);
59
TemporalGenerator::DateTimeGen::make_datetime(&after_sample_timestamp, 2020, 4, 4, 4, 4, 4);
62
template<> void TimestampTestCompareOperators<Timestamp>::initBeforeIdenticalAfter()
64
TemporalGenerator::TimestampGen::make_timestamp(&before_sample_timestamp, 2010, 9, 7, 23, 59, 59);
65
TemporalGenerator::TimestampGen::make_timestamp(&identical_with_sample_timestamp, 2010, 9, 8, 0, 0, 0);
66
TemporalGenerator::TimestampGen::make_timestamp(&after_sample_timestamp, 2010, 9, 8, 0, 0, 1);
69
typedef ::testing::Types<Date, DateTime, Timestamp> typesForTimestampTestCompareOperators;
70
TYPED_TEST_CASE(TimestampTestCompareOperators, typesForTimestampTestCompareOperators);
72
TYPED_TEST(TimestampTestCompareOperators, operatorEqual_ComparingWithIdencticalTemporal_ShouldReturn_True)
74
this->result= (this->sample_timestamp == this->identical_with_sample_timestamp);
76
ASSERT_TRUE(this->result);
79
TYPED_TEST(TimestampTestCompareOperators, operatorEqual_ComparingWithDifferentTemporal_ShouldReturn_False)
81
this->result= (this->sample_timestamp == this->before_sample_timestamp);
83
ASSERT_FALSE(this->result);
86
TYPED_TEST(TimestampTestCompareOperators, operatorNotEqual_ComparingWithIdencticalTemporal_ShouldReturn_False)
88
this->result= (this->sample_timestamp != this->identical_with_sample_timestamp);
90
ASSERT_FALSE(this->result);
93
TYPED_TEST(TimestampTestCompareOperators, operatorNotEqual_ComparingWithDifferentTemporal_ShouldReturn_True)
95
this->result= (this->sample_timestamp != this->before_sample_timestamp);
97
ASSERT_TRUE(this->result);
100
TYPED_TEST(TimestampTestCompareOperators, operatorGreaterThan_ComparingWithIdenticalTemporal_ShouldReturn_False)
102
this->result= (this->sample_timestamp > this->identical_with_sample_timestamp);
104
ASSERT_FALSE(this->result);
107
TYPED_TEST(TimestampTestCompareOperators, operatorGreaterThan_ComparingWithLaterTemporal_ShouldReturn_False)
109
this->result= (this->sample_timestamp > this->after_sample_timestamp);
111
ASSERT_FALSE(this->result);
114
TYPED_TEST(TimestampTestCompareOperators, operatorGreaterThan_ComparingWithEarlierTemporal_ShouldReturn_True)
116
this->result= (this->sample_timestamp > this->before_sample_timestamp);
118
ASSERT_TRUE(this->result);
121
TYPED_TEST(TimestampTestCompareOperators, operatorGreaterThanOrEqual_ComparingWithIdenticalTemporal_ShouldReturn_True)
123
this->result= (this->sample_timestamp >= this->identical_with_sample_timestamp);
125
ASSERT_TRUE(this->result);
128
TYPED_TEST(TimestampTestCompareOperators, operatorGreaterThanOrEqual_ComparingWithLaterTemporal_ShouldReturn_False)
130
this->result= (this->sample_timestamp >= this->after_sample_timestamp);
132
ASSERT_FALSE(this->result);
135
TYPED_TEST(TimestampTestCompareOperators, operatorGreaterThanOrEqual_ComparingWithEarlierTemporal_ShouldReturn_True)
137
this->result= (this->sample_timestamp >= this->before_sample_timestamp);
139
ASSERT_TRUE(this->result);
142
TYPED_TEST(TimestampTestCompareOperators, operatorLessThan_ComparingWithIdenticalTemporal_ShouldReturn_False)
144
this->result= (this->sample_timestamp < this->identical_with_sample_timestamp);
146
ASSERT_FALSE(this->result);
149
TYPED_TEST(TimestampTestCompareOperators, operatorLessThan_ComparingWithLaterTemporal_ShouldReturn_True)
151
this->result= (this->sample_timestamp < this->after_sample_timestamp);
153
ASSERT_TRUE(this->result);
156
TYPED_TEST(TimestampTestCompareOperators, operatorLessThan_ComparingWithEarlierTemporal_ShouldReturn_False)
158
this->result= (this->sample_timestamp < this->before_sample_timestamp);
160
ASSERT_FALSE(this->result);
163
TYPED_TEST(TimestampTestCompareOperators, operatorLessThanOrEqual_ComparingWithIdenticalTemporal_ShouldReturn_True)
165
this->result= (this->sample_timestamp <= this->identical_with_sample_timestamp);
167
ASSERT_TRUE(this->result);
170
TYPED_TEST(TimestampTestCompareOperators, operatorLessThanOrEqual_ComparingWithLaterTemporal_ShouldReturn_True)
172
this->result= (this->sample_timestamp <= this->after_sample_timestamp);
174
ASSERT_TRUE(this->result);
177
TYPED_TEST(TimestampTestCompareOperators, operatorLessThanOrEqual_ComparingWithEarlierTemporal_ShouldReturn_False)
179
this->result= (this->sample_timestamp <= this->before_sample_timestamp);
181
ASSERT_FALSE(this->result);
184
class TimestampTest : public ::testing::Test
191
TEST_F(TimestampTest, is_valid_minOfTimestampRange_shouldReturn_True)
193
uint32_t year= 1970, month= 1, day= 1, hour= 0, minute= 0, second= 0;
194
TemporalGenerator::TimestampGen::make_timestamp(×tamp, year, month, day, hour, minute, second);
196
result= timestamp.is_valid();
201
TEST_F(TimestampTest, is_valid_maxOfTimestampRange_shouldReturn_True)
203
uint32_t year= 2038, month= 1, day= 19, hour= 3, minute= 14, second= 7;
204
TemporalGenerator::TimestampGen::make_timestamp(×tamp, year, month, day, hour, minute, second);
206
result= timestamp.is_valid();
211
TEST_F(TimestampTest, is_valid_oneSecondBeforeTimestampMinOfRange_shouldReturn_False)
213
uint32_t year= 1969, month= 12, day= 31, hour= 23, minute= 59, second= 59;
214
TemporalGenerator::TimestampGen::make_timestamp(×tamp, year, month, day, hour, minute, second);
216
result= timestamp.is_valid();
218
ASSERT_FALSE(result);
221
TEST_F(TimestampTest, is_valid_oneSecondAfterTimestampMaxOfRange_shouldReturn_False)
223
uint32_t year= 2038, month= 1, day= 19, hour= 3, minute= 14, second= 8;
224
TemporalGenerator::TimestampGen::make_timestamp(×tamp, year, month, day, hour, minute, second);
226
result= timestamp.is_valid();
228
ASSERT_FALSE(result);
231
TEST_F(TimestampTest, is_valid_InsideOfTimestampRange_shouldReturn_True)
233
uint32_t year= 1980, month= 11, day= 1, hour= 5, minute= 8, second= 5;
234
TemporalGenerator::TimestampGen::make_timestamp(×tamp, year, month, day, hour, minute, second);
236
result= timestamp.is_valid();
241
TEST_F(TimestampTest, to_time_t)
244
TemporalGenerator::TimestampGen::make_timestamp(×tamp, 2009, 6, 3, 4, 59, 1);
246
timestamp.to_time_t(time);
248
ASSERT_EQ(1244005141, time);
251
TEST_F(TimestampTest, outputStreamOperator_shouldWrite_hyphenSeperatedDateElements_and_colonSeperatedTimeElements)
253
std::ostringstream output;
254
std::string expected= "2010-05-01 08:07:06";
255
std::string returned;
256
TemporalGenerator::TimestampGen::make_timestamp(×tamp, 2010, 5, 1, 8, 7, 6);
259
returned= output.str();
261
ASSERT_EQ(expected, returned);