UUIDTests.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/UnitTest/TestTypes.h>
  9. #include <AzCore/Math/Uuid.h>
  10. #include <AzCore/std/containers/unordered_set.h>
  11. using namespace AZ;
  12. namespace UnitTest
  13. {
  14. class UuidTests
  15. : public UnitTest::AllocatorsTestFixture
  16. {
  17. static const int numUuid = 2000;
  18. Uuid* m_array;
  19. public:
  20. void SetUp() override
  21. {
  22. AllocatorsTestFixture::SetUp();
  23. m_array = (Uuid*)azmalloc(sizeof(Uuid) * numUuid, AZStd::alignment_of<Uuid>::value);
  24. }
  25. void TearDown() override
  26. {
  27. azfree(m_array);
  28. AllocatorsTestFixture::TearDown();
  29. }
  30. void run()
  31. {
  32. Uuid defId;
  33. defId.data[0] = 0xb5;
  34. defId.data[1] = 0x70;
  35. defId.data[2] = 0x0f;
  36. defId.data[3] = 0x2e;
  37. defId.data[4] = 0x66;
  38. defId.data[5] = 0x1b;
  39. defId.data[6] = 0x4a;
  40. defId.data[7] = 0xc0;
  41. defId.data[8] = 0x93;
  42. defId.data[9] = 0x35;
  43. defId.data[10] = 0x81;
  44. defId.data[11] = 0x7c;
  45. defId.data[12] = 0xb4;
  46. defId.data[13] = 0xc0;
  47. defId.data[14] = 0x9c;
  48. defId.data[15] = 0xcb;
  49. // null
  50. Uuid id = Uuid::CreateNull();
  51. AZ_TEST_ASSERT(id.IsNull());
  52. const char idStr1[] = "{B5700F2E-661B-4AC0-9335-817CB4C09CCB}";
  53. const char idStr2[] = "{B5700F2E661B4AC09335817CB4C09CCB}";
  54. const char idStr3[] = "B5700F2E-661B-4AC0-9335-817CB4C09CCB";
  55. const char idStr4[] = "B5700F2E661B4AC09335817CB4C09CCB";
  56. // create from string
  57. id = Uuid::CreateString(idStr1);
  58. AZ_TEST_ASSERT(id == defId);
  59. id = Uuid::CreateString(idStr2);
  60. AZ_TEST_ASSERT(id == defId);
  61. id = Uuid::CreateString(idStr3);
  62. AZ_TEST_ASSERT(id == defId);
  63. id = Uuid::CreateString(idStr4);
  64. AZ_TEST_ASSERT(id == defId);
  65. // variant
  66. AZ_TEST_ASSERT(id.GetVariant() == Uuid::VAR_RFC_4122);
  67. // version
  68. AZ_TEST_ASSERT(id.GetVersion() == Uuid::VER_RANDOM);
  69. // tostring
  70. char buffer[39];
  71. id = Uuid::CreateString(idStr1);
  72. AZ_TEST_ASSERT(id.ToString(buffer, 39, true, true) == 39);
  73. AZ_TEST_ASSERT(strcmp(buffer, idStr1) == 0);
  74. AZ_TEST_ASSERT(id.ToString(buffer, 35, true, false) == 35);
  75. AZ_TEST_ASSERT(strcmp(buffer, idStr2) == 0);
  76. AZ_TEST_ASSERT(id.ToString(buffer, 37, false, true) == 37);
  77. AZ_TEST_ASSERT(strcmp(buffer, idStr3) == 0);
  78. AZ_TEST_ASSERT(id.ToString(buffer, 33, false, false) == 33);
  79. AZ_TEST_ASSERT(strcmp(buffer, idStr4) == 0);
  80. AZ_TEST_ASSERT(id.ToString<AZStd::string>() == AZStd::string(idStr1));
  81. AZ_TEST_ASSERT(id.ToString<AZStd::string>(true, false) == AZStd::string(idStr2));
  82. AZ_TEST_ASSERT(id.ToString<AZStd::string>(false, true) == AZStd::string(idStr3));
  83. AZ_TEST_ASSERT(id.ToString<AZStd::string>(false, false) == AZStd::string(idStr4));
  84. AZStd::string str1;
  85. id.ToString(str1);
  86. AZ_TEST_ASSERT(str1 == AZStd::string(idStr1));
  87. id.ToString(str1, true, false);
  88. AZ_TEST_ASSERT(str1 == AZStd::string(idStr2));
  89. id.ToString(str1, false, true);
  90. AZ_TEST_ASSERT(str1 == AZStd::string(idStr3));
  91. id.ToString(str1, false, false);
  92. AZ_TEST_ASSERT(str1 == AZStd::string(idStr4));
  93. // operators
  94. Uuid idBigger("C5700F2E661B4ac09335817CB4C09CCB");
  95. AZ_TEST_ASSERT(id < idBigger);
  96. AZ_TEST_ASSERT(id != idBigger);
  97. AZ_TEST_ASSERT(idBigger > id);
  98. // hash
  99. AZStd::hash<AZ::Uuid> hash;
  100. size_t hashVal = hash(id);
  101. AZ_TEST_ASSERT(hashVal != 0);
  102. // test the hashing and equal function in a unordered container
  103. typedef AZStd::unordered_set<AZ::Uuid> UuidSetType;
  104. UuidSetType uuidSet;
  105. uuidSet.insert(id);
  106. AZ_TEST_ASSERT(uuidSet.find(id) != uuidSet.end());
  107. // check uniqueness (very quick and basic)
  108. for (int i = 0; i < numUuid; ++i)
  109. {
  110. m_array[i] = Uuid::Create();
  111. }
  112. for (int i = 0; i < numUuid; ++i)
  113. {
  114. Uuid uniqueToTest = Uuid::Create();
  115. for (int j = 0; j < numUuid; ++j)
  116. {
  117. AZ_TEST_ASSERT(m_array[j] != uniqueToTest);
  118. }
  119. }
  120. // test the name function
  121. Uuid uuidName = Uuid::CreateName("BlaBla");
  122. // check variant
  123. AZ_TEST_ASSERT(uuidName.GetVariant() == Uuid::VAR_RFC_4122);
  124. // check version
  125. AZ_TEST_ASSERT(uuidName.GetVersion() == Uuid::VER_NAME_SHA1);
  126. // check id
  127. AZ_TEST_ASSERT(uuidName == Uuid::CreateName("BlaBla"));
  128. }
  129. };
  130. TEST_F(UuidTests, Test)
  131. {
  132. run();
  133. }
  134. TEST_F(UuidTests, GreaterThanOrEqualTo_LeftGreaterThanRight_ReturnsTrue)
  135. {
  136. const char leftStr[] = "{F418022E-DAFE-4450-BCB9-4B7727070178}";
  137. const char rightStr[] = "{B5700F2E-661B-4AC0-9335-817CB4C09CCB}";
  138. Uuid left = Uuid::CreateString(leftStr);
  139. Uuid right = Uuid::CreateString(rightStr);
  140. EXPECT_TRUE(left >= right);
  141. }
  142. TEST_F(UuidTests, GreaterThanOrEqualTo_LeftLessThanRight_ReturnsFalse)
  143. {
  144. const char leftStr[] = "{A418022E-DAFE-4450-BCB9-4B7727070178}";
  145. const char rightStr[] = "{B5700F2E-661B-4AC0-9335-817CB4C09CCB}";
  146. Uuid left = Uuid::CreateString(leftStr);
  147. Uuid right = Uuid::CreateString(rightStr);
  148. EXPECT_FALSE(left >= right);
  149. }
  150. TEST_F(UuidTests, GreaterThanOrEqualTo_LeftEqualsRight_ReturnsTrue)
  151. {
  152. const char uuidStr[] = "{F418022E-DAFE-4450-BCB9-4B7727070178}";
  153. Uuid left = Uuid::CreateString(uuidStr);
  154. Uuid right = Uuid::CreateString(uuidStr);
  155. EXPECT_TRUE(left >= right);
  156. }
  157. TEST_F(UuidTests, LessThanOrEqualTo_LeftGreaterThanRight_ReturnsFalse)
  158. {
  159. const char leftStr[] = "{F418022E-DAFE-4450-BCB9-4B7727070178}";
  160. const char rightStr[] = "{B5700F2E-661B-4AC0-9335-817CB4C09CCB}";
  161. Uuid left = Uuid::CreateString(leftStr);
  162. Uuid right = Uuid::CreateString(rightStr);
  163. EXPECT_FALSE(left <= right);
  164. }
  165. TEST_F(UuidTests, LessThanOrEqualTo_LeftLessThanRight_ReturnsTrue)
  166. {
  167. const char leftStr[] = "{A418022E-DAFE-4450-BCB9-4B7727070178}";
  168. const char rightStr[] = "{B5700F2E-661B-4AC0-9335-817CB4C09CCB}";
  169. Uuid left = Uuid::CreateString(leftStr);
  170. Uuid right = Uuid::CreateString(rightStr);
  171. EXPECT_TRUE(left <= right);
  172. }
  173. TEST_F(UuidTests, LessThanOrEqualTo_LeftEqualsRight_ReturnsTrue)
  174. {
  175. const char uuidStr[] = "{F418022E-DAFE-4450-BCB9-4B7727070178}";
  176. Uuid left = Uuid::CreateString(uuidStr);
  177. Uuid right = Uuid::CreateString(uuidStr);
  178. EXPECT_TRUE(left <= right);
  179. }
  180. TEST_F(UuidTests, CreateStringPermissive_HexAndSpacesGiven_Success)
  181. {
  182. const char uuidStr[] = "{34D44249-E599-4B30-811F-4215C2DEA269}";
  183. Uuid left = Uuid::CreateString(uuidStr);
  184. const char permissiveStr[] = "{ 0x34D44249 - 0xE5994B30 - 0x811F4215 - 0xC2DEA269 }";
  185. Uuid right = Uuid::CreateStringPermissive(permissiveStr);
  186. EXPECT_EQ(left, right);
  187. const char permissiveStr2[] = "{ 0x34D44249-0xE5994B30 0x811F4215 - C2DEA269 }";
  188. right = Uuid::CreateStringPermissive(permissiveStr2);
  189. EXPECT_EQ(left, right);
  190. const char permissiveStr3[] = "34D44249-0xE5994B30 0x811F4215 - C2DEA269 }";
  191. right = Uuid::CreateStringPermissive(permissiveStr3);
  192. EXPECT_EQ(left, right);
  193. const char permissiveStr4[] = "{ x34D44249 - xE5994B30 - x811F4215 xC2DEA269 }";
  194. right = Uuid::CreateStringPermissive(permissiveStr4);
  195. EXPECT_EQ(left, right);
  196. const char permissiveStr5[] = "{ 0X34D44249 - 0XE5994B30 - 0X811F4215 0XC2DEA269 }";
  197. right = Uuid::CreateStringPermissive(permissiveStr5);
  198. EXPECT_EQ(left, right);
  199. }
  200. TEST_F(UuidTests, CreateStringPermissive_InvalidHexAndSpacesGiven_Fails)
  201. {
  202. const char uuidStr[] = "{8FDDE7B1 - C332 - 4EBA - BD85 - 2898E7440E4C}";
  203. Uuid left = Uuid::CreateStringPermissive(uuidStr);
  204. const char permissiveStr1[] = "{ 8FDDE7B1 - 0 xC332 - 4EBA - BD85 - 2898E7440E4C}";
  205. Uuid right = Uuid::CreateStringPermissive(permissiveStr1);
  206. EXPECT_NE(left, right);
  207. }
  208. TEST_F(UuidTests, CreateStringPermissive_InvalidCharacterGiven_Fails)
  209. {
  210. Uuid left = Uuid::CreateNull();
  211. // The below check should just give an empty uuid due to the g
  212. const char permissiveStr1[] = "{CCF8AB1E- gA04A-43D1-AD8A-70725BC3392E}";
  213. Uuid right = Uuid::CreateStringPermissive(permissiveStr1);
  214. EXPECT_EQ(left, right);
  215. }
  216. TEST_F(UuidTests, CreateStringPermissive_StringWithExtraData_Succeeds)
  217. {
  218. const char uuidStr[] = "{34D44249-E599-4B30-811F-4215C2DEA269}";
  219. Uuid left = Uuid::CreateString(uuidStr);
  220. const char permissiveStr[] = "0x34D44249-0xE5994B30-0x811F4215-0xC2DEA269 Hello World";
  221. Uuid right = Uuid::CreateStringPermissive(permissiveStr);
  222. EXPECT_EQ(left, right);
  223. }
  224. TEST_F(UuidTests, CreateStringPermissive_StringWithLotsOfExtraData_Succeeds)
  225. {
  226. const char uuidStr[] = "{34D44249-E599-4B30-811F-4215C2DEA269}";
  227. Uuid left = Uuid::CreateString(uuidStr);
  228. const char permissiveStr[] = "0x34D44249-0xE5994B30-0x811F4215-0xC2DEA269 Hello World this is a really long string "
  229. "with lots of extra data to make sure we can parse a long string without failing as long as the uuid is in "
  230. "the beginning of the string then we should succeed";
  231. Uuid right = Uuid::CreateStringPermissive(permissiveStr);
  232. EXPECT_EQ(left, right);
  233. }
  234. }