diff --git a/lib/library.cpp b/lib/library.cpp index a85d7e1b5..199bc0ea4 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -181,6 +181,8 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc) else if (strcmp(argnode->Name(), "valid") == 0) { // Validate the validation expression const char *p = argnode->GetText(); + if (*p == '-') + ++p; if (!std::isdigit(*p)) return Error(BAD_ATTRIBUTE_VALUE, argnode->GetText()); for (; *p; p++) { @@ -321,6 +323,10 @@ bool Library::isargvalid(const std::string &functionName, int argnr, const MathL TokenList tokenList(0); std::istringstream istr(ac->valid + ','); tokenList.createTokens(istr,""); + if (Token::Match(tokenList.front(), "- %num% -")) { + tokenList.front()->str("-" + tokenList.front()->strAt(1)); + tokenList.front()->deleteNext(); + } for (const Token *tok = tokenList.front(); tok; tok = tok->next()) { if (tok->isNumber() && argvalue == MathLib::toLongNumber(tok->str())) return true; diff --git a/test/testlibrary.cpp b/test/testlibrary.cpp index 6f237fa10..e036ca7fb 100644 --- a/test/testlibrary.cpp +++ b/test/testlibrary.cpp @@ -33,6 +33,7 @@ private: TEST_CASE(function); TEST_CASE(function_arg); TEST_CASE(function_arg_any); + TEST_CASE(function_arg_valid); TEST_CASE(memory); TEST_CASE(memory2); // define extra "free" allocation functions TEST_CASE(resource); @@ -76,8 +77,7 @@ private: " \n" " \n" " \n" - " 1-\n" - " \n" + " \n" " \n" ""; tinyxml2::XMLDocument doc; @@ -89,8 +89,7 @@ private: ASSERT_EQUALS(true, library.argumentChecks["foo"][2].notnull); ASSERT_EQUALS(true, library.argumentChecks["foo"][3].formatstr); ASSERT_EQUALS(true, library.argumentChecks["foo"][4].strz); - ASSERT_EQUALS("1-", library.argumentChecks["foo"][5].valid); - ASSERT_EQUALS(true, library.argumentChecks["foo"][6].notbool); + ASSERT_EQUALS(true, library.argumentChecks["foo"][5].notbool); } void function_arg_any() const { @@ -108,6 +107,45 @@ private: ASSERT_EQUALS(true, library.argumentChecks["foo"][-1].notuninit); } + void function_arg_valid() const { + const char xmldata[] = "\n" + "\n" + " \n" + " 1-\n" + " -7-0\n" + " 1-5,8\n" + " \n" + ""; + tinyxml2::XMLDocument doc; + doc.Parse(xmldata, sizeof(xmldata)); + + Library library; + library.load(doc); + + // 1- + ASSERT_EQUALS(false, library.isargvalid("foo", 1, -10)); + ASSERT_EQUALS(false, library.isargvalid("foo", 1, 0)); + ASSERT_EQUALS(true, library.isargvalid("foo", 1, 1)); + ASSERT_EQUALS(true, library.isargvalid("foo", 1, 10)); + + // -7-0 + ASSERT_EQUALS(false, library.isargvalid("foo", 2, -10)); + ASSERT_EQUALS(true, library.isargvalid("foo", 2, -7)); + ASSERT_EQUALS(true, library.isargvalid("foo", 2, -3)); + ASSERT_EQUALS(true, library.isargvalid("foo", 2, 0)); + ASSERT_EQUALS(false, library.isargvalid("foo", 2, 1)); + + // 1-5,8 + ASSERT_EQUALS(false, library.isargvalid("foo", 3, 0)); + ASSERT_EQUALS(true, library.isargvalid("foo", 3, 1)); + ASSERT_EQUALS(true, library.isargvalid("foo", 3, 3)); + ASSERT_EQUALS(true, library.isargvalid("foo", 3, 5)); + ASSERT_EQUALS(false, library.isargvalid("foo", 3, 6)); + ASSERT_EQUALS(false, library.isargvalid("foo", 3, 7)); + ASSERT_EQUALS(true, library.isargvalid("foo", 3, 8)); + ASSERT_EQUALS(false, library.isargvalid("foo", 3, 9)); + } + void memory() const { const char xmldata[] = "\n" "\n"