Compare commits

..

No commits in common. "110ee840ccd1ac53e2caabcd17b905c92a93c634" and "383de57f9a226f2d392c0f9f9c85babc2c4a0646" have entirely different histories.

9 changed files with 439 additions and 476 deletions

View File

@ -102,14 +102,10 @@ public:
} }
size_t line() const { size_t line() const {
return reader_.line_number_ > 0 ? reader_.line_number_ - 1 return reader_.line_number_ > 1 ? reader_.line_number_ - 1
: reader_.line_number_; : reader_.line_number_;
} }
size_t position() const {
return reader_.chars_read_;
}
template <typename T, typename... Ts> template <typename T, typename... Ts>
no_void_validator_tup_t<T, Ts...> get_next() { no_void_validator_tup_t<T, Ts...> get_next() {
std::optional<std::string> error; std::optional<std::string> error;
@ -214,7 +210,7 @@ public:
reader_.next_line_converter_.set_column_mapping(column_mappings, reader_.next_line_converter_.set_column_mapping(column_mappings,
header_.size()); header_.size());
if (line() == 0) { if (line() == 1) {
ignore_next(); ignore_next();
} }
} }
@ -697,7 +693,7 @@ private:
csv_data_buffer_{other.csv_data_buffer_}, csv_data_buffer_{other.csv_data_buffer_},
csv_data_size_{other.csv_data_size_}, csv_data_size_{other.csv_data_size_},
curr_char_{other.curr_char_}, crlf_{other.crlf_}, curr_char_{other.curr_char_}, crlf_{other.crlf_},
line_number_{other.line_number_}, chars_read_{other.chars_read_}, line_number_{other.line_number_},
next_line_size_{other.next_line_size_} { next_line_size_{other.next_line_size_} {
other.buffer_ = nullptr; other.buffer_ = nullptr;
other.next_line_buffer_ = nullptr; other.next_line_buffer_ = nullptr;
@ -722,14 +718,12 @@ private:
curr_char_ = other.curr_char_; curr_char_ = other.curr_char_;
crlf_ = other.crlf_; crlf_ = other.crlf_;
line_number_ = other.line_number_; line_number_ = other.line_number_;
chars_read_ = other.chars_read_;
next_line_size_ = other.next_line_size_; next_line_size_ = other.next_line_size_;
other.buffer_ = nullptr; other.buffer_ = nullptr;
other.next_line_buffer_ = nullptr; other.next_line_buffer_ = nullptr;
other.helper_buffer_ = nullptr; other.helper_buffer_ = nullptr;
other.file_ = nullptr; other.file_ = nullptr;
other.csv_data_buffer_ = nullptr;
} }
return *this; return *this;
@ -809,11 +803,9 @@ private:
next_line_buffer_[0] = '\0'; next_line_buffer_[0] = '\0';
} }
chars_read_ = curr_char_;
if (file_) { if (file_) {
ssize = get_line_file(&next_line_buffer_, ssize = get_line_file(&next_line_buffer_,
&next_line_buffer_size_, file_); &next_line_buffer_size_, file_);
curr_char_ = ftell(file_);
} else { } else {
ssize = get_line_buffer(&next_line_buffer_, ssize = get_line_buffer(&next_line_buffer_,
&next_line_buffer_size_, &next_line_buffer_size_,
@ -1017,7 +1009,6 @@ private:
bool crlf_{false}; bool crlf_{false};
size_t line_number_{0}; size_t line_number_{0};
size_t chars_read_{0};
size_t next_line_size_{0}; size_t next_line_size_{0};
}; };

View File

@ -165,25 +165,25 @@ using get_multiline_t = typename get_multiline<Ts...>::type;
// string_error // string_error
//////////////// ////////////////
class string_error {}; class string_error;
//////////////// ////////////////
// ignore_header // ignore_header
//////////////// ////////////////
class ignore_header {}; class ignore_header;
//////////////// ////////////////
// ignore_empty // ignore_empty
//////////////// ////////////////
class ignore_empty {}; class ignore_empty;
//////////////// ////////////////
// throw_on_error // throw_on_error
//////////////// ////////////////
class throw_on_error {}; class throw_on_error;
//////////////// ////////////////
// setup implementation // setup implementation

83
ssp.hpp
View File

@ -646,7 +646,7 @@ inline ssize_t get_line_file(char** lineptr, size_t* n, FILE* stream) {
} }
#else #else
using ssize_t = intptr_t; using ssize_t = int64_t;
ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) { ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) {
if (lineptr == nullptr || n == nullptr || fp == nullptr) { if (lineptr == nullptr || n == nullptr || fp == nullptr) {
@ -670,15 +670,14 @@ ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) {
(*lineptr)[0] = '\0'; (*lineptr)[0] = '\0';
size_t line_used = 0;
while (fgets(buff, sizeof(buff), fp) != nullptr) { while (fgets(buff, sizeof(buff), fp) != nullptr) {
line_used = strlen(*lineptr); size_t line_used = strlen(*lineptr);
size_t buff_used = strlen(buff); size_t buff_used = strlen(buff);
if (*n <= buff_used + line_used) { if (*n < buff_used + line_used) {
size_t new_n = *n * 2; size_t new_n = *n * 2;
auto new_lineptr = static_cast<char*>(realloc(*lineptr, new_n)); auto new_lineptr = static_cast<char*>(realloc(*lineptr, *n));
if (new_lineptr == nullptr) { if (new_lineptr == nullptr) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
@ -697,7 +696,7 @@ ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) {
} }
} }
return (line_used != 0) ? line_used : -1; return -1;
} }
#endif #endif
@ -867,25 +866,25 @@ using get_multiline_t = typename get_multiline<Ts...>::type;
// string_error // string_error
//////////////// ////////////////
class string_error {}; class string_error;
//////////////// ////////////////
// ignore_header // ignore_header
//////////////// ////////////////
class ignore_header {}; class ignore_header;
//////////////// ////////////////
// ignore_empty // ignore_empty
//////////////// ////////////////
class ignore_empty {}; class ignore_empty;
//////////////// ////////////////
// throw_on_error // throw_on_error
//////////////// ////////////////
class throw_on_error {}; class throw_on_error;
//////////////// ////////////////
// setup implementation // setup implementation
@ -2239,10 +2238,6 @@ public:
: reader_.line_number_; : reader_.line_number_;
} }
size_t position() const {
return reader_.chars_read_;
}
template <typename T, typename... Ts> template <typename T, typename... Ts>
no_void_validator_tup_t<T, Ts...> get_next() { no_void_validator_tup_t<T, Ts...> get_next() {
std::optional<std::string> error; std::optional<std::string> error;
@ -2831,7 +2826,6 @@ private:
csv_data_size_{other.csv_data_size_}, csv_data_size_{other.csv_data_size_},
curr_char_{other.curr_char_}, crlf_{other.crlf_}, curr_char_{other.curr_char_}, crlf_{other.crlf_},
line_number_{other.line_number_}, line_number_{other.line_number_},
chars_read_{other.chars_read_},
next_line_size_{other.next_line_size_} { next_line_size_{other.next_line_size_} {
other.buffer_ = nullptr; other.buffer_ = nullptr;
other.next_line_buffer_ = nullptr; other.next_line_buffer_ = nullptr;
@ -2856,14 +2850,12 @@ private:
curr_char_ = other.curr_char_; curr_char_ = other.curr_char_;
crlf_ = other.crlf_; crlf_ = other.crlf_;
line_number_ = other.line_number_; line_number_ = other.line_number_;
chars_read_ = other.chars_read_;
next_line_size_ = other.next_line_size_; next_line_size_ = other.next_line_size_;
other.buffer_ = nullptr; other.buffer_ = nullptr;
other.next_line_buffer_ = nullptr; other.next_line_buffer_ = nullptr;
other.helper_buffer_ = nullptr; other.helper_buffer_ = nullptr;
other.file_ = nullptr; other.file_ = nullptr;
other.csv_data_buffer_ = nullptr;
} }
return *this; return *this;
@ -2884,52 +2876,50 @@ private:
reader& operator=(const reader& other) = delete; reader& operator=(const reader& other) = delete;
ssize_t get_line_buffer(char** lineptr, size_t* n, ssize_t get_line_buffer(char** lineptr, size_t* n,
const char* const csv_data_buffer, const char* const buffer, size_t csv_data_size,
size_t csv_data_size, size_t& curr_char) { size_t& curr_char) {
if (lineptr == nullptr || n == nullptr || size_t pos;
csv_data_buffer == nullptr) { int c;
errno = EINVAL;
return -1;
}
if (curr_char >= csv_data_size) { if (curr_char >= csv_data_size) {
return -1; return -1;
} }
c = buffer[curr_char++];
if (*lineptr == nullptr || *n < get_line_initial_buffer_size) { if (*lineptr == nullptr) {
auto new_lineptr = static_cast<char*>( *lineptr =
realloc(*lineptr, get_line_initial_buffer_size)); static_cast<char*>(malloc(get_line_initial_buffer_size));
if (new_lineptr == nullptr) { if (*lineptr == nullptr) {
return -1; return -1;
} }
*lineptr = new_lineptr; *n = 128;
*n = get_line_initial_buffer_size;
} }
size_t line_used = 0; pos = 0;
while (curr_char <= csv_data_size) { while (curr_char <= csv_data_size) {
if (line_used + 1 >= *n) { if (pos + 1 >= *n) {
size_t new_n = *n * 2; size_t new_size = *n + (*n >> 2);
if (new_size < get_line_initial_buffer_size) {
char* new_lineptr = new_size = get_line_initial_buffer_size;
static_cast<char*>(realloc(*lineptr, new_n)); }
if (new_lineptr == nullptr) { char* new_ptr = static_cast<char*>(
errno = ENOMEM; realloc(static_cast<void*>(*lineptr), new_size));
if (new_ptr == nullptr) {
return -1; return -1;
} }
*n = new_n; *n = new_size;
*lineptr = new_lineptr; *lineptr = new_ptr;
} }
auto c = csv_data_buffer[curr_char++]; (*lineptr)[pos++] = c;
(*lineptr)[line_used++] = c;
if (c == '\n') { if (c == '\n') {
(*lineptr)[line_used] = '\0'; break;
return line_used;
} }
c = buffer[curr_char++];
} }
return (line_used != 0) ? line_used : -1; (*lineptr)[pos] = '\0';
return pos;
} }
// read next line each time in order to set eof_ // read next line each time in order to set eof_
@ -2943,11 +2933,9 @@ private:
next_line_buffer_[0] = '\0'; next_line_buffer_[0] = '\0';
} }
chars_read_ = curr_char_;
if (file_) { if (file_) {
ssize = get_line_file(&next_line_buffer_, ssize = get_line_file(&next_line_buffer_,
&next_line_buffer_size_, file_); &next_line_buffer_size_, file_);
curr_char_ = ftell(file_);
} else { } else {
ssize = get_line_buffer(&next_line_buffer_, ssize = get_line_buffer(&next_line_buffer_,
&next_line_buffer_size_, &next_line_buffer_size_,
@ -3151,7 +3139,6 @@ private:
bool crlf_{false}; bool crlf_{false};
size_t line_number_{0}; size_t line_number_{0};
size_t chars_read_{0};
size_t next_line_size_{0}; size_t next_line_size_{0};
}; };

View File

@ -1,14 +1,12 @@
#pragma once #pragma once
#include <algorithm>
#include <ctime> #include <ctime>
#include <filesystem> #include <filesystem>
#include <fstream>
#include <iomanip> #include <iomanip>
#include <ss/common.hpp>
#include <ss/setup.hpp>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <algorithm>
#include <fstream>
#ifdef CMAKE_GITHUB_CI #ifdef CMAKE_GITHUB_CI
#include <doctest/doctest.h> #include <doctest/doctest.h>
@ -22,24 +20,6 @@ class parser;
} /* ss */ } /* ss */
namespace { namespace {
struct bool_error {};
template <typename T, typename U = bool_error>
struct config {
using BufferMode = T;
using ErrorMode = U;
constexpr static auto ThrowOnError = std::is_same_v<U, ss::throw_on_error>;
constexpr static auto StringError = std::is_same_v<U, ss::string_error>;
};
#define ParserOptionCombinations \
config<std::true_type>, config<std::true_type, ss::string_error>, \
config<std::true_type, ss::throw_on_error>, config<std::false_type>, \
config<std::false_type, ss::string_error>, \
config<std::false_type, ss::throw_on_error>
struct buffer { struct buffer {
std::string data_; std::string data_;
@ -78,17 +58,13 @@ struct unique_file_name {
unique_file_name(const std::string& test) { unique_file_name(const std::string& test) {
do { do {
name = "ssp_test_" + test + "_" + std::to_string(i++) + name = "random_" + test + "_" + std::to_string(i++) + "_" +
"_" + time_now_rand() + "_file.csv"; time_now_rand() + "_file.csv";
} while (std::filesystem::exists(name)); } while (std::filesystem::exists(name));
} }
~unique_file_name() { ~unique_file_name() {
try {
std::filesystem::remove(name); std::filesystem::remove(name);
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << e.what() << std::endl;
}
} }
}; };
@ -196,32 +172,23 @@ template <typename T>
} }
template <bool buffer_mode, typename... Ts> template <bool buffer_mode, typename... Ts>
std::tuple<ss::parser<Ts...>, std::string> make_parser_impl( [[maybe_unused]] std::tuple<ss::parser<Ts...>, std::string> make_parser(
const std::string& file_name, std::string delim = ss::default_delimiter) { const std::string& file_name, const std::string& delim = "") {
if (buffer_mode) { if (buffer_mode) {
auto buffer = make_buffer(file_name); auto buffer = make_buffer(file_name);
if (delim.empty()) {
return {ss::parser<Ts...>{buffer.data(), buffer.size()},
std::move(buffer)};
} else {
return {ss::parser<Ts...>{buffer.data(), buffer.size(), delim}, return {ss::parser<Ts...>{buffer.data(), buffer.size(), delim},
std::move(buffer)}; std::move(buffer)};
}
} else {
if (delim.empty()) {
return {ss::parser<Ts...>{file_name}, std::string{}};
} else { } else {
return {ss::parser<Ts...>{file_name, delim}, std::string{}}; return {ss::parser<Ts...>{file_name, delim}, std::string{}};
} }
}
} }
template <bool buffer_mode, typename ErrorMode, typename... Ts>
[[maybe_unused]] std::enable_if_t<
!std::is_same_v<ErrorMode, bool_error>,
std::tuple<ss::parser<ErrorMode, Ts...>, std::string>>
make_parser(const std::string& file_name,
std::string delim = ss::default_delimiter) {
return make_parser_impl<buffer_mode, ErrorMode, Ts...>(file_name, delim);
}
template <bool buffer_mode, typename ErrorMode, typename... Ts>
[[maybe_unused]] std::enable_if_t<std::is_same_v<ErrorMode, bool_error>,
std::tuple<ss::parser<Ts...>, std::string>>
make_parser(const std::string& file_name,
std::string delim = ss::default_delimiter) {
return make_parser_impl<buffer_mode, Ts...>(file_name, delim);
}
} /* namespace */ } /* namespace */

