LRAUV  revA
Timestamp_Test.h
Go to the documentation of this file.
1 
9 #ifndef _TC_TIME_H_
10 #define _TC_TIME_H_
11 
12 #include "utils/Timestamp.h"
13 #include <cxxtest/TestSuite.h>
14 
15 #include <time.h>
16 
17 const double DOUBLE_FUDGE = 1e-6;
19 
25 class Timestamp_Test : public CxxTest::TestSuite
26 {
27 public:
28 
29  // Constructor with double param tests
34  {
35  Timestamp thisTimestamp;
36 
37  // Check both ways
38  TS_ASSERT_EQUALS( thisTimestamp.asDouble(), Timestamp::NOT_SET_TIME.asDouble() );
39  TS_ASSERT_EQUALS( thisTimestamp, Timestamp( Timestamp::NOT_SET_TIME ) );
40 
41  // Should fail...
42  TS_ASSERT_DIFFERS( thisTimestamp.asDouble(), 0.0 );
43  TS_ASSERT_DIFFERS( thisTimestamp, Timestamp( 0.0 ) );
44  }
45 
47  {
48  Timestamp thisTimestamp( "1970-01-01T00:00:00.000000Z" );
49 
50  TS_ASSERT_EQUALS( thisTimestamp.asDouble(), 0 );
51 
52  thisTimestamp = "1970-01-01T00:00:00.015625Z";
53  TS_ASSERT_EQUALS( thisTimestamp.asDouble(), 0.015625 );
54 
55  thisTimestamp = "2009-02-13T23:31:30Z";
56  TS_ASSERT_EQUALS( thisTimestamp.asDouble(), 1234567890.0 );
57 
58  thisTimestamp = "02/13/2009 23:31:30";
59  TS_ASSERT_EQUALS( thisTimestamp.asDouble(), 1234567890.0 );
60 
61  }
62 
64  {
65  double param = 100.0;
66  Timestamp thisTimestamp( param );
67 
68  TS_ASSERT_EQUALS( thisTimestamp.asDouble(), param );
69  TS_ASSERT_EQUALS( thisTimestamp, Timestamp( param ) );
70  }
71 
76  {
77  Timestamp timestampOne( 100.0 ), timestampTwo( timestampOne );
78 
79  TS_ASSERT_EQUALS( timestampOne.asDouble(), timestampTwo.asDouble() );
80  TS_ASSERT_EQUALS( timestampOne, timestampTwo );
81  }
82 
89  void testCopyOperator( void )
90  {
91  Timestamp timestampOne( 100.0 ), timestampTwo( 1234567890.0 );
92 
93  TS_ASSERT_DIFFERS( timestampOne, timestampTwo );
94  timestampOne = timestampTwo;
95  TS_ASSERT_EQUALS( timestampOne, timestampTwo );
96  }
97 
106  void testSetOperatorOne( void )
107  {
108  double A = 123456.0, B = 2042322342.0;
109 
110  Timestamp timestampOne( A );
111 
112  TS_ASSERT_DIFFERS( timestampOne.asDouble(), B );
113  TS_ASSERT_DIFFERS( timestampOne, Timestamp( B ) );
114  timestampOne = B;
115  TS_ASSERT_EQUALS( timestampOne.asDouble(), B );
116  TS_ASSERT_EQUALS( timestampOne, Timestamp( B ) );
117  }
118 
119  void testSetOperatorTwo( void )
120  {
121  Timestamp timestampOne( 2042322342.0 ), timestampTwo( 1.0 );
122 
123  TS_ASSERT_DIFFERS( timestampOne, timestampTwo );
124  timestampOne = timestampTwo.asDouble();
125  TS_ASSERT_EQUALS( timestampOne, timestampTwo );
126  }
127 
133  {
134  double A = 12345.6, B = 92245.0;
135  Timestamp timestampOne( A );
136  Timespan timespanTwo( B );
137 
138  timestampOne += timespanTwo;
139  TS_ASSERT_DELTA( timestampOne.asDouble(), A + B, DOUBLE_FUDGE );
140  TS_ASSERT_DELTA( timestampOne, Timestamp( A + B ), TIMESPAN_FUDGE );
141 
142  }
143 
149  {
150  double A = 12345.6, B = 92245.0;
151  Timestamp timestampOne( A );
152  Timespan timespanTwo( B );
153 
154  timestampOne -= timespanTwo;
155 
156  TS_ASSERT_DELTA( timestampOne.asDouble(), A - B, DOUBLE_FUDGE );
157  TS_ASSERT_DELTA( timestampOne, Timestamp( A - B ), TIMESPAN_FUDGE );
158 
159  }
160 
170  {
171  double A = 244332234.224325234;
172  double B = 1433453234.2342223;
173 
174  Timestamp timestampA( A ), timestampB( B );
175  Timespan timespanA( A ), timespanB( B );
176 
177  TS_ASSERT_DELTA( Timestamp( A + B ), timestampA + timespanB, TIMESPAN_FUDGE );
178  TS_ASSERT_DELTA( Timestamp( A - B ), timestampA - timespanB, TIMESPAN_FUDGE );
179  TS_ASSERT_DELTA( Timestamp( B - A ), timestampB - timespanA, TIMESPAN_FUDGE );
180  TS_ASSERT_DELTA( Timespan( A - B ), timestampA - timestampB, TIMESPAN_FUDGE );
181  TS_ASSERT_DELTA( Timespan( B - A ), timestampB - timestampA, TIMESPAN_FUDGE );
182  }
183 
188  void testInquality( void )
189  {
190  double B = 1033453234.2342223,
191  A = 2044332234.224325234;
192  Timestamp timestampA( A ), timestampB( B );
193 
194  // Make sure the test is predicated on a good assumption
195  TS_ASSERT( A > B );
196 
197  TS_ASSERT( timestampA > timestampB );
198  TS_ASSERT( !( timestampA < timestampB ) );
199  TS_ASSERT( timestampA >= timestampB );
200  TS_ASSERT( !( timestampA <= timestampB ) );
201 
202  timestampB = timestampA;
203  TS_ASSERT( timestampA >= timestampB );
204  TS_ASSERT( timestampB >= timestampA );
205 
206 
207  }
208 
210  void testSleepNow( void )
211  {
212  const Timespan sleepTimestamp( Timespan::Milliseconds( 250 ) );
213  const Timespan ACCEPTABLE_ERROR_SECONDS( 0.01 );
214 
215  Timestamp timestampNow = Timestamp::Now(), timestampAfter;
216 
217  timestampNow += sleepTimestamp;
218 
219  timestampNow.sleepTill();
220 
221  timestampAfter = Timestamp::Now();
222 
223  TS_ASSERT_DELTA( timestampAfter, timestampNow, ACCEPTABLE_ERROR_SECONDS );
224  }
225 
226  void testNow( void )
227  {
228  const Timespan ACCEPTABLE_ERROR_SECONDS( 0.1 );
229  Timestamp timestampNow, timestampNowToo;
230 
231  timestampNow = Timestamp::Now();
232  timestampNowToo = Timestamp::Now();
233 
234  TS_ASSERT_DELTA( timestampNow, timestampNowToo, ACCEPTABLE_ERROR_SECONDS );
235  }
236 
238  void testWholeUnits( void )
239  {
240  Timestamp timestampOne( 0.0 ), timestampTwo( 3661.001 );
241 
242  timestampOne = 0.0;
243  timestampOne += Timespan::Hours( 1 );
244  timestampOne += Timespan::Minutes( 1 );
245  timestampOne += Timespan::Seconds( 1 );
246  timestampOne += Timespan::Milliseconds( 1 );
247 
248  TS_ASSERT_DELTA( timestampOne, timestampTwo, TIMESPAN_FUDGE );
249  }
250 
251  void testToString( void )
252  {
253  Timestamp timestampNow, timestampBefore, timestampAfter;
254  time_t rawtime;
255  struct tm * timeinfo;
256 
257  timestampNow = Timestamp::Now();
258  TS_ASSERT( timestampNow > 0.0 );
259 
260  rawtime = timestampNow.asTimeT();
261  timeinfo = gmtime( &rawtime );
262 
263  char bufferNow[80];
264  timestampNow.toString( bufferNow, 79 );
265 
266  char buffer[80];
267  strftime( buffer, 80, "%Y-%m-%dT%H:%M:%S", timeinfo );
268 
269  TS_ASSERT_SAME_DATA( bufferNow, buffer, 19 );
270  }
271 
272 };
273 
279 class Timespan_Test : public CxxTest::TestSuite
280 {
281 public:
282  // Constructor with no param tests
286  {
287  Timespan thisTimespan;
288 
289  TS_ASSERT_EQUALS( thisTimespan.asDouble(), Timespan::ZERO_TIMESPAN.asDouble() );
290  TS_ASSERT_EQUALS( thisTimespan, Timespan::ZERO_TIMESPAN );
291  }
292 
293  // Constructor with double param tests
297  {
298  double param = 100.0;
299  Timespan thisTimespan( param );
300 
301  TS_ASSERT_EQUALS( thisTimespan.asDouble(), param );
302  TS_ASSERT_EQUALS( thisTimespan, Timespan( param ) );
303  }
304 
309  {
310  Timespan timespanOne( 100.0 ), timespanTwo( timespanOne );
311 
312  TS_ASSERT_EQUALS( timespanOne.asDouble(), timespanTwo.asDouble() );
313  TS_ASSERT_EQUALS( timespanOne, timespanTwo );
314  }
315 
322  void testCopyOperator( void )
323  {
324  Timestamp timespanOne( 100.0 ), timespanTwo( 1234567890.0 );
325 
326  TS_ASSERT_DIFFERS( timespanOne, timespanTwo );
327  TS_ASSERT_DIFFERS( timespanOne.asDouble(), timespanTwo.asDouble() );
328  timespanOne = timespanTwo;
329  TS_ASSERT_EQUALS( timespanOne, timespanTwo );
330  TS_ASSERT_EQUALS( timespanOne.asDouble(), timespanTwo.asDouble() );
331  }
332 
341  void testSetOperatorOne( void )
342  {
343  double A = 123456.0, B = 2042322342.0;
344 
345  Timestamp timespanOne( A );
346 
347  TS_ASSERT_DIFFERS( timespanOne.asDouble(), B );
348  timespanOne = B;
349  TS_ASSERT_EQUALS( timespanOne.asDouble(), B );
350  }
351 
352  void testSetOperatorTwo( void )
353  {
354  Timestamp timespanOne( 2042322342.0 ), timespanTwo( 1.0 );
355 
356  TS_ASSERT_DIFFERS( timespanOne, timespanTwo );
357  timespanOne = timespanTwo.asDouble();
358  TS_ASSERT_EQUALS( timespanOne, timespanTwo );
359  }
360 
369  {
370  double B = 2368.234,
371  A = 8242.233345;
372 
373  Timespan timespanA( A ), timespanB( B );
374 
375  TS_ASSERT_DELTA( Timespan( A + B ), timespanA + timespanB, TIMESPAN_FUDGE );
376  TS_ASSERT_DELTA( Timespan( A - B ), timespanA - timespanB, TIMESPAN_FUDGE );
377  TS_ASSERT_DELTA( Timespan( B + A ), timespanB + timespanA, TIMESPAN_FUDGE );
378  TS_ASSERT_DELTA( Timespan( B - A ), timespanB - timespanA, TIMESPAN_FUDGE );
379  }
380 
385  void testInquality( void )
386  {
387  double B = 2368.234,
388  A = 8242.233345;
389 
390  Timespan timespanA( A ), timespanB( B );
391 
392  // Make sure the test is predicated on a good assumption
393  TS_ASSERT( A > B );
394 
395  TS_ASSERT( timespanA > timespanB );
396  TS_ASSERT( !( timespanA < timespanB ) );
397  TS_ASSERT( timespanA >= timespanB );
398  TS_ASSERT( !( timespanA <= timespanB ) );
399 
400  timespanB = timespanA;
401  TS_ASSERT( timespanA >= timespanB );
402  TS_ASSERT( timespanB >= timespanA );
403 
404 
405  }
406 
408  void testSleepFor( void )
409  {
410  const Timespan sleepTimestamp( Timespan::Milliseconds( 250 ) );
411  const Timespan ACCEPTABLE_ERROR_SECONDS( 0.025 );
412  Timestamp timestampBefore, timestampAfter;
413  Timespan sleptFor;
414 
415  timestampBefore = Timestamp::Now();
416  sleepTimestamp.sleepFor();
417  timestampAfter = Timestamp::Now();
418 
419  sleptFor = timestampAfter - timestampBefore;
420 
421  TS_ASSERT_DELTA( sleepTimestamp, sleptFor, ACCEPTABLE_ERROR_SECONDS );
422  }
423 
424 
425  void testToString( void )
426  {
427  Timespan span = 1234.5678;
428 
429  char buffer[80];
430  span.toString( buffer, 79 );
431 
432  TS_ASSERT( strstr( buffer, "1234.5678" ) );
433  }
434 
435 };
436 
437 
438 
439 #endif // _TC_TIME_H
static const Timespan ZERO_TIMESPAN
Constant value to represent instant of time.
Definition: Timestamp.h:255
void testSetOperatorOne(void)
Set operator.
Definition: Timestamp_Test.h:106
const double DOUBLE_FUDGE
Definition: Timestamp_Test.h:17
time_t asTimeT() const
Definition: Timestamp.h:97
void testCopyOperator(void)
Copy operator.
Definition: Timestamp_Test.h:322
void testSleepNow(void)
Test the sleepTill function.
Definition: Timestamp_Test.h:210
void testAdditionAndSubtraction(void)
Standard addition/subtraction functions.
Definition: Timestamp_Test.h:368
virtual double asDouble() const
Accessor, convert to double.
Definition: Timestamp.cpp:826
void testInPlaceAdditionToTimespan(void)
In-place addition operator to timespan Tests:Create to timestamp objects with params A timespan B...
Definition: Timestamp_Test.h:132
void testToString(void)
Definition: Timestamp_Test.h:251
void testInquality(void)
Inequality operator.
Definition: Timestamp_Test.h:385
Timespan class, represents a delta of time.
Definition: Timestamp.h:248
void testSetOperatorOne(void)
Set operator.
Definition: Timestamp_Test.h:341
const char * toString(char *buf, int buflen, int precision=3) const
Writes the Timestamp as to a unsigned char buffer.
Definition: Timestamp.cpp:418
static Timespan Minutes(int min)
Static allows creation of Timespans denominated in minutes.
Definition: Timestamp.h:313
void testSetOperatorTwo(void)
Definition: Timestamp_Test.h:352
Unit Tests for class Timestamp.
Definition: Timestamp_Test.h:25
void testNow(void)
Definition: Timestamp_Test.h:226
void testAdditionAndSubtraction(void)
Standard addition/subtraction functions.
Definition: Timestamp_Test.h:169
void testConstructorDoubleArg(void)
Tests:Create a new object with double param, Expect asDouble() == param.
Definition: Timestamp_Test.h:296
void testConstructorNoArguments(void)
Tests:Create a new object w/o params.
Definition: Timestamp_Test.h:33
void testWholeUnits(void)
Test whole units.
Definition: Timestamp_Test.h:238
static const Timestamp NOT_SET_TIME
Constant value to represent "no time".
Definition: Timestamp.h:37
void sleepFor(void) const
Sleep for the timespan.
Definition: Timestamp.cpp:951
const Timespan TIMESPAN_FUDGE(DOUBLE_FUDGE)
static Timestamp Now()
Static for current time for use on RHS.
Definition: Timestamp.cpp:362
void testConstructorDoubleArg(void)
Definition: Timestamp_Test.h:63
void testConstructorCopyArg(void)
Constructor with copy param testsCreate new object copied from another object.
Definition: Timestamp_Test.h:308
void testConstructorStringArg(void)
Definition: Timestamp_Test.h:46
static Timespan Seconds(double sec)
Static allows creation of Timespans denominated in seconds (as a float) This function originally took...
Definition: Timestamp.h:307
void testConstructorCopyArg(void)
Constructor with copy param testsCreate new object copied from another object.
Definition: Timestamp_Test.h:75
void sleepTill()
Sleep until the given time.
Definition: Timestamp.cpp:386
void testToString(void)
Definition: Timestamp_Test.h:425
const char * toString(char *buf, int buflen, int precision=3) const
Writes the Timespan to a unsigned char buffer.
Definition: Timestamp.cpp:963
Unit Tests for class Timespan.
Definition: Timestamp_Test.h:279
virtual double asDouble() const
Accessor, convert to double.
Definition: Timestamp.cpp:127
void testInquality(void)
Inequality operator.
Definition: Timestamp_Test.h:188
void testSleepFor(void)
Test the sleepFor function.
Definition: Timestamp_Test.h:408
void testConstructorNoArguments(void)
Tests:Create a new object w/o params.
Definition: Timestamp_Test.h:285
void testInPlaceSubtractionByTimespan(void)
In-place subtraction operator to timespan Tests:Create to timestamp objects with params A timespan B...
Definition: Timestamp_Test.h:148
void testCopyOperator(void)
Copy operator.
Definition: Timestamp_Test.h:89
Contains the Timestamp and Timespan class declarations.
static Timespan Hours(int hour)
Static allows creation of Timespans denominated in hours.
Definition: Timestamp.h:319
Represents absolute times.
Definition: Timestamp.h:31
void testSetOperatorTwo(void)
Definition: Timestamp_Test.h:119
static Timespan Milliseconds(float msec)
Static allows creation of Timespans denominated in milliseconds.
Definition: Timestamp.h:298