add more splitter tests

This commit is contained in:
ado 2021-01-24 01:45:54 +01:00
parent 24fe96dc44
commit ed6157fa9f

View File

@ -28,11 +28,65 @@ std::vector<std::string> words(const ss::split_input& input) {
} }
[[maybe_unused]] std::string concat(const std::vector<std::string>& v) { [[maybe_unused]] std::string concat(const std::vector<std::string>& v) {
std::string ret = "{"; std::string ret = "[";
for (const auto& i : v) { for (const auto& i : v) {
ret.append(i).append(" "); ret.append(i).append(",");
} }
ret.back() = ('}'); ret.back() = (']');
return ret;
}
template <typename... Ts>
size_t strings_size(const std::string& s, const Ts&... ss) {
if constexpr (sizeof...(Ts) > 0) {
return s.size() + strings_size(ss...);
}
return s.size();
}
template <typename... Ts>
void concat_to(std::string& dst, const std::string& s, const Ts&... ss) {
dst.append(s);
if constexpr (sizeof...(Ts) > 0) {
concat_to(dst, ss...);
}
}
template <typename... Ts>
std::string concat(const Ts&... ss) {
std::string ret;
ret.reserve(strings_size(ss...));
concat_to(ret, ss...);
return ret;
}
using case_type = std::vector<std::string>;
auto spaced(const case_type& input, const std::string& s) {
case_type ret = input;
for (const auto& i : input) {
ret.push_back(concat(s, i, s));
ret.push_back(concat(i, s));
ret.push_back(concat(s, i));
ret.push_back(concat(s, s, i));
ret.push_back(concat(s, s, i, s, s));
ret.push_back(concat(i, s, s));
}
return ret;
}
auto spaced(const case_type& input, const std::string& s1,
const std::string& s2) {
case_type ret = input;
for (const auto& i : input) {
ret.push_back(concat(s1, i, s2));
ret.push_back(concat(s2, i, s1));
ret.push_back(concat(s2, s2, s1, s1, i));
ret.push_back(concat(i, s1, s2, s1, s2));
ret.push_back(concat(s1, s1, s1, i, s2, s2, s2));
ret.push_back(concat(s2, s2, s2, i, s1, s1, s1));
}
return ret; return ret;
} }
@ -43,9 +97,9 @@ std::vector<std::string> combinations(const std::vector<std::string>& v,
} }
std::vector<std::string> ret; std::vector<std::string> ret;
auto inner_combinations = combinations(v, delim, n - 1); auto inner_combinations = combinations(v, delim, n - 1);
for (auto& i : v) { for (const auto& i : v) {
for (auto& j : inner_combinations) { for (const auto& j : inner_combinations) {
ret.push_back(i + delim + j); ret.push_back(concat(i, delim, j));
} }
} }
return ret; return ret;
@ -55,23 +109,22 @@ std::vector<std::vector<std::string>> vector_combinations(
const std::vector<std::string>& v, size_t n) { const std::vector<std::string>& v, size_t n) {
std::vector<std::vector<std::string>> ret; std::vector<std::vector<std::string>> ret;
if (n <= 1) { if (n <= 1) {
for (auto& i : v) { for (const auto& i : v) {
ret.push_back({i}); ret.push_back({i});
} }
return ret; return ret;
} }
auto inner_combinations = vector_combinations(v, n - 1); auto inner_combinations = vector_combinations(v, n - 1);
for (auto& i : v) { for (const auto& i : v) {
for (auto j : inner_combinations) { for (auto j : inner_combinations) {
j.insert(j.begin(), i); j.insert(j.begin(), i);
ret.push_back(std::move(j)); ret.push_back(move(j));
} }
} }
return ret; return ret;
} }
using case_type = std::vector<std::string>;
std::pair<std::vector<std::string>, std::vector<std::vector<std::string>>> std::pair<std::vector<std::string>, std::vector<std::vector<std::string>>>
make_combinations(const std::vector<std::string>& input, make_combinations(const std::vector<std::string>& input,
const std::vector<std::string>& output, const std::vector<std::string>& output,
@ -90,36 +143,6 @@ make_combinations(const std::vector<std::string>& input,
return {std::move(lines), std::move(expectations)}; return {std::move(lines), std::move(expectations)};
} }
auto spaced(const case_type& input, std::string s) {
case_type ret = input;
for (auto& i : input) {
ret.push_back(s + i + s);
ret.push_back(i + s);
ret.push_back(s + i);
ret.push_back(s + s + i);
ret.push_back(s + s + i + s);
ret.push_back(s + s + i + s + s);
ret.push_back(s + i + s + s);
ret.push_back(i + s + s);
}
return ret;
}
auto spaced(const case_type& input, std::string s1, std::string s2) {
case_type ret = input;
for (auto& i : input) {
ret.push_back(s1 + i + s2);
ret.push_back(s2 + i + s1);
ret.push_back(s2 + s2 + s1 + s1 + i);
ret.push_back(i + s1 + s2 + s1 + s2);
ret.push_back(s1 + s1 + s1 + i + s2 + s2 + s2);
ret.push_back(s2 + s2 + s2 + i + s1 + s1 + s1);
}
return ret;
}
} /* namespace */ } /* namespace */
/* ********************************** */ /* ********************************** */
@ -293,30 +316,17 @@ TEST_CASE("testing splitter quote and trim") {
test_combinations<ss::quote<'"'>, ss::trim<' '>>(p, delims); test_combinations<ss::quote<'"'>, ss::trim<' '>>(p, delims);
} }
return; case_type case8 = spaced({R"(",")"}, " ", "\t");
case_type case8 = {R"(",")"}; case_type case9 = spaced({R"("x,")"}, " ", "\t");
case_type case9 = {R"("x,")"}; case_type case10 = spaced({R"(",x")"}, " ", "\t");
case_type case10 = {R"(",x")"}; case_type case11 = spaced({R"("x,x")"}, " ", "\t");
case_type case11 = {R"("x,x")"}; case_type case12 = spaced({R"(",,")"}, " ", "\t");
case_type case12 = {R"(",,")"};
{ {
matches_type p{{case1, "\""}, {case3, ""}, {case8, ","}, matches_type p{{case1, "\""}, {case3, ""}, {case8, ","},
{case9, "x,"}, {case10, ",x"}, {case11, "x,x"}, {case9, "x,"}, {case10, ",x"}, {case11, "x,x"},
{case12, ",,"}}; {case12, ",,"}};
test_combinations<ss::quote<'"'>>(p, {","}); test_combinations<ss::quote<'"'>, ss::trim<' ', '\t'>>(p, {","});
}
case_type case13 = {R"("::")"};
case_type case14 = {R"("x::")"};
case_type case15 = {R"("::x")"};
case_type case16 = {R"("x::x")"};
case_type case17 = {R"("::::")"};
{
matches_type p{{case1, "\""}, {case3, ""}, {case13, "::"},
{case14, "x::"}, {case15, "::x"}, {case16, "x::x"},
{case17, "::::"}};
test_combinations<ss::quote<'"'>>(p, {"::"});
} }
} }
@ -367,3 +377,93 @@ TEST_CASE("testing splitter quote and escape") {
test_combinations<ss::quote<'"'>, ss::escape<'\\', '#'>>(p, {","}); test_combinations<ss::quote<'"'>, ss::escape<'\\', '#'>>(p, {","});
} }
} }
TEST_CASE("testing splitter escape and trim") {
case_type case0 = spaced({R"(\ x\ )", R"(\ \x\ )"}, " ");
case_type case1 = spaced({R"(x)", R"(\x)"}, " ");
case_type case3 = spaced({R"(\\)"}, " ");
std::vector<std::string> delims = {",", "::", "\t", "\n"};
{
matches_type p{{case0, " x "}, {case1, "x"}, {case3, "\\"}};
test_combinations<ss::escape<'\\'>, ss::trim<' '>>(p, delims);
}
case_type case4 = spaced({R"(\,)"}, " ");
case_type case6 = spaced({R"(#,x)"}, " ");
case_type case7 = spaced({R"(x\,x)"}, " ");
{
matches_type p{{case1, "x"},
{case3, "\\"},
{case4, ","},
{case6, ",x"},
{case7, "x,x"}};
test_combinations<ss::escape<'\\', '#'>, ss::trim<' '>>(p, {","});
}
case_type case8 = spaced({R"(\:\:)"}, " ", "\t");
case_type case9 = spaced({R"(x\::x)"}, " ", "\t");
{
matches_type p{{case1, "x"},
{case3, "\\"},
{case8, "::"},
{case9, "x::x"}};
test_combinations<ss::escape<'\\'>, ss::trim<' ', '\t'>>(p, {"::"});
}
}
TEST_CASE("testing splitter quote and escape and trim") {
auto guard = set_combinations_size(3);
case_type case1 = spaced({R"("\"")", R"(\")", R"("""")"}, " ");
case_type case2 =
spaced({R"("x\"x")", R"(x\"x)", R"(x"x)", R"("x""x")"}, " ");
case_type case3 = spaced({R"("")", R"()"}, " ");
case_type case4 = spaced({R"("x")", R"(x)"}, " ");
case_type case5 =
spaced({R"("\"\"")", R"("""""")", R"("\"""")", R"("""\"")"}, " ");
case_type case6 = spaced({R"("\\")", R"(\\)"}, " ");
case_type case7 = spaced({R"("xxxxxxxxxx")", R"(xxxxxxxxxx)"}, " ");
std::vector<std::string> delims = {"::", "\n"};
{
matches_type p{{case1, "\""}, {case2, "x\"x"}, {case3, ""},
{case5, "\"\""}, {case6, "\\"}, {case7, "xxxxxxxxxx"}};
test_combinations<ss::quote<'"'>, ss::escape<'\\'>,
ss::trim<' '>>(p, delims);
}
case_type case8 = spaced({R"('xxxxxxxxxx')", R"(xxxxxxxxxx)"}, " ", "\t");
case_type case9 = spaced({R"('')", R"()"}, " ", "\t");
case_type case10 = spaced({R"('#\')", R"(#\)"}, " ", "\t");
case_type case11 = spaced({R"('#'')", R"(#')", R"('''')"}, " ", "\t");
case_type case12 = spaced({R"('##')", R"(##)"}, " ", "\t");
{
matches_type p{{case8, "xxxxxxxxxx"},
{case9, ""},
{case10, "\\"},
{case11, "'"},
{case12, "#"}};
test_combinations<ss::quote<'\''>, ss::escape<'#'>,
ss::trim<' ', '\t'>>(p, {","});
}
case_type case13 = spaced({R"("x,x")", R"(x\,x)", R"(x#,x)", R"("x\,x")",
R"("x#,x")", R"("x#,x")"},
" ", "\t");
case_type case14 =
spaced({R"("#\\#")", R"(#\\#)", R"(\\##)", R"("\\##")"}, " ", "\t");
{
matches_type p{{case1, "\""},
{case2, "x\"x"},
{case3, ""},
{case13, "x,x"},
{case14, "\\#"}};
test_combinations<ss::quote<'"'>, ss::escape<'\\', '#'>,
ss::trim<' ', '\t'>>(p, {","});
}
}