resource_cache.cpp 13 KB

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