| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- #include <cstdint>
- #include <string_view>
- #include <type_traits>
- #include <gtest/gtest.h>
- #include <entt/core/hashed_string.hpp>
- struct BasicHashedString: ::testing::Test {
- static constexpr auto expected() noexcept {
- if constexpr(std::is_same_v<entt::id_type, std::uint32_t>) {
- constexpr auto foobar_hash = 0xbf9cf968;
- return foobar_hash;
- } else if constexpr(std::is_same_v<entt::id_type, std::uint64_t>) {
- constexpr auto foobar_hash = 0x85944171f73967e8;
- return foobar_hash;
- }
- }
- };
- using HashedString = BasicHashedString;
- using HashedWString = BasicHashedString;
- TEST_F(BasicHashedString, DeductionGuide) {
- testing::StaticAssertTypeEq<decltype(entt::basic_hashed_string{"foo"}), entt::hashed_string>();
- testing::StaticAssertTypeEq<decltype(entt::basic_hashed_string{L"foo"}), entt::hashed_wstring>();
- }
- TEST_F(HashedString, Functionalities) {
- using namespace entt::literals;
- using hash_type = entt::hashed_string::hash_type;
- const char *bar = "bar";
- auto foo_hs = entt::hashed_string{"foo"};
- auto bar_hs = entt::hashed_string{bar};
- ASSERT_NE(static_cast<hash_type>(foo_hs), static_cast<hash_type>(bar_hs));
- ASSERT_STREQ(static_cast<const char *>(foo_hs), "foo");
- ASSERT_STREQ(static_cast<const char *>(bar_hs), bar);
- ASSERT_STREQ(foo_hs.data(), "foo");
- ASSERT_STREQ(bar_hs.data(), bar);
- ASSERT_EQ(foo_hs.size(), 3u);
- ASSERT_EQ(bar_hs.size(), 3u);
- ASSERT_EQ(foo_hs, foo_hs);
- ASSERT_NE(foo_hs, bar_hs);
- const entt::hashed_string hs{"foobar"};
- ASSERT_EQ(static_cast<hash_type>(hs), expected());
- ASSERT_EQ(hs.value(), expected());
- ASSERT_EQ(foo_hs, "foo"_hs);
- ASSERT_NE(bar_hs, "foo"_hs);
- entt::hashed_string empty_hs{};
- ASSERT_EQ(empty_hs, entt::hashed_string{});
- ASSERT_NE(empty_hs, foo_hs);
- empty_hs = foo_hs;
- ASSERT_NE(empty_hs, entt::hashed_string{});
- ASSERT_EQ(empty_hs, foo_hs);
- }
- TEST_F(HashedString, Empty) {
- using hash_type = entt::hashed_string::hash_type;
- const entt::hashed_string hs{};
- ASSERT_EQ(hs.size(), 0u);
- ASSERT_EQ(static_cast<hash_type>(hs), entt::internal::offset());
- ASSERT_EQ(static_cast<const char *>(hs), nullptr);
- }
- TEST_F(HashedString, Correctness) {
- const char *foobar = "foobar";
- const std::string_view view{"foobar__", 6};
- ASSERT_EQ(entt::hashed_string{foobar}, expected());
- ASSERT_EQ((entt::hashed_string{view.data(), view.size()}), expected());
- ASSERT_EQ(entt::hashed_string{"foobar"}, expected());
- ASSERT_EQ(entt::hashed_string::value(foobar), expected());
- ASSERT_EQ(entt::hashed_string::value(view.data(), view.size()), expected());
- ASSERT_EQ(entt::hashed_string::value("foobar"), expected());
- ASSERT_EQ(entt::hashed_string{foobar}.size(), 6u);
- ASSERT_EQ((entt::hashed_string{view.data(), view.size()}).size(), 6u);
- ASSERT_EQ(entt::hashed_string{"foobar"}.size(), 6u);
- }
- TEST_F(HashedString, Order) {
- using namespace entt::literals;
- const entt::hashed_string lhs = "foo"_hs;
- const entt::hashed_string rhs = "bar"_hs;
- ASSERT_FALSE(lhs < lhs);
- ASSERT_FALSE(rhs < rhs);
- ASSERT_LT(rhs, lhs);
- ASSERT_LE(rhs, lhs);
- ASSERT_GT(lhs, rhs);
- ASSERT_GE(lhs, rhs);
- }
- TEST_F(HashedString, Constexprness) {
- using namespace entt::literals;
- constexpr std::string_view view{"foobar__", 6};
- ASSERT_EQ(entt::hashed_string{"quux"}, "quux"_hs);
- ASSERT_EQ(entt::hashed_string{"foobar"}, expected());
- ASSERT_EQ(entt::hashed_string::value("quux"), "quux"_hs);
- ASSERT_EQ(entt::hashed_string::value("foobar"), expected());
- ASSERT_EQ((entt::hashed_string{"quux", 4}), "quux"_hs);
- ASSERT_EQ((entt::hashed_string{view.data(), view.size()}), expected());
- ASSERT_EQ((entt::hashed_string::value("quux", 4)), "quux"_hs);
- ASSERT_EQ((entt::hashed_string::value(view.data(), view.size())), expected());
- ASSERT_LT(entt::hashed_string{"bar"}, "foo"_hs);
- ASSERT_LE(entt::hashed_string{"bar"}, "bar"_hs);
- ASSERT_GT(entt::hashed_string{"foo"}, "bar"_hs);
- ASSERT_GE(entt::hashed_string{"foo"}, "foo"_hs);
- }
- TEST_F(HashedWString, Functionalities) {
- using namespace entt::literals;
- using hash_type = entt::hashed_wstring::hash_type;
- const wchar_t *bar = L"bar";
- auto foo_hws = entt::hashed_wstring{L"foo"};
- auto bar_hws = entt::hashed_wstring{bar};
- ASSERT_NE(static_cast<hash_type>(foo_hws), static_cast<hash_type>(bar_hws));
- ASSERT_STREQ(static_cast<const wchar_t *>(foo_hws), L"foo");
- ASSERT_STREQ(static_cast<const wchar_t *>(bar_hws), bar);
- ASSERT_STREQ(foo_hws.data(), L"foo");
- ASSERT_STREQ(bar_hws.data(), bar);
- ASSERT_EQ(foo_hws.size(), 3u);
- ASSERT_EQ(bar_hws.size(), 3u);
- ASSERT_EQ(foo_hws, foo_hws);
- ASSERT_NE(foo_hws, bar_hws);
- const entt::hashed_wstring hws{L"foobar"};
- ASSERT_EQ(static_cast<hash_type>(hws), expected());
- ASSERT_EQ(hws.value(), expected());
- ASSERT_EQ(foo_hws, L"foo"_hws);
- ASSERT_NE(bar_hws, L"foo"_hws);
- }
- TEST_F(HashedWString, Empty) {
- using hash_type = entt::hashed_wstring::hash_type;
- const entt::hashed_wstring hws{};
- ASSERT_EQ(hws.size(), 0u);
- ASSERT_EQ(static_cast<hash_type>(hws), entt::internal::offset());
- ASSERT_EQ(static_cast<const wchar_t *>(hws), nullptr);
- }
- TEST_F(HashedWString, Correctness) {
- const wchar_t *foobar = L"foobar";
- const std::wstring_view view{L"foobar__", 6};
- ASSERT_EQ(entt::hashed_wstring{foobar}, expected());
- ASSERT_EQ((entt::hashed_wstring{view.data(), view.size()}), expected());
- ASSERT_EQ(entt::hashed_wstring{L"foobar"}, expected());
- ASSERT_EQ(entt::hashed_wstring::value(foobar), expected());
- ASSERT_EQ(entt::hashed_wstring::value(view.data(), view.size()), expected());
- ASSERT_EQ(entt::hashed_wstring::value(L"foobar"), expected());
- ASSERT_EQ(entt::hashed_wstring{foobar}.size(), 6u);
- ASSERT_EQ((entt::hashed_wstring{view.data(), view.size()}).size(), 6u);
- ASSERT_EQ(entt::hashed_wstring{L"foobar"}.size(), 6u);
- }
- TEST_F(HashedWString, Order) {
- using namespace entt::literals;
- const entt::hashed_wstring lhs = L"foo"_hws;
- const entt::hashed_wstring rhs = L"bar"_hws;
- ASSERT_FALSE(lhs < lhs);
- ASSERT_FALSE(rhs < rhs);
- ASSERT_LT(rhs, lhs);
- ASSERT_LE(rhs, lhs);
- ASSERT_GT(lhs, rhs);
- ASSERT_GE(lhs, rhs);
- }
- TEST_F(HashedWString, Constexprness) {
- using namespace entt::literals;
- constexpr std::wstring_view view{L"foobar__", 6};
- ASSERT_EQ(entt::hashed_wstring{L"quux"}, L"quux"_hws);
- ASSERT_EQ(entt::hashed_wstring{L"foobar"}, expected());
- ASSERT_EQ(entt::hashed_wstring::value(L"quux"), L"quux"_hws);
- ASSERT_EQ(entt::hashed_wstring::value(L"foobar"), expected());
- ASSERT_EQ((entt::hashed_wstring{L"quux", 4}), L"quux"_hws);
- ASSERT_EQ((entt::hashed_wstring{view.data(), view.size()}), expected());
- ASSERT_EQ((entt::hashed_wstring::value(L"quux", 4)), L"quux"_hws);
- ASSERT_EQ((entt::hashed_wstring::value(view.data(), view.size())), expected());
- ASSERT_LT(entt::hashed_wstring{L"bar"}, L"foo"_hws);
- ASSERT_LE(entt::hashed_wstring{L"bar"}, L"bar"_hws);
- ASSERT_GT(entt::hashed_wstring{L"foo"}, L"bar"_hws);
- ASSERT_GE(entt::hashed_wstring{L"foo"}, L"foo"_hws);
- }
|