mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement generic testing for 2019.
This method reads a specific directory and applies all inputs there to their proper implementation.
This commit is contained in:
@@ -10,7 +10,7 @@ before_script:
|
||||
- cd 2019
|
||||
|
||||
# CMake build
|
||||
script: cmake . && make
|
||||
script: cmake . && make && ./test_solutions
|
||||
|
||||
addons:
|
||||
apt:
|
||||
|
||||
@@ -14,7 +14,9 @@ target_link_libraries(runner AoCSolutions Boost::program_options)
|
||||
add_executable(test_solutions tests/test_solutions.cpp)
|
||||
target_compile_features(test_solutions PUBLIC cxx_std_17)
|
||||
target_link_libraries(test_solutions AoCSolutions Boost::unit_test_framework)
|
||||
target_include_directories(test_solutions PRIVATE ${CMAKE_SOURCE_DIR}/src)
|
||||
target_include_directories(test_solutions PRIVATE "${CMAKE_SOURCE_DIR}/src")
|
||||
|
||||
enable_testing()
|
||||
add_test(test_solutions test_solutions)
|
||||
add_test(NAME test_solutions
|
||||
COMMAND test_solutions
|
||||
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/tests")
|
||||
|
||||
4
2019/tests/samples/01-1-1.in
Normal file
4
2019/tests/samples/01-1-1.in
Normal file
@@ -0,0 +1,4 @@
|
||||
+1
|
||||
-2
|
||||
+3
|
||||
+1
|
||||
1
2019/tests/samples/01-1-1.out
Normal file
1
2019/tests/samples/01-1-1.out
Normal file
@@ -0,0 +1 @@
|
||||
3
|
||||
3
2019/tests/samples/01-1-2.in
Normal file
3
2019/tests/samples/01-1-2.in
Normal file
@@ -0,0 +1,3 @@
|
||||
+1
|
||||
+1
|
||||
+1
|
||||
1
2019/tests/samples/01-1-2.out
Normal file
1
2019/tests/samples/01-1-2.out
Normal file
@@ -0,0 +1 @@
|
||||
3
|
||||
3
2019/tests/samples/01-1-3.in
Normal file
3
2019/tests/samples/01-1-3.in
Normal file
@@ -0,0 +1,3 @@
|
||||
+1
|
||||
+1
|
||||
-2
|
||||
1
2019/tests/samples/01-1-3.out
Normal file
1
2019/tests/samples/01-1-3.out
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
3
2019/tests/samples/01-1-4.in
Normal file
3
2019/tests/samples/01-1-4.in
Normal file
@@ -0,0 +1,3 @@
|
||||
-1
|
||||
-2
|
||||
-3
|
||||
1
2019/tests/samples/01-1-4.out
Normal file
1
2019/tests/samples/01-1-4.out
Normal file
@@ -0,0 +1 @@
|
||||
-6
|
||||
@@ -1,24 +1,53 @@
|
||||
#define BOOST_TEST_MODULE solutions_tests
|
||||
|
||||
#include <filesystem>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/test/data/test_case.hpp>
|
||||
#include "solutions.hpp"
|
||||
|
||||
static std::string run_day1_part1(std::string_view input) {
|
||||
std::stringstream input_stream;
|
||||
input_stream.write(input.data(), input.size());
|
||||
std::vector<std::string> get_samples() {
|
||||
std::vector<std::string> samples;
|
||||
for (auto entry : std::filesystem::directory_iterator("./samples")) {
|
||||
if (entry.path().filename().extension() == ".in") {
|
||||
samples.push_back(entry.path().string());
|
||||
}
|
||||
}
|
||||
|
||||
std::stringstream output_stream;
|
||||
|
||||
aoc2019::day01_part1(input_stream, output_stream);
|
||||
return output_stream.str();
|
||||
return samples;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(sample_day1_part1)
|
||||
{
|
||||
BOOST_TEST(run_day1_part1("+1\n-2\n+3\n+1") == "3\n");
|
||||
BOOST_TEST(run_day1_part1("+1\n+1\n+1") == "3\n");
|
||||
BOOST_TEST(run_day1_part1("+1\n+1\n-2") == "0\n");
|
||||
static std::string read_file(const std::string &file_name) {
|
||||
std::ifstream file(file_name);
|
||||
return std::string(std::istreambuf_iterator<char>(file),
|
||||
std::istreambuf_iterator<char>());
|
||||
}
|
||||
|
||||
static void test_solution_impl(const std::string &input_name) {
|
||||
std::regex name_parser("/(\\d{2})-(1|2).*\\.in$");
|
||||
std::smatch match;
|
||||
// Sanity check, is this a parseable input file?
|
||||
BOOST_TEST(std::regex_search(input_name, match, name_parser));
|
||||
|
||||
const auto output_filename = input_name.substr(0, input_name.length() - 3) + ".out";
|
||||
const int day = std::atoi(match[1].str().c_str());
|
||||
const int part2 = match[2].str() == "2";
|
||||
|
||||
const auto desired_output = read_file(output_filename);
|
||||
const auto implementation = aoc2019::get_implementation(day, part2);
|
||||
|
||||
std::stringstream output_buffer;
|
||||
std::ifstream input(input_name);
|
||||
|
||||
implementation(input, output_buffer);
|
||||
|
||||
BOOST_TEST(desired_output == output_buffer.str());
|
||||
}
|
||||
|
||||
BOOST_DATA_TEST_CASE(test_solution,
|
||||
boost::unit_test::data::make(get_samples()),
|
||||
input_name) {
|
||||
test_solution_impl(input_name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user