#include #include #include struct base { virtual ~base() = default; virtual const entt::type_info &type() const noexcept { return entt::type_id(); } }; struct derived: base { const entt::type_info &type() const noexcept override { return entt::type_id(); } }; template entt::resource dynamic_resource_cast(const entt::resource &other) { if(other->type() == entt::type_id()) { return entt::resource{other, static_cast(*other)}; } return {}; } TEST(Resource, Functionalities) { entt::resource resource{}; ASSERT_FALSE(resource); ASSERT_EQ(resource.operator->(), nullptr); ASSERT_EQ(resource.handle().use_count(), 0l); const auto value = std::make_shared(); entt::resource other{value}; ASSERT_TRUE(other); ASSERT_EQ(other.operator->(), value.get()); ASSERT_EQ(&static_cast(other), value.get()); ASSERT_EQ(&*other, value.get()); ASSERT_EQ(other.handle().use_count(), 2l); entt::resource copy{resource}; entt::resource move{std::move(other)}; ASSERT_FALSE(copy); ASSERT_TRUE(move); copy = std::move(move); move = copy; ASSERT_TRUE(copy); ASSERT_TRUE(move); ASSERT_EQ(copy, move); } TEST(Resource, DerivedToBase) { entt::resource resource{std::make_shared()}; entt::resource other{resource}; entt::resource cother{resource}; ASSERT_TRUE(resource); ASSERT_TRUE(other); ASSERT_TRUE(cother); ASSERT_EQ(resource, other); ASSERT_EQ(other, cother); other = resource; cother = resource; ASSERT_EQ(resource, other); ASSERT_EQ(other, cother); } TEST(Resource, ConstNonConstAndAllInBetween) { entt::resource resource{std::make_shared()}; entt::resource other{resource}; static_assert(std::is_same_v); static_assert(std::is_same_v{other}), const derived &>); static_assert(std::is_same_v); entt::resource copy{resource}; entt::resource move{std::move(other)}; ASSERT_TRUE(resource); ASSERT_FALSE(other); ASSERT_TRUE(copy); ASSERT_EQ(copy, resource); ASSERT_NE(copy, entt::resource{}); ASSERT_EQ(copy.handle().use_count(), 3); ASSERT_TRUE(move); ASSERT_EQ(move, resource); ASSERT_NE(move, entt::resource{}); ASSERT_EQ(move.handle().use_count(), 3); copy = resource; move = std::move(resource); ASSERT_FALSE(resource); ASSERT_FALSE(other); ASSERT_TRUE(copy); ASSERT_TRUE(move); ASSERT_EQ(copy.handle().use_count(), 2); } TEST(Resource, DynamicResourceHandleCast) { entt::resource resource{std::make_shared()}; entt::resource other = resource; ASSERT_TRUE(other); ASSERT_EQ(resource.handle().use_count(), 2); ASSERT_EQ(resource, other); entt::resource cast = dynamic_resource_cast(other); ASSERT_TRUE(cast); ASSERT_EQ(resource.handle().use_count(), 3); ASSERT_EQ(resource, cast); other = entt::resource{std::make_shared()}; cast = dynamic_resource_cast(other); ASSERT_FALSE(cast); ASSERT_EQ(resource.handle().use_count(), 1); } TEST(Resource, Comparison) { entt::resource resource{std::make_shared()}; entt::resource other = resource; ASSERT_TRUE(resource == other); ASSERT_FALSE(resource != other); ASSERT_FALSE(resource < other); ASSERT_FALSE(resource > other); ASSERT_TRUE(resource <= other); ASSERT_TRUE(resource >= other); }