Skip to content

Commit

Permalink
format access tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbusch committed Oct 9, 2024
1 parent c0955d6 commit d542112
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 70 deletions.
34 changes: 17 additions & 17 deletions etsi_its_msgs_utils/include/etsi_its_msgs_utils/impl/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ SOFTWARE.

namespace etsi_its_msgs {

const uint64_t UNIX_SECONDS_2004 = 1072915200; // Unix-Seconds for 2004-01-01T00:00:00.000Z
const uint64_t UNIX_SECONDS_2004 = 1072915200; // Unix-Seconds for 2004-01-01T00:00:00.000Z

/**
* @brief std::map that stores all leap second insertions since 2004 with the corresponding unix-date of the insertion
*
*/
const std::map<uint64_t, uint16_t> LEAP_SECOND_INSERTIONS_SINCE_2004 {
{UNIX_SECONDS_2004, 0},
{1136073599, 1}, // 2005-12-31T23:59:59.000Z
{1230767999, 2}, // 2008-12-31T23:59:59.000Z
{1341100799, 3}, // 2012-06-30T23:59:59.000Z
{1435708799, 4}, // 2015-06-30T23:59:59.000Z
{1483228799, 5} // 2016-12-31T23:59:59.000Z
const std::map<uint64_t, uint16_t> LEAP_SECOND_INSERTIONS_SINCE_2004{
{UNIX_SECONDS_2004, 0}, // 2004-01-01T00:00:00.000Z
{1136073599, 1}, // 2005-12-31T23:59:59.000Z
{1230767999, 2}, // 2008-12-31T23:59:59.000Z
{1341100799, 3}, // 2012-06-30T23:59:59.000Z
{1435708799, 4}, // 2015-06-30T23:59:59.000Z
{1483228799, 5} // 2016-12-31T23:59:59.000Z
};

/**
Expand All @@ -57,14 +57,14 @@ const std::map<uint64_t, uint16_t> LEAP_SECOND_INSERTIONS_SINCE_2004 {
* @param unix_seconds the current unix seconds for that the leap second insertions since 2004 shall be provided
* @return uint16_t the number of leap second insertions since 2004 for unix_seconds
*/
inline uint16_t getLeapSecondInsertionsSince2004(const uint64_t unix_seconds)
{
// Check if the map is empty
if (LEAP_SECOND_INSERTIONS_SINCE_2004.empty()) return 0;
auto it = LEAP_SECOND_INSERTIONS_SINCE_2004.upper_bound(unix_seconds); // Find the first element greater than givenUnixSecond
if (it == LEAP_SECOND_INSERTIONS_SINCE_2004.begin()) return 0;
--it; // Move iterator to the element with a key less than or equal to givenUnixSecond
return it->second; // Return the corresponding value
inline uint16_t getLeapSecondInsertionsSince2004(const uint64_t unix_seconds) {
// Check if the map is empty
if (LEAP_SECOND_INSERTIONS_SINCE_2004.empty()) return 0;
auto it = LEAP_SECOND_INSERTIONS_SINCE_2004.upper_bound(
unix_seconds); // Find the first element greater than givenUnixSecond
if (it == LEAP_SECOND_INSERTIONS_SINCE_2004.begin()) return 0;
--it; // Move iterator to the element with a key less than or equal to givenUnixSecond
return it->second; // Return the corresponding value
}

} // namespace etsi_its_msgs
} // namespace etsi_its_msgs
36 changes: 21 additions & 15 deletions etsi_its_msgs_utils/test/impl/test_cam_access.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <cmath>
#include <gtest/gtest.h>
#include <cmath>

namespace cam_access = etsi_its_cam_msgs::access;

TEST(etsi_its_cam_msgs, test_set_get_cam) {

cam_msgs::CAM cam;

int station_id = randomInt(cam_msgs::StationID::MIN,cam_msgs::StationID::MAX);
int protocol_version = randomInt(cam_msgs::ItsPduHeader::PROTOCOL_VERSION_MIN,cam_msgs::ItsPduHeader::PROTOCOL_VERSION_MAX);
int station_id = randomInt(cam_msgs::StationID::MIN, cam_msgs::StationID::MAX);
int protocol_version =
randomInt(cam_msgs::ItsPduHeader::PROTOCOL_VERSION_MIN, cam_msgs::ItsPduHeader::PROTOCOL_VERSION_MAX);
cam_access::setItsPduHeader(cam, station_id, protocol_version);
EXPECT_EQ(cam_msgs::ItsPduHeader::MESSAGE_ID_CAM, cam.header.message_id);
EXPECT_EQ(protocol_version, cam.header.protocol_version);
Expand All @@ -19,19 +19,24 @@ TEST(etsi_its_cam_msgs, test_set_get_cam) {
// The value for TimestampIts for 2007-01-01T00:00:00.000Z is
// 94694401000 milliseconds, which includes one leap second insertion
// since 2004-01-01T00:00:00.000Z.
uint64_t t_2007 = ((uint64_t)1167609600)*1e9;
uint64_t t_2007 = ((uint64_t)1167609600) * 1e9;
cam_msgs::TimestampIts t_its;
EXPECT_EQ(1, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9));
cam_access::setTimestampITS(t_its, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9));
EXPECT_EQ(1, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9));
cam_access::setTimestampITS(t_its, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9));
EXPECT_EQ(94694401000, t_its.value);
cam_access::setGenerationDeltaTime(cam, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9));
EXPECT_EQ(94694401000%65536, cam_access::getGenerationDeltaTimeValue(cam));
cam_access::setGenerationDeltaTime(cam, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9));
EXPECT_EQ(94694401000 % 65536, cam_access::getGenerationDeltaTimeValue(cam));
cam_msgs::TimestampIts t_its2;
uint64_t t_2007_off = t_2007 + 5*1e9;
cam_access::setTimestampITS(t_its2, t_2007_off, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9));
EXPECT_EQ(94694401000, cam_access::getTimestampITSFromGenerationDeltaTime(cam_access::getGenerationDeltaTime(cam), t_its2).value);
EXPECT_EQ(t_2007, cam_access::getUnixNanosecondsFromGenerationDeltaTime(cam_access::getGenerationDeltaTime(cam), t_its2, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9)));
EXPECT_EQ(t_2007, cam_access::getUnixNanosecondsFromGenerationDeltaTime(cam_access::getGenerationDeltaTime(cam), t_2007_off, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9)));
uint64_t t_2007_off = t_2007 + 5 * 1e9;
cam_access::setTimestampITS(t_its2, t_2007_off, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9));
EXPECT_EQ(94694401000,
cam_access::getTimestampITSFromGenerationDeltaTime(cam_access::getGenerationDeltaTime(cam), t_its2).value);
EXPECT_EQ(t_2007, cam_access::getUnixNanosecondsFromGenerationDeltaTime(
cam_access::getGenerationDeltaTime(cam), t_its2,
etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9)));
EXPECT_EQ(t_2007, cam_access::getUnixNanosecondsFromGenerationDeltaTime(
cam_access::getGenerationDeltaTime(cam), t_2007_off,
etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9)));

