4 #include <gtest/gtest.h> 20 static const int N = 1000;
22 for (
int i = 0; i < N; ++i)
25 for (
int j = i-1; j >= 0; --j)
27 EXPECT_NE(uu[i], uu[j]);
36 uint64_t hw_addr = 0xAABBCCDDEEFF;
42 std::vector<uint8_t> data(uu.size());
43 std::copy(uu.begin(), uu.end(), data.begin());
45 uint64_t timestamp = (
static_cast<uint64_t
>(data[0]) << 24) + (
static_cast<uint64_t
>(data[1]) << 16) +
46 (
static_cast<uint64_t
>(data[2]) << 8) +
static_cast<uint64_t
>(data[3]) +
47 (static_cast<uint64_t>(data[4]) << 40) + (static_cast<uint64_t>(data[5]) << 32) +
48 (static_cast<uint64_t>(data[6] & 0x0F) << 56) + (static_cast<uint64_t>(data[7]) << 48);
49 uint64_t offset = 122192928000000000;
50 uint64_t ns_intervals = timestamp - offset;
51 uint64_t uu_hw_addr = 0;
52 for (int8_t i=0; i < 6; i++)
54 uu_hw_addr +=
static_cast<uint64_t
>(data[uu.size() - 1 - i]) << 8*i;
58 uu_time.
sec =
static_cast<int32_t
>(ns_intervals / 1e9 * 100);
59 uu_time.
nsec =
static_cast<int32_t
>((ns_intervals -
static_cast<uint64_t
>(uu_time.
sec) / (100 * 1e-9)) * 100);
61 EXPECT_EQ(t.
sec, uu_time.
sec);
64 EXPECT_EQ(t.
nsec - uu_time.
nsec, 89);
65 EXPECT_EQ(hw_addr, uu_hw_addr);
68 TEST(BoostUUID, timeWithinSameInterval)
72 uint64_t hw_addr = 0xAABBCCDDEEFF;
79 std::vector<uint8_t> data(uu1.size());
80 std::copy(uu1.begin(), uu1.end(), data.begin());
82 uint64_t timestamp = (
static_cast<uint64_t
>(data[0]) << 24) + (
static_cast<uint64_t
>(data[1]) << 16) +
83 (
static_cast<uint64_t
>(data[2]) << 8) +
static_cast<uint64_t
>(data[3]) +
84 (static_cast<uint64_t>(data[4]) << 40) + (static_cast<uint64_t>(data[5]) << 32) +
85 (static_cast<uint64_t>(data[6] & 0x0F) << 56) + (static_cast<uint64_t>(data[7]) << 48);
86 uint64_t offset = 122192928000000000;
87 uint64_t ns_intervals = timestamp - offset;
90 uu1_time.
sec =
static_cast<int32_t
>(ns_intervals / 1e9 * 100);
91 uu1_time.
nsec =
static_cast<int32_t
>((ns_intervals -
static_cast<uint64_t
>(uu1_time.
sec) / (100 * 1e-9)) * 100);
94 std::copy(uu2.begin(), uu2.end(), data.begin());
96 timestamp = (
static_cast<uint64_t
>(data[0]) << 24) + (
static_cast<uint64_t
>(data[1]) << 16) +
97 (
static_cast<uint64_t
>(data[2]) << 8) +
static_cast<uint64_t
>(data[3]) +
98 (static_cast<uint64_t>(data[4]) << 40) + (static_cast<uint64_t>(data[5]) << 32) +
99 (static_cast<uint64_t>(data[6] & 0x0F) << 56) + (static_cast<uint64_t>(data[7]) << 48);
100 ns_intervals = timestamp - offset;
103 uu2_time.
sec =
static_cast<int32_t
>(ns_intervals / 1e9 * 100);
104 uu2_time.
nsec =
static_cast<int32_t
>((ns_intervals -
static_cast<uint64_t
>(uu2_time.
sec) / (100 * 1e-9)) * 100);
108 EXPECT_EQ(uu1_time.
sec, uu2_time.
sec);
109 EXPECT_EQ(uu1_time.
nsec, uu2_time.
nsec);
116 TEST(BoostUUID, timeWithinDifferentInterval)
120 uint64_t hw_addr = 0xAABBCCDDEEFF;
127 std::vector<uint8_t> data(uu1.size());
128 std::copy(uu1.begin(), uu1.end(), data.begin());
130 uint64_t timestamp = (
static_cast<uint64_t
>(data[0]) << 24) + (
static_cast<uint64_t
>(data[1]) << 16) +
131 (
static_cast<uint64_t
>(data[2]) << 8) +
static_cast<uint64_t
>(data[3]) +
132 (static_cast<uint64_t>(data[4]) << 40) + (static_cast<uint64_t>(data[5]) << 32) +
133 (static_cast<uint64_t>(data[6] & 0x0F) << 56) + (static_cast<uint64_t>(data[7]) << 48);
134 uint64_t offset = 122192928000000000;
135 uint64_t ns_intervals = timestamp - offset;
138 uu1_time.
sec =
static_cast<int32_t
>(ns_intervals / 1e9 * 100);
139 uu1_time.
nsec =
static_cast<int32_t
>((ns_intervals -
static_cast<uint64_t
>(uu1_time.
sec) / (100 * 1e-9)) * 100);
142 std::copy(uu2.begin(), uu2.end(), data.begin());
144 timestamp = (
static_cast<uint64_t
>(data[0]) << 24) + (
static_cast<uint64_t
>(data[1]) << 16) +
145 (
static_cast<uint64_t
>(data[2]) << 8) +
static_cast<uint64_t
>(data[3]) +
146 (static_cast<uint64_t>(data[4]) << 40) + (static_cast<uint64_t>(data[5]) << 32) +
147 (static_cast<uint64_t>(data[6] & 0x0F) << 56) + (static_cast<uint64_t>(data[7]) << 48);
148 ns_intervals = timestamp - offset;
151 uu2_time.
sec =
static_cast<int32_t
>(ns_intervals / 1e9 * 100);
152 uu2_time.
nsec =
static_cast<int32_t
>((ns_intervals -
static_cast<uint64_t
>(uu2_time.
sec) / (100 * 1e-9)) * 100);
156 EXPECT_EQ(uu1_time.
sec, uu2_time.
sec);
157 EXPECT_NE(uu1_time.
nsec, uu2_time.
nsec);
167 EXPECT_EQ(
toHexString(x),
"1b4db7eb-4057-5ddf-91e0-36dec72071f5");
172 std::string
s(
"http://openstreetmap.org/node/1");
177 EXPECT_EQ(
toHexString(x),
"ef362ac8-9659-5481-b954-88e9b741c8f9");
180 TEST(BoostUUID, differentOsmNamespace)
182 uuid x =
fromURL(
"http://openstreetmap.org/node/1");
183 uuid y =
fromURL(
"http://openstreetmap.org/way/1");
186 EXPECT_EQ(
toHexString(y),
"b3180681-b125-5e41-bd04-3c8b046175b4");
191 uuid x =
fromURL(
"http://openstreetmap.org/node/1");
192 uuid y =
fromURL(
"http://openstreetmap.org/node/152370223");
195 EXPECT_EQ(
toHexString(y),
"8e0b7d8a-c433-5c42-be2e-fbd97ddff9ac");
200 std::string
s(
"da7c242f-2efe-5175-9961-49cc621b80b9");
205 TEST(BoostUUID, fromStringNoDashes)
207 std::string
s(
"da7c242f-2efe-5175-9961-49cc621b80b9");
208 std::string s_hex(
"da7c242f2efe5175996149cc621b80b9");
213 TEST(BoostUUID, fromBracesString)
215 std::string
s(
"da7c242f-2efe-5175-9961-49cc621b80b9");
216 std::string s_braces =
"{" + s +
"}";
225 std::string
s(
"da7c242f-2efe-5175-9961-49cc621b80b9");
226 std::string s_urn =
"urn:uuid:" + s;
231 TEST(BoostUUID, fromTooLongString)
235 std::string
s(
"da7c242f-2efe-5175-9961-49cc621b80b9");
236 std::string s_too_long = s +
"-0001";
241 TEST(BoostUUID, fromTooShortString)
245 std::string
s(
"da7c242f-2efe-5175-9961-49cc621b80");
252 catch (std::runtime_error &e)
254 EXPECT_EQ(e.what(), std::string(
"invalid uuid string"));
262 TEST(BoostUUID, fromBogusString)
266 std::string
s(
"Invalid UUID string");
273 catch (std::runtime_error &e)
275 EXPECT_EQ(e.what(), std::string(
"invalid uuid string"));
287 EXPECT_EQ(x.uuid, y.uuid);
294 EXPECT_NE(x.uuid, y.uuid);
299 std::string
s(
"da7c242f-2efe-5175-9961-49cc621b80b9");
302 EXPECT_EQ(x.uuid, y.uuid);
308 std::string
s(
"da7c242f-2efe-5175-9961-49cc621b80b9");
316 std::string
s(
"da7c242f-2efe-5175-9961-49cc621b80b9");
323 int main(
int argc,
char **argv)
325 testing::InitGoogleTest(&argc, argv);
326 return RUN_ALL_TESTS();
static boost::uuids::uuid fromRandom(void)
Generate a random UUID object.
uuid_msgs::UniqueID UniqueID
static boost::uuids::uuid fromMsg(uuid_msgs::UniqueID const &msg)
Create UUID object from UniqueID message.
C++ namespace for unique_id helper functions.
TEST(BoostUUID, fromRandom)
int main(int argc, char **argv)
static boost::uuids::uuid fromTime(ros::Time timestamp, uint64_t hw_addr)
Generate a Time Based UUID object. Users are recommended to seed the random number generator using sr...
uuid_msgs::UniqueID UniqueID
static std::string toHexString(boost::uuids::uuid const &uu)
Get the canonical string representation for a boost UUID.
static boost::uuids::uuid fromURL(std::string const &url)
Generate UUID from Uniform Resource Identifier.
static uuid_msgs::UniqueID toMsg(boost::uuids::uuid const &uu)
Create a UniqueID message from a UUID object.
static boost::uuids::uuid fromHexString(std::string const &str)
Generate UUID from canonical hex string.
Helper functions for universally unique identifiers and messages.