Skip to content

Commit 41c106f

Browse files
TestAstUtils: test isSameExpression() with C code
Co-authored-by: chrchr-github <78114321+chrchr-github@users.noreply.github.com>
1 parent 087be12 commit 41c106f

1 file changed

Lines changed: 39 additions & 27 deletions

File tree

test/testastutils.cpp

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class TestAstUtils : public TestFixture {
4242
TEST_CASE(findLambdaStartTokenTest);
4343
TEST_CASE(isNullOperandTest);
4444
TEST_CASE(isReturnScopeTest);
45-
TEST_CASE(isSameExpressionTest);
45+
TEST_CASE(isSameExpressionCpp);
46+
TEST_CASE(isSameExpressionC);
4647
TEST_CASE(isVariableChangedTest);
4748
TEST_CASE(isVariableChangedByFunctionCallTest);
4849
TEST_CASE(isExpressionChangedTest);
@@ -176,42 +177,53 @@ class TestAstUtils : public TestFixture {
176177
}
177178

178179
#define isSameExpression(...) isSameExpression_(__FILE__, __LINE__, __VA_ARGS__)
179-
bool isSameExpression_(const char* file, int line, const char code[], const char tokStr1[], const char tokStr2[]) {
180+
bool isSameExpression_(const char* file, int line, const char code[], const char tokStr1[], const char tokStr2[], bool cpp) {
180181
const Settings settings;
181182
Library library;
182183
Tokenizer tokenizer(settings, this);
183184
std::istringstream istr(code);
184-
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
185+
ASSERT_LOC(tokenizer.tokenize(istr, cpp ? "test.cpp" : "test.c"), file, line);
185186
const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), tokStr1, strlen(tokStr1));
186187
const Token * const tok2 = Token::findsimplematch(tok1->next(), tokStr2, strlen(tokStr2));
187-
return (isSameExpression)(/*cpp*/ true, false, tok1, tok2, library, false, true, nullptr);
188+
return (isSameExpression)(cpp, false, tok1, tok2, library, false, true, nullptr);
188189
}
189190

