123 lines
3.5 KiB
C++
123 lines
3.5 KiB
C++
|
#include <cxxtest/TestSuite.h>
|
||
|
|
||
|
#include "timestamp.hh"
|
||
|
|
||
|
|
||
|
// Allows to test equality of two struct timespec
|
||
|
bool operator==(const struct timespec &d1,
|
||
|
const struct timespec &d2)
|
||
|
{
|
||
|
return
|
||
|
d1.tv_sec == d2.tv_sec &&
|
||
|
d1.tv_nsec == d2.tv_nsec ;
|
||
|
}
|
||
|
|
||
|
|
||
|
class Timestamp_test: public CxxTest::TestSuite
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
void test_zero(void)
|
||
|
{
|
||
|
// Default constructor
|
||
|
Timestamp timestamp1 ;
|
||
|
|
||
|
// struct timespec constructor
|
||
|
struct timespec timespec1 ;
|
||
|
timespec1.tv_sec = 0 ;
|
||
|
timespec1.tv_nsec = 0 ;
|
||
|
Timestamp timestamp2(timespec1) ;
|
||
|
TS_ASSERT_EQUALS(timestamp1, timestamp2) ;
|
||
|
|
||
|
// ms constructor
|
||
|
uint64_t msec = 0 ; // ms time value
|
||
|
Timestamp timestamp3(msec) ;
|
||
|
TS_ASSERT_EQUALS(timestamp1, timestamp3) ;
|
||
|
|
||
|
// Read accessors
|
||
|
TS_ASSERT_EQUALS(timestamp1.get_timestamp(), timespec1) ;
|
||
|
TS_ASSERT_EQUALS(timestamp1.get_timestamp_ms(), msec) ;
|
||
|
}
|
||
|
|
||
|
void test_non_zero(void)
|
||
|
{
|
||
|
uint64_t sec = 1234567 ;
|
||
|
uint64_t nsec = 891234567 ; // Do not put a value >= 1 sec. for the test!
|
||
|
uint64_t msec = sec * 1000 + nsec / 1000000 ;
|
||
|
struct timespec timespec1 ;
|
||
|
timespec1.tv_sec = sec ;
|
||
|
timespec1.tv_nsec = nsec ;
|
||
|
|
||
|
// struct timespec constructor
|
||
|
Timestamp timestamp1(timespec1) ;
|
||
|
timespec1.tv_nsec = nsec / 1000000 * 1000000 ; // Round to ms
|
||
|
TS_ASSERT_EQUALS(timestamp1.get_timestamp(), timespec1) ;
|
||
|
TS_ASSERT_EQUALS(timestamp1.get_timestamp_ms(), msec) ;
|
||
|
|
||
|
// ms constructor
|
||
|
Timestamp timestamp2(msec) ;
|
||
|
TS_ASSERT_EQUALS(timestamp2.get_timestamp(), timespec1) ;
|
||
|
TS_ASSERT_EQUALS(timestamp2.get_timestamp_ms(), msec) ;
|
||
|
TS_ASSERT_EQUALS(timestamp1, timestamp2) ;
|
||
|
}
|
||
|
|
||
|
void test_current_time(void)
|
||
|
{
|
||
|
Timestamp timestamp1 ;
|
||
|
struct timespec current_time ;
|
||
|
|
||
|
if (! timestamp1.now() ||
|
||
|
clock_gettime(CLOCK_REALTIME, ¤t_time))
|
||
|
{
|
||
|
TS_FAIL("Error getting current time!") ;
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
Timestamp timestamp2(current_time) ;
|
||
|
|
||
|
TS_ASSERT_DELTA(timestamp1.get_timestamp_ms(),
|
||
|
timestamp2.get_timestamp_ms(),
|
||
|
100) ;
|
||
|
|
||
|
// The following tests may fail if the ns value is a ms
|
||
|
TS_ASSERT_DIFFERS(timestamp1.get_timestamp(), current_time) ;
|
||
|
TS_ASSERT_DIFFERS(timestamp2.get_timestamp(), current_time) ;
|
||
|
}
|
||
|
|
||
|
void test_accessors(void)
|
||
|
{
|
||
|
struct timespec current_time ;
|
||
|
if (clock_gettime(CLOCK_REALTIME, ¤t_time))
|
||
|
{
|
||
|
TS_FAIL("Error calling clock_gettime()!") ;
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
// struct timespec constructor
|
||
|
Timestamp timestamp1(current_time) ;
|
||
|
TS_ASSERT_DIFFERS(timestamp1.get_timestamp(), current_time) ;
|
||
|
|
||
|
// ms constructor
|
||
|
current_time.tv_nsec = current_time.tv_nsec / 1000000 * 1000000 ;
|
||
|
uint64_t msec = timestamp1.get_timestamp_ms() ;
|
||
|
Timestamp timestamp2(msec) ;
|
||
|
TS_ASSERT_EQUALS(timestamp2.get_timestamp_ms(), msec) ;
|
||
|
TS_ASSERT_EQUALS(timestamp2.get_timestamp(), current_time) ;
|
||
|
|
||
|
// Accessors
|
||
|
++current_time.tv_sec ;
|
||
|
timestamp2.set_timestamp(current_time) ;
|
||
|
TS_ASSERT_DIFFERS(timestamp1, timestamp2) ;
|
||
|
msec = timestamp2.get_timestamp_ms() ;
|
||
|
TS_ASSERT_EQUALS(timestamp2.get_timestamp_ms(), msec) ;
|
||
|
TS_ASSERT_EQUALS(timestamp2.get_timestamp(), current_time) ;
|
||
|
|
||
|
msec = 1234567891234567ull ;
|
||
|
timestamp2.set_timestamp_ms(msec) ;
|
||
|
TS_ASSERT_EQUALS(timestamp2.get_timestamp_ms(), msec) ;
|
||
|
current_time = timestamp2.get_timestamp() ;
|
||
|
TS_ASSERT_EQUALS(timestamp2.get_timestamp(), current_time) ;
|
||
|
}
|
||
|
|
||
|
} ;
|