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;
34
class DateTestCompareOperators
40
Date identical_with_sample_date, before_sample_date, after_sample_date;
42
DateTestCompareOperators()
44
TemporalGenerator::DateGen::make_date(&sample_date, 2010, 9, 8);
45
TemporalGenerator::DateGen::make_date(&before_sample_date, 1980, 1, 1);
46
TemporalGenerator::DateGen::make_date(&identical_with_sample_date, 2010, 9, 8);
47
TemporalGenerator::DateGen::make_date(&after_sample_date, 2019, 5, 30);
51
class DateTimeTestCompareOperators
57
DateTime identical_with_sample_date, before_sample_date, after_sample_date;
59
DateTimeTestCompareOperators()
61
TemporalGenerator::DateGen::make_date(&sample_date, 2010, 9, 8);
62
TemporalGenerator::DateTimeGen::make_datetime(&before_sample_date, 1990, 12, 31, 12, 12, 30);
63
TemporalGenerator::DateTimeGen::make_datetime(&identical_with_sample_date, 2010, 9, 8, 0, 0, 0);
64
TemporalGenerator::DateTimeGen::make_datetime(&after_sample_date, 2020, 4, 4, 4, 4, 4);
68
class TimestampTestCompareOperators
74
Timestamp identical_with_sample_date, before_sample_date, after_sample_date;
76
TimestampTestCompareOperators()
78
TemporalGenerator::DateGen::make_date(&sample_date, 2010, 9, 8);
79
TemporalGenerator::TimestampGen::make_timestamp(&before_sample_date, 1980, 1, 1, 13, 56, 41);
80
TemporalGenerator::TimestampGen::make_timestamp(&identical_with_sample_date, 2010, 9, 8, 0, 0, 0);
81
TemporalGenerator::TimestampGen::make_timestamp(&after_sample_date, 2019, 5, 30, 9, 10, 13);
85
BOOST_AUTO_TEST_SUITE(DateTestCompare)
86
BOOST_FIXTURE_TEST_CASE(operatorComparingDate, DateTestCompareOperators)
88
result= (sample_date == identical_with_sample_date);
89
BOOST_REQUIRE(result);
91
result= (sample_date == before_sample_date);
92
BOOST_REQUIRE(not result);
94
result= (sample_date != identical_with_sample_date);
95
BOOST_REQUIRE(not result);
97
result= (sample_date != before_sample_date);
98
BOOST_REQUIRE(result);
100
result= (sample_date > identical_with_sample_date);
101
BOOST_REQUIRE(not this->result);
103
result= (sample_date > after_sample_date);
104
BOOST_REQUIRE(not result);
106
result= (sample_date > before_sample_date);
107
BOOST_REQUIRE(result);
109
result= (sample_date >= identical_with_sample_date);
110
BOOST_REQUIRE(result);
112
result= (sample_date >= after_sample_date);
113
BOOST_REQUIRE(not result);
115
result= (sample_date >= before_sample_date);
116
BOOST_REQUIRE(result);
118
result= (sample_date < identical_with_sample_date);
120
BOOST_REQUIRE(not result);
122
result= (sample_date < after_sample_date);
123
BOOST_REQUIRE(result);
125
result= (sample_date < before_sample_date);
126
BOOST_REQUIRE(not result);
128
result= (sample_date <= identical_with_sample_date);
129
BOOST_REQUIRE(result);
131
result= (sample_date <= after_sample_date);
133
BOOST_REQUIRE(result);
135
result= (sample_date <= before_sample_date);
136
BOOST_REQUIRE(not result);
139
BOOST_FIXTURE_TEST_CASE(operatorComparingDateTime, DateTimeTestCompareOperators)
141
result= (sample_date == identical_with_sample_date);
142
BOOST_REQUIRE(result);
144
result= (sample_date == before_sample_date);
145
BOOST_REQUIRE(not result);
147
result= (sample_date != identical_with_sample_date);
148
BOOST_REQUIRE(not result);
150
result= (sample_date != before_sample_date);
151
BOOST_REQUIRE(result);
153
result= (sample_date > identical_with_sample_date);
154
BOOST_REQUIRE(not this->result);
156
result= (sample_date > after_sample_date);
157
BOOST_REQUIRE(not result);
159
result= (sample_date > before_sample_date);
160
BOOST_REQUIRE(result);
162
result= (sample_date >= identical_with_sample_date);
163
BOOST_REQUIRE(result);
165
result= (sample_date >= after_sample_date);
166
BOOST_REQUIRE(not result);
168
result= (sample_date >= before_sample_date);
169
BOOST_REQUIRE(result);
171
result= (sample_date < identical_with_sample_date);
173
BOOST_REQUIRE(not result);
175
result= (sample_date < after_sample_date);
176
BOOST_REQUIRE(result);
178
result= (sample_date < before_sample_date);
179
BOOST_REQUIRE(not result);
181
result= (sample_date <= identical_with_sample_date);
182
BOOST_REQUIRE(result);
184
result= (sample_date <= after_sample_date);
186
BOOST_REQUIRE(result);
188
result= (sample_date <= before_sample_date);
189
BOOST_REQUIRE(not result);
193
BOOST_FIXTURE_TEST_CASE(operatorComparingTimestamp, TimestampTestCompareOperators)
195
result= (sample_date == identical_with_sample_date);
196
BOOST_REQUIRE(result);
198
result= (sample_date == before_sample_date);
199
BOOST_REQUIRE(not result);
201
result= (sample_date != identical_with_sample_date);
202
BOOST_REQUIRE(not result);
204
result= (sample_date != before_sample_date);
205
BOOST_REQUIRE(result);
207
result= (sample_date > identical_with_sample_date);
208
BOOST_REQUIRE(not this->result);
210
result= (sample_date > after_sample_date);
211
BOOST_REQUIRE(not result);
213
result= (sample_date > before_sample_date);
214
BOOST_REQUIRE(result);
216
result= (sample_date >= identical_with_sample_date);
217
BOOST_REQUIRE(result);
219
result= (sample_date >= after_sample_date);
220
BOOST_REQUIRE(not result);
222
result= (sample_date >= before_sample_date);
223
BOOST_REQUIRE(result);
225
result= (sample_date < identical_with_sample_date);
227
BOOST_REQUIRE(not result);
229
result= (sample_date < after_sample_date);
230
BOOST_REQUIRE(result);
232
result= (sample_date < before_sample_date);
233
BOOST_REQUIRE(not result);
235
result= (sample_date <= identical_with_sample_date);
236
BOOST_REQUIRE(result);
238
result= (sample_date <= after_sample_date);
240
BOOST_REQUIRE(result);
242
result= (sample_date <= before_sample_date);
243
BOOST_REQUIRE(not result);
245
BOOST_AUTO_TEST_SUITE_END()
256
TemporalGenerator::DateGen::make_valid_date(&date);
260
BOOST_AUTO_TEST_SUITE(CurrentDateValidationTest)
261
BOOST_FIXTURE_TEST_CASE(operatorAssign_shouldCopyDateRelatadComponents, DateTest)
265
BOOST_REQUIRE_EQUAL(date.years(), copy.years());
266
BOOST_REQUIRE_EQUAL(date.months(), copy.months());
267
BOOST_REQUIRE_EQUAL(date.days(), copy.days());
270
BOOST_FIXTURE_TEST_CASE(is_valid_onValidDate_shouldReturn_True, DateTest)
272
result= date.is_valid();
273
BOOST_REQUIRE(result);
276
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithYearBelowMinimum_shouldReturn_False, DateTest)
278
date.set_years(DRIZZLE_MIN_YEARS_SQL - 1);
280
result= date.is_valid();
282
BOOST_REQUIRE(not result);
285
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithYearAboveMaximum_shouldReturn_False, DateTest)
287
date.set_years(DRIZZLE_MAX_YEARS_SQL + 1);
289
result= date.is_valid();
291
BOOST_REQUIRE(not result);
294
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithMonthSetToZero_shouldReturn_False, DateTest)
298
result= date.is_valid();
300
BOOST_REQUIRE(not result);
304
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithMonthAboveMaximum_shouldReturn_False, DateTest)
308
result= date.is_valid();
310
BOOST_REQUIRE(not result);
313
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithDaySetToZero_shouldReturn_False, DateTest)
317
result= date.is_valid();
319
BOOST_REQUIRE(not result);
322
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithDayAboveDaysInMonth_shouldReturn_False, DateTest)
326
result= date.is_valid();
328
BOOST_REQUIRE(not result);
331
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithLeapDayInNonLeapYear_shouldReturn_False, DateTest)
333
TemporalGenerator::TemporalGen::leap_day_in_non_leap_year(&date);
335
result= date.is_valid();
337
BOOST_REQUIRE(not result);
340
BOOST_FIXTURE_TEST_CASE(is_valid_onValidDateWithLeapDayInLeapYear_shouldReturn_True, DateTest)
342
TemporalGenerator::TemporalGen::leap_day_in_leap_year(&date);
344
result= date.is_valid();
346
BOOST_REQUIRE(result);
349
BOOST_FIXTURE_TEST_CASE(to_string_shouldProduce_hyphenSeperatedDateElements, DateTest)
351
char expected[Date::MAX_STRING_LENGTH]= "2010-05-01";
352
char returned[Date::MAX_STRING_LENGTH];
353
TemporalGenerator::DateGen::make_date(&date, 2010, 5, 1);
355
date.to_string(returned, Date::MAX_STRING_LENGTH);
357
BOOST_REQUIRE_EQUAL(expected, returned);
360
BOOST_FIXTURE_TEST_CASE(to_string_nullBuffer_shouldReturnProperLengthAnyway, DateTest)
362
int length= date.to_string(NULL, 0);
364
BOOST_REQUIRE_EQUAL(Date::MAX_STRING_LENGTH - 1, length);
367
BOOST_FIXTURE_TEST_CASE(from_string_validString_shouldPopulateCorrectly, DateTest)
369
char valid_string[Date::MAX_STRING_LENGTH]= "2010-05-01";
370
uint32_t years, months, days;
372
init_temporal_formats();
374
result= date.from_string(valid_string, Date::MAX_STRING_LENGTH - 1);
375
BOOST_REQUIRE(result);
378
months= date.months();
381
deinit_temporal_formats();
383
BOOST_REQUIRE_EQUAL(2010, years);
384
BOOST_REQUIRE_EQUAL(5, months);
385
BOOST_REQUIRE_EQUAL(1, days);
388
BOOST_FIXTURE_TEST_CASE(from_string_invalidString_shouldReturn_False, DateTest)
390
char valid_string[Date::MAX_STRING_LENGTH]= "2x10-05-01";
392
init_temporal_formats();
393
result= date.from_string(valid_string, Date::MAX_STRING_LENGTH - 1);
394
deinit_temporal_formats();
396
BOOST_REQUIRE(not result);
399
BOOST_FIXTURE_TEST_CASE(to_int64_t, DateTest)
401
TemporalGenerator::DateGen::make_date(&date, 2030, 8, 17);
402
int64_t representation;
404
date.to_int64_t(&representation);
406
BOOST_REQUIRE_EQUAL(20300817, representation);
409
BOOST_FIXTURE_TEST_CASE(to_int32_t, DateTest)
411
TemporalGenerator::DateGen::make_date(&date, 2030, 8, 17);
412
int32_t representation;
414
date.to_int32_t(&representation);
416
BOOST_REQUIRE_EQUAL(20300817, representation);
419
BOOST_FIXTURE_TEST_CASE(from_int32_t_shouldPopulateDateCorrectly, DateTest)
421
uint32_t decoded_years, decoded_months, decoded_days;
423
date.from_int32_t(20300817);
425
decoded_years= date.years();
426
decoded_months= date.months();
427
decoded_days= date.days();
429
BOOST_REQUIRE_EQUAL(2030, decoded_years);
430
BOOST_REQUIRE_EQUAL(8, decoded_months);
431
BOOST_REQUIRE_EQUAL(17, decoded_days);
434
BOOST_FIXTURE_TEST_CASE(to_julian_day_number, DateTest)
437
TemporalGenerator::DateGen::make_date(&date, 1999, 12, 31);
439
date.to_julian_day_number(&julian_day);
441
BOOST_REQUIRE_EQUAL(2451544, julian_day);
444
BOOST_FIXTURE_TEST_CASE(from_julian_day_number, DateTest)
446
int64_t julian_day= 2451544;
447
uint32_t years, months, days;
449
date.from_julian_day_number(julian_day);
452
months= date.months();
455
BOOST_REQUIRE_EQUAL(1999, years);
456
BOOST_REQUIRE_EQUAL(12, months);
457
BOOST_REQUIRE_EQUAL(31, days);
460
BOOST_FIXTURE_TEST_CASE(to_tm, DateTest)
462
uint32_t years= 2030, months= 8, days= 17;
463
TemporalGenerator::DateGen::make_date(&date, years, months, days);
468
BOOST_REQUIRE_EQUAL(130, filled.tm_year);
469
BOOST_REQUIRE_EQUAL(7, filled.tm_mon);
470
BOOST_REQUIRE_EQUAL(17, filled.tm_mday);
471
BOOST_REQUIRE_EQUAL(0, filled.tm_hour);
472
BOOST_REQUIRE_EQUAL(0, filled.tm_min);
473
BOOST_REQUIRE_EQUAL(0, filled.tm_sec);
475
/* TODO: these fail, shouldn't they also be set properly? */
476
/* From Andrew: These fail because Temporal::to_tm doesn't populate them.
477
Does it need to or would we be wasting cycles doing it? */
479
BOOST_WARN_EQUAL(228, filled.tm_yday);
480
BOOST_WARN_EQUAL(6, filled.tm_wday);
481
BOOST_WARN_EQUAL(-1, filled.tm_isdst);
484
BOOST_FIXTURE_TEST_CASE(from_tm, DateTest)
486
uint32_t years, months, days;
488
from.tm_year= 1956 - 1900;
495
months= date.months();
498
BOOST_REQUIRE_EQUAL(1956, years);
499
BOOST_REQUIRE_EQUAL(3, months);
500
BOOST_REQUIRE_EQUAL(30, days);
503
BOOST_FIXTURE_TEST_CASE(to_time_t, DateTest)
506
TemporalGenerator::DateGen::make_date(&date, 1990, 9, 9);
508
date.to_time_t(time);
510
BOOST_REQUIRE_EQUAL(652838400, time);
513
BOOST_FIXTURE_TEST_CASE(from_time_t, DateTest)
515
uint32_t years, months, days;
517
date.from_time_t(652838400);
520
months= date.months();
523
BOOST_REQUIRE_EQUAL(1990, years);
524
BOOST_REQUIRE_EQUAL(9, months);
525
BOOST_REQUIRE_EQUAL(9, days);
528
BOOST_FIXTURE_TEST_CASE(to_decimal, DateTest)
530
drizzled::type::Decimal to;
531
TemporalGenerator::DateGen::make_date(&date, 1987, 5, 6);
533
date.to_decimal(&to);
535
BOOST_REQUIRE_EQUAL(19870506, to.buf[0]);
537
BOOST_AUTO_TEST_SUITE_END()
539
BOOST_AUTO_TEST_SUITE(DateStringTest)
540
BOOST_AUTO_TEST_CASE(DateFromStringTest)
543
const char *valid_strings[]= {"20100607", /* YYYYMMDD */
544
"06/07/2010",/* MM[-/.]DD[-/.]YYYY (US common format)*/
545
"10.06.07",/* YY[-/.]MM[-/.]DD */
546
"10/6/7",/* YY[-/.][M]M[-/.][D]D */
547
"2010-6-7"/* YYYY[-/.][M]M[-/.][D]D */};
549
init_temporal_formats();
550
for (int it= 0; it < 5; it++)
552
const char *valid_string= valid_strings[it];
553
bool result= date.from_string(valid_string, strlen(valid_string));
554
BOOST_REQUIRE(result);
556
BOOST_REQUIRE_EQUAL(2010, date.years());
557
BOOST_REQUIRE_EQUAL(6, date.months());
558
BOOST_REQUIRE_EQUAL(7, date.days());
560
deinit_temporal_formats();
562
BOOST_AUTO_TEST_SUITE_END()