190-
void isSameExpressionTest() {
191-
ASSERT_EQUALS(true, isSameExpression("x = 1 + 1;", "1", "1"));
192-
ASSERT_EQUALS(false, isSameExpression("x = 1 + 1u;", "1", "1u"));
193-
ASSERT_EQUALS(true, isSameExpression("x = 1.0 + 1.0;", "1.0", "1.0"));
194-
ASSERT_EQUALS(false, isSameExpression("x = 1.0f + 1.0;", "1.0f", "1.0"));
195-
ASSERT_EQUALS(false, isSameExpression("x = 1L + 1;", "1L", "1"));
196-
ASSERT_EQUALS(true, isSameExpression("x = 0.0f + 0x0p+0f;", "0.0f", "0x0p+0f"));
197-
ASSERT_EQUALS(true, isSameExpression("x < x;", "x", "x"));
198-
ASSERT_EQUALS(false, isSameExpression("x < y;", "x", "y"));
199-
ASSERT_EQUALS(true, isSameExpression("(x + 1) < (x + 1);", "+", "+"));
200-
ASSERT_EQUALS(false, isSameExpression("(x + 1) < (x + 1L);", "+", "+"));
201-
ASSERT_EQUALS(false, isSameExpression("(1 + x) < (x + 1);", "+", "+"));
202-
ASSERT_EQUALS(false, isSameExpression("(1.0l + x) < (1.0 + x);", "+", "+"));
203-
ASSERT_EQUALS(false, isSameExpression("(0.0 + x) < (x + 0x0p+0);", "+", "+"));
204-
ASSERT_EQUALS(true, isSameExpression("void f() {double y = 1e1; (x + y) < (x + 10.0); } ", "+", "+"));
205-
ASSERT_EQUALS(false, isSameExpression("void f() {double y = 1e1; (x + 10.0) < (y + x); } ", "+", "+"));
206-
ASSERT_EQUALS(true, isSameExpression("void f() {double y = 1e1; double z = 10.0; (x + y) < (x + z); } ", "+", "+"));
207-
ASSERT_EQUALS(true, isSameExpression("A + A", "A", "A"));
191+
void isSameExpressionTestInternal(bool cpp) {
192+
ASSERT_EQUALS(true, isSameExpression("x = 1 + 1;", "1", "1", cpp));
193+
ASSERT_EQUALS(false, isSameExpression("x = 1 + 1u;", "1", "1u", cpp));
194+
ASSERT_EQUALS(true, isSameExpression("x = 1.0 + 1.0;", "1.0", "1.0", cpp));
195+
ASSERT_EQUALS(false, isSameExpression("x = 1.0f + 1.0;", "1.0f", "1.0", cpp));
196+
ASSERT_EQUALS(false, isSameExpression("x = 1L + 1;", "1L", "1", cpp));
197+
ASSERT_EQUALS(true, isSameExpression("x = 0.0f + 0x0p+0f;", "0.0f", "0x0p+0f", cpp));
198+
ASSERT_EQUALS(true, isSameExpression("x < x;", "x", "x", cpp));
199+
ASSERT_EQUALS(false, isSameExpression("x < y;", "x", "y", cpp));
200+
ASSERT_EQUALS(true, isSameExpression("(x + 1) < (x + 1);", "+", "+", cpp));
201+
ASSERT_EQUALS(false, isSameExpression("(x + 1) < (x + 1L);", "+", "+", cpp));
202+
ASSERT_EQUALS(!cpp, isSameExpression("(1 + x) < (x + 1);", "+", "+", cpp));
203+
ASSERT_EQUALS(false, isSameExpression("(1.0l + x) < (1.0 + x);", "+", "+", cpp));
204+
ASSERT_EQUALS(!cpp, isSameExpression("(0.0 + x) < (x + 0x0p+0);", "+", "+", cpp));
205+
ASSERT_EQUALS(true, isSameExpression("void f() {double y = 1e1; (x + y) < (x + 10.0); } ", "+", "+", cpp));
206+
ASSERT_EQUALS(!cpp, isSameExpression("void f() {double y = 1e1; (x + 10.0) < (y + x); } ", "+", "+", cpp));
207+
ASSERT_EQUALS(true, isSameExpression("void f() {double y = 1e1; double z = 10.0; (x + y) < (x + z); } ", "+", "+", cpp));
208+
ASSERT_EQUALS(true, isSameExpression("A + A", "A", "A", cpp));
209+
210+
// the remaining test cases are not valid C code
211+
if (!cpp)
212+
return;
208213

209214
//https://trac.cppcheck.net/ticket/9700
210-
ASSERT_EQUALS(true, isSameExpression("A::B + A::B;", "::", "::"));
211-
ASSERT_EQUALS(false, isSameExpression("A::B + A::C;", "::", "::"));
212-
ASSERT_EQUALS(true, isSameExpression("A::B* get() { if(x) return new A::B(true); else return new A::B(true); }", "new", "new"));
213-
ASSERT_EQUALS(false, isSameExpression("A::B* get() { if(x) return new A::B(true); else return new A::C(true); }", "new", "new"));
214-
ASSERT_EQUALS(true, true);
215+
ASSERT_EQUALS(true, isSameExpression("A::B + A::B;", "::", "::", cpp));
216+
ASSERT_EQUALS(false, isSameExpression("A::B + A::C;", "::", "::", cpp));
217+
ASSERT_EQUALS(true, isSameExpression("A::B* get() { if(x) return new A::B(true); else return new A::B(true); }", "new", "new", cpp));
218+
ASSERT_EQUALS(false, isSameExpression("A::B* get() { if(x) return new A::B(true); else return new A::C(true); }", "new", "new", cpp));
219+
}
220+
221+
void isSameExpressionCpp() {
222+
isSameExpressionTestInternal(true);
223+
}
224+
225+
void isSameExpressionC() {
226+
isSameExpressionTestInternal(false);
215227
}
216228

217229
#define isVariableChanged(code, startPattern, endPattern) isVariableChanged_(code, startPattern, endPattern, __FILE__, __LINE__)

0 commit comments

Comments
 (0)