fsfw/unittests/osal/TestClock.cpp

86 lines
3.3 KiB
C++
Raw Normal View History

#include <fsfw/globalfunctions/timevalOperations.h>
2022-03-25 18:48:53 +01:00
#include <fsfw/timemanager/Clock.h>
#include <array>
#include <catch2/catch_approx.hpp>
2022-03-25 18:48:53 +01:00
#include <catch2/catch_test_macros.hpp>
2022-07-18 11:58:55 +02:00
#include "CatchDefinitions.h"
2022-03-25 18:48:53 +01:00
TEST_CASE("OSAL::Clock Test", "[OSAL::Clock Test]") {
SECTION("Test getClock") {
timeval time;
ReturnValue_t result = Clock::getClock_timeval(&time);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-03-25 18:48:53 +01:00
Clock::TimeOfDay_t timeOfDay;
result = Clock::getDateAndTime(&timeOfDay);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-03-25 18:48:53 +01:00
timeval timeOfDayAsTimeval;
result = Clock::convertTimeOfDayToTimeval(&timeOfDay, &timeOfDayAsTimeval);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-03-25 18:48:53 +01:00
// We require timeOfDayAsTimeval to be larger than time as it
// was request a few ns later
double difference = timevalOperations::toDouble(timeOfDayAsTimeval - time);
CHECK(difference >= 0.0);
CHECK(difference <= 0.005);
2022-03-25 18:48:53 +01:00
// Conversion in the other direction
Clock::TimeOfDay_t timevalAsTimeOfDay;
result = Clock::convertTimevalToTimeOfDay(&time, &timevalAsTimeOfDay);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-03-25 18:48:53 +01:00
CHECK(timevalAsTimeOfDay.year <= timeOfDay.year);
// TODO We should write TimeOfDay operators!
}
SECTION("Leap seconds") {
uint16_t leapSeconds = 0;
ReturnValue_t result = Clock::getLeapSeconds(&leapSeconds);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::FAILED);
2022-03-25 18:48:53 +01:00
REQUIRE(leapSeconds == 0);
result = Clock::setLeapSeconds(18);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-03-25 18:48:53 +01:00
result = Clock::getLeapSeconds(&leapSeconds);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-03-25 18:48:53 +01:00
REQUIRE(leapSeconds == 18);
}
SECTION("usec Test") {
timeval timeAsTimeval;
ReturnValue_t result = Clock::getClock_timeval(&timeAsTimeval);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-03-25 18:48:53 +01:00
uint64_t timeAsUsec = 0;
result = Clock::getClock_usecs(&timeAsUsec);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-03-25 18:48:53 +01:00
double timeAsUsecDouble = static_cast<double>(timeAsUsec) / 1000000.0;
timeval timeAsUsecTimeval = timevalOperations::toTimeval(timeAsUsecDouble);
2022-03-28 15:17:59 +02:00
double difference = timevalOperations::toDouble(timeAsUsecTimeval - timeAsTimeval);
2022-03-25 18:48:53 +01:00
// We accept 5 ms difference
2022-03-28 15:17:59 +02:00
CHECK(difference >= 0.0);
CHECK(difference <= 0.005);
2022-03-25 18:48:53 +01:00
uint64_t timevalAsUint64 = static_cast<uint64_t>(timeAsTimeval.tv_sec) * 1000000ull +
static_cast<uint64_t>(timeAsTimeval.tv_usec);
2022-03-28 15:17:59 +02:00
CHECK((timeAsUsec - timevalAsUint64) >= 0);
CHECK((timeAsUsec - timevalAsUint64) <= (5 * 1000));
2022-03-25 18:48:53 +01:00
}
SECTION("Test j2000") {
double j2000;
timeval time;
time.tv_sec = 1648208539;
time.tv_usec = 0;
ReturnValue_t result = Clock::convertTimevalToJD2000(time, &j2000);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-03-25 18:48:53 +01:00
double correctJ2000 = 2459663.98772 - 2451545.0;
CHECK(j2000 == Catch::Approx(correctJ2000).margin(1.2 * 1e-8));
}
SECTION("Convert to TT") {
timeval utcTime;
utcTime.tv_sec = 1648208539;
utcTime.tv_usec = 999000;
timeval tt;
ReturnValue_t result = Clock::setLeapSeconds(27);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-03-25 18:48:53 +01:00
result = Clock::convertUTCToTT(utcTime, &tt);
2022-08-16 01:08:26 +02:00
REQUIRE(result == returnvalue::OK);
2022-03-25 18:48:53 +01:00
CHECK(tt.tv_usec == 183000);
// The plus 1 is a own forced overflow of usecs
CHECK(tt.tv_sec == (1648208539 + 27 + 10 + 32 + 1));
}
}