~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to unittests/date_test.cc

  • Committer: Monty Taylor
  • Date: 2009-01-29 19:04:39 UTC
  • mto: (779.3.29 devel)
  • mto: This revision was merged to the branch mainline in revision 823.
  • Revision ID: mordred@inaugust.com-20090129190439-vfr95s6gaudjacm7
Add timegm which is missing on Solaris.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2010 Pawel Blokus
5
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 */
20
 
 
21
 
#include "config.h"
22
 
 
23
 
#define BOOST_TEST_DYN_LINK
24
 
#include <boost/test/unit_test.hpp>
25
 
 
26
 
#include <drizzled/type/decimal.h>
27
 
#include <drizzled/temporal.h>
28
 
#include <drizzled/temporal_format.h>
29
 
 
30
 
#include "temporal_generator.h"
31
 
 
32
 
using namespace drizzled;
33
 
 
34
 
class DateTestCompareOperators
35
 
{
36
 
 protected:
37
 
  Date sample_date;
38
 
  bool result;
39
 
  
40
 
  Date identical_with_sample_date, before_sample_date, after_sample_date;
41
 
  
42
 
  DateTestCompareOperators()
43
 
  {
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);
48
 
  }
49
 
};
50
 
 
51
 
class DateTimeTestCompareOperators
52
 
{
53
 
 protected:
54
 
  Date sample_date;
55
 
  bool result;
56
 
  
57
 
  DateTime identical_with_sample_date, before_sample_date, after_sample_date;
58
 
  
59
 
  DateTimeTestCompareOperators()
60
 
  {
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);
65
 
  }
66
 
};
67
 
 
68
 
class TimestampTestCompareOperators
69
 
{
70
 
 protected:
71
 
  Date sample_date;
72
 
  bool result;
73
 
  
74
 
  Timestamp identical_with_sample_date, before_sample_date, after_sample_date;
75
 
  
76
 
  TimestampTestCompareOperators()
77
 
  {
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);
82
 
  }
83
 
};
84
 
 
85
 
BOOST_AUTO_TEST_SUITE(DateTestCompare)
86
 
BOOST_FIXTURE_TEST_CASE(operatorComparingDate, DateTestCompareOperators)
87
 
{
88
 
  result= (sample_date == identical_with_sample_date);
89
 
  BOOST_REQUIRE(result);
90
 
 
91
 
  result= (sample_date == before_sample_date);  
92
 
  BOOST_REQUIRE(not result);
93
 
 
94
 
  result= (sample_date != identical_with_sample_date);
95
 
  BOOST_REQUIRE(not result);
96
 
 
97
 
  result= (sample_date != before_sample_date);  
98
 
  BOOST_REQUIRE(result);
99
 
 
100
 
  result= (sample_date > identical_with_sample_date);
101
 
  BOOST_REQUIRE(not this->result);
102
 
 
103
 
  result= (sample_date > after_sample_date);
104
 
  BOOST_REQUIRE(not result);
105
 
 
106
 
  result= (sample_date > before_sample_date);
107
 
  BOOST_REQUIRE(result);
108
 
 
109
 
  result= (sample_date >= identical_with_sample_date);
110
 
  BOOST_REQUIRE(result);
111
 
 
112
 
  result= (sample_date >= after_sample_date);
113
 
  BOOST_REQUIRE(not result);
114
 
 
115
 
  result= (sample_date >= before_sample_date);
116
 
  BOOST_REQUIRE(result);
117
 
 
118
 
  result= (sample_date < identical_with_sample_date);
119
 
  
120
 
  BOOST_REQUIRE(not result);
121
 
 
122
 
  result= (sample_date < after_sample_date);
123
 
  BOOST_REQUIRE(result);
124
 
 
125
 
  result= (sample_date < before_sample_date);
126
 
  BOOST_REQUIRE(not result);
127
 
 
128
 
  result= (sample_date <= identical_with_sample_date);
129
 
  BOOST_REQUIRE(result);
130
 
 
131
 
  result= (sample_date <= after_sample_date);
132
 
  
133
 
  BOOST_REQUIRE(result);
134
 
 
135
 
  result= (sample_date <= before_sample_date);
136
 
  BOOST_REQUIRE(not result);
137
 
}
138
 
 
139
 
