Add positions method to parser, write unit tests for it, update other parser tests

This commit is contained in:
ado 2024-02-25 02:06:48 +01:00
parent 383de57f9a
commit 88e711a5f7
9 changed files with 460 additions and 425 deletions

View File

@ -106,6 +106,10 @@ public:
: reader_.line_number_;
}
size_t position() const {
return reader_.chars_read_;
}
template <typename T, typename... Ts>
no_void_validator_tup_t<T, Ts...> get_next() {
std::optional<std::string> error;
@ -694,6 +698,7 @@ private:
csv_data_size_{other.csv_data_size_},
curr_char_{other.curr_char_}, crlf_{other.crlf_},
line_number_{other.line_number_},
chars_read_{other.chars_read_},
next_line_size_{other.next_line_size_} {
other.buffer_ = nullptr;
other.next_line_buffer_ = nullptr;
@ -718,12 +723,14 @@ private:
curr_char_ = other.curr_char_;
crlf_ = other.crlf_;
line_number_ = other.line_number_;
chars_read_ = other.chars_read_;
next_line_size_ = other.next_line_size_;
other.buffer_ = nullptr;
other.next_line_buffer_ = nullptr;
other.helper_buffer_ = nullptr;
other.file_ = nullptr;
other.csv_data_buffer_ = nullptr;
}
return *this;
@ -803,9 +810,11 @@ private:
next_line_buffer_[0] = '\0';
}
chars_read_ = curr_char_;
if (file_) {
ssize = get_line_file(&next_line_buffer_,
&next_line_buffer_size_, file_);
curr_char_ = ftell(file_);
} else {
ssize = get_line_buffer(&next_line_buffer_,
&next_line_buffer_size_,
@ -1009,6 +1018,7 @@ private:
bool crlf_{false};
size_t line_number_{0};
size_t chars_read_{0};
size_t next_line_size_{0};
};

View File

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

View File

@ -1,12 +1,14 @@
#pragma once
#include <algorithm>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <ss/common.hpp>
#include <ss/setup.hpp>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#ifdef CMAKE_GITHUB_CI
#include <doctest/doctest.h>
@ -20,6 +22,24 @@ class parser;
} /* ss */
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 {
std::string data_;
@ -172,23 +192,32 @@ template <typename T>
}
template <bool buffer_mode, typename... Ts>
[[maybe_unused]] std::tuple<ss::parser<Ts...>, std::string> make_parser(
const std::string& file_name, const std::string& delim = "") {
std::tuple<ss::parser<Ts...>, std::string> make_parser_impl(
const std::string& file_name, std::string delim = ss::default_delimiter) {
if (buffer_mode) {
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},
std::move(buffer)};
}
return {ss::parser<Ts...>{buffer.data(), buffer.size(), delim},
std::move(buffer)};
} else {
if (delim.empty()) {
return {ss::parser<Ts...>{file_name}, std::string{}};
} 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 */

View File

@ -1,7 +1,7 @@
#include "test_parser1.hpp"
TEST_CASE("test file not found") {
unique_file_name f{"test_parser"};
unique_file_name f{"file_not_found"};
{
ss::parser p{f.name, ","};
@ -11,6 +11,7 @@ TEST_CASE("test file not found") {
{
ss::parser<ss::string_error> p{f.name, ","};
CHECK_FALSE(p.valid());
CHECK_FALSE(p.error_msg().empty());
}
try {
@ -30,6 +31,7 @@ TEST_CASE("test null buffer") {
{
ss::parser<ss::string_error> p{nullptr, 10, ","};
CHECK_FALSE(p.valid());
CHECK_FALSE(p.error_msg().empty());
}
try {
@ -40,20 +42,105 @@ TEST_CASE("test null buffer") {
}
}
template <bool buffer_mode, typename... Ts>
void test_various_cases() {
struct Y {
constexpr static auto delim = ",";
std::string s1;
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'));
}
}
// TODO uncomment
/*
TEST_CASE_TEMPLATE("test line method", BufferMode, std::true_type,
std::false_type) {
unique_file_name f{"test_parser"};
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<BufferMode::value>(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"},
{7, 8, "u"}, {9, 10, "v"}, {11, 12, "w"}};
make_and_write(f.name, data);
auto csv_data_buffer = make_buffer(f.name);
{
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
ss::parser p0{std::move(p)};
p = std::move(p0);
std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2;
auto move_rotate = [&p = p, &p0 = p0] {
@ -77,13 +164,13 @@ void test_various_cases() {
}
{
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2;
auto [p3, ___] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p3, ___] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i3;
std::vector<X> expected = {std::begin(data) + 1, std::end(data)};
@ -112,9 +199,9 @@ void test_various_cases() {
}
{
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2;
while (!p.eof()) {
@ -131,7 +218,7 @@ void test_various_cases() {
}
{
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i;
for (auto&& a :
@ -143,10 +230,10 @@ void test_various_cases() {
}
{
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2;
using tup = std::tuple<int, double, std::string>;
@ -164,7 +251,7 @@ void test_various_cases() {
}
{
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i;
using tup = std::tuple<int, double, std::string>;
@ -176,7 +263,7 @@ void test_various_cases() {
}
{
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i;
while (!p.eof()) {
@ -187,7 +274,7 @@ void test_various_cases() {
}
{
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i;
for (auto&& a : p.template iterate<X>()) {
@ -199,10 +286,10 @@ void test_various_cases() {
{
constexpr int excluded = 3;
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2;
while (!p.eof()) {
@ -217,7 +304,7 @@ void test_various_cases() {
};
}
if (!ss::setup<Ts...>::throw_on_error) {
if (!T::ThrowOnError) {
for (auto&& a : p2.template iterate_object<X, ss::ax<int, excluded>,
double, std::string>()) {
if (p2.valid()) {
@ -237,16 +324,16 @@ void test_various_cases() {
[&](const X& x) { return x.i != excluded; });
CHECK_EQ(i, expected);
if (!ss::setup<Ts...>::throw_on_error) {
if (!T::ThrowOnError) {
CHECK_EQ(i2, expected);
}
}
{
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2;
while (!p.eof()) {
@ -261,7 +348,7 @@ void test_various_cases() {
}
}
if (!ss::setup<Ts...>::throw_on_error) {
if (!T::ThrowOnError) {
for (auto&& a : p2.template iterate_object<X, ss::nx<int, 3>,
double, std::string>()) {
if (p2.valid()) {
@ -272,21 +359,21 @@ void test_various_cases() {
std::vector<X> expected = {{3, 4, "y"}};
CHECK_EQ(i, expected);
if (!ss::setup<Ts...>::throw_on_error) {
if (!T::ThrowOnError) {
CHECK_EQ(i2, expected);
}
}
{
unique_file_name empty_f{"test_parser"};
unique_file_name empty_f{"various_valid_cases"};
std::vector<X> empty_data = {};
make_and_write(empty_f.name, empty_data);
auto [p, _] = make_parser<buffer_mode, Ts...>(empty_f.name, ",");
auto [p, _] = make_parser<buffer_mode, ErrorMode>(empty_f.name, ",");
std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(empty_f.name, ",");
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(empty_f.name, ",");
std::vector<X> i2;
while (!p.eof()) {
@ -302,15 +389,6 @@ void test_various_cases() {
}
}
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>;
struct test_struct {
int i;
@ -324,9 +402,10 @@ struct test_struct {
static inline void expect_test_struct(const test_struct&) {
}
template <bool buffer_mode, typename... Ts>
void test_composite_conversion() {
unique_file_name f{"test_parser"};
TEST_CASE_TEMPLATE("parser test composite conversion", BufferMode,
std::true_type, std::false_type) {
constexpr auto buffer_mode = BufferMode::value;
unique_file_name f{"composite_conversion"};
{
std::ofstream out{f.name};
for (auto& i :
@ -336,7 +415,7 @@ void test_composite_conversion() {
}
}
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ",");
auto [p, _] = make_parser<buffer_mode, ss::string_error>(f.name, ",");
auto fail = [] { FAIL(""); };
auto expect_error = [](auto error) { CHECK(!error.empty()); };
auto ignore_error = [] {};
@ -546,18 +625,12 @@ void test_composite_conversion() {
CHECK(p.eof());
}
// 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>
template <bool buffer_mode, typename... Ts>
void test_no_new_line_at_eof_impl(const std::vector<X>& data) {
unique_file_name f{"test_parser"};
unique_file_name f{"no_new_line_at_eof"};
make_and_write(f.name, data, {}, false);
auto [p, _] = make_parser<buffer_mode>(f.name);
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name);
std::vector<X> parsed_data;
for (const auto& el : p.template iterate<X>()) {
@ -567,32 +640,36 @@ void test_no_new_line_at_eof_impl(const std::vector<X>& data) {
CHECK_EQ(data, parsed_data);
}
template <bool buffer_mode>
template <bool buffer_mode, typename... Ts>
void test_no_new_line_at_eof() {
test_no_new_line_at_eof_impl<buffer_mode>({});
test_no_new_line_at_eof_impl<buffer_mode>({{1, 2, "X"}});
test_no_new_line_at_eof_impl<buffer_mode>({{1, 2, "X"}, {}});
test_no_new_line_at_eof_impl<buffer_mode>({{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>(
test_no_new_line_at_eof_impl<buffer_mode, Ts...>({});
test_no_new_line_at_eof_impl<buffer_mode, Ts...>({{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, Ts...>(
{{1, 2, "X"}, {3, 4, "YY"}});
test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
{{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"}});
for (size_t i = 0; i < 2 * ss::get_line_initial_buffer_size; ++i) {
test_no_new_line_at_eof_impl<buffer_mode>(
test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
{{1, 2, std::string(i, 'X')}});
for (size_t j = 0; j < 2 * ss::get_line_initial_buffer_size; j += 13) {
test_no_new_line_at_eof_impl<buffer_mode>(
test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
{{1, 2, std::string(i, 'X')}, {3, 4, std::string(j, 'Y')}});
test_no_new_line_at_eof_impl<buffer_mode>(
test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
{{1, 2, std::string(j, 'X')}, {3, 4, std::string(i, 'Y')}});
}
}
}
TEST_CASE("test no new line at end of data") {
test_no_new_line_at_eof<false>();
test_no_new_line_at_eof<true>();
TEST_CASE_TEMPLATE("test no new line at end of data", T,
ParserOptionCombinations) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
test_no_new_line_at_eof<buffer_mode, ErrorMode>();
}

View File

@ -41,11 +41,16 @@ struct xyz {
}
};
template <bool buffer_mode, typename... Ts>
void test_moving_of_parsed_composite_values() {
TEST_CASE_TEMPLATE("test moving of parsed composite values", T,
config<std::true_type>, config<std::false_type>,
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
return;
auto [p, _] = make_parser<buffer_mode, Ts...>("", "");
auto [p, _] = make_parser<buffer_mode, ErrorMode>("", "");
p.template try_next<my_string, my_string, my_string>()
.template or_else<my_string, my_string, my_string, my_string>(
[](auto&&) {})
@ -56,70 +61,41 @@ void test_moving_of_parsed_composite_values() {
[](auto&, auto&, auto&) {});
}
TEST_CASE("parser test the moving of parsed composite values") {
test_moving_of_parsed_composite_values<false>();
test_moving_of_parsed_composite_values<false, ss::string_error>();
test_moving_of_parsed_composite_values<true>();
test_moving_of_parsed_composite_values<true, ss::string_error>();
}
TEST_CASE("parser test error mode") {
unique_file_name f{"test_parser"};
TEST_CASE_TEMPLATE("parser test string error mode", BufferMode, std::true_type,
std::false_type) {
unique_file_name f{"string_error"};
{
std::ofstream out{f.name};
out << "junk" << std::endl;
out << "junk" << std::endl;
}
{
auto [p, _] = make_parser<false, ss::string_error>(f.name, ",");
auto [p, _] = make_parser<BufferMode::value, 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());
}
REQUIRE_FALSE(p.eof());
p.template 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"};
TEST_CASE_TEMPLATE("parser throw on error mode", BufferMode, std::true_type,
std::false_type) {
unique_file_name f{"throw_on_error"};
{
std::ofstream out{f.name};
out << "junk" << std::endl;
out << "junk" << std::endl;
}
{
auto [p, _] = make_parser<false, ss::throw_on_error>(f.name, ",");
auto [p, _] =
make_parser<BufferMode::value, 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());
}
}
{
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());
}
REQUIRE_FALSE(p.eof());
try {
p.template get_next<int>();
FAIL("Expected exception...");
} catch (const std::exception& e) {
CHECK_FALSE(std::string{e.what()}.empty());
}
}
@ -130,9 +106,11 @@ static inline std::string no_quote(const std::string& s) {
return s;
}
template <bool buffer_mode, typename... Ts>
void test_quote_multiline() {
unique_file_name f{"test_parser"};
TEST_CASE_TEMPLATE("test quote multiline", T, ParserOptionCombinations) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"quote_multiline"};
std::vector<X> data = {{1, 2, "\"x\r\nx\nx\""},
{3, 4, "\"y\ny\r\ny\""},
{5, 6, "\"z\nz\""},
@ -151,9 +129,8 @@ void test_quote_multiline() {
}
}
auto [p, _] =
make_parser<buffer_mode, ss::multiline, ss::quote<'"'>, Ts...>(f.name,
",");
auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline,
ss::quote<'"'>>(f.name, ",");
std::vector<X> i;
@ -168,7 +145,7 @@ void test_quote_multiline() {
CHECK_EQ(i, data);
auto [p_no_multiline, __] =
make_parser<buffer_mode, ss::quote<'"'>, Ts...>(f.name, ",");
make_parser<buffer_mode, ErrorMode, ss::quote<'"'>>(f.name, ",");
while (!p.eof()) {
auto command = [&p_no_multiline = p_no_multiline] {
p_no_multiline.template get_next<int, double, std::string>();
@ -177,23 +154,16 @@ void test_quote_multiline() {
}
}
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) {
s.erase(std::remove(begin(s), end(s), '\\'), end(s));
return s;
}
template <bool buffer_mode, typename... Ts>
void test_escape_multiline() {
unique_file_name f{"test_parser"};
TEST_CASE_TEMPLATE("test escape multiline", T, ParserOptionCombinations) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"escape_multiline"};
std::vector<X> data = {{1, 2, "x\\\nx\\\r\nx"},
{5, 6, "z\\\nz\\\nz"},
{7, 8, "u"},
@ -212,9 +182,8 @@ void test_escape_multiline() {
}
}
auto [p, _] =
make_parser<buffer_mode, ss::multiline, ss::escape<'\\'>, Ts...>(f.name,
",");
auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline,
ss::escape<'\\'>>(f.name, ",");
std::vector<X> i;
while (!p.eof()) {
@ -228,7 +197,7 @@ void test_escape_multiline() {
CHECK_EQ(i, data);
auto [p_no_multiline, __] =
make_parser<buffer_mode, ss::escape<'\\'>, Ts...>(f.name, ",");
make_parser<buffer_mode, ErrorMode, ss::escape<'\\'>>(f.name, ",");
while (!p.eof()) {
auto command = [&p_no_multiline = p_no_multiline] {
auto a =
@ -238,18 +207,11 @@ void test_escape_multiline() {
}
}
TEST_CASE("parser test csv on multiple lines with escapes") {
test_escape_multiline<false>();
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>();
}
TEST_CASE_TEMPLATE("test quote escape multiline", T, ParserOptionCombinations) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
template <bool buffer_mode, typename... Ts>
void test_quote_escape_multiline() {
unique_file_name f{"test_parser"};
unique_file_name f{"quote_escape_multiline"};
{
std::ofstream out{f.name};
out << "1,2,\"just\\\n\nstrings\"" << std::endl;
@ -266,8 +228,8 @@ void test_quote_escape_multiline() {
size_t bad_lines = 1;
auto num_errors = 0;
auto [p, _] = make_parser<buffer_mode, ss::multiline, ss::escape<'\\'>,
ss::quote<'"'>, Ts...>(f.name);
auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline,
ss::escape<'\\'>, ss::quote<'"'>>(f.name);
std::vector<X> i;
while (!p.eof()) {
@ -298,12 +260,3 @@ void test_quote_escape_multiline() {
}
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,8 +1,10 @@
#include "test_parser1.hpp"
template <bool buffer_mode, typename... Ts>
void test_multiline_restricted() {
unique_file_name f{"test_parser"};
TEST_CASE_TEMPLATE("test multiline restricted", T, ParserOptionCombinations) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"multiline_restricted"};
{
std::ofstream out{f.name};
out << "1,2,\"just\n\nstrings\"" << std::endl;
@ -24,8 +26,8 @@ void test_multiline_restricted() {
auto num_errors = 0;
auto [p, _] =
make_parser<buffer_mode, ss::multiline_restricted<2>, ss::quote<'"'>,
ss::escape<'\\'>, Ts...>(f.name, ",");
make_parser<buffer_mode, ErrorMode, ss::multiline_restricted<2>,
ss::quote<'"'>, ss::escape<'\\'>>(f.name, ",");
std::vector<X> i;
while (!p.eof()) {
@ -63,26 +65,20 @@ void test_multiline_restricted() {
CHECK_EQ(i, data);
}
TEST_CASE("parser test multiline restricted") {
test_multiline_restricted<false>();
test_multiline_restricted<false, ss::string_error>();
test_multiline_restricted<false, ss::throw_on_error>();
test_multiline_restricted<true>();
test_multiline_restricted<true, ss::string_error>();
test_multiline_restricted<true, ss::throw_on_error>();
}
template <typename T, typename... Ts>
void test_unterminated_line(const std::vector<std::string>& lines,
size_t bad_line) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
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"};
unique_file_name f{"unterminated_line"};
std::ofstream out{f.name};
for (const auto& line : lines) {
out << line << std::endl;
}
out.close();
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name);
auto [p, _] = make_parser<buffer_mode, ErrorMode, Ts...>(f.name);
size_t line = 0;
while (!p.eof()) {
auto command = [&p = p] {
@ -100,21 +96,8 @@ void test_unterminated_line_impl(const std::vector<std::string>& lines,
}
}
template <typename... Ts>
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") {
TEST_CASE_TEMPLATE("parser test csv on multiline with errors", T,
ParserOptionCombinations) {
using multiline = ss::multiline_restricted<3>;
using escape = ss::escape<'\\'>;
using quote = ss::quote<'"'>;
@ -122,209 +105,209 @@ TEST_CASE("parser test csv on multiline with errors") {
// unterminated escape
{
const std::vector<std::string> lines{"1,2,just\\"};
test_unterminated_line<multiline, escape>(lines, 0);
test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, escape>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0);
}
{
const std::vector<std::string> lines{"1,2,just\\", "9,8,second"};
test_unterminated_line<multiline, escape>(lines, 0);
test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, escape>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0);
}
{
const std::vector<std::string> lines{"9,8,first", "1,2,just\\"};
test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,first", "1,2,just\\",
"3,4,third"};
test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,first",
"1,2,just\\\nstrings\\",
"3,4,th\\\nird"};
test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,first", "3,4,second",
"1,2,just\\"};
test_unterminated_line<multiline, escape>(lines, 2);
test_unterminated_line<multiline, escape, quote>(lines, 2);
test_unterminated_line<T, multiline, escape>(lines, 2);
test_unterminated_line<T, multiline, escape, quote>(lines, 2);
}
{
const std::vector<std::string> lines{"9,8,\\first", "3,4,second",
"1,2,jus\\t\\"};
test_unterminated_line<multiline, escape>(lines, 2);
test_unterminated_line<multiline, escape, quote>(lines, 2);
test_unterminated_line<T, multiline, escape>(lines, 2);
test_unterminated_line<T, multiline, escape, quote>(lines, 2);
}
// unterminated quote
{
const std::vector<std::string> lines{"1,2,\"just"};
test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<multiline, quote>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, quote>(lines, 0);
}
{
const std::vector<std::string> lines{"1,2,\"just", "9,8,second"};
test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<multiline, quote>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, quote>(lines, 0);
}
{
const std::vector<std::string> lines{"9,8,first", "1,2,\"just"};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<multiline, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,first", "1,2,\"just",
"3,4,th\\,ird"};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<multiline, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,first", "3,4,second",
"1,2,\"just"};
test_unterminated_line<multiline, escape, quote>(lines, 2);
test_unterminated_line<multiline, quote>(lines, 2);
test_unterminated_line<T, multiline, escape, quote>(lines, 2);
test_unterminated_line<T, multiline, quote>(lines, 2);
}
{
const std::vector<std::string> lines{"9,8,\"first\"",
"\"3\",4,\"sec,ond\"",
"1,2,\"ju\"\"st"};
test_unterminated_line<multiline, escape, quote>(lines, 2);
test_unterminated_line<multiline, quote>(lines, 2);
test_unterminated_line<T, multiline, escape, quote>(lines, 2);
test_unterminated_line<T, multiline, quote>(lines, 2);
}
// unterminated quote and escape
{
const std::vector<std::string> lines{"1,2,\"just\\"};
test_unterminated_line<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<T, multiline, escape, quote>(lines, 0);
}
{
const std::vector<std::string> lines{"1,2,\"just\\\n\\"};
test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0);
}
{
const std::vector<std::string> lines{"1,2,\"just\n\\"};
test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0);
}
{
const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\"};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\",
"4,3,thrid"};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,f\\\nirst", "1,2,\"just\n\\",
"4,3,thrid"};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,\"f\ni\nrst\"",
"1,2,\"just\n\\", "4,3,thrid"};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
// multiline limmit reached escape
{
const std::vector<std::string> lines{"1,2,\\\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape>(lines, 0);
test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, escape>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0);
}
{
const std::vector<std::string> lines{"9,8,first",
"1,2,\\\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,fi\\\nrs\\\nt",
"1,2,\\\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,first",
"1,2,\\\n\\\n\\\n\\\njust",
"4,3,third"};
test_unterminated_line<multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
// multiline limmit reached quote
{
const std::vector<std::string> lines{"1,2,\"\n\n\n\n\njust\""};
test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<multiline, quote>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, quote>(lines, 0);
}
{
const std::vector<std::string> lines{"9,8,first",
"1,2,\"\n\n\n\n\njust\""};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<multiline, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,\"fir\nst\"",
"1,2,\"\n\n\n\n\njust\""};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<multiline, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, quote>(lines, 1);
}
// multiline limmit reached quote and escape
{
const std::vector<std::string> lines{"1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape, quote>(lines, 0);
test_unterminated_line<T, multiline, escape, quote>(lines, 0);
}
{
const std::vector<std::string> lines{"9,8,first",
"1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,fi\\\nrst",
"1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,\"fi\nrst\"",
"1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
{
const std::vector<std::string> lines{"9,8,\"fi\nr\\\nst\"",
"1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape, quote>(lines, 1);
test_unterminated_line<T, multiline, escape, quote>(lines, 1);
}
}

View File

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

View File

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