From 63a618957bedf7248d629ea7ee2d236669292f46 Mon Sep 17 00:00:00 2001 From: ado Date: Sun, 18 Feb 2024 18:46:26 +0100 Subject: [PATCH] Add more unit tests for buffer mode --- test/test_parser1.hpp | 13 ++++-- test/test_parser1_4.cpp | 100 +++++++++++++++++++++++----------------- 2 files changed, 67 insertions(+), 46 deletions(-) diff --git a/test/test_parser1.hpp b/test/test_parser1.hpp index a68a939..32365c0 100644 --- a/test/test_parser1.hpp +++ b/test/test_parser1.hpp @@ -109,9 +109,8 @@ std::string make_buffer(const std::string& file_name) { std::ifstream in{file_name, std::ios::binary}; std::string tmp; std::string out; - out.reserve(sizeof(out) + 1); - while (in >> tmp) { - out += tmp; + + auto copy_if_whitespaces = [&] { std::string matches = "\n\r\t "; while (std::any_of(matches.begin(), matches.end(), [&](auto c) { return in.peek() == c; })) { @@ -123,6 +122,14 @@ std::string make_buffer(const std::string& file_name) { in.ignore(1); } } + }; + + out.reserve(sizeof(out) + 1); + + copy_if_whitespaces(); + while (in >> tmp) { + out += tmp; + copy_if_whitespaces(); } return out; } diff --git a/test/test_parser1_4.cpp b/test/test_parser1_4.cpp index 1045850..82233c6 100644 --- a/test/test_parser1_4.cpp +++ b/test/test_parser1_4.cpp @@ -7,17 +7,13 @@ template struct has_type> : std::disjunction...> {}; -static inline void check_size(size_t size1, size_t size2) { - CHECK_EQ(size1, size2); -} - -template +template static void test_fields_impl(const std::string file_name, const std::vector& data, const std::vector& fields) { using CaseType = std::tuple; - ss::parser p{file_name, ","}; + auto [p, _] = make_parser(file_name, ","); CHECK_FALSE(p.field_exists("Unknown")); p.use_fields(fields); std::vector i; @@ -26,7 +22,7 @@ static void test_fields_impl(const std::string file_name, i.push_back(a); } - check_size(i.size(), data.size()); + CHECK_EQ(i.size(), data.size()); for (size_t j = 0; j < i.size(); ++j) { if constexpr (has_type::value) { CHECK_EQ(std::get(i[j]), data[j].i); @@ -43,11 +39,16 @@ static void test_fields_impl(const std::string file_name, template static void test_fields(const std::string file_name, const std::vector& data, const std::vector& fields) { - test_fields_impl, Ts...>(file_name, data, fields); - test_fields_impl, Ts...>(file_name, data, - fields); - test_fields_impl, Ts...>(file_name, data, - fields); + test_fields_impl, Ts...>(file_name, data, fields); + test_fields_impl, Ts...>(file_name, data, + fields); + test_fields_impl, Ts...>(file_name, + data, fields); + test_fields_impl, Ts...>(file_name, data, fields); + test_fields_impl, Ts...>(file_name, data, + fields); + test_fields_impl, Ts...>(file_name, + data, fields); } TEST_CASE("parser test various cases with header") { @@ -196,34 +197,37 @@ TEST_CASE("parser test various cases with header") { test_fields(o, d, {Dbl, Int, Str}); } -template +template void test_invalid_fields_impl(const std::vector& lines, const std::vector& fields) { unique_file_name f{"test_parser"}; - std::ofstream out{f.name}; - for (const auto& line : lines) { - out << line << std::endl; + { + std::ofstream out{f.name}; + for (const auto& line : lines) { + out << line << std::endl; + } } - out.close(); { // No fields specified - ss::parser p{f.name, ","}; - auto command = [&] { p.use_fields(); }; + auto [p, _] = make_parser(f.name, ","); + auto command = [&p = p] { p.use_fields(); }; expect_error_on_command(p, command); } { // Unknown field - ss::parser p{f.name, ","}; - auto command = [&] { p.use_fields("Unknown"); }; + auto [p, _] = make_parser(f.name, ","); + auto command = [&p = p] { p.use_fields("Unknown"); }; expect_error_on_command(p, command); } { // Field used multiple times - ss::parser p{f.name, ","}; - auto command = [&] { p.use_fields(fields.at(0), fields.at(0)); }; + auto [p, _] = make_parser(f.name, ","); + auto command = [&p = p, &fields = fields] { + p.use_fields(fields.at(0), fields.at(0)); + }; if (!fields.empty()) { expect_error_on_command(p, command); } @@ -231,8 +235,8 @@ void test_invalid_fields_impl(const std::vector& lines, { // Mapping out of range - ss::parser p{f.name, ","}; - auto command = [&] { + auto [p, _] = make_parser(f.name, ","); + auto command = [&p = p, &fields = fields] { p.use_fields(fields.at(0)); p.template get_next(); }; @@ -243,8 +247,8 @@ void test_invalid_fields_impl(const std::vector& lines, { // Invalid header - ss::parser p{f.name, ","}; - auto command = [&] { p.use_fields(fields); }; + auto [p, _] = make_parser(f.name, ","); + auto command = [&p = p, &fields = fields] { p.use_fields(fields); }; if (!fields.empty()) { // Pass if there are no duplicates, fail otherwise @@ -267,9 +271,12 @@ void test_invalid_fields_impl(const std::vector& lines, template void test_invalid_fields(const std::vector& lines, const std::vector& fields) { - test_invalid_fields_impl(lines, fields); - test_invalid_fields_impl(lines, fields); - test_invalid_fields_impl(lines, fields); + test_invalid_fields_impl(lines, fields); + test_invalid_fields_impl(lines, fields); + test_invalid_fields_impl(lines, fields); + test_invalid_fields_impl(lines, fields); + test_invalid_fields_impl(lines, fields); + test_invalid_fields_impl(lines, fields); } TEST_CASE("parser test invalid header fields usage") { @@ -296,7 +303,7 @@ TEST_CASE("parser test invalid header fields usage") { test_invalid_fields({"Int,String,Int", "1,hi,3"}, {"Int", "String", "Int"}); } -template +template void test_invalid_rows_with_header() { unique_file_name f{"test_parser"}; { @@ -311,7 +318,7 @@ void test_invalid_rows_with_header() { } { - ss::parser p{f.name}; + auto [p, _] = make_parser(f.name); p.use_fields("Int", "String", "Double"); using data = std::tuple; @@ -337,7 +344,7 @@ void test_invalid_rows_with_header() { } { - ss::parser p{f.name}; + auto [p, _] = make_parser(f.name); p.use_fields("Double", "Int"); using data = std::tuple; @@ -361,7 +368,7 @@ void test_invalid_rows_with_header() { } { - ss::parser p{f.name}; + auto [p, _] = make_parser(f.name); p.use_fields("String", "Double"); using data = std::tuple; @@ -389,12 +396,15 @@ void test_invalid_rows_with_header() { } TEST_CASE("parser test invalid rows with header") { - test_invalid_rows_with_header(); - test_invalid_rows_with_header(); - test_invalid_rows_with_header(); + test_invalid_rows_with_header(); + test_invalid_rows_with_header(); + test_invalid_rows_with_header(); + test_invalid_rows_with_header(); + test_invalid_rows_with_header(); + test_invalid_rows_with_header(); } -template +template void test_ignore_empty_impl(const std::vector& data) { unique_file_name f{"test_parser"}; make_and_write(f.name, data); @@ -407,7 +417,8 @@ void test_ignore_empty_impl(const std::vector& data) { } { - ss::parser p{f.name, ","}; + auto [p, _] = + make_parser(f.name, ","); std::vector i; for (const auto& a : p.template iterate()) { @@ -418,7 +429,7 @@ void test_ignore_empty_impl(const std::vector& data) { } { - ss::parser p{f.name, ","}; + auto [p, _] = make_parser(f.name, ","); std::vector i; size_t n = 0; while (!p.eof()) { @@ -441,9 +452,12 @@ void test_ignore_empty_impl(const std::vector& data) { template void test_ignore_empty(const std::vector& data) { - test_ignore_empty_impl(data); - test_ignore_empty_impl(data); - test_ignore_empty_impl(data); + test_ignore_empty_impl(data); + test_ignore_empty_impl(data); + test_ignore_empty_impl(data); + test_ignore_empty_impl(data); + test_ignore_empty_impl(data); + test_ignore_empty_impl(data); } TEST_CASE("parser test various cases with empty lines") {