moved sizeof tests to the TestSimplifyTokens class

This commit is contained in:
Daniel Marjamäki 2009-02-28 09:09:55 +00:00
parent c7197aed8c
commit 045477e6ac
2 changed files with 90 additions and 82 deletions

View File

@ -58,7 +58,6 @@ private:
void run() void run()
{ {
TEST_CASE(cast0); TEST_CASE(cast0);
TEST_CASE(sizeof1);
TEST_CASE(iftruefalse); TEST_CASE(iftruefalse);
TEST_CASE(combine_strings); TEST_CASE(combine_strings);
TEST_CASE(double_plus); TEST_CASE(double_plus);
@ -67,6 +66,13 @@ private:
TEST_CASE(paranthesesVar); // Remove redundant parantheses around variable .. "( %var% )" TEST_CASE(paranthesesVar); // Remove redundant parantheses around variable .. "( %var% )"
TEST_CASE(elseif1); TEST_CASE(elseif1);
TEST_CASE(sizeof1);
TEST_CASE(sizeof2);
TEST_CASE(sizeof3);
TEST_CASE(sizeof4);
TEST_CASE(sizeof5);
TEST_CASE(sizeof6);
} }
std::string tok(const char code[]) std::string tok(const char code[])
@ -91,15 +97,6 @@ private:
ASSERT_EQUALS(tok(code1), tok(code2)); ASSERT_EQUALS(tok(code1), tok(code2));
} }
void sizeof1()
{
const char code1[] = " struct ABC *abc = malloc(sizeof(*abc)); ";
const char code2[] = " struct ABC *abc = malloc(100); ";
const char code3[] = " struct ABC *abc = malloc(sizeof *abc ); ";
ASSERT_EQUALS(tok(code1), tok(code2));
ASSERT_EQUALS(tok(code2), tok(code3));
}
void iftruefalse() void iftruefalse()
{ {
{ {
@ -339,6 +336,89 @@ private:
const char code[] = "else if(ab) { cd } else { ef }gh"; const char code[] = "else if(ab) { cd } else { ef }gh";
ASSERT_EQUALS("\n1: else { if ( ab ) { cd } else { ef } } gh\n", elseif(code)); ASSERT_EQUALS("\n1: else { if ( ab ) { cd } else { ef } } gh\n", elseif(code));
} }
// Simplify 'sizeof'..
std::string sizeof_(const char 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();
return ostr.str();
}
void sizeof1()
{
const char code1[] = " struct ABC *abc = malloc(sizeof(*abc)); ";
const char code2[] = " struct ABC *abc = malloc(100); ";
const char code3[] = " struct ABC *abc = malloc(sizeof *abc ); ";
ASSERT_EQUALS(tok(code1), tok(code2));
ASSERT_EQUALS(tok(code2), tok(code3));
}
void sizeof2()
{
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 sizeof3()
{
const char code[] = "static int i[4];\n"
"void f()\n"
"{\n"
" int i[10];\n"
" sizeof(i);\n"
"}\n";
ASSERT_EQUALS(std::string(" static int i [ 4 ] ; void f ( ) { int i [ 10 ] ; 40 ; }"), sizeof_(code));
}
void sizeof4()
{
const char code[] = "int i[10];\n"
"sizeof(i[0]);\n";
ASSERT_EQUALS(std::string(" int i [ 10 ] ; 4 ;"), sizeof_(code));
}
void sizeof5()
{
const char code[] =
"for (int i = 0; i < sizeof(g_ReservedNames[0]); i++)"
"{}";
ASSERT_EQUALS(std::string(" for ( int i = 0 ; i < 100 ; i ++ ) { }"), sizeof_(code));
}
void sizeof6()
{
const char code[] = ";int i;\n"
"sizeof(i);\n";
std::ostringstream expected;
expected << " ; int i ; " << sizeof(int) << " ;";
ASSERT_EQUALS(expected.str(), sizeof_(code));
}
}; };
REGISTER_TEST(TestSimplifyTokens) REGISTER_TEST(TestSimplifyTokens)

View File

@ -112,11 +112,6 @@ private:
TEST_CASE(reduce_redundant_paranthesis); // Ticket #61 TEST_CASE(reduce_redundant_paranthesis); // Ticket #61
TEST_CASE(sizeof1);
TEST_CASE(sizeof2);
TEST_CASE(sizeof3);
TEST_CASE(sizeof4);
TEST_CASE(sizeof5);
TEST_CASE(simplify_numeric_condition); TEST_CASE(simplify_numeric_condition);
TEST_CASE(tokenize_double); TEST_CASE(tokenize_double);
TEST_CASE(tokenize_strings); TEST_CASE(tokenize_strings);
@ -1078,73 +1073,6 @@ private:
// Simplify 'sizeof'..
std::string sizeof_(const char 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();
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()
{
const char code[] = "static int i[4];\n"
"void f()\n"
"{\n"
" int i[10];\n"
" sizeof(i);\n"
"}\n";
ASSERT_EQUALS(std::string(" static int i [ 4 ] ; void f ( ) { int i [ 10 ] ; 40 ; }"), sizeof_(code));
}
void sizeof3()
{
const char code[] = "int i[10];\n"
"sizeof(i[0]);\n";
ASSERT_EQUALS(std::string(" int i [ 10 ] ; 4 ;"), sizeof_(code));
}
void sizeof4()
{
const char code[] =
"for (int i = 0; i < sizeof(g_ReservedNames[0]); i++)"
"{}";
ASSERT_EQUALS(std::string(" for ( int i = 0 ; i < 100 ; i ++ ) { }"), sizeof_(code));
}
void sizeof5()
{
const char code[] = ";int i;\n"
"sizeof(i);\n";
std::ostringstream expected;
expected << " ; int i ; " << sizeof(int) << " ;";
ASSERT_EQUALS(expected.str(), sizeof_(code));
}
void simplify_numeric_condition() void simplify_numeric_condition()
{ {
const char code[] = const char code[] =