From a4f21371c34b894429bb1845a69d0ad1093e88b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Wed, 11 Dec 2024 00:42:28 +0100 Subject: [PATCH] Fix bad unique pointer usage --- src/Program.cpp | 19 +++++++++---- tests/src/TestCases.cpp | 63 ++++++++++++++++++++++------------------- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/Program.cpp b/src/Program.cpp index ec34b34..0a53ea3 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -33,15 +33,22 @@ void Program::run() runSolvers(); } +template +void runSolver(SolverEngine& solverEngine) +{ + auto solver = std::make_unique(); + solverEngine.run(*solver); +} + void Program::runSolvers() { SolverEngine solverEngine{ getInputPaths() }; - solverEngine.run(*std::make_unique()); - solverEngine.run(*std::make_unique()); - solverEngine.run(*std::make_unique()); - solverEngine.run(*std::make_unique()); - solverEngine.run(*std::make_unique()); - solverEngine.run(*std::make_unique()); + runSolver(solverEngine); + runSolver(solverEngine); + runSolver(solverEngine); + runSolver(solverEngine); + runSolver(solverEngine); + runSolver(solverEngine); } std::vector Program::getInputPaths() const diff --git a/tests/src/TestCases.cpp b/tests/src/TestCases.cpp index 4debf88..6a2f51f 100644 --- a/tests/src/TestCases.cpp +++ b/tests/src/TestCases.cpp @@ -13,58 +13,63 @@ // You should have received a copy of the GNU General Public License along with // this program. If not, see . -#include -#include +#include #include #include #include -#include +#include +#include #define REQUIRE_MESSAGE(cond, msg) if (!(cond)) { INFO(msg); REQUIRE(cond); } -class HistorianHysteriaTests +class TestCaseBase { public: - Part1TestContext part1TestContext_{ getInputPaths() }; - Part2TestContext part2TestContext_{ getInputPaths() }; - Part1TestContext part1ExampleTestContext_{ getExampleInputPaths() }; - Part2TestContext part2ExampleTestContext_{ getExampleInputPaths() }; - - void runTest(Solver &solver, const long long int expected, const TestContext &context) - { - SolverEngine solverEngine{context.getInputPaths()}; - solverEngine.run(solver); - - REQUIRE(expected == context.getResult(solver)); - } - + Part1TestContext part1TestContext{ getInputPaths() }; + Part2TestContext part2TestContext{ getInputPaths() }; + Part1TestContext part1ExampleTestContext{ getExampleInputPaths() }; + Part2TestContext part2ExampleTestContext{ getExampleInputPaths() }; private: std::vector getInputPaths() const { return std::vector{ "data", "../../../data", "../../../../data" }; } - std::vector getExampleInputPaths() const { - return std::vector{ "data/examples", "../../../data/examples", "../../../../data/examples" }; + return std::vector{ "data/examples", "../../../data/examples", + "../../../../data/examples" }; } }; -TEST_CASE( "[HistorianHysteriaTests]" ) +template +void runTest(const long long int expected, const TestContext& context) { - HistorianHysteriaTests tests; - SECTION( "FullData1" ) { - tests.runTest(*std::make_unique(), 2176849, tests.part1TestContext_); + auto solver = std::make_unique(); + SolverEngine solverEngine{ context.getInputPaths() }; + solverEngine.run(*solver); + + REQUIRE(expected == context.getResult(*solver)); +} + +TEST_CASE("[HistorianHysteriaTests]") +{ + TestCaseBase test; + SECTION("FullData1") + { + runTest(2176849, test.part1TestContext); } - SECTION( "FullData2" ) { - tests.runTest(*std::make_unique(), 23384288, tests.part2TestContext_); + SECTION("FullData2") + { + runTest(23384288, test.part2TestContext); } - SECTION( "ExampleData1" ) { - tests.runTest(*std::make_unique(), 11, tests.part1ExampleTestContext_); + SECTION("ExampleData1") + { + runTest(11, test.part1ExampleTestContext); } - SECTION( "ExampleData2" ) { - tests.runTest(*std::make_unique(), 31, tests.part2ExampleTestContext_); + SECTION("ExampleData2") + { + runTest(31, test.part2ExampleTestContext); } }