Library: Added simple testing

This commit is contained in:
Daniel Marjamäki 2013-10-27 17:10:43 +01:00
parent 6d982b823d
commit 569a29bc4a
5 changed files with 101 additions and 4 deletions

View File

@ -163,6 +163,7 @@ TESTOBJ = test/options.o \
test/testinternal.o \
test/testio.o \
test/testleakautovar.o \
test/testlibrary.o \
test/testmathlib.o \
test/testmemleak.o \
test/testnonreentrantfunctions.o \
@ -434,6 +435,9 @@ test/testio.o: test/testio.cpp lib/checkio.h lib/check.h lib/config.h lib/token.
test/testleakautovar.o: test/testleakautovar.cpp lib/tokenize.h lib/errorlogger.h lib/config.h lib/suppressions.h lib/tokenlist.h lib/checkleakautovar.h lib/check.h lib/token.h lib/settings.h lib/library.h lib/path.h lib/standards.h test/testsuite.h test/redirect.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o test/testleakautovar.o test/testleakautovar.cpp
test/testlibrary.o: test/testlibrary.cpp lib/library.h lib/config.h lib/path.h test/testsuite.h lib/errorlogger.h lib/suppressions.h test/redirect.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o test/testlibrary.o test/testlibrary.cpp
test/testmathlib.o: test/testmathlib.cpp lib/mathlib.h lib/config.h test/testsuite.h lib/errorlogger.h lib/suppressions.h test/redirect.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o test/testmathlib.o test/testmathlib.cpp

View File

@ -29,8 +29,6 @@ Library::Library() : allocid(0)
bool Library::load(const char exename[], const char path[])
{
tinyxml2::XMLDocument doc;
if (std::strchr(path,',') != NULL) {
bool ret = true;
std::string p(path);
@ -65,9 +63,15 @@ bool Library::load(const char exename[], const char path[])
return false;
}
tinyxml2::XMLDocument doc;
if (doc.LoadFile(fp) != tinyxml2::XML_NO_ERROR)
return false;
return load(doc);
}
bool Library::load(const tinyxml2::XMLDocument &doc)
{
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
if (strcmp(rootnode->Name(),"def") != 0)
return false;

View File

@ -22,14 +22,15 @@
//---------------------------------------------------------------------------
#include "config.h"
#include "path.h"
#include <tinyxml2.h>
#include <map>
#include <set>
#include <string>
#include <list>
#include <algorithm>
#include "path.h"
/// @addtogroup Core
/// @{
@ -41,6 +42,7 @@ public:
Library();
bool load(const char exename [], const char path []);
bool load(const tinyxml2::XMLDocument &doc);
/** get allocation id for function (by name) */
int alloc(const std::string &name) const {

View File

@ -23,6 +23,7 @@ SOURCES += $${BASEPATH}/test64bit.cpp \
$${BASEPATH}/testinternal.cpp \
$${BASEPATH}/testio.cpp \
$${BASEPATH}/testleakautovar.cpp \
$${BASEPATH}/testlibrary.cpp \
$${BASEPATH}/testmathlib.cpp \
$${BASEPATH}/testmemleak.cpp \
$${BASEPATH}/testnonreentrantfunctions.cpp \

86
test/testlibrary.cpp Normal file
View File

@ -0,0 +1,86 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2013 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 "library.h"
#include "testsuite.h"
class TestLibrary : public TestFixture {
public:
TestLibrary() : TestFixture("TestLibrary") { }
private:
void run() {
TEST_CASE(empty);
TEST_CASE(function);
TEST_CASE(memory);
}
void empty() {
const char xmldata[] = "<?xml version=\"1.0\"?>\n<def/>";
tinyxml2::XMLDocument doc;
doc.Parse(xmldata, sizeof(xmldata));
Library library;
library.load(doc);
ASSERT(library.use.empty());
ASSERT(library.leakignore.empty());
ASSERT(library.argumentChecks.empty());
}
void function() {
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"foo\">\n"
" <noreturn>false</noreturn>\n"
" </function>\n"
"</def>";
tinyxml2::XMLDocument doc;
doc.Parse(xmldata, sizeof(xmldata));
Library library;
library.load(doc);
ASSERT(library.use.empty());
ASSERT(library.leakignore.empty());
ASSERT(library.argumentChecks.empty());
ASSERT(library.isnotnoreturn("foo"));
}
void memory() {
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <memory>\n"
" <alloc>CreateX</alloc>\n"
" <dealloc>DeleteX</dealloc>\n"
" </memory>\n"
"</def>";
tinyxml2::XMLDocument doc;
doc.Parse(xmldata, sizeof(xmldata));
Library library;
library.load(doc);
ASSERT(library.use.empty());
ASSERT(library.leakignore.empty());
ASSERT(library.argumentChecks.empty());
ASSERT(Library::ismemory(library.alloc("CreateX")));
ASSERT_EQUALS(library.alloc("CreateX"), library.dealloc("DeleteX"));
}
};
REGISTER_TEST(TestLibrary)