hashed_string.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <cstdint>
  2. #include <string_view>
  3. #include <gtest/gtest.h>
  4. #include <entt/core/hashed_string.hpp>
  5. struct BasicHashedString: ::testing::Test {
  6. static constexpr auto expected() noexcept {
  7. if constexpr(std::is_same_v<entt::id_type, std::uint32_t>) {
  8. constexpr auto foobar_hash = 0xbf9cf968;
  9. return foobar_hash;
  10. } else if constexpr(std::is_same_v<entt::id_type, std::uint64_t>) {
  11. constexpr auto foobar_hash = 0x85944171f73967e8;
  12. return foobar_hash;
  13. }
  14. }
  15. };
  16. using HashedString = BasicHashedString;
  17. using HashedWString = BasicHashedString;
  18. TEST_F(BasicHashedString, DeductionGuide) {
  19. testing::StaticAssertTypeEq<decltype(entt::basic_hashed_string{"foo"}), entt::hashed_string>();
  20. testing::StaticAssertTypeEq<decltype(entt::basic_hashed_string{L"foo"}), entt::hashed_wstring>();
  21. }
  22. TEST_F(HashedString, Functionalities) {
  23. using namespace entt::literals;
  24. using hash_type = entt::hashed_string::hash_type;
  25. const char *bar = "bar";
  26. auto foo_hs = entt::hashed_string{"foo"};
  27. auto bar_hs = entt::hashed_string{bar};
  28. ASSERT_NE(static_cast<hash_type>(foo_hs), static_cast<hash_type>(bar_hs));
  29. ASSERT_STREQ(static_cast<const char *>(foo_hs), "foo");
  30. ASSERT_STREQ(static_cast<const char *>(bar_hs), bar);
  31. ASSERT_STREQ(foo_hs.data(), "foo");
  32. ASSERT_STREQ(bar_hs.data(), bar);
  33. ASSERT_EQ(foo_hs.size(), 3u);
  34. ASSERT_EQ(bar_hs.size(), 3u);
  35. ASSERT_EQ(foo_hs, foo_hs);
  36. ASSERT_NE(foo_hs, bar_hs);
  37. const entt::hashed_string hs{"foobar"};
  38. ASSERT_EQ(static_cast<hash_type>(hs), expected());
  39. ASSERT_EQ(hs.value(), expected());
  40. ASSERT_EQ(foo_hs, "foo"_hs);
  41. ASSERT_NE(bar_hs, "foo"_hs);
  42. entt::hashed_string empty_hs{};
  43. ASSERT_EQ(empty_hs, entt::hashed_string{});
  44. ASSERT_NE(empty_hs, foo_hs);
  45. empty_hs = foo_hs;
  46. ASSERT_NE(empty_hs, entt::hashed_string{});
  47. ASSERT_EQ(empty_hs, foo_hs);
  48. }
  49. TEST_F(HashedString, Empty) {
  50. using hash_type = entt::hashed_string::hash_type;
  51. const entt::hashed_string hs{};
  52. ASSERT_EQ(hs.size(), 0u);
  53. ASSERT_EQ(static_cast<hash_type>(hs), entt::internal::offset());
  54. ASSERT_EQ(static_cast<const char *>(hs), nullptr);
  55. }
  56. TEST_F(HashedString, Correctness) {
  57. const char *foobar = "foobar";
  58. const std::string_view view{"foobar__", 6};
  59. ASSERT_EQ(entt::hashed_string{foobar}, expected());
  60. ASSERT_EQ((entt::hashed_string{view.data(), view.size()}), expected());
  61. ASSERT_EQ(entt::hashed_string{"foobar"}, expected());
  62. ASSERT_EQ(entt::hashed_string::value(foobar), expected());
  63. ASSERT_EQ(entt::hashed_string::value(view.data(), view.size()), expected());
  64. ASSERT_EQ(entt::hashed_string::value("foobar"), expected());
  65. ASSERT_EQ(entt::hashed_string{foobar}.size(), 6u);
  66. ASSERT_EQ((entt::hashed_string{view.data(), view.size()}).size(), 6u);
  67. ASSERT_EQ(entt::hashed_string{"foobar"}.size(), 6u);
  68. }
  69. TEST_F(HashedString, Order) {
  70. using namespace entt::literals;
  71. const entt::hashed_string lhs = "foo"_hs;
  72. const entt::hashed_string rhs = "bar"_hs;
  73. ASSERT_FALSE(lhs < lhs);
  74. ASSERT_FALSE(rhs < rhs);
  75. ASSERT_LT(rhs, lhs);
  76. ASSERT_LE(rhs, lhs);
  77. ASSERT_GT(lhs, rhs);
  78. ASSERT_GE(lhs, rhs);
  79. }
  80. TEST_F(HashedString, Constexprness) {
  81. using namespace entt::literals;
  82. constexpr std::string_view view{"foobar__", 6};
  83. ASSERT_EQ(entt::hashed_string{"quux"}, "quux"_hs);
  84. ASSERT_EQ(entt::hashed_string{"foobar"}, expected());
  85. ASSERT_EQ(entt::hashed_string::value("quux"), "quux"_hs);
  86. ASSERT_EQ(entt::hashed_string::value("foobar"), expected());
  87. ASSERT_EQ((entt::hashed_string{"quux", 4}), "quux"_hs);
  88. ASSERT_EQ((entt::hashed_string{view.data(), view.size()}), expected());
  89. ASSERT_EQ((entt::hashed_string::value("quux", 4)), "quux"_hs);
  90. ASSERT_EQ((entt::hashed_string::value(view.data(), view.size())), expected());
  91. ASSERT_LT(entt::hashed_string{"bar"}, "foo"_hs);
  92. ASSERT_LE(entt::hashed_string{"bar"}, "bar"_hs);
  93. ASSERT_GT(entt::hashed_string{"foo"}, "bar"_hs);
  94. ASSERT_GE(entt::hashed_string{"foo"}, "foo"_hs);
  95. }
  96. TEST_F(HashedWString, Functionalities) {
  97. using namespace entt::literals;
  98. using hash_type = entt::hashed_wstring::hash_type;
  99. const wchar_t *bar = L"bar";
  100. auto foo_hws = entt::hashed_wstring{L"foo"};
  101. auto bar_hws = entt::hashed_wstring{bar};
  102. ASSERT_NE(static_cast<hash_type>(foo_hws), static_cast<hash_type>(bar_hws));
  103. ASSERT_STREQ(static_cast<const wchar_t *>(foo_hws), L"foo");
  104. ASSERT_STREQ(static_cast<const wchar_t *>(bar_hws), bar);
  105. ASSERT_STREQ(foo_hws.data(), L"foo");
  106. ASSERT_STREQ(bar_hws.data(), bar);
  107. ASSERT_EQ(foo_hws.size(), 3u);
  108. ASSERT_EQ(bar_hws.size(), 3u);
  109. ASSERT_EQ(foo_hws, foo_hws);
  110. ASSERT_NE(foo_hws, bar_hws);
  111. const entt::hashed_wstring hws{L"foobar"};
  112. ASSERT_EQ(static_cast<hash_type>(hws), expected());
  113. ASSERT_EQ(hws.value(), expected());
  114. ASSERT_EQ(foo_hws, L"foo"_hws);
  115. ASSERT_NE(bar_hws, L"foo"_hws);
  116. }
  117. TEST_F(HashedWString, Empty) {
  118. using hash_type = entt::hashed_wstring::hash_type;
  119. const entt::hashed_wstring hws{};
  120. ASSERT_EQ(hws.size(), 0u);
  121. ASSERT_EQ(static_cast<hash_type>(hws), entt::internal::offset());
  122. ASSERT_EQ(static_cast<const wchar_t *>(hws), nullptr);
  123. }
  124. TEST_F(HashedWString, Correctness) {
  125. const wchar_t *foobar = L"foobar";
  126. const std::wstring_view view{L"foobar__", 6};
  127. ASSERT_EQ(entt::hashed_wstring{foobar}, expected());
  128. ASSERT_EQ((entt::hashed_wstring{view.data(), view.size()}), expected());
  129. ASSERT_EQ(entt::hashed_wstring{L"foobar"}, expected());
  130. ASSERT_EQ(entt::hashed_wstring::value(foobar), expected());
  131. ASSERT_EQ(entt::hashed_wstring::value(view.data(), view.size()), expected());
  132. ASSERT_EQ(entt::hashed_wstring::value(L"foobar"), expected());
  133. ASSERT_EQ(entt::hashed_wstring{foobar}.size(), 6u);
  134. ASSERT_EQ((entt::hashed_wstring{view.data(), view.size()}).size(), 6u);
  135. ASSERT_EQ(entt::hashed_wstring{L"foobar"}.size(), 6u);
  136. }
  137. TEST_F(HashedWString, Order) {
  138. using namespace entt::literals;
  139. const entt::hashed_wstring lhs = L"foo"_hws;
  140. const entt::hashed_wstring rhs = L"bar"_hws;
  141. ASSERT_FALSE(lhs < lhs);
  142. ASSERT_FALSE(rhs < rhs);
  143. ASSERT_LT(rhs, lhs);
  144. ASSERT_LE(rhs, lhs);
  145. ASSERT_GT(lhs, rhs);
  146. ASSERT_GE(lhs, rhs);
  147. }
  148. TEST_F(HashedWString, Constexprness) {
  149. using namespace entt::literals;
  150. constexpr std::wstring_view view{L"foobar__", 6};
  151. ASSERT_EQ(entt::hashed_wstring{L"quux"}, L"quux"_hws);
  152. ASSERT_EQ(entt::hashed_wstring{L"foobar"}, expected());
  153. ASSERT_EQ(entt::hashed_wstring::value(L"quux"), L"quux"_hws);
  154. ASSERT_EQ(entt::hashed_wstring::value(L"foobar"), expected());
  155. ASSERT_EQ((entt::hashed_wstring{L"quux", 4}), L"quux"_hws);
  156. ASSERT_EQ((entt::hashed_wstring{view.data(), view.size()}), expected());
  157. ASSERT_EQ((entt::hashed_wstring::value(L"quux", 4)), L"quux"_hws);
  158. ASSERT_EQ((entt::hashed_wstring::value(view.data(), view.size())), expected());
  159. ASSERT_LT(entt::hashed_wstring{L"bar"}, L"foo"_hws);
  160. ASSERT_LE(entt::hashed_wstring{L"bar"}, L"bar"_hws);
  161. ASSERT_GT(entt::hashed_wstring{L"foo"}, L"bar"_hws);
  162. ASSERT_GE(entt::hashed_wstring{L"foo"}, L"foo"_hws);
  163. }