~xnox/ubuntu/saucy/drizzle/merge

« back to all changes in this revision

Viewing changes to .pc/debian-changes-2010.12.06-0ubuntu4/unittests/date_time_test.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2011-01-01 13:55:03 UTC
  • Revision ID: james.westby@ubuntu.com-20110101135503-x2ub1akxoisgwi6z
Tags: 2010.12.06-0ubuntu4
* Fixed missing build depends.
* Added Lee to uploaders.

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
#include <gtest/gtest.h>
 
24
#include <drizzled/decimal.h>
 
25
#include <drizzled/temporal.h>
 
26
#include <drizzled/temporal_format.h>
 
27
 
 
28
#include "temporal_generator.h"
 
29
 
 
30
using namespace drizzled;
 
31
 
 
32
class DateTimeTest: public ::testing::Test
 
33
{
 
34
  protected:
 
35
    DateTime datetime;
 
36
    bool result;
 
37
    uint32_t years, months, days;
 
38
    uint32_t hours, minutes, seconds;
 
39
    
 
40
    virtual void SetUp()
 
41
    {
 
42
      TemporalGenerator::DateTimeGen::make_valid_datetime(&datetime);
 
43
    }
 
44
 
 
45
    void assignDateTimeValues()
 
46
    {
 
47
      years= datetime.years();
 
48
      months= datetime.months();
 
49
      days= datetime.days();
 
50
      hours= datetime.hours();
 
51
      minutes= datetime.minutes();
 
52
      seconds= datetime.seconds();
 
53
    }
 
54
};
 
55
 
 
56
TEST_F(DateTimeTest, is_valid_onValidDateTime_shouldReturn_True)
 
57
{
 
58
  result= datetime.is_valid();
 
59
  ASSERT_TRUE(result);
 
60
}
 
61
 
 
62
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithYearBelowMinimum_shouldReturn_False)
 
63
{
 
64
  datetime.set_years(DRIZZLE_MIN_YEARS_SQL - 1);
 
65
  
 
66
  result= datetime.is_valid();
 
67
  
 
68
  ASSERT_FALSE(result);
 
69
}
 
70
 
 
71
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithYearAboveMaximum_shouldReturn_False)
 
72
{
 
73
  datetime.set_years(DRIZZLE_MAX_YEARS_SQL + 1);
 
74
    
 
75
  result= datetime.is_valid();
 
76
  
 
77
  ASSERT_FALSE(result);
 
78
}
 
79
 
 
80
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMonthSetToZero_shouldReturn_False)
 
81
{
 
82
  datetime.set_months(0);
 
83
  
 
84
  result= datetime.is_valid();
 
85
  
 
86
  ASSERT_FALSE(result);
 
87
}
 
88
 
 
89
 
 
90
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMonthAboveMaximum_shouldReturn_False)
 
91
{
 
92
  datetime.set_months(13);
 
93
  
 
94
  result= datetime.is_valid();
 
95
  
 
96
  ASSERT_FALSE(result);
 
97
}
 
98
 
 
99
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithDaySetToZero_shouldReturn_False)
 
100
{
 
101
  datetime.set_days(0);
 
102
  
 
103
  result= datetime.is_valid();
 
104
  
 
105
  ASSERT_FALSE(result);
 
106
}
 
107
 
 
108
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithDayAboveDaysInMonth_shouldReturn_False)
 
109
{
 
110
  datetime.set_days(32);
 
111
  
 
112
  result= datetime.is_valid();
 
113
  
 
114
  ASSERT_FALSE(result);
 
115
}
 
116
 
 
117
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithLeapDayInNonLeapYear_shouldReturn_False)
 
118
{
 
119
  TemporalGenerator::TemporalGen::leap_day_in_non_leap_year(&datetime);
 
120
  
 
121
  result= datetime.is_valid();
 
122
  
 
123
  ASSERT_FALSE(result);
 
124
}
 
125
 
 
126
TEST_F(DateTimeTest, is_valid_onValidDateTimeWithLeapDayInLeapYear_shouldReturn_True)
 
127
{
 
128
  TemporalGenerator::TemporalGen::leap_day_in_leap_year(&datetime);
 
129
  
 
130
  result= datetime.is_valid();
 
131
  
 
132
  ASSERT_TRUE(result);
 
133
}
 
134
 
 
135
TEST_F(DateTimeTest, is_valid_onValidMinimalTime_shouldReturn_True)
 
136
{
 
137
  TemporalGenerator::TemporalGen::make_min_time(&datetime);
 
138
  
 
139
  result= datetime.is_valid();
 
140
  
 
141
  ASSERT_TRUE(result);
 
142
}
 
