TestTokenizer: code cleanup

This commit is contained in:
Daniel Marjamäki 2011-05-01 07:50:19 +02:00
parent 6bf5eb67be
commit 68efad05af
1 changed files with 35 additions and 184 deletions

View File

@ -3730,202 +3730,83 @@ private:
} }
} }
// Simplify "((..))" into "(..)" // Simplify "((..))" into "(..)"
void removeParentheses1() void removeParentheses1()
{ {
const char code[] = "void foo()\n" const char code[] = "void foo()"
"{\n" "{"
" free(((void*)p));\n" " free(((void*)p));"
"}"; "}";
errout.str(""); ASSERT_EQUALS("void foo ( ) { free ( p ) ; }", tokenizeAndStringify(code, true));
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void foo ( ) { free ( p ) ; }", ostr.str());
} }
void removeParentheses2() void removeParentheses2()
{ {
const char code[] = "void foo()\n" const char code[] = "void foo()"
"{\n" "{"
" if (__builtin_expect((s == NULL), 0))\n" " if (__builtin_expect((s == NULL), 0))"
" return;\n" " return;"
"}"; "}";
errout.str(""); ASSERT_EQUALS("void foo ( ) { if ( ! s ) { return ; } }", tokenizeAndStringify(code));
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void foo ( ) { if ( ! s ) { return ; } }", ostr.str());
} }
void removeParentheses3() void removeParentheses3()
{ {
{ {
const char code[] = "void foo()\n" const char code[] = "void foo()"
"{\n" "{"
" if (( true )==(true)){}\n" " if (( true )==(true)){}"
"}"; "}";
ASSERT_EQUALS("void foo ( ) { { } }", tokenizeAndStringify(code, true));
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void foo ( ) { { } }", ostr.str());
} }
{ {
const char code[] = "void foo()\n" const char code[] = "void foo()"
"{\n" "{"
" if (( 2 )==(2)){}\n" " if (( 2 )==(2)){}"
"}"; "}";
ASSERT_EQUALS("void foo ( ) { { } }", tokenizeAndStringify(code, true));
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void foo ( ) { { } }", ostr.str());
} }
{ {
const char code[] = "void foo()\n" const char code[] = "void foo()"
"{\n" "{"
" if( g(10)){}\n" " if( g(10)){}"
"}"; "}";
ASSERT_EQUALS("void foo ( ) { if ( g ( 10 ) ) { } }", tokenizeAndStringify(code));
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void foo ( ) { if ( g ( 10 ) ) { } }", ostr.str());
} }
} }
// Simplify "( function (..))" into "function (..)" // Simplify "( function (..))" into "function (..)"
void removeParentheses4() void removeParentheses4()
{ {
const char code[] = "void foo()\n" const char code[] = "void foo()"
"{\n" "{"
" (free(p));\n" " (free(p));"
"}"; "}";
ASSERT_EQUALS("void foo ( ) { free ( p ) ; }", tokenizeAndStringify(code));
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void foo ( ) { free ( p ) ; }", ostr.str());
} }
void removeParentheses5() void removeParentheses5()
{ {
// Simplify "( delete x )" into "delete x" // Simplify "( delete x )" into "delete x"
{ {
const char code[] = "void foo()\n" const char code[] = "void foo()"
"{\n" "{"
" (delete p);\n" " (delete p);"
"}"; "}";
ASSERT_EQUALS("void foo ( ) { delete p ; }", tokenizeAndStringify(code));
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void foo ( ) { delete p ; }", ostr.str());
} }
// Simplify "( delete [] x )" into "delete [] x" // Simplify "( delete [] x )" into "delete [] x"
{ {
const char code[] = "void foo()\n" const char code[] = "void foo()"
"{\n" "{"
" (delete [] p);\n" " (delete [] p);"
"}"; "}";
ASSERT_EQUALS("void foo ( ) { delete [ ] p ; }", tokenizeAndStringify(code));
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void foo ( ) { delete [ ] p ; }", ostr.str());
} }
} }
@ -3933,43 +3814,13 @@ private:
void removeParentheses6() void removeParentheses6()
{ {
const char code[] = "(!(abc.a))"; const char code[] = "(!(abc.a))";
ASSERT_EQUALS("( ! abc . a )", tokenizeAndStringify(code));
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" ( ! abc . a )", ostr.str());
} }
void removeParentheses7() void removeParentheses7()
{ {
const char code[] = ";char *p; (delete(p), (p)=0);"; const char code[] = ";char *p; (delete(p), (p)=0);";
ASSERT_EQUALS("; char * p ; delete p ; ( p ) = 0 ;", tokenizeAndStringify(code,true));
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" ; char * p ; delete p ; ( p ) = 0 ;", ostr.str());
} }
void removeParentheses8() void removeParentheses8()