BOOST_FIXTURE_TEST_CASE(operatorComparingDateTime, DateTimeTestCompareOperators)
140
 
{
141
 
  result= (sample_date == identical_with_sample_date);
142
 
  BOOST_REQUIRE(result);
143
 
 
144
 
  result= (sample_date == before_sample_date);  
145
 
  BOOST_REQUIRE(not result);
146
 
 
147
 
  result= (sample_date != identical_with_sample_date);
148
 
  BOOST_REQUIRE(not result);
149
 
 
150
 
  result= (sample_date != before_sample_date);  
151
 
  BOOST_REQUIRE(result);
152
 
 
153
 
  result= (sample_date > identical_with_sample_date);
154
 
  BOOST_REQUIRE(not this->result);
155
 
 
156
 
  result= (sample_date > after_sample_date);
157
 
  BOOST_REQUIRE(not result);
158
 
 
159
 
  result= (sample_date > before_sample_date);
160
 
  BOOST_REQUIRE(result);
161
 
 
162
 
  result= (sample_date >= identical_with_sample_date);
163
 
  BOOST_REQUIRE(result);
164
 
 
165
 
  result= (sample_date >= after_sample_date);
166
 
  BOOST_REQUIRE(not result);
167
 
 
168
 
  result= (sample_date >= before_sample_date);
169
 
  BOOST_REQUIRE(result);
170
 
 
171
 
  result= (sample_date < identical_with_sample_date);
172
 
  
173
 
  BOOST_REQUIRE(not result);
174
 
 
175
 
  result= (sample_date < after_sample_date);
176
 
  BOOST_REQUIRE(result);
177
 
 
178
 
  result= (sample_date < before_sample_date);
179
 
  BOOST_REQUIRE(not result);
180
 
 
181
 
  result= (sample_date <= identical_with_sample_date);
182
 
  BOOST_REQUIRE(result);
183
 
 
184
 
  result= (sample_date <= after_sample_date);
185
 
  
186
 
  BOOST_REQUIRE(result);
187
 
 
188
 
  result= (sample_date <= before_sample_date);
189
 
  BOOST_REQUIRE(not result);
190
 
}
191
 
 
192
 
 
193
 
BOOST_FIXTURE_TEST_CASE(operatorComparingTimestamp, TimestampTestCompareOperators)
194
 
{
195
 
  result= (sample_date == identical_with_sample_date);
196
 
  BOOST_REQUIRE(result);
197
 
 
198
 
  result= (sample_date == before_sample_date);  
199
 
  BOOST_REQUIRE(not result);
200
 
 
201
 
  result= (sample_date != identical_with_sample_date);
202
 
  BOOST_REQUIRE(not result);
203
 
 
204
 
  result= (sample_date != before_sample_date);  
205
 
  BOOST_REQUIRE(result);
206
 
 
207
 
  result= (sample_date > identical_with_sample_date);
208
 
  BOOST_REQUIRE(not this->result);
209
 
 
210
 
  result= (sample_date > after_sample_date);
211
 
  BOOST_REQUIRE(not result);
212
 
 
213
 
  result= (sample_date > before_sample_date);
214
 
  BOOST_REQUIRE(result);
215
 
 
216
 
  result= (sample_date >= identical_with_sample_date);
217
 
  BOOST_REQUIRE(result);
218
 
 
219
 
  result= (sample_date >= after_sample_date);
220
 
  BOOST_REQUIRE(not result);
221
 
 
222
 
  result= (sample_date >= before_sample_date);
223
 
  BOOST_REQUIRE(result);
224
 
 
225
 
  result= (sample_date < identical_with_sample_date);
226
 
  
227
 
  BOOST_REQUIRE(not result);
228
 
 
229
 
  result= (sample_date < after_sample_date);
230
 
  BOOST_REQUIRE(result);
231
 
 
232
 
  result= (sample_date < before_sample_date);
233
 
  BOOST_REQUIRE(not result);
234
 
 
235
 
  result= (sample_date <= identical_with_sample_date);
236
 
  BOOST_REQUIRE(result);
237
 
 
238
 
  result= (sample_date <= after_sample_date);
239
 
  
240
 
  BOOST_REQUIRE(result);
241
 
 
242
 
  result= (sample_date <= before_sample_date);
243
 
  BOOST_REQUIRE(not result);
244
 
}
245
 
