1
0

hashed_string.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <string>
  2. #include <string_view>
  3. #include <type_traits>
  4. #include <gtest/gtest.h>
  5. #include <entt/core/hashed_string.hpp>
  6. TEST(HashedString, Functionalities) {
  7. using hash_type = entt::hashed_string::hash_type;
  8. const char *bar = "bar";
  9. auto foo_hs = entt::hashed_string{"foo"};
  10. auto bar_hs = entt::hashed_string{bar};
  11. ASSERT_NE(static_cast<hash_type>(foo_hs), static_cast<hash_type>(bar_hs));
  12. ASSERT_STREQ(static_cast<const char *>(foo_hs), "foo");
  13. ASSERT_STREQ(static_cast<const char *>(bar_hs), bar);
  14. ASSERT_STREQ(foo_hs.data(), "foo");
  15. ASSERT_STREQ(bar_hs.data(), bar);
  16. ASSERT_EQ(foo_hs, foo_hs);
  17. ASSERT_NE(foo_hs, bar_hs);
  18. entt::hashed_string hs{"foobar"};
  19. ASSERT_EQ(static_cast<hash_type>(hs), 0xbf9cf968);
  20. ASSERT_EQ(hs.value(), 0xbf9cf968);
  21. ASSERT_EQ(foo_hs, "foo"_hs);
  22. ASSERT_NE(bar_hs, "foo"_hs);
  23. }
  24. TEST(HashedString, Empty) {
  25. using hash_type = entt::hashed_string::hash_type;
  26. entt::hashed_string hs{};
  27. ASSERT_EQ(static_cast<hash_type>(hs), hash_type{});
  28. ASSERT_EQ(static_cast<const char *>(hs), nullptr);
  29. }
  30. TEST(HashedString, Constexprness) {
  31. using hash_type = entt::hashed_string::hash_type;
  32. // how would you test a constexpr otherwise?
  33. (void)std::integral_constant<hash_type, entt::hashed_string{"quux"}>{};
  34. (void)std::integral_constant<hash_type, "quux"_hs>{};
  35. ASSERT_TRUE(true);
  36. }
  37. TEST(HashedString, ToValue) {
  38. using hash_type = entt::hashed_string::hash_type;
  39. const char *foobar = "foobar";
  40. ASSERT_EQ(entt::hashed_string::to_value(foobar), 0xbf9cf968);
  41. // how would you test a constexpr otherwise?
  42. (void)std::integral_constant<hash_type, entt::hashed_string::to_value("quux")>{};
  43. }
  44. TEST(HashedString, StringView) {
  45. std::string str{"__foobar__"};
  46. std::string_view view{str.data()+2, 6};
  47. ASSERT_EQ(entt::hashed_string::to_value(view.data(), view.size()), 0xbf9cf968);
  48. }
  49. TEST(HashedWString, Functionalities) {
  50. using hash_type = entt::hashed_wstring::hash_type;
  51. const wchar_t *bar = L"bar";
  52. auto foo_hws = entt::hashed_wstring{L"foo"};
  53. auto bar_hws = entt::hashed_wstring{bar};
  54. ASSERT_NE(static_cast<hash_type>(foo_hws), static_cast<hash_type>(bar_hws));
  55. ASSERT_STREQ(static_cast<const wchar_t *>(foo_hws), L"foo");
  56. ASSERT_STREQ(static_cast<const wchar_t *>(bar_hws), bar);
  57. ASSERT_STREQ(foo_hws.data(), L"foo");
  58. ASSERT_STREQ(bar_hws.data(), bar);
  59. ASSERT_EQ(foo_hws, foo_hws);
  60. ASSERT_NE(foo_hws, bar_hws);
  61. entt::hashed_wstring hws{L"foobar"};
  62. ASSERT_EQ(static_cast<hash_type>(hws), 0xbf9cf968);
  63. ASSERT_EQ(hws.value(), 0xbf9cf968);
  64. ASSERT_EQ(foo_hws, L"foo"_hws);
  65. ASSERT_NE(bar_hws, L"foo"_hws);
  66. }
  67. TEST(HashedWString, Empty) {
  68. using hash_type = entt::hashed_wstring::hash_type;
  69. entt::hashed_wstring hws{};
  70. ASSERT_EQ(static_cast<hash_type>(hws), hash_type{});
  71. ASSERT_EQ(static_cast<const wchar_t *>(hws), nullptr);
  72. }
  73. TEST(HashedWString, Constexprness) {
  74. using hash_type = entt::hashed_wstring::hash_type;
  75. // how would you test a constexpr otherwise?
  76. (void)std::integral_constant<hash_type, entt::hashed_wstring{L"quux"}>{};
  77. (void)std::integral_constant<hash_type, L"quux"_hws>{};
  78. ASSERT_TRUE(true);
  79. }
  80. TEST(HashedWString, ToValue) {
  81. using hash_type = entt::hashed_wstring::hash_type;
  82. const wchar_t *foobar = L"foobar";
  83. ASSERT_EQ(entt::hashed_wstring::to_value(foobar), 0xbf9cf968);
  84. // how would you test a constexpr otherwise?
  85. (void)std::integral_constant<hash_type, entt::hashed_wstring::to_value(L"quux")>{};
  86. }
  87. TEST(HashedWString, StringView) {
  88. std::wstring str{L"__foobar__"};
  89. std::wstring_view view{str.data()+2, 6};
  90. ASSERT_EQ(entt::hashed_wstring::to_value(view.data(), view.size()), 0xbf9cf968);
  91. }