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
|
||||
|
||||
[](https://travis-ci.org/bertptrs/adventofcode)
|
||||
|
||||
This project contains my implementations for Advent of Code 2019. The
|
||||
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-
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
#define BOOST_TEST_MODULE solutions_tests
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cctype>
|
||||
#include <charconv>
|
||||
#include <string>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
#include "implementations.hpp"
|
||||
|
||||
class SolutionsTest : public testing::TestWithParam<std::string> {
|
||||
public:
|
||||
static std::string nameInstantiatedTest(const testing::TestParamInfo<SolutionsTest::ParamType> ¶mInfo);
|
||||
|
||||
protected:
|
||||
std::string input_data = "";
|
||||
std::string output_data = "";
|
||||
@@ -18,23 +22,17 @@ protected:
|
||||
|
||||
private:
|
||||
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() {
|
||||
const auto input_name = GetParam();
|
||||
const auto output_name = input_name.substr(0, input_name.length() - 3) + ".out";
|
||||
|
||||
const char *base_name = input_name.c_str();
|
||||
if (const auto last_slash = input_name.rfind('/'); last_slash != std::string::npos) {
|
||||
base_name += last_slash + 1;
|
||||
}
|
||||
|
||||
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;
|
||||
int day;
|
||||
bool part2;
|
||||
std::tie(day, part2, std::ignore) = parseInputName(input_name);
|
||||
implementation = aoc2019::get_implementation(day, part2);
|
||||
|
||||
readToString(input_name, input_data);
|
||||
@@ -48,6 +46,38 @@ void SolutionsTest::readToString(const std::string &name, std::string &target) {
|
||||
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) {
|
||||
std::stringstream input_buffer, output_buffer;
|
||||
|
||||
@@ -75,5 +105,6 @@ static std::vector<std::string> get_samples() {
|
||||
return samples;
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(InstantiatedSolutionsTest, SolutionsTest,
|
||||
testing::ValuesIn(get_samples()));
|
||||
INSTANTIATE_TEST_CASE_P(DaysTest, SolutionsTest,
|
||||
testing::ValuesIn(get_samples()),
|
||||
SolutionsTest::nameInstantiatedTest);
|
||||
|
||||
Reference in New Issue
Block a user