Update unit tests with contexts

This commit is contained in:
Stefan Müller 2024-12-01 16:25:58 +01:00
parent f8567033f4
commit 5f08565799
9 changed files with 201 additions and 16 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
#include "pch.h"
#include "Part1TestContext.h"
Part1TestContext::Part1TestContext(std::vector<std::string> inputPaths)
: TestContext{ inputPaths } {}
long long int Part1TestContext::getResult(Solver& solver) const
{
return solver.getResultPart1();
}

View File

@ -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 <http://www.gnu.org/licenses/>.
#pragma once
#include "TestContext.h"
class Part1TestContext :
public TestContext
{
public:
Part1TestContext(std::vector<std::string> inputPaths);
virtual long long int getResult(Solver& solver) const override;
};

View File

@ -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 <http://www.gnu.org/licenses/>.
#include "pch.h"
#include "Part2TestContext.h"
Part2TestContext::Part2TestContext(std::vector<std::string> inputPaths)
: TestContext{ inputPaths } {}
long long int Part2TestContext::getResult(Solver& solver) const
{
return solver.getResultPart2();
}

View File

@ -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 <http://www.gnu.org/licenses/>.
#pragma once
#include "TestContext.h"
class Part2TestContext :
public TestContext
{
public:
Part2TestContext(std::vector<std::string> inputPaths);
virtual long long int getResult(Solver& solver) const override;
};

View File

@ -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 <http://www.gnu.org/licenses/>.
#include "pch.h"
#include "TestContext.h"
TestContext::TestContext(std::vector<std::string> inputPaths)
: inputPaths_{ inputPaths } {}
std::vector<std::string> TestContext::getInputPaths() const
{
return inputPaths_;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
#pragma once
#include <string>
#include <vector>
#include <Solver.h>
class TestContext
{
public:
TestContext(std::vector<std::string> inputPaths);
virtual ~TestContext() {};
std::vector<std::string> getInputPaths() const;
virtual long long int getResult(Solver& solver) const = 0;
private:
std::vector<std::string> inputPaths_;
};

View File

@ -16,6 +16,8 @@
#include "pch.h" #include "pch.h"
#include "CppUnitTest.h" #include "CppUnitTest.h"
#include "Part1TestContext.h"
#include "Part2TestContext.h"
#include "../Demo.h" #include "../Demo.h"
#include "../Solver.h" #include "../Solver.h"
#include "../SolverEngine.h" #include "../SolverEngine.h"
@ -29,37 +31,37 @@ namespace UnitTests
public: public:
TEST_METHOD(FullData1) TEST_METHOD(FullData1)
{ {
SolverEngine solverEngine{ getInputPaths() }; runTest(*std::make_unique<Demo>(), 66, part1TestContext_);
std::unique_ptr<Solver> solver{ std::make_unique<Demo>() };
solverEngine.run(*solver);
assertAreEqual(66, solver->getResultPart1());
} }
TEST_METHOD(FullData2) TEST_METHOD(FullData2)
{ {
SolverEngine solverEngine{ getInputPaths() }; runTest(*std::make_unique<Demo>(), 39916800, part2TestContext_);
std::unique_ptr<Solver> solver = std::make_unique<Demo>();
solverEngine.run(*solver);
assertAreEqual(39916800, solver->getResultPart2());
} }
TEST_METHOD(ExampleData1) TEST_METHOD(ExampleData1)
{ {
SolverEngine solverEngine{ getExampleInputPaths() }; runTest(*std::make_unique<Demo>(), 10, part1ExampleTestContext_);
std::unique_ptr<Solver> solver = std::make_unique<Demo>();
solverEngine.run(*solver);
assertAreEqual(10, solver->getResultPart1());
} }
TEST_METHOD(ExampleData2) TEST_METHOD(ExampleData2)
{ {
SolverEngine solverEngine{ getExampleInputPaths() }; runTest(*std::make_unique<Demo>(), 24, part2ExampleTestContext_);
std::unique_ptr<Solver> solver = std::make_unique<Demo>();
solverEngine.run(*solver);
assertAreEqual(24, solver->getResultPart2());
} }
private: 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<std::string> getInputPaths() const std::vector<std::string> getInputPaths() const
{ {
return std::vector<std::string>{ "../../data", "../../../../data" }; return std::vector<std::string>{ "../../data", "../../../../data" };

View File

@ -159,16 +159,22 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Part1TestContext.cpp" />
<ClCompile Include="Part2TestContext.cpp" />
<ClCompile Include="pch.cpp"> <ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile> </ClCompile>
<ClCompile Include="TestContext.cpp" />
<ClCompile Include="UnitTests.cpp" /> <ClCompile Include="UnitTests.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Part1TestContext.h" />
<ClInclude Include="Part2TestContext.h" />
<ClInclude Include="pch.h" /> <ClInclude Include="pch.h" />
<ClInclude Include="TestContext.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\AdventOfCode2024.vcxproj"> <ProjectReference Include="..\AdventOfCode2024.vcxproj">

View File

@ -21,10 +21,28 @@
<ClCompile Include="pch.cpp"> <ClCompile Include="pch.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="TestContext.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Part1TestContext.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Part2TestContext.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="pch.h"> <ClInclude Include="pch.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="TestContext.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Part1TestContext.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Part2TestContext.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>