Skip to content

Commit e895fa4

Browse files
committed
TestTrac: Add separate file for black box / integration tests in testrunner
1 parent 259724d commit e895fa4

6 files changed

Lines changed: 120 additions & 52 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ TESTOBJ = test/options.o \
272272
test/testtoken.o \
273273
test/testtokenize.o \
274274
test/testtokenlist.o \
275+
test/testtrac.o \
275276
test/testtype.o \
276277
test/testuninitvar.o \
277278
test/testunusedfunctions.o \
@@ -495,7 +496,7 @@ $(libcppdir)/ctu.o: lib/ctu.cpp externals/tinyxml/tinyxml2.h lib/astutils.h lib/
495496
$(libcppdir)/errorlogger.o: lib/errorlogger.cpp externals/tinyxml/tinyxml2.h lib/analyzerinfo.h lib/check.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h
496497
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/errorlogger.o $(libcppdir)/errorlogger.cpp
497498

498-
$(libcppdir)/exprengine.o: lib/exprengine.cpp lib/astutils.h lib/config.h lib/errorlogger.h lib/exprengine.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h
499+
$(libcppdir)/exprengine.o: lib/exprengine.cpp externals/z3_version.h lib/astutils.h lib/config.h lib/errorlogger.h lib/exprengine.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h
499500
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(libcppdir)/exprengine.o $(libcppdir)/exprengine.cpp
500501

501502
$(libcppdir)/forwardanalyzer.o: lib/forwardanalyzer.cpp lib/astutils.h lib/config.h lib/errorlogger.h lib/forwardanalyzer.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h
@@ -732,6 +733,9 @@ test/testtokenize.o: test/testtokenize.cpp externals/simplecpp/simplecpp.h lib/c
732733
test/testtokenlist.o: test/testtokenlist.cpp lib/config.h lib/errorlogger.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h
733734
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testtokenlist.o test/testtokenlist.cpp
734735

736+
test/testtrac.o: test/testtrac.cpp lib/check.h lib/checkbool.h lib/checknullpointer.h lib/checkother.h lib/config.h lib/ctu.h lib/errorlogger.h lib/mathlib.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h
737+
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testtrac.o test/testtrac.cpp
738+
735739
test/testtype.o: test/testtype.cpp lib/check.h lib/checktype.h lib/config.h lib/errorlogger.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h
736740
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testtype.o test/testtype.cpp
737741

test/testastutils.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,6 @@ class TestAstUtils : public TestFixture {
188188
ASSERT_EQUALS(true, isSameExpression("void f() {double y = 1e1; (x + y) < (x + 10.0); } ", "+", "+"));
189189
ASSERT_EQUALS(true, isSameExpression("void f() {double y = 1e1; (x + 10.0) < (y + x); } ", "+", "+"));
190190
ASSERT_EQUALS(true, isSameExpression("void f() {double y = 1e1; double z = 10.0; (x + y) < (x + z); } ", "+", "+"));
191-
ASSERT_EQUALS(true, isSameExpression("A + A", "A", "A"));
192-
193-
//https://trac.cppcheck.net/ticket/9700
194-
ASSERT_EQUALS(true, isSameExpression("A::B + A::B;", "::", "::"));
195-
ASSERT_EQUALS(false, isSameExpression("A::B + A::C;", "::", "::"));
196-
ASSERT_EQUALS(true, isSameExpression("A::B* get() { if(x) return new A::B(true); else return new A::B(true); }", "new", "new"));
197-
ASSERT_EQUALS(false, isSameExpression("A::B* get() { if(x) return new A::B(true); else return new A::C(true); }", "new", "new"));
198-
ASSERT_EQUALS(true, true);
199191
}
200192