143
 
 
144
TEST_F(DateTimeTest, is_valid_onValidMaximalTime_shouldReturn_True)
 
145
{
 
146
  TemporalGenerator::TemporalGen::make_max_time(&datetime);
 
147
  
 
148
  result= datetime.is_valid();
 
149
  
 
150
  ASSERT_TRUE(result);
 
151
}
 
152
 
 
153
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithHourAboveMaximum23_shouldReturn_False)
 
154
{
 
155
  datetime.set_hours(24);
 
156
  
 
157
  result= datetime.is_valid();
 
158
  
 
159
  ASSERT_FALSE(result);
 
160
}
 
161
 
 
162
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithMinutesAboveMaximum59_shouldReturn_False)
 
163
{
 
164
  datetime.set_minutes(60);
 
165
  
 
166
  result= datetime.is_valid();
 
167
  
 
168
  ASSERT_FALSE(result);
 
169
}
 
170
 
 
171
TEST_F(DateTimeTest, is_valid_onInvalidDateTimeWithSecondsAboveMaximum61_shouldReturn_False)
 
172
{
 
173
  datetime.set_seconds(62);
 
174
  
 
175
  result= datetime.is_valid();
 
176
  
 
177
  ASSERT_FALSE(result);
 
178
}
 
179
 
 
180
TEST_F(DateTimeTest, to_string_shouldProduce_hyphenSeperatedDateElements_and_colonSeperatedTimeElements)
 
181
{
 
182
  char expected[DateTime::MAX_STRING_LENGTH]= "2010-05-01 08:07:06.123456";
 
183
  char returned[DateTime::MAX_STRING_LENGTH];
 
184
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, 2010, 5, 1, 8, 7, 6, 123456);
 
185
  
 
186
  datetime.to_string(returned, DateTime::MAX_STRING_LENGTH);
 
187
  
 
188
  ASSERT_STREQ(expected, returned);
 
189
}
 
190
 
 
191
TEST_F(DateTimeTest, to_string_nullBuffer_noMicroSeconds_shouldReturnProperLengthAnyway)
 
192
{
 
193
  int length= datetime.to_string(NULL, 0);
 
194
  
 
195
  ASSERT_EQ(DateTime::MAX_STRING_LENGTH - 1 - 7, length);  
 
196
}
 
197
 
 
198
TEST_F(DateTimeTest, to_int64_t)
 
199
{
 
200
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, 2030, 8, 7, 14, 5, 13);
 
201
  int64_t representation;
 
202
 
 
203
  datetime.to_int64_t(&representation);
 
204
 
 
205
  ASSERT_EQ(20300807140513LL, representation);
 
206
}
 
207
 
 
208
TEST_F(DateTimeTest, from_int64_t_no_conversion_format_YYYYMMDDHHMMSSshouldPopulateDateTimeCorrectly)
 
209
{
 
210
  datetime.from_int64_t(20300807140513LL, false);
 
211
  
 
212
  assignDateTimeValues();
 
213
  
 
214
  EXPECT_EQ(2030, years);
 
215
  EXPECT_EQ(8, months);
 
216
  EXPECT_EQ(7, days);
 
217
  EXPECT_EQ(14, hours);
 
218
  EXPECT_EQ(5, minutes);
 
219
  EXPECT_EQ(13, seconds);
 
220
}
 
221
 
 
222
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYYYMMDDHHMMSS_yearOver2000)
 
223
{
 
224
  datetime.from_int64_t(20300807140513LL, true);
 
225
  
 
226
  assignDateTimeValues();
 
227
  
 
228
  EXPECT_EQ(2030, years);
 
229
  EXPECT_EQ(8, months);
 
230
  EXPECT_EQ(7, days);
 
231
  EXPECT_EQ(14, hours);
 
232
  EXPECT_EQ(5, minutes);
 
233
  EXPECT_EQ(13, seconds);
 
234
}
 
235
 
 
236
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYYYMMDDHHMMSS_yearBelow2000)
 
237
{
 
238
  datetime.from_int64_t(19900807140513LL, true);
 
239
  
 
240
  assignDateTimeValues();
 
241
  
 
242
  EXPECT_EQ(1990, years);
 
243
  EXPECT_EQ(8, months);
 
244
  EXPECT_EQ(7, days);
 
245
  EXPECT_EQ(14, hours);
 
246
  EXPECT_EQ(5, minutes);
 
247
  EXPECT_EQ(13, seconds);
 
248
}
 
