From 045477e6ac33c38a02cd8dd706e49fc2fa020a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 28 Feb 2009 09:09:55 +0000 Subject: [PATCH] moved sizeof tests to the TestSimplifyTokens class --- test/testsimplifytokens.cpp | 100 ++++++++++++++++++++++++++++++++---- test/testtokenize.cpp | 72 -------------------------- 2 files changed, 90 insertions(+), 82 deletions(-) diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 54bdcf5a8..5a1c17d39 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -58,7 +58,6 @@ private: void run() { TEST_CASE(cast0); - TEST_CASE(sizeof1); TEST_CASE(iftruefalse); TEST_CASE(combine_strings); TEST_CASE(double_plus); @@ -67,6 +66,13 @@ private: TEST_CASE(paranthesesVar); // Remove redundant parantheses around variable .. "( %var% )" 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[]) @@ -91,15 +97,6 @@ private: 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() { { @@ -339,6 +336,89 @@ private: const char code[] = "else if(ab) { cd } else { ef }gh"; 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) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index cc3c7d41f..61ace53aa 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -112,11 +112,6 @@ private: 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(tokenize_double); 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() { const char code[] =