int stationType_val = randomInt(cam_msgs::StationType::MIN, cam_msgs::StationType::MAX);
cam_access::setStationType(cam, stationType_val);
Expand Down Expand Up @@ -94,7 +99,8 @@ TEST(etsi_its_cam_msgs, test_set_get_cam) {
exterior_lights.at(i) = randomInt(0, 1);
}
cam.cam.cam_parameters.low_frequency_container_is_present = true;
cam.cam.cam_parameters.low_frequency_container.choice = cam_msgs::LowFrequencyContainer::CHOICE_BASIC_VEHICLE_CONTAINER_LOW_FREQUENCY;
cam.cam.cam_parameters.low_frequency_container.choice =
cam_msgs::LowFrequencyContainer::CHOICE_BASIC_VEHICLE_CONTAINER_LOW_FREQUENCY;
cam_access::setExteriorLights(cam, exterior_lights);
EXPECT_EQ(exterior_lights, cam_access::getExteriorLights(cam));
}
36 changes: 21 additions & 15 deletions etsi_its_msgs_utils/test/impl/test_cam_ts_access.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include <cmath>
#include <gtest/gtest.h>
#include <cmath>

namespace cam_ts_access = etsi_its_cam_ts_msgs::access;

TEST(etsi_its_cam_ts_msgs, test_set_get_cam) {

cam_ts_msgs::CAM cam;

int station_id = randomInt(cam_ts_msgs::StationId::MIN,cam_ts_msgs::StationId::MAX);
int protocol_version = randomInt(cam_ts_msgs::OrdinalNumber1B::MIN,cam_ts_msgs::OrdinalNumber1B::MAX);
int station_id = randomInt(cam_ts_msgs::StationId::MIN, cam_ts_msgs::StationId::MAX);
int protocol_version = randomInt(cam_ts_msgs::OrdinalNumber1B::MIN, cam_ts_msgs::OrdinalNumber1B::MAX);
cam_ts_access::setItsPduHeader(cam, station_id, protocol_version);
EXPECT_EQ(cam_ts_msgs::MessageId::CAM, cam.header.message_id.value);
EXPECT_EQ(protocol_version, cam.header.protocol_version.value);
Expand All @@ -19,19 +18,25 @@ TEST(etsi_its_cam_ts_msgs, test_set_get_cam) {
// The value for TimestampIts for 2007-01-01T00:00:00.000Z is
// 94694401000 milliseconds, which includes one leap second insertion
// since 2004-01-01T00:00:00.000Z.
uint64_t t_2007 = ((uint64_t)1167609600)*1e9;
uint64_t t_2007 = ((uint64_t)1167609600) * 1e9;
cam_ts_msgs::TimestampIts t_its;
EXPECT_EQ(1, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9));
cam_ts_access::setTimestampITS(t_its, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9));
EXPECT_EQ(1, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9));
cam_ts_access::setTimestampITS(t_its, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9));
EXPECT_EQ(94694401000, t_its.value);
cam_ts_access::setGenerationDeltaTime(cam, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9));
EXPECT_EQ(94694401000%65536, cam_ts_access::getGenerationDeltaTimeValue(cam));
cam_ts_access::setGenerationDeltaTime(cam, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9));
EXPECT_EQ(94694401000 % 65536, cam_ts_access::getGenerationDeltaTimeValue(cam));
cam_ts_msgs::TimestampIts t_its2;
uint64_t t_2007_off = t_2007 + 5*1e9;
cam_ts_access::setTimestampITS(t_its2, t_2007_off, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9));
EXPECT_EQ(94694401000, cam_ts_access::getTimestampITSFromGenerationDeltaTime(cam_ts_access::getGenerationDeltaTime(cam), t_its2).value);
EXPECT_EQ(t_2007, cam_ts_access::getUnixNanosecondsFromGenerationDeltaTime(cam_ts_access::getGenerationDeltaTime(cam), t_its2, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9)));
EXPECT_EQ(t_2007, cam_ts_access::getUnixNanosecondsFromGenerationDeltaTime(cam_ts_access::getGenerationDeltaTime(cam), t_2007_off, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9)));
uint64_t t_2007_off = t_2007 + 5 * 1e9;
cam_ts_access::setTimestampITS(t_its2, t_2007_off, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9));
EXPECT_EQ(
94694401000,
cam_ts_access::getTimestampITSFromGenerationDeltaTime(cam_ts_access::getGenerationDeltaTime(cam), t_its2).value);
EXPECT_EQ(t_2007, cam_ts_access::getUnixNanosecondsFromGenerationDeltaTime(
cam_ts_access::getGenerationDeltaTime(cam), t_its2,
etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9)));
EXPECT_EQ(t_2007, cam_ts_access::getUnixNanosecondsFromGenerationDeltaTime(
cam_ts_access::getGenerationDeltaTime(cam), t_2007_off,
etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9)));

