Rework test case names.

This commit is contained in:
2019-09-22 18:46:00 +02:00
parent 99c1522122
commit c7a7a98a39
2 changed files with 47 additions and 14 deletions

View File

@@ -1,5 +1,7 @@
# Advent of Code 2019
[![Build Status](https://travis-ci.org/bertptrs/adventofcode.svg?branch=master)](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-

View File

@@ -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> &paramInfo);
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> &paramInfo) {
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);