aboutsummaryrefslogtreecommitdiff
path: root/src/test/ResourceManagerTest.cpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-26 20:03:41 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-26 20:03:41 +0100
commita685e5f743786cc6499e7ce8973bb78a83d101f7 (patch)
tree21d7aeb768912bef8f98436d13acb4423cffd8f0 /src/test/ResourceManagerTest.cpp
parent92e3fbda73128e65a31b3760b3fa4bd0147debe3 (diff)
big WIP
Diffstat (limited to 'src/test/ResourceManagerTest.cpp')
-rw-r--r--src/test/ResourceManagerTest.cpp115
1 files changed, 82 insertions, 33 deletions
diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp
index 3fc9ebd..f57c419 100644
--- a/src/test/ResourceManagerTest.cpp
+++ b/src/test/ResourceManagerTest.cpp
@@ -1,52 +1,101 @@
#include <gtest/gtest.h>
+#define private public
+#define protected public
+
#include <crepe/util/Log.h>
-#include <crepe/api/Config.h>
-#include <crepe/api/ResourceManager.h>
+#include <crepe/ResourceManager.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/Component.h>
+#include <crepe/ComponentManager.h>
using namespace std;
using namespace crepe;
using namespace testing;
+class TestComponent : public Component {
+public:
+ TestComponent(game_object_id_t id, const Asset & asset)
+ : Component(id),
+ source(asset) {}
+ const Asset source;
+};
+
+class TestResource : public Resource {
+public:
+ static unsigned instances;
+
+public:
+ const unsigned instance;
+ TestResource(const Asset & src)
+ : Resource(src),
+ instance(this->instances++) { }
+};
+unsigned TestResource::instances = 0;
+
+template <>
+TestResource & ResourceManager::get(const TestComponent & component) {
+ return this->get_internal<TestResource>(component, component.source);
+}
+
class ResourceManagerTest : public Test {
public:
- ResourceManager & resman = ResourceManager::get_instance();
- Config & cfg = Config::get_instance();
+ ResourceManager resource_manager{};
+
+ static constexpr const char * ASSET_LOCATION = "asset/texture/img.png";
+
+ TestComponent a{0, ASSET_LOCATION};
+ TestComponent b{1, ASSET_LOCATION};
+private:
void SetUp() override {
- resman.clear();
+ TestResource::instances = 0;
}
};
-TEST_F(ResourceManagerTest, Main) {
- // NOTE: there is no way (that I know of) to ensure the last cache call
- // allocates the new Sound instance in a different location than the first,
- // so this test should be verified manually using these print statements and
- // debug trace messages.
- cfg.log.level = Log::Level::TRACE;
-
- Asset path1 = "mwe/audio/sfx1.wav";
- Asset path2 = "mwe/audio/sfx1.wav";
- ASSERT_EQ(path1, path2);
-
- Sound * ptr1 = nullptr;
- Sound * ptr2 = nullptr;
- {
- Log::logf(Log::Level::DEBUG, "Get first sound (constructor call)");
- Sound & sound = resman.cache<Sound>(path1);
- ptr1 = &sound;
- }
- {
- Log::logf(Log::Level::DEBUG, "Get same sound (NO constructor call)");
- Sound & sound = resman.cache<Sound>(path2);
- ptr2 = &sound;
- }
- EXPECT_EQ(ptr1, ptr2);
+TEST_F(ResourceManagerTest, Uncached) {
+ TestResource & res_1 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_2 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_3 = resource_manager.get<TestResource>(b); // 2
+ TestResource & res_4 = resource_manager.get<TestResource>(b); // 2
+
+ ASSERT_EQ(res_1, res_2);
+ ASSERT_EQ(res_3, res_4);
+ EXPECT_NE(res_1, res_3);
+
+ EXPECT_EQ(TestResource::instances, 2);
+}
+
+// TODO: per GameObject / Component
+TEST_F(ResourceManagerTest, PerComponent) {
+ resource_manager.cache(a);
+ resource_manager.cache(b);
+
+ TestResource & res_1 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_2 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_3 = resource_manager.get<TestResource>(b); // 2
+ TestResource & res_4 = resource_manager.get<TestResource>(b); // 2
+
+ ASSERT_EQ(res_1, res_2);
+ ASSERT_EQ(res_3, res_4);
+ EXPECT_NE(res_1, res_3);
+
+ EXPECT_EQ(TestResource::instances, 2);
+}
+
+TEST_F(ResourceManagerTest, PerAsset) {
+ resource_manager.cache(ASSET_LOCATION);
+ EXPECT_EQ(TestResource::instances, 1);
+
+ TestResource & res_1 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_2 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_3 = resource_manager.get<TestResource>(b); // 1
+ TestResource & res_4 = resource_manager.get<TestResource>(b); // 1
- Log::logf(Log::Level::DEBUG, "Clear cache (destructor call)");
- resman.clear();
+ EXPECT_EQ(res_1, res_2);
+ EXPECT_EQ(res_2, res_3);
+ EXPECT_EQ(res_3, res_4);
- Log::logf(Log::Level::DEBUG, "Get first sound again (constructor call)");
- Sound & sound = resman.cache<Sound>(path1);
+ EXPECT_EQ(TestResource::instances, 1);
}