int stationType_val = randomInt(cam_ts_msgs::TrafficParticipantType::MIN, cam_ts_msgs::TrafficParticipantType::MAX);
cam_ts_access::setStationType(cam, stationType_val);
Expand Down Expand Up @@ -94,7 +99,8 @@ TEST(etsi_its_cam_ts_msgs, test_set_get_cam) {
exterior_lights.at(i) = randomInt(0, 1);
}
cam.cam.cam_parameters.low_frequency_container_is_present = true;
cam.cam.cam_parameters.low_frequency_container.choice = cam_ts_msgs::LowFrequencyContainer::CHOICE_BASIC_VEHICLE_CONTAINER_LOW_FREQUENCY;
cam.cam.cam_parameters.low_frequency_container.choice =
cam_ts_msgs::LowFrequencyContainer::CHOICE_BASIC_VEHICLE_CONTAINER_LOW_FREQUENCY;
cam_ts_access::setExteriorLights(cam, exterior_lights);
EXPECT_EQ(exterior_lights, cam_ts_access::getExteriorLights(cam));
}
18 changes: 8 additions & 10 deletions etsi_its_msgs_utils/test/impl/test_cpm_ts_access.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include <cmath>
#include <gtest/gtest.h>
#include <cmath>

namespace cpm_ts_access = etsi_its_cpm_ts_msgs::access;

