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

@@ -4,9 +4,9 @@
#include <algorithm>
TEST_CASE("testing split") {
ss::converter c;
for (const auto& [s, expected, delim] :
// clang-format off
ss::converter c;
for (const auto& [s, expected, delim] :
// clang-format off
{std::tuple{"a,b,c,d", std::vector{"a", "b", "c", "d"}, ","},
{"", {}, " "},
{"a,b,c", {"a", "b", "c"}, ""},
@@ -14,270 +14,265 @@ TEST_CASE("testing split") {
{"a::b::c::d", {"a", "b", "c", "d"}, "::"},
{"x\t-\ty", {"x", "y"}, "\t-\t"},
{"x", {"x"}, ","}} // clang-format on
) {
auto split = c.split(s, delim);
CHECK(split.size() == expected.size());
for (size_t i = 0; i < split.size(); ++i) {
auto s = std::string(split[i].first, split[i].second);
CHECK(s == expected[i]);
}
) {
auto split = c.split(s, delim);
CHECK(split.size() == expected.size());
for (size_t i = 0; i < split.size(); ++i) {
auto s = std::string(split[i].first, split[i].second);
CHECK(s == expected[i]);
}
}
}
TEST_CASE("testing valid conversions") {
ss::converter c;
ss::converter c;
{
auto tup = c.convert<int>("5");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<int, void>("5,junk");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<void, int>("junk,5");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<int, void, void>("5\njunk\njunk", "\n");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<void, int, void>("junk 5 junk", " ");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<void, void, int>("junk\tjunk\t5", "\t");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup =
c.convert<void, void, std::optional<int>>("junk\tjunk\t5",
"\t");
REQUIRE(c.valid());
REQUIRE(tup.has_value());
CHECK(tup == 5);
}
{
auto tup = c.convert<int, double, void>("5,6.6,junk");
REQUIRE(c.valid());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup = c.convert<int, void, double>("5,junk,6.6");
REQUIRE(c.valid());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup = c.convert<void, int, double>("junk;5;6.6", ";");
REQUIRE(c.valid());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup =
c.convert<void, std::optional<int>, double>("junk;5;6.6",
";");
REQUIRE(c.valid());
REQUIRE(std::get<0>(tup).has_value());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup =
c.convert<void, std::optional<int>, double>("junk;5.4;6.6",
";");
REQUIRE(c.valid());
REQUIRE(!std::get<0>(tup).has_value());
CHECK(tup == std::tuple{std::nullopt, 6.6});
}
{
auto tup = c.convert<void, std::variant<int, double>,
double>("junk;5;6.6", ";");
REQUIRE(c.valid());
REQUIRE(std::holds_alternative<int>(std::get<0>(tup)));
CHECK(tup == std::tuple{std::variant<int, double>{5}, 6.6});
}
{
auto tup = c.convert<void, std::variant<int, double>,
double>("junk;5.5;6.6", ";");
REQUIRE(c.valid());
REQUIRE(std::holds_alternative<double>(std::get<0>(tup)));
CHECK(tup == std::tuple{std::variant<int, double>{5.5}, 6.6});
}
{
auto tup = c.convert<int>("5");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<int, void>("5,junk");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<void, int>("junk,5");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<int, void, void>("5\njunk\njunk", "\n");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<void, int, void>("junk 5 junk", " ");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup = c.convert<void, void, int>("junk\tjunk\t5", "\t");
REQUIRE(c.valid());
CHECK(tup == 5);
}
{
auto tup =
c.convert<void, void, std::optional<int>>("junk\tjunk\t5", "\t");
REQUIRE(c.valid());
REQUIRE(tup.has_value());
CHECK(tup == 5);
}
{
auto tup = c.convert<int, double, void>("5,6.6,junk");
REQUIRE(c.valid());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup = c.convert<int, void, double>("5,junk,6.6");
REQUIRE(c.valid());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup = c.convert<void, int, double>("junk;5;6.6", ";");
REQUIRE(c.valid());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup =
c.convert<void, std::optional<int>, double>("junk;5;6.6", ";");
REQUIRE(c.valid());
REQUIRE(std::get<0>(tup).has_value());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup =
c.convert<void, std::optional<int>, double>("junk;5.4;6.6", ";");
REQUIRE(c.valid());
REQUIRE(!std::get<0>(tup).has_value());
CHECK(tup == std::tuple{std::nullopt, 6.6});
}
{
auto tup =
c.convert<void, std::variant<int, double>, double>("junk;5;6.6",
";");
REQUIRE(c.valid());
REQUIRE(std::holds_alternative<int>(std::get<0>(tup)));
CHECK(tup == std::tuple{std::variant<int, double>{5}, 6.6});
}
{
auto tup =
c.convert<void, std::variant<int, double>, double>("junk;5.5;6.6",
";");
REQUIRE(c.valid());
REQUIRE(std::holds_alternative<double>(std::get<0>(tup)));
CHECK(tup == std::tuple{std::variant<int, double>{5.5}, 6.6});
}
}
TEST_CASE("testing invalid conversions") {
ss::converter c;
ss::converter c;
c.convert<int>("");
REQUIRE(!c.valid());
c.convert<int>("");
REQUIRE(!c.valid());
c.convert<int, void>("");
REQUIRE(!c.valid());
c.convert<int, void>("");
REQUIRE(!c.valid());
c.convert<int, void>(",junk");
REQUIRE(!c.valid());
c.convert<int, void>(",junk");
REQUIRE(!c.valid());
c.convert<void, int>("junk,");
REQUIRE(!c.valid());
c.convert<void, int>("junk,");
REQUIRE(!c.valid());
c.convert<int>("x");
REQUIRE(!c.valid());
c.convert<int>("x");
REQUIRE(!c.valid());
c.convert<int, void>("x");
REQUIRE(!c.valid());
c.convert<int, void>("x");
REQUIRE(!c.valid());
c.convert<int, void>("x,junk");
REQUIRE(!c.valid());
c.convert<int, void>("x,junk");
REQUIRE(!c.valid());
c.convert<void, int>("junk,x");
REQUIRE(!c.valid());
c.convert<void, int>("junk,x");
REQUIRE(!c.valid());
c.convert<void, std::variant<int, double>, double>("junk;.5.5;6", ";");
REQUIRE(!c.valid());
c.convert<void, std::variant<int, double>, double>("junk;.5.5;6", ";");
REQUIRE(!c.valid());
}
TEST_CASE("testing ss:ax restriction (all except)") {
ss::converter c;
ss::converter c;
c.convert<ss::ax<int, 0>>("0");
REQUIRE(!c.valid());
c.convert<ss::ax<int, 0>>("0");
REQUIRE(!c.valid());
c.convert<ss::ax<int, 0, 1, 2>>("1");
REQUIRE(!c.valid());
c.convert<ss::ax<int, 0, 1, 2>>("1");
REQUIRE(!c.valid());
c.convert<void, char, ss::ax<int, 0, 1, 2>>("junk,c,1");
REQUIRE(!c.valid());
c.convert<void, char, ss::ax<int, 0, 1, 2>>("junk,c,1");
REQUIRE(!c.valid());
c.convert<ss::ax<int, 1>, char>("1,c");
REQUIRE(!c.valid());
{
int tup = c.convert<ss::ax<int, 1>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
std::tuple<char, int> tup =
c.convert<char, ss::ax<int, 1>>("c,3");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 3});
}
{
std::tuple<int, char> tup =
c.convert<ss::ax<int, 1>, char>("3,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{3, 'c'});
}
c.convert<ss::ax<int, 1>, char>("1,c");
REQUIRE(!c.valid());
{
int tup = c.convert<ss::ax<int, 1>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
std::tuple<char, int> tup = c.convert<char, ss::ax<int, 1>>("c,3");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 3});
}
{
std::tuple<int, char> tup = c.convert<ss::ax<int, 1>, char>("3,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{3, 'c'});
}
}
TEST_CASE("testing ss:nx restriction (none except)") {
ss::converter c;
ss::converter c;
c.convert<ss::nx<int, 1>>("3");
REQUIRE(!c.valid());
c.convert<ss::nx<int, 1>>("3");
REQUIRE(!c.valid());
c.convert<char, ss::nx<int, 1, 2, 69>>("c,3");
REQUIRE(!c.valid());
c.convert<char, ss::nx<int, 1, 2, 69>>("c,3");
REQUIRE(!c.valid());
c.convert<ss::nx<int, 1>, char>("3,c");
REQUIRE(!c.valid());
c.convert<ss::nx<int, 1>, char>("3,c");
REQUIRE(!c.valid());
{
auto tup = c.convert<ss::nx<int, 3>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
auto tup = c.convert<ss::nx<int, 0, 1, 2>>("2");
REQUIRE(c.valid());
CHECK(tup == 2);
}
{
auto tup =
c.convert<char, void, ss::nx<int, 0, 1, 2>>("c,junk,1");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 1});
}
{
auto tup = c.convert<ss::nx<int, 1>, char>("1,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{1, 'c'});
}
{
auto tup = c.convert<ss::nx<int, 3>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
auto tup = c.convert<ss::nx<int, 0, 1, 2>>("2");
REQUIRE(c.valid());
CHECK(tup == 2);
}
{
auto tup = c.convert<char, void, ss::nx<int, 0, 1, 2>>("c,junk,1");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 1});
}
{
auto tup = c.convert<ss::nx<int, 1>, char>("1,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{1, 'c'});
}
}
TEST_CASE("testing ss:ir restriction (in range)") {
ss::converter c;
ss::converter c;
c.convert<ss::ir<int, 0, 2>>("3");
REQUIRE(!c.valid());
c.convert<ss::ir<int, 0, 2>>("3");
REQUIRE(!c.valid());
c.convert<char, ss::ir<int, 4, 69>>("c,3");
REQUIRE(!c.valid());
c.convert<char, ss::ir<int, 4, 69>>("c,3");
REQUIRE(!c.valid());
c.convert<ss::ir<int, 1, 2>, char>("3,c");
REQUIRE(!c.valid());
c.convert<ss::ir<int, 1, 2>, char>("3,c");
REQUIRE(!c.valid());
{
auto tup = c.convert<ss::ir<int, 1, 5>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
auto tup = c.convert<ss::ir<int, 0, 2>>("2");
REQUIRE(c.valid());
CHECK(tup == 2);
}
{
auto tup = c.convert<char, void, ss::ir<int, 0, 1>>("c,junk,1");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 1});
}
{
auto tup = c.convert<ss::ir<int, 1, 20>, char>("1,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{1, 'c'});
}
{
auto tup = c.convert<ss::ir<int, 1, 5>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
auto tup = c.convert<ss::ir<int, 0, 2>>("2");
REQUIRE(c.valid());
CHECK(tup == 2);
}
{
auto tup = c.convert<char, void, ss::ir<int, 0, 1>>("c,junk,1");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 1});
}
{
auto tup = c.convert<ss::ir<int, 1, 20>, char>("1,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{1, 'c'});
}
}
TEST_CASE("testing ss:oor restriction (out of range)") {
ss::converter c;
ss::converter c;
c.convert<ss::oor<int, 1, 5>>("3");
REQUIRE(!c.valid());
c.convert<ss::oor<int, 1, 5>>("3");
REQUIRE(!c.valid());
c.convert<ss::oor<int, 0, 2>>("2");
REQUIRE(!c.valid());
c.convert<ss::oor<int, 0, 2>>("2");
REQUIRE(!c.valid());
c.convert<char, ss::oor<int, 0, 1>, void>("c,1,junk");
REQUIRE(!c.valid());
c.convert<char, ss::oor<int, 0, 1>, void>("c,1,junk");
REQUIRE(!c.valid());
c.convert<ss::oor<int, 1, 20>, char>("1,c");
REQUIRE(!c.valid());
c.convert<ss::oor<int, 1, 20>, char>("1,c");
REQUIRE(!c.valid());
{
auto tup = c.convert<ss::oor<int, 0, 2>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
auto tup = c.convert<ss::oor<int, 0, 2>>("3");
REQUIRE(c.valid());
CHECK(tup == 3);
}
{
auto tup =
c.convert<char, void, ss::oor<int, 4, 69>>("c,junk,3");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 3});
}
{
auto tup = c.convert<char, void, ss::oor<int, 4, 69>>("c,junk,3");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 3});
}
{
auto tup = c.convert<ss::oor<int, 1, 2>, char>("3,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{3, 'c'});
}
{
auto tup = c.convert<ss::oor<int, 1, 2>, char>("3,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{3, 'c'});
}
}
const std::vector<int> extracted_vector = {1, 2, 3};
@@ -286,45 +281,44 @@ const std::vector<int> extracted_vector = {1, 2, 3};
template <>
inline bool ss::extract(const char* begin, const char* end,
std::vector<int>& value) {
if (begin == end) {
return false;
}
value = extracted_vector;
return true;
if (begin == end) {
return false;
}
value = extracted_vector;
return true;
}
TEST_CASE("testing ss:ne restriction (not empty)") {
ss::converter c;
ss::converter c;
c.convert<ss::ne<std::string>>("");
REQUIRE(!c.valid());
c.convert<ss::ne<std::string>>("");
REQUIRE(!c.valid());
c.convert<int, ss::ne<std::string>>("3,");
REQUIRE(!c.valid());
c.convert<int, ss::ne<std::string>>("3,");
REQUIRE(!c.valid());
c.convert<ss::ne<std::string>, int>(",3");
REQUIRE(!c.valid());
c.convert<ss::ne<std::string>, int>(",3");
REQUIRE(!c.valid());
c.convert<void, ss::ne<std::string>, int>("junk,,3");
REQUIRE(!c.valid());
c.convert<void, ss::ne<std::string>, int>("junk,,3");
REQUIRE(!c.valid());
c.convert<ss::ne<std::vector<int>>>("");
REQUIRE(!c.valid());
c.convert<ss::ne<std::vector<int>>>("");
REQUIRE(!c.valid());
{
auto tup = c.convert<ss::ne<std::string>>("s");
REQUIRE(c.valid());
CHECK(tup == "s");
}
{
auto tup =
c.convert<std::optional<int>, ss::ne<std::string>>("1,s");
REQUIRE(c.valid());
CHECK(tup == std::tuple{1, "s"});
}
{
auto tup = c.convert<ss::ne<std::vector<int>>>("{1 2 3}");
REQUIRE(c.valid());
CHECK(tup == extracted_vector);
}
{
auto tup = c.convert<ss::ne<std::string>>("s");
REQUIRE(c.valid());
CHECK(tup == "s");
}
{
auto tup = c.convert<std::optional<int>, ss::ne<std::string>>("1,s");
REQUIRE(c.valid());
CHECK(tup == std::tuple{1, "s"});
}
{
auto tup = c.convert<ss::ne<std::vector<int>>>("{1 2 3}");
REQUIRE(c.valid());
CHECK(tup == extracted_vector);
}
}

View File

@@ -7,59 +7,58 @@ constexpr auto eps = 0.000001;
using ld = long double;
#define CHECK_FLOATING_CONVERSION(input, type) \
{ \
std::string s = #input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(std::abs(t.value() - type(input)) < eps); \
} \
{ \
/* check negative too */ \
auto s = std::string("-") + #input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(std::abs(t.value() - type(-input)) < eps); \
}
{ \
std::string s = #input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(std::abs(t.value() - type(input)) < eps); \
} \
{ \
/* check negative too */ \
auto s = std::string("-") + #input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(std::abs(t.value() - type(-input)) < eps); \
}
TEST_CASE("testing extract functions for floating point values") {
CHECK_FLOATING_CONVERSION(123.456, float);
CHECK_FLOATING_CONVERSION(123.456, double);
CHECK_FLOATING_CONVERSION(123.456, ld);
CHECK_FLOATING_CONVERSION(123.456, float);
CHECK_FLOATING_CONVERSION(123.456, double);
CHECK_FLOATING_CONVERSION(123.456, ld);
CHECK_FLOATING_CONVERSION(69, float);
CHECK_FLOATING_CONVERSION(69, double);
CHECK_FLOATING_CONVERSION(69, ld);
CHECK_FLOATING_CONVERSION(69, float);
CHECK_FLOATING_CONVERSION(69, double);
CHECK_FLOATING_CONVERSION(69, ld);
CHECK_FLOATING_CONVERSION(420., float);
CHECK_FLOATING_CONVERSION(420., double);
CHECK_FLOATING_CONVERSION(420., ld);
CHECK_FLOATING_CONVERSION(420., float);
CHECK_FLOATING_CONVERSION(420., double);
CHECK_FLOATING_CONVERSION(420., ld);
CHECK_FLOATING_CONVERSION(0.123, float);
CHECK_FLOATING_CONVERSION(0.123, double);
CHECK_FLOATING_CONVERSION(0.123, ld);
CHECK_FLOATING_CONVERSION(0.123, float);
CHECK_FLOATING_CONVERSION(0.123, double);
CHECK_FLOATING_CONVERSION(0.123, ld);
CHECK_FLOATING_CONVERSION(123e4, float);
CHECK_FLOATING_CONVERSION(123e4, double);
CHECK_FLOATING_CONVERSION(123e4, ld);
CHECK_FLOATING_CONVERSION(123e4, float);
CHECK_FLOATING_CONVERSION(123e4, double);
CHECK_FLOATING_CONVERSION(123e4, ld);
}
#define CHECK_DECIMAL_CONVERSION(input, type) \
{ \
std::string s = #input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(t.value() == type(input)); \
{ \
std::string s = #input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(t.value() == type(input)); \
} \
{ \
/* check negative too */ \
if (std::is_signed_v<type>) { \
auto s = std::string("-") + #input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(t.value() == type(-input)); \
} \
{ \
/* check negative too */ \
if (std::is_signed_v<type>) { \
auto s = std::string("-") + #input; \
auto t = \
ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(t.value() == type(-input)); \
} \
}
}
using us = unsigned short;
using ui = unsigned int;
@@ -68,266 +67,251 @@ using ll = long long;
using ull = unsigned long long;
TEST_CASE("testing extract functions for decimal values") {
CHECK_DECIMAL_CONVERSION(1234, short);
CHECK_DECIMAL_CONVERSION(1234, us);
CHECK_DECIMAL_CONVERSION(1234, int);
CHECK_DECIMAL_CONVERSION(1234, ui);
CHECK_DECIMAL_CONVERSION(1234, long);
CHECK_DECIMAL_CONVERSION(1234, ul);
CHECK_DECIMAL_CONVERSION(1234, ll);
CHECK_DECIMAL_CONVERSION(1234567891011, ull);
CHECK_DECIMAL_CONVERSION(1234, short);
CHECK_DECIMAL_CONVERSION(1234, us);
CHECK_DECIMAL_CONVERSION(1234, int);
CHECK_DECIMAL_CONVERSION(1234, ui);
CHECK_DECIMAL_CONVERSION(1234, long);
CHECK_DECIMAL_CONVERSION(1234, ul);
CHECK_DECIMAL_CONVERSION(1234, ll);
CHECK_DECIMAL_CONVERSION(1234567891011, ull);
}
#define CHECK_INVALID_CONVERSION(input, type) \
{ \
std::string s = input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(!t.has_value()); \
}
{ \
std::string s = input; \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(!t.has_value()); \
}
TEST_CASE("testing extract functions for numbers with invalid inputs") {
// negative unsigned value
CHECK_INVALID_CONVERSION("-1234", ul);
// negative unsigned value
CHECK_INVALID_CONVERSION("-1234", ul);
// floating pint for int
CHECK_INVALID_CONVERSION("123.4", int);
// floating pint for int
CHECK_INVALID_CONVERSION("123.4", int);
// random input for float
CHECK_INVALID_CONVERSION("xxx1", float);
// random input for float
CHECK_INVALID_CONVERSION("xxx1", float);
// random input for int
CHECK_INVALID_CONVERSION("xxx1", int);
// random input for int
CHECK_INVALID_CONVERSION("xxx1", int);
// empty field for int
CHECK_INVALID_CONVERSION("", int);
// empty field for int
CHECK_INVALID_CONVERSION("", int);
}
#define CHECK_OUT_OF_RANGE_CONVERSION(type) \
{ \
std::string s = \
std::to_string(std::numeric_limits<type>::max()); \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(t.has_value()); \
for (auto& i : s) { \
if (i != '9' && i != '.') { \
i = '9'; \
break; \
} \
} \
t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(!t.has_value()); \
{ \
std::string s = std::to_string(std::numeric_limits<type>::max()); \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(t.has_value()); \
for (auto& i : s) { \
if (i != '9' && i != '.') { \
i = '9'; \
break; \
} \
} \
{ \
std::string s = \
std::to_string(std::numeric_limits<type>::min()); \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(t.has_value()); \
for (auto& i : s) { \
if (std::is_signed_v<type> && i != '9' && i != '.') { \
i = '9'; \
break; \
} else if (std::is_unsigned_v<type>) { \
s = "-1"; \
break; \
} \
} \
t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(!t.has_value()); \
}
t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(!t.has_value()); \
} \
{ \
std::string s = std::to_string(std::numeric_limits<type>::min()); \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(t.has_value()); \
for (auto& i : s) { \
if (std::is_signed_v<type> && i != '9' && i != '.') { \
i = '9'; \
break; \
} else if (std::is_unsigned_v<type>) { \
s = "-1"; \
break; \
} \
} \
t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(!t.has_value()); \
}
TEST_CASE("testing extract functions for numbers with out of range inputs") {
CHECK_OUT_OF_RANGE_CONVERSION(short);
CHECK_OUT_OF_RANGE_CONVERSION(us);
CHECK_OUT_OF_RANGE_CONVERSION(int);
CHECK_OUT_OF_RANGE_CONVERSION(ui);
CHECK_OUT_OF_RANGE_CONVERSION(long);
CHECK_OUT_OF_RANGE_CONVERSION(ul);
CHECK_OUT_OF_RANGE_CONVERSION(ll);
CHECK_OUT_OF_RANGE_CONVERSION(ull);
CHECK_OUT_OF_RANGE_CONVERSION(short);
CHECK_OUT_OF_RANGE_CONVERSION(us);
CHECK_OUT_OF_RANGE_CONVERSION(int);
CHECK_OUT_OF_RANGE_CONVERSION(ui);
CHECK_OUT_OF_RANGE_CONVERSION(long);
CHECK_OUT_OF_RANGE_CONVERSION(ul);
CHECK_OUT_OF_RANGE_CONVERSION(ll);
CHECK_OUT_OF_RANGE_CONVERSION(ull);
}
TEST_CASE("testing extract functions for boolean values") {
for (const auto& [b, s] : {std::pair<bool, std::string>{true, "1"},
{false, "0"},
{true, "true"},
{false, "false"}}) {
bool v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
CHECK(v == b);
}
for (const auto& [b, s] : {std::pair<bool, std::string>{true, "1"},
{false, "0"},
{true, "true"},
{false, "false"}}) {
bool v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
CHECK(v == b);
}
for (const std::string& s : {"2", "tru", "truee", "xxx", ""}) {
bool v;
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v));
}
for (const std::string& s : {"2", "tru", "truee", "xxx", ""}) {
bool v;
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v));
}
}
TEST_CASE("testing extract functions for char values") {
for (const auto& [c, s] :
{std::pair<char, std::string>{'a', "a"}, {'x', "x"}, {' ', " "}}) {
char v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
CHECK(v == c);
}
for (const auto& [c, s] :
{std::pair<char, std::string>{'a', "a"}, {'x', "x"}, {' ', " "}}) {
char v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
CHECK(v == c);
}
for (const std::string& s : {"aa", "xxx", ""}) {
char v;
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v));
}
for (const std::string& s : {"aa", "xxx", ""}) {
char v;
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v));
}
}
TEST_CASE("testing extract functions for std::optional") {
for (const auto& [i, s] :
{std::pair<std::optional<int>, std::string>{1, "1"},
{69, "69"},
{-4, "-4"}}) {
std::optional<int> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(v.has_value());
CHECK(*v == i);
}
for (const auto& [i, s] :
{std::pair<std::optional<int>, std::string>{1, "1"},
{69, "69"},
{-4, "-4"}}) {
std::optional<int> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(v.has_value());
CHECK(*v == i);
}
for (const auto& [c, s] :
{std::pair<std::optional<char>, std::string>{'a', "a"},
{'x', "x"},
{' ', " "}}) {
std::optional<char> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(v.has_value());
CHECK(*v == c);
}
for (const auto& [c, s] :
{std::pair<std::optional<char>, std::string>{'a', "a"},
{'x', "x"},
{' ', " "}}) {
std::optional<char> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(v.has_value());
CHECK(*v == c);
}
for (const std::string& s : {"aa", "xxx", ""}) {
std::optional<int> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(!v.has_value());
}
for (const std::string& s : {"aa", "xxx", ""}) {
std::optional<int> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(!v.has_value());
}
for (const std::string& s : {"aa", "xxx", ""}) {
std::optional<char> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(!v.has_value());
}
for (const std::string& s : {"aa", "xxx", ""}) {
std::optional<char> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(!v.has_value());
}
}
#define REQUIRE_VARIANT(var, el, type) \
{ \
auto ptr = std::get_if<type>(&var); \
REQUIRE(ptr); \
REQUIRE(el == *ptr); \
}
{ \
auto ptr = std::get_if<type>(&var); \
REQUIRE(ptr); \
REQUIRE(el == *ptr); \
}
#define CHECK_NOT_VARIANT(var, type) CHECK(!std::holds_alternative<type>(var));
TEST_CASE("testing extract functions for std::variant") {
{
std::string s = "22";
{
std::string s = "22";
{
std::variant<int, double, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, double);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22, int);
}
{
std::variant<double, int, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22, double);
}
{
std::variant<std::string, double, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22", std::string);
}
{
std::variant<int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, 22, int);
}
std::variant<int, double, std::string> var;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, double);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22, int);
}
{
std::string s = "22.2";
{
std::variant<int, double, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22.2, double);
}
{
std::variant<double, int, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22.2, double);
}
{
std::variant<std::string, double, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22.2", std::string);
}
std::variant<double, int, std::string> var;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22, double);
}
{
std::string s = "2.2.2";
{
std::variant<int, double, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<double, std::string, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<std::string, double, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<int, double> var;
REQUIRE(
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, int{}, int);
CHECK_NOT_VARIANT(var, double);
}
{
std::variant<double, int> var;
REQUIRE(
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, double{}, double);
CHECK_NOT_VARIANT(var, int);
}
{
std::variant<int> var;
REQUIRE(
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, int{}, int);
}
std::variant<std::string, double, int> var;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22", std::string);
}
{
std::variant<int> var;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, 22, int);
}
}
{
std::string s = "22.2";
{
std::variant<int, double, std::string> var;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22.2, double);
}
{
std::variant<double, int, std::string> var;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22.2, double);
}
{
std::variant<std::string, double, int> var;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22.2", std::string);
}
}
{
std::string s = "2.2.2";
{
std::variant<int, double, std::string> var;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<double, std::string, int> var;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<std::string, double, int> var;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<int, double> var;
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, int{}, int);
CHECK_NOT_VARIANT(var, double);
}
{
std::variant<double, int> var;
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, double{}, double);
CHECK_NOT_VARIANT(var, int);
}
{
std::variant<int> var;
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, int{}, int);
}
}
}

