diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 713a4c21..92b2281a 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -174,13 +174,11 @@ void Entity::ApplyComponentWhitelist(std::vector& compone const auto whitelistIndex = GetVar(u"componentWhitelist"); if (whitelistIndex < 0 || whitelistIndex >= m_ComponentWhitelists.size()) return; - for (const auto& componentToKeep : m_ComponentWhitelists.at(whitelistIndex)) { - while (true) { - const auto componentIter = std::find(components.begin(), components.end(), componentToKeep); - if (componentIter == components.end()) break; - components.erase(componentIter); - } - } + const auto& whitelist = m_ComponentWhitelists.at(whitelistIndex); + const auto endRange = std::remove_if(components.begin(), components.end(), [&whitelist](const eReplicaComponentType& componentCandidate) { + return std::find(whitelist.begin(), whitelist.end(), componentCandidate) == whitelist.end(); + }); + components.erase(endRange, components.end()); } void Entity::Initialize() { diff --git a/tests/dGameTests/EntityTests.cpp b/tests/dGameTests/EntityTests.cpp index d69bb6ce..3a2d71a8 100644 --- a/tests/dGameTests/EntityTests.cpp +++ b/tests/dGameTests/EntityTests.cpp @@ -21,19 +21,20 @@ protected: this->TearDownDependencies(); } - void RunWhitelistTest(const int32_t componentIndex, std::vector& whitelist) { - entity->SetVar(u"componentWhitelist", componentIndex); - entity->ApplyComponentWhitelist(whitelist); - const auto whitelists = Entity::GetComponentWhitelists(); - for (const auto& component : whitelists.at(componentIndex)) { - EXPECT_FALSE(std::find(whitelist.begin(), whitelist.end(), component) != whitelist.end()); - } + void RunWhitelistTest(const int32_t whitelistIndex, std::vector componentList) { + Game::logger->Log("EntityTests", "whitelist test %i", whitelistIndex); + entity->SetVar(u"componentWhitelist", whitelistIndex); + entity->ApplyComponentWhitelist(componentList); + const auto whitelist = Entity::GetComponentWhitelists().at(whitelistIndex); + std::for_each(whitelist.begin(), whitelist.end(), [&componentList](const eReplicaComponentType& keptComponent) { + EXPECT_EQ(std::count(componentList.begin(), componentList.end(), keptComponent), 2); + }); } }; TEST_F(EntityTests, WhitelistTest) { const auto whitelists = Entity::GetComponentWhitelists(); - std::vector whitelist = { + std::vector components = { eReplicaComponentType::CONTROLLABLE_PHYSICS, eReplicaComponentType::SIMPLE_PHYSICS, eReplicaComponentType::MODEL_BEHAVIOR, @@ -55,9 +56,9 @@ TEST_F(EntityTests, WhitelistTest) { eReplicaComponentType::SKILL, eReplicaComponentType::DESTROYABLE, }; - RunWhitelistTest(0, whitelist); - RunWhitelistTest(1, whitelist); - RunWhitelistTest(2, whitelist); - RunWhitelistTest(3, whitelist); - RunWhitelistTest(4, whitelist); + RunWhitelistTest(0, components); + RunWhitelistTest(1, components); + RunWhitelistTest(2, components); + RunWhitelistTest(3, components); + RunWhitelistTest(4, components); }