Changeset View
Changeset View
Standalone View
Standalone View
src/test/util_tests.cpp
Show First 20 Lines • Show All 431 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(util_ParseKeyValue) { | ||||
std::string key = "--b=abc"; | std::string key = "--b=abc"; | ||||
std::string value; | std::string value; | ||||
BOOST_CHECK(ParseKeyValue(key, value)); | BOOST_CHECK(ParseKeyValue(key, value)); | ||||
BOOST_CHECK_EQUAL(key, "-b"); | BOOST_CHECK_EQUAL(key, "-b"); | ||||
BOOST_CHECK_EQUAL(value, "abc"); | BOOST_CHECK_EQUAL(value, "abc"); | ||||
} | } | ||||
} | } | ||||
static void TestParse(const std::string &str, bool expected_bool, | |||||
int64_t expected_int) { | |||||
TestArgsManager test; | |||||
test.SetupArgs({{"-value", ArgsManager::ALLOW_ANY}}); | |||||
std::string arg = "-value=" + str; | |||||
const char *argv[] = {"ignored", arg.c_str()}; | |||||
std::string error; | |||||
BOOST_CHECK(test.ParseParameters(2, (char **)argv, error)); | |||||
BOOST_CHECK_EQUAL(test.GetBoolArg("-value", false), expected_bool); | |||||
BOOST_CHECK_EQUAL(test.GetBoolArg("-value", true), expected_bool); | |||||
BOOST_CHECK_EQUAL(test.GetArg("-value", 99998), expected_int); | |||||
BOOST_CHECK_EQUAL(test.GetArg("-value", 99999), expected_int); | |||||
} | |||||
// Test bool and int parsing. | |||||
BOOST_AUTO_TEST_CASE(util_ArgParsing) { | |||||
// Some of these cases could be ambiguous or surprising to users, and might | |||||
// be worth triggering errors or warnings in the future. But for now basic | |||||
// test coverage is useful to avoid breaking backwards compatibility | |||||
// unintentionally. | |||||
TestParse("", true, 0); | |||||
TestParse(" ", false, 0); | |||||
TestParse("0", false, 0); | |||||
TestParse("0 ", false, 0); | |||||
TestParse(" 0", false, 0); | |||||
TestParse("+0", false, 0); | |||||
TestParse("-0", false, 0); | |||||
TestParse("5", true, 5); | |||||
TestParse("5 ", true, 5); | |||||
TestParse(" 5", true, 5); | |||||
TestParse("+5", true, 5); | |||||
TestParse("-5", true, -5); | |||||
TestParse("0 5", false, 0); | |||||
TestParse("5 0", true, 5); | |||||
TestParse("050", true, 50); | |||||
TestParse("0.", false, 0); | |||||
TestParse("5.", true, 5); | |||||
TestParse("0.0", false, 0); | |||||
TestParse("0.5", false, 0); | |||||
TestParse("5.0", true, 5); | |||||
TestParse("5.5", true, 5); | |||||
TestParse("x", false, 0); | |||||
TestParse("x0", false, 0); | |||||
TestParse("x5", false, 0); | |||||
TestParse("0x", false, 0); | |||||
TestParse("5x", true, 5); | |||||
TestParse("0x5", false, 0); | |||||
TestParse("false", false, 0); | |||||
TestParse("true", false, 0); | |||||
TestParse("yes", false, 0); | |||||
TestParse("no", false, 0); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(util_GetBoolArg) { | BOOST_AUTO_TEST_CASE(util_GetBoolArg) { | ||||
TestArgsManager testArgs; | TestArgsManager testArgs; | ||||
const auto a = std::make_pair("-a", ArgsManager::ALLOW_ANY); | const auto a = std::make_pair("-a", ArgsManager::ALLOW_ANY); | ||||
const auto b = std::make_pair("-b", ArgsManager::ALLOW_ANY); | const auto b = std::make_pair("-b", ArgsManager::ALLOW_ANY); | ||||
const auto c = std::make_pair("-c", ArgsManager::ALLOW_ANY); | const auto c = std::make_pair("-c", ArgsManager::ALLOW_ANY); | ||||
const auto d = std::make_pair("-d", ArgsManager::ALLOW_ANY); | const auto d = std::make_pair("-d", ArgsManager::ALLOW_ANY); | ||||
const auto e = std::make_pair("-e", ArgsManager::ALLOW_ANY); | const auto e = std::make_pair("-e", ArgsManager::ALLOW_ANY); | ||||
const auto f = std::make_pair("-f", ArgsManager::ALLOW_ANY); | const auto f = std::make_pair("-f", ArgsManager::ALLOW_ANY); | ||||
▲ Show 20 Lines • Show All 1,980 Lines • Show Last 20 Lines |