Testing: Updated the testing to the new test framework

This commit is contained in:
Daniel Marjamäki 2008-10-13 06:42:40 +00:00
parent 26549bf916
commit d330eb197a
9 changed files with 310 additions and 257 deletions

View File

@ -2,15 +2,19 @@
#include "tokenize.h" #include "tokenize.h"
#include "CommonCheck.h" #include "CommonCheck.h"
#include "CheckBufferOverrun.h" #include "CheckBufferOverrun.h"
#include "MiniCppUnit.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll; extern bool ShowAll;
class TestBufferOverrun : public TestFixture<TestBufferOverrun> class TestBufferOverrun : public TestFixture
{ {
public:
TestBufferOverrun() : TestFixture("TestBufferOverrun")
{ }
private: private:
void check( const char code[] ) void check( const char code[] )
{ {
@ -31,8 +35,7 @@ private:
CheckBufferOverrun(); CheckBufferOverrun();
} }
public: void run()
TEST_FIXTURE( TestBufferOverrun )
{ {
TEST_CASE( noerr1 ); TEST_CASE( noerr1 );
TEST_CASE( noerr2 ); TEST_CASE( noerr2 );
@ -316,6 +319,6 @@ public:
}; };
REGISTER_FIXTURE( TestBufferOverrun ) static TestBufferOverrun testbufferoverrun;

View File

@ -4,16 +4,26 @@
#include "tokenize.h" #include "tokenize.h"
#include "CommonCheck.h" #include "CommonCheck.h"
#include "CheckOther.h" #include "CheckOther.h"
#include "MiniCppUnit.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll; extern bool ShowAll;
class TestCharVar : public TestFixture<TestCharVar> class TestCharVar : public TestFixture
{ {
public:
TestCharVar() : TestFixture("TestCharVar")
{ }
private: private:
void run()
{
TEST_CASE( array_index );
TEST_CASE( bitop );
}
void check( const char code[] ) void check( const char code[] )
{ {
// Tokenize.. // Tokenize..
@ -32,14 +42,6 @@ private:
CheckCharVariable(); CheckCharVariable();
} }
public:
TEST_FIXTURE( TestCharVar )
{
TEST_CASE( array_index );
TEST_CASE( bitop );
}
void array_index() void array_index()
{ {
check( "void foo()\n" check( "void foo()\n"
@ -75,5 +77,5 @@ public:
} }
}; };
REGISTER_FIXTURE( TestCharVar ) static TestCharVar testcharvar;

View File

@ -1,95 +1,100 @@
#include "tokenize.h" #include "tokenize.h"
#include "CheckClass.h" #include "CheckClass.h"
#include "MiniCppUnit.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
class TestConstructors : public TestFixture<TestConstructors> class TestConstructors : public TestFixture
{ {
private: public:
void check( const char code[] ) TestConstructors() : TestFixture("TestConstructors")
{ { }
// Tokenize..
tokens = tokens_back = NULL; private:
std::istringstream istr(code); void check( const char code[] )
TokenizeCode( istr ); {
SimplifyTokenList(); // Tokenize..
tokens = tokens_back = NULL;
// Clear the error buffer.. std::istringstream istr(code);
errout.str(""); TokenizeCode( istr );
SimplifyTokenList();
// Check for memory leaks..
CheckConstructors(); // Clear the error buffer..
} errout.str("");
public: // Check for memory leaks..
TEST_FIXTURE( TestConstructors ) CheckConstructors();
{ }
TEST_CASE( simple1 );
TEST_CASE( simple2 ); void run()
TEST_CASE( simple3 ); {
TEST_CASE( simple4 ); TEST_CASE( simple1 );
} TEST_CASE( simple2 );
TEST_CASE( simple3 );
TEST_CASE( simple4 );
void simple1() }
{
check( "class Fred\n"
"{\n" void simple1()
"public:\n" {
" int i;\n" check( "class Fred\n"
"};\n" ); "{\n"
ASSERT_EQUALS( std::string("[test.cpp:1] The class 'Fred' has no constructor\n"), errout.str() ); "public:\n"
} " int i;\n"
"};\n" );
std::string actual( errout.str() );
void simple2() std::string expected( "[test.cpp:1] The class 'Fred' has no constructor\n" );
{ ASSERT_EQUALS( expected, actual );
check( "class Fred\n" }
"{\n"
"public:\n"
" Fred() { }\n" void simple2()
" int i;\n" {
"};\n" ); check( "class Fred\n"
ASSERT_EQUALS( std::string("[test.cpp:4] Uninitialized member variable 'Fred::i'\n"), errout.str() ); "{\n"
} "public:\n"
" Fred() { }\n"
" int i;\n"
void simple3() "};\n" );
{ ASSERT_EQUALS( std::string("[test.cpp:4] Uninitialized member variable 'Fred::i'\n"), errout.str() );
check( "class Fred\n" }
"{\n"
"public:\n"
" Fred();\n" void simple3()
" int i;\n" {
"};\n" check( "class Fred\n"
"Fred::Fred()\n" "{\n"
"{ }\n" ); "public:\n"
ASSERT_EQUALS( std::string("[test.cpp:7] Uninitialized member variable 'Fred::i'\n"), errout.str() ); " Fred();\n"
} " int i;\n"
"};\n"
"Fred::Fred()\n"
void simple4() "{ }\n" );
{ ASSERT_EQUALS( std::string("[test.cpp:7] Uninitialized member variable 'Fred::i'\n"), errout.str() );
check( "class Fred\n" }
"{\n"
"public:\n"
" Fred();\n" void simple4()
" Fred(int _i);\n" {
" int i;\n" check( "class Fred\n"
"};\n" "{\n"
"Fred::Fred()\n" "public:\n"
"{ }\n" " Fred();\n"
"Fred::Fred(int _i)\n" " Fred(int _i);\n"
"{\n" " int i;\n"
" i = _i;\n" "};\n"
"}\n" ); "Fred::Fred()\n"
ASSERT_EQUALS( std::string("[test.cpp:8] Uninitialized member variable 'Fred::i'\n"), errout.str() ); "{ }\n"
} "Fred::Fred(int _i)\n"
"{\n"
}; " i = _i;\n"
"}\n" );
REGISTER_FIXTURE( TestConstructors ) ASSERT_EQUALS( std::string("[test.cpp:8] Uninitialized member variable 'Fred::i'\n"), errout.str() );
}
};
static TestConstructors testconstructors;

View File

@ -5,15 +5,19 @@
#include "tokenize.h" #include "tokenize.h"
#include "CheckOther.h" #include "CheckOther.h"
#include "MiniCppUnit.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll; extern bool ShowAll;
class TestDivision : public TestFixture<TestDivision> class TestDivision : public TestFixture
{ {
public:
TestDivision() : TestFixture("TestDivision")
{ }
private: private:
void check( const char code[] ) void check( const char code[] )
{ {
@ -31,8 +35,7 @@ private:
CheckUnsignedDivision(); CheckUnsignedDivision();
} }
public: void run()
TEST_FIXTURE( TestDivision )
{ {
TEST_CASE( division1 ); TEST_CASE( division1 );
TEST_CASE( division2 ); TEST_CASE( division2 );
@ -125,6 +128,6 @@ public:
} }
}; };
REGISTER_FIXTURE( TestDivision ) static TestDivision testdivision;

View File

@ -1,15 +1,19 @@
#include "tokenize.h" #include "tokenize.h"
#include "CheckMemoryLeak.h" #include "CheckMemoryLeak.h"
#include "MiniCppUnit.h" #include "testsuite.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
extern bool ShowAll; extern bool ShowAll;
class TestMemleak : public TestFixture<TestMemleak> class TestMemleak : public TestFixture
{ {
public:
TestMemleak() : TestFixture("TestMemleak")
{ }
private: private:
void check( const char code[] ) void check( const char code[] )
{ {
@ -27,8 +31,7 @@ private:
CheckMemoryLeak(); CheckMemoryLeak();
} }
public: void run()
TEST_FIXTURE( TestMemleak )
{ {
TEST_CASE( simple1 ); TEST_CASE( simple1 );
TEST_CASE( simple2 ); TEST_CASE( simple2 );
@ -58,7 +61,6 @@ public:
TEST_CASE( func2 ); TEST_CASE( func2 );
TEST_CASE( class1 ); TEST_CASE( class1 );
} }
void simple1() void simple1()
@ -524,6 +526,6 @@ public:
}; };
REGISTER_FIXTURE( TestMemleak ) static TestMemleak testmemleak;

View File

@ -18,15 +18,15 @@
<CfgParent>Base</CfgParent> <CfgParent>Base</CfgParent>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Base)'!=''"> <PropertyGroup Condition="'$(Base)'!=''">
<OutputExt>exe</OutputExt>
<BCC_OptimizeForSpeed>true</BCC_OptimizeForSpeed> <BCC_OptimizeForSpeed>true</BCC_OptimizeForSpeed>
<DCC_CBuilderOutput>JPHNE</DCC_CBuilderOutput> <OutputExt>exe</OutputExt>
<Defines>NO_STRICT</Defines> <Defines>NO_STRICT</Defines>
<DCC_CBuilderOutput>JPHNE</DCC_CBuilderOutput>
<DynamicRTL>true</DynamicRTL> <DynamicRTL>true</DynamicRTL>
<ILINK_ObjectSearchPath>C:\cppcheck</ILINK_ObjectSearchPath>
<UsePackages>true</UsePackages> <UsePackages>true</UsePackages>
<NoVCL>true</NoVCL> <ILINK_ObjectSearchPath>C:\cppcheck</ILINK_ObjectSearchPath>
<ProjectType>CppConsoleApplication</ProjectType> <ProjectType>CppConsoleApplication</ProjectType>
<NoVCL>true</NoVCL>
<FinalOutputDir>.</FinalOutputDir> <FinalOutputDir>.</FinalOutputDir>
<PackageImports>vclx.bpi;vcl.bpi;rtl.bpi;vclactnband.bpi</PackageImports> <PackageImports>vclx.bpi;vcl.bpi;rtl.bpi;vclactnband.bpi</PackageImports>
<BCC_wpar>false</BCC_wpar> <BCC_wpar>false</BCC_wpar>
@ -34,10 +34,10 @@
<ILINK_LibraryPath>$(BDS)\lib;$(BDS)\lib\obj;$(BDS)\lib\psdk;C:\cppcheck</ILINK_LibraryPath> <ILINK_LibraryPath>$(BDS)\lib;$(BDS)\lib\obj;$(BDS)\lib\psdk;C:\cppcheck</ILINK_LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Cfg_1)'!=''"> <PropertyGroup Condition="'$(Cfg_1)'!=''">
<BCC_OptimizeForSpeed>false</BCC_OptimizeForSpeed>
<DCC_Optimize>false</DCC_Optimize> <DCC_Optimize>false</DCC_Optimize>
<DCC_DebugInfoInExe>true</DCC_DebugInfoInExe> <BCC_OptimizeForSpeed>false</BCC_OptimizeForSpeed>
<Defines>_DEBUG;$(Defines)</Defines> <Defines>_DEBUG;$(Defines)</Defines>
<DCC_DebugInfoInExe>true</DCC_DebugInfoInExe>
<ILINK_FullDebugInfo>true</ILINK_FullDebugInfo> <ILINK_FullDebugInfo>true</ILINK_FullDebugInfo>
<BCC_InlineFunctionExpansion>false</BCC_InlineFunctionExpansion> <BCC_InlineFunctionExpansion>false</BCC_InlineFunctionExpansion>
<ILINK_DisableIncrementalLinking>true</ILINK_DisableIncrementalLinking> <ILINK_DisableIncrementalLinking>true</ILINK_DisableIncrementalLinking>
@ -47,8 +47,8 @@
<IntermediateOutputDir>Debug</IntermediateOutputDir> <IntermediateOutputDir>Debug</IntermediateOutputDir>
<TASM_DisplaySourceLines>true</TASM_DisplaySourceLines> <TASM_DisplaySourceLines>true</TASM_DisplaySourceLines>
<BCC_StackFrames>true</BCC_StackFrames> <BCC_StackFrames>true</BCC_StackFrames>
<BCC_DisableOptimizations>true</BCC_DisableOptimizations>
<ILINK_LibraryPath>$(BDS)\lib\debug;$(ILINK_LibraryPath)</ILINK_LibraryPath> <ILINK_LibraryPath>$(BDS)\lib\debug;$(ILINK_LibraryPath)</ILINK_LibraryPath>
<BCC_DisableOptimizations>true</BCC_DisableOptimizations>
<TASM_Debugging>Full</TASM_Debugging> <TASM_Debugging>Full</TASM_Debugging>
<BCC_SourceDebuggingOn>true</BCC_SourceDebuggingOn> <BCC_SourceDebuggingOn>true</BCC_SourceDebuggingOn>
</PropertyGroup> </PropertyGroup>
@ -71,49 +71,49 @@
<ItemGroup> <ItemGroup>
<CppCompile Include="CheckBufferOverrun.cpp"> <CppCompile Include="CheckBufferOverrun.cpp">
<DependentOn>CheckBufferOverrun.h</DependentOn> <DependentOn>CheckBufferOverrun.h</DependentOn>
<BuildOrder>9</BuildOrder> <BuildOrder>7</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="CheckClass.cpp"> <CppCompile Include="CheckClass.cpp">
<DependentOn>CheckClass.h</DependentOn> <DependentOn>CheckClass.h</DependentOn>
<BuildOrder>10</BuildOrder> <BuildOrder>8</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="CheckMemoryLeak.cpp"> <CppCompile Include="CheckMemoryLeak.cpp">
<DependentOn>CheckMemoryLeak.h</DependentOn> <DependentOn>CheckMemoryLeak.h</DependentOn>
<BuildOrder>2</BuildOrder> <BuildOrder>1</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="CheckOther.cpp"> <CppCompile Include="CheckOther.cpp">
<DependentOn>CheckOther.h</DependentOn> <DependentOn>CheckOther.h</DependentOn>
<BuildOrder>11</BuildOrder> <BuildOrder>9</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="CommonCheck.cpp"> <CppCompile Include="CommonCheck.cpp">
<DependentOn>CommonCheck.h</DependentOn> <DependentOn>CommonCheck.h</DependentOn>
<BuildOrder>5</BuildOrder>
</CppCompile>
<CppCompile Include="MiniCppUnit.cpp">
<BuildOrder>3</BuildOrder> <BuildOrder>3</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="testbufferoverrun.cpp"> <CppCompile Include="testbufferoverrun.cpp">
<BuildOrder>7</BuildOrder> <BuildOrder>5</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="testcharvar.cpp"> <CppCompile Include="testcharvar.cpp">
<DependentOn>testcharvar.h</DependentOn> <DependentOn>testcharvar.h</DependentOn>
<BuildOrder>12</BuildOrder> <BuildOrder>10</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="testconstructors.cpp"> <CppCompile Include="testconstructors.cpp">
<BuildOrder>8</BuildOrder>
</CppCompile>
<CppCompile Include="testdivision.cpp">
<BuildOrder>6</BuildOrder> <BuildOrder>6</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="testmemleak.cpp"> <CppCompile Include="testdivision.cpp">
<BuildOrder>4</BuildOrder> <BuildOrder>4</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="TestsRunner.cpp"> <CppCompile Include="testmemleak.cpp">
<BuildOrder>0</BuildOrder> <BuildOrder>2</BuildOrder>
</CppCompile>
<CppCompile Include="testrunner.cpp">
<BuildOrder>11</BuildOrder>
</CppCompile>
<CppCompile Include="testsuite.cpp">
<BuildOrder>12</BuildOrder>
</CppCompile> </CppCompile>
<CppCompile Include="tokenize.cpp"> <CppCompile Include="tokenize.cpp">
<DependentOn>tokenize.h</DependentOn> <DependentOn>tokenize.h</DependentOn>
<BuildOrder>1</BuildOrder> <BuildOrder>0</BuildOrder>
</CppCompile> </CppCompile>
<BuildConfiguration Include="Debug"> <BuildConfiguration Include="Debug">
<Key>Cfg_1</Key> <Key>Cfg_1</Key>

View File

@ -1,10 +1,18 @@
#include "testsuite.h" #include "testsuite.h"
#include <string>
#include <vector>
int main()
{
TestSuite::runTests(); bool ShowAll = false;
return 0; bool CheckCodingStyle = true;
} extern std::vector<std::string> Files;
int main()
{
Files.push_back( "test.cpp" );
TestFixture::runTests();
return 0;
}

View File

@ -1,75 +1,101 @@
#include "testsuite.h" #include "testsuite.h"
#include <iostream> #include <iostream>
#include <list> #include <list>
/** /**
* TestRegistry * TestRegistry
**/ **/
class TestRegistry class TestRegistry
{ {
private: private:
std::list<TestSuite *> _tests; std::list<TestFixture *> _tests;
public: public:
static TestRegistry &theInstance() static TestRegistry &theInstance()
{ {
static TestRegistry testreg; static TestRegistry testreg;
return testreg; return testreg;
} }
void addTest( TestSuite *t ) void addTest( TestFixture *t )
{ {
_tests.push_back( t ); _tests.push_back( t );
} }
void removeTest( TestSuite *t ) void removeTest( TestFixture *t )
{ {
_tests.remove( t ); _tests.remove( t );
} }
const std::list<TestSuite *> &tests() const const std::list<TestFixture *> &tests() const
{ return _tests; } {
}; return _tests;
}
};
/**
* TestSuite
**/ /**
* TestFixture
TestSuite::TestSuite(const std::string &_name) : classname(_name) **/
{
TestRegistry::theInstance().addTest(this); std::ostringstream TestFixture::errmsg;
} unsigned int TestFixture::countTests;
TestSuite::~TestSuite() TestFixture::TestFixture(const std::string &_name) : classname(_name)
{ {
TestRegistry::theInstance().removeTest(this); TestRegistry::theInstance().addTest(this);
} }
void TestSuite::printTests() TestFixture::~TestFixture()
{ {
const std::list<TestSuite *> &tests = TestRegistry::theInstance().tests(); TestRegistry::theInstance().removeTest(this);
}
for ( std::list<TestSuite *>::const_iterator it = tests.begin(); it != tests.end(); ++it )
{ bool TestFixture::runTest(const char testname[])
std::cout << (*it)->classname << std::endl; {
} countTests++;
} std::cout << classname << "::" << testname << "\n";
return true;
void TestSuite::runTests() }
{
const std::list<TestSuite *> &tests = TestRegistry::theInstance().tests(); void TestFixture::assertFail(const char *filename, int linenr)
{
for ( std::list<TestSuite *>::const_iterator it = tests.begin(); it != tests.end(); ++it ) errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl;
{ }
(*it)->run();
} void TestFixture::printTests()
} {
const std::list<TestFixture *> &tests = TestRegistry::theInstance().tests();
for ( std::list<TestFixture *>::const_iterator it = tests.begin(); it != tests.end(); ++it )
{
std::cout << (*it)->classname << std::endl;
}
}
void TestFixture::runTests()
{
countTests = 0;
errmsg.str("");
const std::list<TestFixture *> &tests = TestRegistry::theInstance().tests();
for ( std::list<TestFixture *>::const_iterator it = tests.begin(); it != tests.end(); ++it )
{
(*it)->run();
}
std::cout << "\n\nTesting Complete\nNumber of tests: " << countTests << "\n";
std::cerr << errmsg.str();
}

View File

@ -1,27 +1,31 @@
#include <iostream> #include <sstream>
#include <string>
class TestFixture
class TestSuite {
{ private:
protected: static std::ostringstream errmsg;
std::string classname; static unsigned int countTests;
virtual void run() = 0; protected:
std::string classname;
public:
TestSuite(const std::string &_name); virtual void run()
~TestSuite(); { }
static void printTests(); bool runTest(const char testname[]);
static void runTests(); void assertFail(const char *filename, int linenr);
};
public:
TestFixture(const std::string &_name);
#define TEST_CASE( NAME ) std::cout << classname << "::" << #NAME << std::endl; NAME (); ~TestFixture();
/* static void printTests();
#define ASSERT_EQUALS( EXPECTED , ACTUAL ) static void runTests();
*/ };
#define TEST_CASE( NAME ) if ( runTest(#NAME) ) NAME ();
#define ASSERT_EQUALS( EXPECTED , ACTUAL ) if (EXPECTED!=ACTUAL) assertFail(__FILE__, __LINE__);