diff options
author | Alec Thomas <alec@swapoff.org> | 2014-03-04 13:26:30 +1100 |
---|---|---|
committer | Alec Thomas <alec@swapoff.org> | 2014-03-04 13:26:30 +1100 |
commit | ea005510200ed7851496c77d2c15e6d3de75ac7b (patch) | |
tree | 3aa66fcc4365f5bf046385428d26d37d145550b0 | |
parent | 498df9f08e86d73ceb09746d1f63b4e0bd6c656f (diff) |
Use TEST_CASE_METHOD rather than SECTION.
-rw-r--r-- | entityx/Benchmarks_test.cc | 117 | ||||
-rw-r--r-- | entityx/Entity_test.cc | 581 | ||||
-rw-r--r-- | entityx/System_test.cc | 49 | ||||
-rw-r--r-- | entityx/deps/Dependencies_test.cc | 65 | ||||
-rw-r--r-- | entityx/help/Pool_test.cc | 6 |
5 files changed, 410 insertions, 408 deletions
diff --git a/entityx/Benchmarks_test.cc b/entityx/Benchmarks_test.cc index e9eb9ef..d0aff02 100644 --- a/entityx/Benchmarks_test.cc +++ b/entityx/Benchmarks_test.cc @@ -27,87 +27,90 @@ struct Listener : public Receiver<Listener> { struct Position : public Component<Position> { }; -TEST_CASE("BenchmarksTest", "[entitymanager]") { + +struct BenchmarkFixture { + BenchmarkFixture() : em(ev) {} + EventManager ev; - EntityManager em(ev); + EntityManager em; +}; - SECTION("TestCreateEntities") { - AutoTimer t; +TEST_CASE_METHOD(BenchmarkFixture, "TestCreateEntities") { + AutoTimer t; - uint64_t count = 10000000L; - cout << "creating " << count << " entities" << endl; + uint64_t count = 10000000L; + cout << "creating " << count << " entities" << endl; - for (uint64_t i = 0; i < count; i++) { - em.create(); - } + for (uint64_t i = 0; i < count; i++) { + em.create(); } +} - SECTION("TestDestroyEntities") { - uint64_t count = 10000000L; - vector<Entity> entities; - for (uint64_t i = 0; i < count; i++) { - entities.push_back(em.create()); - } +TEST_CASE_METHOD(BenchmarkFixture, "TestDestroyEntities") { + uint64_t count = 10000000L; + vector<Entity> entities; + for (uint64_t i = 0; i < count; i++) { + entities.push_back(em.create()); + } - AutoTimer t; - cout << "destroying " << count << " entities" << endl; + AutoTimer t; + cout << "destroying " << count << " entities" << endl; - for (auto e : entities) { - e.destroy(); - } + for (auto e : entities) { + e.destroy(); } +} - SECTION("TestCreateEntitiesWithListener") { - Listener listen; - ev.subscribe<EntityCreatedEvent>(listen); +TEST_CASE_METHOD(BenchmarkFixture, "TestCreateEntitiesWithListener") { + Listener listen; + ev.subscribe<EntityCreatedEvent>(listen); - uint64_t count = 10000000L; + uint64_t count = 10000000L; - AutoTimer t; - cout << "creating " << count << " entities while notifying a single EntityCreatedEvent listener" << endl; + AutoTimer t; + cout << "creating " << count << " entities while notifying a single EntityCreatedEvent listener" << endl; - vector<Entity> entities; - for (uint64_t i = 0; i < count; i++) { - entities.push_back(em.create()); - } + vector<Entity> entities; + for (uint64_t i = 0; i < count; i++) { + entities.push_back(em.create()); } +} - SECTION("TestDestroyEntitiesWithListener") { - Listener listen; - ev.subscribe<EntityDestroyedEvent>(listen); +TEST_CASE_METHOD(BenchmarkFixture, "TestDestroyEntitiesWithListener") { + Listener listen; + ev.subscribe<EntityDestroyedEvent>(listen); - uint64_t count = 10000000L; - vector<Entity> entities; - for (uint64_t i = 0; i < count; i++) { - entities.push_back(em.create()); - } + uint64_t count = 10000000L; + vector<Entity> entities; + for (uint64_t i = 0; i < count; i++) { + entities.push_back(em.create()); + } - AutoTimer t; - cout << "destroying " << count << " entities" << endl; + AutoTimer t; + cout << "destroying " << count << " entities" << endl; - for (auto &e : entities) { - e.destroy(); - } + for (auto &e : entities) { + e.destroy(); } +} - SECTION("TestEntityIteration") { - uint64_t count = 10000000L; - vector<Entity> entities; - for (uint64_t i = 0; i < count; i++) { - auto e = em.create(); - e.assign<Position>(); - entities.push_back(e); - } +TEST_CASE_METHOD(BenchmarkFixture, "TestEntityIteration") { + uint64_t count = 10000000L; + vector<Entity> entities; + for (uint64_t i = 0; i < count; i++) { + auto e = em.create(); + e.assign<Position>(); + entities.push_back(e); + } - AutoTimer t; - cout << "iterating over " << count << " entities with a component 10 times" << endl; + AutoTimer t; + cout << "iterating over " << count << " entities with a component 10 times" << endl; - for (int i = 0; i < 10; ++i) { - Position *position; - for (auto e : em.entities_with_components<Position>(position)) { - } + for (int i = 0; i < 10; ++i) { + Position *position; + for (auto e : em.entities_with_components<Position>(position)) { } } } diff --git a/entityx/Entity_test.cc b/entityx/Entity_test.cc index c8693b5..db7ef18 100644 --- a/entityx/Entity_test.cc +++ b/entityx/Entity_test.cc @@ -78,341 +78,350 @@ ostream &operator<<(ostream &out, const Tag &tag) { return out; } -TEST_CASE("EntityManagerTest", "[entitymanager]") { - EventManager ev; - EntityManager em(ev); - SECTION("TestCreateEntity") { - REQUIRE(em.size() == 0UL); +struct EntityManagerFixture { + EntityManagerFixture() : em(ev) {} - Entity e2; - REQUIRE(!(e2.valid())); + EventManager ev; + EntityManager em; +}; - Entity e = em.create(); - REQUIRE(e.valid()); - REQUIRE(em.size() == 1UL); - e2 = e; - REQUIRE(e2.valid()); - } +TEST_CASE_METHOD(EntityManagerFixture, "TestCreateEntity") { + REQUIRE(em.size() == 0UL); - SECTION("TestEntityAsBoolean") { - REQUIRE(em.size() == 0UL); - Entity e = em.create(); - REQUIRE(e.valid()); - REQUIRE(em.size() == 1UL); - REQUIRE(!(!e)); + Entity e2; + REQUIRE(!(e2.valid())); - e.destroy(); + Entity e = em.create(); + REQUIRE(e.valid()); + REQUIRE(em.size() == 1UL); + + e2 = e; + REQUIRE(e2.valid()); +} - REQUIRE(em.size() == 0UL); +TEST_CASE_METHOD(EntityManagerFixture, "TestEntityAsBoolean") { + REQUIRE(em.size() == 0UL); + Entity e = em.create(); + REQUIRE(e.valid()); + REQUIRE(em.size() == 1UL); + REQUIRE(!(!e)); - REQUIRE(!e); + e.destroy(); - Entity e2; // Not initialized - REQUIRE(!e2); - } + REQUIRE(em.size() == 0UL); - SECTION("TestEntityReuse") { - Entity e1 = em.create(); - Entity e2 = e1; - auto id = e1.id(); - REQUIRE(e1.valid()); - REQUIRE(e2.valid()); - e1.destroy(); - REQUIRE(!e1.valid()); - REQUIRE(!e2.valid()); - Entity e3 = em.create(); - // It is assumed that the allocation will reuse the same entity id, though - // the version will change. - auto new_id = e3.id(); - REQUIRE(new_id != id); - REQUIRE((new_id.id() & 0xffffffffUL) == (id.id() & 0xffffffffUL)); - } + REQUIRE(!e); - SECTION("TestComponentConstruction") { - auto e = em.create(); - auto p = e.assign<Position>(1, 2); - auto cp = e.component<Position>(); - REQUIRE(p == cp); - REQUIRE(1.0 == Approx(cp->x)); - REQUIRE(2.0 == Approx(cp->y)); - } + Entity e2; // Not initialized + REQUIRE(!e2); +} - SECTION("TestDestroyEntity") { - Entity e = em.create(); - Entity f = em.create(); - e.assign<Position>(); - f.assign<Position>(); - e.assign<Direction>(); - f.assign<Direction>(); +TEST_CASE_METHOD(EntityManagerFixture, "TestEntityReuse") { + Entity e1 = em.create(); + Entity e2 = e1; + auto id = e1.id(); + REQUIRE(e1.valid()); + REQUIRE(e2.valid()); + e1.destroy(); + REQUIRE(!e1.valid()); + REQUIRE(!e2.valid()); + Entity e3 = em.create(); + // It is assumed that the allocation will reuse the same entity id, though + // the version will change. + auto new_id = e3.id(); + REQUIRE(new_id != id); + REQUIRE((new_id.id() & 0xffffffffUL) == (id.id() & 0xffffffffUL)); +} - REQUIRE(e.valid()); - REQUIRE(f.valid()); - REQUIRE(static_cast<bool>(e.component<Position>())); - REQUIRE(static_cast<bool>(e.component<Direction>())); - REQUIRE(static_cast<bool>(f.component<Position>())); - REQUIRE(static_cast<bool>(f.component<Direction>())); +TEST_CASE_METHOD(EntityManagerFixture, "TestComponentConstruction") { + auto e = em.create(); + auto p = e.assign<Position>(1, 2); + auto cp = e.component<Position>(); + REQUIRE(p == cp); + REQUIRE(1.0 == Approx(cp->x)); + REQUIRE(2.0 == Approx(cp->y)); +} - e.destroy(); +TEST_CASE_METHOD(EntityManagerFixture, "TestDestroyEntity") { + Entity e = em.create(); + Entity f = em.create(); + e.assign<Position>(); + f.assign<Position>(); + e.assign<Direction>(); + f.assign<Direction>(); + + REQUIRE(e.valid()); + REQUIRE(f.valid()); + REQUIRE(static_cast<bool>(e.component<Position>())); + REQUIRE(static_cast<bool>(e.component<Direction>())); + REQUIRE(static_cast<bool>(f.component<Position>())); + REQUIRE(static_cast<bool>(f.component<Direction>())); + + e.destroy(); + + REQUIRE(!(e.valid())); + REQUIRE(f.valid()); + REQUIRE(static_cast<bool>(f.component<Position>())); + REQUIRE(static_cast<bool>(f.component<Direction>())); +} - REQUIRE(!(e.valid())); - REQUIRE(f.valid()); - REQUIRE(static_cast<bool>(f.component<Position>())); - REQUIRE(static_cast<bool>(f.component<Direction>())); - } +TEST_CASE_METHOD(EntityManagerFixture, "TestGetEntitiesWithComponent") { + Entity e = em.create(); + Entity f = em.create(); + Entity g = em.create(); + e.assign<Position>(); + e.assign<Direction>(); + f.assign<Position>(); + g.assign<Position>(); + REQUIRE(3 == size(em.entities_with_components<Position>())); + REQUIRE(1 == size(em.entities_with_components<Direction>())); +} - SECTION("TestGetEntitiesWithComponent") { +TEST_CASE_METHOD(EntityManagerFixture, "TestGetEntitiesWithIntersectionOfComponents") { + vector<Entity> entities; + for (int i = 0; i < 150; ++i) { Entity e = em.create(); - Entity f = em.create(); - Entity g = em.create(); - e.assign<Position>(); - e.assign<Direction>(); - f.assign<Position>(); - g.assign<Position>(); - REQUIRE(3 == size(em.entities_with_components<Position>())); - REQUIRE(1 == size(em.entities_with_components<Direction>())); + entities.push_back(e); + if (i % 2 == 0) e.assign<Position>(); + if (i % 3 == 0) e.assign<Direction>(); } + REQUIRE(50 == size(em.entities_with_components<Direction>())); + REQUIRE(75 == size(em.entities_with_components<Position>())); + REQUIRE(25 == size(em.entities_with_components<Direction, Position>())); +} - SECTION("TestGetEntitiesWithIntersectionOfComponents") { - vector<Entity> entities; - for (int i = 0; i < 150; ++i) { - Entity e = em.create(); - entities.push_back(e); - if (i % 2 == 0) e.assign<Position>(); - if (i % 3 == 0) e.assign<Direction>(); - } - REQUIRE(50 == size(em.entities_with_components<Direction>())); - REQUIRE(75 == size(em.entities_with_components<Position>())); - REQUIRE(25 == size(em.entities_with_components<Direction, Position>())); +TEST_CASE_METHOD(EntityManagerFixture, "TestGetEntitiesWithComponentAndUnpacking") { + vector<Entity::Id> entities; + Entity e = em.create(); + Entity f = em.create(); + Entity g = em.create(); + std::vector<std::pair<Position *, Direction *>> position_directions; + position_directions.push_back(std::make_pair( + e.assign<Position>(1.0f, 2.0f), e.assign<Direction>(3.0f, 4.0f))); + position_directions.push_back(std::make_pair( + f.assign<Position>(7.0f, 8.0f), f.assign<Direction>(9.0f, 10.0f))); + auto thetag = f.assign<Tag>("tag"); + g.assign<Position>(5.0f, 6.0f); + int i = 0; + + Position *position; + REQUIRE(3 == size(em.entities_with_components(position))); + + Direction *direction; + for (auto unused_entity : em.entities_with_components(position, direction)) { + (void)unused_entity; + REQUIRE(static_cast<bool>(position)); + REQUIRE(static_cast<bool>(direction)); + auto pd = position_directions.at(i); + REQUIRE(position == pd.first); + REQUIRE(direction == pd.second); + ++i; } - - SECTION("TestGetEntitiesWithComponentAndUnpacking") { - vector<Entity::Id> entities; - Entity e = em.create(); - Entity f = em.create(); - Entity g = em.create(); - std::vector<std::pair<Position *, Direction *>> position_directions; - position_directions.push_back(std::make_pair( - e.assign<Position>(1.0f, 2.0f), e.assign<Direction>(3.0f, 4.0f))); - position_directions.push_back(std::make_pair( - f.assign<Position>(7.0f, 8.0f), f.assign<Direction>(9.0f, 10.0f))); - auto thetag = f.assign<Tag>("tag"); - g.assign<Position>(5.0f, 6.0f); - int i = 0; - - Position *position; - REQUIRE(3 == size(em.entities_with_components(position))); - - Direction *direction; - for (auto unused_entity : em.entities_with_components(position, direction)) { - (void)unused_entity; - REQUIRE(static_cast<bool>(position)); - REQUIRE(static_cast<bool>(direction)); - auto pd = position_directions.at(i); - REQUIRE(position == pd.first); - REQUIRE(direction == pd.second); - ++i; - } - REQUIRE(2 == i); - Tag *tag; - i = 0; - for (auto unused_entity : - em.entities_with_components(position, direction, tag)) { - (void)unused_entity; - REQUIRE(static_cast<bool>(position)); - REQUIRE(static_cast<bool>(direction)); - REQUIRE(static_cast<bool>(tag)); - auto pd = position_directions.at(1); - REQUIRE(position == pd.first); - REQUIRE(direction == pd.second); - REQUIRE(tag == thetag); - i++; - } - REQUIRE(1 == i); + REQUIRE(2 == i); + Tag *tag; + i = 0; + for (auto unused_entity : + em.entities_with_components(position, direction, tag)) { + (void)unused_entity; + REQUIRE(static_cast<bool>(position)); + REQUIRE(static_cast<bool>(direction)); + REQUIRE(static_cast<bool>(tag)); + auto pd = position_directions.at(1); + REQUIRE(position == pd.first); + REQUIRE(direction == pd.second); + REQUIRE(tag == thetag); + i++; } + REQUIRE(1 == i); +} - SECTION("TestUnpack") { - Entity e = em.create(); - auto p = e.assign<Position>(1.0, 2.0); - auto d = e.assign<Direction>(3.0, 4.0); - auto t = e.assign<Tag>("tag"); - - Position *up; - Direction *ud; - Tag *ut; - e.unpack(up); - REQUIRE(p == up); - e.unpack(up, ud); - REQUIRE(p == up); - REQUIRE(d == ud); - e.unpack(up, ud, ut); - REQUIRE(p == up); - REQUIRE(d == ud); - REQUIRE(t == ut); - } +TEST_CASE_METHOD(EntityManagerFixture, "TestUnpack") { + Entity e = em.create(); + auto p = e.assign<Position>(1.0, 2.0); + auto d = e.assign<Direction>(3.0, 4.0); + auto t = e.assign<Tag>("tag"); + + Position *up; + Direction *ud; + Tag *ut; + e.unpack(up); + REQUIRE(p == up); + e.unpack(up, ud); + REQUIRE(p == up); + REQUIRE(d == ud); + e.unpack(up, ud, ut); + REQUIRE(p == up); + REQUIRE(d == ud); + REQUIRE(t == ut); +} - // gcc 4.7.2 does not allow this struct to be declared locally inside the - // SECTION." //" SECTION("TestUnpackNullMissing") { - // Entity e = em.create(); - // auto p = e.assign<Position>(); - - // std::shared_ptr<Position> up(reinterpret_cast<Position*>(0Xdeadbeef), - // NullDeleter()); - // std::shared_ptr<Direction> ud(reinterpret_cast<Direction*>(0Xdeadbeef), - // NullDeleter()); - // e.unpack<Position, Direction>(up, ud); - // REQUIRE(p == up); - // REQUIRE(std::shared_ptr<Direction>() == ud); - // } - - SECTION("TestComponentIdsDiffer") { - REQUIRE(Position::family() != Direction::family()); - } +// gcc 4.7.2 does not allow this struct to be declared locally inside the +// TEST_CASE_METHOD.EntityManagerFixture, " //" TEST_CASE_METHOD(EntityManagerFixture, "TestUnpackNullMissing") { +// Entity e = em.create(); +// auto p = e.assign<Position>(); + +// std::shared_ptr<Position> up(reinterpret_cast<Position*>(0Xdeadbeef), +// NullDeleter()); +// std::shared_ptr<Direction> ud(reinterpret_cast<Direction*>(0Xdeadbeef), +// NullDeleter()); +// e.unpack<Position, Direction>(up, ud); +// REQUIRE(p == up); +// REQUIRE(std::shared_ptr<Direction>() == ud); +// } + +TEST_CASE_METHOD(EntityManagerFixture, "TestComponentIdsDiffer") { + REQUIRE(Position::family() != Direction::family()); +} - SECTION("TestEntityCreatedEvent") { - struct EntityCreatedEventReceiver - : public Receiver<EntityCreatedEventReceiver> { - void receive(const EntityCreatedEvent &event) { - created.push_back(event.entity); - } +TEST_CASE_METHOD(EntityManagerFixture, "TestEntityCreatedEvent") { + struct EntityCreatedEventReceiver + : public Receiver<EntityCreatedEventReceiver> { + void receive(const EntityCreatedEvent &event) { + created.push_back(event.entity); + } - vector<Entity> created; - }; + vector<Entity> created; + }; - EntityCreatedEventReceiver receiver; - ev.subscribe<EntityCreatedEvent>(receiver); + EntityCreatedEventReceiver receiver; + ev.subscribe<EntityCreatedEvent>(receiver); - REQUIRE(0UL == receiver.created.size()); - for (int i = 0; i < 10; ++i) { - em.create(); - } - REQUIRE(10UL == receiver.created.size()); + REQUIRE(0UL == receiver.created.size()); + for (int i = 0; i < 10; ++i) { + em.create(); } + REQUIRE(10UL == receiver.created.size()); +} - SECTION("TestEntityDestroyedEvent") { - struct EntityDestroyedEventReceiver - : public Receiver<EntityDestroyedEventReceiver> { - void receive(const EntityDestroyedEvent &event) { - destroyed.push_back(event.entity); - } +TEST_CASE_METHOD(EntityManagerFixture, "TestEntityDestroyedEvent") { + struct EntityDestroyedEventReceiver + : public Receiver<EntityDestroyedEventReceiver> { + void receive(const EntityDestroyedEvent &event) { + destroyed.push_back(event.entity); + } - vector<Entity> destroyed; - }; + vector<Entity> destroyed; + }; - EntityDestroyedEventReceiver receiver; - ev.subscribe<EntityDestroyedEvent>(receiver); + EntityDestroyedEventReceiver receiver; + ev.subscribe<EntityDestroyedEvent>(receiver); - REQUIRE(0UL == receiver.destroyed.size()); - vector<Entity> entities; - for (int i = 0; i < 10; ++i) { - entities.push_back(em.create()); - } - REQUIRE(0UL == receiver.destroyed.size()); - for (auto e : entities) { - e.destroy(); - } - REQUIRE(entities == receiver.destroyed); + REQUIRE(0UL == receiver.destroyed.size()); + vector<Entity> entities; + for (int i = 0; i < 10; ++i) { + entities.push_back(em.create()); + } + REQUIRE(0UL == receiver.destroyed.size()); + for (auto e : entities) { + e.destroy(); } + REQUIRE(entities == receiver.destroyed); +} - SECTION("TestComponentAddedEvent") { - struct ComponentAddedEventReceiver - : public Receiver<ComponentAddedEventReceiver> { - void receive(const ComponentAddedEvent<Position> &event) { - auto p = event.component; - float n = static_cast<float>(position_events); - REQUIRE(p->x == n); - REQUIRE(p->y == n); - position_events++; - } - - void receive(const ComponentAddedEvent<Direction> &event) { - auto p = event.component; - float n = static_cast<float>(direction_events); - REQUIRE(p->x == -n); - REQUIRE(p->y == -n); - direction_events++; - } - - int position_events = 0; - int direction_events = 0; - }; - - ComponentAddedEventReceiver receiver; - ev.subscribe<ComponentAddedEvent<Position>>(receiver); - ev.subscribe<ComponentAddedEvent<Direction>>(receiver); - - REQUIRE(ComponentAddedEvent<Position>::family() != - ComponentAddedEvent<Direction>::family()); - - REQUIRE(0 == receiver.position_events); - REQUIRE(0 == receiver.direction_events); - for (int i = 0; i < 10; ++i) { - Entity e = em.create(); - e.assign<Position>(static_cast<float>(i), static_cast<float>(i)); - e.assign<Direction>(static_cast<float>(-i), static_cast<float>(-i)); +TEST_CASE_METHOD(EntityManagerFixture, "TestComponentAddedEvent") { + struct ComponentAddedEventReceiver + : public Receiver<ComponentAddedEventReceiver> { + void receive(const ComponentAddedEvent<Position> &event) { + auto p = event.component; + float n = static_cast<float>(position_events); + REQUIRE(p->x == n); + REQUIRE(p->y == n); + position_events++; } - REQUIRE(10 == receiver.position_events); - REQUIRE(10 == receiver.direction_events); - } + void receive(const ComponentAddedEvent<Direction> &event) { + auto p = event.component; + float n = static_cast<float>(direction_events); + REQUIRE(p->x == -n); + REQUIRE(p->y == -n); + direction_events++; + } - SECTION("TestComponentRemovedEvent") { - struct ComponentRemovedReceiver : public Receiver<ComponentRemovedReceiver> { - void receive(const ComponentRemovedEvent<Direction> &event) { - removed = event.component; - } + int position_events = 0; + int direction_events = 0; + }; - Direction *removed = nullptr; - }; + ComponentAddedEventReceiver receiver; + ev.subscribe<ComponentAddedEvent<Position>>(receiver); + ev.subscribe<ComponentAddedEvent<Direction>>(receiver); - ComponentRemovedReceiver receiver; - ev.subscribe<ComponentRemovedEvent<Direction>>(receiver); + REQUIRE(ComponentAddedEvent<Position>::family() != + ComponentAddedEvent<Direction>::family()); - REQUIRE(!(receiver.removed)); + REQUIRE(0 == receiver.position_events); + REQUIRE(0 == receiver.direction_events); + for (int i = 0; i < 10; ++i) { Entity e = em.create(); - Direction *p = e.assign<Direction>(1.0, 2.0); - e.remove<Direction>(); - REQUIRE(receiver.removed == p); - REQUIRE(!(e.component<Direction>())); + e.assign<Position>(static_cast<float>(i), static_cast<float>(i)); + e.assign<Direction>(static_cast<float>(-i), static_cast<float>(-i)); } + REQUIRE(10 == receiver.position_events); + REQUIRE(10 == receiver.direction_events); +} - SECTION("TestEntityAssignment") { - Entity a, b; - a = em.create(); - REQUIRE(a != b); - b = a; - REQUIRE(a == b); - a.invalidate(); - REQUIRE(a != b); - } - SECTION("TestEntityDestroyAll") { - Entity a = em.create(), b = em.create(); - em.reset(); - REQUIRE(!(a.valid())); - REQUIRE(!(b.valid())); - } +TEST_CASE_METHOD(EntityManagerFixture, "TestComponentRemovedEvent") { + struct ComponentRemovedReceiver : public Receiver<ComponentRemovedReceiver> { + void receive(const ComponentRemovedEvent<Direction> &event) { + removed = event.component; + } - SECTION("TestEntityDestroyHole") { - std::vector<Entity> entities; + Direction *removed = nullptr; + }; - auto count = [&em]()->int { - auto e = em.entities_with_components<Position>(); - return std::count_if(e.begin(), e.end(), - [](const Entity &) { return true; }); - }; + ComponentRemovedReceiver receiver; + ev.subscribe<ComponentRemovedEvent<Direction>>(receiver); - for (int i = 0; i < 5000; i++) { - auto e = em.create(); - e.assign<Position>(); - entities.push_back(e); - } + REQUIRE(!(receiver.removed)); + Entity e = em.create(); + Direction *p = e.assign<Direction>(1.0, 2.0); + e.remove<Direction>(); + REQUIRE(receiver.removed == p); + REQUIRE(!(e.component<Direction>())); +} + +TEST_CASE_METHOD(EntityManagerFixture, "TestEntityAssignment") { + Entity a, b; + a = em.create(); + REQUIRE(a != b); + b = a; + REQUIRE(a == b); + a.invalidate(); + REQUIRE(a != b); +} + +TEST_CASE_METHOD(EntityManagerFixture, "TestEntityDestroyAll") { + Entity a = em.create(), b = em.create(); + em.reset(); + REQUIRE(!(a.valid())); + REQUIRE(!(b.valid())); +} - REQUIRE(count() == 5000); +TEST_CASE_METHOD(EntityManagerFixture, "TestEntityDestroyHole") { + std::vector<Entity> entities; - entities[2500].destroy(); + auto count = [this]()->int { + auto e = em.entities_with_components<Position>(); + return std::count_if(e.begin(), e.end(), + [](const Entity &) { return true; }); + }; - REQUIRE(count() == 4999); + for (int i = 0; i < 5000; i++) { + auto e = em.create(); + e.assign<Position>(); + entities.push_back(e); } + + REQUIRE(count() == 5000); + + entities[2500].destroy(); + + REQUIRE(count() == 4999); +} + +TEST_CASE_METHOD(EntityManagerFixture, "DeleteComponentThrowsBadAlloc") { + Position *position = new Position(); + REQUIRE_THROWS_AS(delete position, std::bad_alloc); } diff --git a/entityx/System_test.cc b/entityx/System_test.cc index 0f2b663..9253003 100644 --- a/entityx/System_test.cc +++ b/entityx/System_test.cc @@ -51,11 +51,11 @@ class MovementSystem : public System<MovementSystem> { string label; }; -class TestContainer : public EntityX { +class EntitiesFixture : public EntityX { public: std::vector<Entity> created_entities; - void initialize() { + EntitiesFixture() { for (int i = 0; i < 150; ++i) { Entity e = entities.create(); created_entities.push_back(e); @@ -65,33 +65,28 @@ class TestContainer : public EntityX { } }; -TEST_CASE("SystemManagerTest", "[systemmanager]") { - TestContainer manager; - manager.initialize(); +TEST_CASE_METHOD(EntitiesFixture, "TestConstructSystemWithArgs") { + systems.add<MovementSystem>("movement"); + systems.configure(); - SECTION("TestConstructSystemWithArgs") { - manager.systems.add<MovementSystem>("movement"); - manager.systems.configure(); - - REQUIRE("movement" == manager.systems.system<MovementSystem>()->label); - } - - SECTION("TestApplySystem") { - manager.systems.add<MovementSystem>(); - manager.systems.configure(); + REQUIRE("movement" == systems.system<MovementSystem>()->label); +} - manager.systems.update<MovementSystem>(0.0); - Position *position; - Direction *direction; - for (auto entity : manager.created_entities) { - entity.unpack<Position, Direction>(position, direction); - if (position && direction) { - REQUIRE(2.0 == Approx(position->x)); - REQUIRE(3.0 == Approx(position->y)); - } else if (position) { - REQUIRE(1.0 == Approx(position->x)); - REQUIRE(2.0 == Approx(position->y)); - } +TEST_CASE_METHOD(EntitiesFixture, "TestApplySystem") { + systems.add<MovementSystem>(); + systems.configure(); + + systems.update<MovementSystem>(0.0); + Position *position; + Direction *direction; + for (auto entity : created_entities) { + entity.unpack<Position, Direction>(position, direction); + if (position && direction) { + REQUIRE(2.0 == Approx(position->x)); + REQUIRE(3.0 == Approx(position->y)); + } else if (position) { + REQUIRE(1.0 == Approx(position->x)); + REQUIRE(2.0 == Approx(position->y)); } } } diff --git a/entityx/deps/Dependencies_test.cc b/entityx/deps/Dependencies_test.cc index e18623d..fe9844c 100644 --- a/entityx/deps/Dependencies_test.cc +++ b/entityx/deps/Dependencies_test.cc @@ -26,44 +26,39 @@ struct B : public entityx::Component<B> { }; struct C : public entityx::Component<C> {}; +TEST_CASE_METHOD(entityx::EntityX, "TestSingleDependency") { + systems.add<deps::Dependency<A, B>>(); + systems.configure(); -TEST_CASE("DepsTest", "[deps]") { - entityx::EntityX ex; - - SECTION("TestSingleDependency") { - ex.systems.add<deps::Dependency<A, B>>(); - ex.systems.configure(); - - entityx::Entity e = ex.entities.create(); - REQUIRE(!static_cast<bool>(e.component<A>())); - REQUIRE(!static_cast<bool>(e.component<B>())); - e.assign<A>(); - REQUIRE(static_cast<bool>(e.component<A>())); - REQUIRE(static_cast<bool>(e.component<B>())); - } + entityx::Entity e = entities.create(); + REQUIRE(!static_cast<bool>(e.component<A>())); + REQUIRE(!static_cast<bool>(e.component<B>())); + e.assign<A>(); + REQUIRE(static_cast<bool>(e.component<A>())); + REQUIRE(static_cast<bool>(e.component<B>())); +} - SECTION("TestMultipleDependencies") { - ex.systems.add<deps::Dependency<A, B, C>>(); - ex.systems.configure(); +TEST_CASE_METHOD(entityx::EntityX, "TestMultipleDependencies") { + systems.add<deps::Dependency<A, B, C>>(); + systems.configure(); - entityx::Entity e = ex.entities.create(); - REQUIRE(!static_cast<bool>(e.component<A>())); - REQUIRE(!static_cast<bool>(e.component<B>())); - REQUIRE(!static_cast<bool>(e.component<C>())); - e.assign<A>(); - REQUIRE(static_cast<bool>(e.component<A>())); - REQUIRE(static_cast<bool>(e.component<B>())); - REQUIRE(static_cast<bool>(e.component<C>())); - } + entityx::Entity e = entities.create(); + REQUIRE(!static_cast<bool>(e.component<A>())); + REQUIRE(!static_cast<bool>(e.component<B>())); + REQUIRE(!static_cast<bool>(e.component<C>())); + e.assign<A>(); + REQUIRE(static_cast<bool>(e.component<A>())); + REQUIRE(static_cast<bool>(e.component<B>())); + REQUIRE(static_cast<bool>(e.component<C>())); +} - SECTION("TestDependencyDoesNotRecreateComponent") { - ex.systems.add<deps::Dependency<A, B>>(); - ex.systems.configure(); +TEST_CASE_METHOD(entityx::EntityX, "TestDependencyDoesNotRecreateComponent") { + systems.add<deps::Dependency<A, B>>(); + systems.configure(); - entityx::Entity e = ex.entities.create(); - e.assign<B>(true); - REQUIRE(e.component<B>()->b); - e.assign<A>(); - REQUIRE(e.component<B>()->b); - } + entityx::Entity e = entities.create(); + e.assign<B>(true); + REQUIRE(e.component<B>()->b); + e.assign<A>(); + REQUIRE(e.component<B>()->b); } diff --git a/entityx/help/Pool_test.cc b/entityx/help/Pool_test.cc index 611b8c0..f04bc29 100644 --- a/entityx/help/Pool_test.cc +++ b/entityx/help/Pool_test.cc @@ -27,7 +27,7 @@ struct Position { }; -TEST_CASE("TestPoolReserve", "[pool]") { +TEST_CASE("TestPoolReserve") { entityx::Pool<Position, 8> pool; REQUIRE(0 == pool.capacity()); REQUIRE(0 == pool.chunks()); @@ -41,7 +41,7 @@ TEST_CASE("TestPoolReserve", "[pool]") { REQUIRE(2 == pool.chunks()); } -TEST_CASE("TestPoolPointers", "[pool]") { +TEST_CASE("TestPoolPointers") { entityx::Pool<Position, 8> pool; std::vector<char*> ptrs; for (int i = 0; i < 4; i++) { @@ -66,7 +66,7 @@ TEST_CASE("TestPoolPointers", "[pool]") { REQUIRE(extrapolated_p24 != p24); } -TEST_CASE("TestDeconstruct", "[pool]") { +TEST_CASE("TestDeconstruct") { entityx::Pool<Position, 8> pool; pool.expand(8); |