From f8567033f45f2155ca6492765da1d46a12f28e5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Sat, 30 Nov 2024 23:27:15 +0100 Subject: [PATCH] Update basic solver logic and add unit tests - Fix wrong year in program text - Fix bad Solver reference parameter in SolverEngine::run() - Add demo test cases and unit test helper methods - Fix unit test project linker settings --- AdventOfCode2024/Program.cpp | 4 +- AdventOfCode2024/SolverEngine.cpp | 14 ++-- AdventOfCode2024/SolverEngine.h | 2 +- AdventOfCode2024/UnitTests/UnitTests.cpp | 69 ++++++++++++++++++-- AdventOfCode2024/UnitTests/UnitTests.vcxproj | 5 +- 5 files changed, 77 insertions(+), 17 deletions(-) diff --git a/AdventOfCode2024/Program.cpp b/AdventOfCode2024/Program.cpp index 642ad5a..83908da 100644 --- a/AdventOfCode2024/Program.cpp +++ b/AdventOfCode2024/Program.cpp @@ -22,14 +22,14 @@ void Program::run() { - std::cout << "### Advent of Code 2023 ###\n"; + std::cout << "### Advent of Code 2024 ###\n"; runSolvers(); } void Program::runSolvers() { SolverEngine solverEngine{ getInputPaths() }; - solverEngine.run(std::make_unique()); + solverEngine.run(*std::make_unique()); } std::vector Program::getInputPaths() const diff --git a/AdventOfCode2024/SolverEngine.cpp b/AdventOfCode2024/SolverEngine.cpp index 1940e4f..72314d8 100644 --- a/AdventOfCode2024/SolverEngine.cpp +++ b/AdventOfCode2024/SolverEngine.cpp @@ -21,25 +21,25 @@ SolverEngine::SolverEngine(const std::vector& inputPaths) : inputPaths_{ inputPaths } {} -void SolverEngine::run(const std::unique_ptr solver) +void SolverEngine::run(Solver& solver) { - std::cout << "\n--- " << solver->getPuzzleName() << " ---\n"; + std::cout << "\n--- " << solver.getPuzzleName() << " ---\n"; - auto fullFilePath = tryGetValidFullInputFilePath(solver->getInputFileName()); + auto fullFilePath = tryGetValidFullInputFilePath(solver.getInputFileName()); if (fullFilePath != "") { std::string line; std::ifstream inputFile{ fullFilePath }; while (std::getline(inputFile, line)) { - solver->processDataLine(line); + solver.processDataLine(line); } inputFile.close(); - solver->finish(); + solver.finish(); - std::cout << "Part 1: " << solver->getResultPart1() - << "\nPart 2: " << solver->getResultPart2() << std::endl; + std::cout << "Part 1: " << solver.getResultPart1() + << "\nPart 2: " << solver.getResultPart2() << std::endl; } } diff --git a/AdventOfCode2024/SolverEngine.h b/AdventOfCode2024/SolverEngine.h index 854733a..b3a1eaf 100644 --- a/AdventOfCode2024/SolverEngine.h +++ b/AdventOfCode2024/SolverEngine.h @@ -26,7 +26,7 @@ class SolverEngine { public: SolverEngine(const std::vector& inputPaths); - void run(const std::unique_ptr solver); + void run(Solver& solver); private: std::vector inputPaths_; std::filesystem::path tryGetValidFullInputFilePath(const std::string& inputFileName); diff --git a/AdventOfCode2024/UnitTests/UnitTests.cpp b/AdventOfCode2024/UnitTests/UnitTests.cpp index 7c0e4c4..2be975a 100644 --- a/AdventOfCode2024/UnitTests/UnitTests.cpp +++ b/AdventOfCode2024/UnitTests/UnitTests.cpp @@ -1,22 +1,79 @@ +// 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 "CppUnitTest.h" +#include "../Demo.h" +#include "../Solver.h" +#include "../SolverEngine.h" + using namespace Microsoft::VisualStudio::CppUnitTestFramework; namespace UnitTests { - TEST_CLASS(UnitTests) + TEST_CLASS(DemoTests) { public: - - TEST_METHOD(TestMethod1) + TEST_METHOD(FullData1) { - Assert::Fail(); + SolverEngine solverEngine{ getInputPaths() }; + std::unique_ptr solver{ std::make_unique() }; + solverEngine.run(*solver); + assertAreEqual(66, solver->getResultPart1()); } - TEST_METHOD(TestMethod2) + TEST_METHOD(FullData2) { - Assert::AreEqual(1, 1); + SolverEngine solverEngine{ getInputPaths() }; + std::unique_ptr solver = std::make_unique(); + solverEngine.run(*solver); + assertAreEqual(39916800, solver->getResultPart2()); + } + + TEST_METHOD(ExampleData1) + { + SolverEngine solverEngine{ getExampleInputPaths() }; + std::unique_ptr solver = std::make_unique(); + solverEngine.run(*solver); + assertAreEqual(10, solver->getResultPart1()); + } + + TEST_METHOD(ExampleData2) + { + SolverEngine solverEngine{ getExampleInputPaths() }; + std::unique_ptr solver = std::make_unique(); + solverEngine.run(*solver); + assertAreEqual(24, solver->getResultPart2()); + } + + private: + std::vector getInputPaths() const + { + return std::vector{ "../../data", "../../../../data" }; + } + + std::vector getExampleInputPaths() const + { + return std::vector{ "../../data/examples", "../../../../data/examples" }; + } + + void assertAreEqual(const long long int expected, const long long int actual) const + { + std::wstring message = L"Expected: <" + std::to_wstring(expected) + L"> Actual: <" + std::to_wstring(actual) + L">"; + Assert::IsTrue(expected == actual, message.c_str()); } }; } diff --git a/AdventOfCode2024/UnitTests/UnitTests.vcxproj b/AdventOfCode2024/UnitTests/UnitTests.vcxproj index 46d085c..e11dda1 100644 --- a/AdventOfCode2024/UnitTests/UnitTests.vcxproj +++ b/AdventOfCode2024/UnitTests/UnitTests.vcxproj @@ -77,6 +77,7 @@ true + ..;$(IncludePath) true @@ -96,10 +97,12 @@ _DEBUG;%(PreprocessorDefinitions) true pch.h + stdcpp17 Windows - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + $(VCInstallDir)UnitTest\lib;..\$(Platform)\$(Configuration);D:\Projects\VS\AdventOfCode2024Full\code\AdventOfCode2024\x64\Debug;%(AdditionalLibraryDirectories) + Demo.obj;Solver.obj;SolverEngine.obj;%(AdditionalDependencies)