From 8f9f2b2b8d60f599e5fe327d86b2cb4e55444ee7 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 17 Apr 2021 16:23:29 +0200 Subject: [PATCH] Fixes issue with iterating over two script sets. Signed-off-by: Deukhoofd --- .../ScriptHandling/ScriptAggregator.hpp | 17 +-- .../ScriptTests/ScriptAggregatorTests.cpp | 105 +++++++++++------- 2 files changed, 74 insertions(+), 48 deletions(-) diff --git a/src/Battling/ScriptHandling/ScriptAggregator.hpp b/src/Battling/ScriptHandling/ScriptAggregator.hpp index a7a7f2a..e5b56ac 100644 --- a/src/Battling/ScriptHandling/ScriptAggregator.hpp +++ b/src/Battling/ScriptHandling/ScriptAggregator.hpp @@ -7,7 +7,7 @@ namespace CreatureLib::Battling { class ScriptAggregator { - const ScriptWrapper *_scripts; + const ScriptWrapper* _scripts; i32 _size; i32 _index = 0; i32 _setIndex = 0; @@ -15,7 +15,7 @@ namespace CreatureLib::Battling { inline void IncrementToNextNotNull(bool initialIncrement = true) { if (_scripts[_index].IsSet()) { _setIndex++; - if (_setIndex >= (i32) _scripts[_index].GetScriptSet()->Count()) { + if (_setIndex >= (i32)_scripts[_index].GetScriptSet()->Count()) { _setIndex = -1; } else { return; @@ -32,10 +32,10 @@ namespace CreatureLib::Battling { } public: - ScriptAggregator() {}; + ScriptAggregator(){}; - explicit ScriptAggregator(const ArbUt::List &scripts) - : _scripts(scripts.RawData()), _size(scripts.Count()) { + explicit ScriptAggregator(const ArbUt::List& scripts) + : _scripts(scripts.RawData()), _size(scripts.Count()) { Reset(); }; @@ -60,13 +60,16 @@ namespace CreatureLib::Battling { } ArbUt::BorrowedPtr GetNext() { - auto ¤t = _scripts[_index]; + auto& current = _scripts[_index]; if (!current.IsSet()) { auto s = current.GetScript(); IncrementToNextNotNull(); return (*s); } else { - auto &set = current.GetScriptSet()->GetIterator(); + auto& set = current.GetScriptSet()->GetIterator(); + if (_setIndex == -1) { + _setIndex = 0; + } auto v = set[_setIndex]; IncrementToNextNotNull(); return v; diff --git a/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp index 5903733..3cc105c 100644 --- a/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp +++ b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp @@ -12,49 +12,49 @@ private: ArbUt::StringView _name; public: - explicit TestScript(const ArbUt::StringView &name) : _name(name) {}; + explicit TestScript(const ArbUt::StringView& name) : _name(name){}; - const ArbUt::StringView &GetName() const noexcept override { return _name; } + const ArbUt::StringView& GetName() const noexcept override { return _name; } - void TestMethod(int &runCount) { runCount++; } + void TestMethod(int& runCount) { runCount++; } - BattleScript *Clone() override { return new TestScript(_name); } + BattleScript* Clone() override { return new TestScript(_name); } }; -TEST_CASE ("Script Aggregator properly iterates containing script.") { +TEST_CASE("Script Aggregator properly iterates containing script.") { auto script = std::make_unique("test"); auto ran = 0; auto vec = ArbUt::List{ - ScriptWrapper::FromScript(reinterpret_cast *>(&script))}; + ScriptWrapper::FromScript(reinterpret_cast*>(&script))}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNext(); next.As()->TestMethod(ran); } - CHECK(ran == 1); + CHECK(ran == 1); } -TEST_CASE ("Script Aggregator properly iterates multiple scripts.") { +TEST_CASE("Script Aggregator properly iterates multiple scripts.") { auto script = std::make_unique("test"); auto script2 = std::make_unique("test2"); auto script3 = std::make_unique("test3"); auto ran = 0; auto vec = ArbUt::List{ - ScriptWrapper::FromScript(reinterpret_cast *>(&script)), - ScriptWrapper::FromScript(reinterpret_cast *>(&script2)), - ScriptWrapper::FromScript(reinterpret_cast *>(&script3))}; + ScriptWrapper::FromScript(reinterpret_cast*>(&script)), + ScriptWrapper::FromScript(reinterpret_cast*>(&script2)), + ScriptWrapper::FromScript(reinterpret_cast*>(&script3))}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNext(); next.As()->TestMethod(ran); } - CHECK(ran == 3); + CHECK(ran == 3); } -TEST_CASE ("Script Aggregator properly iterates Script Set.") { - BattleScript *script = new TestScript("test"); - BattleScript *script2 = new TestScript("test2"); - BattleScript *script3 = new TestScript("test3"); +TEST_CASE("Script Aggregator properly iterates Script Set.") { + BattleScript* script = new TestScript("test"); + BattleScript* script2 = new TestScript("test2"); + BattleScript* script3 = new TestScript("test3"); auto ran = 0; auto set = ScriptSet(); set.Add(script); @@ -66,80 +66,103 @@ TEST_CASE ("Script Aggregator properly iterates Script Set.") { auto next = aggr.GetNextNotNull(); next.value().As()->TestMethod(ran); } - CHECK(ran == 3); + CHECK(ran == 3); } -TEST_CASE ("Script Aggregator properly iterates data of Script Set and Script.") { +TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.") { auto script = std::make_unique("test"); - BattleScript *script2 = new TestScript("test2"); - BattleScript *script3 = new TestScript("test3"); + BattleScript* script2 = new TestScript("test2"); + BattleScript* script3 = new TestScript("test3"); auto ran = 0; auto set = ScriptSet(); set.Add(script2); set.Add(script3); auto vec = ArbUt::List{ - ScriptWrapper::FromSet(&set), - ScriptWrapper::FromScript(reinterpret_cast *>(&script))}; + ScriptWrapper::FromSet(&set), + ScriptWrapper::FromScript(reinterpret_cast*>(&script))}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNextNotNull(); next.value().As()->TestMethod(ran); } - CHECK(ran == 3); + CHECK(ran == 3); } -TEST_CASE ("Script Aggregator properly iterates data of Script and Script Set.") { +TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.") { auto script = std::make_unique("test"); - BattleScript *script2 = new TestScript("test2"); - BattleScript *script3 = new TestScript("test3"); + BattleScript* script2 = new TestScript("test2"); + BattleScript* script3 = new TestScript("test3"); auto ran = 0; auto set = ScriptSet(); set.Add(script2); set.Add(script3); auto vec = - ArbUt::List{ - ScriptWrapper::FromScript(reinterpret_cast *>(&script)), - ScriptWrapper::FromSet(&set)}; + ArbUt::List{ScriptWrapper::FromScript(reinterpret_cast*>(&script)), + ScriptWrapper::FromSet(&set)}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNextNotNull(); next.value().As()->TestMethod(ran); } - CHECK(ran == 3); + CHECK(ran == 3); } -TEST_CASE ("Script Aggregator properly iterates data of Script, Script Set and Script.") { +TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Script.") { auto script = std::make_unique("test"); - BattleScript *script2 = new TestScript("test2"); - BattleScript *script3 = new TestScript("test3"); + BattleScript* script2 = new TestScript("test2"); + BattleScript* script3 = new TestScript("test3"); auto script4 = std::make_unique("test4"); auto ran = 0; auto set = ScriptSet(); set.Add(script2); set.Add(script3); auto vec = ArbUt::List{ - ScriptWrapper::FromScript(reinterpret_cast *>(&script)), - ScriptWrapper::FromSet(&set), - ScriptWrapper::FromScript(reinterpret_cast *>(&script4))}; + ScriptWrapper::FromScript(reinterpret_cast*>(&script)), + ScriptWrapper::FromSet(&set), + ScriptWrapper::FromScript(reinterpret_cast*>(&script4))}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNextNotNull(); next.value().As()->TestMethod(ran); } - CHECK(ran == 4); + CHECK(ran == 4); } -TEST_CASE ("Script Aggregator properly iterates when empty.") { +TEST_CASE("Script Aggregator properly iterates multiple script sets.") { + BattleScript* script1 = new TestScript("test"); + BattleScript* script2 = new TestScript("test2"); + BattleScript* script3 = new TestScript("test3"); + BattleScript* script4 = new TestScript("test4"); + auto ran = 0; + auto set1 = ScriptSet(); + set1.Add(script2); + set1.Add(script3); + auto set2 = ScriptSet(); + set2.Add(script1); + set2.Add(script4); + auto vec = ArbUt::List{ + ScriptWrapper::FromSet(&set1), + ScriptWrapper::FromSet(&set2), + }; + auto aggr = ScriptAggregator(vec); + while (aggr.HasNext()) { + auto next = aggr.GetNextNotNull(); + next.value().As()->TestMethod(ran); + } + CHECK(ran == 4); +} + +TEST_CASE("Script Aggregator properly iterates when empty.") { auto ran = 0; auto vec = ArbUt::List{}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { THROW("Aggregator returned a script, but should have been empty."); } - CHECK(ran == 0); + CHECK(ran == 0); } -TEST_CASE ("Script Aggregator properly iterates empty Script Set.") { +TEST_CASE("Script Aggregator properly iterates empty Script Set.") { auto ran = 0; auto set = ScriptSet(); auto vec = ArbUt::List{ScriptWrapper::FromSet(&set)}; @@ -148,7 +171,7 @@ TEST_CASE ("Script Aggregator properly iterates empty Script Set.") { while (aggr.HasNext()) { ran++; } - CHECK(ran == 0); + CHECK(ran == 0); } #endif \ No newline at end of file