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
This commit is contained in:
Stefan Müller 2024-11-30 23:27:15 +01:00
parent 06bf00f979
commit f8567033f4
5 changed files with 77 additions and 17 deletions

View File

@ -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<Demo>());
solverEngine.run(*std::make_unique<Demo>());
}
std::vector<std::string> Program::getInputPaths() const

View File

@ -21,25 +21,25 @@
SolverEngine::SolverEngine(const std::vector<std::string>& inputPaths)
: inputPaths_{ inputPaths } {}
void SolverEngine::run(const std::unique_ptr<Solver> 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;
}
}

View File

@ -26,7 +26,7 @@ class SolverEngine
{
public:
SolverEngine(const std::vector<std::string>& inputPaths);
void run(const std::unique_ptr<Solver> solver);
void run(Solver& solver);
private:
std::vector<std::string> inputPaths_;
std::filesystem::path tryGetValidFullInputFilePath(const std::string& inputFileName);

View File

@ -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 <http://www.gnu.org/licenses/>.
#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> solver{ std::make_unique<Demo>() };
solverEngine.run(*solver);
assertAreEqual(66, solver->getResultPart1());
}
TEST_METHOD(TestMethod2)
TEST_METHOD(FullData2)
{
Assert::AreEqual(1, 1);
SolverEngine solverEngine{ getInputPaths() };
std::unique_ptr<Solver> solver = std::make_unique<Demo>();
solverEngine.run(*solver);
assertAreEqual(39916800, solver->getResultPart2());
}
TEST_METHOD(ExampleData1)
{
SolverEngine solverEngine{ getExampleInputPaths() };
std::unique_ptr<Solver> solver = std::make_unique<Demo>();
solverEngine.run(*solver);
assertAreEqual(10, solver->getResultPart1());
}
TEST_METHOD(ExampleData2)
{
SolverEngine solverEngine{ getExampleInputPaths() };
std::unique_ptr<Solver> solver = std::make_unique<Demo>();
solverEngine.run(*solver);
assertAreEqual(24, solver->getResultPart2());
}
private:
std::vector<std::string> getInputPaths() const
{
return std::vector<std::string>{ "../../data", "../../../../data" };
}
std::vector<std::string> getExampleInputPaths() const
{
return std::vector<std::string>{ "../../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());
}
};
}

View File

@ -77,6 +77,7 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>..;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
@ -96,10 +97,12 @@
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<UseFullPaths>true</UseFullPaths>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;..\$(Platform)\$(Configuration);D:\Projects\VS\AdventOfCode2024Full\code\AdventOfCode2024\x64\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Demo.obj;Solver.obj;SolverEngine.obj;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">