~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/temporal.h>
27
28
#include "temporal_generator.h"
29
30
using namespace drizzled;
31
32
class DateTimestampTestCompareOperators
33
{
34
 protected:
35
  Timestamp sample_timestamp;
36
  bool result;
37
  
38
  Date identical_with_sample_timestamp, before_sample_timestamp, after_sample_timestamp;
39
  
40
  DateTimestampTestCompareOperators()
41
  {
42
    TemporalGenerator::TimestampGen::make_timestamp(&sample_timestamp, 2010, 9, 8, 0, 0, 0);
43
    TemporalGenerator::DateGen::make_date(&before_sample_timestamp, 1980, 1, 1);
44
    TemporalGenerator::DateGen::make_date(&identical_with_sample_timestamp, 2010, 9, 8);
45
    TemporalGenerator::DateGen::make_date(&after_sample_timestamp, 2019, 5, 30);
46
  }
47
};
48
49
class DateTimeTimestampTestCompareOperators
50
{
51
 protected:
52
  Timestamp sample_timestamp;
53
  bool result;
54
  
55
  DateTime identical_with_sample_timestamp, before_sample_timestamp, after_sample_timestamp;
56
  
57
  DateTimeTimestampTestCompareOperators()
58
  {
59
    TemporalGenerator::TimestampGen::make_timestamp(&sample_timestamp, 2010, 9, 8, 0, 0, 0);
60
    TemporalGenerator::DateTimeGen::make_datetime(&before_sample_timestamp, 1990, 12, 31, 12, 12, 30);
61
    TemporalGenerator::DateTimeGen::make_datetime(&identical_with_sample_timestamp, 2010, 9, 8, 0, 0, 0);
62
    TemporalGenerator::DateTimeGen::make_datetime(&after_sample_timestamp, 2020, 4, 4, 4, 4, 4);
63
  }
64
};
65
66
class TimestampTimestampTestCompareOperators
67
{
68
 protected:
69
  Timestamp sample_timestamp;
70
  bool result;
71
  
72
  Timestamp identical_with_sample_timestamp, before_sample_timestamp, after_sample_timestamp;
73
  
74
  TimestampTimestampTestCompareOperators()
75
  {
76
    TemporalGenerator::TimestampGen::make_timestamp(&sample_timestamp, 2010, 9, 8, 0, 0, 0);
77
    TemporalGenerator::TimestampGen::make_timestamp(&before_sample_timestamp, 2010, 9, 7, 23, 59, 59);
78
    TemporalGenerator::TimestampGen::make_timestamp(&identical_with_sample_timestamp, 2010, 9, 8, 0, 0, 0);
79
    TemporalGenerator::TimestampGen::make_timestamp(&after_sample_timestamp, 2010, 9, 8, 0, 0, 1);
80
  }
81
};
82
83
84
BOOST_AUTO_TEST_SUITE(TimestampCompareTestSuite)
85
BOOST_FIXTURE_TEST_CASE(DateTimestampTestCompareTest, DateTimestampTestCompareOperators)
86
{
87
  this->result= (this->sample_timestamp == this->identical_with_sample_timestamp);  
88
  BOOST_REQUIRE(this->result);
89
90
  this->result= (this->sample_timestamp == this->before_sample_timestamp);  
91
  BOOST_REQUIRE(not this->result);
92
93
  this->result= (this->sample_timestamp != this->identical_with_sample_timestamp);  
94
  BOOST_REQUIRE(not this->result);
95
96
  this->result= (this->sample_timestamp != this->before_sample_timestamp);  
97
  BOOST_REQUIRE(this->result);
98
99
  this->result= (this->sample_timestamp > this->identical_with_sample_timestamp);  
100
  BOOST_REQUIRE(not this->result);
101
102
  this->result= (this->sample_timestamp > this->after_sample_timestamp);  
103
  BOOST_REQUIRE(not this->result);
104
105
  this->result= (this->sample_timestamp > this->before_sample_timestamp);  
106
  BOOST_REQUIRE(this->result);
107
108
  this->result= (this->sample_timestamp >= this->identical_with_sample_timestamp);  
109
  BOOST_REQUIRE(this->result);
110
111
  this->result= (this->sample_timestamp >= this->after_sample_timestamp);  
112
  BOOST_REQUIRE(not this->result);
113
114
  this->result= (this->sample_timestamp >= this->before_sample_timestamp);  
115
  BOOST_REQUIRE(this->result);
116
117
  this->result= (this->sample_timestamp < this->identical_with_sample_timestamp);  
118
  BOOST_REQUIRE(not this->result);
119
120
  this->result= (this->sample_timestamp < this->after_sample_timestamp);  
121
  BOOST_REQUIRE(this->result);
122
123
  this->result= (this->sample_timestamp < this->before_sample_timestamp);  
124
  BOOST_REQUIRE(not this->result);
125
126
  this->result= (this->sample_timestamp <= this->identical_with_sample_timestamp);  
127
  BOOST_REQUIRE(this->result);
128
129
  this->result= (this->sample_timestamp <= this->after_sample_timestamp);  
130
  BOOST_REQUIRE(this->result);
131
132
  this->result= (this->sample_timestamp <= this->before_sample_timestamp);  
133
  BOOST_REQUIRE(not this->result);
134
}
135
136
BOOST_FIXTURE_TEST_CASE(DateTimeTimestampTestCompareTest, DateTimeTimestampTestCompareOperators)
137
{
138
  this->result= (this->sample_timestamp == this->identical_with_sample_timestamp);  
139
  BOOST_REQUIRE(this->result);
140
141
  this->result= (this->sample_timestamp == this->before_sample_timestamp);  
142
  BOOST_REQUIRE(not this->result);
143
144
  this->result= (this->sample_timestamp != this->identical_with_sample_timestamp);  
145
  BOOST_REQUIRE(not this->result);
146
147
  this->result= (this->sample_timestamp != this->before_sample_timestamp);  
148
  BOOST_REQUIRE(this->result);
149
150
  this->result= (this->sample_timestamp > this->identical_with_sample_timestamp);  
151
  BOOST_REQUIRE(not this->result);
152
153
  this->result= (this->sample_timestamp > this->after_sample_timestamp);  
154
  BOOST_REQUIRE(not this->result);
155
156
  this->result= (this->sample_timestamp > this->before_sample_timestamp);  
157
  BOOST_REQUIRE(this->result);
158
159
  this->result= (this->sample_timestamp >= this->identical_with_sample_timestamp);  
160
  BOOST_REQUIRE(this->result);
161
162
  this->result= (this->sample_timestamp >= this->after_sample_timestamp);  
163
  BOOST_REQUIRE(not this->result);
164
165
  this->result= (this->sample_timestamp >= this->before_sample_timestamp);  
166
  BOOST_REQUIRE(this->result);
167
168
  this->result= (this->sample_timestamp < this->identical_with_sample_timestamp);  
169
  BOOST_REQUIRE(not this->result);
170
171
  this->result= (this->sample_timestamp < this->after_sample_timestamp);  
172
  BOOST_REQUIRE(this->result);
173
174
  this->result= (this->sample_timestamp < this->before_sample_timestamp);  
175
  BOOST_REQUIRE(not this->result);
176
177
  this->result= (this->sample_timestamp <= this->identical_with_sample_timestamp);  
178
  BOOST_REQUIRE(this->result);
179
180
  this->result= (this->sample_timestamp <= this->after_sample_timestamp);  
181
  BOOST_REQUIRE(this->result);
182
183
  this->result= (this->sample_timestamp <= this->before_sample_timestamp);  
184
  BOOST_REQUIRE(not this->result);
185
}
186
187
BOOST_FIXTURE_TEST_CASE(TimestampTimestampTestCompareTest, TimestampTimestampTestCompareOperators)
188
{
189
  this->result= (this->sample_timestamp == this->identical_with_sample_timestamp);  
190
  BOOST_REQUIRE(this->result);
191
192
  this->result= (this->sample_timestamp == this->before_sample_timestamp);  
193
  BOOST_REQUIRE(not this->result);
194
195
  this->result= (this->sample_timestamp != this->identical_with_sample_timestamp);  
196
  BOOST_REQUIRE(not this->result);
197
198
  this->result= (this->sample_timestamp != this->before_sample_timestamp);  
199
  BOOST_REQUIRE(this->result);
200
201
  this->result= (this->sample_timestamp > this->identical_with_sample_timestamp);  
202
  BOOST_REQUIRE(not this->result);
203
204
  this->result= (this->sample_timestamp > this->after_sample_timestamp);  
205
  BOOST_REQUIRE(not this->result);
206
207
  this->result= (this->sample_timestamp > this->before_sample_timestamp);  
208
  BOOST_REQUIRE(this->result);
209
210
  this->result= (this->sample_timestamp >= this->identical_with_sample_timestamp);  
211
  BOOST_REQUIRE(this->result);
212
213
  this->result= (this->sample_timestamp >= this->after_sample_timestamp);  
214
  BOOST_REQUIRE(not this->result);
215
216
  this->result= (this->sample_timestamp >= this->before_sample_timestamp);  
217
  BOOST_REQUIRE(this->result);
218
219
  this->result= (this->sample_timestamp < this->identical_with_sample_timestamp);  
220
  BOOST_REQUIRE(not this->result);
221
222
  this->result= (this->sample_timestamp < this->after_sample_timestamp);  
223
  BOOST_REQUIRE(this->result);
224
225
  this->result= (this->sample_timestamp < this->before_sample_timestamp);  
226
  BOOST_REQUIRE(not this->result);
227
228
  this->result= (this->sample_timestamp <= this->identical_with_sample_timestamp);  
229
  BOOST_REQUIRE(this->result);
230
231
  this->result= (this->sample_timestamp <= this->after_sample_timestamp);  
232
  BOOST_REQUIRE(this->result);
233
234
  this->result= (this->sample_timestamp <= this->before_sample_timestamp);  
235
  BOOST_REQUIRE(not this->result);
236
}
237
BOOST_AUTO_TEST_SUITE_END()
238
239
class TimestampTest
240
{
241
  protected:
242
    Timestamp timestamp;
243
    bool result;
244
};
245
246
BOOST_FIXTURE_TEST_SUITE(TimestampRangesSuite, TimestampTest)
247
BOOST_AUTO_TEST_CASE(is_valid_minOfTimestampRange_shouldReturn_True)
248
{
249
  uint32_t year= 1970, month= 1, day= 1, hour= 0, minute= 0, second= 0;
250
  TemporalGenerator::TimestampGen::make_timestamp(&timestamp, year, month, day, hour, minute, second);
251
252
  result= timestamp.is_valid();
253
254
  BOOST_REQUIRE(result);
255
}
256
257
BOOST_AUTO_TEST_CASE(is_valid_maxOfTimestampRange_shouldReturn_True)
258
{
259
  uint32_t year= 2038, month= 1, day= 19, hour= 3, minute= 14, second= 7;
260
  TemporalGenerator::TimestampGen::make_timestamp(&timestamp, year, month, day, hour, minute, second);
261
  
262
  result= timestamp.is_valid();
263
  
264
  BOOST_REQUIRE(result);
265
}
266
267
BOOST_AUTO_TEST_CASE(is_valid_oneSecondBeforeTimestampMinOfRange_shouldReturn_False)
268
{
269
  uint32_t year= 1969, month= 12, day= 31, hour= 23, minute= 59, second= 59;
270
  TemporalGenerator::TimestampGen::make_timestamp(&timestamp, year, month, day, hour, minute, second);
271
  
272
  result= timestamp.is_valid();
273
  
274
  BOOST_REQUIRE(not result);
275
}
276
277
BOOST_AUTO_TEST_CASE(is_valid_oneSecondAfterTimestampMaxOfRange_shouldReturn_False)
278
{
279
  uint32_t year= 2038, month= 1, day= 19, hour= 3, minute= 14, second= 8;
280
  TemporalGenerator::TimestampGen::make_timestamp(&timestamp, year, month, day, hour, minute, second);
281
  
282
  result= timestamp.is_valid();
283
  
284
  BOOST_REQUIRE(not result);
285
}
286
287
BOOST_AUTO_TEST_CASE(is_valid_InsideOfTimestampRange_shouldReturn_True)
288
{
289
  uint32_t year= 1980, month= 11, day= 1, hour= 5, minute= 8, second= 5;
290
  TemporalGenerator::TimestampGen::make_timestamp(&timestamp, year, month, day, hour, minute, second);
291
  
292
  result= timestamp.is_valid();
293
  
294
  BOOST_REQUIRE(result);
295
}
296
297
BOOST_AUTO_TEST_CASE(to_time_t)
298
{
299
  time_t time;
300
  TemporalGenerator::TimestampGen::make_timestamp(&timestamp, 2009, 6, 3, 4, 59, 1);
301
  
302
  timestamp.to_time_t(time);
303
  
304
  BOOST_REQUIRE_EQUAL(1244005141, time);
305
}
306
307
BOOST_AUTO_TEST_CASE(outputStreamOperator_shouldWrite_hyphenSeperatedDateElements_and_colonSeperatedTimeElements)
308
{
309
  std::ostringstream output;
310
  std::string expected= "2010-05-01 08:07:06";
311
  std::string returned;
312
  TemporalGenerator::TimestampGen::make_timestamp(&timestamp, 2010, 5, 1, 8, 7, 6);
313
  
314
  output << timestamp;
315
  returned= output.str();
316
  
317
  BOOST_REQUIRE_EQUAL(expected, returned);
318
}
319
BOOST_AUTO_TEST_SUITE_END()