hashed_string.cpp 7.2 KB

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