]> git.seodisparate.com - EntityComponentMetaSystem/commitdiff
Add UnitTest for previously fixed bug
authorStephen Seo <seo.disparate@gmail.com>
Mon, 17 May 2021 03:34:12 +0000 (12:34 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 17 May 2021 03:34:12 +0000 (12:34 +0900)
src/test/ECTest.cpp

index 3f1c50311d504fdc52534554dd7839f6576c6adf..24b8b116f2986b3d933eba34e29479bd73ea8650 100644 (file)
@@ -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<ListComponentsAll, ListTagsAll> manager;
+
+    std::size_t first = manager.addEntity();
+    std::size_t second = manager.addEntity();
+
+    manager.addComponent<C1>(second);
+    manager.getEntityComponent<C1>(second)->vx = 1;
+
+    std::size_t fnIdx = manager.addForMatchingFunction<EC::Meta::TypeList<C1>>(
+            [] (std::size_t id, void *data, C1 *c) {
+        c->vx -= 1;
+        if(c->vx <= 0) {
+            auto *manager = (EC::Manager<ListComponentsAll, ListTagsAll>*)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));
+}