201193
bool isVariableChanged(const char code[], const char startPattern[], const char endPattern[]) {

test/testbool.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ class TestBool : public TestFixture {
6262
TEST_CASE(checkComparisonOfFuncReturningBool4);
6363
TEST_CASE(checkComparisonOfFuncReturningBool5);
6464
TEST_CASE(checkComparisonOfFuncReturningBool6);
65-
// Integration tests..
66-
TEST_CASE(checkComparisonOfFuncReturningBoolIntegrationTest1); // #7798 overloaded functions
6765

6866
TEST_CASE(checkComparisonOfBoolWithBool);
6967

@@ -748,18 +746,6 @@ class TestBool : public TestFixture {
748746
ASSERT_EQUALS("", errout.str());
749747
}
750748

751-
void checkComparisonOfFuncReturningBoolIntegrationTest1() { // #7798
752-
check("bool eval(double *) { return false; }\n"
753-
"double eval(char *) { return 1.0; }\n"
754-
"int main(int argc, char *argv[])\n"
755-
"{\n"
756-
" if ( eval(argv[1]) > eval(argv[2]) )\n"
757-
" return 1;\n"
758-
" return 0;\n"
759-
"}");
760-
ASSERT_EQUALS("", errout.str());
761-
}
762-
763749
void checkComparisonOfBoolWithBool() {
764750
const char code[] = "void f(){\n"
765751
" int temp = 4;\n"

test/testnullpointer.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ class TestNullPointer : public TestFixture {
9494
TEST_CASE(nullpointer51);
9595
TEST_CASE(nullpointer52);
9696
TEST_CASE(nullpointer53); // #8005
97-
TEST_CASE(nullpointer54); // #9573
9897
TEST_CASE(nullpointer_addressOf); // address of
9998
TEST_CASE(nullpointerSwitch); // #2626
10099
TEST_CASE(nullpointer_cast); // #4692
@@ -1763,22 +1762,6 @@ class TestNullPointer : public TestFixture {
17631762
ASSERT_EQUALS("[test.cpp:3]: (warning) Possible null pointer dereference: params\n", errout.str());
17641763
}
17651764

1766-
void nullpointer54() {
1767-
check("int foo (int **array, size_t n_array) {\n"
1768-
" size_t i;\n"
1769-
" for (i = 0; i < n_array; ++i) {\n"
1770-
" if (*array[i] == 1)\n"
1771-
" return 1;\n"
1772-
" }\n"
1773-
" return 0;\n"
1774-
"} \n"
1775-
"int bar() {\n"
1776-
" int **array = NULL; \n"
1777-
" foo (array, 0);\n"
1778-
"}\n");
1779-
ASSERT_EQUALS("", errout.str());
1780-
}
1781-
17821765
void nullpointer_addressOf() { // address of
17831766
check("void f() {\n"
17841767
" struct X *x = 0;\n"

test/testother.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ class TestOther : public TestFixture {
130130
TEST_CASE(duplicateBranch1); // tests extracted by http://www.viva64.com/en/b/0149/ ( Comparison between PVS-Studio and cppcheck ): Errors detected in Quake 3: Arena by PVS-Studio: Fragment 2
131131
TEST_CASE(duplicateBranch2); // empty macro
132132
TEST_CASE(duplicateBranch3);
133-
TEST_CASE(duplicateBranch4);
134133
TEST_CASE(duplicateExpression1);
135134
TEST_CASE(duplicateExpression2); // ticket #2730
136135
TEST_CASE(duplicateExpression3); // ticket #3317
@@ -4102,17 +4101,6 @@ class TestOther : public TestFixture {
41024101
ASSERT_EQUALS("", errout.str());
41034102
}
41044103

4105-
void duplicateBranch4() {
4106-
check("void* f(bool b) {\n"
4107-
" if (b) {\n"
4108-
" return new A::Y(true);\n"
4109-
" } else {\n"
4110-
" return new A::Z(true);\n"
4111-
" }\n"
4112-
"}\n");
4113-
ASSERT_EQUALS("", errout.str());
4114-
}
4115-
41164104
void duplicateExpression1() {
41174105
check("void foo(int a) {\n"
41184106
" if (a == a) { }\n"

test/testtrac.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2020 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
20+
// This file is for integration tests for trac tickets.
21+
//
22+
// The intention is mostly to put tests here for false positives that
23+
// are fixed by fixing AST/ValueFlow/SymbolDatabase/etc. The check itself
24+
// worked as it should but there was problem with the input.
25+
//
26+
// These tests are typically "black box" tests and don't consider how the
27+
// checker works internally.
28+
29+
#include "checkbool.h"
30+
#include "checknullpointer.h"
31+
#include "checkother.h"
32+
#include "testsuite.h"
33+
#include "settings.h"
34+
35+
class TestTrac : public TestFixture {
36+
public:
37+
TestTrac() : TestFixture("TestTrac") {
38+
}
39+
40+
private:
41+
42+
void run() OVERRIDE {
43+
TEST_CASE(ticket_7798);
44+
TEST_CASE(ticket_9573);
45+
TEST_CASE(ticket_9700);
46+
}
47+
48+
template<class C>
49+
void check(const char code[], const char *filename = "test.cpp") {
50+
// Clear the error buffer..
51+
errout.str("");
52+
53+
Settings settings;
54+
settings.addEnabled("style");
55+
settings.addEnabled("warning");
56+
settings.addEnabled("portability");
57+
settings.addEnabled("performance");
58+
settings.standards.c = Standards::CLatest;
59+
settings.standards.cpp = Standards::CPPLatest;
60+
settings.inconclusive = true;
61+
62+
// Tokenize..
63+
Tokenizer tokenizer(&settings, this);
64+
std::istringstream istr(code);
65+
tokenizer.tokenize(istr, filename);
66+
67+
// Check..
68+
C c(&tokenizer, &settings, this);
69+
c.runChecks(&tokenizer, &settings, this);
70+
}
71+
72+
void ticket_7798() {
73+
// checkComparisonOfFuncReturningBool (overloaded functions)
74+
check<CheckBool>("bool eval(double *) { return false; }\n"
75+
"double eval(char *) { return 1.0; }\n"
76+
"int main(int argc, char *argv[])\n"
77+
"{\n"
78+
" if ( eval(argv[1]) > eval(argv[2]) )\n"
79+
" return 1;\n"
80+
" return 0;\n"
81+
"}");
82+
ASSERT_EQUALS("", errout.str());
83+
}
84+
85+
void ticket_9573() {
86+
// nullpointer (valueflow)
87+
check<CheckNullPointer>("int foo (int **array, size_t n_array) {\n"
88+
" size_t i;\n"
89+
" for (i = 0; i < n_array; ++i) {\n"
90+
" if (*array[i] == 1)\n"
91+
" return 1;\n"
92+
" }\n"
93+
" return 0;\n"
94+
"} \n"
95+
"int bar() {\n"
96+
" int **array = NULL; \n"
97+
" foo (array, 0);\n"
98+
"}\n");
99+
ASSERT_EQUALS("", errout.str());
100+
}
101+
102+
void ticket_9700() {
103+
// FP: duplicateBranch
104+
check<CheckOther>("void* f(bool b) {\n"
105+
" if (b) {\n"
106+
" return new A::Y(true);\n"
107+
" } else {\n"
108+
" return new A::Z(true);\n"
109+
" }\n"
110+
"}\n");
111+
ASSERT_EQUALS("", errout.str());
112+
}
113+
};
114+
115+
REGISTER_TEST(TestTrac)

0 commit comments

Comments
 (0)