Skip to content

Commit

Permalink
Added testing of samples/ to testrunner (#3515).
Browse files Browse the repository at this point in the history
  • Loading branch information
PKEuS committed Mar 28, 2014
1 parent 838cac5 commit c078b3f
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -183,6 +183,7 @@ TESTOBJ = test/options.o \
test/testpostfixoperator.o \
test/testpreprocessor.o \
test/testrunner.o \
test/testsamples.o \
test/testsimplifytokens.o \
test/testsizeof.o \
test/teststl.o \
Expand Down Expand Up @@ -485,6 +486,9 @@ test/testpreprocessor.o: test/testpreprocessor.cpp lib/cxx11emu.h test/testsuite
test/testrunner.o: test/testrunner.cpp lib/cxx11emu.h test/testsuite.h lib/errorlogger.h lib/config.h lib/suppressions.h test/redirect.h lib/library.h lib/path.h lib/mathlib.h test/options.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CFG) $(CXXFLAGS) -std=c++0x -c -o test/testrunner.o test/testrunner.cpp

test/testsamples.o: test/testsamples.cpp lib/cxx11emu.h test/testsuite.h lib/errorlogger.h lib/config.h lib/suppressions.h test/redirect.h lib/library.h lib/path.h lib/mathlib.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CFG) $(CXXFLAGS) -std=c++0x -c -o test/testsamples.o test/testsamples.cpp

test/testsimplifytokens.o: test/testsimplifytokens.cpp lib/cxx11emu.h test/testsuite.h lib/errorlogger.h lib/config.h lib/suppressions.h test/redirect.h lib/library.h lib/path.h lib/mathlib.h lib/tokenize.h lib/tokenlist.h lib/token.h lib/valueflow.h lib/settings.h lib/standards.h lib/timer.h lib/templatesimplifier.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CFG) $(CXXFLAGS) -std=c++0x -c -o test/testsimplifytokens.o test/testsimplifytokens.cpp

