diff --git a/src/test/ECTest.cpp b/src/test/ECTest.cpp index d8e6caa..e0c4c89 100644 --- a/src/test/ECTest.cpp +++ b/src/test/ECTest.cpp @@ -391,3 +391,41 @@ TEST(EC, FunctionStorage) } } +/* + This test demonstrates that while entity ids may change after cleanup, + pointers to their components do not. +*/ +TEST(EC, DataPointers) +{ + EC::Manager manager; + + auto first = manager.addEntity(); + auto second = manager.addEntity(); + + manager.addComponent(first, 5, 5); + manager.addComponent(second, 10, 10); + manager.addTag(second); + + auto* cptr = &manager.getEntityData(second); + + EXPECT_EQ(10, cptr->x); + EXPECT_EQ(10, cptr->y); + + manager.deleteEntity(first); + manager.cleanup(); + + auto newSecond = second; + manager.forMatchingSignature>( + [&newSecond] (auto eid) { + newSecond = eid; + }); + + EXPECT_NE(newSecond, second); + + auto* newcptr = &manager.getEntityData(newSecond); + EXPECT_EQ(newcptr, cptr); + + EXPECT_EQ(10, newcptr->x); + EXPECT_EQ(10, newcptr->y); +} +