mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Rework test case names.
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
# Advent of Code 2019
|
# Advent of Code 2019
|
||||||
|
|
||||||
|
[](https://travis-ci.org/bertptrs/adventofcode)
|
||||||
|
|
||||||
This project contains my implementations for Advent of Code 2019. The
|
This project contains my implementations for Advent of Code 2019. The
|
||||||
goal is to create reasonably fast C++ implementations in readable and
|
goal is to create reasonably fast C++ implementations in readable and
|
||||||
ergonomic C++. At the end of the contest, I will probably do a write-
|
ergonomic C++. At the end of the contest, I will probably do a write-
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
#define BOOST_TEST_MODULE solutions_tests
|
#define BOOST_TEST_MODULE solutions_tests
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdio>
|
#include <cctype>
|
||||||
|
#include <charconv>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <boost/filesystem.hpp>
|
#include <boost/filesystem.hpp>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include "implementations.hpp"
|
#include "implementations.hpp"
|
||||||
|
|
||||||
class SolutionsTest : public testing::TestWithParam<std::string> {
|
class SolutionsTest : public testing::TestWithParam<std::string> {
|
||||||
|
public:
|
||||||
|
static std::string nameInstantiatedTest(const testing::TestParamInfo<SolutionsTest::ParamType> ¶mInfo);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string input_data = "";
|
std::string input_data = "";
|
||||||
std::string output_data = "";
|
std::string output_data = "";
|
||||||
@@ -18,23 +22,17 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static void readToString(const std::string &name, std::string &target);
|
static void readToString(const std::string &name, std::string &target);
|
||||||
|
|
||||||
|
static std::tuple<int, bool, std::string> parseInputName(const std::string &name);
|
||||||
};
|
};
|
||||||
|
|
||||||
void SolutionsTest::SetUp() {
|
void SolutionsTest::SetUp() {
|
||||||
const auto input_name = GetParam();
|
const auto input_name = GetParam();
|
||||||
const auto output_name = input_name.substr(0, input_name.length() - 3) + ".out";
|
const auto output_name = input_name.substr(0, input_name.length() - 3) + ".out";
|
||||||
|
|
||||||
const char *base_name = input_name.c_str();
|
int day;
|
||||||
if (const auto last_slash = input_name.rfind('/'); last_slash != std::string::npos) {
|
bool part2;
|
||||||
base_name += last_slash + 1;
|
std::tie(day, part2, std::ignore) = parseInputName(input_name);
|
||||||
}
|
|
||||||
|
|
||||||
int day, part;
|
|
||||||
const auto read_result = std::sscanf(base_name, "%02d-%1d-", &day, &part); // NOLINT(cert-err34-c)
|
|
||||||
// Ensure that we've read the input files.
|
|
||||||
assert(read_result != 0);
|
|
||||||
|
|
||||||
const bool part2 = part == 2;
|
|
||||||
implementation = aoc2019::get_implementation(day, part2);
|
implementation = aoc2019::get_implementation(day, part2);
|
||||||
|
|
||||||
readToString(input_name, input_data);
|
readToString(input_name, input_data);
|
||||||
@@ -48,6 +46,38 @@ void SolutionsTest::readToString(const std::string &name, std::string &target) {
|
|||||||
std::istreambuf_iterator<char>());
|
std::istreambuf_iterator<char>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::tuple<int, bool, std::string> SolutionsTest::parseInputName(const std::string &name) {
|
||||||
|
const char *base_name = name.c_str();
|
||||||
|
if (const auto last_slash = name.rfind('/'); last_slash != std::string::npos) {
|
||||||
|
base_name += last_slash + 1;
|
||||||
|
}
|
||||||
|
int day, part;
|
||||||
|
auto res = std::from_chars(base_name, base_name + 2, day);
|
||||||
|
assert(res.ec == std::errc());
|
||||||
|
res = std::from_chars(base_name + 3, base_name + 4, part);
|
||||||
|
assert(res.ec == std::errc());
|
||||||
|
|
||||||
|
return {day, part == 2, std::string(base_name + 5, std::strchr(base_name, '.'))};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string SolutionsTest::nameInstantiatedTest(const testing::TestParamInfo<SolutionsTest::ParamType> ¶mInfo) {
|
||||||
|
int day;
|
||||||
|
bool part2;
|
||||||
|
std::string sampleName;
|
||||||
|
|
||||||
|
std::tie(day, part2, sampleName) = parseInputName(paramInfo.param);
|
||||||
|
|
||||||
|
std::stringstream nameBuilder;
|
||||||
|
|
||||||
|
nameBuilder << "Day" << day << "Part" << (part2 ? 2 : 1) << "Sample";
|
||||||
|
|
||||||
|
std::copy_if(sampleName.cbegin(), sampleName.cend(), std::ostream_iterator<char>(nameBuilder), [](char c) {
|
||||||
|
return std::isalnum(c);
|
||||||
|
});
|
||||||
|
|
||||||
|
return nameBuilder.str();
|
||||||
|
}
|
||||||
|
|
||||||
TEST_P(SolutionsTest, TestExpectedOutcome) {
|
TEST_P(SolutionsTest, TestExpectedOutcome) {
|
||||||
std::stringstream input_buffer, output_buffer;
|
std::stringstream input_buffer, output_buffer;
|
||||||
|
|
||||||
@@ -75,5 +105,6 @@ static std::vector<std::string> get_samples() {
|
|||||||
return samples;
|
return samples;
|
||||||
}
|
}
|
||||||
|
|
||||||
INSTANTIATE_TEST_CASE_P(InstantiatedSolutionsTest, SolutionsTest,
|
INSTANTIATE_TEST_CASE_P(DaysTest, SolutionsTest,
|
||||||
testing::ValuesIn(get_samples()));
|
testing::ValuesIn(get_samples()),
|
||||||
|
SolutionsTest::nameInstantiatedTest);
|
||||||
|
|||||||
Reference in New Issue
Block a user