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/type/decimal.h>
27
#include <drizzled/temporal.h>
28
#include <drizzled/temporal_format.h>
30
#include "temporal_generator.h"
32
using namespace drizzled;
39
uint32_t hours, minutes, seconds;
41
Time identical_with_sample_time, before_sample_time, after_sample_time;
45
TemporalGenerator::TimeGen::make_time(&sample_time, 18, 34, 59);
47
TemporalGenerator::TimeGen::make_time(&before_sample_time, 18, 34, 58);
48
TemporalGenerator::TimeGen::make_time(&identical_with_sample_time, 18, 34, 59);
49
TemporalGenerator::TimeGen::make_time(&after_sample_time, 18, 35, 0);
53
void assign_time_values()
55
hours= sample_time.hours();
56
minutes= sample_time.minutes();
57
seconds= sample_time.seconds();
60
void from_string(const char *string)
63
init_temporal_formats();
64
result= sample_time.from_string(string, strlen(string));
65
deinit_temporal_formats();
71
BOOST_FIXTURE_TEST_SUITE(TimeTestSuite, TimeTest)
72
BOOST_AUTO_TEST_CASE(operatorEqual_ComparingWithIdencticalTime_ShouldReturn_True)
74
this->result= (this->sample_time == this->identical_with_sample_time);
76
BOOST_REQUIRE(this->result);
79
BOOST_AUTO_TEST_CASE(operatorEqual_ComparingWithDifferentTemporal_ShouldReturn_False)
81
this->result= (this->sample_time == this->before_sample_time);
83
BOOST_REQUIRE(not this->result);
86
BOOST_AUTO_TEST_CASE(operatorNotEqual_ComparingWithIdencticalTemporal_ShouldReturn_False)
88
this->result= (this->sample_time != this->identical_with_sample_time);
90
BOOST_REQUIRE(not this->result);
93
BOOST_AUTO_TEST_CASE(operatorNotEqual_ComparingWithDifferentTemporal_ShouldReturn_True)
95
this->result= (this->sample_time != this->before_sample_time);
97
BOOST_REQUIRE(this->result);
100
BOOST_AUTO_TEST_CASE(operatorGreaterThan_ComparingWithIdenticalTemporal_ShouldReturn_False)
102
this->result= (this->sample_time > this->identical_with_sample_time);
104
BOOST_REQUIRE(not this->result);
107
BOOST_AUTO_TEST_CASE(operatorGreaterThan_ComparingWithLaterTemporal_ShouldReturn_False)
109
this->result= (this->sample_time > this->after_sample_time);
111
BOOST_REQUIRE(not this->result);
114
BOOST_AUTO_TEST_CASE(operatorGreaterThan_ComparingWithEarlierTemporal_ShouldReturn_True)
116
this->result= (this->sample_time > this->before_sample_time);
118
BOOST_REQUIRE(this->result);
121
BOOST_AUTO_TEST_CASE(operatorGreaterThanOrEqual_ComparingWithIdenticalTemporal_ShouldReturn_True)
123
this->result= (this->sample_time >= this->identical_with_sample_time);
125
BOOST_REQUIRE(this->result);
128
BOOST_AUTO_TEST_CASE(operatorGreaterThanOrEqual_ComparingWithLaterTemporal_ShouldReturn_False)
130
this->result= (this->sample_time >= this->after_sample_time);
132
BOOST_REQUIRE(not this->result);
135
BOOST_AUTO_TEST_CASE(operatorGreaterThanOrEqual_ComparingWithEarlierTemporal_ShouldReturn_True)
137
this->result= (this->sample_time >= this->before_sample_time);
139
BOOST_REQUIRE(this->result);
142
BOOST_AUTO_TEST_CASE(operatorLessThan_ComparingWithIdenticalTemporal_ShouldReturn_False)
144
this->result= (this->sample_time < this->identical_with_sample_time);
146
BOOST_REQUIRE(not this->result);
149
BOOST_AUTO_TEST_CASE(operatorLessThan_ComparingWithLaterTemporal_ShouldReturn_True)
151
this->result= (this->sample_time < this->after_sample_time);
153
BOOST_REQUIRE(this->result);
156
BOOST_AUTO_TEST_CASE(operatorLessThan_ComparingWithEarlierTemporal_ShouldReturn_False)
158
this->result= (this->sample_time < this->before_sample_time);
160
BOOST_REQUIRE(not this->result);
163
BOOST_AUTO_TEST_CASE(operatorLessThanOrEqual_ComparingWithIdenticalTemporal_ShouldReturn_True)
165
this->result= (this->sample_time <= this->identical_with_sample_time);
167
BOOST_REQUIRE(this->result);
170
BOOST_AUTO_TEST_CASE(operatorLessThanOrEqual_ComparingWithLaterTemporal_ShouldReturn_True)
172
this->result= (this->sample_time <= this->after_sample_time);
174
BOOST_REQUIRE(this->result);
177
BOOST_AUTO_TEST_CASE(operatorLessThanOrEqual_ComparingWithEarlierTemporal_ShouldReturn_False)
179
this->result= (this->sample_time <= this->before_sample_time);
181
BOOST_REQUIRE(not this->result);
184
BOOST_AUTO_TEST_CASE(is_valid_onValidTime_shouldReturn_True)
186
result= sample_time.is_valid();
188
BOOST_REQUIRE(result);
191
BOOST_AUTO_TEST_CASE(is_valid_onValidMinimalTime_shouldReturn_True)
193
TemporalGenerator::TemporalGen::make_min_time(&sample_time);
195
result= sample_time.is_valid();
197
BOOST_REQUIRE(result);
200
BOOST_AUTO_TEST_CASE(is_valid_onValidMaximalTime_shouldReturn_True)
202
TemporalGenerator::TemporalGen::make_max_time(&sample_time);
204
result= sample_time.is_valid();
206
BOOST_REQUIRE(result);
209
BOOST_AUTO_TEST_CASE(is_valid_onInvalidTimeWithHourAboveMaximum23_shouldReturn_False)
211
sample_time.set_hours(24);
213
result= sample_time.is_valid();
215
BOOST_REQUIRE(not result);
218
BOOST_AUTO_TEST_CASE(is_valid_onInvalidTimeWithMinutesAboveMaximum59_shouldReturn_False)
220
sample_time.set_minutes(60);
222
result= sample_time.is_valid();
224
BOOST_REQUIRE(not result);
227
BOOST_AUTO_TEST_CASE(is_valid_onInvalidTimeWithSecondsAboveMaximum59_shouldReturn_False)
229
sample_time.set_seconds(60);
231
result= sample_time.is_valid();
233
BOOST_REQUIRE(not result);
236
BOOST_AUTO_TEST_CASE(to_string_shouldProduce_colonSeperatedTimeElements)
238
char expected[Time::MAX_STRING_LENGTH]= "18:34:59";
239
char returned[Time::MAX_STRING_LENGTH];
241
sample_time.to_string(returned, Time::MAX_STRING_LENGTH);
243
BOOST_REQUIRE_EQUAL(expected, returned);
246
BOOST_AUTO_TEST_CASE(to_string_nullBuffer_shouldReturnProperLengthAnyway)
248
int length= sample_time.to_string(NULL, 0);
250
BOOST_REQUIRE_EQUAL(Time::MAX_STRING_LENGTH - 1, length);
253
BOOST_AUTO_TEST_CASE(to_int32_t)
255
int32_t representation;
257
sample_time.to_int32_t(&representation);
259
BOOST_REQUIRE_EQUAL(representation, 183459);
262
BOOST_AUTO_TEST_CASE(from_int32_t_shouldPopulateTimeCorrectly)
264
sample_time.from_int32_t(183459);
266
assign_time_values();;
268
BOOST_REQUIRE_EQUAL(18, hours);
269
BOOST_REQUIRE_EQUAL(34, minutes);
270
BOOST_REQUIRE_EQUAL(59, seconds);
273
BOOST_AUTO_TEST_CASE(from_time_t)
275
sample_time.from_time_t(59588);
277
assign_time_values();
279
BOOST_REQUIRE_EQUAL(16, hours);
280
BOOST_REQUIRE_EQUAL(33, minutes);
281
BOOST_REQUIRE_EQUAL(8, seconds);
284
BOOST_AUTO_TEST_CASE(to_decimal)
286
drizzled::type::Decimal to;
287
TemporalGenerator::TimeGen::make_time(&sample_time, 8, 4, 9, 56);
289
sample_time.to_decimal(&to);
291
BOOST_REQUIRE_EQUAL(80409, to.buf[0]);
292
BOOST_REQUIRE_EQUAL(56000, to.buf[1]);
295
BOOST_AUTO_TEST_CASE(from_string_invalidString_shouldReturn_False)
297
char invalid_string[Time::MAX_STRING_LENGTH]= "1o:34:59";
299
init_temporal_formats();
300
result= sample_time.from_string(invalid_string, strlen(invalid_string));
301
deinit_temporal_formats();
303
BOOST_REQUIRE(not result);
306
BOOST_AUTO_TEST_CASE(from_string_validString_minuteAndSecond_shouldPopulateCorrectly)
308
char valid_string[Time::MAX_STRING_LENGTH]= "4:52";
310
from_string(valid_string);
312
BOOST_REQUIRE_EQUAL(4, minutes);
313
BOOST_REQUIRE_EQUAL(52, seconds);
316
BOOST_AUTO_TEST_CASE(from_string_validString_minuteAndSecondNoColon_shouldPopulateCorrectly)
318
char valid_string[Time::MAX_STRING_LENGTH]= "3456";
320
from_string(valid_string);
322
BOOST_REQUIRE_EQUAL(34, minutes);
323
BOOST_REQUIRE_EQUAL(56, seconds);
326
BOOST_AUTO_TEST_CASE(from_string_validString_secondsOnly_shouldPopulateCorrectly)
328
char valid_string[Time::MAX_STRING_LENGTH]= "59";
330
from_string(valid_string);
332
BOOST_REQUIRE_EQUAL(59, seconds);
334
BOOST_AUTO_TEST_SUITE_END()
336
class TimeFromStringTest
341
uint32_t hours, minutes, seconds;
345
init_temporal_formats();
348
~TimeFromStringTest()
350
deinit_temporal_formats();
353
void assign_time_values()
356
minutes= time.minutes();
357
seconds= time.seconds();
361
BOOST_FIXTURE_TEST_SUITE(TimeFromStringTestSuite, TimeFromStringTest)
362
BOOST_AUTO_TEST_CASE(from_string)
364
const char *valid_strings[]= {"080409",
370
for (int it= 0; it < 6; it++)
372
const char *valid_string= valid_strings[it];
374
result= time.from_string(valid_string, strlen(valid_string));
375
BOOST_REQUIRE(result);
377
assign_time_values();
379
BOOST_REQUIRE_EQUAL(8, hours);
380
BOOST_REQUIRE_EQUAL(4, minutes);
381
BOOST_REQUIRE_EQUAL(9, seconds);
384
BOOST_AUTO_TEST_SUITE_END()