mirror of
https://github.com/red0124/ssp.git
synced 2025-12-14 21:59:55 +01:00
Compare commits
57 Commits
improvemen
...
110ee840cc
| Author | SHA1 | Date | |
|---|---|---|---|
| 110ee840cc | |||
| 05f87bc78b | |||
| 88e711a5f7 | |||
| 383de57f9a | |||
| c6f6ba9821 | |||
| c5e491041d | |||
| 21b543ea4f | |||
| 8881649aca | |||
| 273e8ad950 | |||
|
|
f8fdb97151 | ||
| c0ee100f99 | |||
| 09e628020d | |||
| ea21b9ba04 | |||
| 230da6a3f2 | |||
| 3ea8adedfd | |||
| 57ba23c574 | |||
| 6516c6cc94 | |||
| cbb0a1ad8e | |||
| 1798b4c6f3 | |||
| 90a116ac7b | |||
| cbbe0acb25 | |||
| b993eb8852 | |||
| 5e32d722e8 | |||
| 59f6591da3 | |||
| 9d96a7d47f | |||
| d4fc2ee561 | |||
| d422667477 | |||
| aaa22046a5 | |||
| 775b8c93e2 | |||
| e4fba8a918 | |||
| 11d57bd073 | |||
| 45b840a30a | |||
| 8bb773625b | |||
| 0466c7234c | |||
| d21c387a33 | |||
| d019edb2bf | |||
| a2666816de | |||
| 417a03a8a4 | |||
| baf4317ffa | |||
| 63a618957b | |||
| dbaa8131e7 | |||
| ef8cf30919 | |||
| 42d618ed64 | |||
| 8b07f7d6cb | |||
| 82f8ed12b4 | |||
| e89e268280 | |||
| aacf690640 | |||
| f8c5757d99 | |||
| ce03c371ae | |||
| 6c859959d6 | |||
| 4434db29f6 | |||
|
|
fdd153b63e | ||
| c05ab35a33 | |||
| 8d1637cad1 | |||
| 7530be1c44 | |||
| 7062888d72 | |||
| f04ede3a49 |
11
.github/workflows/coverage.yml
vendored
11
.github/workflows/coverage.yml
vendored
@@ -46,21 +46,24 @@ jobs:
|
||||
- name: Install test coverage tools
|
||||
run: |
|
||||
apt update
|
||||
apt install -y gcovr lcov
|
||||
apt install -y gcovr
|
||||
|
||||
- name: Install lcov2.0
|
||||
run: script/ci_install_lcov.sh
|
||||
|
||||
- name: Configure
|
||||
run: cmake -S test -B build -D CMAKE_BUILD_TYPE=Debug -D CMAKE_CXX_FLAGS="-Wall -fprofile-arcs -ftest-coverage --coverage"
|
||||
run: cmake -S test -B build -D CMAKE_BUILD_TYPE=Debug -D CMAKE_CXX_FLAGS="-Wall -fprofile-arcs -ftest-coverage --coverage -fno-elide-constructors -fno-default-inline"
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build -j ${{steps.cores.outputs.count}}
|
||||
|
||||
- name: Run
|
||||
working-directory: build
|
||||
run: ctest --output-on-failure -j ${{steps.cores.outputs.count}}
|
||||
run: ctest --output-on-failure
|
||||
|
||||
- name: Generate coverage report
|
||||
run: |
|
||||
lcov -d . -c -o out.info --rc lcov_branch_coverage=1 --no-external
|
||||
lcov -d . -c -o out.info --rc branch_coverage=1 --no-external --filter branch --filter line --ignore-errors mismatch
|
||||
lcov -e out.info '*include/ss*hpp' -o filtered.info
|
||||
|
||||
- name: Invoke coveralls
|
||||
|
||||
2
.github/workflows/ubuntu-latest-clang.yml
vendored
2
.github/workflows/ubuntu-latest-clang.yml
vendored
@@ -66,4 +66,4 @@ jobs:
|
||||
|
||||
- name: Run
|
||||
working-directory: build
|
||||
run: ctest --output-on-failure -j ${{steps.cores.outputs.count}}
|
||||
run: ctest --output-on-failure
|
||||
|
||||
2
.github/workflows/ubuntu-latest-gcc.yml
vendored
2
.github/workflows/ubuntu-latest-gcc.yml
vendored
@@ -56,4 +56,4 @@ jobs:
|
||||
|
||||
- name: Run
|
||||
working-directory: build
|
||||
run: ctest --output-on-failure -j ${{steps.cores.outputs.count}}
|
||||
run: ctest --output-on-failure
|
||||
|
||||
2
.github/workflows/ubuntu-latest-icc.yml
vendored
2
.github/workflows/ubuntu-latest-icc.yml
vendored
@@ -74,4 +74,4 @@ jobs:
|
||||
|
||||
- name: Run
|
||||
working-directory: build
|
||||
run: ctest --output-on-failure -j ${{steps.cores.outputs.count}}
|
||||
run: ctest --output-on-failure
|
||||
|
||||
2
.github/workflows/win-msvc.yml
vendored
2
.github/workflows/win-msvc.yml
vendored
@@ -62,4 +62,4 @@ jobs:
|
||||
- name: Run
|
||||
working-directory: build
|
||||
run: >-
|
||||
ctest -C Debug --output-on-failure -j ${{steps.cores.outputs.count}}
|
||||
ctest -C Debug --output-on-failure
|
||||
|
||||
2
.github/workflows/win-msys2-clang.yml
vendored
2
.github/workflows/win-msys2-clang.yml
vendored
@@ -73,4 +73,4 @@ jobs:
|
||||
|
||||
- name: Run
|
||||
working-directory: build
|
||||
run: ctest --output-on-failure -j ${{steps.cores.outputs.count}}
|
||||
run: ctest --output-on-failure
|
||||
|
||||
2
.github/workflows/win-msys2-gcc.yml
vendored
2
.github/workflows/win-msys2-gcc.yml
vendored
@@ -71,4 +71,4 @@ jobs:
|
||||
|
||||
- name: Run
|
||||
working-directory: build
|
||||
run: ctest --output-on-failure -j ${{steps.cores.outputs.count}}
|
||||
run: ctest --output-on-failure
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -6,5 +6,3 @@ build/
|
||||
hbuild/
|
||||
subprojects/*
|
||||
!subprojects/*.wrap
|
||||
ssp.cpp
|
||||
ssp.bin
|
||||
|
||||
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.14)
|
||||
|
||||
project(
|
||||
ssp
|
||||
VERSION 1.6.1
|
||||
VERSION 1.6.2
|
||||
DESCRIPTION "csv parser"
|
||||
HOMEPAGE_URL "https://github.com/red0124/ssp"
|
||||
LANGUAGES CXX
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
#pragma once
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
namespace ss {
|
||||
|
||||
struct none {};
|
||||
|
||||
using string_range = std::pair<const char*, const char*>;
|
||||
using split_data = std::vector<string_range>;
|
||||
|
||||
constexpr inline auto default_delimiter = ",";
|
||||
constexpr inline auto get_line_initial_buffer_size = 128;
|
||||
|
||||
template <bool StringError>
|
||||
inline void assert_string_error_defined() {
|
||||
@@ -20,4 +27,67 @@ inline void assert_throw_on_error_not_defined() {
|
||||
static_assert(!ThrowOnError, "cannot handle errors manually if "
|
||||
"'throw_on_error' is enabled");
|
||||
}
|
||||
|
||||
#if __unix__
|
||||
inline ssize_t get_line_file(char** lineptr, size_t* n, FILE* stream) {
|
||||
return getline(lineptr, n, stream);
|
||||
}
|
||||
#else
|
||||
|
||||
using ssize_t = intptr_t;
|
||||
|
||||
ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) {
|
||||
if (lineptr == nullptr || n == nullptr || fp == nullptr) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
char buff[get_line_initial_buffer_size];
|
||||
|
||||
if (*lineptr == nullptr || *n < sizeof(buff)) {
|
||||
size_t new_n = sizeof(buff);
|
||||
auto new_lineptr = static_cast<char*>(realloc(*lineptr, new_n));
|
||||
if (new_lineptr == nullptr) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
*lineptr = new_lineptr;
|
||||
*n = new_n;
|
||||
}
|
||||
|
||||
(*lineptr)[0] = '\0';
|
||||
|
||||
size_t line_used = 0;
|
||||
while (fgets(buff, sizeof(buff), fp) != nullptr) {
|
||||
line_used = strlen(*lineptr);
|
||||
size_t buff_used = strlen(buff);
|
||||
|
||||
if (*n <= buff_used + line_used) {
|
||||
size_t new_n = *n * 2;
|
||||
|
||||
auto new_lineptr = static_cast<char*>(realloc(*lineptr, new_n));
|
||||
if (new_lineptr == nullptr) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
*lineptr = new_lineptr;
|
||||
*n = new_n;
|
||||
}
|
||||
|
||||
memcpy(*lineptr + line_used, buff, buff_used);
|
||||
line_used += buff_used;
|
||||
(*lineptr)[line_used] = '\0';
|
||||
|
||||
if ((*lineptr)[line_used - 1] == '\n') {
|
||||
return line_used;
|
||||
}
|
||||
}
|
||||
|
||||
return (line_used != 0) ? line_used : -1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} /* ss */
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -109,7 +109,7 @@ struct get_matcher<Matcher, T, Ts...> {
|
||||
struct is_matcher : is_instance_of_matcher<U, Matcher> {};
|
||||
|
||||
static_assert(count_v<is_matcher, T, Ts...> <= 1,
|
||||
"the same matcher cannot"
|
||||
"the same matcher cannot "
|
||||
"be defined multiple times");
|
||||
using type = std::conditional_t<is_matcher<T>::value, T,
|
||||
typename get_matcher<Matcher, Ts...>::type>;
|
||||
@@ -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
|
||||
|
||||
@@ -6,7 +6,7 @@ project(
|
||||
'cpp_std=c++17',
|
||||
'buildtype=debugoptimized',
|
||||
'wrap_mode=forcefallback'],
|
||||
version: '1.6.1',
|
||||
version: '1.6.2',
|
||||
meson_version:'>=0.54.0')
|
||||
|
||||
fast_float_dep = dependency('fast_float')
|
||||
|
||||
6
script/ci_install_lcov.sh
Executable file
6
script/ci_install_lcov.sh
Executable file
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
echo yes | cpan DateTime Capture::Tiny
|
||||
|
||||
wget -qO- https://github.com/linux-test-project/lcov/releases/download/v2.0/lcov-2.0.tar.gz | tar xvz
|
||||
(cd lcov-2.0 && make install)
|
||||
278
ssp.hpp
278
ssp.hpp
@@ -1,5 +1,6 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <charconv>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
@@ -625,6 +626,7 @@ using string_range = std::pair<const char*, const char*>;
|
||||
using split_data = std::vector<string_range>;
|
||||
|
||||
constexpr inline auto default_delimiter = ",";
|
||||
constexpr inline auto get_line_initial_buffer_size = 128;
|
||||
|
||||
template <bool StringError>
|
||||
inline void assert_string_error_defined() {
|
||||
@@ -639,59 +641,65 @@ inline void assert_throw_on_error_not_defined() {
|
||||
}
|
||||
|
||||
#if __unix__
|
||||
inline ssize_t get_line(char** lineptr, size_t* n, FILE* stream) {
|
||||
inline ssize_t get_line_file(char** lineptr, size_t* n, FILE* stream) {
|
||||
return getline(lineptr, n, stream);
|
||||
}
|
||||
#else
|
||||
using ssize_t = int64_t;
|
||||
inline ssize_t get_line(char** lineptr, size_t* n, FILE* stream) {
|
||||
size_t pos;
|
||||
int c;
|
||||
|
||||
if (lineptr == nullptr || stream == nullptr || n == nullptr) {
|
||||
using ssize_t = intptr_t;
|
||||
|
||||
ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) {
|
||||
if (lineptr == nullptr || n == nullptr || fp == nullptr) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
c = getc(stream);
|
||||
if (c == EOF) {
|
||||
return -1;
|
||||
}
|
||||
char buff[get_line_initial_buffer_size];
|
||||
|
||||
if (*lineptr == nullptr) {
|
||||
*lineptr = static_cast<char*>(malloc(128));
|
||||
if (*lineptr == nullptr) {
|
||||
if (*lineptr == nullptr || *n < sizeof(buff)) {
|
||||
size_t new_n = sizeof(buff);
|
||||
auto new_lineptr = static_cast<char*>(realloc(*lineptr, new_n));
|
||||
if (new_lineptr == nullptr) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
*n = 128;
|
||||
|
||||
*lineptr = new_lineptr;
|
||||
*n = new_n;
|
||||
}
|
||||
|
||||
pos = 0;
|
||||
while (c != EOF) {
|
||||
if (pos + 1 >= *n) {
|
||||
size_t new_size = *n + (*n >> 2);
|
||||
if (new_size < 128) {
|
||||
new_size = 128;
|
||||
}
|
||||
char* new_ptr = static_cast<char*>(
|
||||
realloc(static_cast<void*>(*lineptr), new_size));
|
||||
if (new_ptr == nullptr) {
|
||||
(*lineptr)[0] = '\0';
|
||||
|
||||
size_t line_used = 0;
|
||||
while (fgets(buff, sizeof(buff), fp) != nullptr) {
|
||||
line_used = strlen(*lineptr);
|
||||
size_t buff_used = strlen(buff);
|
||||
|
||||
if (*n <= buff_used + line_used) {
|
||||
size_t new_n = *n * 2;
|
||||
|
||||
auto new_lineptr = static_cast<char*>(realloc(*lineptr, new_n));
|
||||
if (new_lineptr == nullptr) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
*n = new_size;
|
||||
*lineptr = new_ptr;
|
||||
|
||||
*lineptr = new_lineptr;
|
||||
*n = new_n;
|
||||
}
|
||||
|
||||
(*lineptr)[pos++] = c;
|
||||
if (c == '\n') {
|
||||
break;
|
||||
memcpy(*lineptr + line_used, buff, buff_used);
|
||||
line_used += buff_used;
|
||||
(*lineptr)[line_used] = '\0';
|
||||
|
||||
if ((*lineptr)[line_used - 1] == '\n') {
|
||||
return line_used;
|
||||
}
|
||||
c = getc(stream);
|
||||
}
|
||||
|
||||
(*lineptr)[pos] = '\0';
|
||||
return pos;
|
||||
return (line_used != 0) ? line_used : -1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} /* ss */
|
||||
@@ -803,7 +811,7 @@ struct get_matcher<Matcher, T, Ts...> {
|
||||
struct is_matcher : is_instance_of_matcher<U, Matcher> {};
|
||||
|
||||
static_assert(count_v<is_matcher, T, Ts...> <= 1,
|
||||
"the same matcher cannot"
|
||||
"the same matcher cannot "
|
||||
"be defined multiple times");
|
||||
using type = std::conditional_t<is_matcher<T>::value, T,
|
||||
typename get_matcher<Matcher, Ts...>::type>;
|
||||
@@ -859,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
|
||||
@@ -2174,6 +2182,23 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
parser(const char* const csv_data_buffer, size_t csv_data_size,
|
||||
const std::string& delim = ss::default_delimiter)
|
||||
: file_name_{"buffer line"},
|
||||
reader_{csv_data_buffer, csv_data_size, delim} {
|
||||
if (csv_data_buffer) {
|
||||
read_line();
|
||||
if constexpr (ignore_header) {
|
||||
ignore_next();
|
||||
} else {
|
||||
raw_header_ = reader_.get_buffer();
|
||||
}
|
||||
} else {
|
||||
handle_error_null_buffer();
|
||||
eof_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
parser(parser&& other) = default;
|
||||
parser& operator=(parser&& other) = default;
|
||||
|
||||
@@ -2214,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;
|
||||
@@ -2281,7 +2310,7 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
if (header_.empty()) {
|
||||
if (header_.empty() && !eof()) {
|
||||
split_header_data();
|
||||
}
|
||||
|
||||
@@ -2477,20 +2506,22 @@ public:
|
||||
|
||||
template <typename U, typename... Us, typename Fun = none>
|
||||
void try_convert_and_invoke(std::optional<U>& value, Fun&& fun) {
|
||||
if (!parser_.valid()) {
|
||||
auto tuple_output = try_same<Us...>();
|
||||
if (!parser_.valid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if constexpr (!std::is_same_v<U, decltype(tuple_output)>) {
|
||||
value = to_object<U>(std::move(tuple_output));
|
||||
} else {
|
||||
value = std::move(tuple_output);
|
||||
}
|
||||
|
||||
parser_.try_invoke(*value, std::forward<Fun>(fun));
|
||||
if (parser_.valid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto tuple_output = try_same<Us...>();
|
||||
if (!parser_.valid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if constexpr (!std::is_same_v<U, decltype(tuple_output)>) {
|
||||
value = to_object<U>(std::move(tuple_output));
|
||||
} else {
|
||||
value = std::move(tuple_output);
|
||||
}
|
||||
|
||||
parser_.try_invoke(*value, std::forward<Fun>(fun));
|
||||
}
|
||||
|
||||
template <typename U, typename... Us>
|
||||
@@ -2638,6 +2669,19 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
void handle_error_null_buffer() {
|
||||
constexpr static auto error_msg = " received null data buffer";
|
||||
|
||||
if constexpr (string_error) {
|
||||
error_.clear();
|
||||
error_.append(file_name_).append(error_msg);
|
||||
} else if constexpr (throw_on_error) {
|
||||
throw ss::exception{file_name_ + error_msg};
|
||||
} else {
|
||||
error_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void handle_error_file_not_open() {
|
||||
constexpr static auto error_msg = " could not be opened";
|
||||
|
||||
@@ -2767,18 +2811,28 @@ private:
|
||||
: delim_{delim}, file_{fopen(file_name_.c_str(), "rb")} {
|
||||
}
|
||||
|
||||
reader(const char* const buffer, size_t csv_data_size,
|
||||
const std::string& delim)
|
||||
: delim_{delim}, csv_data_buffer_{buffer},
|
||||
csv_data_size_{csv_data_size} {
|
||||
}
|
||||
|
||||
reader(reader&& other)
|
||||
: buffer_{other.buffer_},
|
||||
next_line_buffer_{other.next_line_buffer_},
|
||||
helper_buffer_{other.helper_buffer_}, converter_{std::move(
|
||||
other.converter_)},
|
||||
helper_buffer_{other.helper_buffer_},
|
||||
converter_{std::move(other.converter_)},
|
||||
next_line_converter_{std::move(other.next_line_converter_)},
|
||||
buffer_size_{other.buffer_size_},
|
||||
next_line_buffer_size_{other.next_line_buffer_size_},
|
||||
helper_size_{other.helper_size_}, delim_{std::move(other.delim_)},
|
||||
file_{other.file_}, crlf_{other.crlf_},
|
||||
line_number_{other.line_number_}, next_line_size_{
|
||||
other.next_line_size_} {
|
||||
helper_buffer_size{other.helper_buffer_size},
|
||||
delim_{std::move(other.delim_)}, file_{other.file_},
|
||||
csv_data_buffer_{other.csv_data_buffer_},
|
||||
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;
|
||||
other.helper_buffer_ = nullptr;
|
||||
@@ -2794,17 +2848,22 @@ private:
|
||||
next_line_converter_ = std::move(other.next_line_converter_);
|
||||
buffer_size_ = other.buffer_size_;
|
||||
next_line_buffer_size_ = other.next_line_buffer_size_;
|
||||
helper_size_ = other.helper_size_;
|
||||
helper_buffer_size = other.helper_buffer_size;
|
||||
delim_ = std::move(other.delim_);
|
||||
file_ = other.file_;
|
||||
csv_data_buffer_ = other.csv_data_buffer_;
|
||||
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;
|
||||
other.helper_buffer_ = nullptr;
|
||||
other.file_ = nullptr;
|
||||
other.csv_data_buffer_ = nullptr;
|
||||
}
|
||||
|
||||
return *this;
|
||||
@@ -2824,6 +2883,55 @@ private:
|
||||
reader(const reader& other) = delete;
|
||||
reader& operator=(const reader& other) = delete;
|
||||
|
||||
ssize_t get_line_buffer(char** lineptr, size_t* n,
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
*lineptr = new_lineptr;
|
||||
*n = get_line_initial_buffer_size;
|
||||
}
|
||||
|
||||
size_t line_used = 0;
|
||||
while (curr_char <= csv_data_size) {
|
||||
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_n;
|
||||
*lineptr = new_lineptr;
|
||||
}
|
||||
|
||||
auto c = csv_data_buffer[curr_char++];
|
||||
(*lineptr)[line_used++] = c;
|
||||
if (c == '\n') {
|
||||
(*lineptr)[line_used] = '\0';
|
||||
return line_used;
|
||||
}
|
||||
}
|
||||
|
||||
return (line_used != 0) ? line_used : -1;
|
||||
}
|
||||
|
||||
// read next line each time in order to set eof_
|
||||
bool read_next() {
|
||||
next_line_converter_.clear_error();
|
||||
@@ -2834,10 +2942,23 @@ private:
|
||||
if (next_line_buffer_size_ > 0) {
|
||||
next_line_buffer_[0] = '\0';
|
||||
}
|
||||
ssize = get_line(&next_line_buffer_, &next_line_buffer_size_,
|
||||
file_);
|
||||
|
||||
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_,
|
||||
csv_data_buffer_, csv_data_size_,
|
||||
curr_char_);
|
||||
}
|
||||
|
||||
if (ssize == -1) {
|
||||
if (errno == ENOMEM) {
|
||||
throw std::bad_alloc{};
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2947,6 +3068,10 @@ private:
|
||||
}
|
||||
|
||||
size_t remove_eol(char*& buffer, size_t ssize) {
|
||||
if (buffer[ssize - 1] != '\n') {
|
||||
return ssize;
|
||||
}
|
||||
|
||||
size_t size = ssize - 1;
|
||||
if (ssize >= 2 && buffer[ssize - 2] == '\r') {
|
||||
crlf_ = true;
|
||||
@@ -2960,12 +3085,12 @@ private:
|
||||
}
|
||||
|
||||
void realloc_concat(char*& first, size_t& first_size,
|
||||
const char* const second, size_t second_size) {
|
||||
// TODO make buffer_size an argument
|
||||
next_line_buffer_size_ = first_size + second_size + 3;
|
||||
size_t& buffer_size, const char* const second,
|
||||
size_t second_size) {
|
||||
buffer_size = first_size + second_size + 3;
|
||||
auto new_first = static_cast<char*>(
|
||||
realloc(static_cast<void*>(first), next_line_buffer_size_));
|
||||
if (!first) {
|
||||
realloc(static_cast<void*>(first), buffer_size));
|
||||
if (!new_first) {
|
||||
throw std::bad_alloc{};
|
||||
}
|
||||
|
||||
@@ -2977,15 +3102,25 @@ private:
|
||||
bool append_next_line_to_buffer(char*& buffer, size_t& size) {
|
||||
undo_remove_eol(buffer, size);
|
||||
|
||||
ssize_t next_ssize =
|
||||
get_line(&helper_buffer_, &helper_size_, file_);
|
||||
ssize_t next_ssize;
|
||||
if (file_) {
|
||||
next_ssize =
|
||||
get_line_file(&helper_buffer_, &helper_buffer_size, file_);
|
||||
} else {
|
||||
next_ssize =
|
||||
get_line_buffer(&helper_buffer_, &helper_buffer_size,
|
||||
csv_data_buffer_, csv_data_size_,
|
||||
curr_char_);
|
||||
}
|
||||
|
||||
if (next_ssize == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
++line_number_;
|
||||
size_t next_size = remove_eol(helper_buffer_, next_ssize);
|
||||
realloc_concat(buffer, size, helper_buffer_, next_size);
|
||||
realloc_concat(buffer, size, next_line_buffer_size_, helper_buffer_,
|
||||
next_size);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -3005,13 +3140,18 @@ private:
|
||||
|
||||
size_t buffer_size_{0};
|
||||
size_t next_line_buffer_size_{0};
|
||||
size_t helper_size_{0};
|
||||
size_t helper_buffer_size{0};
|
||||
|
||||
std::string delim_;
|
||||
FILE* file_{nullptr};
|
||||
|
||||
const char* csv_data_buffer_{nullptr};
|
||||
size_t csv_data_size_{0};
|
||||
size_t curr_char_{0};
|
||||
|
||||
bool crlf_{false};
|
||||
size_t line_number_{0};
|
||||
size_t chars_read_{0};
|
||||
|
||||
size_t next_line_size_{0};
|
||||
};
|
||||
|
||||
@@ -32,9 +32,11 @@ set(DOCTEST "${FETCHCONTENT_BASE_DIR}/doctest-src")
|
||||
|
||||
enable_testing()
|
||||
|
||||
foreach(name IN ITEMS test_splitter test_parser test_converter test_extractions
|
||||
test_parser2_1 test_parser2_2 test_parser2_3
|
||||
test_parser2_4 test_extractions_without_fast_float)
|
||||
foreach(name IN ITEMS test_splitter test_parser1_1 test_parser1_2
|
||||
test_parser1_3 test_parser1_4 test_converter
|
||||
test_extractions test_parser2_1 test_parser2_2
|
||||
test_parser2_3 test_parser2_4 test_parser2_5
|
||||
test_parser2_6 test_extractions_without_fast_float)
|
||||
add_executable("${name}" "${name}.cpp")
|
||||
target_link_libraries("${name}" PRIVATE ssp::ssp fast_float
|
||||
doctest::doctest)
|
||||
|
||||
@@ -2,7 +2,10 @@ doctest_dep = dependency('doctest')
|
||||
add_project_arguments('-DDOCTEST_CONFIG_IMPLEMENT_WITH_MAIN', language: 'cpp')
|
||||
|
||||
tests = [
|
||||
'parser',
|
||||
'parser1_1',
|
||||
'parser1_2',
|
||||
'parser1_3',
|
||||
'parser1_4',
|
||||
'splitter',
|
||||
'converter',
|
||||
'extractions',
|
||||
@@ -10,6 +13,8 @@ tests = [
|
||||
'parser2_2',
|
||||
'parser2_3',
|
||||
'parser2_4',
|
||||
'parser2_5',
|
||||
'parser2_6',
|
||||
'extractions_without_fast_float',
|
||||
]
|
||||
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
#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>
|
||||
@@ -12,7 +16,30 @@
|
||||
#include <doctest.h>
|
||||
#endif
|
||||
|
||||
namespace ss {
|
||||
template <typename... Ts>
|
||||
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_;
|
||||
|
||||
@@ -34,7 +61,8 @@ struct buffer {
|
||||
|
||||
[[maybe_unused]] inline buffer buff;
|
||||
|
||||
std::string time_now_rand() {
|
||||
[[maybe_unused]] std::string time_now_rand() {
|
||||
srand(time(nullptr));
|
||||
std::stringstream ss;
|
||||
auto t = std::time(nullptr);
|
||||
auto tm = *std::localtime(&t);
|
||||
@@ -46,15 +74,21 @@ std::string time_now_rand() {
|
||||
struct unique_file_name {
|
||||
static inline int i = 0;
|
||||
|
||||
const std::string name;
|
||||
std::string name;
|
||||
|
||||
unique_file_name(const std::string& test)
|
||||
: name{"random_" + test + "_" + std::to_string(i++) + "_" +
|
||||
time_now_rand() + "_file.csv"} {
|
||||
unique_file_name(const std::string& test) {
|
||||
do {
|
||||
name = "ssp_test_" + test + "_" + std::to_string(i++) +
|
||||
"_" + time_now_rand() + "_file.csv";
|
||||
} while (std::filesystem::exists(name));
|
||||
}
|
||||
|
||||
~unique_file_name() {
|
||||
std::filesystem::remove(name);
|
||||
try {
|
||||
std::filesystem::remove(name);
|
||||
} catch (const std::filesystem::filesystem_error& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -112,8 +146,8 @@ struct unique_file_name {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::vector<std::vector<T>> vector_combinations(const std::vector<T>& v,
|
||||
size_t n) {
|
||||
[[maybe_unused]] std::vector<std::vector<T>> vector_combinations(
|
||||
const std::vector<T>& v, size_t n) {
|
||||
std::vector<std::vector<T>> ret;
|
||||
if (n <= 1) {
|
||||
for (const auto& i : v) {
|
||||
@@ -126,9 +160,68 @@ std::vector<std::vector<T>> vector_combinations(const std::vector<T>& v,
|
||||
for (const auto& i : v) {
|
||||
for (auto j : inner_combinations) {
|
||||
j.insert(j.begin(), i);
|
||||
ret.push_back(move(j));
|
||||
ret.push_back(std::move(j));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
[[maybe_unused]] std::string make_buffer(const std::string& file_name) {
|
||||
std::ifstream in{file_name, std::ios::binary};
|
||||
std::string tmp;
|
||||
std::string out;
|
||||
|
||||
auto copy_if_whitespaces = [&] {
|
||||
std::string matches = "\n\r\t ";
|
||||
while (std::any_of(matches.begin(), matches.end(),
|
||||
[&](auto c) { return in.peek() == c; })) {
|
||||
if (in.peek() == '\r') {
|
||||
out += "\r\n";
|
||||
in.ignore(2);
|
||||
} else {
|
||||
out += std::string{static_cast<char>(in.peek())};
|
||||
in.ignore(1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
out.reserve(sizeof(out) + 1);
|
||||
|
||||
copy_if_whitespaces();
|
||||
while (in >> tmp) {
|
||||
out += tmp;
|
||||
copy_if_whitespaces();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template <bool buffer_mode, typename... Ts>
|
||||
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);
|
||||
return {ss::parser<Ts...>{buffer.data(), buffer.size(), delim},
|
||||
std::move(buffer)};
|
||||
} else {
|
||||
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 */
|
||||
|
||||
1723
test/test_parser.cpp
1723
test/test_parser.cpp
File diff suppressed because it is too large
Load Diff
112
test/test_parser1.hpp
Normal file
112
test/test_parser1.hpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#pragma once
|
||||
|
||||
#include "test_helpers.hpp"
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <ss/parser.hpp>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace {
|
||||
[[maybe_unused]] void replace_all(std::string& s, const std::string& from,
|
||||
const std::string& to) {
|
||||
if (from.empty()) return;
|
||||
size_t start_pos = 0;
|
||||
while ((start_pos = s.find(from, start_pos)) != std::string::npos) {
|
||||
s.replace(start_pos, from.length(), to);
|
||||
start_pos += to.length();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
void expect_error_on_command(ss::parser<Ts...>& p,
|
||||
const std::function<void()> command) {
|
||||
if (ss::setup<Ts...>::throw_on_error) {
|
||||
try {
|
||||
command();
|
||||
} catch (const std::exception& e) {
|
||||
CHECK_FALSE(std::string{e.what()}.empty());
|
||||
}
|
||||
} else {
|
||||
command();
|
||||
CHECK(!p.valid());
|
||||
if constexpr (ss::setup<Ts...>::string_error) {
|
||||
CHECK_FALSE(p.error_msg().empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[[maybe_unused]] void update_if_crlf(std::string& s) {
|
||||
#ifdef _WIN32
|
||||
replace_all(s, "\r\n", "\n");
|
||||
#else
|
||||
(void)(s);
|
||||
#endif
|
||||
}
|
||||
|
||||
struct X {
|
||||
constexpr static auto delim = ",";
|
||||
constexpr static auto empty = "_EMPTY_";
|
||||
int i;
|
||||
double d;
|
||||
std::string s;
|
||||
|
||||
std::string to_string() const {
|
||||
if (s == empty) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return std::to_string(i)
|
||||
.append(delim)
|
||||
.append(std::to_string(d))
|
||||
.append(delim)
|
||||
.append(s);
|
||||
}
|
||||
auto tied() const {
|
||||
return std::tie(i, d, s);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<ss::has_m_tied_t<T>, bool> operator==(const T& lhs,
|
||||
const T& rhs) {
|
||||
return lhs.tied() == rhs.tied();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void make_and_write(const std::string& file_name,
|
||||
const std::vector<T>& data,
|
||||
const std::vector<std::string>& header = {},
|
||||
bool new_line_eof = true) {
|
||||
std::ofstream out{file_name};
|
||||
|
||||
#ifdef _WIN32
|
||||
std::vector<const char*> new_lines = {"\n"};
|
||||
#else
|
||||
std::vector<const char*> new_lines = {"\n", "\r\n"};
|
||||
#endif
|
||||
|
||||
for (const auto& i : header) {
|
||||
if (&i != &header.front()) {
|
||||
out << T::delim;
|
||||
}
|
||||
out << i;
|
||||
}
|
||||
|
||||
if (!header.empty()) {
|
||||
out << new_lines.front();
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < data.size(); ++i) {
|
||||
out << data[i].to_string();
|
||||
if (new_line_eof || i + 1 < data.size()) {
|
||||
out << new_lines[i % new_lines.size()];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
674
test/test_parser1_1.cpp
Normal file
674
test/test_parser1_1.cpp
Normal file
@@ -0,0 +1,674 @@
|
||||
#include "test_parser1.hpp"
|
||||
|
||||
TEST_CASE("test file not found") {
|
||||
unique_file_name f{"file_not_found"};
|
||||
|
||||
{
|
||||
ss::parser p{f.name, ","};
|
||||
CHECK_FALSE(p.valid());
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser<ss::string_error> p{f.name, ","};
|
||||
CHECK_FALSE(p.valid());
|
||||
CHECK_FALSE(p.error_msg().empty());
|
||||
}
|
||||
|
||||
try {
|
||||
ss::parser<ss::throw_on_error> p{f.name, ","};
|
||||
FAIL("Expected exception...");
|
||||
} catch (const std::exception& e) {
|
||||
CHECK_FALSE(std::string{e.what()}.empty());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("test null buffer") {
|
||||
{
|
||||
ss::parser p{nullptr, 10, ","};
|
||||
CHECK_FALSE(p.valid());
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser<ss::string_error> p{nullptr, 10, ","};
|
||||
CHECK_FALSE(p.valid());
|
||||
CHECK_FALSE(p.error_msg().empty());
|
||||
}
|
||||
|
||||
try {
|
||||
ss::parser<ss::throw_on_error> p{nullptr, 10, ","};
|
||||
FAIL("Expected exception...");
|
||||
} catch (const std::exception& e) {
|
||||
CHECK_FALSE(std::string{e.what()}.empty());
|
||||
}
|
||||
}
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
||||
|
||||
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"},
|
||||
{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, ErrorMode>(f.name, ",");
|
||||
ss::parser p0{std::move(p)};
|
||||
p = std::move(p0);
|
||||
std::vector<X> i;
|
||||
|
||||
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i2;
|
||||
|
||||
auto move_rotate = [&p = p, &p0 = p0] {
|
||||
auto p1 = std::move(p);
|
||||
p0 = std::move(p1);
|
||||
p = std::move(p0);
|
||||
};
|
||||
|
||||
while (!p.eof()) {
|
||||
move_rotate();
|
||||
auto a = p.template get_next<int, double, std::string>();
|
||||
i.emplace_back(ss::to_object<X>(a));
|
||||
}
|
||||
|
||||
for (const auto& a : p2.template iterate<int, double, std::string>()) {
|
||||
i2.emplace_back(ss::to_object<X>(a));
|
||||
}
|
||||
|
||||
CHECK_EQ(i, data);
|
||||
CHECK_EQ(i2, data);
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
|
||||
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i2;
|
||||
|
||||
auto [p3, ___] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i3;
|
||||
|
||||
std::vector<X> expected = {std::begin(data) + 1, std::end(data)};
|
||||
using tup = std::tuple<int, double, std::string>;
|
||||
|
||||
p.ignore_next();
|
||||
while (!p.eof()) {
|
||||
auto a = p.template get_next<tup>();
|
||||
i.emplace_back(ss::to_object<X>(a));
|
||||
}
|
||||
|
||||
p2.ignore_next();
|
||||
for (const auto& a : p2.template iterate<tup>()) {
|
||||
i2.emplace_back(ss::to_object<X>(a));
|
||||
}
|
||||
|
||||
p3.ignore_next();
|
||||
for (auto it = p3.template iterate<tup>().begin();
|
||||
it != p3.template iterate<tup>().end(); ++it) {
|
||||
i3.emplace_back(ss::to_object<X>(*it));
|
||||
}
|
||||
|
||||
CHECK_EQ(i, expected);
|
||||
CHECK_EQ(i2, expected);
|
||||
CHECK_EQ(i3, expected);
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i2;
|
||||
|
||||
while (!p.eof()) {
|
||||
i.push_back(p.template get_object<X, int, double, std::string>());
|
||||
}
|
||||
|
||||
for (auto&& a :
|
||||
p2.template iterate_object<X, int, double, std::string>()) {
|
||||
i2.push_back(std::move(a));
|
||||
}
|
||||
|
||||
CHECK_EQ(i, data);
|
||||
CHECK_EQ(i2, data);
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
|
||||
for (auto&& a :
|
||||
p.template iterate_object<X, int, double, std::string>()) {
|
||||
i.push_back(std::move(a));
|
||||
}
|
||||
|
||||
CHECK_EQ(i, data);
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
|
||||
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i2;
|
||||
|
||||
using tup = std::tuple<int, double, std::string>;
|
||||
while (!p.eof()) {
|
||||
i.push_back(p.template get_object<X, tup>());
|
||||
}
|
||||
|
||||
for (auto it = p2.template iterate_object<X, tup>().begin();
|
||||
it != p2.template iterate_object<X, tup>().end(); it++) {
|
||||
i2.push_back({it->i, it->d, it->s});
|
||||
}
|
||||
|
||||
CHECK_EQ(i, data);
|
||||
CHECK_EQ(i2, data);
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
|
||||
using tup = std::tuple<int, double, std::string>;
|
||||
for (auto&& a : p.template iterate_object<X, tup>()) {
|
||||
i.push_back(std::move(a));
|
||||
}
|
||||
|
||||
CHECK_EQ(i, data);
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
i.push_back(p.template get_next<X>());
|
||||
}
|
||||
|
||||
CHECK_EQ(i, data);
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
|
||||
for (auto&& a : p.template iterate<X>()) {
|
||||
i.push_back(std::move(a));
|
||||
}
|
||||
|
||||
CHECK_EQ(i, data);
|
||||
}
|
||||
|
||||
{
|
||||
constexpr int excluded = 3;
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
|
||||
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i2;
|
||||
|
||||
while (!p.eof()) {
|
||||
try {
|
||||
auto a = p.template get_object<X, ss::ax<int, excluded>, double,
|
||||
std::string>();
|
||||
if (p.valid()) {
|
||||
i.push_back(a);
|
||||
}
|
||||
} catch (...) {
|
||||
// ignore
|
||||
};
|
||||
}
|
||||
|
||||
if (!T::ThrowOnError) {
|
||||
for (auto&& a : p2.template iterate_object<X, ss::ax<int, excluded>,
|
||||
double, std::string>()) {
|
||||
if (p2.valid()) {
|
||||
i2.push_back(std::move(a));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<X> expected;
|
||||
for (auto& x : data) {
|
||||
if (x.i != excluded) {
|
||||
expected.push_back(x);
|
||||
}
|
||||
}
|
||||
|
||||
std::copy_if(data.begin(), data.end(), expected.begin(),
|
||||
[&](const X& x) { return x.i != excluded; });
|
||||
CHECK_EQ(i, expected);
|
||||
|
||||
if (!T::ThrowOnError) {
|
||||
CHECK_EQ(i2, expected);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
|
||||
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i2;
|
||||
|
||||
while (!p.eof()) {
|
||||
try {
|
||||
auto a = p.template get_object<X, ss::nx<int, 3>, double,
|
||||
std::string>();
|
||||
if (p.valid()) {
|
||||
i.push_back(a);
|
||||
}
|
||||
} catch (...) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
if (!T::ThrowOnError) {
|
||||
for (auto&& a : p2.template iterate_object<X, ss::nx<int, 3>,
|
||||
double, std::string>()) {
|
||||
if (p2.valid()) {
|
||||
i2.push_back(std::move(a));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<X> expected = {{3, 4, "y"}};
|
||||
CHECK_EQ(i, expected);
|
||||
if (!T::ThrowOnError) {
|
||||
CHECK_EQ(i2, expected);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
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, ErrorMode>(empty_f.name, ",");
|
||||
std::vector<X> i;
|
||||
|
||||
auto [p2, __] = make_parser<buffer_mode, ErrorMode>(empty_f.name, ",");
|
||||
std::vector<X> i2;
|
||||
|
||||
while (!p.eof()) {
|
||||
i.push_back(p.template get_next<X>());
|
||||
}
|
||||
|
||||
for (auto&& a : p2.template iterate<X>()) {
|
||||
i2.push_back(std::move(a));
|
||||
}
|
||||
|
||||
CHECK(i.empty());
|
||||
CHECK(i2.empty());
|
||||
}
|
||||
}
|
||||
|
||||
using test_tuple = std::tuple<double, char, double>;
|
||||
struct test_struct {
|
||||
int i;
|
||||
double d;
|
||||
char c;
|
||||
auto tied() {
|
||||
return std::tie(i, d, c);
|
||||
}
|
||||
};
|
||||
|
||||
static inline void expect_test_struct(const test_struct&) {
|
||||
}
|
||||
|
||||
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 :
|
||||
{"10,a,11.1", "10,20,11.1", "junk", "10,11.1", "1,11.1,a", "junk",
|
||||
"10,junk", "11,junk", "10,11.1,c", "10,20", "10,22.2,f"}) {
|
||||
out << i << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
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 = [] {};
|
||||
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE_FALSE(p.eof());
|
||||
|
||||
{
|
||||
constexpr static auto expectedData = std::tuple{10, 'a', 11.1};
|
||||
|
||||
auto [d1, d2, d3, d4] =
|
||||
p.template try_next<int, int, double>(fail)
|
||||
.template or_else<test_struct>(fail)
|
||||
.template or_else<int, char, double>(
|
||||
[&](auto&& data) { CHECK_EQ(data, expectedData); })
|
||||
.on_error(fail)
|
||||
.template or_else<test_tuple>(fail)
|
||||
.values();
|
||||
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE_FALSE(d1);
|
||||
REQUIRE_FALSE(d2);
|
||||
REQUIRE(d3);
|
||||
REQUIRE_FALSE(d4);
|
||||
CHECK_EQ(*d3, expectedData);
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
constexpr static auto expectedData = std::tuple{10, 20, 11.1};
|
||||
|
||||
auto [d1, d2, d3, d4] =
|
||||
p.template try_next<int, int, double>(
|
||||
[&](auto& i1, auto i2, double d) {
|
||||
CHECK_EQ(std::tie(i1, i2, d), expectedData);
|
||||
})
|
||||
.on_error(fail)
|
||||
.template or_object<test_struct, int, double, char>(fail)
|
||||
.on_error(fail)
|
||||
.template or_else<test_tuple>(fail)
|
||||
.on_error(fail)
|
||||
.template or_else<int, char, double>(fail)
|
||||
.values();
|
||||
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE(d1);
|
||||
REQUIRE_FALSE(d2);
|
||||
REQUIRE_FALSE(d3);
|
||||
REQUIRE_FALSE(d4);
|
||||
CHECK_EQ(*d1, expectedData);
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
|
||||
auto [d1, d2, d3, d4, d5] =
|
||||
p.template try_object<test_struct, int, double, char>(fail)
|
||||
.on_error(expect_error)
|
||||
.template or_else<int, char, char>(fail)
|
||||
.template or_else<test_struct>(fail)
|
||||
.template or_else<test_tuple>(fail)
|
||||
.template or_else<int, char, double>(fail)
|
||||
.values();
|
||||
|
||||
REQUIRE_FALSE(p.valid());
|
||||
REQUIRE_FALSE(d1);
|
||||
REQUIRE_FALSE(d2);
|
||||
REQUIRE_FALSE(d3);
|
||||
REQUIRE_FALSE(d4);
|
||||
REQUIRE_FALSE(d5);
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
|
||||
auto [d1, d2] =
|
||||
p.template try_next<int, double>([](auto& i, auto& d) {
|
||||
REQUIRE_EQ(std::tie(i, d), std::tuple{10, 11.1});
|
||||
})
|
||||
.template or_else<int, double>([](auto&, auto&) { FAIL(""); })
|
||||
.values();
|
||||
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE(d1);
|
||||
REQUIRE_FALSE(d2);
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
|
||||
auto [d1, d2] =
|
||||
p.template try_next<int, double>([](auto&, auto&) { FAIL(""); })
|
||||
.template or_else<test_struct>(expect_test_struct)
|
||||
.values();
|
||||
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE_FALSE(d1);
|
||||
REQUIRE(d2);
|
||||
CHECK_EQ(d2->tied(), std::tuple{1, 11.1, 'a'});
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
|
||||
auto [d1, d2, d3, d4, d5] =
|
||||
p.template try_next<int, int, double>(fail)
|
||||
.template or_object<test_struct, int, double, char>()
|
||||
.template or_else<test_struct>(expect_test_struct)
|
||||
.template or_else<test_tuple>(fail)
|
||||
.template or_else<std::tuple<int, double>>(fail)
|
||||
.on_error(ignore_error)
|
||||
.on_error(expect_error)
|
||||
.values();
|
||||
|
||||
REQUIRE_FALSE(p.valid());
|
||||
REQUIRE_FALSE(d1);
|
||||
REQUIRE_FALSE(d2);
|
||||
REQUIRE_FALSE(d3);
|
||||
REQUIRE_FALSE(d4);
|
||||
REQUIRE_FALSE(d5);
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
|
||||
auto [d1, d2] =
|
||||
p.template try_next<int, std::optional<int>>()
|
||||
.on_error(ignore_error)
|
||||
.on_error(fail)
|
||||
.template or_else<std::tuple<int, std::string>>(fail)
|
||||
.on_error(ignore_error)
|
||||
.on_error(fail)
|
||||
.on_error(ignore_error)
|
||||
.values();
|
||||
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE(d1);
|
||||
REQUIRE_FALSE(d2);
|
||||
CHECK_EQ(*d1, std::tuple{10, std::nullopt});
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE_FALSE(p.eof());
|
||||
|
||||
auto [d1, d2] =
|
||||
p.template try_next<int, std::variant<int, std::string>>()
|
||||
.on_error(fail)
|
||||
.template or_else<std::tuple<int, std::string>>(fail)
|
||||
.on_error(fail)
|
||||
.values();
|
||||
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE(d1);
|
||||
REQUIRE_FALSE(d2);
|
||||
CHECK_EQ(*d1, std::tuple{11, std::variant<int, std::string>{"junk"}});
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
|
||||
auto [d1, d2] = p.template try_object<test_struct, int, double, char>()
|
||||
.template or_else<int>(fail)
|
||||
.values();
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE(d1);
|
||||
REQUIRE_FALSE(d2);
|
||||
CHECK_EQ(d1->tied(), std::tuple{10, 11.1, 'c'});
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE_FALSE(p.eof());
|
||||
|
||||
auto [d1, d2, d3, d4] =
|
||||
p.template try_next<int, int>([] { return false; })
|
||||
.template or_else<int, double>([](auto&) { return false; })
|
||||
.template or_else<int, int>()
|
||||
.template or_else<int, int>(fail)
|
||||
.values();
|
||||
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE_FALSE(d1);
|
||||
REQUIRE_FALSE(d2);
|
||||
REQUIRE(d3);
|
||||
REQUIRE_FALSE(d4);
|
||||
CHECK_EQ(d3.value(), std::tuple{10, 20});
|
||||
}
|
||||
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
|
||||
auto [d1, d2, d3, d4] =
|
||||
p.template try_object<test_struct, int, double, char>(
|
||||
[] { return false; })
|
||||
.template or_else<int, double>([](auto&) { return false; })
|
||||
.template or_object<test_struct, int, double, char>()
|
||||
.template or_else<int, int>(fail)
|
||||
.values();
|
||||
|
||||
REQUIRE(p.valid());
|
||||
REQUIRE_FALSE(d1);
|
||||
REQUIRE_FALSE(d2);
|
||||
REQUIRE(d3);
|
||||
REQUIRE_FALSE(d4);
|
||||
CHECK_EQ(d3->tied(), std::tuple{10, 22.2, 'f'});
|
||||
}
|
||||
|
||||
CHECK(p.eof());
|
||||
}
|
||||
|
||||
template <bool buffer_mode, typename... Ts>
|
||||
void test_no_new_line_at_eof_impl(const std::vector<X>& data) {
|
||||
unique_file_name f{"no_new_line_at_eof"};
|
||||
make_and_write(f.name, data, {}, false);
|
||||
|
||||
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name);
|
||||
std::vector<X> parsed_data;
|
||||
|
||||
for (const auto& el : p.template iterate<X>()) {
|
||||
parsed_data.push_back(el);
|
||||
}
|
||||
|
||||
CHECK_EQ(data, parsed_data);
|
||||
}
|
||||
|
||||
template <bool buffer_mode, typename... Ts>
|
||||
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, 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, 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, Ts...>(
|
||||
{{1, 2, std::string(i, 'X')}, {3, 4, std::string(j, 'Y')}});
|
||||
|
||||
test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
|
||||
{{1, 2, std::string(j, 'X')}, {3, 4, std::string(i, 'Y')}});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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>();
|
||||
}
|
||||
262
test/test_parser1_2.cpp
Normal file
262
test/test_parser1_2.cpp
Normal file
@@ -0,0 +1,262 @@
|
||||
#include "test_parser1.hpp"
|
||||
|
||||
struct my_string {
|
||||
char* data{nullptr};
|
||||
|
||||
my_string() = default;
|
||||
|
||||
~my_string() {
|
||||
delete[] data;
|
||||
}
|
||||
|
||||
// make sure no object is copied
|
||||
my_string(const my_string&) = delete;
|
||||
my_string& operator=(const my_string&) = delete;
|
||||
|
||||
my_string(my_string&& other) : data{other.data} {
|
||||
other.data = nullptr;
|
||||
}
|
||||
|
||||
my_string& operator=(my_string&& other) {
|
||||
data = other.data;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
inline bool ss::extract(const char* begin, const char* end, my_string& s) {
|
||||
size_t size = end - begin;
|
||||
s.data = new char[size + 1];
|
||||
strncpy(s.data, begin, size);
|
||||
s.data[size] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
struct xyz {
|
||||
my_string x;
|
||||
my_string y;
|
||||
my_string z;
|
||||
auto tied() {
|
||||
return std::tie(x, y, z);
|
||||
}
|
||||
};
|
||||
|
||||
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, ErrorMode>("", "");
|
||||
p.template try_next<my_string, my_string, my_string>()
|
||||
.template or_else<my_string, my_string, my_string, my_string>(
|
||||
[](auto&&) {})
|
||||
.template or_else<my_string>([](auto&) {})
|
||||
.template or_else<xyz>([](auto&&) {})
|
||||
.template or_object<xyz, my_string, my_string, my_string>([](auto&&) {})
|
||||
.template or_else<std::tuple<my_string, my_string, my_string>>(
|
||||
[](auto&, auto&, auto&) {});
|
||||
}
|
||||
|
||||
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<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,
|
||||
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<BufferMode::value, ss::throw_on_error>(f.name, ",");
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
static inline std::string no_quote(const std::string& s) {
|
||||
if (!s.empty() && s[0] == '"') {
|
||||
return {std::next(begin(s)), std::prev(end(s))};
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
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\""},
|
||||
{7, 8, "\"u\"\"\""},
|
||||
{9, 10, "v"},
|
||||
{11, 12, "\"w\n\""}};
|
||||
for (auto& [_, __, s] : data) {
|
||||
update_if_crlf(s);
|
||||
}
|
||||
|
||||
make_and_write(f.name, data);
|
||||
for (auto& [_, __, s] : data) {
|
||||
s = no_quote(s);
|
||||
if (s[0] == 'u') {
|
||||
s = "u\"";
|
||||
}
|
||||
}
|
||||
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline,
|
||||
ss::quote<'"'>>(f.name, ",");
|
||||
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
auto a = p.template get_next<int, double, std::string>();
|
||||
i.emplace_back(ss::to_object<X>(a));
|
||||
}
|
||||
|
||||
for (auto& [_, __, s] : i) {
|
||||
update_if_crlf(s);
|
||||
}
|
||||
CHECK_EQ(i, data);
|
||||
|
||||
auto [p_no_multiline, __] =
|
||||
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>();
|
||||
};
|
||||
expect_error_on_command(p_no_multiline, command);
|
||||
}
|
||||
}
|
||||
|
||||
static inline std::string no_escape(std::string& s) {
|
||||
s.erase(std::remove(begin(s), end(s), '\\'), end(s));
|
||||
return s;
|
||||
}
|
||||
|
||||
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"},
|
||||
{3, 4, "y\\\ny\\\ny"},
|
||||
{9, 10, "v\\\\"},
|
||||
{11, 12, "w\\\n"}};
|
||||
for (auto& [_, __, s] : data) {
|
||||
update_if_crlf(s);
|
||||
}
|
||||
|
||||
make_and_write(f.name, data);
|
||||
for (auto& [_, __, s] : data) {
|
||||
s = no_escape(s);
|
||||
if (s == "v") {
|
||||
s = "v\\";
|
||||
}
|
||||
}
|
||||
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline,
|
||||
ss::escape<'\\'>>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
auto a = p.template get_next<int, double, std::string>();
|
||||
i.emplace_back(ss::to_object<X>(a));
|
||||
}
|
||||
|
||||
for (auto& [_, __, s] : i) {
|
||||
update_if_crlf(s);
|
||||
}
|
||||
CHECK_EQ(i, data);
|
||||
|
||||
auto [p_no_multiline, __] =
|
||||
make_parser<buffer_mode, ErrorMode, ss::escape<'\\'>>(f.name, ",");
|
||||
while (!p.eof()) {
|
||||
auto command = [&p_no_multiline = p_no_multiline] {
|
||||
auto a =
|
||||
p_no_multiline.template get_next<int, double, std::string>();
|
||||
};
|
||||
expect_error_on_command(p_no_multiline, command);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_TEMPLATE("test quote escape multiline", T, ParserOptionCombinations) {
|
||||
constexpr auto buffer_mode = T::BufferMode::value;
|
||||
using ErrorMode = typename T::ErrorMode;
|
||||
|
||||
unique_file_name f{"quote_escape_multiline"};
|
||||
{
|
||||
std::ofstream out{f.name};
|
||||
out << "1,2,\"just\\\n\nstrings\"" << std::endl;
|
||||
#ifndef _WIN32
|
||||
out << "3,4,\"just\r\nsome\\\r\n\n\\\nstrings\"" << std::endl;
|
||||
out << "5,6,\"just\\\n\\\r\n\r\n\nstrings" << std::endl;
|
||||
#else
|
||||
out << "3,4,\"just\nsome\\\n\n\\\nstrings\"" << std::endl;
|
||||
out << "5,6,\"just\\\n\\\n\n\nstrings" << std::endl;
|
||||
#endif
|
||||
out << "7,8,\"just strings\"" << std::endl;
|
||||
out << "9,10,just strings" << std::endl;
|
||||
}
|
||||
size_t bad_lines = 1;
|
||||
auto num_errors = 0;
|
||||
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline,
|
||||
ss::escape<'\\'>, ss::quote<'"'>>(f.name);
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
try {
|
||||
auto a = p.template get_next<int, double, std::string>();
|
||||
if (p.valid()) {
|
||||
i.emplace_back(ss::to_object<X>(a));
|
||||
} else {
|
||||
++num_errors;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
++num_errors;
|
||||
}
|
||||
}
|
||||
|
||||
CHECK(bad_lines == num_errors);
|
||||
|
||||
std::vector<X> data = {{1, 2, "just\n\nstrings"},
|
||||
#ifndef _WIN32
|
||||
{3, 4, "just\r\nsome\r\n\n\nstrings"},
|
||||
#else
|
||||
{3, 4, "just\nsome\n\n\nstrings"},
|
||||
#endif
|
||||
{9, 10, "just strings"}};
|
||||
|
||||
for (auto& [_, __, s] : i) {
|
||||
update_if_crlf(s);
|
||||
}
|
||||
CHECK_EQ(i, data);
|
||||
}
|
||||
313
test/test_parser1_3.cpp
Normal file
313
test/test_parser1_3.cpp
Normal file
@@ -0,0 +1,313 @@
|
||||
#include "test_parser1.hpp"
|
||||
|
||||
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;
|
||||
#ifndef _WIN32
|
||||
out << "3,4,\"ju\n\r\n\nnk\"" << std::endl;
|
||||
out << "5,6,just\\\n\\\r\nstrings" << std::endl;
|
||||
#else
|
||||
out << "3,4,\"ju\n\n\nnk\"" << std::endl;
|
||||
out << "5,6,just\\\n\\\nstrings" << std::endl;
|
||||
#endif
|
||||
out << "7,8,ju\\\n\\\n\\\nnk" << std::endl;
|
||||
out << "9,10,\"just\\\n\nstrings\"" << std::endl;
|
||||
out << "11,12,\"ju\\\n|\n\n\n\n\nk\"" << std::endl;
|
||||
out << "13,14,\"ju\\\n\\\n15,16\"\\\n\\\\\n\nnk\"" << std::endl;
|
||||
out << "17,18,\"ju\\\n\\\n\\\n\\\\\n\nnk\"" << std::endl;
|
||||
out << "19,20,just strings" << std::endl;
|
||||
}
|
||||
auto bad_lines = 15;
|
||||
auto num_errors = 0;
|
||||
|
||||
auto [p, _] =
|
||||
make_parser<buffer_mode, ErrorMode, ss::multiline_restricted<2>,
|
||||
ss::quote<'"'>, ss::escape<'\\'>>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
try {
|
||||
auto a = p.template get_next<int, double, std::string>();
|
||||
if (p.valid()) {
|
||||
i.emplace_back(ss::to_object<X>(a));
|
||||
} else {
|
||||
++num_errors;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
++num_errors;
|
||||
}
|
||||
}
|
||||
|
||||
CHECK(bad_lines == num_errors);
|
||||
|
||||
std::vector<X> data = {{1, 2, "just\n\nstrings"},
|
||||
#ifndef _WIN32
|
||||
{5, 6, "just\n\r\nstrings"},
|
||||
#else
|
||||
{5, 6, "just\n\nstrings"},
|
||||
#endif
|
||||
{9, 10, "just\n\nstrings"},
|
||||
{19, 20, "just strings"}};
|
||||
|
||||
for (auto& [_, __, s] : i) {
|
||||
update_if_crlf(s);
|
||||
}
|
||||
|
||||
if (i.size() != data.size()) {
|
||||
CHECK_EQ(i.size(), data.size());
|
||||
}
|
||||
|
||||
CHECK_EQ(i, data);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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, ErrorMode, Ts...>(f.name);
|
||||
size_t line = 0;
|
||||
while (!p.eof()) {
|
||||
auto command = [&p = p] {
|
||||
p.template get_next<int, double, std::string>();
|
||||
};
|
||||
|
||||
if (line == bad_line) {
|
||||
expect_error_on_command(p, command);
|
||||
break;
|
||||
} else {
|
||||
command();
|
||||
CHECK(p.valid());
|
||||
++line;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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<'"'>;
|
||||
|
||||
// unterminated escape
|
||||
{
|
||||
const std::vector<std::string> lines{"1,2,just\\"};
|
||||
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<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<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<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<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<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<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<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<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<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<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<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<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<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<T, multiline, escape, quote>(lines, 0);
|
||||
}
|
||||
|
||||
{
|
||||
const std::vector<std::string> lines{"1,2,\"just\n\\"};
|
||||
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<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<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<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<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<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<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<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<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<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<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<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<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<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<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<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<T, multiline, escape, quote>(lines, 1);
|
||||
}
|
||||
}
|
||||
471
test/test_parser1_4.cpp
Normal file
471
test/test_parser1_4.cpp
Normal file
@@ -0,0 +1,471 @@
|
||||
#include "test_parser1.hpp"
|
||||
|
||||
template <typename T, typename Tuple>
|
||||
struct has_type;
|
||||
|
||||
template <typename T, typename... Us>
|
||||
struct has_type<T, std::tuple<Us...>>
|
||||
: std::disjunction<std::is_same<T, Us>...> {};
|
||||
|
||||
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, ErrorMode>(file_name, ",");
|
||||
CHECK_FALSE(p.field_exists("Unknown"));
|
||||
p.use_fields(fields);
|
||||
std::vector<CaseType> i;
|
||||
|
||||
for (const auto& a : p.template iterate<CaseType>()) {
|
||||
i.push_back(a);
|
||||
}
|
||||
|
||||
CHECK_EQ(i.size(), data.size());
|
||||
for (size_t j = 0; j < i.size(); ++j) {
|
||||
if constexpr (has_type<int, CaseType>::value) {
|
||||
CHECK_EQ(std::get<int>(i[j]), data[j].i);
|
||||
}
|
||||
if constexpr (has_type<double, CaseType>::value) {
|
||||
CHECK_EQ(std::get<double>(i[j]), data[j].d);
|
||||
}
|
||||
if constexpr (has_type<std::string, CaseType>::value) {
|
||||
CHECK_EQ(std::get<std::string>(i[j]), data[j].s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
using str = std::string;
|
||||
|
||||
std::vector<std::string> header{Int, Dbl, Str};
|
||||
|
||||
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, header);
|
||||
const auto& o = f.name;
|
||||
const auto& d = data;
|
||||
|
||||
{
|
||||
ss::parser<ss::string_error> p{f.name, ","};
|
||||
std::vector<X> i;
|
||||
|
||||
for (const auto& a : p.iterate<int, double, std::string>()) {
|
||||
i.emplace_back(ss::to_object<X>(a));
|
||||
}
|
||||
|
||||
CHECK_NE(i, data);
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser<ss::string_error> p{f.name, ","};
|
||||
std::vector<X> i;
|
||||
|
||||
p.ignore_next();
|
||||
for (const auto& a : p.iterate<int, double, std::string>()) {
|
||||
i.emplace_back(ss::to_object<X>(a));
|
||||
}
|
||||
|
||||
CHECK_EQ(i, data);
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser<ss::ignore_header> p{f.name, ","};
|
||||
std::vector<X> i;
|
||||
|
||||
for (const auto& a : p.iterate<int, double, std::string>()) {
|
||||
i.emplace_back(ss::to_object<X>(a));
|
||||
}
|
||||
|
||||
CHECK_EQ(i, data);
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser<ss::ignore_header, ss::string_error> p{f.name, ","};
|
||||
p.use_fields(Int, Dbl, Str);
|
||||
CHECK_FALSE(p.valid());
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser<ss::ignore_header, ss::string_error> p{f.name, ","};
|
||||
CHECK_FALSE(p.field_exists("Unknown"));
|
||||
|
||||
p.use_fields(Int, "Unknown");
|
||||
CHECK_FALSE(p.valid());
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser<ss::ignore_header, ss::string_error> p{f.name, ","};
|
||||
p.use_fields(Int, Int);
|
||||
CHECK_FALSE(p.valid());
|
||||
}
|
||||
|
||||
{
|
||||
ss::parser<ss::string_error> p{f.name, ","};
|
||||
p.use_fields(Int, Dbl);
|
||||
|
||||
{
|
||||
auto [int_, double_] = p.get_next<int, double>();
|
||||
CHECK_EQ(int_, data[0].i);
|
||||
CHECK_EQ(double_, data[0].d);
|
||||
}
|
||||
|
||||
p.use_fields(Dbl, Int);
|
||||
|
||||
{
|
||||
auto [double_, int_] = p.get_next<double, int>();
|
||||
CHECK_EQ(int_, data[1].i);
|
||||
CHECK_EQ(double_, data[1].d);
|
||||
}
|
||||
|
||||
p.use_fields(Str);
|
||||
|
||||
{
|
||||
auto string_ = p.get_next<std::string>();
|
||||
CHECK_EQ(string_, data[2].s);
|
||||
}
|
||||
|
||||
p.use_fields(Str, Int, Dbl);
|
||||
|
||||
{
|
||||
auto [string_, int_, double_] =
|
||||
p.get_next<std::string, int, double>();
|
||||
CHECK_EQ(double_, data[3].d);
|
||||
CHECK_EQ(int_, data[3].i);
|
||||
CHECK_EQ(string_, data[3].s);
|
||||
}
|
||||
}
|
||||
|
||||
/* python used to generate permutations
|
||||
import itertools
|
||||
|
||||
header = {'str': 'Str',
|
||||
'double': 'Dbl',
|
||||
'int': 'Int'}
|
||||
|
||||
keys = ['str', 'int', 'double']
|
||||
|
||||
for r in range (1, 3):
|
||||
combinations = list(itertools.permutations(keys, r = r))
|
||||
|
||||
for combination in combinations:
|
||||
template_params = []
|
||||
arg_params = []
|
||||
for type in combination:
|
||||
template_params.append(type)
|
||||
arg_params.append(header[type])
|
||||
call = 'testFields<' + ', '.join(template_params) + \
|
||||
'>(o, d, {' + ', '.join(arg_params) + '});'
|
||||
print(call)
|
||||
*/
|
||||
|
||||
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 <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) {
|
||||
out << line << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// No fields specified
|
||||
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, 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, ErrorMode>(f.name, ",");
|
||||
auto command = [&p = p, &fields = fields] {
|
||||
p.use_fields(fields.at(0), fields.at(0));
|
||||
};
|
||||
if (!fields.empty()) {
|
||||
expect_error_on_command(p, command);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Mapping out of range
|
||||
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>();
|
||||
};
|
||||
if (!fields.empty()) {
|
||||
expect_error_on_command(p, command);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Invalid header
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
auto command = [&p = p, &fields = fields] { p.use_fields(fields); };
|
||||
|
||||
if (!fields.empty()) {
|
||||
// Pass if there are no duplicates, fail otherwise
|
||||
if (std::unordered_set<std::string>{fields.begin(), fields.end()}
|
||||
.size() != fields.size()) {
|
||||
expect_error_on_command(p, command);
|
||||
} else {
|
||||
command();
|
||||
CHECK(p.valid());
|
||||
if (!p.valid()) {
|
||||
if constexpr (T::StringError) {
|
||||
std::cout << p.error_msg() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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_TEMPLATE("test invalid rows with header", T,
|
||||
ParserOptionCombinations) {
|
||||
constexpr auto buffer_mode = T::BufferMode::value;
|
||||
using ErrorMode = typename T::ErrorMode;
|
||||
|
||||
unique_file_name f{"invalid rows with header"};
|
||||
{
|
||||
std::ofstream out{f.name};
|
||||
out << "Int,String,Double" << std::endl;
|
||||
out << "1,line1,2.34" << std::endl;
|
||||
out << "2,line2" << std::endl;
|
||||
out << "3,line3,67.8" << std::endl;
|
||||
out << "4,line4,67.8,9" << std::endl;
|
||||
out << "5,line5,9.10" << std::endl;
|
||||
out << "six,line6,10.11" << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name);
|
||||
|
||||
p.use_fields("Int", "String", "Double");
|
||||
using data = std::tuple<int, std::string, double>;
|
||||
std::vector<data> i;
|
||||
|
||||
CHECK(p.valid());
|
||||
|
||||
while (!p.eof()) {
|
||||
try {
|
||||
const auto& t = p.template get_next<data>();
|
||||
if (p.valid()) {
|
||||
i.push_back(t);
|
||||
}
|
||||
} catch (const ss::exception&) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<data> expected = {{1, "line1", 2.34},
|
||||
{3, "line3", 67.8},
|
||||
{5, "line5", 9.10}};
|
||||
CHECK_EQ(i, expected);
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name);
|
||||
|
||||
p.use_fields("Double", "Int");
|
||||
using data = std::tuple<double, int>;
|
||||
std::vector<data> i;
|
||||
|
||||
CHECK(p.valid());
|
||||
|
||||
while (!p.eof()) {
|
||||
try {
|
||||
const auto& t = p.template get_next<data>();
|
||||
if (p.valid()) {
|
||||
i.push_back(t);
|
||||
}
|
||||
} catch (const ss::exception&) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<data> expected = {{2.34, 1}, {67.8, 3}, {9.10, 5}};
|
||||
CHECK_EQ(i, expected);
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name);
|
||||
|
||||
p.use_fields("String", "Double");
|
||||
using data = std::tuple<std::string, double>;
|
||||
std::vector<data> i;
|
||||
|
||||
CHECK(p.valid());
|
||||
|
||||
while (!p.eof()) {
|
||||
try {
|
||||
const auto& t = p.template get_next<data>();
|
||||
if (p.valid()) {
|
||||
i.push_back(t);
|
||||
}
|
||||
} catch (const ss::exception&) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<data> expected = {{"line1", 2.34},
|
||||
{"line3", 67.8},
|
||||
{"line5", 9.10},
|
||||
{"line6", 10.11}};
|
||||
CHECK_EQ(i, expected);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void test_ignore_empty(const std::vector<X>& data) {
|
||||
constexpr auto buffer_mode = T::BufferMode::value;
|
||||
using ErrorMode = typename T::ErrorMode;
|
||||
|
||||
unique_file_name f{"ignore_empty"};
|
||||
make_and_write(f.name, data);
|
||||
|
||||
std::vector<X> expected;
|
||||
for (const auto& d : data) {
|
||||
if (d.s != X::empty) {
|
||||
expected.push_back(d);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] =
|
||||
make_parser<buffer_mode, ErrorMode, ss::ignore_empty>(f.name, ",");
|
||||
|
||||
std::vector<X> i;
|
||||
for (const auto& a : p.template iterate<X>()) {
|
||||
i.push_back(a);
|
||||
}
|
||||
|
||||
CHECK_EQ(i, expected);
|
||||
}
|
||||
|
||||
{
|
||||
auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
|
||||
std::vector<X> i;
|
||||
size_t n = 0;
|
||||
while (!p.eof()) {
|
||||
try {
|
||||
++n;
|
||||
const auto& a = p.template get_next<X>();
|
||||
if (data.at(n - 1).s == X::empty) {
|
||||
CHECK_FALSE(p.valid());
|
||||
continue;
|
||||
}
|
||||
i.push_back(a);
|
||||
} catch (...) {
|
||||
CHECK_EQ(data.at(n - 1).s, X::empty);
|
||||
}
|
||||
}
|
||||
|
||||
CHECK_EQ(i, expected);
|
||||
}
|
||||
}
|
||||
|
||||
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_ignore_empty<T>(
|
||||
{{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, "w"}});
|
||||
|
||||
test_ignore_empty<T>(
|
||||
{{1, 2, "x"}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}});
|
||||
|
||||
test_ignore_empty<T>(
|
||||
{{1, 2, "x"}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}});
|
||||
|
||||
test_ignore_empty<T>(
|
||||
{{1, 2, X::empty}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}});
|
||||
|
||||
test_ignore_empty<T>(
|
||||
{{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}});
|
||||
|
||||
test_ignore_empty<T>(
|
||||
{{1, 2, "x"}, {3, 4, "y"}, {9, 10, X::empty}, {11, 12, X::empty}});
|
||||
|
||||
test_ignore_empty<T>(
|
||||
{{1, 2, X::empty}, {3, 4, "y"}, {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<T>(
|
||||
{{1, 2, "x"}, {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, "w"}});
|
||||
|
||||
test_ignore_empty<T>({{11, 12, X::empty}});
|
||||
|
||||
test_ignore_empty<T>({});
|
||||
}
|
||||
@@ -121,7 +121,7 @@ column make_column(const std::string& input_header,
|
||||
}
|
||||
|
||||
[[maybe_unused]] void replace_all2(std::string& s, const std::string& old_value,
|
||||
const std::string& new_value) {
|
||||
const std::string& new_value) {
|
||||
for (size_t i = 0; i < 999; ++i) {
|
||||
size_t pos = s.find(old_value);
|
||||
if (pos == std::string::npos) {
|
||||
@@ -257,9 +257,12 @@ std::vector<std::string> generate_csv_data(const std::vector<field>& data,
|
||||
}
|
||||
|
||||
[[maybe_unused]] void write_to_file(const std::vector<std::string>& data,
|
||||
const std::string& delim, const std::string& file_name) {
|
||||
const std::string& delim,
|
||||
const std::string& file_name,
|
||||
bool add_new_line = true) {
|
||||
std::ofstream out{file_name, std::ios_base::app};
|
||||
std::string line;
|
||||
|
||||
for (size_t i = 0; i < data.size(); ++i) {
|
||||
line += data[i];
|
||||
if (i != data.size() - 1) {
|
||||
@@ -267,7 +270,10 @@ std::vector<std::string> generate_csv_data(const std::vector<field>& data,
|
||||
}
|
||||
}
|
||||
|
||||
out << line << std::endl;
|
||||
out << line;
|
||||
if (add_new_line) {
|
||||
out << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
#define CHECK_EQ_CRLF(V1, V2) \
|
||||
@@ -299,7 +305,7 @@ std::vector<std::string> generate_csv_data(const std::vector<field>& data,
|
||||
CHECK(V1 == V2); \
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
template <bool buffer_mode, typename... Ts>
|
||||
void test_data_combinations(const std::vector<column>& input_data,
|
||||
const std::string& delim, bool include_header) {
|
||||
using setup = ss::setup<Ts...>;
|
||||
@@ -308,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;
|
||||
@@ -329,7 +335,11 @@ void test_data_combinations(const std::vector<column>& input_data,
|
||||
|
||||
if (include_header) {
|
||||
auto header_data = generate_csv_data<Ts...>(field_header, delim);
|
||||
write_to_file(header_data, delim, f.name);
|
||||
if (input_data.size() == 0 && rand() % 10 == 0) {
|
||||
write_to_file(header_data, delim, f.name, false);
|
||||
} else {
|
||||
write_to_file(header_data, delim, f.name);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int> layout;
|
||||
@@ -354,15 +364,12 @@ void test_data_combinations(const std::vector<column>& input_data,
|
||||
|
||||
expected_data.push_back(raw_data);
|
||||
auto data = generate_csv_data<Ts...>(raw_data, delim);
|
||||
write_to_file(data, delim, f.name);
|
||||
|
||||
/*
|
||||
std::cout << "[.";
|
||||
for (const auto& el : data) {
|
||||
std::cout << el << '.';
|
||||
if (i + 1 == n && rand() % 10 == 0) {
|
||||
write_to_file(data, delim, f.name, false);
|
||||
} else {
|
||||
write_to_file(data, delim, f.name);
|
||||
}
|
||||
std::cout << "]" << std::endl;
|
||||
*/
|
||||
}
|
||||
|
||||
auto layout_combinations = include_header && !setup::ignore_header
|
||||
@@ -388,7 +395,7 @@ void test_data_combinations(const std::vector<column>& input_data,
|
||||
}
|
||||
|
||||
for (const auto& layout : unique_layout_combinations) {
|
||||
ss::parser<setup> p{f.name, delim};
|
||||
auto [p, _] = make_parser<buffer_mode, setup>(f.name, delim);
|
||||
|
||||
if (include_header && !setup::ignore_header) {
|
||||
std::vector<std::string> fields;
|
||||
@@ -409,7 +416,7 @@ void test_data_combinations(const std::vector<column>& input_data,
|
||||
REQUIRE(p.valid());
|
||||
}
|
||||
|
||||
auto check_error = [&p] {
|
||||
auto check_error = [&p = p] {
|
||||
CHECK(p.valid());
|
||||
if (!p.valid()) {
|
||||
if constexpr (setup::string_error) {
|
||||
@@ -426,7 +433,6 @@ void test_data_combinations(const std::vector<column>& input_data,
|
||||
auto s0 = p.template get_next<std::string>();
|
||||
if (i < n) {
|
||||
check_error();
|
||||
// std::cout << s0 << std::endl;
|
||||
CHECK_EQ_CRLF(s0, expected_data[i][layout[0]].value);
|
||||
} else {
|
||||
CHECK(p.eof());
|
||||
@@ -439,7 +445,6 @@ void test_data_combinations(const std::vector<column>& input_data,
|
||||
p.template get_next<std::string, std::string>();
|
||||
if (i < n) {
|
||||
check_error();
|
||||
// std::cout << s0 << ' ' << s1 << std::endl;
|
||||
CHECK_EQ_CRLF(s0, expected_data[i][layout[0]].value);
|
||||
CHECK_EQ_CRLF(s1, expected_data[i][layout[1]].value);
|
||||
} else {
|
||||
@@ -454,8 +459,6 @@ void test_data_combinations(const std::vector<column>& input_data,
|
||||
std::string>();
|
||||
if (i < n) {
|
||||
check_error();
|
||||
// std::cout << s0 << ' ' << s1 << ' ' << s2 <<
|
||||
// std::endl;
|
||||
CHECK_EQ_CRLF(s0, expected_data[i][layout[0]].value);
|
||||
CHECK_EQ_CRLF(s1, expected_data[i][layout[1]].value);
|
||||
CHECK_EQ_CRLF(s2, expected_data[i][layout[2]].value);
|
||||
@@ -471,10 +474,6 @@ void test_data_combinations(const std::vector<column>& input_data,
|
||||
std::string, std::string>();
|
||||
if (i < n) {
|
||||
check_error();
|
||||
/*
|
||||
std::cout << s0 << ' ' << s1 << ' ' << s2 << ' ' << s3
|
||||
<< std::endl;
|
||||
*/
|
||||
CHECK_EQ_CRLF(s0, expected_data[i][layout[0]].value);
|
||||
CHECK_EQ_CRLF(s1, expected_data[i][layout[1]].value);
|
||||
CHECK_EQ_CRLF(s2, expected_data[i][layout[2]].value);
|
||||
@@ -492,9 +491,6 @@ void test_data_combinations(const std::vector<column>& input_data,
|
||||
std::string>();
|
||||
if (i < n) {
|
||||
check_error();
|
||||
// std::cout << s0 << ' ' << s1 << ' ' << s2 << ' ' <<
|
||||
// s3
|
||||
// << ' ' << s4 << std::endl;
|
||||
CHECK_EQ_CRLF(s0, expected_data[i][layout[0]].value);
|
||||
CHECK_EQ_CRLF(s1, expected_data[i][layout[1]].value);
|
||||
CHECK_EQ_CRLF(s2, expected_data[i][layout[2]].value);
|
||||
@@ -570,8 +566,14 @@ void test_option_combinations() {
|
||||
{columns0, columns1, columns2, columns3, columns4, columns5,
|
||||
columns6, columns7}) {
|
||||
try {
|
||||
test_data_combinations<Ts...>(columns, delimiter, false);
|
||||
test_data_combinations<Ts...>(columns, delimiter, true);
|
||||
test_data_combinations<false, Ts...>(columns, delimiter,
|
||||
false);
|
||||
test_data_combinations<false, Ts...>(columns, delimiter,
|
||||
true);
|
||||
test_data_combinations<true, Ts...>(columns, delimiter,
|
||||
false);
|
||||
test_data_combinations<true, Ts...>(columns, delimiter,
|
||||
true);
|
||||
} catch (std::exception& e) {
|
||||
std::cout << typeid(ss::parser<Ts...>).name() << std::endl;
|
||||
FAIL_CHECK(std::string{e.what()});
|
||||
@@ -616,6 +618,7 @@ void test_option_combinations3() {
|
||||
|
||||
} /* namespace */
|
||||
|
||||
// Tests split into multiple compilation units
|
||||
#if 0
|
||||
|
||||
TEST_CASE("parser test various cases version 2 segment 1") {
|
||||
@@ -627,25 +630,33 @@ TEST_CASE("parser test various cases version 2 segment 1") {
|
||||
using multiline_r = ss::multiline_restricted<10>;
|
||||
using trimr = ss::trim_right<' '>;
|
||||
using triml = ss::trim_left<' '>;
|
||||
using trim = ss::trim<' '>;
|
||||
|
||||
// segment 1
|
||||
test_option_combinations3<>();
|
||||
test_option_combinations3<escape>();
|
||||
test_option_combinations3<quote>();
|
||||
|
||||
// segment 2
|
||||
test_option_combinations3<quote>();
|
||||
test_option_combinations3<escape, quote>();
|
||||
|
||||
// segment 3
|
||||
test_option_combinations3<escape, multiline>();
|
||||
test_option_combinations3<quote, multiline>();
|
||||
|
||||
// segment 3
|
||||
// segment 4
|
||||
test_option_combinations3<escape, quote, multiline>();
|
||||
test_option_combinations3<escape, quote, multiline_r>();
|
||||
|
||||
// segment 4
|
||||
// segment 5
|
||||
test_option_combinations<escape, quote, multiline, triml>();
|
||||
test_option_combinations<escape, quote, multiline, trimr>();
|
||||
|
||||
// segment 6
|
||||
test_option_combinations3<escape, quote, multiline>();
|
||||
test_option_combinations3<escape, quote, multiline, trim>();
|
||||
#else
|
||||
|
||||
test_option_combinations3<escape, quote, multiline>();
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -3,12 +3,10 @@
|
||||
|
||||
TEST_CASE("parser test various cases version 2 segment 1") {
|
||||
#ifdef CMAKE_GITHUB_CI
|
||||
using quote = ss::quote<'"'>;
|
||||
using escape = ss::escape<'\\'>;
|
||||
|
||||
test_option_combinations3<>();
|
||||
test_option_combinations3<escape>();
|
||||
test_option_combinations3<quote>();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -5,11 +5,9 @@ TEST_CASE("parser test various cases version 2 segment 2") {
|
||||
#ifdef CMAKE_GITHUB_CI
|
||||
using quote = ss::quote<'"'>;
|
||||
using escape = ss::escape<'\\'>;
|
||||
using multiline = ss::multiline;
|
||||
|
||||
test_option_combinations3<quote>();
|
||||
test_option_combinations3<escape, quote>();
|
||||
test_option_combinations3<escape, multiline>();
|
||||
test_option_combinations3<quote, multiline>();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -6,10 +6,9 @@ TEST_CASE("parser test various cases version 2 segment 3") {
|
||||
using quote = ss::quote<'"'>;
|
||||
using escape = ss::escape<'\\'>;
|
||||
using multiline = ss::multiline;
|
||||
using multiline_r = ss::multiline_restricted<10>;
|
||||
|
||||
test_option_combinations3<escape, quote, multiline>();
|
||||
test_option_combinations3<escape, quote, multiline_r>();
|
||||
test_option_combinations3<escape, multiline>();
|
||||
test_option_combinations3<quote, multiline>();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -2,18 +2,14 @@
|
||||
#include "test_parser2.hpp"
|
||||
|
||||
TEST_CASE("parser test various cases version 2 segment 4") {
|
||||
#ifdef CMAKE_GITHUB_CI
|
||||
using quote = ss::quote<'"'>;
|
||||
using escape = ss::escape<'\\'>;
|
||||
using multiline = ss::multiline;
|
||||
using multiline_r = ss::multiline_restricted<10>;
|
||||
|
||||
#ifdef CMAKE_GITHUB_CI
|
||||
using trimr = ss::trim_right<' '>;
|
||||
using triml = ss::trim_left<' '>;
|
||||
|
||||
test_option_combinations<escape, quote, multiline, triml>();
|
||||
test_option_combinations<escape, quote, multiline, trimr>();
|
||||
#else
|
||||
test_option_combinations3<escape, quote, multiline>();
|
||||
test_option_combinations3<escape, quote, multiline_r>();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
16
test/test_parser2_5.cpp
Normal file
16
test/test_parser2_5.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#define SEGMENT_NAME "segment5"
|
||||
#include "test_parser2.hpp"
|
||||
|
||||
TEST_CASE("parser test various cases version 2 segment 5") {
|
||||
#ifdef CMAKE_GITHUB_CI
|
||||
using quote = ss::quote<'"'>;
|
||||
using escape = ss::escape<'\\'>;
|
||||
using multiline = ss::multiline;
|
||||
using trimr = ss::trim_right<' '>;
|
||||
using triml = ss::trim_left<' '>;
|
||||
|
||||
test_option_combinations<escape, quote, multiline, triml>();
|
||||
test_option_combinations<escape, quote, multiline, trimr>();
|
||||
#endif
|
||||
}
|
||||
|
||||
11
test/test_parser2_6.cpp
Normal file
11
test/test_parser2_6.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#define SEGMENT_NAME "segment6"
|
||||
#include "test_parser2.hpp"
|
||||
|
||||
TEST_CASE("parser test various cases version 2 segment 6") {
|
||||
using quote = ss::quote<'"'>;
|
||||
using escape = ss::escape<'\\'>;
|
||||
using multiline = ss::multiline;
|
||||
|
||||
test_option_combinations3<escape, quote, multiline>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user