Browse Source

tests: updated tests for the resource module

Michele Caini 3 năm trước cách đây
mục cha
commit
428bfbd810

+ 2 - 0
test/CMakeLists.txt

@@ -251,6 +251,8 @@ SETUP_BASIC_TEST(scheduler entt/process/scheduler.cpp)
 # Test resource
 # Test resource
 
 
 SETUP_BASIC_TEST(resource entt/resource/resource.cpp)
 SETUP_BASIC_TEST(resource entt/resource/resource.cpp)
+SETUP_BASIC_TEST(resource_cache entt/resource/resource_cache.cpp)
+SETUP_BASIC_TEST(resource_loader entt/resource/resource_loader.cpp)
 
 
 # Test signal
 # Test signal
 
 

+ 77 - 213
test/entt/resource/resource.cpp

@@ -1,266 +1,130 @@
-#include <memory>
-#include <type_traits>
-#include <utility>
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
-#include <entt/config/config.h>
-#include <entt/core/hashed_string.hpp>
 #include <entt/core/type_info.hpp>
 #include <entt/core/type_info.hpp>
-#include <entt/resource/cache.hpp>
-#include <entt/resource/handle.hpp>
+#include <entt/resource/resource.hpp>
 
 
-struct resource {
-    virtual ~resource() = default;
+struct base {
+    virtual ~base() = default;
 
 
     virtual const entt::type_info &type() const ENTT_NOEXCEPT {
     virtual const entt::type_info &type() const ENTT_NOEXCEPT {
-        return entt::type_id<resource>();
+        return entt::type_id<base>();
     }
     }
-
-    int value;
 };
 };
 
 
-struct derived_resource: resource {
+struct derived: base {
     const entt::type_info &type() const ENTT_NOEXCEPT override {
     const entt::type_info &type() const ENTT_NOEXCEPT override {
-        return entt::type_id<derived_resource>();
-    }
-};
-
-template<typename Resource>
-struct loader {
-    using value_type = Resource;
-
-    entt::resource_handle<Resource> operator()(int value) const {
-        if(value < 0) {
-            return {};
-        }
-
-        auto res = std::shared_ptr<Resource>(new Resource);
-        res->value = value;
-        return res;
+        return entt::type_id<derived>();
     }
     }
 };
 };
 
 
 template<typename Type, typename Other>
 template<typename Type, typename Other>
