test_unique_id.cpp
Go to the documentation of this file.
00001 //
00002 // C++ unit tests for unique_id interface.
00003 //
00004 
00005 #include <gtest/gtest.h>
00006 
00007 #include <unique_id/unique_id.h>
00008 using namespace unique_id;
00009 typedef boost::uuids::uuid uuid;
00010 typedef uuid_msgs::UniqueID UniqueID;
00011 
00013 // Test cases
00015 
00016 // Test random generator
00017 TEST(BoostUUID, fromRandom)
00018 {
00019   static const int N = 1000;
00020   uuid uu[N];
00021   for (int i = 0; i < N; ++i)
00022     {
00023       uu[i] = fromRandom();
00024       for (int j = i-1; j >= 0; --j)
00025         {
00026           EXPECT_NE(uu[i], uu[j]);
00027         }
00028     }
00029 }
00030 
00031 TEST(BoostUUID, emptyURL)
00032 {
00033   std::string s;
00034   uuid x = fromURL(s);
00035   uuid y = fromURL(s);
00036   EXPECT_EQ(x, y);
00037   // MUST yield same result as Python fromURL() function:
00038   EXPECT_EQ(toHexString(x), "1b4db7eb-4057-5ddf-91e0-36dec72071f5");
00039 }
00040 
00041 TEST(BoostUUID, sameURL)
00042 {
00043   std::string s("http://openstreetmap.org/node/1");
00044   uuid x = fromURL(s);
00045   uuid y = fromURL(s);
00046   EXPECT_EQ(x, y);
00047   // MUST yield same result as Python fromURL() function:
00048   EXPECT_EQ(toHexString(x), "ef362ac8-9659-5481-b954-88e9b741c8f9");
00049 }
00050 
00051 TEST(BoostUUID, differentOsmNamespace)
00052 {
00053   uuid x = fromURL("http://openstreetmap.org/node/1");
00054   uuid y = fromURL("http://openstreetmap.org/way/1");
00055   EXPECT_NE(x, y);
00056   // MUST yield same result as Python fromURL() function:
00057   EXPECT_EQ(toHexString(y), "b3180681-b125-5e41-bd04-3c8b046175b4");
00058 }
00059 
00060 TEST(BoostUUID, actualOsmNode)
00061 {
00062   uuid x = fromURL("http://openstreetmap.org/node/1");
00063   uuid y = fromURL("http://openstreetmap.org/node/152370223");
00064   EXPECT_NE(x, y);
00065   // MUST yield same result as Python fromURL() function:
00066   EXPECT_EQ(toHexString(y), "8e0b7d8a-c433-5c42-be2e-fbd97ddff9ac");
00067 }
00068 
00069 TEST(BoostUUID, fromHexString)
00070 {
00071   std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
00072   std::string r = toHexString(fromHexString(s));
00073   EXPECT_EQ(s, r);
00074 }
00075 
00076 TEST(BoostUUID, fromStringNoDashes)
00077 {
00078   std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
00079   std::string s_hex("da7c242f2efe5175996149cc621b80b9");
00080   std::string r = toHexString(fromHexString(s_hex));
00081   EXPECT_EQ(s, r);
00082 }
00083 
00084 TEST(BoostUUID, fromBracesString)
00085 {
00086   std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
00087   std::string s_braces = "{" + s + "}";
00088   std::string r = toHexString(fromHexString(s_braces));
00089   EXPECT_EQ(s, r);
00090 }
00091 
00092 TEST(BoostUUID, fromUrnString)
00093 {
00094   // This documents boost 1.46.1 behavior, but is an undefined
00095   // fromHexString() input, not really a valid test case.
00096   std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
00097   std::string s_urn = "urn:uuid:" + s;
00098   std::string r = toHexString(fromHexString(s_urn));
00099   EXPECT_NE(s, r);
00100 }
00101 
00102 TEST(BoostUUID, fromTooLongString)
00103 {
00104   // This documents boost 1.46.1 behavior, but is an undefined
00105   // fromHexString() input, not really a valid test case.
00106   std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
00107   std::string s_too_long = s + "-0001";
00108   std::string r = toHexString(fromHexString(s_too_long));
00109   EXPECT_EQ(s, r);
00110 }
00111 
00112 TEST(BoostUUID, fromTooShortString)
00113 {
00114   // This documents boost 1.46.1 behavior, but is an undefined
00115   // fromHexString() input, not really a valid test case.
00116   std::string s("da7c242f-2efe-5175-9961-49cc621b80");
00117   try
00118     {
00119       uuid x = fromHexString(s);
00120       FAIL();                           // expected exception not thrown
00121       EXPECT_NE(toHexString(x), s);
00122     }
00123   catch (std::runtime_error &e)
00124     {
00125       EXPECT_EQ(e.what(), std::string("invalid uuid string"));
00126     }
00127   catch (...)
00128     {
00129       FAIL();                           // unexpected exception
00130     }
00131 }
00132 
00133 TEST(BoostUUID, fromBogusString)
00134 {
00135   // This documents boost 1.46.1 behavior, but is an undefined
00136   // fromHexString() input, not really a valid test case.
00137   std::string s("Invalid UUID string");
00138   try
00139     {
00140       uuid x = fromHexString(s);
00141       FAIL();                           // expected exception not thrown
00142       EXPECT_NE(toHexString(x), s);
00143     }
00144   catch (std::runtime_error &e)
00145     {
00146       EXPECT_EQ(e.what(), std::string("invalid uuid string"));
00147     }
00148   catch (...)
00149     {
00150       FAIL();                           // unexpected exception
00151     }
00152 }
00153 
00154 TEST(UniqueID, nilMessage)
00155 {
00156   UniqueID x;
00157   UniqueID y = toMsg(uuid());
00158   EXPECT_EQ(x.uuid, y.uuid);
00159 }
00160 
00161 TEST(UniqueID, randomMessage)
00162 {
00163   UniqueID x;
00164   UniqueID y = toMsg(fromRandom());
00165   EXPECT_NE(x.uuid, y.uuid);
00166 }
00167 
00168 TEST(UniqueID, equivalentMessages)
00169 {
00170   std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
00171   UniqueID x = toMsg(fromHexString(s));
00172   UniqueID y = toMsg(fromHexString(s));
00173   EXPECT_EQ(x.uuid, y.uuid);
00174   EXPECT_EQ(s, toHexString(y));
00175 }
00176 
00177 TEST(UniqueID, toAndFromMessage)
00178 {
00179   std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
00180   uuid x = uuid(fromHexString(s));
00181   uuid y = fromMsg(toMsg(x));
00182   EXPECT_EQ(x, y);
00183 }
00184 
00185 TEST(UniqueID, messageToString)
00186 {
00187   std::string s("da7c242f-2efe-5175-9961-49cc621b80b9");
00188   UniqueID x = toMsg(fromHexString(s));
00189   std::string y = toHexString(x);
00190   EXPECT_EQ(s, y);
00191 }
00192 
00193 // Run all the tests that were declared with TEST()
00194 int main(int argc, char **argv)
00195 {
00196   testing::InitGoogleTest(&argc, argv);
00197   return RUN_ALL_TESTS();
00198 }


unique_id
Author(s): Jack O'Quin
autogenerated on Mon Oct 6 2014 08:23:45