From 86f4b35e96b0999a164450d569f591b95cd6be20 Mon Sep 17 00:00:00 2001 From: Lars Pensjö Date: Sat, 2 Mar 2013 15:23:10 +0100 Subject: Add test for reusing an Entity. This will lead to a crash. --- entityx/Entity_test.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index 67026e8..3fa6548 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -75,8 +75,19 @@ class EntityManagerTest : public ::testing::Test { TEST_F(EntityManagerTest, TestCreateEntity) { + ASSERT_TRUE(em.size() == 0); Entity e = em.create(); ASSERT_TRUE(em.exists(e)); + ASSERT_TRUE(em.size() == 1); +} + +TEST_F(EntityManagerTest, TestEntityReuse) { + Entity e1 = em.create(); + auto id = e1.id(); + em.destroy(e1); + ASSERT_TRUE(!em.exists(e1)); + Entity e2 = em.create(); + ASSERT_EQ(e2.id(), id); } TEST_F(EntityManagerTest, TestComponentConstruction) { -- cgit v1.2.3 From 48d8f894bd7bfddcb49743855397e3ab622bc40f Mon Sep 17 00:00:00 2001 From: Lars Pensjö Date: Sat, 2 Mar 2013 15:26:31 +0100 Subject: Entity allocation error fixed. Need to save the id before erasing. --- entityx/Entity.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/entityx/Entity.h b/entityx/Entity.h index 6d046c3..ae28443 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -1,10 +1,10 @@ /** * Copyright (C) 2012 Alec Thomas * All rights reserved. - * + * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. - * + * * Author: Alec Thomas */ @@ -227,7 +227,7 @@ class EntityManager : boost::noncopyable { return true; } - EntityManager &manager_; + EntityManager &manager_; const std::vector predicates_; std::vector> unpackers_; Entity::Id i_; @@ -246,7 +246,7 @@ class EntityManager : boost::noncopyable { template View &unpack_to(boost::shared_ptr &a) { unpackers_.push_back(Unpacker(manager_, a)); - // This resulted in a segfault under clang 4.1 on OSX. No idea why. + // This resulted in a segfault under clang 4.1 on OSX. No idea why. /*unpackers_.push_back([&a, this](Entity::Id id) { a = manager_.component(id); });*/ @@ -299,7 +299,9 @@ class EntityManager : boost::noncopyable { id = id_counter_++; accomodate_entity(id); } else { - id = *free_list_.erase(free_list_.begin()); + auto it = free_list_.begin(); + id = *it; + free_list_.erase(it); } event_manager_.emit(*this, id); return Entity(*this, id); -- cgit v1.2.3 From 2c78e9edf9042acab9660f09ae6119ca21e5aacf Mon Sep 17 00:00:00 2001 From: Lars Pensjö Date: Sat, 2 Mar 2013 15:33:11 +0100 Subject: Added comment. --- entityx/Entity_test.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index 3fa6548..d042b4b 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -87,6 +87,7 @@ TEST_F(EntityManagerTest, TestEntityReuse) { em.destroy(e1); ASSERT_TRUE(!em.exists(e1)); Entity e2 = em.create(); + // It is assumed that the allocation will reuse the same entity id. ASSERT_EQ(e2.id(), id); } -- cgit v1.2.3