249
 
 
250
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYMMDDHHMMSS_yearOver2000)
 
251
{
 
252
  datetime.from_int64_t(300807140513LL, true);
 
253
  
 
254
  assignDateTimeValues();
 
255
  
 
256
  EXPECT_EQ(2030, years);
 
257
  EXPECT_EQ(8, months);
 
258
  EXPECT_EQ(7, days);
 
259
  EXPECT_EQ(14, hours);
 
260
  EXPECT_EQ(5, minutes);
 
261
  EXPECT_EQ(13, seconds);
 
262
}
 
263
 
 
264
TEST_F(DateTimeTest, from_int64_t_with_conversion_format_YYMMDDHHMMSS_yearBelow2000)
 
265
{
 
266
  datetime.from_int64_t(900807140513LL, true);
 
267
  
 
268
  assignDateTimeValues();
 
269
  
 
270
  EXPECT_EQ(1990, years);
 
271
  EXPECT_EQ(8, months);
 
272
  EXPECT_EQ(7, days);
 
273
  EXPECT_EQ(14, hours);
 
274
  EXPECT_EQ(5, minutes);
 
275
  EXPECT_EQ(13, seconds);
 
276
}
 
277
 
 
278
TEST_F(DateTimeTest, DISABLED_to_tm)
 
279
{
 
280
  years= 2030, months= 8, days= 17, hours= 14, minutes= 45, seconds= 13;
 
281
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, years, months, days, hours, minutes, seconds);
 
282
  struct tm filled;
 
283
  
 
284
  datetime.to_tm(&filled);
 
285
  
 
286
  EXPECT_EQ(2030 - 1900, filled.tm_year);
 
287
  EXPECT_EQ(8 - 1, filled.tm_mon);
 
288
  EXPECT_EQ(17, filled.tm_mday);
 
289
  EXPECT_EQ(14, filled.tm_hour);
 
290
  EXPECT_EQ(45, filled.tm_min);
 
291
  EXPECT_EQ(13, filled.tm_sec);
 
292
 
 
293
  /* TODO:these fail, shouldn't they also be set properly? */
 
294
  EXPECT_EQ(228, filled.tm_yday);
 
295
  EXPECT_EQ(6, filled.tm_wday);
 
296
  EXPECT_EQ(-1, filled.tm_isdst);
 
297
}
 
298
 
 
299
TEST_F(DateTimeTest, to_decimal)
 
300
{
 
301
  drizzled::my_decimal to;
 
302
  TemporalGenerator::DateTimeGen::make_datetime(&datetime, 1987, 6, 13, 5, 10, 13, 456);
 
303
 
 
304
  datetime.to_decimal(&to);
 
305
  
 
306
  EXPECT_EQ(19870,to.buf[0]);
 
307
  EXPECT_EQ(613051013,to.buf[1]);
 
308
  EXPECT_EQ(456000,to.buf[2]);
 
309
}
 
310
 
 
311
 
 
312
 
 
313
class DateTimeFromStringTest
 
314
{
 
315
  protected:
 
316
    DateTime datetime;
 
317
    bool result;
 
318
    uint32_t years, months, days;
 
319
    uint32_t hours, minutes, seconds;
 
320
 
 
321
    void init()
 
322
    {
 
323
      init_temporal_formats();
 
324
    }
 
325
 
 
326
    void deinit()
 
327
    {
 
328
      deinit_temporal_formats();
 
329
    }
 
330
 
 
331
    void assignDateTimeValues()
 
332
    {
 
333
      years= datetime.years();
 
334
      months= datetime.months();
 
335
      days= datetime.days();
 
336
      hours= datetime.hours();
 
337
      minutes= datetime.minutes();
 
338
      seconds= datetime.seconds();
 
339
    }
 
340
};
 
341
 
 
342
class DateTimeFromStringFullFormatTest: public ::testing::TestWithParam<const char*>, public DateTimeFromStringTest
 
343
{
 
344
  virtual void SetUp()
 
345
  {
 
346
    init();
 
347
  }
 
348
  
 
349
  virtual void TearDown()
 
350
  {
 
351
    deinit();
 
352
  }  
 
353
};
 
354
 
 
355
class DateTimeFromStringNoSecondFormatTest: public ::testing::TestWithParam<const char*>, public DateTimeFromStringTest
 
356
{
 
357
  virtual void SetUp()
 
358
  {
 
359
    init();
 
360
  }
 
361
  
 
362
  virtual void TearDown()
 
363
  {
 
364
    deinit();
 
365
  }
 
366
};
 