-entt::resource_handle<Type> dynamic_resource_handle_cast(const entt::resource_handle<Other> &other) {
+entt::resource<Type> dynamic_resource_cast(const entt::resource<Other> &other) {
     if(other->type() == entt::type_id<Type>()) {
     if(other->type() == entt::type_id<Type>()) {
-        return entt::resource_handle<Type>{other, static_cast<Type &>(other.get())};
+        return entt::resource<Type>{other, static_cast<Type &>(*other)};
     }
     }
 
 
     return {};
     return {};
 }
 }
 
 
 TEST(Resource, Functionalities) {
 TEST(Resource, Functionalities) {
-    entt::resource_cache<resource, loader<resource>> cache;
-
-    constexpr auto hs1 = entt::hashed_string{"res1"};
-    constexpr auto hs2 = entt::hashed_string{"res2"};
-
-    ASSERT_EQ(cache.size(), 0u);
-    ASSERT_TRUE(cache.empty());
-    ASSERT_FALSE(cache.contains(hs1));
-    ASSERT_FALSE(cache.contains(hs2));
-
-    ASSERT_FALSE(cache.load(hs1, -1));
-    ASSERT_FALSE(cache.reload(hs1, -1));
-
-    ASSERT_EQ(cache.size(), 0u);
-    ASSERT_TRUE(cache.empty());
-    ASSERT_FALSE(cache.contains(hs1));
-    ASSERT_FALSE(cache.contains(hs2));
-
-    ASSERT_TRUE(cache.load(hs1, 42));
-    ASSERT_TRUE(cache.reload(hs1, 42));
-
-    ASSERT_EQ(cache.handle(hs1).use_count(), 2);
-
-    auto tmp = cache.handle(hs1);
+    entt::resource<derived> resource{};
 
 
-    ASSERT_EQ(std::as_const(cache).handle(hs1).use_count(), 3);
-    ASSERT_TRUE(static_cast<bool>(tmp));
+    ASSERT_FALSE(resource);
+    ASSERT_EQ(resource.operator->(), nullptr);
+    ASSERT_EQ(resource.use_count(), 0l);
 
 
-    tmp = {};
+    const auto value = std::make_shared<derived>();
+    entt::resource<derived> other{value};
 
 
-    ASSERT_EQ(cache.handle(hs1).use_count(), 2);
-    ASSERT_FALSE(static_cast<bool>(tmp));
-
-    ASSERT_NE(cache.size(), 0u);
-    ASSERT_FALSE(cache.empty());
-    ASSERT_TRUE(cache.contains(hs1));
-    ASSERT_FALSE(cache.contains(hs2));
-    ASSERT_EQ((*std::as_const(cache).handle(hs1)).value, 42);
-
-    ASSERT_TRUE(cache.load(hs1, 42));
-    ASSERT_TRUE(cache.load(hs2, 42));
-
-    ASSERT_NE(cache.size(), 0u);
-    ASSERT_FALSE(cache.empty());
-    ASSERT_TRUE(cache.contains(hs1));
-    ASSERT_TRUE(cache.contains(hs2));
-    ASSERT_EQ((*cache.handle(hs1)).value, 42);
-    ASSERT_EQ(std::as_const(cache).handle(hs2)->value, 42);
-
-    ASSERT_NO_FATAL_FAILURE(cache.discard(hs1));
-
-    ASSERT_FALSE(cache.contains(hs1));
-    ASSERT_TRUE(cache.contains(hs2));
-    ASSERT_EQ(cache.handle(hs2)->value, 42);
+    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.use_count(), 2l);
 
 
-    ASSERT_TRUE(cache.load(hs1, 42));
-    ASSERT_NO_FATAL_FAILURE(cache.clear());
+    entt::resource<derived> copy{resource};
+    entt::resource<derived> move{std::move(other)};
 
 
-    ASSERT_EQ(cache.size(), 0u);
-    ASSERT_TRUE(cache.empty());
-    ASSERT_FALSE(cache.contains(hs1));
-    ASSERT_FALSE(cache.contains(hs2));
+    ASSERT_FALSE(copy);
+    ASSERT_TRUE(move);
 
 
-    ASSERT_TRUE(cache.load(hs1, 42));
+    copy = std::move(move);
+    move = copy;
 
 
-    ASSERT_NE(cache.size(), 0u);
-    ASSERT_FALSE(cache.empty());
-    ASSERT_TRUE(cache.handle(hs1));
-    ASSERT_FALSE(cache.handle(hs2));
-    ASSERT_TRUE(std::as_const(cache).handle(hs1));
-    ASSERT_FALSE(std::as_const(cache).handle(hs2));
+    ASSERT_TRUE(copy);
+    ASSERT_TRUE(move);
+    ASSERT_EQ(copy, move);
+}
 
 
-    ASSERT_TRUE(std::as_const(cache).handle(hs1));
-    ASSERT_EQ(&cache.handle(hs1).get(), &static_cast<const resource &>(cache.handle(hs1)));
-    ASSERT_NO_FATAL_FAILURE(cache.clear());
+TEST(Resource, DerivedToBase) {
+    entt::resource<derived> resource{std::make_shared<derived>()};
+    entt::resource<base> other{resource};
+    entt::resource<const base> cother{resource};
 
 
-    ASSERT_EQ(cache.size(), 0u);
-    ASSERT_TRUE(cache.empty());
+    ASSERT_TRUE(resource);
+    ASSERT_TRUE(other);
+    ASSERT_TRUE(cother);
+    ASSERT_EQ(resource, other);
+    ASSERT_EQ(other, cother);
 
 
-    ASSERT_TRUE(cache.temp(42));
-    ASSERT_TRUE(cache.empty());
+    other = resource;
+    cother = resource;
 
 
-    ASSERT_FALSE(entt::resource_handle<resource>{});
-    ASSERT_TRUE(std::is_copy_constructible_v<entt::resource_handle<resource>>);
-    ASSERT_TRUE(std::is_move_constructible_v<entt::resource_handle<resource>>);
-    ASSERT_TRUE(std::is_copy_assignable_v<entt::resource_handle<resource>>);
-    ASSERT_TRUE(std::is_move_assignable_v<entt::resource_handle<resource>>);
+    ASSERT_EQ(resource, other);
+    ASSERT_EQ(other, cother);
 }
 }
 
 
