From 5f085657994fdd2164a4fd5d4474e75e1580505f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Sun, 1 Dec 2024 16:25:58 +0100 Subject: [PATCH] Update unit tests with contexts --- .../UnitTests/Part1TestContext.cpp | 25 ++++++++++++++ AdventOfCode2024/UnitTests/Part1TestContext.h | 26 ++++++++++++++ .../UnitTests/Part2TestContext.cpp | 25 ++++++++++++++ AdventOfCode2024/UnitTests/Part2TestContext.h | 26 ++++++++++++++ AdventOfCode2024/UnitTests/TestContext.cpp | 25 ++++++++++++++ AdventOfCode2024/UnitTests/TestContext.h | 32 +++++++++++++++++ AdventOfCode2024/UnitTests/UnitTests.cpp | 34 ++++++++++--------- AdventOfCode2024/UnitTests/UnitTests.vcxproj | 6 ++++ .../UnitTests/UnitTests.vcxproj.filters | 18 ++++++++++ 9 files changed, 201 insertions(+), 16 deletions(-) create mode 100644 AdventOfCode2024/UnitTests/Part1TestContext.cpp create mode 100644 AdventOfCode2024/UnitTests/Part1TestContext.h create mode 100644 AdventOfCode2024/UnitTests/Part2TestContext.cpp create mode 100644 AdventOfCode2024/UnitTests/Part2TestContext.h create mode 100644 AdventOfCode2024/UnitTests/TestContext.cpp create mode 100644 AdventOfCode2024/UnitTests/TestContext.h diff --git a/AdventOfCode2024/UnitTests/Part1TestContext.cpp b/AdventOfCode2024/UnitTests/Part1TestContext.cpp new file mode 100644 index 0000000..7601f3c --- /dev/null +++ b/AdventOfCode2024/UnitTests/Part1TestContext.cpp @@ -0,0 +1,25 @@ +// Solutions to the Advent Of Code 2024. +// Copyright (C) 2024 Stefan Müller +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +#include "pch.h" +#include "Part1TestContext.h" + +Part1TestContext::Part1TestContext(std::vector inputPaths) + : TestContext{ inputPaths } {} + +long long int Part1TestContext::getResult(Solver& solver) const +{ + return solver.getResultPart1(); +} diff --git a/AdventOfCode2024/UnitTests/Part1TestContext.h b/AdventOfCode2024/UnitTests/Part1TestContext.h new file mode 100644 index 0000000..a10f634 --- /dev/null +++ b/AdventOfCode2024/UnitTests/Part1TestContext.h @@ -0,0 +1,26 @@ +// Solutions to the Advent Of Code 2024. +// Copyright (C) 2024 Stefan Müller +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +#pragma once + +#include "TestContext.h" + +class Part1TestContext : + public TestContext +{ +public: + Part1TestContext(std::vector inputPaths); + virtual long long int getResult(Solver& solver) const override; +}; diff --git a/AdventOfCode2024/UnitTests/Part2TestContext.cpp b/AdventOfCode2024/UnitTests/Part2TestContext.cpp new file mode 100644 index 0000000..a3b3b63 --- /dev/null +++ b/AdventOfCode2024/UnitTests/Part2TestContext.cpp @@ -0,0 +1,25 @@ +// Solutions to the Advent Of Code 2024. +// Copyright (C) 2024 Stefan Müller +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +#include "pch.h" +#include "Part2TestContext.h" + +Part2TestContext::Part2TestContext(std::vector inputPaths) + : TestContext{ inputPaths } {} + +long long int Part2TestContext::getResult(Solver& solver) const +{ + return solver.getResultPart2(); +} diff --git a/AdventOfCode2024/UnitTests/Part2TestContext.h b/AdventOfCode2024/UnitTests/Part2TestContext.h new file mode 100644 index 0000000..d157fbe --- /dev/null +++ b/AdventOfCode2024/UnitTests/Part2TestContext.h @@ -0,0 +1,26 @@ +// Solutions to the Advent Of Code 2024. +// Copyright (C) 2024 Stefan Müller +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +#pragma once + +#include "TestContext.h" + +class Part2TestContext : + public TestContext +{ +public: + Part2TestContext(std::vector inputPaths); + virtual long long int getResult(Solver& solver) const override; +}; diff --git a/AdventOfCode2024/UnitTests/TestContext.cpp b/AdventOfCode2024/UnitTests/TestContext.cpp new file mode 100644 index 0000000..3abd629 --- /dev/null +++ b/AdventOfCode2024/UnitTests/TestContext.cpp @@ -0,0 +1,25 @@ +// Solutions to the Advent Of Code 2024. +// Copyright (C) 2024 Stefan Müller +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +#include "pch.h" +#include "TestContext.h" + +TestContext::TestContext(std::vector inputPaths) + : inputPaths_{ inputPaths } {} + +std::vector TestContext::getInputPaths() const +{ + return inputPaths_; +} diff --git a/AdventOfCode2024/UnitTests/TestContext.h b/AdventOfCode2024/UnitTests/TestContext.h new file mode 100644 index 0000000..d8255cb --- /dev/null +++ b/AdventOfCode2024/UnitTests/TestContext.h @@ -0,0 +1,32 @@ +// Solutions to the Advent Of Code 2024. +// Copyright (C) 2024 Stefan Müller +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +#pragma once + +#include +#include + +#include + +class TestContext +{ +public: + TestContext(std::vector inputPaths); + virtual ~TestContext() {}; + std::vector getInputPaths() const; + virtual long long int getResult(Solver& solver) const = 0; +private: + std::vector inputPaths_; +}; diff --git a/AdventOfCode2024/UnitTests/UnitTests.cpp b/AdventOfCode2024/UnitTests/UnitTests.cpp index 2be975a..62ad50e 100644 --- a/AdventOfCode2024/UnitTests/UnitTests.cpp +++ b/AdventOfCode2024/UnitTests/UnitTests.cpp @@ -16,6 +16,8 @@ #include "pch.h" #include "CppUnitTest.h" +#include "Part1TestContext.h" +#include "Part2TestContext.h" #include "../Demo.h" #include "../Solver.h" #include "../SolverEngine.h" @@ -29,37 +31,37 @@ namespace UnitTests public: TEST_METHOD(FullData1) { - SolverEngine solverEngine{ getInputPaths() }; - std::unique_ptr solver{ std::make_unique() }; - solverEngine.run(*solver); - assertAreEqual(66, solver->getResultPart1()); + runTest(*std::make_unique(), 66, part1TestContext_); } TEST_METHOD(FullData2) { - SolverEngine solverEngine{ getInputPaths() }; - std::unique_ptr solver = std::make_unique(); - solverEngine.run(*solver); - assertAreEqual(39916800, solver->getResultPart2()); + runTest(*std::make_unique(), 39916800, part2TestContext_); } TEST_METHOD(ExampleData1) { - SolverEngine solverEngine{ getExampleInputPaths() }; - std::unique_ptr solver = std::make_unique(); - solverEngine.run(*solver); - assertAreEqual(10, solver->getResultPart1()); + runTest(*std::make_unique(), 10, part1ExampleTestContext_); } TEST_METHOD(ExampleData2) { - SolverEngine solverEngine{ getExampleInputPaths() }; - std::unique_ptr solver = std::make_unique(); - solverEngine.run(*solver); - assertAreEqual(24, solver->getResultPart2()); + runTest(*std::make_unique(), 24, part2ExampleTestContext_); } private: + 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); + assertAreEqual(expected, context.getResult(solver)); + } + std::vector getInputPaths() const { return std::vector{ "../../data", "../../../../data" }; diff --git a/AdventOfCode2024/UnitTests/UnitTests.vcxproj b/AdventOfCode2024/UnitTests/UnitTests.vcxproj index e11dda1..7306324 100644 --- a/AdventOfCode2024/UnitTests/UnitTests.vcxproj +++ b/AdventOfCode2024/UnitTests/UnitTests.vcxproj @@ -159,16 +159,22 @@ + + Create Create Create Create + + + + diff --git a/AdventOfCode2024/UnitTests/UnitTests.vcxproj.filters b/AdventOfCode2024/UnitTests/UnitTests.vcxproj.filters index 264bee8..dc63046 100644 --- a/AdventOfCode2024/UnitTests/UnitTests.vcxproj.filters +++ b/AdventOfCode2024/UnitTests/UnitTests.vcxproj.filters @@ -21,10 +21,28 @@ Source Files + + Source Files + + + Source Files + + + Source Files + Header Files + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file