From 381e069ec2a4485e55c00ca06554080ee0b55499 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 17 May 2021 12:34:12 +0900 Subject: [PATCH] Add UnitTest for previously fixed bug --- src/test/ECTest.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/ECTest.cpp b/src/test/ECTest.cpp index 3f1c503..24b8b11 100644 --- a/src/test/ECTest.cpp +++ b/src/test/ECTest.cpp @@ -1339,3 +1339,34 @@ TEST(EC, forMatchingIterableFn) EXPECT_EQ(c->y, 117); } } + +// This test tests for the previously fixed bug where +// callForMatchingFunction(s) can fail to call fns on +// the correct entities. +// Fixed in commit e0f30db951fcedd0ec51c680bd60a2157bd355a6 +TEST(EC, MultiThreadedForMatching) { + EC::Manager manager; + + std::size_t first = manager.addEntity(); + std::size_t second = manager.addEntity(); + + manager.addComponent(second); + manager.getEntityComponent(second)->vx = 1; + + std::size_t fnIdx = manager.addForMatchingFunction>( + [] (std::size_t id, void *data, C1 *c) { + c->vx -= 1; + if(c->vx <= 0) { + auto *manager = (EC::Manager*)data; + manager->deleteEntity(id); + } + }, &manager); + + EXPECT_TRUE(manager.isAlive(first)); + EXPECT_TRUE(manager.isAlive(second)); + + manager.callForMatchingFunction(fnIdx, 2); + + EXPECT_TRUE(manager.isAlive(first)); + EXPECT_FALSE(manager.isAlive(second)); +}