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
|
2013-01-01 17:29:08 +01:00
|
|
|
* Copyright (C) 2007-2013 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
|
|
|
|
2011-11-06 21:20:13 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
2011-10-13 20:53:06 +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:
|
2011-11-06 21:20:13 +01:00
|
|
|
std::vector<std::string> arithmeticalOps;
|
2012-04-23 21:05:26 +02:00
|
|
|
std::vector<std::string> logicalOps;
|
|
|
|
std::vector<std::string> bitOps;
|
|
|
|
std::vector<std::string> comparisionOps;
|
2011-11-06 21:20:13 +01:00
|
|
|
std::vector<std::string> extendedOps;
|
|
|
|
std::vector<std::string> assignmentOps;
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void run() {
|
2011-11-06 21:20:13 +01:00
|
|
|
initOps();
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(nextprevious);
|
2009-05-12 20:28:02 +02:00
|
|
|
TEST_CASE(multiCompare);
|
2011-11-05 14:04:23 +01:00
|
|
|
TEST_CASE(multiCompare2); // #3294 - false negative multi compare between "=" and "=="
|
2011-11-06 22:12:28 +01:00
|
|
|
TEST_CASE(multiCompare3); // false positive for %or% on code using "|="
|
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);
|
2011-10-23 11:23:48 +02:00
|
|
|
TEST_CASE(nextArgument);
|
2011-11-08 18:59:31 +01:00
|
|
|
TEST_CASE(eraseTokens);
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2010-09-26 05:31:36 +02:00
|
|
|
TEST_CASE(matchAny);
|
2011-07-15 19:01:36 +02:00
|
|
|
TEST_CASE(matchSingleChar);
|
2010-09-26 05:31:36 +02:00
|
|
|
TEST_CASE(matchNothingOrAnyNotElse);
|
2011-11-06 18:30:34 +01:00
|
|
|
TEST_CASE(matchType);
|
2012-11-04 16:34:32 +01:00
|
|
|
TEST_CASE(matchChar);
|
2012-12-01 00:47:07 +01:00
|
|
|
TEST_CASE(matchCompOp);
|
2011-11-06 18:30:34 +01:00
|
|
|
TEST_CASE(matchStr);
|
2011-11-06 18:37:00 +01:00
|
|
|
TEST_CASE(matchVarid);
|
2010-09-26 05:31:36 +02:00
|
|
|
TEST_CASE(matchNumeric);
|
|
|
|
TEST_CASE(matchBoolean);
|
2010-10-25 03:14:21 +02:00
|
|
|
TEST_CASE(matchOr);
|
2011-11-06 18:02:18 +01:00
|
|
|
TEST_CASE(matchOp);
|
2011-10-23 20:38:03 +02:00
|
|
|
|
2011-11-06 18:55:02 +01:00
|
|
|
TEST_CASE(isArithmeticalOp);
|
2011-11-06 21:20:13 +01:00
|
|
|
TEST_CASE(isOp);
|
2011-11-06 18:19:27 +01:00
|
|
|
TEST_CASE(isExtendedOp);
|
|
|
|
TEST_CASE(isAssignmentOp);
|
2011-11-06 18:55:02 +01:00
|
|
|
TEST_CASE(isStandardType);
|
2012-04-23 21:05:26 +02:00
|
|
|
TEST_CASE(literals);
|
|
|
|
TEST_CASE(operators);
|
2011-11-06 18:19:27 +01:00
|
|
|
|
2011-10-23 20:38:03 +02:00
|
|
|
TEST_CASE(updateProperties)
|
2011-10-23 21:21:42 +02:00
|
|
|
TEST_CASE(updatePropertiesConcatStr)
|
2011-10-23 20:38:03 +02:00
|
|
|
TEST_CASE(isNameGuarantees1)
|
|
|
|
TEST_CASE(isNameGuarantees2)
|
|
|
|
TEST_CASE(isNameGuarantees3)
|
|
|
|
TEST_CASE(isNameGuarantees4)
|
|
|
|
TEST_CASE(isNameGuarantees5)
|
2012-04-18 16:02:03 +02:00
|
|
|
|
|
|
|
TEST_CASE(canFindMatchingBracketsNeedsOpen);
|
|
|
|
TEST_CASE(canFindMatchingBracketsInnerPair);
|
|
|
|
TEST_CASE(canFindMatchingBracketsOuterPair);
|
|
|
|
TEST_CASE(canFindMatchingBracketsWithTooManyClosing);
|
|
|
|
TEST_CASE(canFindMatchingBracketsWithTooManyOpening);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02: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
|
|
|
|
2012-05-05 18:33:26 +02:00
|
|
|
TokenList::deleteTokens(token);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2012-01-06 09:03:23 +01:00
|
|
|
bool Match(const std::string &code, const std::string &pattern, unsigned int varid=0) {
|
2012-05-05 18:33:26 +02:00
|
|
|
static const Settings settings;
|
2012-01-06 09:03:23 +01:00
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
return Token::Match(tokenizer.tokens(), pattern.c_str(), varid);
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void multiCompare() {
|
2009-05-12 20:28:02 +02:00
|
|
|
// 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")));
|
2011-04-09 18:09:13 +02:00
|
|
|
|
|
|
|
// %op%
|
|
|
|
ASSERT_EQUALS(1, Token::multiCompare("one|%op%", "+"));
|
|
|
|
ASSERT_EQUALS(1, Token::multiCompare("%op%|two", "+"));
|
|
|
|
ASSERT_EQUALS(-1, Token::multiCompare("one|%op%", "x"));
|
|
|
|
ASSERT_EQUALS(-1, Token::multiCompare("%op%|two", "x"));
|
2009-05-12 20:28:02 +02:00
|
|
|
}
|
2009-08-30 13:07:10 +02:00
|
|
|
|
2011-11-05 14:04:23 +01:00
|
|
|
void multiCompare2() { // #3294
|
|
|
|
// Original pattern that failed: [[,(=<>+-*|&^] %num% [+-*/] %num% ]|,|)|;|=|%op%
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize toks("a == 1", true);
|
2011-11-05 19:24:21 +01:00
|
|
|
ASSERT_EQUALS(true, Token::Match(toks.tokens(), "a =|%op%"));
|
2011-11-05 14:04:23 +01:00
|
|
|
}
|
|
|
|
|
2011-11-06 22:12:28 +01:00
|
|
|
void multiCompare3() {
|
|
|
|
// Original pattern that failed: "return|(|&&|%oror% %var% &&|%oror%|==|!=|<=|>=|<|>|-|%or% %var% )|&&|%oror%|;"
|
|
|
|
// Code snippet that failed: "return lv@86 |= rv@87 ;"
|
|
|
|
|
|
|
|
// Note: Also test "reverse" alternative pattern, two different code paths to handle it
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize toks("return a |= b ;", true);
|
2011-11-06 22:12:28 +01:00
|
|
|
ASSERT_EQUALS(false, Token::Match(toks.tokens(), "return %var% xyz|%or% %var% ;"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(toks.tokens(), "return %var% %or%|xyz %var% ;"));
|
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize toks2("return a | b ;", true);
|
2011-11-06 22:12:28 +01:00
|
|
|
ASSERT_EQUALS(true, Token::Match(toks2.tokens(), "return %var% xyz|%or% %var% ;"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(toks2.tokens(), "return %var% %or%|xyz %var% ;"));
|
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize toks3("return a || b ;", true);
|
2011-11-06 22:12:28 +01:00
|
|
|
ASSERT_EQUALS(false, Token::Match(toks3.tokens(), "return %var% xyz|%or% %var% ;"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(toks3.tokens(), "return %var% %or%|xyz %var% ;"));
|
|
|
|
|
|
|
|
ASSERT_EQUALS(true, Token::Match(toks3.tokens(), "return %var% xyz|%oror% %var% ;"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(toks3.tokens(), "return %var% %oror%|xyz %var% ;"));
|
2012-08-30 13:33:19 +02:00
|
|
|
|
|
|
|
givenACodeSampleToTokenize toks4("a % b ;", true);
|
|
|
|
ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%var% >>|<<|&|%or%|^|% %var% ;"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%var% %|>>|<<|&|%or%|^ %var% ;"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%var% >>|<<|&|%or%|%|^ %var% ;"));
|
2012-11-17 20:25:12 +01:00
|
|
|
|
|
|
|
//%var%|%num% support
|
|
|
|
givenACodeSampleToTokenize num("100", true);
|
|
|
|
ASSERT_EQUALS(true, Token::Match(num.tokens(), "%num%|%var%"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(num.tokens(), "%var%|%num%"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(num.tokens(), "%var%|%num%|%bool%"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(num.tokens(), "%var%|%bool%|%num%"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(num.tokens(), "%var%|%bool%|%str%|%num%"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(num.tokens(), "%bool%|%var%"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(num.tokens(), "%type%|%bool%|%char%"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(num.tokens(), "%type%|%bool%|100"));
|
|
|
|
|
|
|
|
givenACodeSampleToTokenize numparen("( 100 )", true);
|
|
|
|
ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %num%|%var% )|"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %var%|%num% )|"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %var%|%num%|%bool% )|"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %var%|%bool%|%num% )|"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %var%|%bool%|%str%|%num% )|"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(numparen.tokens(), "(| %bool%|%var% )|"));
|
|
|
|
|
|
|
|
ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %num%|%var%| )|"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %var%|%num%| )|"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %var%|%num%|%bool%| )|"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %var%|%bool%|%num%| )|"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %var%|%bool%|%str%|%num%| )|"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %bool%|%var%| )|"));
|
2011-11-06 22:12:28 +01:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +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("\"\"");
|
2011-01-01 09:16:31 +01:00
|
|
|
ASSERT_EQUALS(0, (int)Token::getStrLength(&tok));
|
2009-08-30 13:07:10 +02:00
|
|
|
|
2009-09-13 10:07:26 +02:00
|
|
|
tok.str("\"test\"");
|
2011-01-01 09:16:31 +01:00
|
|
|
ASSERT_EQUALS(4, (int)Token::getStrLength(&tok));
|
2009-08-30 13:07:10 +02:00
|
|
|
|
2009-09-13 10:07:26 +02:00
|
|
|
tok.str("\"test \\\\test\"");
|
2011-01-01 09:16:31 +01:00
|
|
|
ASSERT_EQUALS(10, (int)Token::getStrLength(&tok));
|
2009-09-26 17:58:14 +02:00
|
|
|
|
|
|
|
tok.str("\"a\\0\"");
|
2011-01-01 09:16:31 +01:00
|
|
|
ASSERT_EQUALS(1, (int)Token::getStrLength(&tok));
|
2009-08-30 13:07:10 +02:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +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
|
|
|
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void deleteLast() {
|
2010-01-06 20:19:27 +01:00
|
|
|
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
|
|
|
|
2011-10-23 11:23:48 +02:00
|
|
|
void nextArgument() {
|
|
|
|
givenACodeSampleToTokenize example1("foo(1, 2, 3, 4);");
|
2011-10-28 22:30:33 +02:00
|
|
|
ASSERT_EQUALS(true, Token::simpleMatch(example1.tokens()->tokAt(2)->nextArgument(), "2 , 3"));
|
2011-10-23 11:23:48 +02:00
|
|
|
|
|
|
|
givenACodeSampleToTokenize example2("foo();");
|
|
|
|
ASSERT_EQUALS(true, example2.tokens()->tokAt(2)->nextArgument() == 0);
|
|
|
|
|
|
|
|
givenACodeSampleToTokenize example3("foo(bar(a, b), 2, 3);");
|
2011-10-28 22:30:33 +02:00
|
|
|
ASSERT_EQUALS(true, Token::simpleMatch(example3.tokens()->tokAt(2)->nextArgument(), "2 , 3"));
|
2011-10-23 11:23:48 +02:00
|
|
|
}
|
|
|
|
|
2011-11-08 18:59:31 +01:00
|
|
|
void eraseTokens() {
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize code("begin ; { this code will be removed } end", true);
|
2011-11-08 18:59:31 +01:00
|
|
|
Token::eraseTokens(code.tokens()->next(), code.tokens()->tokAt(9));
|
2012-04-17 12:57:16 +02:00
|
|
|
ASSERT_EQUALS("begin ; end", code.tokens()->stringifyList(0, false));
|
2011-11-08 18:59:31 +01:00
|
|
|
}
|
|
|
|
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void matchAny() {
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize varBitOrVar("abc|def", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(varBitOrVar.tokens(), "%var% | %var%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize varLogOrVar("abc||def", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(varLogOrVar.tokens(), "%var% || %var%"));
|
|
|
|
}
|
2011-07-15 19:02:16 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void matchSingleChar() {
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize singleChar("a", true);
|
2011-07-15 19:01:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(singleChar.tokens(), "[a|bc]"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(singleChar.tokens(), "[d|ef]"));
|
|
|
|
|
|
|
|
Token multiChar(0);
|
|
|
|
multiChar.str("[ab");
|
|
|
|
ASSERT_EQUALS(false, Token::Match(&multiChar, "[ab|def]"));
|
|
|
|
}
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void matchNothingOrAnyNotElse() {
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize emptyString("", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
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
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize ifSemicolon("if ;", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
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
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize ifSemicolonSomething("if ; something", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(ifSemicolonSomething.tokens(), "if ; !!else"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize justElse("else", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(false, Token::Match(justElse.tokens(), "!!else"));
|
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize ifSemicolonElse("if ; else", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(false, Token::Match(ifSemicolonElse.tokens(), "if ; !!else"));
|
2010-09-07 20:42:29 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:37:00 +01:00
|
|
|
void matchType() {
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize type("abc", true);
|
2011-11-06 18:30:34 +01:00
|
|
|
ASSERT_EQUALS(true, Token::Match(type.tokens(), "%type%"));
|
|
|
|
|
|
|
|
givenACodeSampleToTokenize isVar("int a = 3 ;");
|
|
|
|
ASSERT_EQUALS(true, Token::Match(isVar.tokens(), "%type%"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(isVar.tokens(), "%type% %var%"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(isVar.tokens(), "%type% %type%"));
|
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize noType("delete", true);
|
2011-11-06 18:30:34 +01:00
|
|
|
ASSERT_EQUALS(false, Token::Match(noType.tokens(), "%type%"));
|
|
|
|
}
|
|
|
|
|
2012-11-04 16:34:32 +01:00
|
|
|
void matchChar() {
|
|
|
|
givenACodeSampleToTokenize chr1("'a'", true);
|
|
|
|
ASSERT_EQUALS(true, Token::Match(chr1.tokens(), "%char%"));
|
|
|
|
|
|
|
|
givenACodeSampleToTokenize chr2("'1'", true);
|
|
|
|
ASSERT_EQUALS(true, Token::Match(chr2.tokens(), "%char%"));
|
|
|
|
|
|
|
|
givenACodeSampleToTokenize noChr("\"10\"", true);
|
|
|
|
ASSERT_EQUALS(false, Token::Match(noChr.tokens(), "%char%"));
|
|
|
|
}
|
|
|
|
|
2012-12-01 00:47:07 +01:00
|
|
|
void matchCompOp() {
|
|
|
|
givenACodeSampleToTokenize comp1("<=", true);
|
|
|
|
ASSERT_EQUALS(true, Token::Match(comp1.tokens(), "%comp%"));
|
|
|
|
|
|
|
|
givenACodeSampleToTokenize comp2(">", true);
|
|
|
|
ASSERT_EQUALS(true, Token::Match(comp2.tokens(), "%comp%"));
|
|
|
|
|
|
|
|
givenACodeSampleToTokenize noComp("=", true);
|
|
|
|
ASSERT_EQUALS(false, Token::Match(noComp.tokens(), "%comp%"));
|
|
|
|
}
|
|
|
|
|
2011-11-06 18:37:00 +01:00
|
|
|
void matchStr() {
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize noStr1("abc", true);
|
2011-11-06 18:30:34 +01:00
|
|
|
ASSERT_EQUALS(false, Token::Match(noStr1.tokens(), "%str%"));
|
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize noStr2("'a'", true);
|
2011-11-06 18:30:34 +01:00
|
|
|
ASSERT_EQUALS(false, Token::Match(noStr2.tokens(), "%str%"));
|
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize str("\"abc\"", true);
|
2011-11-06 18:30:34 +01:00
|
|
|
ASSERT_EQUALS(true, Token::Match(str.tokens(), "%str%"));
|
2011-11-06 21:20:13 +01:00
|
|
|
|
|
|
|
// Empty string
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize emptyStr("\"\"", true);
|
2011-11-06 21:20:13 +01:00
|
|
|
ASSERT_EQUALS(true, Token::Match(emptyStr.tokens(), "%str%"));
|
2011-11-06 18:30:34 +01:00
|
|
|
}
|
2010-09-26 05:31:36 +02:00
|
|
|
|
2011-11-06 18:37:00 +01:00
|
|
|
void matchVarid() {
|
|
|
|
givenACodeSampleToTokenize var("int a ; int b ;");
|
2011-11-20 22:38:37 +01:00
|
|
|
|
|
|
|
// Varid == 0 should throw exception
|
2012-01-08 21:19:44 +01:00
|
|
|
ASSERT_THROW(Token::Match(var.tokens(), "%type% %varid% ; %type% %var%", 0),InternalError);
|
2011-11-06 18:37:00 +01:00
|
|
|
|
|
|
|
ASSERT_EQUALS(true, Token::Match(var.tokens(), "%type% %varid% ; %type% %var%", 1));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(var.tokens(), "%type% %var% ; %type% %varid%", 2));
|
|
|
|
|
|
|
|
// Try to match two different varids in one match call
|
|
|
|
ASSERT_EQUALS(false, Token::Match(var.tokens(), "%type% %varid% ; %type% %varid%", 2));
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void matchNumeric() {
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize nonNumeric("abc", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(false, Token::Match(nonNumeric.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize binary("101010b", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(binary.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize octal("0123", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(octal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize decimal("4567", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(decimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize hexadecimal("0xDEADBEEF", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize floatingPoint("0.0f", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize doublePrecision("0.0d", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize unsignedInt("0U", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize unsignedLong("0UL", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize unsignedLongLong("0ULL", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize positive("+666", true);
|
2010-09-26 08:39:19 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(positive.tokens(), "+ %num%"));
|
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize negative("-42", true);
|
2010-09-26 08:39:19 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(negative.tokens(), "- %num%"));
|
2010-09-26 05:31:36 +02:00
|
|
|
}
|
2010-09-07 20:42:29 +02:00
|
|
|
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void matchBoolean() {
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize yes("YES", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(false, Token::Match(yes.tokens(), "%bool%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize positive("true", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(positive.tokens(), "%bool%"));
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize negative("false", true);
|
2010-09-26 05:31:36 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(negative.tokens(), "%bool%"));
|
|
|
|
}
|
2010-09-07 20:42:29 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void matchOr() {
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize bitwiseOr("|", true);
|
2011-07-31 19:26:26 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(bitwiseOr.tokens(), "%or%"));
|
2011-11-06 18:02:18 +01:00
|
|
|
ASSERT_EQUALS(true, Token::Match(bitwiseOr.tokens(), "%op%"));
|
2011-07-31 19:26:26 +02:00
|
|
|
ASSERT_EQUALS(false, Token::Match(bitwiseOr.tokens(), "%oror%"));
|
2010-10-25 03:14:21 +02:00
|
|
|
|
2011-11-06 22:12:28 +01:00
|
|
|
givenACodeSampleToTokenize bitwiseOrAssignment("|=");
|
|
|
|
ASSERT_EQUALS(false, Token::Match(bitwiseOrAssignment.tokens(), "%or%"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(bitwiseOrAssignment.tokens(), "%op%"));
|
|
|
|
ASSERT_EQUALS(false, Token::Match(bitwiseOrAssignment.tokens(), "%oror%"));
|
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize logicalOr("||", true);
|
2010-10-25 03:14:21 +02:00
|
|
|
ASSERT_EQUALS(false, Token::Match(logicalOr.tokens(), "%or%"));
|
2011-11-06 18:02:18 +01:00
|
|
|
ASSERT_EQUALS(true, Token::Match(logicalOr.tokens(), "%op%"));
|
2011-07-31 19:26:26 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(logicalOr.tokens(), "%oror%"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(logicalOr.tokens(), "&&|%oror%"));
|
|
|
|
ASSERT_EQUALS(true, Token::Match(logicalOr.tokens(), "%oror%|&&"));
|
2011-07-16 13:28:26 +02:00
|
|
|
|
2012-05-16 18:04:03 +02:00
|
|
|
givenACodeSampleToTokenize logicalAnd("&&", true);
|
2011-10-28 22:30:33 +02:00
|
|
|
ASSERT_EQUALS(true, Token::simpleMatch(logicalAnd.tokens(), "&&"));
|
2011-07-16 13:28:26 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(logicalAnd.tokens(), "&&|%oror%"));
|
2011-07-31 19:26:26 +02:00
|
|
|
ASSERT_EQUALS(true, Token::Match(logicalAnd.tokens(), "%oror%|&&"));
|
2010-10-25 03:14:21 +02:00
|
|
|
}
|
2011-04-10 11:15:57 +02:00
|
|
|
|
2012-05-17 10:33:24 +02:00
|
|
|
void append_vector(std::vector<std::string> &dest, const std::vector<std::string> &src) const {
|
2011-11-06 21:20:13 +01:00
|
|
|
dest.insert(dest.end(), src.begin(), src.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void initOps() {
|
|
|
|
arithmeticalOps.push_back("+");
|
|
|
|
arithmeticalOps.push_back("-");
|
|
|
|
arithmeticalOps.push_back("*");
|
|
|
|
arithmeticalOps.push_back("/");
|
|
|
|
arithmeticalOps.push_back("%");
|
|
|
|
arithmeticalOps.push_back("<<");
|
|
|
|
arithmeticalOps.push_back(">>");
|
|
|
|
|
2012-04-23 21:05:26 +02:00
|
|
|
logicalOps.push_back("&&");
|
|
|
|
logicalOps.push_back("||");
|
|
|
|
logicalOps.push_back("!");
|
|
|
|
comparisionOps.push_back("==");
|
|
|
|
comparisionOps.push_back("!=");
|
|
|
|
comparisionOps.push_back("<");
|
|
|
|
comparisionOps.push_back("<=");
|
|
|
|
comparisionOps.push_back(">");
|
|
|
|
comparisionOps.push_back(">=");
|
|
|
|
bitOps.push_back("&");
|
|
|
|
bitOps.push_back("|");
|
|
|
|
bitOps.push_back("^");
|
|
|
|
bitOps.push_back("~");
|
2011-11-06 21:20:13 +01:00
|
|
|
|
|
|
|
extendedOps.push_back(",");
|
|
|
|
extendedOps.push_back("[");
|
|
|
|
extendedOps.push_back("]");
|
|
|
|
extendedOps.push_back("(");
|
|
|
|
extendedOps.push_back(")");
|
|
|
|
extendedOps.push_back("?");
|
|
|
|
extendedOps.push_back(":");
|
|
|
|
|
|
|
|
assignmentOps.push_back("=");
|
|
|
|
assignmentOps.push_back("+=");
|
|
|
|
assignmentOps.push_back("-=");
|
|
|
|
assignmentOps.push_back("*=");
|
|
|
|
assignmentOps.push_back("/=");
|
|
|
|
assignmentOps.push_back("%=");
|
|
|
|
assignmentOps.push_back("&=");
|
|
|
|
assignmentOps.push_back("^=");
|
|
|
|
assignmentOps.push_back("|=");
|
|
|
|
assignmentOps.push_back("<<=");
|
|
|
|
assignmentOps.push_back(">>=");
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
void matchOp() {
|
2011-11-06 21:20:13 +01:00
|
|
|
std::vector<std::string> test_ops;
|
|
|
|
append_vector(test_ops, arithmeticalOps);
|
2012-04-23 21:05:26 +02:00
|
|
|
append_vector(test_ops, bitOps);
|
|
|
|
append_vector(test_ops, comparisionOps);
|
|
|
|
append_vector(test_ops, logicalOps);
|
2011-11-06 21:20:13 +01:00
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator test_op, test_ops_end = test_ops.end();
|
|
|
|
for (test_op = test_ops.begin(); test_op != test_ops_end; ++test_op) {
|
2012-01-06 09:03:23 +01:00
|
|
|
ASSERT_EQUALS(true, Match(*test_op, "%op%"));
|
2011-11-06 21:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Negative test against other operators
|
|
|
|
std::vector<std::string> other_ops;
|
|
|
|
append_vector(other_ops, extendedOps);
|
|
|
|
append_vector(other_ops, assignmentOps);
|
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator other_op, other_ops_end = other_ops.end();
|
|
|
|
for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) {
|
2012-01-06 09:03:23 +01:00
|
|
|
ASSERT_EQUALS_MSG(false, Match(*other_op, "%op%"), "Failing other operator: " + *other_op);
|
2011-11-06 21:20:13 +01:00
|
|
|
}
|
2011-04-10 11:15:57 +02:00
|
|
|
}
|
2011-10-23 20:38:03 +02:00
|
|
|
|
2011-11-06 18:55:02 +01:00
|
|
|
void isArithmeticalOp() {
|
2011-11-06 21:20:13 +01:00
|
|
|
std::vector<std::string>::const_iterator test_op, test_ops_end = arithmeticalOps.end();
|
|
|
|
for (test_op = arithmeticalOps.begin(); test_op != test_ops_end; ++test_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*test_op);
|
|
|
|
ASSERT_EQUALS(true, tok.isArithmeticalOp());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Negative test against other operators
|
|
|
|
std::vector<std::string> other_ops;
|
2012-04-23 21:05:26 +02:00
|
|
|
append_vector(other_ops, bitOps);
|
|
|
|
append_vector(other_ops, comparisionOps);
|
|
|
|
append_vector(other_ops, logicalOps);
|
2011-11-06 21:20:13 +01:00
|
|
|
append_vector(other_ops, extendedOps);
|
|
|
|
append_vector(other_ops, assignmentOps);
|
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator other_op, other_ops_end = other_ops.end();
|
|
|
|
for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*other_op);
|
|
|
|
ASSERT_EQUALS_MSG(false, tok.isArithmeticalOp(), "Failing arithmetical operator: " + *other_op);
|
|
|
|
}
|
|
|
|
}
|
2011-11-06 18:55:02 +01:00
|
|
|
|
2011-11-06 21:20:13 +01:00
|
|
|
void isOp() {
|
|
|
|
std::vector<std::string> test_ops;
|
|
|
|
append_vector(test_ops, arithmeticalOps);
|
2012-04-23 21:05:26 +02:00
|
|
|
append_vector(test_ops, bitOps);
|
|
|
|
append_vector(test_ops, comparisionOps);
|
|
|
|
append_vector(test_ops, logicalOps);
|
2011-11-06 21:20:13 +01:00
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator test_op, test_ops_end = test_ops.end();
|
|
|
|
for (test_op = test_ops.begin(); test_op != test_ops_end; ++test_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*test_op);
|
|
|
|
ASSERT_EQUALS(true, tok.isOp());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Negative test against other operators
|
|
|
|
std::vector<std::string> other_ops;
|
|
|
|
append_vector(other_ops, extendedOps);
|
|
|
|
append_vector(other_ops, assignmentOps);
|
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator other_op, other_ops_end = other_ops.end();
|
|
|
|
for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*other_op);
|
|
|
|
ASSERT_EQUALS_MSG(false, tok.isOp(), "Failing normal operator: " + *other_op);
|
|
|
|
}
|
2011-11-06 18:55:02 +01:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:19:27 +01:00
|
|
|
void isExtendedOp() {
|
2011-11-06 21:20:13 +01:00
|
|
|
std::vector<std::string> test_ops;
|
|
|
|
append_vector(test_ops, arithmeticalOps);
|
2012-04-23 21:05:26 +02:00
|
|
|
append_vector(test_ops, bitOps);
|
|
|
|
append_vector(test_ops, comparisionOps);
|
|
|
|
append_vector(test_ops, logicalOps);
|
2011-11-06 21:20:13 +01:00
|
|
|
append_vector(test_ops, extendedOps);
|
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator test_op, test_ops_end = test_ops.end();
|
|
|
|
for (test_op = test_ops.begin(); test_op != test_ops_end; ++test_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*test_op);
|
|
|
|
ASSERT_EQUALS(true, tok.isExtendedOp());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Negative test against assignment operators
|
|
|
|
std::vector<std::string>::const_iterator other_op, other_ops_end = assignmentOps.end();
|
|
|
|
for (other_op = assignmentOps.begin(); other_op != other_ops_end; ++other_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*other_op);
|
|
|
|
ASSERT_EQUALS_MSG(false, tok.isExtendedOp(), "Failing assignment operator: " + *other_op);
|
|
|
|
}
|
2011-11-06 18:19:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void isAssignmentOp() {
|
2011-11-06 21:20:13 +01:00
|
|
|
std::vector<std::string>::const_iterator test_op, test_ops_end = assignmentOps.end();
|
|
|
|
for (test_op = assignmentOps.begin(); test_op != test_ops_end; ++test_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*test_op);
|
|
|
|
ASSERT_EQUALS(true, tok.isAssignmentOp());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Negative test against other operators
|
|
|
|
std::vector<std::string> other_ops;
|
|
|
|
append_vector(other_ops, arithmeticalOps);
|
2012-04-23 21:05:26 +02:00
|
|
|
append_vector(other_ops, bitOps);
|
|
|
|
append_vector(other_ops, comparisionOps);
|
|
|
|
append_vector(other_ops, logicalOps);
|
2011-11-06 21:20:13 +01:00
|
|
|
append_vector(other_ops, extendedOps);
|
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator other_op, other_ops_end = other_ops.end();
|
|
|
|
for (other_op = other_ops.begin(); other_op != other_ops_end; ++other_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*other_op);
|
|
|
|
ASSERT_EQUALS_MSG(false, tok.isAssignmentOp(), "Failing assignment operator: " + *other_op);
|
|
|
|
}
|
2011-11-06 18:19:27 +01:00
|
|
|
}
|
|
|
|
|
2012-04-23 21:05:26 +02:00
|
|
|
void operators() {
|
|
|
|
std::vector<std::string>::const_iterator test_op;
|
|
|
|
for (test_op = extendedOps.begin(); test_op != extendedOps.end(); ++test_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*test_op);
|
|
|
|
ASSERT_EQUALS(Token::eExtendedOp, tok.type());
|
|
|
|
}
|
|
|
|
for (test_op = logicalOps.begin(); test_op != logicalOps.end(); ++test_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*test_op);
|
|
|
|
ASSERT_EQUALS(Token::eLogicalOp, tok.type());
|
|
|
|
}
|
|
|
|
for (test_op = bitOps.begin(); test_op != bitOps.end(); ++test_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*test_op);
|
|
|
|
ASSERT_EQUALS(Token::eBitOp, tok.type());
|
|
|
|
}
|
|
|
|
for (test_op = comparisionOps.begin(); test_op != comparisionOps.end(); ++test_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*test_op);
|
2012-04-26 23:04:55 +02:00
|
|
|
ASSERT_EQUALS(Token::eComparisonOp, tok.type());
|
2012-04-23 21:05:26 +02:00
|
|
|
}
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str("++");
|
|
|
|
ASSERT_EQUALS(Token::eIncDecOp, tok.type());
|
|
|
|
tok.str("--");
|
|
|
|
ASSERT_EQUALS(Token::eIncDecOp, tok.type());
|
|
|
|
}
|
|
|
|
|
|
|
|
void literals() {
|
|
|
|
Token tok(NULL);
|
|
|
|
|
|
|
|
tok.str("\"foo\"");
|
|
|
|
ASSERT(tok.type() == Token::eString);
|
|
|
|
tok.str("\"\"");
|
|
|
|
ASSERT(tok.type() == Token::eString);
|
|
|
|
tok.str("'f'");
|
|
|
|
ASSERT(tok.type() == Token::eChar);
|
|
|
|
tok.str("12345");
|
|
|
|
ASSERT(tok.type() == Token::eNumber);
|
|
|
|
tok.str("-55");
|
|
|
|
ASSERT(tok.type() == Token::eNumber);
|
|
|
|
tok.str("true");
|
|
|
|
ASSERT(tok.type() == Token::eBoolean);
|
|
|
|
tok.str("false");
|
|
|
|
ASSERT(tok.type() == Token::eBoolean);
|
|
|
|
}
|
|
|
|
|
2011-11-06 18:55:02 +01:00
|
|
|
void isStandardType() {
|
2011-11-06 21:20:13 +01:00
|
|
|
std::vector<std::string> standard_types;
|
|
|
|
standard_types.push_back("bool");
|
|
|
|
standard_types.push_back("char");
|
|
|
|
standard_types.push_back("short");
|
|
|
|
standard_types.push_back("int");
|
|
|
|
standard_types.push_back("long");
|
|
|
|
standard_types.push_back("float");
|
|
|
|
standard_types.push_back("double");
|
|
|
|
standard_types.push_back("size_t");
|
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator test_op, test_ops_end = standard_types.end();
|
|
|
|
for (test_op = standard_types.begin(); test_op != test_ops_end; ++test_op) {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str(*test_op);
|
|
|
|
ASSERT_EQUALS_MSG(true, tok.isStandardType(), "Failing standard type: " + *test_op);
|
|
|
|
}
|
2011-11-06 18:55:02 +01:00
|
|
|
|
|
|
|
// Negative test
|
2011-11-06 21:20:13 +01:00
|
|
|
Token tok(0);
|
2011-11-06 18:55:02 +01:00
|
|
|
tok.str("string");
|
|
|
|
ASSERT_EQUALS(false, tok.isStandardType());
|
2011-11-09 21:45:59 +01:00
|
|
|
|
|
|
|
// Change back to standard type
|
|
|
|
tok.str("int");
|
|
|
|
ASSERT_EQUALS(true, tok.isStandardType());
|
2011-11-06 18:55:02 +01:00
|
|
|
}
|
|
|
|
|
2011-10-23 20:38:03 +02:00
|
|
|
void updateProperties() {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str("foobar");
|
|
|
|
|
|
|
|
ASSERT_EQUALS(true, tok.isName());
|
|
|
|
ASSERT_EQUALS(false, tok.isNumber());
|
|
|
|
|
|
|
|
tok.str("123456");
|
|
|
|
|
|
|
|
ASSERT_EQUALS(false, tok.isName());
|
|
|
|
ASSERT_EQUALS(true, tok.isNumber());
|
|
|
|
}
|
|
|
|
|
2011-10-23 21:21:42 +02:00
|
|
|
void updatePropertiesConcatStr() {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str("true");
|
|
|
|
|
|
|
|
ASSERT_EQUALS(true, tok.isBoolean());
|
|
|
|
|
|
|
|
tok.concatStr("123");
|
|
|
|
|
|
|
|
ASSERT_EQUALS(false, tok.isBoolean());
|
|
|
|
ASSERT_EQUALS("tru23", tok.str());
|
|
|
|
}
|
|
|
|
|
2011-10-23 20:38:03 +02:00
|
|
|
void isNameGuarantees1() {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str("Name");
|
|
|
|
ASSERT_EQUALS(true, tok.isName());
|
|
|
|
}
|
|
|
|
|
|
|
|
void isNameGuarantees2() {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str("_name");
|
|
|
|
ASSERT_EQUALS(true, tok.isName());
|
|
|
|
}
|
|
|
|
|
|
|
|
void isNameGuarantees3() {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str("_123");
|
|
|
|
ASSERT_EQUALS(true, tok.isName());
|
|
|
|
}
|
|
|
|
|
|
|
|
void isNameGuarantees4() {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str("123456");
|
|
|
|
ASSERT_EQUALS(false, tok.isName());
|
|
|
|
ASSERT_EQUALS(true, tok.isNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
void isNameGuarantees5() {
|
|
|
|
Token tok(NULL);
|
|
|
|
tok.str("a123456");
|
|
|
|
ASSERT_EQUALS(true, tok.isName());
|
|
|
|
ASSERT_EQUALS(false, tok.isNumber());
|
|
|
|
}
|
2012-04-18 16:02:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
void canFindMatchingBracketsNeedsOpen() {
|
|
|
|
givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;");
|
|
|
|
|
|
|
|
const Token* t = 0;
|
|
|
|
bool found = var.tokens()->findClosingBracket(t);
|
|
|
|
ASSERT(! found);
|
|
|
|
ASSERT(! t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void canFindMatchingBracketsInnerPair() {
|
|
|
|
givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;");
|
|
|
|
|
|
|
|
Token* t = 0;
|
|
|
|
bool found = var.tokens()->tokAt(7)->findClosingBracket(t);
|
|
|
|
ASSERT(found);
|
|
|
|
ASSERT_EQUALS(">", t->str());
|
|
|
|
ASSERT(var.tokens()->tokAt(9) == t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void canFindMatchingBracketsOuterPair() {
|
|
|
|
givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;");
|
|
|
|
|
|
|
|
const Token* t = 0;
|
|
|
|
bool found = var.tokens()->tokAt(3)->findClosingBracket(t);
|
|
|
|
ASSERT(found);
|
|
|
|
ASSERT_EQUALS(">", t->str());
|
|
|
|
ASSERT(var.tokens()->tokAt(10) == t);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void canFindMatchingBracketsWithTooManyClosing() {
|
|
|
|
givenACodeSampleToTokenize var("X< 1>2 > x1;\n");
|
|
|
|
|
|
|
|
const Token* t = 0;
|
|
|
|
bool found = var.tokens()->next()->findClosingBracket(t);
|
|
|
|
ASSERT(found);
|
|
|
|
ASSERT_EQUALS(">", t->str());
|
|
|
|
ASSERT(var.tokens()->tokAt(3) == t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void canFindMatchingBracketsWithTooManyOpening() {
|
|
|
|
givenACodeSampleToTokenize var("X < (2 < 1) > x1;\n");
|
|
|
|
|
|
|
|
const Token* t = 0;
|
|
|
|
bool found = var.tokens()->next()->findClosingBracket(t);
|
|
|
|
ASSERT(found);
|
|
|
|
|
|
|
|
found = var.tokens()->tokAt(4)->findClosingBracket(t);
|
|
|
|
ASSERT(!found);
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
};
|
|
|
|
|
2010-04-05 10:15:30 +02:00
|
|
|
REGISTER_TEST(TestToken)
|