367
 
 
368
class DateTimeFromStringDateOnlyTest: public ::testing::TestWithParam<const char*>, public DateTimeFromStringTest
 
369
{
 
370
  virtual void SetUp()
 
371
  {
 
372
    init();
 
373
  }
 
374
  
 
375
  virtual void TearDown()
 
376
  {
 
377
    deinit();
 
378
  }
 
379
};
 
380
 
 
381
TEST_P(DateTimeFromStringFullFormatTest, from_string_validString)
 
382
{
 
383
  const char *valid_string= GetParam();
 
384
 
 
385
  result= datetime.from_string(valid_string, strlen(valid_string));
 
386
  ASSERT_TRUE(result);
 
387
 
 
388
  assignDateTimeValues();
 
389
 
 
390
  EXPECT_EQ(2010, years);
 
391
  EXPECT_EQ(5, months);
 
392
  EXPECT_EQ(1, days);
 
393
  EXPECT_EQ(8, hours);
 
394
  EXPECT_EQ(7, minutes);
 
395
  EXPECT_EQ(6, seconds);
 
396
}
 
397
/* TODO:for some reason this was not declared by the macro, needs clarification*/
 
398
testing::internal::ParamGenerator<const char*> gtest_ValidStringDateTimeFromStringFullFormatTest_EvalGenerator_();
 
399
INSTANTIATE_TEST_CASE_P(ValidString, DateTimeFromStringFullFormatTest,
 
400
                        ::testing::Values("20100501080706",
 
401
                                          "2010-05-01 08:07:06",
 
402
                                          "2010/05/01T08:07:06",
 
403
                                          "2010.5.1 08:07:06",
 
404
                                          "10-05-01 08:07:06",
 
405
                                          "10/5/1 08:07:06",
 
406
                                          "10.5.1 08:07:06"));
 
407
 
 
408
                                          
 
409
TEST_P(DateTimeFromStringNoSecondFormatTest, from_string_validString)
 
410
{
 
411
  const char *valid_string= GetParam();
 
412
 
 
413
  result= datetime.from_string(valid_string, strlen(valid_string));
 
414
  ASSERT_TRUE(result);
 
415
 
 
416
  assignDateTimeValues();
 
417
 
 
418
  EXPECT_EQ(2010, years);
 
419
  EXPECT_EQ(5, months);
 
420
  EXPECT_EQ(1, days);
 
421
  EXPECT_EQ(8, hours);
 
422
  EXPECT_EQ(7, minutes);
 
423
}
 
424
 
 
425
/* TODO:for some reason this was not declared by the macro, needs clarification*/
 
426
testing::internal::ParamGenerator<const char*> gtest_ValidStringDateTimeFromStringNoSecondFormatTest_EvalGenerator_();
 
427
INSTANTIATE_TEST_CASE_P(ValidString, DateTimeFromStringNoSecondFormatTest,
 
428
                        ::testing::Values("2010-05-01 08:07",
 
429
                                          "2010/05/01 08:07",
 
430
                                          "2010.5.1 08:07",
 
431
                                          "10-05-01 08:07",
 
432
                                          "10/5/1 08:07",
 
433
                                          "10.5.1 08:07"));
 
434
 
 
435
 
 
436
 
 
437
TEST_P(DateTimeFromStringDateOnlyTest, from_string_validString)
 
438
{
 
439
  const char *valid_string= GetParam();
 
440
 
 
441
  result= datetime.from_string(valid_string, strlen(valid_string));
 
442
  ASSERT_TRUE(result);
 
443
 
 
444
  assignDateTimeValues();
 
445
 
 
446
  EXPECT_EQ(2010, years);
 
447
  EXPECT_EQ(6, months);
 
448
  EXPECT_EQ(7, days);
 
449
}
 
450
 
 
451
/* TODO:for some reason this was not declared by the macro, needs clarification*/
 
452
testing::internal::ParamGenerator<const char*> gtest_ValidStringDateTimeFromStringDateOnlyTest_EvalGenerator_();
 
453
INSTANTIATE_TEST_CASE_P(ValidString, DateTimeFromStringDateOnlyTest,
 
454
                        ::testing::Values("20100607", /* YYYYMMDD */
 
455
                                          "06/07/2010",/* MM[-/.]DD[-/.]YYYY (US common format)*/
 
456
                                          "10.06.07",/* YY[-/.]MM[-/.]DD */
 
457
                                          "10/6/7",/* YY[-/.][M]M[-/.][D]D */
 
458
                                          "2010-6-7"/* YYYY[-/.][M]M[-/.][D]D */));
 
459
 
 
460
                                          
 
 
b'\\ No newline at end of file'