Skip to content

Commit 569a29b

Browse files
committed
Library: Added simple testing
1 parent 6d982b8 commit 569a29b

File tree

5 files changed

+101
-4
lines changed

5 files changed

+101
-4
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ TESTOBJ = test/options.o \
163163
test/testinternal.o \
164164
test/testio.o \
165165
test/testleakautovar.o \
166+
test/testlibrary.o \
166167
test/testmathlib.o \
167168
test/testmemleak.o \
168169
test/testnonreentrantfunctions.o \
@@ -434,6 +435,9 @@ test/testio.o: test/testio.cpp lib/checkio.h lib/check.h lib/config.h lib/token.
434435
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
435436
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o test/testleakautovar.o test/testleakautovar.cpp
436437

438+
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
439+
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o test/testlibrary.o test/testlibrary.cpp
440+
437441
test/testmathlib.o: test/testmathlib.cpp lib/mathlib.h lib/config.h test/testsuite.h lib/errorlogger.h lib/suppressions.h test/redirect.h
438442
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o test/testmathlib.o test/testmathlib.cpp
439443

lib/library.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ Library::Library() : allocid(0)
2929

3030
bool Library::load(const char exename[], const char path[])
3131
{
32-
tinyxml2::XMLDocument doc;
33-
3432
if (std::strchr(path,',') != NULL) {
3533
bool ret = true;
3634
std::string p(path);
@@ -65,9 +63,15 @@ bool Library::load(const char exename[], const char path[])
6563
return false;
6664
}
6765

66+
tinyxml2::XMLDocument doc;
6867
if (doc.LoadFile(fp) != tinyxml2::XML_NO_ERROR)
6968
return false;
7069

70+
return load(doc);
71+
}
72+
73+
bool Library::load(const tinyxml2::XMLDocument &doc)
74+
{
7175
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
7276
if (strcmp(rootnode->Name(),"def") != 0)
7377
return false;

lib/library.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
//---------------------------------------------------------------------------
2323

2424
#include "config.h"
25+
#include "path.h"
26+
27+
#include <tinyxml2.h>
2528
#include <map>
2629
#include <set>
2730
#include <string>
2831
#include <list>
2932
#include <algorithm>
3033

31-
#include "path.h"
32-
3334
/// @addtogroup Core
3435
/// @{
3536

@@ -41,6 +42,7 @@ class CPPCHECKLIB Library {
4142
Library();
4243

4344
bool load(const char exename [], const char path []);
45+
bool load(const tinyxml2::XMLDocument &doc);
4446

4547
/** get allocation id for function (by name) */
4648
int alloc(const std::string &name) const {

test/testfiles.pri

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ SOURCES += $${BASEPATH}/test64bit.cpp \
2323
$${BASEPATH}/testinternal.cpp \
2424
$${BASEPATH}/testio.cpp \
2525
$${BASEPATH}/testleakautovar.cpp \
26+
$${BASEPATH}/testlibrary.cpp \
2627
$${BASEPATH}/testmathlib.cpp \
2728
$${BASEPATH}/testmemleak.cpp \
2829
$${BASEPATH}/testnonreentrantfunctions.cpp \

test/testlibrary.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2013 Daniel Marjamäki and 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+
#include "library.h"
20+
#include "testsuite.h"
21+
22+
class TestLibrary : public TestFixture {
23+
public:
24+
TestLibrary() : TestFixture("TestLibrary") { }
25+
26+
private:
27+
28+
void run() {
29+
TEST_CASE(empty);
30+
TEST_CASE(function);
31+
TEST_CASE(memory);
32+
}
33+
34+
void empty() {
35+
const char xmldata[] = "<?xml version=\"1.0\"?>\n<def/>";
36+
tinyxml2::XMLDocument doc;
37+
doc.Parse(xmldata, sizeof(xmldata));
38+
39+
Library library;
40+
library.load(doc);
41+
ASSERT(library.use.empty());
42+
ASSERT(library.leakignore.empty());
43+
ASSERT(library.argumentChecks.empty());
44+
}
45+
46+
void function() {
47+
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
48+
"<def>\n"
49+
" <function name=\"foo\">\n"
50+
" <noreturn>false</noreturn>\n"
51+
" </function>\n"
52+
"</def>";
53+
tinyxml2::XMLDocument doc;
54+
doc.Parse(xmldata, sizeof(xmldata));
55+
56+
Library library;
57+
library.load(doc);
58+
ASSERT(library.use.empty());
59+
ASSERT(library.leakignore.empty());
60+
ASSERT(library.argumentChecks.empty());
61+
ASSERT(library.isnotnoreturn("foo"));
62+
}
63+
64+
void memory() {
65+
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
66+
"<def>\n"
67+
" <memory>\n"
68+
" <alloc>CreateX</alloc>\n"
69+
" <dealloc>DeleteX</dealloc>\n"
70+
" </memory>\n"
71+
"</def>";
72+
tinyxml2::XMLDocument doc;
73+
doc.Parse(xmldata, sizeof(xmldata));
74+
75+
Library library;
76+
library.load(doc);
77+
ASSERT(library.use.empty());
78+
ASSERT(library.leakignore.empty());
79+
ASSERT(library.argumentChecks.empty());
80+
81+
ASSERT(Library::ismemory(library.alloc("CreateX")));
82+
ASSERT_EQUALS(library.alloc("CreateX"), library.dealloc("DeleteX"));
83+
}
84+
};
85+
86+
REGISTER_TEST(TestLibrary)

0 commit comments

Comments
 (0)