trick/trick_source/sim_services/Clock/test/GetTimeOfDayClock_test.cpp

165 lines
4.8 KiB
C++
Raw Normal View History

2015-02-26 15:02:31 +00:00
/* Listed requirements are under requirement Trick-153 (3.7.1.1) */
/* Real-time control shall be able to use a system clock source */
2015-02-26 15:02:31 +00:00
#include <iostream>
#include <sys/types.h>
#include <signal.h>
#include <sys/time.h>
#include <map>
2015-02-26 15:02:31 +00:00
#include "gtest/gtest.h"
#include "trick/GetTimeOfDayClock.hh"
#include "trick/JobData.hh"
//#include "trick/RequirementScribe.hh"
2015-02-26 15:02:31 +00:00
#define TIME_TOL 1e3
#define RATIO_TOL 1e-9
#define TICS_PER_SEC 1000000
2015-07-01 23:21:56 +00:00
// Stub for message_publish
extern "C" int message_publish(int level, const char * format_msg, ...) { return 0; }
2015-02-26 15:02:31 +00:00
class GetTimeOfDayClockTest : public ::testing::Test {
protected:
Trick::GetTimeOfDayClock dClk;
//Trick::RequirementScribe req;
GetTimeOfDayClockTest() {}
~GetTimeOfDayClockTest() {}
virtual void SetUp() {}
virtual void TearDown() {}
2015-02-26 15:02:31 +00:00
} ;
/* Ensure clock initializes correctly */
TEST_F(GetTimeOfDayClockTest, Initialize) {
//req.add_requirement("timeofday_clock");
//"The system clock shall initialize with time equal to computer system time, reference time of 0, and all ratios set to 1.");
2015-02-26 15:02:31 +00:00
struct timeval res;
2015-07-01 23:21:56 +00:00
Trick::GetTimeOfDayClock * timclk = new Trick::GetTimeOfDayClock;
2015-02-26 15:02:31 +00:00
timclk->clock_init();
2015-02-26 15:02:31 +00:00
/* General expected clock initialization */
ASSERT_EQ(timclk->get_rt_clock_ratio() , 1.0);
ASSERT_EQ(timclk->sim_tic_ratio, 1.0);
ASSERT_EQ(timclk->ref_time_tics, 0);
2015-02-26 15:02:31 +00:00
/* Time of Day clock initialization tests */
2015-07-01 23:21:56 +00:00
EXPECT_STREQ(timclk->get_name() , "GetTimeOfDay - CLOCK_REALTIME");
2015-02-26 15:02:31 +00:00
gettimeofday(&res, NULL);
2015-02-26 15:02:31 +00:00
EXPECT_NEAR(timclk->wall_clock_time(), (res.tv_sec * 1000000LL + res.tv_usec), TIME_TOL);
2015-02-26 15:02:31 +00:00
}
TEST_F(GetTimeOfDayClockTest, ClockSimRatio) {
//req.add_requirement("timeofday_clock");
//"The system clock shall provide the ability to change the sim time-to-real time ratio.");
2015-02-26 15:02:31 +00:00
long long tim_curr;
double tic_ratio, tic_val;
2015-07-01 23:21:56 +00:00
Trick::GetTimeOfDayClock * timclk = new Trick::GetTimeOfDayClock;
2015-02-26 15:02:31 +00:00
tic_val = 100000;
2015-02-26 15:02:31 +00:00
timclk->calc_sim_time_ratio(tic_val);
2015-02-26 15:02:31 +00:00
usleep(100000);
tim_curr = timclk->wall_clock_time();
tic_ratio = (double) timclk->clock_time() / (double) tim_curr;
2015-02-26 15:02:31 +00:00
EXPECT_NEAR(tic_ratio, (double) tic_val / (double) TICS_PER_SEC, RATIO_TOL);
2015-02-26 15:02:31 +00:00
delete timclk;
2015-02-26 15:02:31 +00:00
}
TEST_F(GetTimeOfDayClockTest, ClockRTRatio) {
//req.add_requirement("timeofday_clock");
//"The system clock shall provide the ability to adjust the reference time ratio.");
2015-02-26 15:02:31 +00:00
long long tim_curr, tim_diff;
2015-02-26 15:02:31 +00:00
double tic_ratio;
int rt_ratio;
2015-07-01 23:21:56 +00:00
Trick::GetTimeOfDayClock * timclk = new Trick::GetTimeOfDayClock;
2015-02-26 15:02:31 +00:00
rt_ratio = 6.0;
ASSERT_EQ(timclk->sim_tic_ratio, 1);
2015-02-26 15:02:31 +00:00
timclk->calc_sim_time_ratio(TICS_PER_SEC);
2015-02-26 15:02:31 +00:00
timclk->set_rt_clock_ratio(rt_ratio);
tim_diff = timclk->wall_clock_time() - timclk->ref_time_tics;
EXPECT_NEAR(tim_diff, (long long) (timclk->wall_clock_time() / rt_ratio), TIME_TOL);
timclk->set_reference(0);
2015-02-26 15:02:31 +00:00
usleep(100000);
tim_curr = timclk->wall_clock_time();
tic_ratio = (double) timclk->clock_time() / (double) tim_curr;
2015-02-26 15:02:31 +00:00
EXPECT_NEAR(tic_ratio, rt_ratio, RATIO_TOL);
delete timclk;
2015-02-26 15:02:31 +00:00
}
TEST_F(GetTimeOfDayClockTest, ClockSpin) {
//req.add_requirement("timeofday_clock");
//"The system clock shall provide the ability to spin (wait) until a specified time is reached.");
long long tim_curr, spin_time;
2015-07-01 23:21:56 +00:00
Trick:: GetTimeOfDayClock * timclk = new Trick::GetTimeOfDayClock;
2015-02-26 15:02:31 +00:00
spin_time = 100000;
2015-02-26 15:02:31 +00:00
tim_curr = timclk->wall_clock_time();
2015-02-26 15:02:31 +00:00
dClk.clock_spin(tim_curr + spin_time);
EXPECT_EQ((timclk->wall_clock_time() - tim_curr), spin_time);
2015-02-26 15:02:31 +00:00
delete timclk;
2015-02-26 15:02:31 +00:00
}
/* Change and test reference times */
TEST_F(GetTimeOfDayClockTest, ReferenceTime) {
//req.add_requirement("timeofday_clock");
//"The system clock shall provide the ability to set a reference time.");
2015-02-26 15:02:31 +00:00
long long tic_adjust;
2015-07-01 23:21:56 +00:00
Trick:: GetTimeOfDayClock * timclk = new Trick::GetTimeOfDayClock;
2015-02-26 15:02:31 +00:00
timclk->clock_init();
2015-02-26 15:02:31 +00:00
tic_adjust = 50000;
timclk->set_reference(tic_adjust);
2015-02-26 15:02:31 +00:00
EXPECT_NEAR(timclk->clock_time(), timclk->wall_clock_time() - tic_adjust, TIME_TOL);
2015-02-26 15:02:31 +00:00
timclk->adjust_ref_time(2*tic_adjust);
EXPECT_EQ(timclk->ref_time_tics, tic_adjust + 2*tic_adjust);
delete timclk;
2015-02-26 15:02:31 +00:00
}
TEST_F(GetTimeOfDayClockTest, ClockSync) {
//req.add_requirement("timeofday_clock");
//"The system clock shall provide the ability to sync reference time to wall clock time");
double align_tics_mult;
int clock_unit;
2015-07-01 23:21:56 +00:00
Trick::GetTimeOfDayClock * timclk = new Trick::GetTimeOfDayClock;
align_tics_mult = 1.5;
clock_unit = int((TICS_PER_SEC * align_tics_mult) /
(timclk->rt_clock_ratio * timclk->sim_tic_ratio));
timclk->sync_to_wall_clock(align_tics_mult, TICS_PER_SEC);
EXPECT_EQ((timclk->ref_time_tics%clock_unit), 0);
delete timclk;
2015-02-26 15:02:31 +00:00
}