This repository was archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(autoware_utils): add functions to generate and convert uuids (#238)
Signed-off-by: Alexey Panferov <lexavtanke@gmail.com>
- Loading branch information
1 parent
119ea82
commit 7c55915
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2022 Tier IV, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef AUTOWARE_UTILS__ROS__UUID_HELPER_HPP_ | ||
#define AUTOWARE_UTILS__ROS__UUID_HELPER_HPP_ | ||
|
||
#include <unique_identifier_msgs/msg/uuid.hpp> | ||
|
||
#include <boost/uuid/uuid.hpp> | ||
|
||
#include <algorithm> | ||
#include <random> | ||
#include <string> | ||
|
||
namespace autoware_utils | ||
{ | ||
inline unique_identifier_msgs::msg::UUID generate_uuid() | ||
{ | ||
// Generate random number | ||
unique_identifier_msgs::msg::UUID uuid; | ||
std::mt19937 gen(std::random_device{}()); | ||
std::independent_bits_engine<std::mt19937, 8, uint8_t> bit_eng(gen); | ||
std::generate(uuid.uuid.begin(), uuid.uuid.end(), bit_eng); | ||
|
||
return uuid; | ||
} | ||
inline unique_identifier_msgs::msg::UUID generate_default_uuid() | ||
{ | ||
// Generate UUID with all zeros | ||
unique_identifier_msgs::msg::UUID default_uuid; | ||
// Use std::generate to fill the UUID with zeros | ||
std::generate(default_uuid.uuid.begin(), default_uuid.uuid.end(), []() { return 0; }); | ||
|
||
return default_uuid; | ||
} | ||
inline std::string to_hex_string(const unique_identifier_msgs::msg::UUID & id) | ||
{ | ||
std::stringstream ss; | ||
for (auto i = 0; i < 16; ++i) { | ||
ss << std::hex << std::setfill('0') << std::setw(2) << +id.uuid[i]; | ||
} | ||
return ss.str(); | ||
} | ||
|
||
inline boost::uuids::uuid to_boost_uuid(const unique_identifier_msgs::msg::UUID & id) | ||
{ | ||
boost::uuids::uuid boost_uuid{}; | ||
std::copy(id.uuid.begin(), id.uuid.end(), boost_uuid.begin()); | ||
return boost_uuid; | ||
} | ||
|
||
inline unique_identifier_msgs::msg::UUID to_uuid_msg(const boost::uuids::uuid & id) | ||
{ | ||
unique_identifier_msgs::msg::UUID ros_uuid{}; | ||
std::copy(id.begin(), id.end(), ros_uuid.uuid.begin()); | ||
return ros_uuid; | ||
} | ||
|
||
} // namespace autoware_utils | ||
|
||
#endif // AUTOWARE_UTILS__ROS__UUID_HELPER_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2024 TIER IV, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "autoware_utils/ros/uuid_helper.hpp" | ||
|
||
#include <boost/uuid/uuid_generators.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(UUIDHelperTest, generate_uuid) | ||
{ | ||
// Generate two UUIDs and ensure they are all different | ||
|
||
unique_identifier_msgs::msg::UUID uuid1 = autoware_utils::generate_uuid(); | ||
unique_identifier_msgs::msg::UUID uuid2 = autoware_utils::generate_uuid(); | ||
|
||
EXPECT_FALSE(uuid1 == uuid2) << "Duplicate UUID generated: " | ||
<< autoware_utils::to_hex_string(uuid2); | ||
} | ||
|
||
TEST(UUIDHelperTest, generate_default_uuid) | ||
{ | ||
// Generate two UUIDs and ensure they are all different | ||
|
||
unique_identifier_msgs::msg::UUID default_uuid = autoware_utils::generate_default_uuid(); | ||
unique_identifier_msgs::msg::UUID zero_uuid; | ||
std::fill(zero_uuid.uuid.begin(), zero_uuid.uuid.end(), 0x00); | ||
|
||
EXPECT_EQ(default_uuid, zero_uuid); | ||
} | ||
|
||
TEST(UUIDHelperTest, to_hex_string) | ||
{ | ||
unique_identifier_msgs::msg::UUID uuid; | ||
// Populate the UUID with some values | ||
std::fill(uuid.uuid.begin(), uuid.uuid.end(), 0x42); | ||
|
||
std::string hex_string = autoware_utils::to_hex_string(uuid); | ||
|
||
// Check if the generated hex string is correct | ||
EXPECT_EQ(hex_string, "42424242424242424242424242424242"); | ||
} | ||
|
||
TEST(UUIDHelperTest, to_boost_uuid) | ||
{ | ||
unique_identifier_msgs::msg::UUID uuid; | ||
// Populate the UUID with some values | ||
std::fill(uuid.uuid.begin(), uuid.uuid.end(), 0x42); | ||
|
||
boost::uuids::uuid boost_uuid{}; | ||
std::fill(boost_uuid.begin(), boost_uuid.end(), 0x42); | ||
|
||
// Check if the conversion from ROS UUID to Boost UUID is correct | ||
EXPECT_TRUE(boost_uuid == autoware_utils::to_boost_uuid(uuid)); | ||
} | ||
|
||
TEST(UUIDHelperTest, to_uuid_msg) | ||
{ | ||
boost::uuids::random_generator generator; | ||
boost::uuids::uuid boost_uuid = generator(); | ||
unique_identifier_msgs::msg::UUID ros_uuid = autoware_utils::to_uuid_msg(boost_uuid); | ||
|
||
// Check if the conversion from Boost UUID to ROS UUID is correct | ||
EXPECT_TRUE(std::equal(boost_uuid.begin(), boost_uuid.end(), ros_uuid.uuid.begin())); | ||
} |