-TEST(Resource, ConstNonConstHandle) {
-    entt::resource_cache<resource, loader<resource>> cache;
-
-    entt::resource_handle<resource> handle = cache.temp(42);
-    entt::resource_handle<const resource> chandle = handle;
-
-    static_assert(std::is_same_v<decltype(handle.get()), resource &>);
-    static_assert(std::is_same_v<decltype(chandle.get()), const resource &>);
-    static_assert(std::is_same_v<decltype(std::as_const(handle).get()), resource &>);
+TEST(Resource, ConstNonConstAndAllInBetween) {
+    entt::resource<derived> resource{std::make_shared<derived>()};
+    entt::resource<derived> other{resource};
 
 
-    ASSERT_TRUE(chandle);
-    ASSERT_EQ(handle, chandle);
-    ASSERT_NE(handle, entt::resource_handle<resource>{});
-    ASSERT_EQ(handle.use_count(), 2u);
-    ASSERT_EQ(chandle->value, 42);
+    static_assert(std::is_same_v<decltype(*resource), derived &>);
+    static_assert(std::is_same_v<decltype(*entt::resource<const derived>{other}), const derived &>);
+    static_assert(std::is_same_v<decltype(*std::as_const(resource)), derived &>);
 
 
-    chandle = {};
+    entt::resource<const derived> copy{resource};
+    entt::resource<const derived> move{std::move(other)};
 
 
-    ASSERT_FALSE(chandle);
-    ASSERT_EQ(handle.use_count(), 1u);
-}
+    ASSERT_TRUE(resource);
+    ASSERT_FALSE(other);
 
 
-TEST(Resource, MutableHandle) {
-    entt::resource_cache<resource, loader<resource>> cache;
+    ASSERT_TRUE(copy);
+    ASSERT_EQ(copy, resource);
+    ASSERT_NE(copy, entt::resource<derived>{});
+    ASSERT_EQ(copy.use_count(), 3u);
 
 
-    constexpr auto hs = entt::hashed_string{"res"};
-    auto handle = cache.load(hs, 0);
+    ASSERT_TRUE(move);
+    ASSERT_EQ(move, resource);
+    ASSERT_NE(move, entt::resource<derived>{});
+    ASSERT_EQ(move.use_count(), 3u);
 
 
-    ASSERT_TRUE(handle);
+    copy = resource;
+    move = std::move(resource);
 
 
-    ++handle.get().value;
-    ++static_cast<resource &>(handle).value;
-    ++(*handle).value;
-    ++handle->value;
+    ASSERT_FALSE(resource);
+    ASSERT_FALSE(other);
 
 
-    ASSERT_EQ(cache.handle(hs)->value, 4);
+    ASSERT_TRUE(copy);
+    ASSERT_TRUE(move);
+    ASSERT_EQ(copy.use_count(), 2u);
 }
 }
 
 
-TEST(Resource, HandleImplicitCast) {
-    entt::resource_cache<resource, loader<derived_resource>> cache;
-    auto handle = cache.temp(0);
-
-    auto resource = std::make_shared<derived_resource>();
-    entt::resource_handle<derived_resource> other{resource};
+TEST(Resource, DynamicResourceHandleCast) {
+    entt::resource<derived> resource{std::make_shared<derived>()};
+    entt::resource<const base> other = resource;
 
 
-    ASSERT_TRUE(handle);
     ASSERT_TRUE(other);
     ASSERT_TRUE(other);
-    ASSERT_NE(&*handle, &*other);
     ASSERT_EQ(resource.use_count(), 2u);
     ASSERT_EQ(resource.use_count(), 2u);
+    ASSERT_EQ(resource, other);
 
 
-    auto temp = std::move(handle);
-    handle = other;
+    entt::resource<const derived> cast = dynamic_resource_cast<const derived>(other);
 
 
-    ASSERT_TRUE(handle);
-    ASSERT_TRUE(other);
-    ASSERT_TRUE(temp);
-    ASSERT_EQ(&*handle, &*other);
-    ASSERT_EQ(resource.use_count(), 3u);
-
-    temp = std::move(other);
-
-    ASSERT_TRUE(handle);
-    ASSERT_FALSE(other);
-    ASSERT_TRUE(temp);
-    ASSERT_EQ(&*handle, &*temp);
+    ASSERT_TRUE(cast);
     ASSERT_EQ(resource.use_count(), 3u);
     ASSERT_EQ(resource.use_count(), 3u);
+    ASSERT_EQ(resource, cast);
 
 
-    temp = handle = {};
+    other = entt::resource<base>{std::make_shared<base>()};
+    cast = dynamic_resource_cast<const derived>(other);
 
 
-    ASSERT_FALSE(handle);
-    ASSERT_FALSE(other);
-    ASSERT_FALSE(temp);
+    ASSERT_FALSE(cast);
     ASSERT_EQ(resource.use_count(), 1u);
     ASSERT_EQ(resource.use_count(), 1u);
 }
 }
