| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #include <memory>
- #include <utility>
- #include <gtest/gtest.h>
- #include <entt/core/type_info.hpp>
- #include <entt/resource/resource.hpp>
- #include "../../common/linter.hpp"
- struct base {
- virtual ~base() noexcept = default;
- [[nodiscard]] virtual const entt::type_info &type() const noexcept {
- return entt::type_id<base>();
- }
- };
- struct derived: base {
- [[nodiscard]] const entt::type_info &type() const noexcept override {
- return entt::type_id<derived>();
- }
- };
- template<typename Type, typename Other>
- entt::resource<Type> dynamic_resource_cast(const entt::resource<Other> &other) {
- if(other->type() == entt::type_id<Type>()) {
- return entt::resource<Type>{other, static_cast<Type &>(*other)};
- }
- return {};
- }
- TEST(Resource, Functionalities) {
- const entt::resource<derived> resource{};
- ASSERT_FALSE(resource);
- ASSERT_EQ(resource.operator->(), nullptr);
- ASSERT_EQ(resource.handle().use_count(), 0l);
- const auto value = std::make_shared<derived>();
- entt::resource<derived> other{value};
- ASSERT_TRUE(other);
- ASSERT_EQ(other.operator->(), value.get());
- ASSERT_EQ(&static_cast<derived &>(other), value.get());
- ASSERT_EQ(&*other, value.get());
- ASSERT_EQ(other.handle().use_count(), 2l);
- entt::resource<derived> copy{resource};
- entt::resource<derived> 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);
- copy.reset(std::make_shared<derived>());
- ASSERT_TRUE(copy);
- ASSERT_TRUE(move);
- ASSERT_NE(copy, move);
- move.reset();
- ASSERT_TRUE(copy);
- ASSERT_FALSE(move);
- ASSERT_NE(copy, move);
- }
- TEST(Resource, Swap) {
- entt::resource<int> resource{};
- entt::resource<int> other{};
- ASSERT_FALSE(resource);
- ASSERT_FALSE(other);
- resource.swap(other);
- ASSERT_FALSE(resource);
- ASSERT_FALSE(other);
- resource.reset(std::make_shared<int>(1));
- ASSERT_TRUE(resource);
- ASSERT_EQ(*resource, 1);
- ASSERT_FALSE(other);
- resource.swap(other);
- ASSERT_FALSE(resource);
- ASSERT_TRUE(other);
- ASSERT_EQ(*other, 1);
- }
- TEST(Resource, DerivedToBase) {
- const entt::resource<derived> resource{std::make_shared<derived>()};
- entt::resource<base> other{resource};
- entt::resource<const base> 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<derived> resource{std::make_shared<derived>()};
- entt::resource<derived> other{resource};
- testing::StaticAssertTypeEq<decltype(*resource), derived &>();
- testing::StaticAssertTypeEq<decltype(*entt::resource<const derived>{other}), const derived &>();
- testing::StaticAssertTypeEq<decltype(*std::as_const(resource)), derived &>();
- entt::resource<const derived> copy{resource};
- entt::resource<const derived> move{std::move(other)};
- test::is_initialized(other);
- ASSERT_TRUE(resource);
- ASSERT_FALSE(other);
- ASSERT_TRUE(copy);
- ASSERT_EQ(copy, resource);
- ASSERT_NE(copy, entt::resource<derived>{});
- ASSERT_EQ(copy.handle().use_count(), 3);
- ASSERT_TRUE(move);
- ASSERT_EQ(move, resource);
- ASSERT_NE(move, entt::resource<derived>{});
- ASSERT_EQ(move.handle().use_count(), 3);
- copy = resource;
- move = std::move(resource);
- test::is_initialized(resource);
- ASSERT_FALSE(resource);
- ASSERT_FALSE(other);
- ASSERT_TRUE(copy);
- ASSERT_TRUE(move);
- ASSERT_EQ(copy.handle().use_count(), 2);
- }
- TEST(Resource, DynamicResourceHandleCast) {
- const entt::resource<derived> resource{std::make_shared<derived>()};
- entt::resource<const base> other = resource;
- ASSERT_TRUE(other);
- ASSERT_EQ(resource.handle().use_count(), 2);
- ASSERT_EQ(resource, other);
- entt::resource<const derived> cast = dynamic_resource_cast<const derived>(other);
- ASSERT_TRUE(cast);
- ASSERT_EQ(resource.handle().use_count(), 3);
- ASSERT_EQ(resource, cast);
- other = entt::resource<base>{std::make_shared<base>()};
- cast = dynamic_resource_cast<const derived>(other);
- ASSERT_FALSE(cast);
- ASSERT_EQ(resource.handle().use_count(), 1);
- }
- TEST(Resource, Comparison) {
- const entt::resource<derived> resource{std::make_shared<derived>()};
- const entt::resource<const base> 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);
- }
|