resource_cache.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #include <cstddef>
  2. #include <memory>
  3. #include <tuple>
  4. #include <utility>
  5. #include <gtest/gtest.h>
  6. #include <entt/container/dense_map.hpp>
  7. #include <entt/core/hashed_string.hpp>
  8. #include <entt/core/iterator.hpp>
  9. #include <entt/resource/cache.hpp>
  10. #include <entt/resource/loader.hpp>
  11. #include <entt/resource/resource.hpp>
  12. #include "../common/throwing_allocator.hpp"
  13. struct broken_tag {};
  14. struct with_callback {};
  15. template<typename Type>
  16. struct loader {
  17. using result_type = std::shared_ptr<Type>;
  18. template<typename... Args>
  19. result_type operator()(Args &&...args) const {
  20. return std::make_shared<Type>(std::forward<Args>(args)...);
  21. }
  22. template<typename Func>
  23. result_type operator()(with_callback, Func &&func) const {
  24. return std::forward<Func>(func)();
  25. }
  26. template<typename... Args>
  27. result_type operator()(broken_tag, Args &&...) const {
  28. return {};
  29. }
  30. };
  31. TEST(ResourceCache, Functionalities) {
  32. using namespace entt::literals;
  33. entt::resource_cache<int> cache;
  34. ASSERT_NO_FATAL_FAILURE([[maybe_unused]] auto alloc = cache.get_allocator());
  35. ASSERT_TRUE(cache.empty());
  36. ASSERT_EQ(cache.size(), 0u);
  37. ASSERT_EQ(cache.begin(), cache.end());
  38. ASSERT_EQ(std::as_const(cache).begin(), std::as_const(cache).end());
  39. ASSERT_EQ(cache.cbegin(), cache.cend());
  40. ASSERT_FALSE(cache.contains("resource"_hs));
  41. cache.load("resource"_hs, 42);
  42. ASSERT_FALSE(cache.empty());
  43. ASSERT_EQ(cache.size(), 1u);
  44. ASSERT_NE(cache.begin(), cache.end());
  45. ASSERT_NE(std::as_const(cache).begin(), std::as_const(cache).end());
  46. ASSERT_NE(cache.cbegin(), cache.cend());
  47. ASSERT_TRUE(cache.contains("resource"_hs));
  48. cache.clear();
  49. ASSERT_TRUE(cache.empty());
  50. ASSERT_EQ(cache.size(), 0u);
  51. ASSERT_EQ(cache.begin(), cache.end());
  52. ASSERT_EQ(std::as_const(cache).begin(), std::as_const(cache).end());
  53. ASSERT_EQ(cache.cbegin(), cache.cend());
  54. ASSERT_FALSE(cache.contains("resource"_hs));
  55. }
  56. TEST(ResourceCache, Constructors) {
  57. using namespace entt::literals;
  58. entt::resource_cache<int> cache;
  59. cache = entt::resource_cache<int>{std::allocator<int>{}};
  60. cache = entt::resource_cache<int>{entt::resource_loader<int>{}, std::allocator<float>{}};
  61. cache.load("resource"_hs, 42u);
  62. entt::resource_cache<int> temp{cache, cache.get_allocator()};
  63. entt::resource_cache<int> other{std::move(temp), cache.get_allocator()};
  64. ASSERT_EQ(cache.size(), 1u);
  65. ASSERT_EQ(other.size(), 1u);
  66. }
  67. TEST(ResourceCache, Copy) {
  68. using namespace entt::literals;
  69. entt::resource_cache<std::size_t> cache;
  70. cache.load("resource"_hs, 42u);
  71. entt::resource_cache<std::size_t> other{cache};
  72. ASSERT_TRUE(cache.contains("resource"_hs));
  73. ASSERT_TRUE(other.contains("resource"_hs));
  74. cache.load("foo"_hs, 99u);
  75. cache.load("bar"_hs, 77u);
  76. other.load("quux"_hs, 0u);
  77. other = cache;
  78. ASSERT_TRUE(other.contains("resource"_hs));
  79. ASSERT_TRUE(other.contains("foo"_hs));
  80. ASSERT_TRUE(other.contains("bar"_hs));
  81. ASSERT_FALSE(other.contains("quux"_hs));
  82. ASSERT_EQ(other["resource"_hs], 42u);
  83. ASSERT_EQ(other["foo"_hs], 99u);
  84. ASSERT_EQ(other["bar"_hs], 77u);
  85. }
  86. TEST(ResourceCache, Move) {
  87. using namespace entt::literals;
  88. entt::resource_cache<std::size_t> cache;
  89. cache.load("resource"_hs, 42u);
  90. entt::resource_cache<std::size_t> other{std::move(cache)};
  91. ASSERT_EQ(cache.size(), 0u);
  92. ASSERT_TRUE(other.contains("resource"_hs));
  93. cache = other;
  94. cache.load("foo"_hs, 99u);
  95. cache.load("bar"_hs, 77u);
  96. other.load("quux"_hs, 0u);
  97. other = std::move(cache);
  98. ASSERT_EQ(cache.size(), 0u);
  99. ASSERT_TRUE(other.contains("resource"_hs));
  100. ASSERT_TRUE(other.contains("foo"_hs));
  101. ASSERT_TRUE(other.contains("bar"_hs));
  102. ASSERT_FALSE(other.contains("quux"_hs));
  103. ASSERT_EQ(other["resource"_hs], 42u);
  104. ASSERT_EQ(other["foo"_hs], 99u);
  105. ASSERT_EQ(other["bar"_hs], 77u);
  106. }
  107. TEST(ResourceCache, Iterator) {
  108. using namespace entt::literals;
  109. using iterator = typename entt::resource_cache<int>::iterator;
  110. testing::StaticAssertTypeEq<iterator::value_type, std::pair<entt::id_type, entt::resource<int>>>();
  111. testing::StaticAssertTypeEq<iterator::pointer, entt::input_iterator_pointer<std::pair<entt::id_type, entt::resource<int>>>>();
  112. testing::StaticAssertTypeEq<iterator::reference, std::pair<entt::id_type, entt::resource<int>>>();
  113. entt::resource_cache<int> cache;
  114. cache.load("resource"_hs, 42);
  115. iterator end{cache.begin()};
  116. iterator begin{};
  117. begin = cache.end();
  118. std::swap(begin, end);
  119. ASSERT_EQ(begin, cache.begin());
  120. ASSERT_EQ(end, cache.end());
  121. ASSERT_NE(begin, end);
  122. ASSERT_EQ(begin++, cache.begin());
  123. ASSERT_EQ(begin--, cache.end());
  124. ASSERT_EQ(begin + 1, cache.end());
  125. ASSERT_EQ(end - 1, cache.begin());
  126. ASSERT_EQ(++begin, cache.end());
  127. ASSERT_EQ(--begin, cache.begin());
  128. ASSERT_EQ(begin += 1, cache.end());
  129. ASSERT_EQ(begin -= 1, cache.begin());
  130. ASSERT_EQ(begin + (end - begin), cache.end());
  131. ASSERT_EQ(begin - (begin - end), cache.end());
  132. ASSERT_EQ(end - (end - begin), cache.begin());
  133. ASSERT_EQ(end + (begin - end), cache.begin());
  134. ASSERT_EQ(begin[0u].first, cache.begin()->first);
  135. ASSERT_EQ(begin[0u].second, (*cache.begin()).second);
  136. ASSERT_LT(begin, end);
  137. ASSERT_LE(begin, cache.begin());
  138. ASSERT_GT(end, begin);
  139. ASSERT_GE(end, cache.end());
  140. cache.load("other"_hs, 3);
  141. begin = cache.begin();
  142. ASSERT_EQ(begin[0u].first, "resource"_hs);
  143. ASSERT_EQ(begin[1u].second, 3);
  144. }
  145. TEST(ResourceCache, ConstIterator) {
  146. using namespace entt::literals;
  147. using iterator = typename entt::resource_cache<int>::const_iterator;
  148. testing::StaticAssertTypeEq<iterator::value_type, std::pair<entt::id_type, entt::resource<const int>>>();
  149. testing::StaticAssertTypeEq<iterator::pointer, entt::input_iterator_pointer<std::pair<entt::id_type, entt::resource<const int>>>>();
  150. testing::StaticAssertTypeEq<iterator::reference, std::pair<entt::id_type, entt::resource<const int>>>();
  151. entt::resource_cache<int> cache;
  152. cache.load("resource"_hs, 42);
  153. iterator cend{cache.cbegin()};
  154. iterator cbegin{};
  155. cbegin = cache.cend();
  156. std::swap(cbegin, cend);
  157. ASSERT_EQ(cbegin, cache.cbegin());
  158. ASSERT_EQ(cend, cache.cend());
  159. ASSERT_NE(cbegin, cend);
  160. ASSERT_EQ(cbegin++, cache.cbegin());
  161. ASSERT_EQ(cbegin--, cache.cend());
  162. ASSERT_EQ(cbegin + 1, cache.cend());
  163. ASSERT_EQ(cend - 1, cache.cbegin());
  164. ASSERT_EQ(++cbegin, cache.cend());
  165. ASSERT_EQ(--cbegin, cache.cbegin());
  166. ASSERT_EQ(cbegin += 1, cache.cend());
  167. ASSERT_EQ(cbegin -= 1, cache.cbegin());
  168. ASSERT_EQ(cbegin + (cend - cbegin), cache.cend());
  169. ASSERT_EQ(cbegin - (cbegin - cend), cache.cend());
  170. ASSERT_EQ(cend - (cend - cbegin), cache.cbegin());
  171. ASSERT_EQ(cend + (cbegin - cend), cache.cbegin());
  172. ASSERT_EQ(cbegin[0u].first, cache.cbegin()->first);
  173. ASSERT_EQ(cbegin[0u].second, (*cache.cbegin()).second);
  174. ASSERT_LT(cbegin, cend);
  175. ASSERT_LE(cbegin, cache.cbegin());
  176. ASSERT_GT(cend, cbegin);
  177. ASSERT_GE(cend, cache.cend());
  178. cache.load("other"_hs, 3);
  179. cbegin = cache.cbegin();
  180. ASSERT_EQ(cbegin[0u].first, "resource"_hs);
  181. ASSERT_EQ(cbegin[1u].second, 3);
  182. }
  183. TEST(ResourceCache, IteratorConversion) {
  184. using namespace entt::literals;
  185. entt::resource_cache<int> cache;
  186. cache.load("resource"_hs, 42);
  187. typename entt::resource_cache<int>::iterator it = cache.begin();
  188. typename entt::resource_cache<int>::const_iterator cit = it;
  189. testing::StaticAssertTypeEq<decltype(*it), std::pair<entt::id_type, entt::resource<int>>>();
  190. testing::StaticAssertTypeEq<decltype(*cit), std::pair<entt::id_type, entt::resource<const int>>>();
  191. ASSERT_EQ(it->first, "resource"_hs);
  192. ASSERT_EQ((*it).second, 42);
  193. ASSERT_EQ(it->first, cit->first);
  194. ASSERT_EQ((*it).second, (*cit).second);
  195. ASSERT_EQ(it - cit, 0);
  196. ASSERT_EQ(cit - it, 0);
  197. ASSERT_LE(it, cit);
  198. ASSERT_LE(cit, it);
  199. ASSERT_GE(it, cit);
  200. ASSERT_GE(cit, it);
  201. ASSERT_EQ(it, cit);
  202. ASSERT_NE(++cit, it);
  203. }
  204. TEST(ResourceCache, Load) {
  205. using namespace entt::literals;
  206. entt::resource_cache<int> cache;
  207. typename entt::resource_cache<int>::iterator it;
  208. bool result;
  209. ASSERT_TRUE(cache.empty());
  210. ASSERT_EQ(cache.size(), 0u);
  211. ASSERT_EQ(cache["resource"_hs], entt::resource<int>{});
  212. ASSERT_EQ(std::as_const(cache)["resource"_hs], entt::resource<const int>{});
  213. ASSERT_FALSE(cache.contains("resource"_hs));
  214. std::tie(it, result) = cache.load("resource"_hs, 1);
  215. ASSERT_TRUE(result);
  216. ASSERT_EQ(cache.size(), 1u);
  217. ASSERT_EQ(it, --cache.end());
  218. ASSERT_TRUE(cache.contains("resource"_hs));
  219. ASSERT_NE(cache["resource"_hs], entt::resource<int>{});
  220. ASSERT_NE(std::as_const(cache)["resource"_hs], entt::resource<const int>{});
  221. ASSERT_EQ(it->first, "resource"_hs);
  222. ASSERT_EQ(it->second, 1);
  223. std::tie(it, result) = cache.load("resource"_hs, 2);
  224. ASSERT_FALSE(result);
  225. ASSERT_EQ(cache.size(), 1u);
  226. ASSERT_EQ(it, --cache.end());
  227. ASSERT_EQ(it->second, 1);
  228. std::tie(it, result) = cache.force_load("resource"_hs, 3);
  229. ASSERT_TRUE(result);
  230. ASSERT_EQ(cache.size(), 1u);
  231. ASSERT_EQ(it, --cache.end());
  232. ASSERT_EQ(it->second, 3);
  233. }
  234. TEST(ResourceCache, Erase) {
  235. constexpr std::size_t resource_count = 8u;
  236. entt::resource_cache<std::size_t> cache;
  237. for(std::size_t next{}, last = resource_count + 1u; next < last; ++next) {
  238. cache.load(static_cast<entt::id_type>(next), next);
  239. }
  240. ASSERT_EQ(cache.size(), resource_count + 1u);
  241. for(std::size_t next{}, last = resource_count + 1u; next < last; ++next) {
  242. ASSERT_TRUE(cache.contains(static_cast<entt::id_type>(next)));
  243. }
  244. auto it = cache.erase(++cache.begin());
  245. it = cache.erase(it, it + 1);
  246. ASSERT_EQ((--cache.end())->first, 6u);
  247. ASSERT_EQ(cache.erase(6u), 1u);
  248. ASSERT_EQ(cache.erase(6u), 0u);
  249. ASSERT_EQ(cache.size(), resource_count + 1u - 3u);
  250. ASSERT_EQ(it, ++cache.begin());
  251. ASSERT_EQ(it->first, 7u);
  252. ASSERT_EQ((--cache.end())->first, 5u);
  253. for(std::size_t next{}, last = resource_count + 1u; next < last; ++next) {
  254. if(next == 1u || next == 8u || next == 6u) {
  255. ASSERT_FALSE(cache.contains(static_cast<entt::id_type>(next)));
  256. } else {
  257. ASSERT_TRUE(cache.contains(static_cast<entt::id_type>(next)));
  258. }
  259. }
  260. cache.erase(cache.begin(), cache.end());
  261. for(std::size_t next{}, last = resource_count + 1u; next < last; ++next) {
  262. ASSERT_FALSE(cache.contains(static_cast<entt::id_type>(next)));
  263. }
  264. ASSERT_EQ(cache.size(), 0u);
  265. }
  266. TEST(ResourceCache, Indexing) {
  267. using namespace entt::literals;
  268. entt::resource_cache<int> cache;
  269. ASSERT_FALSE(cache.contains("resource"_hs));
  270. ASSERT_FALSE(cache["resource"_hs]);
  271. ASSERT_FALSE(std::as_const(cache)["resource"_hs]);
  272. cache.load("resource"_hs, 99);
  273. ASSERT_TRUE(cache.contains("resource"_hs));
  274. ASSERT_EQ(cache[std::move("resource"_hs)], 99);
  275. ASSERT_EQ(std::as_const(cache)["resource"_hs], 99);
  276. }
  277. TEST(ResourceCache, LoaderDispatching) {
  278. using namespace entt::literals;
  279. entt::resource_cache<int, loader<int>> cache;
  280. cache.force_load("resource"_hs, 99);
  281. ASSERT_TRUE(cache.contains("resource"_hs));
  282. ASSERT_EQ(cache["resource"_hs], 99);
  283. cache.force_load("resource"_hs, with_callback{}, []() { return std::make_shared<int>(42); });
  284. ASSERT_TRUE(cache.contains("resource"_hs));
  285. ASSERT_EQ(cache["resource"_hs], 42);
  286. }
  287. TEST(ResourceCache, BrokenLoader) {
  288. using namespace entt::literals;
  289. entt::resource_cache<int, loader<int>> cache;
  290. cache.load("resource"_hs, broken_tag{});
  291. ASSERT_TRUE(cache.contains("resource"_hs));
  292. ASSERT_FALSE(cache["resource"_hs]);
  293. cache.force_load("resource"_hs, 42);
  294. ASSERT_TRUE(cache.contains("resource"_hs));
  295. ASSERT_TRUE(cache["resource"_hs]);
  296. }
  297. TEST(ResourceCache, ThrowingAllocator) {
  298. using namespace entt::literals;
  299. using allocator = test::throwing_allocator<std::size_t>;
  300. using packed_allocator = test::throwing_allocator<entt::internal::dense_map_node<entt::id_type, std::shared_ptr<std::size_t>>>;
  301. using packed_exception = typename packed_allocator::exception_type;
  302. entt::resource_cache<std::size_t, entt::resource_loader<std::size_t>, allocator> cache{};
  303. packed_allocator::trigger_on_allocate = true;
  304. ASSERT_THROW(cache.load("resource"_hs), packed_exception);
  305. ASSERT_FALSE(cache.contains("resource"_hs));
  306. packed_allocator::trigger_on_allocate = true;
  307. ASSERT_THROW(cache.force_load("resource"_hs), packed_exception);
  308. ASSERT_FALSE(cache.contains("resource"_hs));
  309. }