-
-TEST(Resource, DynamicResourceHandleCast) {
-    entt::resource_handle<derived_resource> handle = entt::resource_cache<derived_resource, loader<derived_resource>>{}.temp(42);
-    entt::resource_handle<const resource> base = handle;
-
-    ASSERT_TRUE(base);
-    ASSERT_EQ(handle.use_count(), 2u);
-    ASSERT_EQ(base->value, 42);
-
-    entt::resource_handle<const derived_resource> chandle = dynamic_resource_handle_cast<const derived_resource>(base);
-
-    ASSERT_TRUE(chandle);
-    ASSERT_EQ(handle.use_count(), 3u);
-    ASSERT_EQ(chandle->value, 42);
-
-    base = entt::resource_cache<resource, loader<resource>>{}.temp(42);
-    chandle = dynamic_resource_handle_cast<const derived_resource>(base);
-
-    ASSERT_FALSE(chandle);
-    ASSERT_EQ(handle.use_count(), 1u);
-}
-
-TEST(Resource, Each) {
-    using namespace entt::literals;
-
-    entt::resource_cache<resource, loader<resource>> cache;
-    cache.load("resource"_hs, 0);
-
-    cache.each([](entt::resource_handle<resource> res) {
-        ++res->value;
-    });
-
-    ASSERT_FALSE(cache.empty());
-    ASSERT_EQ(cache.handle("resource"_hs)->value, 1);
-
-    cache.each([](entt::id_type id, entt::resource_handle<resource> res) {
-        ASSERT_EQ(id, "resource"_hs);
-        ++res->value;
-    });
-
-    ASSERT_FALSE(cache.empty());
-
-    std::as_const(cache).each([](entt::id_type id, entt::resource_handle<const resource> res) {
-        ASSERT_EQ(res->value, 2);
-    });
-
-    cache.each([&cache](entt::id_type id) {
-        cache.discard(id);
-    });
-
-    ASSERT_TRUE(cache.empty());
-}

+ 428 - 0
test/entt/resource/resource_cache.cpp

