|
15 | 15 | #include <gtest/gtest.h> |
16 | 16 |
|
17 | 17 | #include <limits> |
| 18 | +#include <random> |
18 | 19 | #include "rclcpp_action/types.hpp" |
19 | 20 |
|
20 | 21 | TEST(TestActionTypes, goal_uuid_to_string) { |
@@ -59,3 +60,35 @@ TEST(TestActionTypes, rcl_action_goal_info_to_goal_uuid) { |
59 | 60 | EXPECT_EQ(goal_info.goal_id.uuid[i], goal_id[i]); |
60 | 61 | } |
61 | 62 | } |
| 63 | + |
| 64 | +TEST(TestActionTypes, goal_uuid_to_hashed_uuid_random) { |
| 65 | + // Use std::random_device to seed the generator of goal IDs. |
| 66 | + std::random_device rd; |
| 67 | + std::independent_bits_engine< |
| 68 | + std::default_random_engine, 8, decltype(rd())> random_bytes_generator(rd()); |
| 69 | + |
| 70 | + std::vector<size_t> hashed_guuids; |
| 71 | + constexpr size_t iterations = 1000; |
| 72 | + |
| 73 | + for (size_t i = 0; i < iterations; i++) { |
| 74 | + rclcpp_action::GoalUUID goal_id; |
| 75 | + |
| 76 | + // Generate random bytes for each element of the array |
| 77 | + for (auto & element : goal_id) { |
| 78 | + element = static_cast<uint8_t>(random_bytes_generator()); |
| 79 | + } |
| 80 | + |
| 81 | + size_t new_hashed_guuid = std::hash<rclcpp_action::GoalUUID>()(goal_id); |
| 82 | + |
| 83 | + // Search for any prevoius hashed goal_id with the same value |
| 84 | + for (auto prev_hashed_guuid : hashed_guuids) { |
| 85 | + EXPECT_NE(prev_hashed_guuid, new_hashed_guuid); |
| 86 | + if (prev_hashed_guuid == new_hashed_guuid) { |
| 87 | + // Fail before the first occurrence of a collision |
| 88 | + GTEST_FAIL(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + hashed_guuids.push_back(new_hashed_guuid); |
| 93 | + } |
| 94 | +} |
0 commit comments