TEST(etsi_its_cpm_ts_msgs, test_set_get_cpm) {

cpm_ts_msgs::CollectivePerceptionMessage cpm;

int station_id = randomInt(cpm_ts_msgs::StationId::MIN,cpm_ts_msgs::StationId::MAX);
int protocol_version = randomInt(cpm_ts_msgs::OrdinalNumber1B::MIN,cpm_ts_msgs::OrdinalNumber1B::MAX);
int station_id = randomInt(cpm_ts_msgs::StationId::MIN, cpm_ts_msgs::StationId::MAX);
int protocol_version = randomInt(cpm_ts_msgs::OrdinalNumber1B::MIN, cpm_ts_msgs::OrdinalNumber1B::MAX);
cpm_ts_access::setItsPduHeader(cpm, station_id, protocol_version);
EXPECT_EQ(cpm_ts_msgs::MessageId::CPM, cpm.header.message_id.value);
EXPECT_EQ(protocol_version, cpm.header.protocol_version.value);
Expand All @@ -19,13 +18,13 @@ TEST(etsi_its_cpm_ts_msgs, test_set_get_cpm) {
// The value for TimestampIts for 2007-01-01T00:00:00.000Z is
// 94694401000 milliseconds, which includes one leap second insertion
// since 2004-01-01T00:00:00.000Z.
uint64_t t_2007 = ((uint64_t)1167609600)*1e9;

uint64_t t_2007 = ((uint64_t)1167609600) * 1e9;
cpm_ts_msgs::TimestampIts t_its;
EXPECT_EQ(1, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9));
cpm_ts_access::setTimestampITS(t_its, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9));
EXPECT_EQ(1, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9));
cpm_ts_access::setTimestampITS(t_its, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9));
EXPECT_EQ(94694401000, t_its.value);
cpm_ts_access::setReferenceTime(cpm, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007*1e-9));
cpm_ts_access::setReferenceTime(cpm, t_2007, etsi_its_msgs::getLeapSecondInsertionsSince2004(t_2007 * 1e-9));
EXPECT_EQ(94694401000, cpm_ts_access::getReferenceTimeValue(cpm));
EXPECT_EQ(t_2007, cpm_ts_access::getUnixNanosecondsFromReferenceTime(cpm_ts_access::getReferenceTime(cpm)));

