second_test_unique_id.cpp
Go to the documentation of this file.
1 //
2 // A second C++ unit test for unique_id interface, which must be able
3 // to be included in more than one source file.
4 //
5 
6 #include <gtest/gtest.h>
7 
8 #include <unique_id/unique_id.h>
9 
10 using namespace unique_id;
13 
15 // Test cases
17 
18 // Test random generator
19 TEST(BoostUUID2, fromRandom)
20 {
21  static const int N = 1000;
22  uuid uu[N];
23  for (int i = 0; i < N; ++i)
24  {
25  uu[i] = fromRandom();
26  for (int j = i-1; j >= 0; --j)
27  {
28  EXPECT_NE(uu[i], uu[j]);
29  }
30  }
31 }
32 
33 TEST(BoostUUID2, emptyURL)
34 {
35  std::string s;
36  uuid x = fromURL(s);
37  uuid y = fromURL(s);
38  EXPECT_EQ(x, y);
39  // MUST yield same result as Python fromURL() function:
40  EXPECT_EQ(toHexString(x), "1b4db7eb-4057-5ddf-91e0-36dec72071f5");
41 }
42 
43 TEST(BoostUUID2, sameURL)
44 {
45  std::string s("http://openstreetmap.org/node/1");
46  uuid x = fromURL(s);
47  uuid y = fromURL(s);
48  EXPECT_EQ(x, y);
49  // MUST yield same result as Python fromURL() function:
50  EXPECT_EQ(toHexString(x), "ef362ac8-9659-5481-b954-88e9b741c8f9");
51 }
52 
53 TEST(BoostUUID2, differentOsmNamespace)
54 {
55  uuid x = fromURL("http://openstreetmap.org/node/1");
56  uuid y = fromURL("http://openstreetmap.org/way/1");
57  EXPECT_NE(x, y);
58  // MUST yield same result as Python fromURL() function:
59  EXPECT_EQ(toHexString(y), "b3180681-b125-5e41-bd04-3c8b046175b4");
60 }
61 
62 TEST(BoostUUID2, actualOsmNode)
63 {
64  uuid x = fromURL("http://openstreetmap.org/node/1");
65  uuid y = fromURL("http://openstreetmap.org/node/152370223");
66  EXPECT_NE(x, y);
67  // MUST yield same result as Python fromURL() function:
68  EXPECT_EQ(toHexString(y), "8e0b7d8a-c433-5c42-be2e-fbd97ddff9ac");
69 }
70 
71 TEST(BoostUUID2, fromHexString)
72 {
73  std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
74  std::string r = toHexString(fromHexString(s));
75  EXPECT_EQ(s, r);
76 }
77 
78 TEST(BoostUUID2, fromStringNoDashes)
79 {
80  std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
81  std::string s_hex("da7c242f2efe5175996149cc621b80b9");
82  std::string r = toHexString(fromHexString(s_hex));
83  EXPECT_EQ(s, r);
84 }
85 
86 TEST(BoostUUID2, fromBracesString)
87 {
88  std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
89  std::string s_braces = "{" + s + "}";
90  std::string r = toHexString(fromHexString(s_braces));
91  EXPECT_EQ(s, r);
92 }
93 
94 TEST(BoostUUID2, fromUrnString)
95 {
96  // This documents boost 1.46.1 behavior, but is an undefined
97  // fromHexString() input, not really a valid test case.
98  std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
99  std::string s_urn = "urn:uuid:" + s;
100  std::string r = toHexString(fromHexString(s_urn));
101  EXPECT_NE(s, r);
102 }
103 
104 TEST(BoostUUID2, fromTooLongString)
105 {
106  // This documents boost 1.46.1 behavior, but is an undefined
107  // fromHexString() input, not really a valid test case.
108  std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
109  std::string s_too_long = s + "-0001";
110  std::string r = toHexString(fromHexString(s_too_long));
111  EXPECT_EQ(s, r);
112 }
113 
114 TEST(BoostUUID2, fromTooShortString)
115 {
116  // This documents boost 1.46.1 behavior, but is an undefined
117  // fromHexString() input, not really a valid test case.
118  std::string s("da7c242f-2efe-5175-9961-49cc621b80");
119  try
120  {
121  uuid x = fromHexString(s);
122  FAIL(); // expected exception not thrown
123  EXPECT_NE(toHexString(x), s);
124  }
125  catch (std::runtime_error &e)
126  {
127  EXPECT_EQ(e.what(), std::string("invalid uuid string"));
128  }
129  catch (...)
130  {
131  FAIL(); // unexpected exception
132  }
133 }
134 
135 TEST(BoostUUID2, fromBogusString)
136 {
137  // This documents boost 1.46.1 behavior, but is an undefined
138  // fromHexString() input, not really a valid test case.
139  std::string s("Invalid UUID string");
140  try
141  {
142  uuid x = fromHexString(s);
143  FAIL(); // expected exception not thrown
144  EXPECT_NE(toHexString(x), s);
145  }
146  catch (std::runtime_error &e)
147  {
148  EXPECT_EQ(e.what(), std::string("invalid uuid string"));
149  }
150  catch (...)
151  {
152  FAIL(); // unexpected exception
153  }
154 }
155 
156 TEST(UniqueID2, nilMessage)
157 {
158  UniqueID x;
159  UniqueID y = toMsg(uuid());
160  EXPECT_EQ(x.uuid, y.uuid);
161 }
162 
163 TEST(UniqueID2, randomMessage)
164 {
165  UniqueID x;
166  UniqueID y = toMsg(fromRandom());
167  EXPECT_NE(x.uuid, y.uuid);
168 }
169 
170 TEST(UniqueID2, equivalentMessages)
171 {
172  std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
173  UniqueID x = toMsg(fromHexString(s));
174  UniqueID y = toMsg(fromHexString(s));
175  EXPECT_EQ(x.uuid, y.uuid);
176  EXPECT_EQ(s, toHexString(y));
177 }
178 
179 TEST(UniqueID2, toAndFromMessage)
180 {
181  std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
182  uuid x = uuid(fromHexString(s));
183  uuid y = fromMsg(toMsg(x));
184  EXPECT_EQ(x, y);
185 }
186 
187 TEST(UniqueID2, messageToString)
188 {
189  std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
190  UniqueID x = toMsg(fromHexString(s));
191  std::string y = toHexString(x);
192  EXPECT_EQ(s, y);
193 }
static boost::uuids::uuid fromRandom(void)
Generate a random UUID object.
Definition: unique_id.h:100
static boost::uuids::uuid fromMsg(uuid_msgs::UniqueID const &msg)
Create UUID object from UniqueID message.
Definition: unique_id.h:85
C++ namespace for unique_id helper functions.
XmlRpcServer s
uuid_msgs::UniqueID UniqueID
static std::string toHexString(boost::uuids::uuid const &uu)
Get the canonical string representation for a boost UUID.
Definition: unique_id.h:192
boost::uuids::uuid uuid
static boost::uuids::uuid fromURL(std::string const &url)
Generate UUID from Uniform Resource Identifier.
Definition: unique_id.h:151
static uuid_msgs::UniqueID toMsg(boost::uuids::uuid const &uu)
Create a UniqueID message from a UUID object.
Definition: unique_id.h:177
TEST(BoostUUID2, fromRandom)
static boost::uuids::uuid fromHexString(std::string const &str)
Generate UUID from canonical hex string.
Definition: unique_id.h:125
Helper functions for universally unique identifiers and messages.


unique_id
Author(s): Jack O'Quin
autogenerated on Mon Mar 6 2023 03:18:07