From baf4317ffaef044bcbff0b4f6000aece3c4aca95 Mon Sep 17 00:00:00 2001 From: ado Date: Sun, 18 Feb 2024 18:58:51 +0100 Subject: [PATCH] Add more unit tests for buffer mode --- test/test_helpers.hpp | 52 +++++++++++++++++++++++++++++++++++++++++++ test/test_parser1.hpp | 50 ----------------------------------------- test/test_parser2.hpp | 22 ++++++++++++------ 3 files changed, 67 insertions(+), 57 deletions(-) diff --git a/test/test_helpers.hpp b/test/test_helpers.hpp index 2509edf..78d9ab9 100644 --- a/test/test_helpers.hpp +++ b/test/test_helpers.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #ifdef CMAKE_GITHUB_CI #include @@ -134,4 +135,55 @@ std::vector> vector_combinations(const std::vector& v, } return ret; } + +std::string make_buffer(const std::string& file_name) { + std::ifstream in{file_name, std::ios::binary}; + std::string tmp; + std::string out; + + auto copy_if_whitespaces = [&] { + std::string matches = "\n\r\t "; + while (std::any_of(matches.begin(), matches.end(), + [&](auto c) { return in.peek() == c; })) { + if (in.peek() == '\r') { + out += "\r\n"; + in.ignore(2); + } else { + out += std::string{static_cast(in.peek())}; + in.ignore(1); + } + } + }; + + out.reserve(sizeof(out) + 1); + + copy_if_whitespaces(); + while (in >> tmp) { + out += tmp; + copy_if_whitespaces(); + } + return out; +} + +template +std::tuple, std::string> make_parser( + const std::string& file_name, const std::string& delim = "") { + if (buffer_mode) { + auto buffer = make_buffer(file_name); + if (delim.empty()) { + return {ss::parser{buffer.data(), buffer.size()}, + std::move(buffer)}; + } else { + return {ss::parser{buffer.data(), buffer.size(), delim}, + std::move(buffer)}; + } + } else { + if (delim.empty()) { + return {ss::parser{file_name}, std::string{}}; + } else { + return {ss::parser{file_name, delim}, std::string{}}; + } + } +} + } /* namespace */ diff --git a/test/test_parser1.hpp b/test/test_parser1.hpp index 32365c0..a0fd9ce 100644 --- a/test/test_parser1.hpp +++ b/test/test_parser1.hpp @@ -105,54 +105,4 @@ static void make_and_write(const std::string& file_name, } } -std::string make_buffer(const std::string& file_name) { - std::ifstream in{file_name, std::ios::binary}; - std::string tmp; - std::string out; - - auto copy_if_whitespaces = [&] { - std::string matches = "\n\r\t "; - while (std::any_of(matches.begin(), matches.end(), - [&](auto c) { return in.peek() == c; })) { - if (in.peek() == '\r') { - out += "\r\n"; - in.ignore(2); - } else { - out += std::string{static_cast(in.peek())}; - in.ignore(1); - } - } - }; - - out.reserve(sizeof(out) + 1); - - copy_if_whitespaces(); - while (in >> tmp) { - out += tmp; - copy_if_whitespaces(); - } - return out; -} - -template -std::tuple, std::string> make_parser( - const std::string& file_name, const std::string& delim = "") { - if (buffer_mode) { - auto buffer = make_buffer(file_name); - if (delim.empty()) { - return {ss::parser{buffer.data(), buffer.size()}, - std::move(buffer)}; - } else { - return {ss::parser{buffer.data(), buffer.size(), delim}, - std::move(buffer)}; - } - } else { - if (delim.empty()) { - return {ss::parser{file_name}, std::string{}}; - } else { - return {ss::parser{file_name, delim}, std::string{}}; - } - } -} - } /* namespace */ diff --git a/test/test_parser2.hpp b/test/test_parser2.hpp index 262cb92..ba6fd1a 100644 --- a/test/test_parser2.hpp +++ b/test/test_parser2.hpp @@ -121,7 +121,7 @@ column make_column(const std::string& input_header, } [[maybe_unused]] void replace_all2(std::string& s, const std::string& old_value, - const std::string& new_value) { + const std::string& new_value) { for (size_t i = 0; i < 999; ++i) { size_t pos = s.find(old_value); if (pos == std::string::npos) { @@ -257,7 +257,8 @@ std::vector generate_csv_data(const std::vector& data, } [[maybe_unused]] void write_to_file(const std::vector& data, - const std::string& delim, const std::string& file_name) { + const std::string& delim, + const std::string& file_name) { std::ofstream out{file_name, std::ios_base::app}; std::string line; for (size_t i = 0; i < data.size(); ++i) { @@ -299,7 +300,7 @@ std::vector generate_csv_data(const std::vector& data, CHECK(V1 == V2); \ } -template +template void test_data_combinations(const std::vector& input_data, const std::string& delim, bool include_header) { using setup = ss::setup; @@ -388,7 +389,7 @@ void test_data_combinations(const std::vector& input_data, } for (const auto& layout : unique_layout_combinations) { - ss::parser p{f.name, delim}; + auto [p, _] = make_parser(f.name, delim); if (include_header && !setup::ignore_header) { std::vector fields; @@ -409,7 +410,7 @@ void test_data_combinations(const std::vector& input_data, REQUIRE(p.valid()); } - auto check_error = [&p] { + auto check_error = [&p = p] { CHECK(p.valid()); if (!p.valid()) { if constexpr (setup::string_error) { @@ -570,8 +571,14 @@ void test_option_combinations() { {columns0, columns1, columns2, columns3, columns4, columns5, columns6, columns7}) { try { - test_data_combinations(columns, delimiter, false); - test_data_combinations(columns, delimiter, true); + test_data_combinations(columns, delimiter, + false); + test_data_combinations(columns, delimiter, + true); + test_data_combinations(columns, delimiter, + false); + test_data_combinations(columns, delimiter, + true); } catch (std::exception& e) { std::cout << typeid(ss::parser).name() << std::endl; FAIL_CHECK(std::string{e.what()}); @@ -616,6 +623,7 @@ void test_option_combinations3() { } /* namespace */ +// Tests split into multiple compilation units #if 0 TEST_CASE("parser test various cases version 2 segment 1") {