/******************************************************************************* * This file is part of the "Enduro2D" * For conditions of distribution and use, see copyright notice in LICENSE.md * Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com) ******************************************************************************/ #include "_high.hpp" using namespace e2d; namespace { class safe_starter_initializer final : private noncopyable { public: safe_starter_initializer() { modules::initialize(0, nullptr, starter::parameters( engine::parameters("library_untests", "enduro2d"))); } ~safe_starter_initializer() noexcept { modules::shutdown(); } }; class fake_asset final : public content_asset { public: static const char* type_name() noexcept { return "fake_asset"; } static load_async_result load_async(const library& library, str_view address) { E2D_UNUSED(library, address); return stdex::make_resolved_promise(fake_asset::create(42)); } }; class big_fake_asset final : public content_asset { public: static const char* type_name() noexcept { return "big_fake_asset"; } static load_async_result load_async(const library& library, str_view address) { E2D_UNUSED(library, address); return the().do_in_worker_thread([](){ std::this_thread::sleep_for(std::chrono::milliseconds(10)); return big_fake_asset::create(42); }); } }; } TEST_CASE("library"){ safe_starter_initializer initializer; library& l = the(); { { auto p = l.load_asset_async(""); REQUIRE(l.loading_asset_count() == 0); } REQUIRE(1u == l.unload_unused_assets()); } { binary_asset::ptr b1; binary_asset::ptr b2; { auto p1 = l.load_asset_async("binary_asset.bin"); auto p2 = l.load_asset_async("binary_asset.bin"); the().active_safe_wait_promise(p1); the().active_safe_wait_promise(p2); b1 = p1.get(); b2 = p2.get(); REQUIRE(b1 == b2); } b1.reset(); b2.reset(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); REQUIRE(1u == l.unload_unused_assets()); REQUIRE(l.cache().asset_count() == 0); } { { auto p1 = l.load_asset_async(""); REQUIRE(l.loading_asset_count() == 1); the().active_safe_wait_promise(p1); REQUIRE(l.loading_asset_count() == 0); auto p2 = l.load_asset_async("none_asset"); the().active_safe_wait_promise(p2); REQUIRE(l.loading_asset_count() == 0); } std::this_thread::sleep_for(std::chrono::milliseconds(10)); REQUIRE(1u == l.unload_unused_assets()); } { auto text_res = l.load_asset("text_asset.txt"); REQUIRE(text_res); REQUIRE(text_res->content() == "hello"); auto text_res_from_cache = l.load_asset("text_asset.txt"); REQUIRE(text_res_from_cache); REQUIRE(text_res_from_cache.get() == text_res.get()); REQUIRE(0u == l.unload_unused_assets()); REQUIRE(l.cache().asset_count() == 1); REQUIRE(l.cache().asset_count() == 1); text_res.reset(); text_res_from_cache.reset(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); REQUIRE(1u == l.unload_unused_assets()); REQUIRE(l.cache().asset_count() == 0); REQUIRE(l.cache().asset_count() == 0); } { auto text_res = l.load_asset("text_asset.txt"); REQUIRE(text_res); REQUIRE(text_res->content() == "hello"); auto binary_res = l.load_asset("binary_asset.bin"); REQUIRE(binary_res); REQUIRE(binary_res->content() == buffer("world", 5)); REQUIRE(0u == l.unload_unused_assets()); REQUIRE(l.cache().asset_count() == 2); text_res.reset(); binary_res.reset(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); REQUIRE(2u == l.unload_unused_assets()); REQUIRE(l.cache().asset_count() == 0); } { auto empty_res = l.load_asset("empty_asset"); REQUIRE_FALSE(empty_res); } { auto image_res = l.load_asset("image.png"); REQUIRE(image_res); REQUIRE(!image_res->content().empty()); REQUIRE(l.cache().find("image.png")); REQUIRE(l.cache().find("image.png")); std::this_thread::sleep_for(std::chrono::milliseconds(10)); l.unload_unused_assets(); REQUIRE(l.cache().find("image.png")); REQUIRE_FALSE(l.cache().find("image.png")); image_res.reset(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); l.unload_unused_assets(); REQUIRE_FALSE(l.cache().find("image.png")); REQUIRE_FALSE(l.cache().find("image.png")); } { if ( modules::is_initialized