View File

@@ -5,172 +5,170 @@
#include <filesystem>
struct unique_file_name {
const std::string name;
const std::string name;
unique_file_name() : name{std::tmpnam(nullptr)} {
}
unique_file_name() : name{std::tmpnam(nullptr)} {
}
~unique_file_name() {
std::filesystem::remove(name);
}
~unique_file_name() {
std::filesystem::remove(name);
}
};
struct X {
constexpr static auto delim = ",";
int i;
double d;
std::string s;
constexpr static auto delim = ",";
int i;
double d;
std::string s;
std::string to_string() const {
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);
}
std::string to_string() const {
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();
return lhs.tied() == rhs.tied();
}
template <typename T>
static void make_and_write(const std::string& file_name,
const std::vector<T>& data) {
std::ofstream out{file_name};
std::vector<const char*> new_lines = {"\n", "\r\n"};
for (size_t i = 0; i < data.size(); ++i) {
out << data[i].to_string() << new_lines[i % new_lines.size()];
}
std::ofstream out{file_name};
std::vector<const char*> new_lines = {"\n", "\r\n"};
for (size_t i = 0; i < data.size(); ++i) {
out << data[i].to_string() << new_lines[i % new_lines.size()];
}
}
TEST_CASE("testing parser") {
unique_file_name f;
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);
{
ss::parser p{f.name, ","};
std::vector<X> i;
unique_file_name f;
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);
{
ss::parser p{f.name, ","};
std::vector<X> i;
while (!p.eof()) {
auto a = p.get_next<int, double, std::string>();
i.emplace_back(ss::to_object<X>(a));
}
CHECK(std::equal(i.begin(), i.end(), data.begin()));
while (!p.eof()) {
auto a = p.get_next<int, double, std::string>();
i.emplace_back(ss::to_object<X>(a));
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
CHECK(std::equal(i.begin(), i.end(), data.begin()));
}
p.ignore_next();
while (!p.eof()) {
using tup = std::tuple<int, double, std::string>;
auto a = p.get_next<tup>();
i.emplace_back(ss::to_object<X>(a));
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
CHECK(std::equal(i.begin(), i.end(), data.begin() + 1));
p.ignore_next();
while (!p.eof()) {
using tup = std::tuple<int, double, std::string>;
auto a = p.get_next<tup>();
i.emplace_back(ss::to_object<X>(a));
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
CHECK(std::equal(i.begin(), i.end(), data.begin() + 1));
}
while (!p.eof()) {
i.push_back(
p.get_object<X, int, double, std::string>());
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
CHECK(std::equal(i.begin(), i.end(), data.begin()));
while (!p.eof()) {
i.push_back(p.get_object<X, int, double, std::string>());
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
CHECK(std::equal(i.begin(), i.end(), data.begin()));
}
while (!p.eof()) {
using tup = std::tuple<int, double, std::string>;
i.push_back(p.get_object<X, tup>());
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
CHECK(std::equal(i.begin(), i.end(), data.begin()));
while (!p.eof()) {
using tup = std::tuple<int, double, std::string>;
i.push_back(p.get_object<X, tup>());
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
CHECK(std::equal(i.begin(), i.end(), data.begin()));
}
while (!p.eof()) {
i.push_back(p.get_next<X>());
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
CHECK(std::equal(i.begin(), i.end(), data.begin()));
while (!p.eof()) {
i.push_back(p.get_next<X>());
}
{
constexpr int excluded = 3;
ss::parser p{f.name, ","};
std::vector<X> i;
CHECK(std::equal(i.begin(), i.end(), data.begin()));
}
while (!p.eof()) {
auto a = p.get_object<X, ss::ax<int, excluded>, double,
std::string>();
if (p.valid()) {
i.push_back(a);
}
}
std::vector<X> expected = data;
std::remove_if(expected.begin(), expected.end(),
[](const X& x) { return x.i == excluded; });
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
{
constexpr int excluded = 3;
ss::parser p{f.name, ","};
std::vector<X> i;
while (!p.eof()) {
auto a =
p.get_object<X, ss::ax<int, excluded>, double, std::string>();
if (p.valid()) {
i.push_back(a);
}
}
std::vector<X> expected = data;
std::remove_if(expected.begin(), expected.end(),
[](const X& x) { return x.i == excluded; });
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
}
{
ss::parser p{f.name, ","};
std::vector<X> i;
{
ss::parser p{f.name, ","};
std::vector<X> i;
while (!p.eof()) {
auto a = p.get_object<X, ss::nx<int, 3>, double,
std::string>();
if (p.valid()) {
i.push_back(a);
}
}
std::vector<X> expected = {{3, 4, "y"}};
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
while (!p.eof()) {
auto a = p.get_object<X, ss::nx<int, 3>, double, std::string>();
if (p.valid()) {
i.push_back(a);
}
}
std::vector<X> expected = {{3, 4, "y"}};
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
}
{
unique_file_name empty_f;
std::vector<X> empty_data = {};
make_and_write(empty_f.name, empty_data);
{
unique_file_name empty_f;
std::vector<X> empty_data = {};
make_and_write(empty_f.name, empty_data);
ss::parser p{empty_f.name, ","};
std::vector<X> i;
ss::parser p{empty_f.name, ","};
std::vector<X> i;
while (!p.eof()) {
i.push_back(p.get_next<X>());
}
CHECK(i.empty());
while (!p.eof()) {
i.push_back(p.get_next<X>());
}
CHECK(i.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);
}
int i;
double d;
char c;
auto tied() {
return std::tie(i, d, c);
}
};
void expect_test_struct(const test_struct&) {
@@ -178,269 +176,265 @@ void expect_test_struct(const test_struct&) {
// various scenarios
TEST_CASE("testing composite conversion") {
unique_file_name f;
{
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"}) {
out << i << std::endl;
}
unique_file_name f;
{
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"}) {
out << i << std::endl;
}
}
ss::parser p{f.name, ","};
auto fail = [] { FAIL(""); };
auto expect_error = [](auto error) { CHECK(!error.empty()); };
ss::parser p{f.name, ","};
auto fail = [] { FAIL(""); };
auto expect_error = [](auto error) { CHECK(!error.empty()); };
REQUIRE(p.valid());
REQUIRE(!p.eof());
{
constexpr static auto expectedData = std::tuple{10, 'a', 11.1};
auto [d1, d2, d3, d4] =
p.try_next<int, int, double>(fail)
.or_else<test_struct>(fail)
.or_else<int, char, double>(
[](auto&& data) { CHECK(data == expectedData); })
.on_error(fail)
.or_else<test_tuple>(fail)
.values();
REQUIRE(p.valid());
REQUIRE(!d1);
REQUIRE(!d2);
REQUIRE(d3);
REQUIRE(!d4);
CHECK(*d3 == expectedData);
}
{
REQUIRE(!p.eof());
constexpr static auto expectedData = std::tuple{10, 20, 11.1};
auto [d1, d2, d3, d4] =
p.try_next<int, int, double>([](auto& i1, auto i2, double d) {
CHECK(std::tie(i1, i2, d) == expectedData);
})
.on_error(fail)
.or_else_object<test_struct, int, double, char>(fail)
.on_error(fail)
.or_else<test_tuple>(fail)
.on_error(fail)
.or_else<int, char, double>(fail)
.values();
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
REQUIRE(!d3);
REQUIRE(!d4);
CHECK(*d1 == expectedData);
}
{
REQUIRE(!p.eof());
{
constexpr static auto expectedData = std::tuple{10, 'a', 11.1};
auto [d1, d2, d3, d4, d5] =
p.try_next<int, int, double>(fail)
.on_error(expect_error)
.or_else_object<test_struct, int, double, char>(fail)
.or_else<test_struct>(fail)
.or_else<test_tuple>(fail)
.or_else<int, char, double>(fail)
.values();
auto [d1, d2, d3, d4] =
p.try_next<int, int, double>(fail)
.or_else<test_struct>(fail)
.or_else<int, char, double>(
[](auto&& data) { CHECK(data == expectedData); })
.on_error(fail)
.or_else<test_tuple>(fail)
.values();
REQUIRE(!p.valid());
REQUIRE(!d1);
REQUIRE(!d2);
REQUIRE(!d3);
REQUIRE(!d4);
REQUIRE(!d5);
}
REQUIRE(p.valid());
REQUIRE(!d1);
REQUIRE(!d2);
REQUIRE(d3);
REQUIRE(!d4);
CHECK(*d3 == expectedData);
}
{
REQUIRE(!p.eof());
{
REQUIRE(!p.eof());
constexpr static auto expectedData = std::tuple{10, 20, 11.1};
auto [d1, d2] =
p.try_next<int, double>([](auto& i, auto& d) {
REQUIRE(std::tie(i, d) == std::tuple{10, 11.1});
})
.or_else<int, double>([](auto&, auto&) { FAIL(""); })
.values();
auto [d1, d2, d3, d4] =
p.try_next<int, int, double>(
[](auto& i1, auto i2, double d) {
CHECK(std::tie(i1, i2, d) == expectedData);
})
.on_error(fail)
.or_else_object<test_struct, int, double, char>(fail)
.on_error(fail)
.or_else<test_tuple>(fail)
.on_error(fail)
.or_else<int, char, double>(fail)
.values();
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
}
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
REQUIRE(!d3);
REQUIRE(!d4);
CHECK(*d1 == expectedData);
}
{
REQUIRE(!p.eof());
{
REQUIRE(!p.eof());
auto [d1, d2] = p.try_next<int, double>([](auto&, auto&) { FAIL(""); })
.or_else<test_struct>(expect_test_struct)
.values();
auto [d1, d2, d3, d4, d5] =
p.try_next<int, int, double>(fail)
.on_error(expect_error)
.or_else_object<test_struct, int, double, char>(fail)
.or_else<test_struct>(fail)
.or_else<test_tuple>(fail)
.or_else<int, char, double>(fail)
.values();
REQUIRE(p.valid());
REQUIRE(!d1);
REQUIRE(d2);
CHECK(d2->tied() == std::tuple{1, 11.1, 'a'});
}
REQUIRE(!p.valid());
REQUIRE(!d1);
REQUIRE(!d2);
REQUIRE(!d3);
REQUIRE(!d4);
REQUIRE(!d5);
}
{
REQUIRE(!p.eof());
{
REQUIRE(!p.eof());
auto [d1, d2, d3, d4, d5] =
p.try_next<int, int, double>(fail)
.or_else_object<test_struct, int, double, char>()
.or_else<test_struct>(expect_test_struct)
.or_else<test_tuple>(fail)
.or_else<std::tuple<int, double>>(fail)
.on_error(expect_error)
.values();
auto [d1, d2] =
p.try_next<int, double>([](auto& i, auto& d) {
REQUIRE(std::tie(i, d) == std::tuple{10, 11.1});
})
.or_else<int, double>([](auto&, auto&) { FAIL(""); })
.values();
REQUIRE(!p.valid());
REQUIRE(!d1);
REQUIRE(!d2);
REQUIRE(!d3);
REQUIRE(!d4);
REQUIRE(!d5);
}
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
}
{
REQUIRE(!p.eof());
{
REQUIRE(!p.eof());
auto [d1, d2] = p.try_next<int, std::optional<int>>()
.on_error(fail)
.or_else<std::tuple<int, std::string>>(fail)
.on_error(fail)
.values();
auto [d1, d2] =
p.try_next<int, double>([](auto&, auto&) { FAIL(""); })
.or_else<test_struct>(expect_test_struct)
.values();
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
CHECK(*d1 == std::tuple{10, std::nullopt});
}
REQUIRE(p.valid());
REQUIRE(!d1);
REQUIRE(d2);
CHECK(d2->tied() == std::tuple{1, 11.1, 'a'});
}
{
REQUIRE(!p.eof());
{
REQUIRE(!p.eof());
auto [d1, d2] = p.try_next<int, std::variant<int, std::string>>()
.on_error(fail)
.or_else<std::tuple<int, std::string>>(fail)
.on_error(fail)
.values();
auto [d1, d2, d3, d4, d5] =
p.try_next<int, int, double>(fail)
.or_else_object<test_struct, int, double, char>()
.or_else<test_struct>(expect_test_struct)
.or_else<test_tuple>(fail)
.or_else<std::tuple<int, double>>(fail)
.on_error(expect_error)
.values();
REQUIRE(!p.valid());
REQUIRE(!d1);
REQUIRE(!d2);
REQUIRE(!d3);
REQUIRE(!d4);
REQUIRE(!d5);
}
{
REQUIRE(!p.eof());
auto [d1, d2] = p.try_next<int, std::optional<int>>()
.on_error(fail)
.or_else<std::tuple<int, std::string>>(fail)
.on_error(fail)
.values();
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
CHECK(*d1 == std::tuple{10, std::nullopt});
}
{
REQUIRE(!p.eof());
auto [d1, d2] =
p.try_next<int, std::variant<int, std::string>>()
.on_error(fail)
.or_else<std::tuple<int, std::string>>(fail)
.on_error(fail)
.values();
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
CHECK(*d1 ==
std::tuple{11, std::variant<int, std::string>{"junk"}});
}
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
CHECK(*d1 == std::tuple{11, std::variant<int, std::string>{"junk"}});
}
}
size_t move_called = 0;
struct my_string {
char* data{nullptr};
char* data{nullptr};
my_string() = default;
my_string() = default;
~my_string() {
delete[] data;
}
~my_string() {
delete[] data;
}
// make sure no object is copied
my_string(const my_string&) = delete;
my_string& operator=(const my_string&) = delete;
// 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} {
move_called++;
other.data = nullptr;
}
my_string(my_string&& other) : data{other.data} {
move_called++;
other.data = nullptr;
}
my_string& operator=(my_string&& other) {
move_called++;
data = other.data;
return *this;
}
my_string& operator=(my_string&& other) {
move_called++;
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;
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);
}
my_string x;
my_string y;
my_string z;
auto tied() {
return std::tie(x, y, z);
}
};
TEST_CASE("testing the moving of parsed values") {
size_t move_called_one_col;
{
unique_file_name f;
{
std::ofstream out{f.name};
out << "x" << std::endl;
}
ss::parser p{f.name, ","};
auto x = p.get_next<my_string>();
CHECK(move_called < 3);
move_called_one_col = move_called;
move_called = 0;
}
size_t move_called_one_col;
{
unique_file_name f;
{
std::ofstream out{f.name};
out << "a,b,c" << std::endl;
std::ofstream out{f.name};
out << "x" << std::endl;
}
{
ss::parser p{f.name, ","};
auto x = p.get_next<my_string>();
CHECK(move_called < 3);
move_called_one_col = move_called;
move_called = 0;
}
ss::parser p{f.name, ","};
auto x = p.get_next<my_string, my_string, my_string>();
CHECK(move_called <= 3 * move_called_one_col);
move_called = 0;
}
unique_file_name f;
{
std::ofstream out{f.name};
out << "a,b,c" << std::endl;
}
{
ss::parser p{f.name, ","};
auto x = p.get_object<xyz, my_string, my_string, my_string>();
CHECK(move_called <= 6 * move_called_one_col);
move_called = 0;
}
{
{
ss::parser p{f.name, ","};
auto x = p.get_next<xyz>();
CHECK(move_called <= 6 * move_called_one_col);
move_called = 0;
}
ss::parser p{f.name, ","};
auto x = p.get_next<my_string, my_string, my_string>();
CHECK(move_called <= 3 * move_called_one_col);
move_called = 0;
}
{
ss::parser p{f.name, ","};
auto x = p.get_object<xyz, my_string, my_string, my_string>();
CHECK(move_called <= 6 * move_called_one_col);
move_called = 0;
}
{
ss::parser p{f.name, ","};
auto x = p.get_next<xyz>();
CHECK(move_called <= 6 * move_called_one_col);
move_called = 0;
}
}
TEST_CASE("testing the moving of parsed composite values") {
// to compile is enough
return;
ss::parser* p;
p->try_next<my_string, my_string, my_string>()
.or_else<my_string, my_string, my_string, my_string>([](auto&&) {})
.or_else<my_string>([](auto&) {})
.or_else<xyz>([](auto&&) {})
.or_else_object<xyz, my_string, my_string, my_string>([](auto&&) {})
.or_else<std::tuple<my_string, my_string, my_string>>(
[](auto&, auto&, auto&) {});
// to compile is enough
return;
ss::parser* p;
p->try_next<my_string, my_string, my_string>()
.or_else<my_string, my_string, my_string, my_string>([](auto&&) {})
.or_else<my_string>([](auto&) {})
.or_else<xyz>([](auto&&) {})
.or_else_object<xyz, my_string, my_string, my_string>([](auto&&) {})
.or_else<std::tuple<my_string, my_string, my_string>>(
[](auto&, auto&, auto&) {});
}