2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
|
2008-12-18 22:28:57 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-18 22:28:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "testsuite.h"
|
2010-12-30 09:46:44 +01:00
|
|
|
#include "testutils.h"
|
2009-10-25 12:49:06 +01:00
|
|
|
#include "token.h"
|
2010-12-01 18:00:55 +01:00
|
|
|
#include "settings.h"
|
2009-10-25 12:49:06 +01:00
|
|
|
|
|
|
|
#include <cstring>
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
2010-04-05 10:15:30 +02:00
|
|
|
class TestToken : public TestFixture
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
public:
|
2010-04-05 10:15:30 +02:00
|
|
|
TestToken() : TestFixture("TestToken")
|
2008-12-18 22:28:57 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(nextprevious);
|
2009-05-12 20:28:02 +02:00
|
|
|
TEST_CASE(multiCompare);
|
2009-08-30 13:07:10 +02:00
|
|
|
TEST_CASE(getStrLength);
|
2009-09-12 22:54:47 +02:00
|
|
|
TEST_CASE(strValue);
|
2010-01-06 20:19:27 +01:00
|
|
|
|
|
|
|
TEST_CASE(deleteLast);
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
TEST_CASE(matchAny);
|
|
|
|
TEST_CASE(matchNothingOrAnyNotElse);
|
|
|
|
TEST_CASE(matchNumeric);
|
|
|
|
TEST_CASE(matchBoolean);
|
2010-10-25 03:14:21 +02:00
|
|
|
TEST_CASE(matchOr);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void nextprevious()
|
|
|
|
{
|
2010-01-06 20:19:27 +01:00
|
|
|
Token *token = new Token(0);
|
2009-01-05 16:49:57 +01:00
|
|
|
token->str("1");
|
|
|
|
token->insertToken("2");
|
|
|
|
token->next()->insertToken("3");
|
2009-06-14 08:20:51 +02:00
|
|
|
Token *last = token->tokAt(2);
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS(token->str(), "1");
|
|
|
|
ASSERT_EQUALS(token->next()->str(), "2");
|
2009-06-14 08:20:51 +02:00
|
|
|
ASSERT_EQUALS(token->tokAt(2)->str(), "3");
|
2010-04-02 07:30:58 +02:00
|
|
|
if (last->next())
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS("Null was expected", "");
|
|
|
|
|
|
|
|
ASSERT_EQUALS(last->str(), "3");
|
|
|
|
ASSERT_EQUALS(last->previous()->str(), "2");
|
2009-06-14 08:26:20 +02:00
|
|
|
ASSERT_EQUALS(last->tokAt(-2)->str(), "1");
|
2010-04-02 07:30:58 +02:00
|
|
|
if (token->previous())
|
2009-01-05 16:49:57 +01:00
|
|
|
ASSERT_EQUALS("Null was expected", "");
|
2009-03-17 20:59:40 +01:00
|
|
|
|
|
|
|
Tokenizer::deleteTokens(token);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-05-12 20:28:02 +02:00
|
|
|
void multiCompare()
|
|
|
|
{
|
|
|
|
// Test for found
|
|
|
|
ASSERT_EQUALS(1, Token::multiCompare("one|two", "one"));
|
|
|
|
ASSERT_EQUALS(1, Token::multiCompare("one|two", "two"));
|
|
|
|
ASSERT_EQUALS(1, Token::multiCompare("verybig|two|", "two"));
|
|
|
|
|
|
|
|
// Test for empty string found
|
|
|
|
ASSERT_EQUALS(0, Token::multiCompare("|one|two", "notfound"));
|
|
|
|
ASSERT_EQUALS(0, Token::multiCompare("one||two", "notfound"));
|
|
|
|
ASSERT_EQUALS(0, Token::multiCompare("one|two|", "notfound"));
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-05-12 20:28:02 +02:00
|
|
|
// Test for not found
|
|
|
|
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare("one|two", "notfound")));
|
|
|
|
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare("verybig|two", "s")));
|
|
|
|
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare("one|two", "ne")));
|
|
|
|
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare("abc|def", "a")));
|
|
|
|
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare("abc|def", "abcd")));
|
|
|
|
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare("abc|def", "default")));
|
|
|
|
}
|
2009-08-30 13:07:10 +02:00
|
|
|
|
|
|
|
void getStrLength()
|
|
|
|
{
|
2010-01-06 20:19:27 +01:00
|
|
|
Token tok(0);
|
2009-08-30 13:07:10 +02:00
|
|
|
|
2009-09-13 10:07:26 +02:00
|
|
|
tok.str("\"\"");
|
|
|
|
ASSERT_EQUALS(0, Token::getStrLength(&tok));
|
2009-08-30 13:07:10 +02:00
|
|
|
|
2009-09-13 10:07:26 +02:00
|
|
|
tok.str("\"test\"");
|
|
|
|
ASSERT_EQUALS(4, Token::getStrLength(&tok));
|
2009-08-30 13:07:10 +02:00
|
|
|
|
2009-09-13 10:07:26 +02:00
|
|
|
tok.str("\"test \\\\test\"");
|
|
|
|
ASSERT_EQUALS(10, Token::getStrLength(&tok));
|
2009-09-26 17:58:14 +02:00
|
|
|
|
|
|
|
tok.str("\"a\\0\"");
|
|
|
|
ASSERT_EQUALS(1, Token::getStrLength(&tok));
|
2009-08-30 13:07:10 +02:00
|
|
|
}
|
|
|
|
|
2009-09-12 22:54:47 +02:00
|
|
|
void strValue()
|
|
|
|
{
|
2010-01-06 20:19:27 +01:00
|
|
|
Token tok(0);
|
2009-09-12 22:54:47 +02:00
|
|
|
tok.str("\"\"");
|
|
|
|
ASSERT_EQUALS(std::string(""), tok.strValue());
|
|
|
|
|
|
|
|
tok.str("\"0\"");
|
|
|
|
ASSERT_EQUALS(std::string("0"), tok.strValue());
|
|
|
|
}
|
2010-01-06 20:19:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
void deleteLast()
|
|
|
|
{
|
|
|
|
Token *tokensBack = 0;
|
|
|
|
Token tok(&tokensBack);
|
|
|
|
tok.insertToken("aba");
|
2010-01-12 20:24:44 +01:00
|
|
|
ASSERT_EQUALS(true, tokensBack == tok.next());
|
2010-01-06 20:19:27 +01:00
|
|
|
tok.deleteNext();
|
2010-01-12 20:24:44 +01:00
|
|
|
ASSERT_EQUALS(true, tokensBack == &tok);
|
2010-01-06 20:19:27 +01:00
|
|
|
}
|
2010-09-07 20:42:29 +02:00
|
|
|
|
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
void matchAny()
|
2010-09-07 20:42:29 +02:00
|
|
|
{
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize varBitOrVar("abc|def");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(varBitOrVar.tokens(), "%var% | %var%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize varLogOrVar("abc||def");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(varLogOrVar.tokens(), "%var% || %var%"));
|
|
|
|
}
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
void matchNothingOrAnyNotElse()
|
|
|
|
{
|
|
|
|
givenACodeSampleToTokenize emptyString("");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(emptyString.tokens(), "!!else"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(emptyString.tokens(), "!!else something"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize ifSemicolon("if ;");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(ifSemicolon.tokens(), "!!return if"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(ifSemicolon.tokens(), "if ; !!else"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize ifSemicolonSomething("if ; something");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(ifSemicolonSomething.tokens(), "if ; !!else"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize justElse("else");
|
|
|
|
ASSERT_EQUALS(false, Token::Match(justElse.tokens(), "!!else"));
|
|
|
|
|
|
|
|
givenACodeSampleToTokenize ifSemicolonElse("if ; else");
|
|
|
|
ASSERT_EQUALS(false, Token::Match(ifSemicolonElse.tokens(), "if ; !!else"));
|
2010-09-07 20:42:29 +02:00
|
|
|
}
|
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
|
|
|
|
void matchNumeric()
|
2010-09-07 20:42:29 +02:00
|
|
|
{
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize nonNumeric("abc");
|
|
|
|
ASSERT_EQUALS(false, Token::Match(nonNumeric.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize binary("101010b");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(binary.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize octal("0123");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(octal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize decimal("4567");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(decimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize hexadecimal("0xDEADBEEF");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize floatingPoint("0.0f");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize doublePrecision("0.0d");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize unsignedInt("0U");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize unsignedLong("0UL");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize unsignedLongLong("0ULL");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize positive("+666");
|
2010-09-26 08:39:19 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(positive.tokens(), "+ %num%"));
|
|
|
|
|
|
|
|
givenACodeSampleToTokenize negative("-42");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(negative.tokens(), "- %num%"));
|
2010-09-26 05:31:36 +02:00
|
|
|
}
|
2010-09-07 20:42:29 +02:00
|
|
|
|
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
void matchBoolean()
|
|
|
|
{
|
|
|
|
givenACodeSampleToTokenize yes("YES");
|
|
|
|
ASSERT_EQUALS(false, Token::Match(yes.tokens(), "%bool%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize positive("true");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(positive.tokens(), "%bool%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
givenACodeSampleToTokenize negative("false");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(negative.tokens(), "%bool%"));
|
|
|
|
}
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-10-25 03:14:21 +02:00
|
|
|
void matchOr()
|
|
|
|
{
|
|
|
|
givenACodeSampleToTokenize bitwiseOr("|");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(bitwiseOr.tokens(), "%or%"));
|
|
|
|
|
|
|
|
givenACodeSampleToTokenize logicalOr("||");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(logicalOr.tokens(), "%oror%"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(logicalOr.tokens(), "%or%"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(bitwiseOr.tokens(), "%oror%"));
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
};
|
|
|
|
|
2010-04-05 10:15:30 +02:00
|
|
|
REGISTER_TEST(TestToken)
|