refactoring unit tests

This commit is contained in:
Daniel Marjamäki 2009-02-28 08:59:48 +00:00
parent 7d9bf491ec
commit c7197aed8c
1 changed files with 22 additions and 63 deletions

View File

@ -1078,16 +1078,9 @@ private:
// Simplify 'sizeof'..
void sizeof1() std::string sizeof_(const char code[])
{ {
const char code[] = "void foo()\n"
"{\n"
" int i[4];\n"
" sizeof(i);\n"
" sizeof(*i);\n"
"}\n";
// tokenize.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
@ -1099,7 +1092,20 @@ private:
std::ostringstream ostr; std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str(); ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" void foo ( ) { int i [ 4 ] ; 16 ; 4 ; }"), ostr.str());
return ostr.str();
}
void sizeof1()
{
const char code[] = "void foo()\n"
"{\n"
" int i[4];\n"
" sizeof(i);\n"
" sizeof(*i);\n"
"}\n";
ASSERT_EQUALS(std::string(" void foo ( ) { int i [ 4 ] ; 16 ; 4 ; }"), sizeof_(code));
} }
void sizeof2() void sizeof2()
@ -1110,38 +1116,14 @@ private:
" int i[10];\n" " int i[10];\n"
" sizeof(i);\n" " sizeof(i);\n"
"}\n"; "}\n";
ASSERT_EQUALS(std::string(" static int i [ 4 ] ; void f ( ) { int i [ 10 ] ; 40 ; }"), sizeof_(code));
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" static int i [ 4 ] ; void f ( ) { int i [ 10 ] ; 40 ; }"), ostr.str());
} }
void sizeof3() void sizeof3()
{ {
const char code[] = "int i[10];\n" const char code[] = "int i[10];\n"
"sizeof(i[0]);\n"; "sizeof(i[0]);\n";
ASSERT_EQUALS(std::string(" int i [ 10 ] ; 4 ;"), sizeof_(code));
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" int i [ 10 ] ; 4 ;"), ostr.str());
} }
void sizeof4() void sizeof4()
@ -1149,19 +1131,7 @@ private:
const char code[] = const char code[] =
"for (int i = 0; i < sizeof(g_ReservedNames[0]); i++)" "for (int i = 0; i < sizeof(g_ReservedNames[0]); i++)"
"{}"; "{}";
ASSERT_EQUALS(std::string(" for ( int i = 0 ; i < 100 ; i ++ ) { }"), sizeof_(code));
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" for ( int i = 0 ; i < 100 ; i ++ ) { }"), ostr.str());
} }
void sizeof5() void sizeof5()
@ -1169,21 +1139,10 @@ private:
const char code[] = ";int i;\n" const char code[] = ";int i;\n"
"sizeof(i);\n"; "sizeof(i);\n";
// tokenize.. std::ostringstream expected;
Tokenizer tokenizer; expected << " ; int i ; " << sizeof(int) << " ;";
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId(); ASSERT_EQUALS(expected.str(), sizeof_(code));
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
std::ostringstream oss;
oss << " ; int i ; " << sizeof(int) << " ;";
ASSERT_EQUALS(oss.str(), ostr.str());
} }
void simplify_numeric_condition() void simplify_numeric_condition()