BOOST_AUTO_TEST_SUITE_END()
246
 
 
247
 
 
248
 
class DateTest
249
 
{
250
 
  protected:
251
 
    Date date;
252
 
    bool result;
253
 
    
254
 
    DateTest()
255
 
    {
256
 
      TemporalGenerator::DateGen::make_valid_date(&date);
257
 
    }
258
 
};
259
 
 
260
 
BOOST_AUTO_TEST_SUITE(CurrentDateValidationTest)
261
 
BOOST_FIXTURE_TEST_CASE(operatorAssign_shouldCopyDateRelatadComponents, DateTest)
262
 
{
263
 
  Date copy= date;
264
 
 
265
 
  BOOST_REQUIRE_EQUAL(date.years(), copy.years());
266
 
  BOOST_REQUIRE_EQUAL(date.months(), copy.months());
267
 
  BOOST_REQUIRE_EQUAL(date.days(), copy.days());
268
 
}
269
 
 
270
 
BOOST_FIXTURE_TEST_CASE(is_valid_onValidDate_shouldReturn_True, DateTest)
271
 
{
272
 
  result= date.is_valid();
273
 
  BOOST_REQUIRE(result);
274
 
}
275
 
 
276
 
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithYearBelowMinimum_shouldReturn_False, DateTest)
277
 
{
278
 
  date.set_years(DRIZZLE_MIN_YEARS_SQL - 1);
279
 
  
280
 
  result= date.is_valid();
281
 
  
282
 
  BOOST_REQUIRE(not result);
283
 
}
284
 
 
285
 
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithYearAboveMaximum_shouldReturn_False, DateTest)
286
 
{
287
 
  date.set_years(DRIZZLE_MAX_YEARS_SQL + 1);
288
 
    
289
 
  result= date.is_valid();
290
 
  
291
 
  BOOST_REQUIRE(not result);
292
 
}
293
 
 
294
 
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithMonthSetToZero_shouldReturn_False, DateTest)
295
 
{
296
 
  date.set_months(0);
297
 
  
298
 
  result= date.is_valid();
299
 
  
300
 
  BOOST_REQUIRE(not result);
301
 
}
302
 
 
303
 
 
304
 
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithMonthAboveMaximum_shouldReturn_False, DateTest)
305
 
{
306
 
  date.set_months(13);
307
 
  
308
 
  result= date.is_valid();
309
 
  
310
 
  BOOST_REQUIRE(not result);
311
 
}
312
 
 
313
 
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithDaySetToZero_shouldReturn_False, DateTest)
314
 
{
315
 
  date.set_days(0);
316
 
  
317
 
  result= date.is_valid();
318
 
  
319
 
  BOOST_REQUIRE(not result);
320
 
}
321
 
 
322
 
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithDayAboveDaysInMonth_shouldReturn_False, DateTest)
323
 
{
324
 
  date.set_days(32);
325
 
  
326
 
  result= date.is_valid();
327
 
  
328
 
  BOOST_REQUIRE(not result);
329
 
}
330
 
 
331
 
BOOST_FIXTURE_TEST_CASE(is_valid_onInvalidDateWithLeapDayInNonLeapYear_shouldReturn_False, DateTest)
332
 
{
333
 
  TemporalGenerator::TemporalGen::leap_day_in_non_leap_year(&date);
334
 
  
335
 
  result= date.is_valid();
336
 
  
337
 
  BOOST_REQUIRE(not result);
338
 
}
339
 
 
340
 
BOOST_FIXTURE_TEST_CASE(is_valid_onValidDateWithLeapDayInLeapYear_shouldReturn_True, DateTest)
341
 
{
342
 
  TemporalGenerator::TemporalGen::leap_day_in_leap_year(&date);
343
 
  
344
 
  result= date.is_valid();
345
 
  
346
 
  BOOST_REQUIRE(result);
347
 
}
348
 
 
349
 
BOOST_FIXTURE_TEST_CASE(to_string_shouldProduce_hyphenSeperatedDateElements, DateTest)
350
 
{
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);
354
 
  
355
 
  date.to_string(returned, Date::MAX_STRING_LENGTH);
356
 
  
357
 
  BOOST_REQUIRE_EQUAL(expected, returned);
