hashed_string.cpp 7.0 KB

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