~drizzle-trunk/drizzle/development

2097.2.4 by Andrew Hutchings
Convert all unit tests to boost::test
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
2097.2.4 by Andrew Hutchings
Convert all unit tests to boost::test
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 TimeTest
35
{
36
protected:
37
  Time sample_time;
38
  bool result;
39
  uint32_t hours, minutes, seconds;
40
  
41
  Time identical_with_sample_time, before_sample_time, after_sample_time;
42
  
43
  TimeTest()
44
  {
45
    TemporalGenerator::TimeGen::make_time(&sample_time, 18, 34, 59);
46
    
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);
50
    
51
  }
52
53
  void assign_time_values()
54
  {
55
    hours= sample_time.hours();
56
    minutes= sample_time.minutes();
57
    seconds= sample_time.seconds();
58
  }
59
60
  void from_string(const char *string)
61
  {
62
    
63
    init_temporal_formats();
64
    result= sample_time.from_string(string, strlen(string));
65
    deinit_temporal_formats();
66
    
67
    assign_time_values();
68
  }
69
};
70
71
BOOST_FIXTURE_TEST_SUITE(TimeTestSuite, TimeTest)
72
BOOST_AUTO_TEST_CASE(operatorEqual_ComparingWithIdencticalTime_ShouldReturn_True)
73
{
74
  this->result= (this->sample_time == this->identical_with_sample_time);
75
  
76
  BOOST_REQUIRE(this->result);
77
}
78
79
BOOST_AUTO_TEST_CASE(operatorEqual_ComparingWithDifferentTemporal_ShouldReturn_False)
80
{
81
  this->result= (this->sample_time == this->before_sample_time);
82
  
83
  BOOST_REQUIRE(not this->result);
84
}
85
86
BOOST_AUTO_TEST_CASE(operatorNotEqual_ComparingWithIdencticalTemporal_ShouldReturn_False)
87
{ 
88
  this->result= (this->sample_time != this->identical_with_sample_time);
89
  
90
  BOOST_REQUIRE(not this->result);
91
}
92
93
BOOST_AUTO_TEST_CASE(operatorNotEqual_ComparingWithDifferentTemporal_ShouldReturn_True)
94
{
95
  this->result= (this->sample_time != this->before_sample_time);
96
  
97
  BOOST_REQUIRE(this->result);
98
}
99
100
BOOST_AUTO_TEST_CASE(operatorGreaterThan_ComparingWithIdenticalTemporal_ShouldReturn_False)
101
{
102
  this->result= (this->sample_time > this->identical_with_sample_time);
103
  
104
  BOOST_REQUIRE(not this->result);
105
}
106
107
BOOST_AUTO_TEST_CASE(operatorGreaterThan_ComparingWithLaterTemporal_ShouldReturn_False)
108
{
109
  this->result= (this->sample_time > this->after_sample_time);
110
  
111
  BOOST_REQUIRE(not this->result);
112
}
113
114
BOOST_AUTO_TEST_CASE(operatorGreaterThan_ComparingWithEarlierTemporal_ShouldReturn_True)
115
{
116
  this->result= (this->sample_time > this->before_sample_time);
117
  
118
  BOOST_REQUIRE(this->result);
119
}
120
121
BOOST_AUTO_TEST_CASE(operatorGreaterThanOrEqual_ComparingWithIdenticalTemporal_ShouldReturn_True)
122
{
123
  this->result= (this->sample_time >= this->identical_with_sample_time);
124
  
125
  BOOST_REQUIRE(this->result);
126
}
127
128
BOOST_AUTO_TEST_CASE(operatorGreaterThanOrEqual_ComparingWithLaterTemporal_ShouldReturn_False)
129
{
130
  this->result= (this->sample_time >= this->after_sample_time);
131
  
132
  BOOST_REQUIRE(not this->result);
133
}
134
135
BOOST_AUTO_TEST_CASE(operatorGreaterThanOrEqual_ComparingWithEarlierTemporal_ShouldReturn_True)
136
{
137
  this->result= (this->sample_time >= this->before_sample_time);
138
  
139
  BOOST_REQUIRE(this->result);
140
}
141
142
BOOST_AUTO_TEST_CASE(operatorLessThan_ComparingWithIdenticalTemporal_ShouldReturn_False)
143
{
144
  this->result= (this->sample_time < this->identical_with_sample_time);
145
  
146
  BOOST_REQUIRE(not this->result);
147
}
148
149
BOOST_AUTO_TEST_CASE(operatorLessThan_ComparingWithLaterTemporal_ShouldReturn_True)
150
{
151
  this->result= (this->sample_time < this->after_sample_time);
152
  
153
  BOOST_REQUIRE(this->result);
154
}
155
156
BOOST_AUTO_TEST_CASE(operatorLessThan_ComparingWithEarlierTemporal_ShouldReturn_False)
157
{
158
  this->result= (this->sample_time < this->before_sample_time);
159
  
160
  BOOST_REQUIRE(not this->result);
161
}
162
163
BOOST_AUTO_TEST_CASE(operatorLessThanOrEqual_ComparingWithIdenticalTemporal_ShouldReturn_True)
164
{
165
  this->result= (this->sample_time <= this->identical_with_sample_time);
166
  
167
  BOOST_REQUIRE(this->result);
168
}
169
170
BOOST_AUTO_TEST_CASE(operatorLessThanOrEqual_ComparingWithLaterTemporal_ShouldReturn_True)
171
{
172
  this->result= (this->sample_time <= this->after_sample_time);
173
  
174
  BOOST_REQUIRE(this->result);
175
}
176
177
BOOST_AUTO_TEST_CASE(operatorLessThanOrEqual_ComparingWithEarlierTemporal_ShouldReturn_False)
178
{
179
  this->result= (this->sample_time <= this->before_sample_time);
180
  
181
  BOOST_REQUIRE(not this->result);
182
}
183
184
BOOST_AUTO_TEST_CASE(is_valid_onValidTime_shouldReturn_True)
185
{
186
  result= sample_time.is_valid();
187
  
188
  BOOST_REQUIRE(result);
189
}
190
191
BOOST_AUTO_TEST_CASE(is_valid_onValidMinimalTime_shouldReturn_True)
192
{
193
  TemporalGenerator::TemporalGen::make_min_time(&sample_time);
194
  
195
  result= sample_time.is_valid();
196
  
197
  BOOST_REQUIRE(result);
198
}
199
200
BOOST_AUTO_TEST_CASE(is_valid_onValidMaximalTime_shouldReturn_True)
201
{
202
  TemporalGenerator::TemporalGen::make_max_time(&sample_time);
203
  
204
  result= sample_time.is_valid();
205
  
206
  BOOST_REQUIRE(result);
207
}
208
209
BOOST_AUTO_TEST_CASE(is_valid_onInvalidTimeWithHourAboveMaximum23_shouldReturn_False)
210
{
211
  sample_time.set_hours(24);
212
  
213
  result= sample_time.is_valid();
214
  
215
  BOOST_REQUIRE(not result);
216
}
217
218
BOOST_AUTO_TEST_CASE(is_valid_onInvalidTimeWithMinutesAboveMaximum59_shouldReturn_False)
219
{
220
  sample_time.set_minutes(60);
221
  
222
  result= sample_time.is_valid();
223
  
224
  BOOST_REQUIRE(not result);
225
}
226
227
BOOST_AUTO_TEST_CASE(is_valid_onInvalidTimeWithSecondsAboveMaximum59_shouldReturn_False)
228
{
229
  sample_time.set_seconds(60);
230
  
231
  result= sample_time.is_valid();
232
  
233
  BOOST_REQUIRE(not result);
234
}
235
236
BOOST_AUTO_TEST_CASE(to_string_shouldProduce_colonSeperatedTimeElements)
237
{
238
  char expected[Time::MAX_STRING_LENGTH]= "18:34:59";
239
  char returned[Time::MAX_STRING_LENGTH];
240
  
241
  sample_time.to_string(returned, Time::MAX_STRING_LENGTH);
242
  
243
  BOOST_REQUIRE_EQUAL(expected, returned);  
244
}
245
246
BOOST_AUTO_TEST_CASE(to_string_nullBuffer_shouldReturnProperLengthAnyway)
247
{
248
  int length= sample_time.to_string(NULL, 0);
249
  
250
  BOOST_REQUIRE_EQUAL(Time::MAX_STRING_LENGTH - 1, length);  
251
}
252
253
BOOST_AUTO_TEST_CASE(to_int32_t)
254
{
255
  int32_t representation;
256
257
  sample_time.to_int32_t(&representation);
258
259
  BOOST_REQUIRE_EQUAL(representation, 183459);
260
}
261
262
BOOST_AUTO_TEST_CASE(from_int32_t_shouldPopulateTimeCorrectly)
263
{
264
  sample_time.from_int32_t(183459);
265
  
266
  assign_time_values();;
267
  
268
  BOOST_REQUIRE_EQUAL(18, hours);
269
  BOOST_REQUIRE_EQUAL(34, minutes);
270
  BOOST_REQUIRE_EQUAL(59, seconds);
271
}
272
273
BOOST_AUTO_TEST_CASE(from_time_t)
274
{
275
  sample_time.from_time_t(59588);
276
  
277
  assign_time_values();
278
  
279
  BOOST_REQUIRE_EQUAL(16, hours);  
280
  BOOST_REQUIRE_EQUAL(33, minutes);
281
  BOOST_REQUIRE_EQUAL(8, seconds);
282
}
283
284
BOOST_AUTO_TEST_CASE(to_decimal)
285
{
286
  drizzled::type::Decimal to;
287
  TemporalGenerator::TimeGen::make_time(&sample_time, 8, 4, 9, 56);
288
289
  sample_time.to_decimal(&to);
290
  
291
  BOOST_REQUIRE_EQUAL(80409, to.buf[0]);
292
  BOOST_REQUIRE_EQUAL(56000, to.buf[1]);
293
}
294
295
BOOST_AUTO_TEST_CASE(from_string_invalidString_shouldReturn_False)
296
{
297
  char invalid_string[Time::MAX_STRING_LENGTH]= "1o:34:59";
298
  
299
  init_temporal_formats();
300
  result= sample_time.from_string(invalid_string, strlen(invalid_string));
301
  deinit_temporal_formats();
302
  
303
  BOOST_REQUIRE(not result);
304
}
305
306
BOOST_AUTO_TEST_CASE(from_string_validString_minuteAndSecond_shouldPopulateCorrectly)
307
{
308
  char valid_string[Time::MAX_STRING_LENGTH]= "4:52";
309
310
  from_string(valid_string);
311
312
  BOOST_REQUIRE_EQUAL(4, minutes);
313
  BOOST_REQUIRE_EQUAL(52, seconds);
314
}
315
316
BOOST_AUTO_TEST_CASE(from_string_validString_minuteAndSecondNoColon_shouldPopulateCorrectly)
317
{
318
  char valid_string[Time::MAX_STRING_LENGTH]= "3456";
319
  
320
  from_string(valid_string);
321
  
322
  BOOST_REQUIRE_EQUAL(34, minutes);
323
  BOOST_REQUIRE_EQUAL(56, seconds);
324
}
325
326
BOOST_AUTO_TEST_CASE(from_string_validString_secondsOnly_shouldPopulateCorrectly)
327
{
328
  char valid_string[Time::MAX_STRING_LENGTH]= "59";
329
  
330
  from_string(valid_string);
331
  
332
  BOOST_REQUIRE_EQUAL(59, seconds);
333
}
334
BOOST_AUTO_TEST_SUITE_END()
335
336
class TimeFromStringTest
337
{
338
  protected:
339
    Time time;
340
    bool result;
341
    uint32_t hours, minutes, seconds;
342
    
343
    TimeFromStringTest()
344
    {
345
      init_temporal_formats();
346
    }
347
    
348
    ~TimeFromStringTest()
349
    {
350
      deinit_temporal_formats();
351
    }
352
    
353
    void assign_time_values()
354
    {
355
      hours= time.hours();
356
      minutes= time.minutes();
357
      seconds= time.seconds();
358
    }
359
};
360
361
BOOST_FIXTURE_TEST_SUITE(TimeFromStringTestSuite, TimeFromStringTest)
362
BOOST_AUTO_TEST_CASE(from_string)
363
{
364
  const char *valid_strings[]= {"080409",
365
                                "80409",
366
                                "08:04:09",
367
                                "8:04:09",
368
                                "8:04:9",
369
                                "8:4:9"};
370
  for (int it= 0; it < 6; it++)
371
  {
372
    const char *valid_string= valid_strings[it];
373
374
    result= time.from_string(valid_string, strlen(valid_string));
375
    BOOST_REQUIRE(result);
376
  
377
    assign_time_values();
378
379
    BOOST_REQUIRE_EQUAL(8, hours);
380
    BOOST_REQUIRE_EQUAL(4, minutes);
381
    BOOST_REQUIRE_EQUAL(9, seconds);
382
  }
383
}
384
BOOST_AUTO_TEST_SUITE_END()
385