358
 
}
359
 
 
360
 
BOOST_FIXTURE_TEST_CASE(to_string_nullBuffer_shouldReturnProperLengthAnyway, DateTest)
361
 
{
362
 
  int length= date.to_string(NULL, 0);
363
 
  
364
 
  BOOST_REQUIRE_EQUAL(Date::MAX_STRING_LENGTH - 1, length);  
365
 
}
366
 
 
367
 
BOOST_FIXTURE_TEST_CASE(from_string_validString_shouldPopulateCorrectly, DateTest)
368
 
{
369
 
  char valid_string[Date::MAX_STRING_LENGTH]= "2010-05-01";
370
 
  uint32_t years, months, days;
371
 
 
372
 
  init_temporal_formats();
373
 
  
374
 
  result= date.from_string(valid_string, Date::MAX_STRING_LENGTH - 1);
375
 
  BOOST_REQUIRE(result);
376
 
  
377
 
  years= date.years();
378
 
  months= date.months();
379
 
  days= date.days();
380
 
 
381
 
  deinit_temporal_formats();
382
 
  
383
 
  BOOST_REQUIRE_EQUAL(2010, years);
384
 
  BOOST_REQUIRE_EQUAL(5, months);
385
 
  BOOST_REQUIRE_EQUAL(1, days);
386
 
}
387
 
 
388
 
BOOST_FIXTURE_TEST_CASE(from_string_invalidString_shouldReturn_False, DateTest)
389
 
{
390
 
  char valid_string[Date::MAX_STRING_LENGTH]= "2x10-05-01";
391
 
 
392
 
  init_temporal_formats();
393
 
  result= date.from_string(valid_string, Date::MAX_STRING_LENGTH - 1);
394
 
  deinit_temporal_formats();
395
 
  
396
 
  BOOST_REQUIRE(not result);
397
 
}
398
 
 
399
 
BOOST_FIXTURE_TEST_CASE(to_int64_t, DateTest)
400
 
{
401
 
  TemporalGenerator::DateGen::make_date(&date, 2030, 8, 17);
402
 
  int64_t representation;
403
 
  
404
 
  date.to_int64_t(&representation);
405
 
  
406
 
  BOOST_REQUIRE_EQUAL(20300817, representation);
407
 
}
408
 
 
409
 
BOOST_FIXTURE_TEST_CASE(to_int32_t, DateTest)
410
 
{
411
 
  TemporalGenerator::DateGen::make_date(&date, 2030, 8, 17);
412
 
  int32_t representation;
413
 
 
414
 
  date.to_int32_t(&representation);
415
 
 
416
 
  BOOST_REQUIRE_EQUAL(20300817, representation);
417
 
}
418
 
 
419
 
BOOST_FIXTURE_TEST_CASE(from_int32_t_shouldPopulateDateCorrectly, DateTest)
420
 
{
421
 
  uint32_t decoded_years, decoded_months, decoded_days;
422
 
 
423
 
  date.from_int32_t(20300817);
424
 
  
425
 
  decoded_years= date.years();
426
 
  decoded_months= date.months();
427
 
  decoded_days= date.days();
428
 
  
429
 
  BOOST_REQUIRE_EQUAL(2030, decoded_years);
430
 
  BOOST_REQUIRE_EQUAL(8, decoded_months);
431
 
  BOOST_REQUIRE_EQUAL(17, decoded_days);
432
 
}
433
 
 
434
 
BOOST_FIXTURE_TEST_CASE(to_julian_day_number, DateTest)
435
 
{
436
 
  int64_t julian_day;
437
 
  TemporalGenerator::DateGen::make_date(&date, 1999, 12, 31);
438
 
  
439
 
  date.to_julian_day_number(&julian_day);
440
 
  
441
 
  BOOST_REQUIRE_EQUAL(2451544, julian_day);
442
 
}
443
 
 
444
 
BOOST_FIXTURE_TEST_CASE(from_julian_day_number, DateTest)
445
 
