2020-02-22 19:55:53 +00:00
|
|
|
#include <util/str.hpp>
|
|
|
|
#include <catch2/catch.hpp>
|
|
|
|
|
2020-03-20 04:38:27 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2020-02-22 19:55:53 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2020-02-23 03:08:27 +00:00
|
|
|
TEST_CASE("TrimWhitespace -- positive tests", "[str][trim]")
|
2020-02-22 19:55:53 +00:00
|
|
|
{
|
|
|
|
// Test that things that should be trimmed actually get trimmed
|
|
|
|
auto fee = " J a c k"s;
|
|
|
|
auto fi = "\ra\nd"s;
|
|
|
|
auto fo = "\fthe "s;
|
|
|
|
auto fum = " \t\r\n\v\f Beanstalk\n\n\n\t\r\f\v \n\n\r\f\f\f\f\v"s;
|
|
|
|
for (auto* s: {&fee, &fi, &fo, &fum})
|
2020-02-25 15:48:16 +00:00
|
|
|
*s = llarp::str(llarp::TrimWhitespace(*s));
|
2020-02-22 19:55:53 +00:00
|
|
|
|
|
|
|
REQUIRE( fee == "J a c k" );
|
|
|
|
REQUIRE( fi == "a\nd" );
|
|
|
|
REQUIRE( fo == "the" );
|
|
|
|
REQUIRE( fum == "Beanstalk" );
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("TrimWhitespace -- negative tests", "[str][trim]")
|
|
|
|
{
|
|
|
|
// Test that things that shouldn't be trimmed don't get trimmed
|
|
|
|
auto c = GENERATE(range(std::numeric_limits<char>::min(), std::numeric_limits<char>::max()));
|
|
|
|
std::string plant = c + "bean"s + c;
|
2020-02-25 15:48:16 +00:00
|
|
|
plant = llarp::str(llarp::TrimWhitespace(plant));
|
2020-02-22 19:55:53 +00:00
|
|
|
if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v')
|
|
|
|
REQUIRE( plant == "bean" );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
REQUIRE( plant.size() == 6 );
|
|
|
|
REQUIRE( plant.substr(1, 4) == "bean" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("caseless comparison tests - less than", "[str][lt]") {
|
|
|
|
using namespace llarp;
|
|
|
|
CaselessLessThan lt;
|
2020-02-24 18:04:03 +00:00
|
|
|
// Workaround for gcc 5's stdlib; we can drop this crap (and drop all the `T`'s below) once we
|
|
|
|
// stop supporting it.
|
|
|
|
using T = std::tuple<const char*, const char*>;
|
2020-02-22 19:55:53 +00:00
|
|
|
auto expect_less_than = GENERATE(table<const char*, const char*>({
|
2020-02-24 18:04:03 +00:00
|
|
|
T{"", "1"},
|
|
|
|
T{"1", "11"},
|
|
|
|
T{"abc", "abcd"},
|
|
|
|
T{"ABC", "abcd"},
|
|
|
|
T{"abc", "ABCD"},
|
|
|
|
T{"abc", "Abcd"},
|
|
|
|
T{"abc", "abcD"},
|
|
|
|
T{"abc", "abCd"},
|
|
|
|
T{"abc", "zz"},
|
|
|
|
T{"abc", "zzzz"},
|
|
|
|
T{"abc", "abd"},
|
|
|
|
T{"abc", "aBd"},
|
|
|
|
T{"abc", "abD"},
|
|
|
|
T{"ABC", "abd"},
|
|
|
|
T{"abC", "abd"},
|
2020-02-22 19:55:53 +00:00
|
|
|
}));
|
|
|
|
REQUIRE( lt(std::get<0>(expect_less_than), std::get<1>(expect_less_than)) );
|
|
|
|
REQUIRE( !lt(std::get<1>(expect_less_than), std::get<0>(expect_less_than)) );
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("caseless comparison tests - equality", "[str][eq]") {
|
|
|
|
using namespace llarp;
|
|
|
|
CaselessLessThan lt;
|
2020-02-24 18:04:03 +00:00
|
|
|
using T = std::tuple<const char*, const char*>; // gcc 5 workaround
|
|
|
|
auto expect_equal = GENERATE(table<const char*, const char*>({
|
|
|
|
T{"1", "1"},
|
|
|
|
T{"a", "A"},
|
|
|
|
T{"abc", "ABC"},
|
|
|
|
T{"abc", "aBc"},
|
|
|
|
T{"ABC", "abc"},
|
2020-02-22 19:55:53 +00:00
|
|
|
}));
|
2020-02-24 18:04:03 +00:00
|
|
|
REQUIRE( !lt(std::get<0>(expect_equal), std::get<1>(expect_equal)) );
|
|
|
|
REQUIRE( !lt(std::get<1>(expect_equal), std::get<0>(expect_equal)) );
|
2020-02-22 19:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("truthy string values", "[str][truthy]") {
|
|
|
|
auto val = GENERATE("true", "TruE", "yes", "yeS", "yES", "yes", "YES", "1", "on", "oN", "ON");
|
|
|
|
REQUIRE( llarp::IsTrueValue(val) );
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("falsey string values", "[str][falsey]") {
|
|
|
|
auto val = GENERATE("false", "FalSe", "no", "NO", "No", "nO", "0", "off", "OFF");
|
|
|
|
REQUIRE( llarp::IsFalseValue(val) );
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("neither true nor false string values", "[str][nottruefalse]") {
|
|
|
|
auto val = GENERATE("false y", "maybe", "not on", "2", "yesno", "YESNO", "-1", "default", "OMG");
|
|
|
|
REQUIRE( !llarp::IsTrueValue(val) );
|
|
|
|
REQUIRE( !llarp::IsFalseValue(val) );
|
|
|
|
}
|
2020-03-20 04:38:27 +00:00
|
|
|
|
|
|
|
TEST_CASE("split strings with multiple matches", "[str]") {
|
|
|
|
auto splits = llarp::split("this is a test", ' ');
|
|
|
|
REQUIRE(splits.size() == 4);
|
|
|
|
REQUIRE(splits[0] == "this");
|
|
|
|
REQUIRE(splits[1] == "is");
|
|
|
|
REQUIRE(splits[2] == "a");
|
|
|
|
REQUIRE(splits[3] == "test");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("split strings with single match", "[str]") {
|
|
|
|
auto splits = llarp::split("uno", ';');
|
|
|
|
REQUIRE(splits.size() == 1);
|
|
|
|
REQUIRE(splits[0] == "uno");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("split strings with consecutive delimiters", "[str]") {
|
|
|
|
auto splits = llarp::split("a o e u", ' ');
|
|
|
|
REQUIRE(splits.size() == 4);
|
|
|
|
REQUIRE(splits[0] == "a");
|
|
|
|
REQUIRE(splits[1] == "o");
|
|
|
|
REQUIRE(splits[2] == "e");
|
|
|
|
REQUIRE(splits[3] == "u");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("split delimiter-only string", "[str]") {
|
|
|
|
auto splits = llarp::split(" ", ' ');
|
|
|
|
REQUIRE(splits.size() == 0);
|
|
|
|
|
|
|
|
splits = llarp::split(" ", ' ');
|
|
|
|
REQUIRE(splits.size() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("split empty string", "[str]") {
|
|
|
|
auto splits = llarp::split("", ' ');
|
|
|
|
REQUIRE(splits.size() == 0);
|
|
|
|
}
|