Expand Down Expand Up @@ -87,5 +86,4 @@ TEST(etsi_its_cpm_ts_msgs, test_set_get_cpm) {
EXPECT_NEAR(acceleration.x, cpm_ts_access::getCartesianAccelerationOfPerceivedObject(object).x, 1e-1);
EXPECT_NEAR(acceleration.y, cpm_ts_access::getCartesianAccelerationOfPerceivedObject(object).y, 1e-1);
EXPECT_NEAR(acceleration.z, cpm_ts_access::getCartesianAccelerationOfPerceivedObject(object).z, 1e-1);

}
14 changes: 7 additions & 7 deletions etsi_its_msgs_utils/test/impl/test_denm_access.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <cmath>
#include <gtest/gtest.h>
#include <cmath>

namespace denm_access = etsi_its_denm_msgs::access;

TEST(etsi_its_denm_msgs, test_set_get_denm) {

denm_msgs::DENM denm;

int station_id = randomInt(denm_msgs::StationID::MIN,denm_msgs::StationID::MAX);
int protocol_version = randomInt(denm_msgs::ItsPduHeader::PROTOCOL_VERSION_MIN,denm_msgs::ItsPduHeader::PROTOCOL_VERSION_MAX);
int station_id = randomInt(denm_msgs::StationID::MIN, denm_msgs::StationID::MAX);
int protocol_version =
randomInt(denm_msgs::ItsPduHeader::PROTOCOL_VERSION_MIN, denm_msgs::ItsPduHeader::PROTOCOL_VERSION_MAX);
denm_access::setItsPduHeader(denm, station_id, protocol_version);
EXPECT_EQ(denm_msgs::ItsPduHeader::MESSAGE_ID_DENM, denm.header.message_id);
EXPECT_EQ(protocol_version, denm.header.protocol_version);
Expand All @@ -19,14 +19,14 @@ TEST(etsi_its_denm_msgs, test_set_get_denm) {
// The value for TimestampIts for 2007-01-01T00:00:00.000Z is
// 94694401000 milliseconds, which includes one leap second insertion
// since 2004-01-01T00:00:00.000Z.
uint64_t t_2007 = ((uint64_t)1167609600)*1e9;
uint64_t t_2007 = ((uint64_t)1167609600) * 1e9;
denm_msgs::TimestampIts t_its;
denm_access::setTimestampITS(t_its, t_2007, 1);
EXPECT_EQ(94694401000, t_its.value);

denm_access::setReferenceTime(denm, t_2007, 1);
EXPECT_EQ(94694401000, denm_access::getReferenceTimeValue(denm));
uint64_t t_2007_off = t_2007 + 5*1e9;
uint64_t t_2007_off = t_2007 + 5 * 1e9;
EXPECT_EQ(t_2007, denm_access::getUnixNanosecondsFromReferenceTime(denm_access::getReferenceTime(denm)));

int stationType_val = randomInt(denm_msgs::StationType::MIN, denm_msgs::StationType::MAX);
Expand Down
4 changes: 1 addition & 3 deletions etsi_its_msgs_utils/test/test_access.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

#include <gtest/gtest.h>
#include <cmath>
#include <random>
#include <gtest/gtest.h>

#include <etsi_its_cam_msgs/CAM.h>
#include <etsi_its_msgs_utils/cam_access.h>
Expand Down Expand Up @@ -39,7 +38,6 @@ namespace cpm_ts_msgs = etsi_its_cpm_ts_msgs;
namespace denm_msgs = etsi_its_denm_msgs;
#include <impl/test_denm_access.cpp>


int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down
4 changes: 1 addition & 3 deletions etsi_its_msgs_utils/test/test_access.ros2.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

#include <gtest/gtest.h>
#include <cmath>
#include <random>
#include <gtest/gtest.h>

#include <etsi_its_cam_msgs/msg/cam.hpp>
#include <etsi_its_msgs_utils/cam_access.hpp>
Expand Down Expand Up @@ -39,7 +38,6 @@ namespace cpm_ts_msgs = etsi_its_cpm_ts_msgs::msg;
namespace denm_msgs = etsi_its_denm_msgs::msg;
#include <impl/test_denm_access.cpp>


int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down

0 comments on commit d542112

Please sign in to comment.