View File

@ -1,7 +1,7 @@
#include "test_parser1.hpp" #include "test_parser1.hpp"
TEST_CASE("test file not found") { TEST_CASE("test file not found") {
unique_file_name f{"file_not_found"}; unique_file_name f{"test_parser"};
{ {
ss::parser p{f.name, ","}; ss::parser p{f.name, ","};
@ -11,7 +11,6 @@ TEST_CASE("test file not found") {
{ {
ss::parser<ss::string_error> p{f.name, ","}; ss::parser<ss::string_error> p{f.name, ","};
CHECK_FALSE(p.valid()); CHECK_FALSE(p.valid());
CHECK_FALSE(p.error_msg().empty());
} }
try { try {
@ -31,7 +30,6 @@ TEST_CASE("test null buffer") {
{ {
ss::parser<ss::string_error> p{nullptr, 10, ","}; ss::parser<ss::string_error> p{nullptr, 10, ","};
CHECK_FALSE(p.valid()); CHECK_FALSE(p.valid());
CHECK_FALSE(p.error_msg().empty());
} }
try { try {
@ -42,104 +40,20 @@ TEST_CASE("test null buffer") {
} }
} }
struct Y { template <bool buffer_mode, typename... Ts>
constexpr static auto delim = ","; void test_various_cases() {
std::string s1; unique_file_name f{"test_parser"};
std::string s2;
std::string s3;
std::string to_string() const {
return std::string{}
.append(s1)
.append(delim)
.append(s2)
.append(delim)
.append(s3);
}
auto tied() const {
return std::tie(s1, s2, s3);
}
};
TEST_CASE_TEMPLATE("test position method", T, ParserOptionCombinations) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"position_method"};
std::vector<Y> data = {{"1", "21", "x"}, {"321", "4", "y"},
{"54", "6", "zz"}, {"7", "876", "uuuu"},
{"910", "10", "v"}, {"10", "321", "ww"}};
make_and_write(f.name, data);
auto [p, buff] = make_parser<buffer_mode, ErrorMode>(f.name);
auto data_at = [&buff = buff, &f = f](auto n) {
if (!buff.empty()) {
return buff[n];
} else {
auto file = fopen(f.name.c_str(), "r");
fseek(file, n, SEEK_SET);
return static_cast<char>(fgetc(file));
}
};
while (!p.eof()) {
auto curr_char = p.position();
const auto& [s1, s2, s3] =
p.template get_next<std::string, std::string, std::string>();
auto s = s1 + "," + s2 + "," + s3;
for (size_t i = 0; i < s1.size(); ++i) {
CHECK_EQ(data_at(curr_char + i), s[i]);
}
auto last_char = data_at(curr_char + s.size());
CHECK((last_char == '\n' || last_char == '\r'));
}
}
TEST_CASE_TEMPLATE("test line method", T, ParserOptionCombinations) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"line_method"};
std::vector<Y> data = {{"1", "21", "x"}, {"321", "4", "y"},
{"54", "6", "zz"}, {"7", "876", "uuuu"},
{"910", "10", "v"}, {"10", "321", "ww"}};
make_and_write(f.name, data);
auto [p, buff] = make_parser<buffer_mode, ErrorMode>(f.name);
auto expected_line = 0;
CHECK_EQ(p.line(), expected_line);
while (!p.eof()) {
auto _ = p.template get_next<std::string, std::string, std::string>();
++expected_line;
CHECK_EQ(p.line(), expected_line);
}
CHECK_EQ(p.line(), data.size());
}
TEST_CASE_TEMPLATE("parser test various valid cases", T,
ParserOptionCombinations) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"various_valid_cases"};
std::vector<X> data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"}, std::vector<X> data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"},
{7, 8, "u"}, {9, 10, "v"}, {11, 12, "w"}}; {7, 8, "u"}, {9, 10, "v"}, {11, 12, "w"}};
make_and_write(f.name, data); make_and_write(f.name, data);
auto csv_data_buffer = make_buffer(f.name); auto csv_data_buffer = make_buffer(f.name);
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
ss::parser p0{std::move(p)}; ss::parser p0{std::move(p)};
p = std::move(p0); p = std::move(p0);
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
auto move_rotate = [&p = p, &p0 = p0] { auto move_rotate = [&p = p, &p0 = p0] {
@ -163,13 +77,13 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
auto [p3, ___] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p3, ___] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i3; std::vector<X> i3;
std::vector<X> expected = {std::begin(data) + 1, std::end(data)}; std::vector<X> expected = {std::begin(data) + 1, std::end(data)};
@ -198,9 +112,9 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
while (!p.eof()) { while (!p.eof()) {
@ -217,7 +131,7 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
for (auto&& a : for (auto&& a :
@ -229,10 +143,10 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
using tup = std::tuple<int, double, std::string>; using tup = std::tuple<int, double, std::string>;
@ -250,7 +164,7 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
using tup = std::tuple<int, double, std::string>; using tup = std::tuple<int, double, std::string>;
@ -262,7 +176,7 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
@ -273,7 +187,7 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
for (auto&& a : p.template iterate<X>()) { for (auto&& a : p.template iterate<X>()) {
@ -285,10 +199,10 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
{ {
constexpr int excluded = 3; constexpr int excluded = 3;
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
while (!p.eof()) { while (!p.eof()) {
@ -303,7 +217,7 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
}; };
} }
if (!T::ThrowOnError) { if (!ss::setup<Ts...>::throw_on_error) {
for (auto&& a : p2.template iterate_object<X, ss::ax<int, excluded>, for (auto&& a : p2.template iterate_object<X, ss::ax<int, excluded>,
double, std::string>()) { double, std::string>()) {
if (p2.valid()) { if (p2.valid()) {
@ -323,16 +237,16 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
[&](const X& x) { return x.i != excluded; }); [&](const X& x) { return x.i != excluded; });
CHECK_EQ(i, expected); CHECK_EQ(i, expected);
if (!T::ThrowOnError) { if (!ss::setup<Ts...>::throw_on_error) {
CHECK_EQ(i2, expected); CHECK_EQ(i2, expected);
} }
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
while (!p.eof()) { while (!p.eof()) {
@ -347,7 +261,7 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
} }
} }
if (!T::ThrowOnError) { if (!ss::setup<Ts...>::throw_on_error) {
for (auto&& a : p2.template iterate_object<X, ss::nx<int, 3>, for (auto&& a : p2.template iterate_object<X, ss::nx<int, 3>,
double, std::string>()) { double, std::string>()) {
if (p2.valid()) { if (p2.valid()) {
@ -358,21 +272,21 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
std::vector<X> expected = {{3, 4, "y"}}; std::vector<X> expected = {{3, 4, "y"}};
CHECK_EQ(i, expected); CHECK_EQ(i, expected);
if (!T::ThrowOnError) { if (!ss::setup<Ts...>::throw_on_error) {
CHECK_EQ(i2, expected); CHECK_EQ(i2, expected);
} }
} }
{ {
unique_file_name empty_f{"various_valid_cases"}; unique_file_name empty_f{"test_parser"};
std::vector<X> empty_data = {}; std::vector<X> empty_data = {};
make_and_write(empty_f.name, empty_data); make_and_write(empty_f.name, empty_data);
auto [p, _] = make_parser<buffer_mode, ErrorMode>(empty_f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(empty_f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(empty_f.name, ","); auto [p2, __] = make_parser<buffer_mode, Ts...>(empty_f.name, ",");
std::vector<X> i2; std::vector<X> i2;
while (!p.eof()) { while (!p.eof()) {
@ -388,6 +302,15 @@ TEST_CASE_TEMPLATE("parser test various valid cases", T,
} }
} }
TEST_CASE("parser test various cases") {
test_various_cases<false>();
test_various_cases<false, ss::string_error>();
test_various_cases<false, ss::throw_on_error>();
test_various_cases<true>();
test_various_cases<true, ss::string_error>();
test_various_cases<true, ss::throw_on_error>();
}
using test_tuple = std::tuple<double, char, double>; using test_tuple = std::tuple<double, char, double>;
struct test_struct { struct test_struct {
int i; int i;
@ -401,10 +324,9 @@ struct test_struct {
static inline void expect_test_struct(const test_struct&) { static inline void expect_test_struct(const test_struct&) {
} }
TEST_CASE_TEMPLATE("parser test composite conversion", BufferMode, template <bool buffer_mode, typename... Ts>
std::true_type, std::false_type) { void test_composite_conversion() {
constexpr auto buffer_mode = BufferMode::value; unique_file_name f{"test_parser"};
unique_file_name f{"composite_conversion"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
for (auto& i : for (auto& i :
@ -414,7 +336,7 @@ TEST_CASE_TEMPLATE("parser test composite conversion", BufferMode,
} }
} }
auto [p, _] = make_parser<buffer_mode, ss::string_error>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto fail = [] { FAIL(""); }; auto fail = [] { FAIL(""); };
auto expect_error = [](auto error) { CHECK(!error.empty()); }; auto expect_error = [](auto error) { CHECK(!error.empty()); };
auto ignore_error = [] {}; auto ignore_error = [] {};
@ -624,12 +546,18 @@ TEST_CASE_TEMPLATE("parser test composite conversion", BufferMode,
CHECK(p.eof()); CHECK(p.eof());
} }
template <bool buffer_mode, typename... Ts> // various scenarios
TEST_CASE("parser test composite conversion") {
test_composite_conversion<false, ss::string_error>();
test_composite_conversion<true, ss::string_error>();
}
template <bool buffer_mode>
void test_no_new_line_at_eof_impl(const std::vector<X>& data) { void test_no_new_line_at_eof_impl(const std::vector<X>& data) {
unique_file_name f{"no_new_line_at_eof"}; unique_file_name f{"test_parser"};
make_and_write(f.name, data, {}, false); make_and_write(f.name, data, {}, false);
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name); auto [p, _] = make_parser<buffer_mode>(f.name);
std::vector<X> parsed_data; std::vector<X> parsed_data;
for (const auto& el : p.template iterate<X>()) { for (const auto& el : p.template iterate<X>()) {
@ -639,36 +567,32 @@ void test_no_new_line_at_eof_impl(const std::vector<X>& data) {
CHECK_EQ(data, parsed_data); CHECK_EQ(data, parsed_data);
} }
template <bool buffer_mode, typename... Ts> template <bool buffer_mode>
void test_no_new_line_at_eof() { void test_no_new_line_at_eof() {
test_no_new_line_at_eof_impl<buffer_mode, Ts...>({}); test_no_new_line_at_eof_impl<buffer_mode>({});
test_no_new_line_at_eof_impl<buffer_mode, Ts...>({{1, 2, "X"}}); test_no_new_line_at_eof_impl<buffer_mode>({{1, 2, "X"}});
test_no_new_line_at_eof_impl<buffer_mode, Ts...>({{1, 2, "X"}, {}}); test_no_new_line_at_eof_impl<buffer_mode>({{1, 2, "X"}, {}});
test_no_new_line_at_eof_impl<buffer_mode, Ts...>( test_no_new_line_at_eof_impl<buffer_mode>({{1, 2, "X"}, {3, 4, "YY"}});
{{1, 2, "X"}, {3, 4, "YY"}}); test_no_new_line_at_eof_impl<buffer_mode>({{1, 2, "X"}, {3, 4, "YY"}, {}});
test_no_new_line_at_eof_impl<buffer_mode, Ts...>( test_no_new_line_at_eof_impl<buffer_mode>(
{{1, 2, "X"}, {3, 4, "YY"}, {}});
test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
{{1, 2, "X"}, {3, 4, "YY"}, {5, 6, "ZZZ"}, {7, 8, "UUU"}}); {{1, 2, "X"}, {3, 4, "YY"}, {5, 6, "ZZZ"}, {7, 8, "UUU"}});
for (size_t i = 0; i < 2 * ss::get_line_initial_buffer_size; ++i) { for (size_t i = 0; i < 2 * ss::get_line_initial_buffer_size; ++i) {
test_no_new_line_at_eof_impl<buffer_mode, Ts...>( test_no_new_line_at_eof_impl<buffer_mode>(
{{1, 2, std::string(i, 'X')}}); {{1, 2, std::string(i, 'X')}});
for (size_t j = 0; j < 2 * ss::get_line_initial_buffer_size; j += 13) { for (size_t j = 0; j < 2 * ss::get_line_initial_buffer_size; j += 13) {
test_no_new_line_at_eof_impl<buffer_mode, Ts...>( test_no_new_line_at_eof_impl<buffer_mode>(
{{1, 2, std::string(i, 'X')}, {3, 4, std::string(j, 'Y')}}); {{1, 2, std::string(i, 'X')}, {3, 4, std::string(j, 'Y')}});
test_no_new_line_at_eof_impl<buffer_mode, Ts...>( test_no_new_line_at_eof_impl<buffer_mode>(
{{1, 2, std::string(j, 'X')}, {3, 4, std::string(i, 'Y')}}); {{1, 2, std::string(j, 'X')}, {3, 4, std::string(i, 'Y')}});
} }
} }
} }
TEST_CASE_TEMPLATE("test no new line at end of data", T, TEST_CASE("test no new line at end of data") {
ParserOptionCombinations) { test_no_new_line_at_eof<false>();
constexpr auto buffer_mode = T::BufferMode::value; test_no_new_line_at_eof<true>();
using ErrorMode = typename T::ErrorMode;
test_no_new_line_at_eof<buffer_mode, ErrorMode>();
} }

View File

@ -41,16 +41,11 @@ struct xyz {
} }
}; };
TEST_CASE_TEMPLATE("test moving of parsed composite values", T, template <bool buffer_mode, typename... Ts>
config<std::true_type>, config<std::false_type>, void test_moving_of_parsed_composite_values() {
config<std::true_type, ss::string_error>,
config<std::false_type, ss::string_error>) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
// to compile is enough // to compile is enough
return; return;
auto [p, _] = make_parser<buffer_mode, ErrorMode>("", ""); auto [p, _] = make_parser<buffer_mode, Ts...>("", "");
p.template try_next<my_string, my_string, my_string>() p.template try_next<my_string, my_string, my_string>()
.template or_else<my_string, my_string, my_string, my_string>( .template or_else<my_string, my_string, my_string, my_string>(
[](auto&&) {}) [](auto&&) {})
@ -61,42 +56,71 @@ TEST_CASE_TEMPLATE("test moving of parsed composite values", T,
[](auto&, auto&, auto&) {}); [](auto&, auto&, auto&) {});
} }
TEST_CASE_TEMPLATE("parser test string error mode", BufferMode, std::true_type, TEST_CASE("parser test the moving of parsed composite values") {
std::false_type) { test_moving_of_parsed_composite_values<false>();
unique_file_name f{"string_error"}; test_moving_of_parsed_composite_values<false, ss::string_error>();
{ test_moving_of_parsed_composite_values<true>();
std::ofstream out{f.name}; test_moving_of_parsed_composite_values<true, ss::string_error>();
out << "junk" << std::endl;
out << "junk" << std::endl;
}
auto [p, _] = make_parser<BufferMode::value, ss::string_error>(f.name, ",");
REQUIRE_FALSE(p.eof());
p.template get_next<int>();
CHECK_FALSE(p.valid());
CHECK_FALSE(p.error_msg().empty());
} }
TEST_CASE_TEMPLATE("parser throw on error mode", BufferMode, std::true_type, TEST_CASE("parser test error mode") {
std::false_type) { unique_file_name f{"test_parser"};
unique_file_name f{"throw_on_error"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
out << "junk" << std::endl; out << "junk" << std::endl;
out << "junk" << std::endl; out << "junk" << std::endl;
} }
auto [p, _] = {
make_parser<BufferMode::value, ss::throw_on_error>(f.name, ","); auto [p, _] = make_parser<false, ss::string_error>(f.name, ",");
REQUIRE_FALSE(p.eof());
p.get_next<int>();
CHECK_FALSE(p.valid());
CHECK_FALSE(p.error_msg().empty());
}
{
auto [p, _] = make_parser<true, ss::string_error>(f.name, ",");
REQUIRE_FALSE(p.eof());
p.get_next<int>();
CHECK_FALSE(p.valid());
CHECK_FALSE(p.error_msg().empty());
}
}
TEST_CASE("parser throw on error mode") {
unique_file_name f{"test_parser"};
{
std::ofstream out{f.name};
out << "junk" << std::endl;
out << "junk" << std::endl;
}
{
auto [p, _] = make_parser<false, ss::throw_on_error>(f.name, ",");
REQUIRE_FALSE(p.eof()); REQUIRE_FALSE(p.eof());
try { try {
p.template get_next<int>(); p.get_next<int>();
FAIL("Expected exception..."); FAIL("Expected exception...");
} catch (const std::exception& e) { } catch (const std::exception& e) {
CHECK_FALSE(std::string{e.what()}.empty()); CHECK_FALSE(std::string{e.what()}.empty());
} }
}
{
auto [p, _] = make_parser<true, ss::throw_on_error>(f.name, ",");
REQUIRE_FALSE(p.eof());
try {
p.get_next<int>();
FAIL("Expected exception...");
} catch (const std::exception& e) {
CHECK_FALSE(std::string{e.what()}.empty());
}
}
} }
static inline std::string no_quote(const std::string& s) { static inline std::string no_quote(const std::string& s) {
@ -106,11 +130,9 @@ static inline std::string no_quote(const std::string& s) {
return s; return s;
} }
TEST_CASE_TEMPLATE("test quote multiline", T, ParserOptionCombinations) { template <bool buffer_mode, typename... Ts>
constexpr auto buffer_mode = T::BufferMode::value; void test_quote_multiline() {
using ErrorMode = typename T::ErrorMode; unique_file_name f{"test_parser"};
unique_file_name f{"quote_multiline"};
std::vector<X> data = {{1, 2, "\"x\r\nx\nx\""}, std::vector<X> data = {{1, 2, "\"x\r\nx\nx\""},
{3, 4, "\"y\ny\r\ny\""}, {3, 4, "\"y\ny\r\ny\""},
{5, 6, "\"z\nz\""}, {5, 6, "\"z\nz\""},
@ -129,8 +151,9 @@ TEST_CASE_TEMPLATE("test quote multiline", T, ParserOptionCombinations) {
} }
} }
auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline, auto [p, _] =
ss::quote<'"'>>(f.name, ","); make_parser<buffer_mode, ss::multiline, ss::quote<'"'>, Ts...>(f.name,
",");
std::vector<X> i; std::vector<X> i;
@ -145,7 +168,7 @@ TEST_CASE_TEMPLATE("test quote multiline", T, ParserOptionCombinations) {
CHECK_EQ(i, data); CHECK_EQ(i, data);
auto [p_no_multiline, __] = auto [p_no_multiline, __] =
make_parser<buffer_mode, ErrorMode, ss::quote<'"'>>(f.name, ","); make_parser<buffer_mode, ss::quote<'"'>, Ts...>(f.name, ",");
while (!p.eof()) { while (!p.eof()) {
auto command = [&p_no_multiline = p_no_multiline] { auto command = [&p_no_multiline = p_no_multiline] {
p_no_multiline.template get_next<int, double, std::string>(); p_no_multiline.template get_next<int, double, std::string>();
@ -154,16 +177,23 @@ TEST_CASE_TEMPLATE("test quote multiline", T, ParserOptionCombinations) {
} }
} }
TEST_CASE("parser test csv on multiple lines with quotes") {
test_quote_multiline<false>();
test_quote_multiline<false, ss::string_error>();
test_quote_multiline<false, ss::throw_on_error>();
test_quote_multiline<true>();
test_quote_multiline<true, ss::string_error>();
test_quote_multiline<true, ss::throw_on_error>();
}
static inline std::string no_escape(std::string& s) { static inline std::string no_escape(std::string& s) {
s.erase(std::remove(begin(s), end(s), '\\'), end(s)); s.erase(std::remove(begin(s), end(s), '\\'), end(s));
return s; return s;
} }
TEST_CASE_TEMPLATE("test escape multiline", T, ParserOptionCombinations) { template <bool buffer_mode, typename... Ts>
constexpr auto buffer_mode = T::BufferMode::value; void test_escape_multiline() {
using ErrorMode = typename T::ErrorMode; unique_file_name f{"test_parser"};
unique_file_name f{"escape_multiline"};
std::vector<X> data = {{1, 2, "x\\\nx\\\r\nx"}, std::vector<X> data = {{1, 2, "x\\\nx\\\r\nx"},
{5, 6, "z\\\nz\\\nz"}, {5, 6, "z\\\nz\\\nz"},
{7, 8, "u"}, {7, 8, "u"},
@ -182,8 +212,9 @@ TEST_CASE_TEMPLATE("test escape multiline", T, ParserOptionCombinations) {
} }
} }
auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline, auto [p, _] =
ss::escape<'\\'>>(f.name, ","); make_parser<buffer_mode, ss::multiline, ss::escape<'\\'>, Ts...>(f.name,
",");
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
@ -197,7 +228,7 @@ TEST_CASE_TEMPLATE("test escape multiline", T, ParserOptionCombinations) {
CHECK_EQ(i, data); CHECK_EQ(i, data);
auto [p_no_multiline, __] = auto [p_no_multiline, __] =
make_parser<buffer_mode, ErrorMode, ss::escape<'\\'>>(f.name, ","); make_parser<buffer_mode, ss::escape<'\\'>, Ts...>(f.name, ",");
while (!p.eof()) { while (!p.eof()) {
auto command = [&p_no_multiline = p_no_multiline] { auto command = [&p_no_multiline = p_no_multiline] {
auto a = auto a =
@ -207,11 +238,18 @@ TEST_CASE_TEMPLATE("test escape multiline", T, ParserOptionCombinations) {
} }
} }
TEST_CASE_TEMPLATE("test quote escape multiline", T, ParserOptionCombinations) { TEST_CASE("parser test csv on multiple lines with escapes") {
constexpr auto buffer_mode = T::BufferMode::value; test_escape_multiline<false>();
using ErrorMode = typename T::ErrorMode; test_escape_multiline<false, ss::string_error>();
test_escape_multiline<false, ss::throw_on_error>();
test_escape_multiline<true>();
test_escape_multiline<true, ss::string_error>();
test_escape_multiline<true, ss::throw_on_error>();
}
unique_file_name f{"quote_escape_multiline"}; template <bool buffer_mode, typename... Ts>
void test_quote_escape_multiline() {
unique_file_name f{"test_parser"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
out << "1,2,\"just\\\n\nstrings\"" << std::endl; out << "1,2,\"just\\\n\nstrings\"" << std::endl;
@ -228,8 +266,8 @@ TEST_CASE_TEMPLATE("test quote escape multiline", T, ParserOptionCombinations) {
size_t bad_lines = 1; size_t bad_lines = 1;
auto num_errors = 0; auto num_errors = 0;
auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline, auto [p, _] = make_parser<buffer_mode, ss::multiline, ss::escape<'\\'>,
ss::escape<'\\'>, ss::quote<'"'>>(f.name); ss::quote<'"'>, Ts...>(f.name);
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
@ -260,3 +298,12 @@ TEST_CASE_TEMPLATE("test quote escape multiline", T, ParserOptionCombinations) {
} }
CHECK_EQ(i, data); CHECK_EQ(i, data);
} }
TEST_CASE("parser test csv on multiple lines with quotes and escapes") {
test_quote_escape_multiline<false>();
test_quote_escape_multiline<false, ss::string_error>();
test_quote_escape_multiline<false, ss::throw_on_error>();
test_quote_escape_multiline<true>();
test_quote_escape_multiline<true, ss::string_error>();
test_quote_escape_multiline<true, ss::throw_on_error>();
}

View File

@ -1,10 +1,8 @@
#include "test_parser1.hpp" #include "test_parser1.hpp"
TEST_CASE_TEMPLATE("test multiline restricted", T, ParserOptionCombinations) { template <bool buffer_mode, typename... Ts>
constexpr auto buffer_mode = T::BufferMode::value; void test_multiline_restricted() {
using ErrorMode = typename T::ErrorMode; unique_file_name f{"test_parser"};
unique_file_name f{"multiline_restricted"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
out << "1,2,\"just\n\nstrings\"" << std::endl; out << "1,2,\"just\n\nstrings\"" << std::endl;
@ -26,8 +24,8 @@ TEST_CASE_TEMPLATE("test multiline restricted", T, ParserOptionCombinations) {
auto num_errors = 0; auto num_errors = 0;
auto [p, _] = auto [p, _] =
make_parser<buffer_mode, ErrorMode, ss::multiline_restricted<2>, make_parser<buffer_mode, ss::multiline_restricted<2>, ss::quote<'"'>,
ss::quote<'"'>, ss::escape<'\\'>>(f.name, ","); ss::escape<'\\'>, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
@ -65,20 +63,26 @@ TEST_CASE_TEMPLATE("test multiline restricted", T, ParserOptionCombinations) {
CHECK_EQ(i, data); CHECK_EQ(i, data);
} }
template <typename T, typename... Ts> TEST_CASE("parser test multiline restricted") {
void test_unterminated_line(const std::vector<std::string>& lines, test_multiline_restricted<false>();
size_t bad_line) { test_multiline_restricted<false, ss::string_error>();
constexpr auto buffer_mode = T::BufferMode::value; test_multiline_restricted<false, ss::throw_on_error>();
using ErrorMode = typename T::ErrorMode; test_multiline_restricted<true>();
test_multiline_restricted<true, ss::string_error>();
test_multiline_restricted<true, ss::throw_on_error>();
}
unique_file_name f{"unterminated_line"}; template <bool buffer_mode, typename... Ts>
void test_unterminated_line_impl(const std::vector<std::string>& lines,
size_t bad_line) {
unique_file_name f{"test_parser"};
std::ofstream out{f.name}; std::ofstream out{f.name};
for (const auto& line : lines) { for (const auto& line : lines) {
out << line << std::endl; out << line << std::endl;
} }
out.close(); out.close();
auto [p, _] = make_parser<buffer_mode, ErrorMode, Ts...>(f.name); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name);
size_t line = 0; size_t line = 0;
while (!p.eof()) { while (!p.eof()) {
auto command = [&p = p] { auto command = [&p = p] {
@ -96,8 +100,21 @@ void test_unterminated_line(const std::vector<std::string>& lines,
} }
} }
TEST_CASE_TEMPLATE("parser test csv on multiline with errors", T, template <typename... Ts>
ParserOptionCombinations) { void test_unterminated_line(const std::vector<std::string>& lines,
size_t bad_line) {
test_unterminated_line_impl<false, Ts...>(lines, bad_line);
test_unterminated_line_impl<false, Ts..., ss::string_error>(lines,
bad_line);
test_unterminated_line_impl<false, Ts..., ss::throw_on_error>(lines,
bad_line);
test_unterminated_line_impl<true, Ts...>(lines, bad_line);
test_unterminated_line_impl<true, Ts..., ss::string_error>(lines, bad_line);
test_unterminated_line_impl<true, Ts..., ss::throw_on_error>(lines,
bad_line);
}
TEST_CASE("parser test csv on multiline with errors") {
using multiline = ss::multiline_restricted<3>; using multiline = ss::multiline_restricted<3>;
using escape = ss::escape<'\\'>; using escape = ss::escape<'\\'>;
using quote = ss::quote<'"'>; using quote = ss::quote<'"'>;
@ -105,209 +122,209 @@ TEST_CASE_TEMPLATE("parser test csv on multiline with errors", T,
// unterminated escape // unterminated escape
{ {
const std::vector<std::string> lines{"1,2,just\\"}; const std::vector<std::string> lines{"1,2,just\\"};
test_unterminated_line<T, multiline, escape>(lines, 0); test_unterminated_line<multiline, escape>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0); test_unterminated_line<multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"1,2,just\\", "9,8,second"}; const std::vector<std::string> lines{"1,2,just\\", "9,8,second"};
test_unterminated_line<T, multiline, escape>(lines, 0); test_unterminated_line<multiline, escape>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0); test_unterminated_line<multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,just\\"}; const std::vector<std::string> lines{"9,8,first", "1,2,just\\"};
test_unterminated_line<T, multiline, escape>(lines, 1); test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,just\\", const std::vector<std::string> lines{"9,8,first", "1,2,just\\",
"3,4,third"}; "3,4,third"};
test_unterminated_line<T, multiline, escape>(lines, 1); test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", const std::vector<std::string> lines{"9,8,first",
"1,2,just\\\nstrings\\", "1,2,just\\\nstrings\\",
"3,4,th\\\nird"}; "3,4,th\\\nird"};
test_unterminated_line<T, multiline, escape>(lines, 1); test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "3,4,second", const std::vector<std::string> lines{"9,8,first", "3,4,second",
"1,2,just\\"}; "1,2,just\\"};
test_unterminated_line<T, multiline, escape>(lines, 2); test_unterminated_line<multiline, escape>(lines, 2);
test_unterminated_line<T, multiline, escape, quote>(lines, 2); test_unterminated_line<multiline, escape, quote>(lines, 2);
} }
{ {
const std::vector<std::string> lines{"9,8,\\first", "3,4,second", const std::vector<std::string> lines{"9,8,\\first", "3,4,second",
"1,2,jus\\t\\"}; "1,2,jus\\t\\"};
test_unterminated_line<T, multiline, escape>(lines, 2); test_unterminated_line<multiline, escape>(lines, 2);
test_unterminated_line<T, multiline, escape, quote>(lines, 2); test_unterminated_line<multiline, escape, quote>(lines, 2);
} }
// unterminated quote // unterminated quote
{ {
const std::vector<std::string> lines{"1,2,\"just"}; const std::vector<std::string> lines{"1,2,\"just"};
test_unterminated_line<T, multiline, escape, quote>(lines, 0); test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, quote>(lines, 0); test_unterminated_line<multiline, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"1,2,\"just", "9,8,second"}; const std::vector<std::string> lines{"1,2,\"just", "9,8,second"};
test_unterminated_line<T, multiline, escape, quote>(lines, 0); test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, quote>(lines, 0); test_unterminated_line<multiline, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,\"just"}; const std::vector<std::string> lines{"9,8,first", "1,2,\"just"};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, quote>(lines, 1); test_unterminated_line<multiline, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,\"just", const std::vector<std::string> lines{"9,8,first", "1,2,\"just",
"3,4,th\\,ird"}; "3,4,th\\,ird"};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, quote>(lines, 1); test_unterminated_line<multiline, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "3,4,second", const std::vector<std::string> lines{"9,8,first", "3,4,second",
"1,2,\"just"}; "1,2,\"just"};
test_unterminated_line<T, multiline, escape, quote>(lines, 2); test_unterminated_line<multiline, escape, quote>(lines, 2);
test_unterminated_line<T, multiline, quote>(lines, 2); test_unterminated_line<multiline, quote>(lines, 2);
} }
{ {
const std::vector<std::string> lines{"9,8,\"first\"", const std::vector<std::string> lines{"9,8,\"first\"",
"\"3\",4,\"sec,ond\"", "\"3\",4,\"sec,ond\"",
"1,2,\"ju\"\"st"}; "1,2,\"ju\"\"st"};
test_unterminated_line<T, multiline, escape, quote>(lines, 2); test_unterminated_line<multiline, escape, quote>(lines, 2);
test_unterminated_line<T, multiline, quote>(lines, 2); test_unterminated_line<multiline, quote>(lines, 2);
} }
// unterminated quote and escape // unterminated quote and escape
{ {
const std::vector<std::string> lines{"1,2,\"just\\"}; const std::vector<std::string> lines{"1,2,\"just\\"};
test_unterminated_line<T, multiline, escape, quote>(lines, 0); test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0); test_unterminated_line<multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"1,2,\"just\\\n\\"}; const std::vector<std::string> lines{"1,2,\"just\\\n\\"};
test_unterminated_line<T, multiline, escape, quote>(lines, 0); test_unterminated_line<multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"1,2,\"just\n\\"}; const std::vector<std::string> lines{"1,2,\"just\n\\"};
test_unterminated_line<T, multiline, escape, quote>(lines, 0); test_unterminated_line<multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\"}; const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\"};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\", const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\",
"4,3,thrid"}; "4,3,thrid"};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,f\\\nirst", "1,2,\"just\n\\", const std::vector<std::string> lines{"9,8,f\\\nirst", "1,2,\"just\n\\",
"4,3,thrid"}; "4,3,thrid"};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,\"f\ni\nrst\"", const std::vector<std::string> lines{"9,8,\"f\ni\nrst\"",
"1,2,\"just\n\\", "4,3,thrid"}; "1,2,\"just\n\\", "4,3,thrid"};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
// multiline limmit reached escape // multiline limmit reached escape
{ {
const std::vector<std::string> lines{"1,2,\\\n\\\n\\\n\\\njust"}; const std::vector<std::string> lines{"1,2,\\\n\\\n\\\n\\\njust"};
test_unterminated_line<T, multiline, escape>(lines, 0); test_unterminated_line<multiline, escape>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0); test_unterminated_line<multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", const std::vector<std::string> lines{"9,8,first",
"1,2,\\\n\\\n\\\n\\\njust"}; "1,2,\\\n\\\n\\\n\\\njust"};
test_unterminated_line<T, multiline, escape>(lines, 1); test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,fi\\\nrs\\\nt", const std::vector<std::string> lines{"9,8,fi\\\nrs\\\nt",
"1,2,\\\n\\\n\\\n\\\njust"}; "1,2,\\\n\\\n\\\n\\\njust"};
test_unterminated_line<T, multiline, escape>(lines, 1); test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", const std::vector<std::string> lines{"9,8,first",
"1,2,\\\n\\\n\\\n\\\njust", "1,2,\\\n\\\n\\\n\\\njust",
"4,3,third"}; "4,3,third"};
test_unterminated_line<T, multiline, escape>(lines, 1); test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
// multiline limmit reached quote // multiline limmit reached quote
{ {
const std::vector<std::string> lines{"1,2,\"\n\n\n\n\njust\""}; const std::vector<std::string> lines{"1,2,\"\n\n\n\n\njust\""};
test_unterminated_line<T, multiline, escape, quote>(lines, 0); test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, quote>(lines, 0); test_unterminated_line<multiline, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", const std::vector<std::string> lines{"9,8,first",
"1,2,\"\n\n\n\n\njust\""}; "1,2,\"\n\n\n\n\njust\""};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, quote>(lines, 1); test_unterminated_line<multiline, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,\"fir\nst\"", const std::vector<std::string> lines{"9,8,\"fir\nst\"",
"1,2,\"\n\n\n\n\njust\""}; "1,2,\"\n\n\n\n\njust\""};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, quote>(lines, 1); test_unterminated_line<multiline, quote>(lines, 1);
} }
// multiline limmit reached quote and escape // multiline limmit reached quote and escape
{ {
const std::vector<std::string> lines{"1,2,\"\\\n\n\\\n\\\n\\\njust"}; const std::vector<std::string> lines{"1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<T, multiline, escape, quote>(lines, 0); test_unterminated_line<multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", const std::vector<std::string> lines{"9,8,first",
"1,2,\"\\\n\n\\\n\\\n\\\njust"}; "1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,fi\\\nrst", const std::vector<std::string> lines{"9,8,fi\\\nrst",
"1,2,\"\\\n\n\\\n\\\n\\\njust"}; "1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,\"fi\nrst\"", const std::vector<std::string> lines{"9,8,\"fi\nrst\"",
"1,2,\"\\\n\n\\\n\\\n\\\njust"}; "1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,\"fi\nr\\\nst\"", const std::vector<std::string> lines{"9,8,\"fi\nr\\\nst\"",
"1,2,\"\\\n\n\\\n\\\n\\\njust"}; "1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<T, multiline, escape, quote>(lines, 1); test_unterminated_line<multiline, escape, quote>(lines, 1);
} }
} }

View File

@ -7,14 +7,13 @@ template <typename T, typename... Us>
struct has_type<T, std::tuple<Us...>> struct has_type<T, std::tuple<Us...>>
: std::disjunction<std::is_same<T, Us>...> {}; : std::disjunction<std::is_same<T, Us>...> {};
template <typename T, typename... Ts> template <bool buffer_mode, typename Setup, typename... Ts>
static void test_fields(const std::string file_name, const std::vector<X>& data, static void test_fields_impl(const std::string file_name,
const std::vector<X>& data,
const std::vector<std::string>& fields) { const std::vector<std::string>& fields) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
using CaseType = std::tuple<Ts...>; using CaseType = std::tuple<Ts...>;
auto [p, _] = make_parser<buffer_mode, ErrorMode>(file_name, ","); auto [p, _] = make_parser<buffer_mode, Setup>(file_name, ",");
CHECK_FALSE(p.field_exists("Unknown")); CHECK_FALSE(p.field_exists("Unknown"));
p.use_fields(fields); p.use_fields(fields);
std::vector<CaseType> i; std::vector<CaseType> i;
@ -37,9 +36,23 @@ static void test_fields(const std::string file_name, const std::vector<X>& data,
} }
} }
TEST_CASE_TEMPLATE("test various cases with header", T, template <typename... Ts>
ParserOptionCombinations) { static void test_fields(const std::string file_name, const std::vector<X>& data,
unique_file_name f{"various_cases_with_header"}; const std::vector<std::string>& fields) {
test_fields_impl<false, ss::setup<>, Ts...>(file_name, data, fields);
test_fields_impl<false, ss::setup<ss::string_error>, Ts...>(file_name, data,
fields);
test_fields_impl<false, ss::setup<ss::throw_on_error>, Ts...>(file_name,
data, fields);
test_fields_impl<true, ss::setup<>, Ts...>(file_name, data, fields);
test_fields_impl<true, ss::setup<ss::string_error>, Ts...>(file_name, data,
fields);
test_fields_impl<true, ss::setup<ss::throw_on_error>, Ts...>(file_name,
data, fields);
}
TEST_CASE("parser test various cases with header") {
unique_file_name f{"test_parser"};
constexpr static auto Int = "Int"; constexpr static auto Int = "Int";
constexpr static auto Dbl = "Double"; constexpr static auto Dbl = "Double";
constexpr static auto Str = "String"; constexpr static auto Str = "String";
@ -167,30 +180,27 @@ TEST_CASE_TEMPLATE("test various cases with header", T,
print(call) print(call)
*/ */
test_fields<T, str>(o, d, {Str}); test_fields<str>(o, d, {Str});
test_fields<T, int>(o, d, {Int}); test_fields<int>(o, d, {Int});
test_fields<T, double>(o, d, {Dbl}); test_fields<double>(o, d, {Dbl});
test_fields<T, str, int>(o, d, {Str, Int}); test_fields<str, int>(o, d, {Str, Int});
test_fields<T, str, double>(o, d, {Str, Dbl}); test_fields<str, double>(o, d, {Str, Dbl});
test_fields<T, int, str>(o, d, {Int, Str}); test_fields<int, str>(o, d, {Int, Str});
test_fields<T, int, double>(o, d, {Int, Dbl}); test_fields<int, double>(o, d, {Int, Dbl});
test_fields<T, double, str>(o, d, {Dbl, Str}); test_fields<double, str>(o, d, {Dbl, Str});
test_fields<T, double, int>(o, d, {Dbl, Int}); test_fields<double, int>(o, d, {Dbl, Int});
test_fields<T, str, int, double>(o, d, {Str, Int, Dbl}); test_fields<str, int, double>(o, d, {Str, Int, Dbl});
test_fields<T, str, double, int>(o, d, {Str, Dbl, Int}); test_fields<str, double, int>(o, d, {Str, Dbl, Int});
test_fields<T, int, str, double>(o, d, {Int, Str, Dbl}); test_fields<int, str, double>(o, d, {Int, Str, Dbl});
test_fields<T, int, double, str>(o, d, {Int, Dbl, Str}); test_fields<int, double, str>(o, d, {Int, Dbl, Str});
test_fields<T, double, str, int>(o, d, {Dbl, Str, Int}); test_fields<double, str, int>(o, d, {Dbl, Str, Int});
test_fields<T, double, int, str>(o, d, {Dbl, Int, Str}); test_fields<double, int, str>(o, d, {Dbl, Int, Str});
} }
template <typename T> template <bool buffer_mode, typename... Ts>
void test_invalid_fields(const std::vector<std::string>& lines, void test_invalid_fields_impl(const std::vector<std::string>& lines,
const std::vector<std::string>& fields) { const std::vector<std::string>& fields) {
constexpr auto buffer_mode = T::BufferMode::value; unique_file_name f{"test_parser"};
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"invalid_fields"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
for (const auto& line : lines) { for (const auto& line : lines) {
@ -200,21 +210,21 @@ void test_invalid_fields(const std::vector<std::string>& lines,
{ {
// No fields specified // No fields specified
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto command = [&p = p] { p.use_fields(); }; auto command = [&p = p] { p.use_fields(); };
expect_error_on_command(p, command); expect_error_on_command(p, command);
} }
{ {
// Unknown field // Unknown field
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto command = [&p = p] { p.use_fields("Unknown"); }; auto command = [&p = p] { p.use_fields("Unknown"); };
expect_error_on_command(p, command); expect_error_on_command(p, command);
} }
{ {
// Field used multiple times // Field used multiple times
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto command = [&p = p, &fields = fields] { auto command = [&p = p, &fields = fields] {
p.use_fields(fields.at(0), fields.at(0)); p.use_fields(fields.at(0), fields.at(0));
}; };
@ -225,7 +235,7 @@ void test_invalid_fields(const std::vector<std::string>& lines,
{ {
// Mapping out of range // Mapping out of range
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto command = [&p = p, &fields = fields] { auto command = [&p = p, &fields = fields] {
p.use_fields(fields.at(0)); p.use_fields(fields.at(0));
p.template get_next<std::string, std::string>(); p.template get_next<std::string, std::string>();
@ -237,7 +247,7 @@ void test_invalid_fields(const std::vector<std::string>& lines,
{ {
// Invalid header // Invalid header
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto command = [&p = p, &fields = fields] { p.use_fields(fields); }; auto command = [&p = p, &fields = fields] { p.use_fields(fields); };
if (!fields.empty()) { if (!fields.empty()) {
@ -249,7 +259,7 @@ void test_invalid_fields(const std::vector<std::string>& lines,
command(); command();
CHECK(p.valid()); CHECK(p.valid());
if (!p.valid()) { if (!p.valid()) {
if constexpr (T::StringError) { if constexpr (ss::setup<Ts...>::string_error) {
std::cout << p.error_msg() << std::endl; std::cout << p.error_msg() << std::endl;
} }
} }
@ -258,38 +268,44 @@ void test_invalid_fields(const std::vector<std::string>& lines,
} }
} }
TEST_CASE_TEMPLATE("test invalid fheader fields usage", T, template <typename... Ts>
ParserOptionCombinations) { void test_invalid_fields(const std::vector<std::string>& lines,
test_invalid_fields<T>({}, {}); const std::vector<std::string>& fields) {
test_invalid_fields_impl<false>(lines, fields);
test_invalid_fields<T>({"Int"}, {"Int"}); test_invalid_fields_impl<false, ss::string_error>(lines, fields);
test_invalid_fields<T>({"Int", "1"}, {"Int"}); test_invalid_fields_impl<false, ss::throw_on_error>(lines, fields);
test_invalid_fields<T>({"Int", "1", "2"}, {"Int"}); test_invalid_fields_impl<true>(lines, fields);
test_invalid_fields_impl<true, ss::string_error>(lines, fields);
test_invalid_fields<T>({"Int,String"}, {"Int", "String"}); test_invalid_fields_impl<true, ss::throw_on_error>(lines, fields);
test_invalid_fields<T>({"Int,String", "1,hi"}, {"Int", "String"});
test_invalid_fields<T>({"Int,String", "2,hello"}, {"Int", "String"});
test_invalid_fields<T>({"Int,String,Double"}, {"Int", "String", "Double"});
test_invalid_fields<T>({"Int,String,Double", "1,hi,2.34"},
{"Int", "String", "Double"});
test_invalid_fields<T>({"Int,String,Double", "1,hi,2.34", "2,hello,3.45"},
{"Int", "String", "Double"});
test_invalid_fields<T>({"Int,Int,Int"}, {"Int", "Int", "Int"});
test_invalid_fields<T>({"Int,Int,Int", "1,2,3"}, {"Int", "Int", "Int"});
test_invalid_fields<T>({"Int,String,Int"}, {"Int", "String", "Int"});
test_invalid_fields<T>({"Int,String,Int", "1,hi,3"},
{"Int", "String", "Int"});
} }
TEST_CASE_TEMPLATE("test invalid rows with header", T, TEST_CASE("parser test invalid header fields usage") {
ParserOptionCombinations) { test_invalid_fields({}, {});
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"invalid rows with header"}; test_invalid_fields({"Int"}, {"Int"});
test_invalid_fields({"Int", "1"}, {"Int"});
test_invalid_fields({"Int", "1", "2"}, {"Int"});
test_invalid_fields({"Int,String"}, {"Int", "String"});
test_invalid_fields({"Int,String", "1,hi"}, {"Int", "String"});
test_invalid_fields({"Int,String", "2,hello"}, {"Int", "String"});
test_invalid_fields({"Int,String,Double"}, {"Int", "String", "Double"});
test_invalid_fields({"Int,String,Double", "1,hi,2.34"},
{"Int", "String", "Double"});
test_invalid_fields({"Int,String,Double", "1,hi,2.34", "2,hello,3.45"},
{"Int", "String", "Double"});
test_invalid_fields({"Int,Int,Int"}, {"Int", "Int", "Int"});
test_invalid_fields({"Int,Int,Int", "1,2,3"}, {"Int", "Int", "Int"});
test_invalid_fields({"Int,String,Int"}, {"Int", "String", "Int"});
test_invalid_fields({"Int,String,Int", "1,hi,3"}, {"Int", "String", "Int"});
}
template <bool buffer_mode, typename... Ts>
void test_invalid_rows_with_header() {
unique_file_name f{"test_parser"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
out << "Int,String,Double" << std::endl; out << "Int,String,Double" << std::endl;
@ -302,7 +318,7 @@ TEST_CASE_TEMPLATE("test invalid rows with header", T,
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name);
p.use_fields("Int", "String", "Double"); p.use_fields("Int", "String", "Double");
using data = std::tuple<int, std::string, double>; using data = std::tuple<int, std::string, double>;
@ -328,7 +344,7 @@ TEST_CASE_TEMPLATE("test invalid rows with header", T,
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name);
p.use_fields("Double", "Int"); p.use_fields("Double", "Int");
using data = std::tuple<double, int>; using data = std::tuple<double, int>;
@ -352,7 +368,7 @@ TEST_CASE_TEMPLATE("test invalid rows with header", T,
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name);
p.use_fields("String", "Double"); p.use_fields("String", "Double");
using data = std::tuple<std::string, double>; using data = std::tuple<std::string, double>;
@ -379,12 +395,18 @@ TEST_CASE_TEMPLATE("test invalid rows with header", T,
} }
} }
template <typename T> TEST_CASE("parser test invalid rows with header") {
void test_ignore_empty(const std::vector<X>& data) { test_invalid_rows_with_header<false>();
constexpr auto buffer_mode = T::BufferMode::value; test_invalid_rows_with_header<false, ss::string_error>();
using ErrorMode = typename T::ErrorMode; test_invalid_rows_with_header<false, ss::throw_on_error>();
test_invalid_rows_with_header<true>();
test_invalid_rows_with_header<true, ss::string_error>();
test_invalid_rows_with_header<true, ss::throw_on_error>();
}
unique_file_name f{"ignore_empty"}; template <bool buffer_mode, typename... Ts>
void test_ignore_empty_impl(const std::vector<X>& data) {
unique_file_name f{"test_parser"};
make_and_write(f.name, data); make_and_write(f.name, data);
std::vector<X> expected; std::vector<X> expected;
@ -396,7 +418,7 @@ void test_ignore_empty(const std::vector<X>& data) {
{ {
auto [p, _] = auto [p, _] =
make_parser<buffer_mode, ErrorMode, ss::ignore_empty>(f.name, ","); make_parser<buffer_mode, ss::ignore_empty, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
for (const auto& a : p.template iterate<X>()) { for (const auto& a : p.template iterate<X>()) {
@ -407,7 +429,7 @@ void test_ignore_empty(const std::vector<X>& data) {
} }
{ {
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ","); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
std::vector<X> i; std::vector<X> i;
size_t n = 0; size_t n = 0;
while (!p.eof()) { while (!p.eof()) {
@ -428,44 +450,52 @@ void test_ignore_empty(const std::vector<X>& data) {
} }
} }
TEST_CASE_TEMPLATE("test various cases with empty lines", T, template <typename... Ts>
ParserOptionCombinations) { void test_ignore_empty(const std::vector<X>& data) {
test_ignore_empty<T>( test_ignore_empty_impl<false>(data);
{{1, 2, "x"}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, "w"}}); test_ignore_empty_impl<false, ss::string_error>(data);
test_ignore_empty_impl<false, ss::throw_on_error>(data);
test_ignore_empty_impl<true>(data);
test_ignore_empty_impl<true, ss::string_error>(data);
test_ignore_empty_impl<true, ss::throw_on_error>(data);
}
test_ignore_empty<T>( TEST_CASE("parser test various cases with empty lines") {
test_ignore_empty({{1, 2, "x"}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, "w"}});
test_ignore_empty(
{{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, "w"}}); {{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, "w"}});
test_ignore_empty<T>( test_ignore_empty(
{{1, 2, "x"}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}}); {{1, 2, "x"}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}});
test_ignore_empty<T>( test_ignore_empty(
{{1, 2, "x"}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}}); {{1, 2, "x"}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}});
test_ignore_empty<T>( test_ignore_empty(
{{1, 2, X::empty}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}}); {{1, 2, X::empty}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}});
test_ignore_empty<T>( test_ignore_empty(
{{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}}); {{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}});
test_ignore_empty<T>( test_ignore_empty(
{{1, 2, "x"}, {3, 4, "y"}, {9, 10, X::empty}, {11, 12, X::empty}}); {{1, 2, "x"}, {3, 4, "y"}, {9, 10, X::empty}, {11, 12, X::empty}});
test_ignore_empty<T>( test_ignore_empty(
{{1, 2, X::empty}, {3, 4, "y"}, {9, 10, X::empty}, {11, 12, X::empty}}); {{1, 2, X::empty}, {3, 4, "y"}, {9, 10, X::empty}, {11, 12, X::empty}});
test_ignore_empty<T>({{1, 2, X::empty}, test_ignore_empty({{1, 2, X::empty},
{3, 4, X::empty}, {3, 4, X::empty},
{9, 10, X::empty}, {9, 10, X::empty},
{11, 12, X::empty}}); {11, 12, X::empty}});
test_ignore_empty<T>( test_ignore_empty(
{{1, 2, "x"}, {3, 4, X::empty}, {9, 10, X::empty}, {11, 12, X::empty}}); {{1, 2, "x"}, {3, 4, X::empty}, {9, 10, X::empty}, {11, 12, X::empty}});
test_ignore_empty<T>( test_ignore_empty(
{{1, 2, X::empty}, {3, 4, X::empty}, {9, 10, X::empty}, {11, 12, "w"}}); {{1, 2, X::empty}, {3, 4, X::empty}, {9, 10, X::empty}, {11, 12, "w"}});
test_ignore_empty<T>({{11, 12, X::empty}}); test_ignore_empty({{11, 12, X::empty}});
test_ignore_empty<T>({}); test_ignore_empty({});
} }

View File

@ -314,7 +314,7 @@ void test_data_combinations(const std::vector<column>& input_data,
return; return;
} }
unique_file_name f{"parser_data_combinations" + std::string{SEGMENT_NAME}}; unique_file_name f{"test_parser2" + std::string{SEGMENT_NAME}};
std::vector<std::vector<field>> expected_data; std::vector<std::vector<field>> expected_data;
std::vector<std::string> header; std::vector<std::string> header;
std::vector<field> field_header; std::vector<field> field_header;