diff --git a/testbufferoverrun.cpp b/testbufferoverrun.cpp index 3be1c7a24..b9e424f00 100644 --- a/testbufferoverrun.cpp +++ b/testbufferoverrun.cpp @@ -2,15 +2,19 @@ #include "tokenize.h" #include "CommonCheck.h" #include "CheckBufferOverrun.h" -#include "MiniCppUnit.h" +#include "testsuite.h" #include extern std::ostringstream errout; extern bool ShowAll; -class TestBufferOverrun : public TestFixture +class TestBufferOverrun : public TestFixture { +public: + TestBufferOverrun() : TestFixture("TestBufferOverrun") + { } + private: void check( const char code[] ) { @@ -31,8 +35,7 @@ private: CheckBufferOverrun(); } -public: - TEST_FIXTURE( TestBufferOverrun ) + void run() { TEST_CASE( noerr1 ); TEST_CASE( noerr2 ); @@ -316,6 +319,6 @@ public: }; -REGISTER_FIXTURE( TestBufferOverrun ) +static TestBufferOverrun testbufferoverrun; diff --git a/testcharvar.cpp b/testcharvar.cpp index 7e4a3511c..00f67dae7 100644 --- a/testcharvar.cpp +++ b/testcharvar.cpp @@ -4,16 +4,26 @@ #include "tokenize.h" #include "CommonCheck.h" #include "CheckOther.h" -#include "MiniCppUnit.h" +#include "testsuite.h" #include extern std::ostringstream errout; extern bool ShowAll; -class TestCharVar : public TestFixture +class TestCharVar : public TestFixture { +public: + TestCharVar() : TestFixture("TestCharVar") + { } + private: + void run() + { + TEST_CASE( array_index ); + TEST_CASE( bitop ); + } + void check( const char code[] ) { // Tokenize.. @@ -32,14 +42,6 @@ private: CheckCharVariable(); } -public: - TEST_FIXTURE( TestCharVar ) - { - TEST_CASE( array_index ); - TEST_CASE( bitop ); - } - - void array_index() { check( "void foo()\n" @@ -75,5 +77,5 @@ public: } }; -REGISTER_FIXTURE( TestCharVar ) +static TestCharVar testcharvar; diff --git a/testconstructors.cpp b/testconstructors.cpp index 084b6300a..b04fb5b46 100644 --- a/testconstructors.cpp +++ b/testconstructors.cpp @@ -1,95 +1,100 @@ - -#include "tokenize.h" -#include "CheckClass.h" -#include "MiniCppUnit.h" - -#include - -extern std::ostringstream errout; - -class TestConstructors : public TestFixture -{ -private: - void check( const char code[] ) - { - // Tokenize.. - tokens = tokens_back = NULL; - std::istringstream istr(code); - TokenizeCode( istr ); - SimplifyTokenList(); - - // Clear the error buffer.. - errout.str(""); - - // Check for memory leaks.. - CheckConstructors(); - } - -public: - TEST_FIXTURE( TestConstructors ) - { - TEST_CASE( simple1 ); - TEST_CASE( simple2 ); - TEST_CASE( simple3 ); - TEST_CASE( simple4 ); - } - - - void simple1() - { - check( "class Fred\n" - "{\n" - "public:\n" - " int i;\n" - "};\n" ); - ASSERT_EQUALS( std::string("[test.cpp:1] The class 'Fred' has no constructor\n"), errout.str() ); - } - - - void simple2() - { - check( "class Fred\n" - "{\n" - "public:\n" - " Fred() { }\n" - " int i;\n" - "};\n" ); - ASSERT_EQUALS( std::string("[test.cpp:4] Uninitialized member variable 'Fred::i'\n"), errout.str() ); - } - - - void simple3() - { - check( "class Fred\n" - "{\n" - "public:\n" - " Fred();\n" - " int i;\n" - "};\n" - "Fred::Fred()\n" - "{ }\n" ); - ASSERT_EQUALS( std::string("[test.cpp:7] Uninitialized member variable 'Fred::i'\n"), errout.str() ); - } - - - void simple4() - { - check( "class Fred\n" - "{\n" - "public:\n" - " Fred();\n" - " Fred(int _i);\n" - " int i;\n" - "};\n" - "Fred::Fred()\n" - "{ }\n" - "Fred::Fred(int _i)\n" - "{\n" - " i = _i;\n" - "}\n" ); - ASSERT_EQUALS( std::string("[test.cpp:8] Uninitialized member variable 'Fred::i'\n"), errout.str() ); - } - -}; - -REGISTER_FIXTURE( TestConstructors ) + +#include "tokenize.h" +#include "CheckClass.h" +#include "testsuite.h" + +#include + +extern std::ostringstream errout; + +class TestConstructors : public TestFixture +{ +public: + TestConstructors() : TestFixture("TestConstructors") + { } + +private: + void check( const char code[] ) + { + // Tokenize.. + tokens = tokens_back = NULL; + std::istringstream istr(code); + TokenizeCode( istr ); + SimplifyTokenList(); + + // Clear the error buffer.. + errout.str(""); + + // Check for memory leaks.. + CheckConstructors(); + } + + void run() + { + TEST_CASE( simple1 ); + TEST_CASE( simple2 ); + TEST_CASE( simple3 ); + TEST_CASE( simple4 ); + } + + + void simple1() + { + check( "class Fred\n" + "{\n" + "public:\n" + " int i;\n" + "};\n" ); + std::string actual( errout.str() ); + std::string expected( "[test.cpp:1] The class 'Fred' has no constructor\n" ); + ASSERT_EQUALS( expected, actual ); + } + + + void simple2() + { + check( "class Fred\n" + "{\n" + "public:\n" + " Fred() { }\n" + " int i;\n" + "};\n" ); + ASSERT_EQUALS( std::string("[test.cpp:4] Uninitialized member variable 'Fred::i'\n"), errout.str() ); + } + + + void simple3() + { + check( "class Fred\n" + "{\n" + "public:\n" + " Fred();\n" + " int i;\n" + "};\n" + "Fred::Fred()\n" + "{ }\n" ); + ASSERT_EQUALS( std::string("[test.cpp:7] Uninitialized member variable 'Fred::i'\n"), errout.str() ); + } + + + void simple4() + { + check( "class Fred\n" + "{\n" + "public:\n" + " Fred();\n" + " Fred(int _i);\n" + " int i;\n" + "};\n" + "Fred::Fred()\n" + "{ }\n" + "Fred::Fred(int _i)\n" + "{\n" + " i = _i;\n" + "}\n" ); + ASSERT_EQUALS( std::string("[test.cpp:8] Uninitialized member variable 'Fred::i'\n"), errout.str() ); + } + +}; + +static TestConstructors testconstructors; diff --git a/testdivision.cpp b/testdivision.cpp index 5356e8a9c..0be9f2f79 100644 --- a/testdivision.cpp +++ b/testdivision.cpp @@ -5,15 +5,19 @@ #include "tokenize.h" #include "CheckOther.h" -#include "MiniCppUnit.h" +#include "testsuite.h" #include extern std::ostringstream errout; extern bool ShowAll; -class TestDivision : public TestFixture +class TestDivision : public TestFixture { +public: + TestDivision() : TestFixture("TestDivision") + { } + private: void check( const char code[] ) { @@ -31,8 +35,7 @@ private: CheckUnsignedDivision(); } -public: - TEST_FIXTURE( TestDivision ) + void run() { TEST_CASE( division1 ); TEST_CASE( division2 ); @@ -125,6 +128,6 @@ public: } }; -REGISTER_FIXTURE( TestDivision ) +static TestDivision testdivision; diff --git a/testmemleak.cpp b/testmemleak.cpp index e24a26870..25cd0b1b0 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -1,15 +1,19 @@ #include "tokenize.h" #include "CheckMemoryLeak.h" -#include "MiniCppUnit.h" +#include "testsuite.h" #include extern std::ostringstream errout; extern bool ShowAll; -class TestMemleak : public TestFixture +class TestMemleak : public TestFixture { +public: + TestMemleak() : TestFixture("TestMemleak") + { } + private: void check( const char code[] ) { @@ -27,8 +31,7 @@ private: CheckMemoryLeak(); } -public: - TEST_FIXTURE( TestMemleak ) + void run() { TEST_CASE( simple1 ); TEST_CASE( simple2 ); @@ -58,7 +61,6 @@ public: TEST_CASE( func2 ); TEST_CASE( class1 ); - } void simple1() @@ -524,6 +526,6 @@ public: }; -REGISTER_FIXTURE( TestMemleak ) +static TestMemleak testmemleak; diff --git a/testrunner.cbproj b/testrunner.cbproj index 1bb1c3aae..2a271426a 100644 --- a/testrunner.cbproj +++ b/testrunner.cbproj @@ -18,15 +18,15 @@ Base - exe true - JPHNE + exe NO_STRICT + JPHNE true - C:\cppcheck true - true + C:\cppcheck CppConsoleApplication + true . vclx.bpi;vcl.bpi;rtl.bpi;vclactnband.bpi false @@ -34,10 +34,10 @@ $(BDS)\lib;$(BDS)\lib\obj;$(BDS)\lib\psdk;C:\cppcheck - false false - true + false _DEBUG;$(Defines) + true true false true @@ -47,8 +47,8 @@ Debug true true - true $(BDS)\lib\debug;$(ILINK_LibraryPath) + true Full true @@ -71,49 +71,49 @@ CheckBufferOverrun.h - 9 + 7 CheckClass.h - 10 + 8 CheckMemoryLeak.h - 2 + 1 CheckOther.h - 11 + 9 CommonCheck.h - 5 - - 3 - 7 + 5 testcharvar.h - 12 + 10 - 8 - - 6 - + 4 - - 0 + + 2 + + + 11 + + + 12 tokenize.h - 1 + 0 Cfg_1 diff --git a/testrunner.cpp b/testrunner.cpp index 6f0f85399..358ae7ca4 100644 --- a/testrunner.cpp +++ b/testrunner.cpp @@ -1,10 +1,18 @@ - -#include "testsuite.h" - - -int main() -{ - TestSuite::runTests(); - return 0; -} - + +#include "testsuite.h" +#include +#include + + +bool ShowAll = false; +bool CheckCodingStyle = true; +extern std::vector Files; + + +int main() +{ + Files.push_back( "test.cpp" ); + TestFixture::runTests(); + return 0; +} + diff --git a/testsuite.cpp b/testsuite.cpp index 17cb28f18..b206236da 100644 --- a/testsuite.cpp +++ b/testsuite.cpp @@ -1,75 +1,101 @@ - -#include "testsuite.h" - -#include -#include - - - -/** - * TestRegistry - **/ - -class TestRegistry -{ -private: - std::list _tests; - -public: - static TestRegistry &theInstance() - { - static TestRegistry testreg; - return testreg; - } - - void addTest( TestSuite *t ) - { - _tests.push_back( t ); - } - - void removeTest( TestSuite *t ) - { - _tests.remove( t ); - } - - const std::list &tests() const - { return _tests; } -}; - - - - -/** - * TestSuite - **/ - -TestSuite::TestSuite(const std::string &_name) : classname(_name) -{ - TestRegistry::theInstance().addTest(this); -} - -TestSuite::~TestSuite() -{ - TestRegistry::theInstance().removeTest(this); -} - -void TestSuite::printTests() -{ - const std::list &tests = TestRegistry::theInstance().tests(); - - for ( std::list::const_iterator it = tests.begin(); it != tests.end(); ++it ) - { - std::cout << (*it)->classname << std::endl; - } -} - -void TestSuite::runTests() -{ - const std::list &tests = TestRegistry::theInstance().tests(); - - for ( std::list::const_iterator it = tests.begin(); it != tests.end(); ++it ) - { - (*it)->run(); - } -} - + +#include "testsuite.h" + +#include +#include + + + +/** + * TestRegistry + **/ + +class TestRegistry +{ +private: + std::list _tests; + +public: + static TestRegistry &theInstance() + { + static TestRegistry testreg; + return testreg; + } + + void addTest( TestFixture *t ) + { + _tests.push_back( t ); + } + + void removeTest( TestFixture *t ) + { + _tests.remove( t ); + } + + const std::list &tests() const + { + return _tests; + } +}; + + + + +/** + * TestFixture + **/ + +std::ostringstream TestFixture::errmsg; +unsigned int TestFixture::countTests; + +TestFixture::TestFixture(const std::string &_name) : classname(_name) +{ + TestRegistry::theInstance().addTest(this); +} + +TestFixture::~TestFixture() +{ + TestRegistry::theInstance().removeTest(this); +} + +bool TestFixture::runTest(const char testname[]) +{ + countTests++; + std::cout << classname << "::" << testname << "\n"; + return true; +} + +void TestFixture::assertFail(const char *filename, int linenr) +{ + errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl; +} + +void TestFixture::printTests() +{ + const std::list &tests = TestRegistry::theInstance().tests(); + + for ( std::list::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 &tests = TestRegistry::theInstance().tests(); + + for ( std::list::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(); +} + + + diff --git a/testsuite.h b/testsuite.h index 73f6b9971..eb802ed90 100644 --- a/testsuite.h +++ b/testsuite.h @@ -1,27 +1,31 @@ - -#include -#include - - -class TestSuite -{ -protected: - std::string classname; - - virtual void run() = 0; - -public: - TestSuite(const std::string &_name); - ~TestSuite(); - - static void printTests(); - static void runTests(); -}; - - -#define TEST_CASE( NAME ) std::cout << classname << "::" << #NAME << std::endl; NAME (); - -/* -#define ASSERT_EQUALS( EXPECTED , ACTUAL ) -*/ - + +#include + + +class TestFixture +{ +private: + static std::ostringstream errmsg; + static unsigned int countTests; + +protected: + std::string classname; + + virtual void run() + { } + + bool runTest(const char testname[]); + void assertFail(const char *filename, int linenr); + +public: + TestFixture(const std::string &_name); + ~TestFixture(); + + static void printTests(); + static void runTests(); +}; + + +#define TEST_CASE( NAME ) if ( runTest(#NAME) ) NAME (); +#define ASSERT_EQUALS( EXPECTED , ACTUAL ) if (EXPECTED!=ACTUAL) assertFail(__FILE__, __LINE__); +