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
#define BOOST_TEST_DYN_LINK
24
#include <boost/test/unit_test.hpp>
26
#include <drizzled/temporal.h>
28
#include "temporal_generator.h"
30
using namespace drizzled;
32
class DateTimestampTestCompareOperators
35
Timestamp sample_timestamp;
38
Date identical_with_sample_timestamp, before_sample_timestamp, after_sample_timestamp;
40
DateTimestampTestCompareOperators()
42
TemporalGenerator::TimestampGen::make_timestamp(&sample_timestamp, 2010, 9, 8, 0, 0, 0);
43
TemporalGenerator::DateGen::make_date(&before_sample_timestamp, 1980, 1, 1);
44
TemporalGenerator::DateGen::make_date(&identical_with_sample_timestamp, 2010, 9, 8);
45
TemporalGenerator::DateGen::make_date(&after_sample_timestamp, 2019, 5, 30);
49
class DateTimeTimestampTestCompareOperators
52
Timestamp sample_timestamp;
55
DateTime identical_with_sample_timestamp, before_sample_timestamp, after_sample_timestamp;
57
DateTimeTimestampTestCompareOperators()
59
TemporalGenerator::TimestampGen::make_timestamp(&sample_timestamp, 2010, 9, 8, 0, 0, 0);
60
TemporalGenerator::DateTimeGen::make_datetime(&before_sample_timestamp, 1990, 12, 31, 12, 12, 30);
61
TemporalGenerator::DateTimeGen::make_datetime(&identical_with_sample_timestamp, 2010, 9, 8, 0, 0, 0);
62
TemporalGenerator::DateTimeGen::make_datetime(&after_sample_timestamp, 2020, 4, 4, 4, 4, 4);
66
class TimestampTimestampTestCompareOperators
69
Timestamp sample_timestamp;
72
Timestamp identical_with_sample_timestamp, before_sample_timestamp, after_sample_timestamp;
74
TimestampTimestampTestCompareOperators()
76
TemporalGenerator::TimestampGen::make_timestamp(&sample_timestamp, 2010, 9, 8, 0, 0, 0);
77
TemporalGenerator::TimestampGen::make_timestamp(&before_sample_timestamp, 2010, 9, 7, 23, 59, 59);
78
TemporalGenerator::TimestampGen::make_timestamp(&identical_with_sample_timestamp, 2010, 9, 8, 0, 0, 0);
79
TemporalGenerator::TimestampGen::make_timestamp(&after_sample_timestamp, 2010, 9, 8, 0, 0, 1);
84
BOOST_AUTO_TEST_SUITE(TimestampCompareTestSuite)
85
BOOST_FIXTURE_TEST_CASE(DateTimestampTestCompareTest, DateTimestampTestCompareOperators)
87
this->result= (this->sample_timestamp == this->identical_with_sample_timestamp);
88
BOOST_REQUIRE(this->result);
90
this->result= (this->sample_timestamp == this->before_sample_timestamp);
91
BOOST_REQUIRE(not this->result);
93
this->result= (this->sample_timestamp != this->identical_with_sample_timestamp);
94
BOOST_REQUIRE(not this->result);
96
this->result= (this->sample_timestamp != this->before_sample_timestamp);
97
BOOST_REQUIRE(this->result);
99
this->result= (this->sample_timestamp > this->identical_with_sample_timestamp);
100
BOOST_REQUIRE(not this->result);
102
this->result= (this->sample_timestamp > this->after_sample_timestamp);
103
BOOST_REQUIRE(not this->result);
105
this->result= (this->sample_timestamp > this->before_sample_timestamp);
106
BOOST_REQUIRE(this->result);
108
this->result= (this->sample_timestamp >= this->identical_with_sample_timestamp);
109
BOOST_REQUIRE(this->result);
111
this->result= (this->sample_timestamp >= this->after_sample_timestamp);
112
BOOST_REQUIRE(not this->result);
114
this->result= (this->sample_timestamp >= this->before_sample_timestamp);
115
BOOST_REQUIRE(this->result);
117
this->result= (this->sample_timestamp < this->identical_with_sample_timestamp);
118
BOOST_REQUIRE(not this->result);
120
this->result= (this->sample_timestamp < this->after_sample_timestamp);
121
BOOST_REQUIRE(this->result);
123
this->result= (this->sample_timestamp < this->before_sample_timestamp);
124
BOOST_REQUIRE(not this->result);
126
this->result= (this->sample_timestamp <= this->identical_with_sample_timestamp);
127
BOOST_REQUIRE(this->result);
129
this->result= (this->sample_timestamp <= this->after_sample_timestamp);
130
BOOST_REQUIRE(this->result);
132
this->result= (this->sample_timestamp <= this->before_sample_timestamp);
133
BOOST_REQUIRE(not this->result);
136
BOOST_FIXTURE_TEST_CASE(DateTimeTimestampTestCompareTest, DateTimeTimestampTestCompareOperators)
138
this->result= (this->sample_timestamp == this->identical_with_sample_timestamp);
139
BOOST_REQUIRE(this->result);
141
this->result= (this->sample_timestamp == this->before_sample_timestamp);
142
BOOST_REQUIRE(not this->result);
144
this->result= (this->sample_timestamp != this->identical_with_sample_timestamp);
145
BOOST_REQUIRE(not this->result);
147
this->result= (this->sample_timestamp != this->before_sample_timestamp);
148
BOOST_REQUIRE(this->result);
150
this->result= (this->sample_timestamp > this->identical_with_sample_timestamp);
151
BOOST_REQUIRE(not this->result);
153
this->result= (this->sample_timestamp > this->after_sample_timestamp);
154
BOOST_REQUIRE(not this->result);
156
this->result= (this->sample_timestamp > this->before_sample_timestamp);
157
BOOST_REQUIRE(this->result);
159
this->result= (this->sample_timestamp >= this->identical_with_sample_timestamp);
160
BOOST_REQUIRE(this->result);
162
this->result= (this->sample_timestamp >= this->after_sample_timestamp);
163
BOOST_REQUIRE(not this->result);
165
this->result= (this->sample_timestamp >= this->before_sample_timestamp);
166
BOOST_REQUIRE(this->result);
168
this->result= (this->sample_timestamp < this->identical_with_sample_timestamp);
169
BOOST_REQUIRE(not this->result);
171
this->result= (this->sample_timestamp < this->after_sample_timestamp);
172
BOOST_REQUIRE(this->result);
174
this->result= (this->sample_timestamp < this->before_sample_timestamp);
175
BOOST_REQUIRE(not this->result);
177
this->result= (this->sample_timestamp <= this->identical_with_sample_timestamp);
178
BOOST_REQUIRE(this->result);
180
this->result= (this->sample_timestamp <= this->after_sample_timestamp);
181
BOOST_REQUIRE(this->result);
183
this->result= (this->sample_timestamp <= this->before_sample_timestamp);
184
BOOST_REQUIRE(not this->result);
187
BOOST_FIXTURE_TEST_CASE(TimestampTimestampTestCompareTest, TimestampTimestampTestCompareOperators)
189
this->result= (this->sample_timestamp == this->identical_with_sample_timestamp);
190
BOOST_REQUIRE(this->result);
192
this->result= (this->sample_timestamp == this->before_sample_timestamp);
193
BOOST_REQUIRE(not this->result);
195
this->result= (this->sample_timestamp != this->identical_with_sample_timestamp);
196
BOOST_REQUIRE(not this->result);
198
this->result= (this->sample_timestamp != this->before_sample_timestamp);
199
BOOST_REQUIRE(this->result);
201
this->result= (this->sample_timestamp > this->identical_with_sample_timestamp);
202
BOOST_REQUIRE(not this->result);
204
this->result= (this->sample_timestamp > this->after_sample_timestamp);
205
BOOST_REQUIRE(not this->result);
207
this->result= (this->sample_timestamp > this->before_sample_timestamp);
208
BOOST_REQUIRE(this->result);
210
this->result= (this->sample_timestamp >= this->identical_with_sample_timestamp);
211
BOOST_REQUIRE(this->result);
213
this->result= (this->sample_timestamp >= this->after_sample_timestamp);
214
BOOST_REQUIRE(not this->result);
216
this->result= (this->sample_timestamp >= this->before_sample_timestamp);
217
BOOST_REQUIRE(this->result);
219
this->result= (this->sample_timestamp < this->identical_with_sample_timestamp);
220
BOOST_REQUIRE(not this->result);
222
this->result= (this->sample_timestamp < this->after_sample_timestamp);
223
BOOST_REQUIRE(this->result);
225
this->result= (this->sample_timestamp < this->before_sample_timestamp);
226
BOOST_REQUIRE(not this->result);
228
this->result= (this->sample_timestamp <= this->identical_with_sample_timestamp);
229
BOOST_REQUIRE(this->result);
231
this->result= (this->sample_timestamp <= this->after_sample_timestamp);
232
BOOST_REQUIRE(this->result);
234
this->result= (this->sample_timestamp <= this->before_sample_timestamp);
235
BOOST_REQUIRE(not this->result);
237
BOOST_AUTO_TEST_SUITE_END()
246
BOOST_FIXTURE_TEST_SUITE(TimestampRangesSuite, TimestampTest)
247
BOOST_AUTO_TEST_CASE(is_valid_minOfTimestampRange_shouldReturn_True)
249
uint32_t year= 1970, month= 1, day= 1, hour= 0, minute= 0, second= 0;
250
TemporalGenerator::TimestampGen::make_timestamp(×tamp, year, month, day, hour, minute, second);
252
result= timestamp.is_valid();
254
BOOST_REQUIRE(result);
257
BOOST_AUTO_TEST_CASE(is_valid_maxOfTimestampRange_shouldReturn_True)
259
uint32_t year= 2038, month= 1, day= 19, hour= 3, minute= 14, second= 7;
260
TemporalGenerator::TimestampGen::make_timestamp(×tamp, year, month, day, hour, minute, second);
262
result= timestamp.is_valid();
264
BOOST_REQUIRE(result);
267
BOOST_AUTO_TEST_CASE(is_valid_oneSecondBeforeTimestampMinOfRange_shouldReturn_False)
269
uint32_t year= 1969, month= 12, day= 31, hour= 23, minute= 59, second= 59;
270
TemporalGenerator::TimestampGen::make_timestamp(×tamp, year, month, day, hour, minute, second);
272
result= timestamp.is_valid();
274
BOOST_REQUIRE(not result);
277
BOOST_AUTO_TEST_CASE(is_valid_oneSecondAfterTimestampMaxOfRange_shouldReturn_False)
279
uint32_t year= 2038, month= 1, day= 19, hour= 3, minute= 14, second= 8;
280
TemporalGenerator::TimestampGen::make_timestamp(×tamp, year, month, day, hour, minute, second);
282
result= timestamp.is_valid();
284
BOOST_REQUIRE(not result);
287
BOOST_AUTO_TEST_CASE(is_valid_InsideOfTimestampRange_shouldReturn_True)
289
uint32_t year= 1980, month= 11, day= 1, hour= 5, minute= 8, second= 5;
290
TemporalGenerator::TimestampGen::make_timestamp(×tamp, year, month, day, hour, minute, second);
292
result= timestamp.is_valid();
294
BOOST_REQUIRE(result);
297
BOOST_AUTO_TEST_CASE(to_time_t)
300
TemporalGenerator::TimestampGen::make_timestamp(×tamp, 2009, 6, 3, 4, 59, 1);
302
timestamp.to_time_t(time);
304
BOOST_REQUIRE_EQUAL(1244005141, time);
307
BOOST_AUTO_TEST_CASE(outputStreamOperator_shouldWrite_hyphenSeperatedDateElements_and_colonSeperatedTimeElements)
309
std::ostringstream output;
310
std::string expected= "2010-05-01 08:07:06";
311
std::string returned;
312
TemporalGenerator::TimestampGen::make_timestamp(×tamp, 2010, 5, 1, 8, 7, 6);
315
returned= output.str();
317
BOOST_REQUIRE_EQUAL(expected, returned);
319
BOOST_AUTO_TEST_SUITE_END()