make indent with 4 caracters instead of 8

This commit is contained in:
ado 2020-12-26 00:46:42 +01:00
parent 16f4648cb3
commit e406253694
9 changed files with 1688 additions and 1746 deletions

View File

@ -41,8 +41,7 @@ using no_validator_t = typename no_validator<T>::type;
template <typename... Ts> template <typename... Ts>
struct no_validator_tup { struct no_validator_tup {
using type = using type = typename apply_trait<no_validator, std::tuple<Ts...>>::type;
typename apply_trait<no_validator, std::tuple<Ts...>>::type;
}; };
template <typename... Ts> template <typename... Ts>
@ -115,14 +114,13 @@ class converter {
using string_range = std::pair<const char*, const char*>; using string_range = std::pair<const char*, const char*>;
constexpr static auto default_delimiter = ','; constexpr static auto default_delimiter = ',';
public: public:
using split_input = std::vector<string_range>; using split_input = std::vector<string_range>;
// parses line with given delimiter, returns a 'T' object created with // parses line with given delimiter, returns a 'T' object created with
// extracted values of type 'Ts' // extracted values of type 'Ts'
template <typename T, typename... Ts> template <typename T, typename... Ts>
T convert_object(const char* const line, T convert_object(const char* const line, const std::string& delim = "") {
const std::string& delim = "") {
return to_object<T>(convert<Ts...>(line, delim)); return to_object<T>(convert<Ts...>(line, delim));
} }
@ -152,11 +150,9 @@ class converter {
typename std::result_of_t<decltype (&T::tied)(T)>; typename std::result_of_t<decltype (&T::tied)(T)>;
using arg_tuple = using arg_tuple =
typename apply_trait<std::decay, typename apply_trait<std::decay, arg_ref_tuple>::type;
arg_ref_tuple>::type;
return to_object<T>( return to_object<T>(convert_impl(elems, (arg_tuple*){}));
convert_impl(elems, (arg_tuple*){}));
} else if constexpr (sizeof...(Ts) == 0 && } else if constexpr (sizeof...(Ts) == 0 &&
is_instance_of<T, std::tuple>::value) { is_instance_of<T, std::tuple>::value) {
return convert_impl(elems, (T*){}); return convert_impl(elems, (T*){});
@ -167,8 +163,7 @@ class converter {
} }
bool valid() const { bool valid() const {
return (error_mode_ == error_mode::String) return (error_mode_ == error_mode::String) ? string_error_.empty()
? string_error_.empty()
: bool_error_ == false; : bool_error_ == false;
} }
@ -199,7 +194,7 @@ class converter {
}; };
} }
private: private:
//////////////// ////////////////
// error // error
//////////////// ////////////////
@ -223,8 +218,7 @@ class converter {
void set_error_invalid_conversion(const string_range msg, size_t pos) { void set_error_invalid_conversion(const string_range msg, size_t pos) {
if (error_mode_ == error_mode::String) { if (error_mode_ == error_mode::String) {
string_error_.clear(); string_error_.clear();
string_error_ string_error_.append("invalid conversion for parameter ")
.append("invalid conversion for parameter ")
.append(error_sufix(msg, pos)); .append(error_sufix(msg, pos));
} else { } else {
bool_error_ = true; bool_error_ = true;
@ -245,8 +239,7 @@ class converter {
void set_error_number_of_colums(size_t expected_pos, size_t pos) { void set_error_number_of_colums(size_t expected_pos, size_t pos) {
if (error_mode_ == error_mode::String) { if (error_mode_ == error_mode::String) {
string_error_.clear(); string_error_.clear();
string_error_ string_error_.append("invalid number of columns, expected: ")
.append("invalid number of columns, expected: ")
.append(std::to_string(expected_pos)) .append(std::to_string(expected_pos))
.append(", got: ") .append(", got: ")
.append(std::to_string(pos)); .append(std::to_string(pos));
@ -329,11 +322,9 @@ class converter {
if constexpr (has_m_ss_valid_t<T>) { if constexpr (has_m_ss_valid_t<T>) {
if (T validator; !validator.ss_valid(dst)) { if (T validator; !validator.ss_valid(dst)) {
if constexpr (has_m_error_t<T>) { if constexpr (has_m_error_t<T>) {
set_error_validate(validator.error(), set_error_validate(validator.error(), msg, pos);
msg, pos);
} else { } else {
set_error_validate("validation error", set_error_validate("validation error", msg, pos);
msg, pos);
} }
return; return;
} }
@ -346,8 +337,7 @@ class converter {
using elem_t = std::tuple_element_t<ArgN, std::tuple<Ts...>>; using elem_t = std::tuple_element_t<ArgN, std::tuple<Ts...>>;
constexpr bool not_void = !std::is_void_v<elem_t>; constexpr bool not_void = !std::is_void_v<elem_t>;
constexpr bool one_element = constexpr bool one_element = count_not<std::is_void, Ts...>::size == 1;
count_not<std::is_void, Ts...>::size == 1;
if constexpr (not_void) { if constexpr (not_void) {
if constexpr (one_element) { if constexpr (one_element) {

View File

@ -245,8 +245,7 @@ inline bool sub_overflow(unsigned long long& result,
template <typename T, typename F> template <typename T, typename F>
bool shift_and_add_overflow(T& value, T digit, F add_last_digit_owerflow) { bool shift_and_add_overflow(T& value, T digit, F add_last_digit_owerflow) {
if (mul_overflow<T>(value, 10) || if (mul_overflow<T>(value, 10) || add_last_digit_owerflow(value, digit)) {
add_last_digit_owerflow(value, digit)) {
return true; return true;
} }
return false; return false;
@ -293,8 +292,7 @@ std::enable_if_t<std::is_integral_v<T>, std::optional<T>> to_num(
T value = 0; T value = 0;
for (auto i = begin; i != end; ++i) { for (auto i = begin; i != end; ++i) {
if (auto digit = from_char(*i); if (auto digit = from_char(*i);
!digit || !digit || shift_and_add_overflow<T>(value, digit.value(),
shift_and_add_overflow<T>(value, digit.value(),
add_last_digit_owerflow)) { add_last_digit_owerflow)) {
return std::nullopt; return std::nullopt;
} }

View File

@ -16,7 +16,7 @@ template <typename...>
class composite; class composite;
class parser { class parser {
public: public:
parser(const std::string& file_name, const std::string& delimiter) parser(const std::string& file_name, const std::string& delimiter)
: file_name_{file_name}, delim_{delimiter}, : file_name_{file_name}, delim_{delimiter},
file_{fopen(file_name_.c_str(), "rb")} { file_{fopen(file_name_.c_str(), "rb")} {
@ -33,8 +33,7 @@ class parser {
} }
bool valid() const { bool valid() const {
return (error_mode_ == error_mode::String) return (error_mode_ == error_mode::String) ? string_error_.empty()
? string_error_.empty()
: bool_error_ == false; : bool_error_ == false;
} }
@ -97,8 +96,8 @@ class parser {
// that parameter can be invoked using the converted value, // that parameter can be invoked using the converted value,
// than it will be invoked in the case of a valid conversion // than it will be invoked in the case of a valid conversion
template <typename... Us, typename Fun = None> template <typename... Us, typename Fun = None>
composite<Ts..., std::optional<no_void_validator_tup_t<Us...>>> composite<Ts..., std::optional<no_void_validator_tup_t<Us...>>> or_else(
or_else(Fun&& fun = None{}) { Fun&& fun = None{}) {
using Value = no_void_validator_tup_t<Us...>; using Value = no_void_validator_tup_t<Us...>;
std::optional<Value> value; std::optional<Value> value;
try_convert_and_invoke<Value, Us...>(value, fun); try_convert_and_invoke<Value, Us...>(value, fun);
@ -108,8 +107,7 @@ class parser {
// same as or_else, but saves the result into a 'U' object // same as or_else, but saves the result into a 'U' object
// instead of a tuple // instead of a tuple
template <typename U, typename... Us, typename Fun = None> template <typename U, typename... Us, typename Fun = None>
composite<Ts..., std::optional<U>> or_else_object( composite<Ts..., std::optional<U>> or_else_object(Fun&& fun = None{}) {
Fun&& fun = None{}) {
std::optional<U> value; std::optional<U> value;
try_convert_and_invoke<U, Us...>(value, fun); try_convert_and_invoke<U, Us...>(value, fun);
return composite_with(std::move(value)); return composite_with(std::move(value));
@ -125,8 +123,7 @@ class parser {
if constexpr (std::is_invocable_v<Fun>) { if constexpr (std::is_invocable_v<Fun>) {
fun(); fun();
} else { } else {
std::invoke(std::forward<Fun>(fun), std::invoke(std::forward<Fun>(fun), parser_.error_msg());
parser_.error_msg());
} }
} }
return *this; return *this;
@ -137,29 +134,23 @@ class parser {
composite<Ts..., T> composite_with(T&& new_value) { composite<Ts..., T> composite_with(T&& new_value) {
auto merged_values = auto merged_values =
std::tuple_cat(std::move(values_), std::tuple_cat(std::move(values_),
std::tuple{ std::tuple{std::forward<T>(new_value)});
std::forward<T>(new_value)});
return {std::move(merged_values), parser_}; return {std::move(merged_values), parser_};
} }
template <typename U, typename... Us, typename Fun = None> template <typename U, typename... Us, typename Fun = None>
void try_convert_and_invoke(std::optional<U>& value, void try_convert_and_invoke(std::optional<U>& value, Fun&& fun) {
Fun&& fun) {
if (!parser_.valid()) { if (!parser_.valid()) {
std::optional<U> new_value; std::optional<U> new_value;
auto tuple_output = try_same<Us...>(); auto tuple_output = try_same<Us...>();
if constexpr (!std::is_same_v< if constexpr (!std::is_same_v<U, decltype(tuple_output)>) {
U, decltype(tuple_output)>) { new_value = to_object<U>(std::move(tuple_output));
new_value = to_object<U>(
std::move(tuple_output));
} else { } else {
new_value = std::move(tuple_output); new_value = std::move(tuple_output);
} }
if (parser_.valid()) { if (parser_.valid()) {
value = std::move(new_value); value = std::move(new_value);
parser_.try_invoke(*value, parser_.try_invoke(*value, std::forward<Fun>(fun));
std::forward<Fun>(
fun));
} }
} }
} }
@ -167,8 +158,8 @@ class parser {
template <typename U, typename... Us> template <typename U, typename... Us>
no_void_validator_tup_t<U, Us...> try_same() { no_void_validator_tup_t<U, Us...> try_same() {
parser_.clear_error(); parser_.clear_error();
auto value = parser_.converter_.convert<U, Us...>( auto value =
parser_.split_input_); parser_.converter_.convert<U, Us...>(parser_.split_input_);
if (!parser_.converter_.valid()) { if (!parser_.converter_.valid()) {
parser_.set_error_invalid_conversion(); parser_.set_error_invalid_conversion();
} }
@ -193,7 +184,7 @@ class parser {
return {std::move(value), *this}; return {std::move(value), *this};
}; };
private: private:
template <typename...> template <typename...>
friend class composite; friend class composite;
@ -204,15 +195,12 @@ class parser {
// failed // failed
template <typename Arg, typename Fun = None> template <typename Arg, typename Fun = None>
void try_invoke(Arg&& arg, Fun&& fun) { void try_invoke(Arg&& arg, Fun&& fun) {
constexpr bool is_none = constexpr bool is_none = std::is_same_v<std::decay_t<Fun>, None>;
std::is_same_v<std::decay_t<Fun>, None>;
if constexpr (!is_none) { if constexpr (!is_none) {
using Ret = decltype( using Ret = decltype(try_invoke_impl(arg, std::forward<Fun>(fun)));
try_invoke_impl(arg, std::forward<Fun>(fun)));
constexpr bool returns_void = std::is_same_v<Ret, void>; constexpr bool returns_void = std::is_same_v<Ret, void>;
if constexpr (!returns_void) { if constexpr (!returns_void) {
if (!try_invoke_impl(arg, if (!try_invoke_impl(arg, std::forward<Fun>(fun))) {
std::forward<Fun>(fun))) {
set_error_failed_check(); set_error_failed_check();
} }
} else { } else {
@ -228,8 +216,7 @@ class parser {
// laid out as a parameter pack // laid out as a parameter pack
template <typename Arg, typename Fun = None> template <typename Arg, typename Fun = None>
auto try_invoke_impl(Arg&& arg, Fun&& fun) { auto try_invoke_impl(Arg&& arg, Fun&& fun) {
constexpr bool is_none = constexpr bool is_none = std::is_same_v<std::decay_t<Fun>, None>;
std::is_same_v<std::decay_t<Fun>, None>;
if constexpr (!is_none) { if constexpr (!is_none) {
if constexpr (std::is_invocable_v<Fun>) { if constexpr (std::is_invocable_v<Fun>) {
return fun(); return fun();
@ -299,8 +286,7 @@ class parser {
void set_error_failed_check() { void set_error_failed_check() {
if (error_mode_ == error_mode::String) { if (error_mode_ == error_mode::String) {
string_error_.append(file_name_) string_error_.append(file_name_).append(" failed check.");
.append(" failed check.");
} else { } else {
bool_error_ = true; bool_error_ = true;
} }
@ -308,8 +294,7 @@ class parser {
void set_error_file_not_open() { void set_error_file_not_open() {
if (error_mode_ == error_mode::String) { if (error_mode_ == error_mode::String) {
string_error_.append(file_name_) string_error_.append(file_name_).append(" could not be not open.");
.append(" could not be not open.");
} else { } else {
bool_error_ = true; bool_error_ = true;
} }
@ -317,8 +302,7 @@ class parser {
void set_error_eof_reached() { void set_error_eof_reached() {
if (error_mode_ == error_mode::String) { if (error_mode_ == error_mode::String) {
string_error_.append(file_name_) string_error_.append(file_name_).append(" reached end of file.");
.append(" reached end of file.");
} else { } else {
bool_error_ = true; bool_error_ = true;
} }

View File

@ -8,7 +8,7 @@ namespace ss {
template <typename T, auto... Values> template <typename T, auto... Values>
struct ax { struct ax {
private: private:
template <auto X, auto... Xs> template <auto X, auto... Xs>
bool ss_valid_impl(const T& x) const { bool ss_valid_impl(const T& x) const {
if constexpr (sizeof...(Xs) != 0) { if constexpr (sizeof...(Xs) != 0) {
@ -17,7 +17,7 @@ struct ax {
return x != X; return x != X;
} }
public: public:
bool ss_valid(const T& value) const { bool ss_valid(const T& value) const {
return ss_valid_impl<Values...>(value); return ss_valid_impl<Values...>(value);
} }
@ -33,7 +33,7 @@ struct ax {
template <typename T, auto... Values> template <typename T, auto... Values>
struct nx { struct nx {
private: private:
template <auto X, auto... Xs> template <auto X, auto... Xs>
bool ss_valid_impl(const T& x) const { bool ss_valid_impl(const T& x) const {
if constexpr (sizeof...(Xs) != 0) { if constexpr (sizeof...(Xs) != 0) {
@ -42,7 +42,7 @@ struct nx {
return x == X; return x == X;
} }
public: public:
bool ss_valid(const T& value) const { bool ss_valid(const T& value) const {
return ss_valid_impl<Values...>(value); return ss_valid_impl<Values...>(value);
} }

View File

@ -133,14 +133,14 @@ struct apply_optional_trait<Trait, std::tuple<T, Ts...>> {
template <template <typename...> class Trait, typename T> template <template <typename...> class Trait, typename T>
struct apply_optional_trait { struct apply_optional_trait {
using type = std::tuple< using type =
typename optional_trait<typename Trait<T>::type, T>::type>; std::tuple<typename optional_trait<typename Trait<T>::type, T>::type>;
}; };
template <template <typename...> class Trait, typename T> template <template <typename...> class Trait, typename T>
struct apply_optional_trait<Trait, std::tuple<T>> { struct apply_optional_trait<Trait, std::tuple<T>> {
using type = std::tuple< using type =
typename optional_trait<typename Trait<T>::type, T>::type>; std::tuple<typename optional_trait<typename Trait<T>::type, T>::type>;
}; };
//////////////// ////////////////
@ -204,8 +204,8 @@ struct filter_if {
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct filter_if<Trait, std::tuple<Ts...>> { struct filter_if<Trait, std::tuple<Ts...>> {
using type = typename remove_false<typename apply_optional_trait< using type = typename remove_false<
Trait, std::tuple<Ts...>>::type>::type; typename apply_optional_trait<Trait, std::tuple<Ts...>>::type>::type;
}; };
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
@ -268,14 +268,12 @@ struct count_not<Trait, T> {
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct all_of { struct all_of {
static constexpr bool value = static constexpr bool value = count<Trait, Ts...>::size == sizeof...(Ts);
count<Trait, Ts...>::size == sizeof...(Ts);
}; };
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct all_of<Trait, std::tuple<Ts...>> { struct all_of<Trait, std::tuple<Ts...>> {
static constexpr bool value = static constexpr bool value = count<Trait, Ts...>::size == sizeof...(Ts);
count<Trait, Ts...>::size == sizeof...(Ts);
}; };
//////////////// ////////////////
@ -317,7 +315,7 @@ struct is_instance_of {
constexpr static bool value = false; constexpr static bool value = false;
}; };
template <typename ...Ts, template <typename...> class Template> template <typename... Ts, template <typename...> class Template>
struct is_instance_of<Template<Ts...>, Template> { struct is_instance_of<Template<Ts...>, Template> {
constexpr static bool value = true; constexpr static bool value = true;
}; };

View File

@ -59,8 +59,7 @@ TEST_CASE("testing valid conversions") {
} }
{ {
auto tup = auto tup =
c.convert<void, void, std::optional<int>>("junk\tjunk\t5", c.convert<void, void, std::optional<int>>("junk\tjunk\t5", "\t");
"\t");
REQUIRE(c.valid()); REQUIRE(c.valid());
REQUIRE(tup.has_value()); REQUIRE(tup.has_value());
CHECK(tup == 5); CHECK(tup == 5);
@ -82,30 +81,30 @@ TEST_CASE("testing valid conversions") {
} }
{ {
auto tup = auto tup =
c.convert<void, std::optional<int>, double>("junk;5;6.6", c.convert<void, std::optional<int>, double>("junk;5;6.6", ";");
";");
REQUIRE(c.valid()); REQUIRE(c.valid());
REQUIRE(std::get<0>(tup).has_value()); REQUIRE(std::get<0>(tup).has_value());
CHECK(tup == std::tuple{5, 6.6}); CHECK(tup == std::tuple{5, 6.6});
} }
{ {
auto tup = auto tup =
c.convert<void, std::optional<int>, double>("junk;5.4;6.6", c.convert<void, std::optional<int>, double>("junk;5.4;6.6", ";");
";");
REQUIRE(c.valid()); REQUIRE(c.valid());
REQUIRE(!std::get<0>(tup).has_value()); REQUIRE(!std::get<0>(tup).has_value());
CHECK(tup == std::tuple{std::nullopt, 6.6}); CHECK(tup == std::tuple{std::nullopt, 6.6});
} }
{ {
auto tup = c.convert<void, std::variant<int, double>, auto tup =
double>("junk;5;6.6", ";"); c.convert<void, std::variant<int, double>, double>("junk;5;6.6",
";");
REQUIRE(c.valid()); REQUIRE(c.valid());
REQUIRE(std::holds_alternative<int>(std::get<0>(tup))); REQUIRE(std::holds_alternative<int>(std::get<0>(tup)));
CHECK(tup == std::tuple{std::variant<int, double>{5}, 6.6}); CHECK(tup == std::tuple{std::variant<int, double>{5}, 6.6});
} }
{ {
auto tup = c.convert<void, std::variant<int, double>, auto tup =
double>("junk;5.5;6.6", ";"); c.convert<void, std::variant<int, double>, double>("junk;5.5;6.6",
";");
REQUIRE(c.valid()); REQUIRE(c.valid());
REQUIRE(std::holds_alternative<double>(std::get<0>(tup))); REQUIRE(std::holds_alternative<double>(std::get<0>(tup)));
CHECK(tup == std::tuple{std::variant<int, double>{5.5}, 6.6}); CHECK(tup == std::tuple{std::variant<int, double>{5.5}, 6.6});
@ -163,14 +162,12 @@ TEST_CASE("testing ss:ax restriction (all except)") {
CHECK(tup == 3); CHECK(tup == 3);
} }
{ {
std::tuple<char, int> tup = std::tuple<char, int> tup = c.convert<char, ss::ax<int, 1>>("c,3");
c.convert<char, ss::ax<int, 1>>("c,3");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 3}); CHECK(tup == std::tuple{'c', 3});
} }
{ {
std::tuple<int, char> tup = std::tuple<int, char> tup = c.convert<ss::ax<int, 1>, char>("3,c");
c.convert<ss::ax<int, 1>, char>("3,c");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == std::tuple{3, 'c'}); CHECK(tup == std::tuple{3, 'c'});
} }
@ -199,8 +196,7 @@ TEST_CASE("testing ss:nx restriction (none except)") {
CHECK(tup == 2); CHECK(tup == 2);
} }
{ {
auto tup = auto tup = c.convert<char, void, ss::nx<int, 0, 1, 2>>("c,junk,1");
c.convert<char, void, ss::nx<int, 0, 1, 2>>("c,junk,1");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 1}); CHECK(tup == std::tuple{'c', 1});
} }
@ -267,8 +263,7 @@ TEST_CASE("testing ss:oor restriction (out of range)") {
} }
{ {
auto tup = auto tup = c.convert<char, void, ss::oor<int, 4, 69>>("c,junk,3");
c.convert<char, void, ss::oor<int, 4, 69>>("c,junk,3");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 3}); CHECK(tup == std::tuple{'c', 3});
} }
@ -317,8 +312,7 @@ TEST_CASE("testing ss:ne restriction (not empty)") {
CHECK(tup == "s"); CHECK(tup == "s");
} }
{ {
auto tup = auto tup = c.convert<std::optional<int>, ss::ne<std::string>>("1,s");
c.convert<std::optional<int>, ss::ne<std::string>>("1,s");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == std::tuple{1, "s"}); CHECK(tup == std::tuple{1, "s"});
} }

View File

@ -54,8 +54,7 @@ TEST_CASE("testing extract functions for floating point values") {
/* check negative too */ \ /* check negative too */ \
if (std::is_signed_v<type>) { \ if (std::is_signed_v<type>) { \
auto s = std::string("-") + #input; \ auto s = std::string("-") + #input; \
auto t = \ auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \ REQUIRE(t.has_value()); \
CHECK(t.value() == type(-input)); \ CHECK(t.value() == type(-input)); \
} \ } \
@ -104,8 +103,7 @@ TEST_CASE("testing extract functions for numbers with invalid inputs") {
#define CHECK_OUT_OF_RANGE_CONVERSION(type) \ #define CHECK_OUT_OF_RANGE_CONVERSION(type) \
{ \ { \
std::string s = \ std::string s = std::to_string(std::numeric_limits<type>::max()); \
std::to_string(std::numeric_limits<type>::max()); \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \ auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(t.has_value()); \ CHECK(t.has_value()); \
for (auto& i : s) { \ for (auto& i : s) { \
@ -118,8 +116,7 @@ TEST_CASE("testing extract functions for numbers with invalid inputs") {
CHECK(!t.has_value()); \ CHECK(!t.has_value()); \
} \ } \
{ \ { \
std::string s = \ std::string s = std::to_string(std::numeric_limits<type>::min()); \
std::to_string(std::numeric_limits<type>::min()); \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \ auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(t.has_value()); \ CHECK(t.has_value()); \
for (auto& i : s) { \ for (auto& i : s) { \
@ -224,32 +221,28 @@ TEST_CASE("testing extract functions for std::variant") {
std::string s = "22"; std::string s = "22";
{ {
std::variant<int, double, std::string> var; std::variant<int, double, std::string> var;
REQUIRE( REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, double); CHECK_NOT_VARIANT(var, double);
CHECK_NOT_VARIANT(var, std::string); CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22, int); REQUIRE_VARIANT(var, 22, int);
} }
{ {
std::variant<double, int, std::string> var; std::variant<double, int, std::string> var;
REQUIRE( REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int); CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string); CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22, double); REQUIRE_VARIANT(var, 22, double);
} }
{ {
std::variant<std::string, double, int> var; std::variant<std::string, double, int> var;
REQUIRE( REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int); CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double); CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22", std::string); REQUIRE_VARIANT(var, "22", std::string);
} }
{ {
std::variant<int> var; std::variant<int> var;
REQUIRE( REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, 22, int); REQUIRE_VARIANT(var, 22, int);
} }
} }
@ -257,24 +250,21 @@ TEST_CASE("testing extract functions for std::variant") {
std::string s = "22.2"; std::string s = "22.2";
{ {
std::variant<int, double, std::string> var; std::variant<int, double, std::string> var;
REQUIRE( REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int); CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string); CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22.2, double); REQUIRE_VARIANT(var, 22.2, double);
} }
{ {
std::variant<double, int, std::string> var; std::variant<double, int, std::string> var;
REQUIRE( REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int); CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string); CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22.2, double); REQUIRE_VARIANT(var, 22.2, double);
} }
{ {
std::variant<std::string, double, int> var; std::variant<std::string, double, int> var;
REQUIRE( REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int); CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double); CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22.2", std::string); REQUIRE_VARIANT(var, "22.2", std::string);
@ -284,48 +274,42 @@ TEST_CASE("testing extract functions for std::variant") {
std::string s = "2.2.2"; std::string s = "2.2.2";
{ {
std::variant<int, double, std::string> var; std::variant<int, double, std::string> var;
REQUIRE( REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int); CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double); CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string); REQUIRE_VARIANT(var, "2.2.2", std::string);
} }
{ {
std::variant<double, std::string, int> var; std::variant<double, std::string, int> var;
REQUIRE( REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int); CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double); CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string); REQUIRE_VARIANT(var, "2.2.2", std::string);
} }
{ {
std::variant<std::string, double, int> var; std::variant<std::string, double, int> var;
REQUIRE( REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int); CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double); CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string); REQUIRE_VARIANT(var, "2.2.2", std::string);
} }
{ {
std::variant<int, double> var; std::variant<int, double> var;
REQUIRE( REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), var));
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, int{}, int); REQUIRE_VARIANT(var, int{}, int);
CHECK_NOT_VARIANT(var, double); CHECK_NOT_VARIANT(var, double);
} }
{ {
std::variant<double, int> var; std::variant<double, int> var;
REQUIRE( REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), var));
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, double{}, double); REQUIRE_VARIANT(var, double{}, double);
CHECK_NOT_VARIANT(var, int); CHECK_NOT_VARIANT(var, int);
} }
{ {
std::variant<int> var; std::variant<int> var;
REQUIRE( REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), var));
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, int{}, int); REQUIRE_VARIANT(var, int{}, int);
} }

View File

@ -85,8 +85,7 @@ TEST_CASE("testing parser") {
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
i.push_back( i.push_back(p.get_object<X, int, double, std::string>());
p.get_object<X, int, double, std::string>());
} }
CHECK(std::equal(i.begin(), i.end(), data.begin())); CHECK(std::equal(i.begin(), i.end(), data.begin()));
@ -121,8 +120,8 @@ TEST_CASE("testing parser") {
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
auto a = p.get_object<X, ss::ax<int, excluded>, double, auto a =
std::string>(); p.get_object<X, ss::ax<int, excluded>, double, std::string>();
if (p.valid()) { if (p.valid()) {
i.push_back(a); i.push_back(a);
} }
@ -138,8 +137,7 @@ TEST_CASE("testing parser") {
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
auto a = p.get_object<X, ss::nx<int, 3>, double, auto a = p.get_object<X, ss::nx<int, 3>, double, std::string>();
std::string>();
if (p.valid()) { if (p.valid()) {
i.push_back(a); i.push_back(a);
} }
@ -219,8 +217,7 @@ TEST_CASE("testing composite conversion") {
constexpr static auto expectedData = std::tuple{10, 20, 11.1}; constexpr static auto expectedData = std::tuple{10, 20, 11.1};
auto [d1, d2, d3, d4] = auto [d1, d2, d3, d4] =
p.try_next<int, int, double>( p.try_next<int, int, double>([](auto& i1, auto i2, double d) {
[](auto& i1, auto i2, double d) {
CHECK(std::tie(i1, i2, d) == expectedData); CHECK(std::tie(i1, i2, d) == expectedData);
}) })
.on_error(fail) .on_error(fail)
@ -277,8 +274,7 @@ TEST_CASE("testing composite conversion") {
{ {
REQUIRE(!p.eof()); REQUIRE(!p.eof());
auto [d1, d2] = auto [d1, d2] = p.try_next<int, double>([](auto&, auto&) { FAIL(""); })
p.try_next<int, double>([](auto&, auto&) { FAIL(""); })
.or_else<test_struct>(expect_test_struct) .or_else<test_struct>(expect_test_struct)
.values(); .values();
@ -326,8 +322,7 @@ TEST_CASE("testing composite conversion") {
{ {
REQUIRE(!p.eof()); REQUIRE(!p.eof());
auto [d1, d2] = auto [d1, d2] = p.try_next<int, std::variant<int, std::string>>()
p.try_next<int, std::variant<int, std::string>>()
.on_error(fail) .on_error(fail)
.or_else<std::tuple<int, std::string>>(fail) .or_else<std::tuple<int, std::string>>(fail)
.on_error(fail) .on_error(fail)
@ -336,8 +331,7 @@ TEST_CASE("testing composite conversion") {
REQUIRE(p.valid()); REQUIRE(p.valid());
REQUIRE(d1); REQUIRE(d1);
REQUIRE(!d2); REQUIRE(!d2);
CHECK(*d1 == CHECK(*d1 == std::tuple{11, std::variant<int, std::string>{"junk"}});
std::tuple{11, std::variant<int, std::string>{"junk"}});
} }
} }