@@ -0,0 +1,428 @@
+#include <cstddef>
+#include <memory>
+#include <tuple>
+#include <utility>
+#include <gtest/gtest.h>
+#include <entt/core/hashed_string.hpp>
+#include <entt/resource/cache.hpp>
+#include <entt/resource/loader.hpp>
+#include "../common/throwing_allocator.hpp"
+
+struct broken_tag {};
+struct with_callback {};
+
+template<typename Type>
+struct loader {
+    using result_type = std::shared_ptr<Type>;
+
+    template<typename... Args>
+    result_type operator()(Args &&...args) const {
+        return std::make_shared<Type>(std::forward<Args>(args)...);
+    }
+
+    template<typename Func>
+    result_type operator()(with_callback, Func &&func) const {
+        return std::forward<Func>(func)();
+    }
+
+    template<typename... Args>
+    result_type operator()(broken_tag, Args &&...) const {
+        return {};
+    }
+};
+
+TEST(ResourceCache, Functionalities) {
+    using namespace entt::literals;
+
+    entt::resource_cache<int> cache;
+
+    ASSERT_NO_THROW([[maybe_unused]] auto alloc = cache.get_allocator());
+
+    ASSERT_TRUE(cache.empty());
+    ASSERT_EQ(cache.size(), 0u);
+
+    ASSERT_EQ(cache.begin(), cache.end());
+    ASSERT_EQ(std::as_const(cache).begin(), std::as_const(cache).end());
+    ASSERT_EQ(cache.cbegin(), cache.cend());
+
+    ASSERT_FALSE(cache.contains("resource"_hs));
+
+    cache.load("resource"_hs, 42);
+
+    ASSERT_FALSE(cache.empty());
+    ASSERT_EQ(cache.size(), 1u);
+
+    ASSERT_NE(cache.begin(), cache.end());
+    ASSERT_NE(std::as_const(cache).begin(), std::as_const(cache).end());
+    ASSERT_NE(cache.cbegin(), cache.cend());
+
+    ASSERT_TRUE(cache.contains("resource"_hs));
+
+    cache.clear();
+
+    ASSERT_TRUE(cache.empty());
+    ASSERT_EQ(cache.size(), 0u);
+
+    ASSERT_EQ(cache.begin(), cache.end());
+    ASSERT_EQ(std::as_const(cache).begin(), std::as_const(cache).end());
+    ASSERT_EQ(cache.cbegin(), cache.cend());
+
+    ASSERT_FALSE(cache.contains("resource"_hs));
+}
+
+TEST(ResourceCache, Constructors) {
+    using namespace entt::literals;
+
+    entt::resource_cache<int> cache;
+
+    cache = entt::resource_cache<int>{std::allocator<int>{}};
+    cache = entt::resource_cache<int>{entt::resource_loader<int>{}, std::allocator<float>{}};
+
+    cache.load("resource"_hs, 42u);
+
+    entt::resource_cache<int> temp{cache, cache.get_allocator()};
+    entt::resource_cache<int> other{std::move(temp), cache.get_allocator()};
+
+    ASSERT_EQ(cache.size(), 1u);
+    ASSERT_EQ(other.size(), 1u);
+}
+
+TEST(ResourceCache, Copy) {
+    using namespace entt::literals;
+
+    entt::resource_cache<std::size_t> cache;
+    cache.load("resource"_hs, 42u);
+
+    entt::resource_cache<std::size_t> other{cache};
+
+    ASSERT_TRUE(cache.contains("resource"_hs));
+    ASSERT_TRUE(other.contains("resource"_hs));
+
+    cache.load("foo"_hs, 99u);
+    cache.load("bar"_hs, 77u);
+    other.load("quux"_hs, 0u);
+    other = cache;
+
+    ASSERT_TRUE(other.contains("resource"_hs));
+    ASSERT_TRUE(other.contains("foo"_hs));
+    ASSERT_TRUE(other.contains("bar"_hs));
+    ASSERT_FALSE(other.contains("quux"_hs));
+
+    ASSERT_EQ(other["resource"_hs], 42u);
+    ASSERT_EQ(other["foo"_hs], 99u);
+    ASSERT_EQ(other["bar"_hs], 77u);
+}
+
+TEST(ResourceCache, Move) {
+    using namespace entt::literals;
+
+    entt::resource_cache<std::size_t> cache;
+    cache.load("resource"_hs, 42u);
+
+    entt::resource_cache<std::size_t> other{std::move(cache)};
+
+    ASSERT_EQ(cache.size(), 0u);
+    ASSERT_TRUE(other.contains("resource"_hs));
+
+    cache = other;
+    cache.load("foo"_hs, 99u);
+    cache.load("bar"_hs, 77u);
+    other.load("quux"_hs, 0u);
+    other = std::move(cache);
+
+    ASSERT_EQ(cache.size(), 0u);
+    ASSERT_TRUE(other.contains("resource"_hs));
+    ASSERT_TRUE(other.contains("foo"_hs));
+    ASSERT_TRUE(other.contains("bar"_hs));
+    ASSERT_FALSE(other.contains("quux"_hs));
+
+    ASSERT_EQ(other["resource"_hs], 42u);
+    ASSERT_EQ(other["foo"_hs], 99u);
+    ASSERT_EQ(other["bar"_hs], 77u);
+}
+
+TEST(ResourceCache, Iterator) {
+    using namespace entt::literals;
+
+    using iterator = typename entt::resource_cache<int>::iterator;
+
+    static_assert(std::is_same_v<iterator::value_type, std::pair<entt::id_type, entt::resource<int>>>);
+    static_assert(std::is_same_v<iterator::pointer, entt::input_iterator_pointer<std::pair<entt::id_type, entt::resource<int>>>>);
+    static_assert(std::is_same_v<iterator::reference, std::pair<entt::id_type, entt::resource<int>>>);
+
+    entt::resource_cache<int> cache;
+    cache.load("resource"_hs, 42);
+
+    iterator end{cache.begin()};
+    iterator begin{};
+    begin = cache.end();
+    std::swap(begin, end);
+
+    ASSERT_EQ(begin, cache.begin());
+    ASSERT_EQ(end, cache.end());
+    ASSERT_NE(begin, end);
+
+    ASSERT_EQ(begin++, cache.begin());
+    ASSERT_EQ(begin--, cache.end());
+
+    ASSERT_EQ(begin + 1, cache.end());
+    ASSERT_EQ(end - 1, cache.begin());
+
+    ASSERT_EQ(++begin, cache.end());
+    ASSERT_EQ(--begin, cache.begin());
+
+    ASSERT_EQ(begin += 1, cache.end());
+    ASSERT_EQ(begin -= 1, cache.begin());
+
+    ASSERT_EQ(begin + (end - begin), cache.end());
+    ASSERT_EQ(begin - (begin - end), cache.end());
+
+    ASSERT_EQ(end - (end - begin), cache.begin());
+    ASSERT_EQ(end + (begin - end), cache.begin());
+
+    ASSERT_EQ(begin[0u].first, cache.begin()->first);
+    ASSERT_EQ(begin[0u].second, (*cache.begin()).second);
+
+    ASSERT_LT(begin, end);
+    ASSERT_LE(begin, cache.begin());
+
+    ASSERT_GT(end, begin);
+    ASSERT_GE(end, cache.end());
+
+    cache.load("other"_hs, 3);
+    begin = cache.begin();
+
+    ASSERT_EQ(begin[0u].first, "resource"_hs);
+    ASSERT_EQ(begin[1u].second, 3);
+}
+
+TEST(ResourceCache, ConstIterator) {
+    using namespace entt::literals;
+
+    using iterator = typename entt::resource_cache<int>::const_iterator;
+
+    static_assert(std::is_same_v<iterator::value_type, std::pair<entt::id_type, entt::resource<const int>>>);
+    static_assert(std::is_same_v<iterator::pointer, entt::input_iterator_pointer<std::pair<entt::id_type, entt::resource<const int>>>>);
+    static_assert(std::is_same_v<iterator::reference, std::pair<entt::id_type, entt::resource<const int>>>);
+
+    entt::resource_cache<int> cache;
+    cache.load("resource"_hs, 42);
+
+    iterator cend{cache.cbegin()};
+    iterator cbegin{};
+    cbegin = cache.cend();
+    std::swap(cbegin, cend);
+
+    ASSERT_EQ(cbegin, cache.cbegin());
+    ASSERT_EQ(cend, cache.cend());
+    ASSERT_NE(cbegin, cend);
+
+    ASSERT_EQ(cbegin++, cache.cbegin());
+    ASSERT_EQ(cbegin--, cache.cend());
+
+    ASSERT_EQ(cbegin + 1, cache.cend());
+    ASSERT_EQ(cend - 1, cache.cbegin());
+
+    ASSERT_EQ(++cbegin, cache.cend());
+    ASSERT_EQ(--cbegin, cache.cbegin());
+
+    ASSERT_EQ(cbegin += 1, cache.cend());
+    ASSERT_EQ(cbegin -= 1, cache.cbegin());
+
+    ASSERT_EQ(cbegin + (cend - cbegin), cache.cend());
+    ASSERT_EQ(cbegin - (cbegin - cend), cache.cend());
+
+    ASSERT_EQ(cend - (cend - cbegin), cache.cbegin());
+    ASSERT_EQ(cend + (cbegin - cend), cache.cbegin());
+
+    ASSERT_EQ(cbegin[0u].first, cache.cbegin()->first);
+    ASSERT_EQ(cbegin[0u].second, (*cache.cbegin()).second);
+
+    ASSERT_LT(cbegin, cend);
+    ASSERT_LE(cbegin, cache.cbegin());
+
+    ASSERT_GT(cend, cbegin);
+    ASSERT_GE(cend, cache.cend());
+
+    cache.load("other"_hs, 3);
+    cbegin = cache.cbegin();
+
+    ASSERT_EQ(cbegin[0u].first, "resource"_hs);
+    ASSERT_EQ(cbegin[1u].second, 3);
+}
+
+TEST(ResourceCache, IteratorConversion) {
+    using namespace entt::literals;
+
+    entt::resource_cache<int> cache;
+    cache.load("resource"_hs, 42);
+
+    typename entt::resource_cache<int>::iterator it = cache.begin();
+    typename entt::resource_cache<int>::const_iterator cit = it;
+
+    static_assert(std::is_same_v<decltype(*it), std::pair<entt::id_type, entt::resource<int>>>);
+    static_assert(std::is_same_v<decltype(*cit), std::pair<entt::id_type, entt::resource<const int>>>);
+
+    ASSERT_EQ(it->first, "resource"_hs);
+    ASSERT_EQ((*it).second, 42);
+    ASSERT_EQ(it->first, cit->first);
+    ASSERT_EQ((*it).second, (*cit).second);
+
+    ASSERT_EQ(it - cit, 0);
+    ASSERT_EQ(cit - it, 0);
+    ASSERT_LE(it, cit);
+    ASSERT_LE(cit, it);
+    ASSERT_GE(it, cit);
+    ASSERT_GE(cit, it);
+    ASSERT_EQ(it, cit);
+    ASSERT_NE(++cit, it);
+}
+
+TEST(ResourceCache, Load) {
+    using namespace entt::literals;
+
+    entt::resource_cache<int> cache;
+    typename entt::resource_cache<int>::iterator it;
+    bool result;
+
+    ASSERT_TRUE(cache.empty());
+    ASSERT_EQ(cache.size(), 0u);
+    ASSERT_EQ(cache["resource"_hs], entt::resource<int>{});
+    ASSERT_EQ(std::as_const(cache)["resource"_hs], entt::resource<const int>{});
+    ASSERT_FALSE(cache.contains("resource"_hs));
+
+    std::tie(it, result) = cache.load("resource"_hs, 1);
+
+    ASSERT_TRUE(result);
+    ASSERT_EQ(cache.size(), 1u);
+    ASSERT_EQ(it, --cache.end());
+    ASSERT_TRUE(cache.contains("resource"_hs));
+    ASSERT_NE(cache["resource"_hs], entt::resource<int>{});
+    ASSERT_NE(std::as_const(cache)["resource"_hs], entt::resource<const int>{});
+    ASSERT_EQ(it->first, "resource"_hs);
+    ASSERT_EQ(it->second, 1);
+
+    std::tie(it, result) = cache.load("resource"_hs, 2);
+
+    ASSERT_FALSE(result);
+    ASSERT_EQ(cache.size(), 1u);
+    ASSERT_EQ(it, --cache.end());
+    ASSERT_EQ(it->second, 1);
+
+    std::tie(it, result) = cache.force_load("resource"_hs, 3);
+
+    ASSERT_TRUE(result);
+    ASSERT_EQ(cache.size(), 1u);
+    ASSERT_EQ(it, --cache.end());
+    ASSERT_EQ(it->second, 3);
+}
+
+TEST(ResourceCache, Erase) {
+    static constexpr std::size_t resource_count = 8u;
+    entt::resource_cache<std::size_t> cache;
+
+    for(std::size_t next{}, last = resource_count + 1u; next < last; ++next) {
+        cache.load(next, next);
+    }
+
+    ASSERT_EQ(cache.size(), resource_count + 1u);
+
+    for(std::size_t next{}, last = resource_count + 1u; next < last; ++next) {
+        ASSERT_TRUE(cache.contains(next));
+    }
+
+    auto it = cache.erase(++cache.begin());
+    it = cache.erase(it, it + 1);
+
+    ASSERT_EQ((--cache.end())->first, 6u);
+    ASSERT_EQ(cache.erase(6u), 1u);
+    ASSERT_EQ(cache.erase(6u), 0u);
+
+    ASSERT_EQ(cache.size(), resource_count + 1u - 3u);
+
+    ASSERT_EQ(it, ++cache.begin());
+    ASSERT_EQ(it->first, 7u);
+    ASSERT_EQ((--cache.end())->first, 5u);
+
+    for(std::size_t next{}, last = resource_count + 1u; next < last; ++next) {
+        if(next == 1u || next == 8u || next == 6u) {
+            ASSERT_FALSE(cache.contains(next));
+        } else {
+            ASSERT_TRUE(cache.contains(next));
+        }
+    }
+
+    cache.erase(cache.begin(), cache.end());
+
+    for(std::size_t next{}, last = resource_count + 1u; next < last; ++next) {
+        ASSERT_FALSE(cache.contains(next));
+    }
+
+    ASSERT_EQ(cache.size(), 0u);
+}
+
+TEST(ResourceCache, Indexing) {
+    using namespace entt::literals;
+
+    entt::resource_cache<int> cache;
+
+    ASSERT_FALSE(cache.contains("resource"_hs));
+    ASSERT_FALSE(cache["resource"_hs]);
+    ASSERT_FALSE(std::as_const(cache)["resource"_hs]);
+
+    cache.load("resource"_hs, 99);
+
+    ASSERT_TRUE(cache.contains("resource"_hs));
+    ASSERT_EQ(cache[std::move("resource"_hs)], 99);
+    ASSERT_EQ(std::as_const(cache)["resource"_hs], 99);
+}
+
+TEST(ResourceCache, LoaderDispatching) {
+    using namespace entt::literals;
+
+    entt::resource_cache<int, loader<int>> cache;
+    cache.force_load("resource"_hs, 99);
+
+    ASSERT_TRUE(cache.contains("resource"_hs));
+    ASSERT_EQ(cache["resource"_hs], 99);
+
+    cache.force_load("resource"_hs, with_callback{}, []() { return std::make_shared<int>(42); });
+
+    ASSERT_TRUE(cache.contains("resource"_hs));
+    ASSERT_EQ(cache["resource"_hs], 42);
+}
+
+TEST(ResourceCache, BrokenLoader) {
+    using namespace entt::literals;
+
+    entt::resource_cache<int, loader<int>> cache;
+    cache.load("resource"_hs, broken_tag{});
+
+    ASSERT_TRUE(cache.contains("resource"_hs));
+    ASSERT_FALSE(cache["resource"_hs]);
+
+    cache.force_load("resource"_hs, 42);
+
+    ASSERT_TRUE(cache.contains("resource"_hs));
+    ASSERT_TRUE(cache["resource"_hs]);
+}
+
+TEST(ResourceCache, ThrowingAllocator) {
+    using namespace entt::literals;
+
+    using allocator = test::throwing_allocator<std::size_t>;
+    using packed_allocator = test::throwing_allocator<entt::internal::dense_map_node<entt::id_type, std::shared_ptr<std::size_t>>>;
+    using packed_exception = typename packed_allocator::exception_type;
+
+    entt::resource_cache<std::size_t, entt::resource_loader<std::size_t>, allocator> cache{};
+
+    packed_allocator::trigger_on_allocate = true;
+
+    ASSERT_THROW(cache.load("resource"_hs), packed_exception);
+    ASSERT_FALSE(cache.contains("resource"_hs));
+
+    packed_allocator::trigger_on_allocate = true;
+
+    ASSERT_THROW(cache.force_load("resource"_hs), packed_exception);
+    ASSERT_FALSE(cache.contains("resource"_hs));
+}

+ 13 - 0
test/entt/resource/resource_loader.cpp

@@ -0,0 +1,13 @@
+#include <type_traits>
+#include <gtest/gtest.h>
+#include <entt/resource/loader.hpp>
+
+TEST(ResourceLoader, Functionalities) {
+    using loader_type = entt::resource_loader<int>;
+    const auto resource = loader_type{}(42);
+
+    static_assert(std::is_same_v<typename loader_type::result_type, std::shared_ptr<int>>);
+
+    ASSERT_TRUE(resource);
+    ASSERT_EQ(*resource, 42);
+}