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
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include "testsuite.h"
|
2009-03-17 20:59:40 +01:00
|
|
|
#include "../src/tokenize.h"
|
2009-01-06 15:18:36 +01:00
|
|
|
#include "../src/token.h"
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
|
|
|
class TestTOKEN : public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestTOKEN() : TestFixture("TestTOKEN")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
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);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void nextprevious()
|
|
|
|
{
|
2009-01-03 21:29:20 +01:00
|
|
|
Token *token = new Token;
|
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");
|
2009-01-05 16:49:57 +01:00
|
|
|
if (last->next())
|
|
|
|
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");
|
2009-01-05 16:49:57 +01:00
|
|
|
if (token->previous())
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
Token *tok = new Token();
|
|
|
|
|
|
|
|
tok->str("\"\"");
|
|
|
|
ASSERT_EQUALS(0, Token::getStrLength(tok));
|
|
|
|
|
|
|
|
tok->str("\"test\"");
|
|
|
|
ASSERT_EQUALS(4, Token::getStrLength(tok));
|
|
|
|
|
|
|
|
tok->str("\"test \\\\test\"");
|
|
|
|
ASSERT_EQUALS(10, Token::getStrLength(tok));
|
|
|
|
|
|
|
|
Tokenizer::deleteTokens(tok);
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
};
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
REGISTER_TEST(TestTOKEN)
|