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


unique_id
Author(s): Jack O'Quin
autogenerated on Fri Aug 28 2015 13:29:41