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
#include <gtest/gtest.h>
24
#include <drizzled/decimal.h>
25
#include <drizzled/temporal.h>
26
#include <drizzled/temporal_format.h>
28
#include "temporal_generator.h"
30
using namespace drizzled;
32
class TimeTest: public ::testing::Test
37
uint32_t hours, minutes, seconds;
39
Time identical_with_sample_time, before_sample_time, after_sample_time;
43
TemporalGenerator::TimeGen::make_time(&sample_time, 18, 34, 59);
45
TemporalGenerator::TimeGen::make_time(&before_sample_time, 18, 34, 58);
46
TemporalGenerator::TimeGen::make_time(&identical_with_sample_time, 18, 34, 59);
47
TemporalGenerator::TimeGen::make_time(&after_sample_time, 18, 35, 0);
51
void assign_time_values()
53
hours= sample_time.hours();
54
minutes= sample_time.minutes();
55
seconds= sample_time.seconds();
58
void from_string(const char *string)
61
init_temporal_formats();
62
result= sample_time.from_string(string, strlen(string));
63
deinit_temporal_formats();
69
TEST_F(TimeTest, operatorEqual_ComparingWithIdencticalTime_ShouldReturn_True)
71
this->result= (this->sample_time == this->identical_with_sample_time);
73
ASSERT_TRUE(this->result);
76
TEST_F(TimeTest, operatorEqual_ComparingWithDifferentTemporal_ShouldReturn_False)
78
this->result= (this->sample_time == this->before_sample_time);
80
ASSERT_FALSE(this->result);
83
TEST_F(TimeTest, operatorNotEqual_ComparingWithIdencticalTemporal_ShouldReturn_False)
85
this->result= (this->sample_time != this->identical_with_sample_time);
87
ASSERT_FALSE(this->result);
90
TEST_F(TimeTest, operatorNotEqual_ComparingWithDifferentTemporal_ShouldReturn_True)
92
this->result= (this->sample_time != this->before_sample_time);
94
ASSERT_TRUE(this->result);
97
TEST_F(TimeTest, operatorGreaterThan_ComparingWithIdenticalTemporal_ShouldReturn_False)
99
this->result= (this->sample_time > this->identical_with_sample_time);
101
ASSERT_FALSE(this->result);
104
TEST_F(TimeTest, operatorGreaterThan_ComparingWithLaterTemporal_ShouldReturn_False)
106
this->result= (this->sample_time > this->after_sample_time);
108
ASSERT_FALSE(this->result);
111
TEST_F(TimeTest, operatorGreaterThan_ComparingWithEarlierTemporal_ShouldReturn_True)
113
this->result= (this->sample_time > this->before_sample_time);
115
ASSERT_TRUE(this->result);
118
TEST_F(TimeTest, operatorGreaterThanOrEqual_ComparingWithIdenticalTemporal_ShouldReturn_True)
120
this->result= (this->sample_time >= this->identical_with_sample_time);
122
ASSERT_TRUE(this->result);
125
TEST_F(TimeTest, operatorGreaterThanOrEqual_ComparingWithLaterTemporal_ShouldReturn_False)
127
this->result= (this->sample_time >= this->after_sample_time);
129
ASSERT_FALSE(this->result);
132
TEST_F(TimeTest, operatorGreaterThanOrEqual_ComparingWithEarlierTemporal_ShouldReturn_True)
134
this->result= (this->sample_time >= this->before_sample_time);
136
ASSERT_TRUE(this->result);
139
TEST_F(TimeTest, operatorLessThan_ComparingWithIdenticalTemporal_ShouldReturn_False)
141
this->result= (this->sample_time < this->identical_with_sample_time);
143
ASSERT_FALSE(this->result);
146
TEST_F(TimeTest, operatorLessThan_ComparingWithLaterTemporal_ShouldReturn_True)
148
this->result= (this->sample_time < this->after_sample_time);
150
ASSERT_TRUE(this->result);
153
TEST_F(TimeTest, operatorLessThan_ComparingWithEarlierTemporal_ShouldReturn_False)
155
this->result= (this->sample_time < this->before_sample_time);
157
ASSERT_FALSE(this->result);
160
TEST_F(TimeTest, operatorLessThanOrEqual_ComparingWithIdenticalTemporal_ShouldReturn_True)
162
this->result= (this->sample_time <= this->identical_with_sample_time);
164
ASSERT_TRUE(this->result);
167
TEST_F(TimeTest, operatorLessThanOrEqual_ComparingWithLaterTemporal_ShouldReturn_True)
169
this->result= (this->sample_time <= this->after_sample_time);
171
ASSERT_TRUE(this->result);
174
TEST_F(TimeTest, operatorLessThanOrEqual_ComparingWithEarlierTemporal_ShouldReturn_False)
176
this->result= (this->sample_time <= this->before_sample_time);
178
ASSERT_FALSE(this->result);
181
TEST_F(TimeTest, is_valid_onValidTime_shouldReturn_True)
183
result= sample_time.is_valid();
188
TEST_F(TimeTest, is_valid_onValidMinimalTime_shouldReturn_True)
190
TemporalGenerator::TemporalGen::make_min_time(&sample_time);
192
result= sample_time.is_valid();
197
TEST_F(TimeTest, is_valid_onValidMaximalTime_shouldReturn_True)
199
TemporalGenerator::TemporalGen::make_max_time(&sample_time);
201
result= sample_time.is_valid();
206
TEST_F(TimeTest, is_valid_onInvalidTimeWithHourAboveMaximum23_shouldReturn_False)
208
sample_time.set_hours(24);
210
result= sample_time.is_valid();
212
ASSERT_FALSE(result);
215
TEST_F(TimeTest, is_valid_onInvalidTimeWithMinutesAboveMaximum59_shouldReturn_False)
217
sample_time.set_minutes(60);
219
result= sample_time.is_valid();
221
ASSERT_FALSE(result);
224
TEST_F(TimeTest, is_valid_onInvalidTimeWithSecondsAboveMaximum59_shouldReturn_False)
226
sample_time.set_seconds(60);
228
result= sample_time.is_valid();
230
ASSERT_FALSE(result);
233
TEST_F(TimeTest, to_string_shouldProduce_colonSeperatedTimeElements)
235
char expected[Time::MAX_STRING_LENGTH]= "18:34:59";
236
char returned[Time::MAX_STRING_LENGTH];
238
sample_time.to_string(returned, Time::MAX_STRING_LENGTH);
240
ASSERT_STREQ(expected, returned);
243
TEST_F(TimeTest, to_string_nullBuffer_shouldReturnProperLengthAnyway)
245
int length= sample_time.to_string(NULL, 0);
247
ASSERT_EQ(Time::MAX_STRING_LENGTH - 1, length);
250
TEST_F(TimeTest, to_int32_t)
252
int32_t representation;
254
sample_time.to_int32_t(&representation);
256
ASSERT_EQ(representation, 183459);
259
TEST_F(TimeTest, from_int32_t_shouldPopulateTimeCorrectly)
261
sample_time.from_int32_t(183459);
263
assign_time_values();;
265
EXPECT_EQ(18, hours);
266
EXPECT_EQ(34, minutes);
267
EXPECT_EQ(59, seconds);
270
TEST_F(TimeTest, from_time_t)
272
sample_time.from_time_t(59588);
274
assign_time_values();
276
EXPECT_EQ(16, hours);
277
EXPECT_EQ(33, minutes);
278
EXPECT_EQ(8, seconds);
281
TEST_F(TimeTest, to_decimal)
283
drizzled::my_decimal to;
284
TemporalGenerator::TimeGen::make_time(&sample_time, 8, 4, 9, 56);
286
sample_time.to_decimal(&to);
288
ASSERT_EQ(80409, to.buf[0]);
289
ASSERT_EQ(56000, to.buf[1]);
292
TEST_F(TimeTest, from_string_invalidString_shouldReturn_False)
294
char invalid_string[Time::MAX_STRING_LENGTH]= "1o:34:59";
296
init_temporal_formats();
297
result= sample_time.from_string(invalid_string, strlen(invalid_string));
298
deinit_temporal_formats();
300
ASSERT_FALSE(result);
303
TEST_F(TimeTest, from_string_validString_minuteAndSecond_shouldPopulateCorrectly)
305
char valid_string[Time::MAX_STRING_LENGTH]= "4:52";
307
from_string(valid_string);
309
EXPECT_EQ(4, minutes);
310
EXPECT_EQ(52, seconds);
313
TEST_F(TimeTest, from_string_validString_minuteAndSecondNoColon_shouldPopulateCorrectly)
315
char valid_string[Time::MAX_STRING_LENGTH]= "3456";
317
from_string(valid_string);
319
EXPECT_EQ(34, minutes);
320
EXPECT_EQ(56, seconds);
323
TEST_F(TimeTest, from_string_validString_secondsOnly_shouldPopulateCorrectly)
325
char valid_string[Time::MAX_STRING_LENGTH]= "59";
327
from_string(valid_string);
329
EXPECT_EQ(59, seconds);
332
class TimeFromStringTest: public ::testing::TestWithParam<const char*>
337
uint32_t hours, minutes, seconds;
341
init_temporal_formats();
344
virtual void TearDown()
346
deinit_temporal_formats();
349
void assign_time_values()
352
minutes= time.minutes();
353
seconds= time.seconds();
357
TEST_P(TimeFromStringTest, from_string)
359
const char *valid_string= GetParam();
361
result= time.from_string(valid_string, strlen(valid_string));
364
assign_time_values();
367
EXPECT_EQ(4, minutes);
368
EXPECT_EQ(9, seconds);
371
/* TODO:for some reason this was not declared by the macro, needs clarification*/
372
testing::internal::ParamGenerator<const char*> gtest_ValidStringTimeFromStringTest_EvalGenerator_();
374
INSTANTIATE_TEST_CASE_P(ValidString, TimeFromStringTest,
375
::testing::Values("080409",
b'\\ No newline at end of file'