From 81c7fa03480f2b463f6c61a9e8c654bc6cda458b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 27 Feb 2021 04:07:33 +0100 Subject: [PATCH] Moved TestTokenizer::tokenize6 to TestSimplifyTokens --- test/testsimplifytokens.cpp | 49 +++++++++++++++++++++++++++++++++++++ test/testtokenize.cpp | 44 --------------------------------- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index d7fcd99a5..ddd0280c1 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -58,6 +58,8 @@ private: // correct order TEST_CASE(simplifyTokenList1); + TEST_CASE(test1); // array access. replace "*(p+1)" => "p[1]" + TEST_CASE(simplifyMathFunctions_sqrt); TEST_CASE(simplifyMathFunctions_cbrt); TEST_CASE(simplifyMathFunctions_exp); @@ -462,6 +464,53 @@ private: } + + void test1() { + // "&p[1]" => "p+1" + /* + ASSERT_EQUALS("; x = p + n ;", tok("; x = & p [ n ] ;")); + ASSERT_EQUALS("; x = ( p + n ) [ m ] ;", tok("; x = & p [ n ] [ m ] ;")); + ASSERT_EQUALS("; x = y & p [ n ] ;", tok("; x = y & p [ n ] ;")); + ASSERT_EQUALS("; x = 10 & p [ n ] ;", tok("; x = 10 & p [ n ] ;")); + ASSERT_EQUALS("; x = y [ 10 ] & p [ n ] ;", tok("; x = y [ 10 ] & p [ n ] ;")); + ASSERT_EQUALS("; x = ( a + m ) & p [ n ] ;", tok("; x = ( a + m ) & p [ n ] ;")); + */ + // "*(p+1)" => "p[1]" + ASSERT_EQUALS("; x = p [ 1 ] ;", tok("; x = * ( p + 1 ) ;")); + ASSERT_EQUALS("; x = p [ 0xA ] ;", tok("; x = * ( p + 0xA ) ;")); + ASSERT_EQUALS("; x = p [ n ] ;", tok("; x = * ( p + n ) ;")); + ASSERT_EQUALS("; x = y * ( p + n ) ;", tok("; x = y * ( p + n ) ;")); + ASSERT_EQUALS("; x = 10 * ( p + n ) ;", tok("; x = 10 * ( p + n ) ;")); + ASSERT_EQUALS("; x = y [ 10 ] * ( p + n ) ;", tok("; x = y [ 10 ] * ( p + n ) ;")); + ASSERT_EQUALS("; x = ( a + m ) * ( p + n ) ;", tok("; x = ( a + m ) * ( p + n ) ;")); + + // "*(p-1)" => "p[-1]" and "*(p-n)" => "p[-n]" + ASSERT_EQUALS("; x = p [ -1 ] ;", tok("; x = *(p - 1);")); + ASSERT_EQUALS("; x = p [ -0xA ] ;", tok("; x = *(p - 0xA);")); + ASSERT_EQUALS("; x = p [ - n ] ;", tok("; x = *(p - n);")); + ASSERT_EQUALS("; x = y * ( p - 1 ) ;", tok("; x = y * (p - 1);")); + ASSERT_EQUALS("; x = 10 * ( p - 1 ) ;", tok("; x = 10 * (p - 1);")); + ASSERT_EQUALS("; x = y [ 10 ] * ( p - 1 ) ;", tok("; x = y[10] * (p - 1);")); + ASSERT_EQUALS("; x = ( a - m ) * ( p - n ) ;", tok("; x = (a - m) * (p - n);")); + + // Test that the array-index simplification is not applied when there's no dereference: + // "(x-y)" => "(x-y)" and "(x+y)" => "(x+y)" + ASSERT_EQUALS("; a = b * ( x - y ) ;", tok("; a = b * (x - y);")); + ASSERT_EQUALS("; a = b * x [ - y ] ;", tok("; a = b * *(x - y);")); + ASSERT_EQUALS("; a = a * ( x - y ) ;", tok("; a *= (x - y);")); + ASSERT_EQUALS("; z = a ++ * ( x - y ) ;", tok("; z = a++ * (x - y);")); + ASSERT_EQUALS("; z = a ++ * ( x + y ) ;", tok("; z = a++ * (x + y);")); + ASSERT_EQUALS("; z = a -- * ( x - y ) ;", tok("; z = a-- * (x - y);")); + ASSERT_EQUALS("; z = a -- * ( x + y ) ;", tok("; z = a-- * (x + y);")); + ASSERT_EQUALS("; z = 'a' * ( x - y ) ;", tok("; z = 'a' * (x - y);")); + ASSERT_EQUALS("; z = \"a\" * ( x - y ) ;", tok("; z = \"a\" * (x - y);")); + ASSERT_EQUALS("; z = 'a' * ( x + y ) ;", tok("; z = 'a' * (x + y);")); + ASSERT_EQUALS("; z = \"a\" * ( x + y ) ;", tok("; z = \"a\" * (x + y);")); + ASSERT_EQUALS("; z = foo ( ) * ( x + y ) ;", tok("; z = foo() * (x + y);")); + } + + + void simplifyMathFunctions_erfc() { // verify erfc(), erfcf(), erfcl() - simplifcation const char code_erfc[] ="void f(int x) {\n" diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index f7d5bfb8e..24198831d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -57,7 +57,6 @@ private: TEST_CASE(tokenize2); TEST_CASE(tokenize4); TEST_CASE(tokenize5); - TEST_CASE(tokenize6); // array access. replace "*(p+1)" => "p[1]" TEST_CASE(tokenize7); TEST_CASE(tokenize8); TEST_CASE(tokenize9); @@ -569,49 +568,6 @@ private: ASSERT_EQUALS("; 1E-2 ;", tokenizeAndStringify("; 1E-2 ;")); } - void tokenize6() { - // "&p[1]" => "p+1" - /* - ASSERT_EQUALS("; x = p + n ;", tokenizeAndStringify("; x = & p [ n ] ;", true)); - ASSERT_EQUALS("; x = ( p + n ) [ m ] ;", tokenizeAndStringify("; x = & p [ n ] [ m ] ;", true)); - ASSERT_EQUALS("; x = y & p [ n ] ;", tokenizeAndStringify("; x = y & p [ n ] ;", true)); - ASSERT_EQUALS("; x = 10 & p [ n ] ;", tokenizeAndStringify("; x = 10 & p [ n ] ;", true)); - ASSERT_EQUALS("; x = y [ 10 ] & p [ n ] ;", tokenizeAndStringify("; x = y [ 10 ] & p [ n ] ;", true)); - ASSERT_EQUALS("; x = ( a + m ) & p [ n ] ;", tokenizeAndStringify("; x = ( a + m ) & p [ n ] ;", true));*/ - // "*(p+1)" => "p[1]" - ASSERT_EQUALS("; x = p [ 1 ] ;", tokenizeAndStringify("; x = * ( p + 1 ) ;", true)); - ASSERT_EQUALS("; x = p [ 0xA ] ;", tokenizeAndStringify("; x = * ( p + 0xA ) ;", true)); - ASSERT_EQUALS("; x = p [ n ] ;", tokenizeAndStringify("; x = * ( p + n ) ;", true)); - ASSERT_EQUALS("; x = y * ( p + n ) ;", tokenizeAndStringify("; x = y * ( p + n ) ;", true)); - ASSERT_EQUALS("; x = 10 * ( p + n ) ;", tokenizeAndStringify("; x = 10 * ( p + n ) ;", true)); - ASSERT_EQUALS("; x = y [ 10 ] * ( p + n ) ;", tokenizeAndStringify("; x = y [ 10 ] * ( p + n ) ;", true)); - ASSERT_EQUALS("; x = ( a + m ) * ( p + n ) ;", tokenizeAndStringify("; x = ( a + m ) * ( p + n ) ;", true)); - - // "*(p-1)" => "p[-1]" and "*(p-n)" => "p[-n]" - ASSERT_EQUALS("; x = p [ -1 ] ;", tokenizeAndStringify("; x = *(p - 1);", true)); - ASSERT_EQUALS("; x = p [ -0xA ] ;", tokenizeAndStringify("; x = *(p - 0xA);", true)); - ASSERT_EQUALS("; x = p [ - n ] ;", tokenizeAndStringify("; x = *(p - n);", true)); - ASSERT_EQUALS("; x = y * ( p - 1 ) ;", tokenizeAndStringify("; x = y * (p - 1);", true)); - ASSERT_EQUALS("; x = 10 * ( p - 1 ) ;", tokenizeAndStringify("; x = 10 * (p - 1);", true)); - ASSERT_EQUALS("; x = y [ 10 ] * ( p - 1 ) ;", tokenizeAndStringify("; x = y[10] * (p - 1);", true)); - ASSERT_EQUALS("; x = ( a - m ) * ( p - n ) ;", tokenizeAndStringify("; x = (a - m) * (p - n);", true)); - - // Test that the array-index simplification is not applied when there's no dereference: - // "(x-y)" => "(x-y)" and "(x+y)" => "(x+y)" - ASSERT_EQUALS("; a = b * ( x - y ) ;", tokenizeAndStringify("; a = b * (x - y);", true)); - ASSERT_EQUALS("; a = b * x [ - y ] ;", tokenizeAndStringify("; a = b * *(x - y);", true)); - ASSERT_EQUALS("; a = a * ( x - y ) ;", tokenizeAndStringify("; a *= (x - y);", true)); - ASSERT_EQUALS("; z = a ++ * ( x - y ) ;", tokenizeAndStringify("; z = a++ * (x - y);", true)); - ASSERT_EQUALS("; z = a ++ * ( x + y ) ;", tokenizeAndStringify("; z = a++ * (x + y);", true)); - ASSERT_EQUALS("; z = a -- * ( x - y ) ;", tokenizeAndStringify("; z = a-- * (x - y);", true)); - ASSERT_EQUALS("; z = a -- * ( x + y ) ;", tokenizeAndStringify("; z = a-- * (x + y);", true)); - ASSERT_EQUALS("; z = 'a' * ( x - y ) ;", tokenizeAndStringify("; z = 'a' * (x - y);", true)); - ASSERT_EQUALS("; z = \"a\" * ( x - y ) ;", tokenizeAndStringify("; z = \"a\" * (x - y);", true)); - ASSERT_EQUALS("; z = 'a' * ( x + y ) ;", tokenizeAndStringify("; z = 'a' * (x + y);", true)); - ASSERT_EQUALS("; z = \"a\" * ( x + y ) ;", tokenizeAndStringify("; z = \"a\" * (x + y);", true)); - ASSERT_EQUALS("; z = foo ( ) * ( x + y ) ;", tokenizeAndStringify("; z = foo() * (x + y);", true)); - } - void tokenize7() { const char code[] = "void f() {\n" " int x1 = 1;\n"