Expand Down
1 change: 1 addition & 0 deletions samples/AssignmentAddressToInteger/out.txt
@@ -0,0 +1 @@
[samples\AssignmentAddressToInteger\bad.c:3]: (portability) Assigning a pointer to an integer is not portable.
1 change: 1 addition & 0 deletions samples/arrayIndexOutOfBounds/out.txt
@@ -0,0 +1 @@
[samples\arrayIndexOutOfBounds\bad.c:6]: (error) Array 'a[2]' accessed at index 2, which is out of bounds.
1 change: 1 addition & 0 deletions samples/autoVariables/out.txt
@@ -0,0 +1 @@
[samples\autoVariables\bad.c:4]: (error) Address of local auto-variable assigned to a function parameter.
2 changes: 2 additions & 0 deletions samples/bufferAccessOutOfBounds/out.txt
@@ -0,0 +1,2 @@
[samples\bufferAccessOutOfBounds\bad.c:6]: (error) Buffer is accessed out of bounds: a
[samples\bufferAccessOutOfBounds\bad.c:6]: (error) Array 'a[2]' accessed at index 2, which is out of bounds.
1 change: 1 addition & 0 deletions samples/erase/out.txt
@@ -0,0 +1 @@
[samples\erase\bad.cpp:9] -> [samples\erase\bad.cpp:11]: (error) Iterator 'iter' used after element has been erased.
1 change: 1 addition & 0 deletions samples/memleak/out.txt
@@ -0,0 +1 @@
[samples\memleak\bad.c:8]: (error) Memory leak: a
1 change: 1 addition & 0 deletions samples/outOfBounds/out.txt
@@ -0,0 +1 @@
[samples\outOfBounds\bad.c:5]: (error) snprintf size is out of bounds: Supplied size 10 is larger than actual size 5.
1 change: 1 addition & 0 deletions samples/resourceLeak/out.txt
@@ -0,0 +1 @@
[samples\resourceLeak\bad.c:8]: (error) Resource leak: a
1 change: 1 addition & 0 deletions samples/syntaxError/out.txt
@@ -0,0 +1 @@
[samples\syntaxError\bad.c:2]: (error) Invalid number of character ({) when these macros are defined: 'A'.
13 changes: 13 additions & 0 deletions test/redirect.h
Expand Up @@ -61,6 +61,17 @@ class RedirectOutputError {
_out.str("");
}

/** Return what would be printed to cerr. See also clearErrout() */
std::string getErrout() const {
return _err.str();
}

/** Normally called after getErrout() to prevent same text to be returned
twice. */
void clearErrout() {
_err.str("");
}

private:
std::ostringstream _out;
std::ostringstream _err;
Expand All @@ -71,5 +82,7 @@ class RedirectOutputError {
#define REDIRECT RedirectOutputError redir;
#define GET_REDIRECT_OUTPUT redir.getOutput()
#define CLEAR_REDIRECT_OUTPUT redir.clearOutput()
#define GET_REDIRECT_ERROUT redir.getErrout()
#define CLEAR_REDIRECT_ERROUT redir.clearErrout()

#endif
4 changes: 0 additions & 4 deletions test/testcppcheck.cpp
Expand Up @@ -17,10 +17,6 @@
*/


// The preprocessor that Cppcheck uses is a bit special. Instead of generating
// the code for a known configuration, it generates the code for each configuration.


#include "cppcheck.h"
#include "cppcheckexecutor.h"
#include "testsuite.h"
Expand Down
1 change: 1 addition & 0 deletions test/testrunner.vcxproj
Expand Up @@ -63,6 +63,7 @@
<ClCompile Include="testpostfixoperator.cpp" />
<ClCompile Include="testpreprocessor.cpp" />
<ClCompile Include="testrunner.cpp" />
<ClCompile Include="testsamples.cpp" />
<ClCompile Include="testsimplifytokens.cpp" />
<ClCompile Include="testsizeof.cpp" />
<ClCompile Include="teststl.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions test/testrunner.vcxproj.filters
Expand Up @@ -181,6 +181,9 @@
<ClCompile Include="testvalueflow.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="testsamples.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="options.h">
Expand Down
83 changes: 83 additions & 0 deletions test/testsamples.cpp
@@ -0,0 +1,83 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#include "filelister.h"
#include "testsuite.h"
#include "cppcheckexecutor.h"
#include <fstream>
#include <cstring>
#include <algorithm>


class TestSamples : public TestFixture {
public:
TestSamples() : TestFixture("TestSamples") {
}

private:

void run() {
TEST_CASE(runSamples);
}

void runSamples() const {
REDIRECT;

std::map<std::string, std::size_t> files;
#ifdef _WIN32
FileLister::recursiveAddFiles(files, "..\\samples");
#else
FileLister::recursiveAddFiles(files, "samples");
#endif
for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i) {
CLEAR_REDIRECT_ERROUT;
char* path = new char[i->first.size() + 1];
strcpy(path, i->first.c_str());
const char* argv[] = {
#ifdef _WIN32
".\\..\\testrunner",
#else
"./testrunner",
#endif
"--enable=style,warning,performance,portability", "--inconclusive", "-rp", "-f", "-q", path
};
std::string filename = i->first.substr(i->first.find_last_of("/\\")+1);
if (filename == "good.cpp" || filename == "good.c") {
CppCheckExecutor exec;
exec.check(7, argv);
ASSERT_EQUALS_MSG("", GET_REDIRECT_ERROUT, i->first);
} else if (filename == "bad.cpp" || filename == "bad.c") {
CppCheckExecutor exec;
exec.check(7, argv);
std::string expected_filename = Path::getPathFromFilename(i->first) + "out.txt";
std::ifstream ifs(expected_filename.c_str());
std::string expected((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
std::string actual = GET_REDIRECT_ERROUT;
// We need some uniformization to make this work on Unix and Windows
std::replace(actual.begin(), actual.end(), '/', '\\'); // Uniformize slashes.
while (actual.find("..\\") != std::string::npos)
actual.erase(actual.find("..\\"), 3); // Remove '..\'
ASSERT_EQUALS_MSG(expected, actual, i->first);
}
delete[] path;
}
}
};

REGISTER_TEST(TestSamples)
4 changes: 0 additions & 4 deletions test/testthreadexecutor.cpp
Expand Up @@ -17,10 +17,6 @@
*/


// The preprocessor that Cppcheck uses is a bit special. Instead of generating
// the code for a known configuration, it generates the code for each configuration.


#include "cppcheck.h"
#include "testsuite.h"
#include "threadexecutor.h"
Expand Down

0 comments on commit c078b3f

Please sign in to comment.