From c7a7a98a39fd715cb6b924c23a76487d7f18a043 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sun, 22 Sep 2019 18:46:00 +0200 Subject: [PATCH] Rework test case names. --- 2019/README.md | 2 ++ 2019/tests/test_solutions.cpp | 59 ++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/2019/README.md b/2019/README.md index c8bb59a..dc723dd 100644 --- a/2019/README.md +++ b/2019/README.md @@ -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- diff --git a/2019/tests/test_solutions.cpp b/2019/tests/test_solutions.cpp index 399ff3c..6a9cd6b 100644 --- a/2019/tests/test_solutions.cpp +++ b/2019/tests/test_solutions.cpp @@ -1,13 +1,17 @@ #define BOOST_TEST_MODULE solutions_tests #include -#include +#include +#include #include #include #include #include "implementations.hpp" class SolutionsTest : public testing::TestWithParam { +public: + static std::string nameInstantiatedTest(const testing::TestParamInfo ¶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 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()); } +std::tuple 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 ¶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(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 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);