hashed_string.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. }