Changeset View
Changeset View
Standalone View
Standalone View
src/test/util_tests.cpp
Show First 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 1317425777), | ||||
"2011-09-30 23:36:17"); | "2011-09-30 23:36:17"); | ||||
BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M", 1317425777), | BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M", 1317425777), | ||||
"2011-09-30 23:36"); | "2011-09-30 23:36"); | ||||
BOOST_CHECK_EQUAL( | BOOST_CHECK_EQUAL( | ||||
DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", 1317425777), | DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", 1317425777), | ||||
"Fri, 30 Sep 2011 23:36:17 +0000"); | "Fri, 30 Sep 2011 23:36:17 +0000"); | ||||
} | } | ||||
class TestArgsManager : public ArgsManager { | struct TestArgsManager : public ArgsManager { | ||||
public: | std::map<std::string, std::string> &GetMapArgs() { return mapArgs; } | ||||
std::map<std::string, std::string> &GetMapArgs() { return mapArgs; }; | |||||
const std::map<std::string, std::vector<std::string>> &GetMapMultiArgs() { | const std::map<std::string, std::vector<std::string>> &GetMapMultiArgs() { | ||||
return mapMultiArgs; | return mapMultiArgs; | ||||
}; | } | ||||
const std::unordered_set<std::string> &GetNegatedArgs() { | |||||
return m_negated_args; | |||||
} | |||||
}; | }; | ||||
BOOST_AUTO_TEST_CASE(util_ParseParameters) { | BOOST_AUTO_TEST_CASE(util_ParseParameters) { | ||||
TestArgsManager testArgs; | TestArgsManager testArgs; | ||||
const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", | const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", | ||||
"-ccc=multiple", "f", "-d=e"}; | "-ccc=multiple", "f", "-d=e"}; | ||||
testArgs.ParseParameters(0, (char **)argv_test); | testArgs.ParseParameters(0, (char **)argv_test); | ||||
Show All 19 Lines | BOOST_CHECK(testArgs.GetMapMultiArgs().count("-a") && | ||||
!testArgs.GetMapMultiArgs().count("f") && | !testArgs.GetMapMultiArgs().count("f") && | ||||
!testArgs.GetMapMultiArgs().count("-d")); | !testArgs.GetMapMultiArgs().count("-d")); | ||||
BOOST_CHECK(testArgs.GetMapArgs()["-a"] == "" && | BOOST_CHECK(testArgs.GetMapArgs()["-a"] == "" && | ||||
testArgs.GetMapArgs()["-ccc"] == "multiple"); | testArgs.GetMapArgs()["-ccc"] == "multiple"); | ||||
BOOST_CHECK(testArgs.GetArgs("-ccc").size() == 2); | BOOST_CHECK(testArgs.GetArgs("-ccc").size() == 2); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(util_GetBoolArg) { | |||||
TestArgsManager testArgs; | |||||
const char *argv_test[] = {"ignored", "-a", "-nob", "-c=0", | |||||
"-d=1", "-e=false", "-f=true"}; | |||||
testArgs.ParseParameters(7, (char **)argv_test); | |||||
// Each letter should be set. | |||||
for (char opt : "abcdef") { | |||||
BOOST_CHECK(testArgs.IsArgSet({'-', opt}) || !opt); | |||||
} | |||||
// Nothing else should be in the map | |||||
BOOST_CHECK(testArgs.GetMapArgs().size() == 6 && | |||||
testArgs.GetMapMultiArgs().size() == 6); | |||||
// The -no prefix should get stripped on the way in. | |||||
BOOST_CHECK(!testArgs.IsArgSet("-nob")); | |||||
// The -b option is flagged as negated, and nothing else is | |||||
BOOST_CHECK(testArgs.IsArgNegated("-b")); | |||||
BOOST_CHECK(testArgs.GetNegatedArgs().size() == 1); | |||||
BOOST_CHECK(!testArgs.IsArgNegated("-a")); | |||||
// Check expected values. | |||||
BOOST_CHECK(testArgs.GetBoolArg("-a", false) == true); | |||||
BOOST_CHECK(testArgs.GetBoolArg("-b", true) == false); | |||||
BOOST_CHECK(testArgs.GetBoolArg("-c", true) == false); | |||||
BOOST_CHECK(testArgs.GetBoolArg("-d", false) == true); | |||||
BOOST_CHECK(testArgs.GetBoolArg("-e", true) == false); | |||||
BOOST_CHECK(testArgs.GetBoolArg("-f", true) == false); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(util_GetBoolArgEdgeCases) { | |||||
// Test some awful edge cases that hopefully no user will ever exercise. | |||||
TestArgsManager testArgs; | |||||
const char *argv_test[] = {"ignored", "-nofoo", "-foo", "-nobar=0"}; | |||||
testArgs.ParseParameters(4, (char **)argv_test); | |||||
// This was passed twice, second one overrides the negative setting. | |||||
BOOST_CHECK(!testArgs.IsArgNegated("-foo")); | |||||
BOOST_CHECK(testArgs.GetBoolArg("-foo", false) == true); | |||||
// A double negative is a positive. | |||||
BOOST_CHECK(testArgs.IsArgNegated("-bar")); | |||||
BOOST_CHECK(testArgs.GetBoolArg("-bar", false) == true); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(util_GetArg) { | BOOST_AUTO_TEST_CASE(util_GetArg) { | ||||
TestArgsManager testArgs; | TestArgsManager testArgs; | ||||
testArgs.GetMapArgs().clear(); | testArgs.GetMapArgs().clear(); | ||||
testArgs.GetMapArgs()["strtest1"] = "string..."; | testArgs.GetMapArgs()["strtest1"] = "string..."; | ||||
// strtest2 undefined on purpose | // strtest2 undefined on purpose | ||||
testArgs.GetMapArgs()["inttest1"] = "12345"; | testArgs.GetMapArgs()["inttest1"] = "12345"; | ||||
testArgs.GetMapArgs()["inttest2"] = "81985529216486895"; | testArgs.GetMapArgs()["inttest2"] = "81985529216486895"; | ||||
// inttest3 undefined on purpose | // inttest3 undefined on purpose | ||||
▲ Show 20 Lines • Show All 565 Lines • Show Last 20 Lines |