{
446
 
  int64_t julian_day= 2451544;
447
 
  uint32_t years, months, days;
448
 
   
449
 
  date.from_julian_day_number(julian_day);
450
 
  
451
 
  years= date.years();
452
 
  months= date.months();
453
 
  days= date.days();
454
 
    
455
 
  BOOST_REQUIRE_EQUAL(1999, years);
456
 
  BOOST_REQUIRE_EQUAL(12, months);
457
 
  BOOST_REQUIRE_EQUAL(31, days);
458
 
}
459
 
 
460
 
BOOST_FIXTURE_TEST_CASE(to_tm, DateTest)
461
 
{
462
 
  uint32_t years= 2030, months= 8, days= 17;
463
 
  TemporalGenerator::DateGen::make_date(&date, years, months, days);
464
 
  struct tm filled;
465
 
  
466
 
  date.to_tm(&filled);
467
 
  
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);
474
 
 
475
 
}
476
 
 
477
 
BOOST_FIXTURE_TEST_CASE(from_tm, DateTest)
478
 
{
479
 
  uint32_t years, months, days;
480
 
  struct tm from;
481
 
  from.tm_year= 1956 - 1900;
482
 
  from.tm_mon= 2;
483
 
  from.tm_mday= 30;
484
 
  
485
 
  date.from_tm(&from);
486
 
  
487
 
  years= date.years();
488
 
  months= date.months();
489
 
  days= date.days();
490
 
  
491
 
  BOOST_REQUIRE_EQUAL(1956, years);  
492
 
  BOOST_REQUIRE_EQUAL(3, months);
493
 
  BOOST_REQUIRE_EQUAL(30, days);
494
 
}
495
 
 
496
 
BOOST_FIXTURE_TEST_CASE(to_time_t, DateTest)
497
 
{
498
 
  time_t time;
499
 
  TemporalGenerator::DateGen::make_date(&date, 1990, 9, 9);
500
 
  
501
 
  date.to_time_t(time);
502
 
  
503
 
  BOOST_REQUIRE_EQUAL(652838400, time);
504
 
}
505
 
 
506
 
BOOST_FIXTURE_TEST_CASE(from_time_t, DateTest)
507
 
{
508
 
  uint32_t years, months, days;
509
 
  
510
 
  date.from_time_t(652838400);
511
 
  
512
 
  years= date.years();
513
 
  months= date.months();
514
 
  days= date.days();
515
 
  
516
 
  BOOST_REQUIRE_EQUAL(1990, years);  
517
 
  BOOST_REQUIRE_EQUAL(9, months);
518
 
  BOOST_REQUIRE_EQUAL(9, days);
519
 
}
520
 
 
521
 
BOOST_FIXTURE_TEST_CASE(to_decimal, DateTest)
522
 
{
523
 
  drizzled::type::Decimal to;
524
 
  TemporalGenerator::DateGen::make_date(&date, 1987, 5, 6);
525
 
 
526
 
  date.to_decimal(&to);
527
 
 
528
 
  BOOST_REQUIRE_EQUAL(19870506, to.buf[0]);
529
 
}
530
 
BOOST_AUTO_TEST_SUITE_END()
531
 
 
532
 
BOOST_AUTO_TEST_SUITE(DateStringTest)
533
 
BOOST_AUTO_TEST_CASE(DateFromStringTest)
534
 
{
535
 
  Date date;
536
 
  const char *valid_strings[]= {"20100607", /* YYYYMMDD */
537
 
                               "06/07/2010",/* MM[-/.]DD[-/.]YYYY (US common format)*/
538
 
                               "10.06.07",/* YY[-/.]MM[-/.]DD */
539
 
                               "10/6/7",/* YY[-/.][M]M[-/.][D]D */
540
 
                               "2010-6-7"/* YYYY[-/.][M]M[-/.][D]D */};
541
 
 
542
 
  init_temporal_formats();
543
 
  for (int it= 0; it < 5; it++)
544
 
  {
545
 
    const char *valid_string= valid_strings[it];
546
 
    bool result= date.from_string(valid_string, strlen(valid_string));
547
 
    BOOST_REQUIRE(result);
548
 
    
549
 
    BOOST_REQUIRE_EQUAL(2010, date.years());
550
 
    BOOST_REQUIRE_EQUAL(6, date.months());
551
 
    BOOST_REQUIRE_EQUAL(7, date.days());
552
 
  }
553
 
  deinit_temporal_formats();
554
 
}
555
 
BOOST_AUTO_TEST_SUITE_END()