testing: Moved tests from tests.cpp to testmemleak.cpp (forwhile, switch)

This commit is contained in:
Daniel Marjamäki 2008-08-22 06:30:06 +00:00
parent 794cdfd2ee
commit c618971609
5 changed files with 618 additions and 599 deletions

View File

@ -251,20 +251,16 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
}
else
{
if (Match(tok, "if"))
addtoken("if");
if (Match(tok, "else"))
addtoken("else");
if (Match(tok, "switch"))
addtoken("switch");
if (Match(tok, "if") ||
Match(tok, "else") ||
Match(tok, "switch") ||
Match(tok, "case") ||
Match(tok, "default"))
addtoken(tok->str);
}
// Loops..
if ( Match(tok, "for") )
addtoken("loop");
if ( Match(tok, "while") )
addtoken("loop");
if ( Match(tok, "do") )
if (Match(tok, "for") || Match(tok, "while") || Match(tok, "do") )
addtoken("loop");
// continue / break..

View File

@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2003-2004 Pau Arum<EFBFBD> & David Garc<EFBFBD>a
*
*
@ -22,7 +22,7 @@
#include <cmath>
#ifdef _MSC_VER
#if defined(_MSC_VER) || defined(__BORLANDC__)
#include <float.h>
namespace std
{
@ -163,12 +163,12 @@ bool isNaN(double x)
double scaledEpsilon(const double& expected, const double& fuzzyEpsilon )
{
const double aa = fabs(expected)+1;
const double aa = std::fabs(expected)+1;
return (std::isinf(aa))? fuzzyEpsilon: fuzzyEpsilon * aa;
}
bool fuzzyEquals(double expected, double result, double fuzzyEpsilon)
{
return (expected==result) || ( fabs(expected-result) <= scaledEpsilon(expected, fuzzyEpsilon) );
return (expected==result) || ( std::fabs(expected-result) <= scaledEpsilon(expected, fuzzyEpsilon) );
}
void Assert::assertEquals( const double& expected, const double& result,
const char* file, int linia )

View File

@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2003-2004 Pau Arum<EFBFBD> & David Garc<EFBFBD>a
*
*
@ -163,7 +163,7 @@ class Assert
static const char * errmsgTag_butWas() { return "But was: "; }
public:
#ifdef _MSC_VER
#if defined(_MSC_VER) || defined(__BORLANDC__)
static const char * blue() { return ""; }
static const char * green() { return ""; }
static const char * red() { return ""; }

View File

@ -43,6 +43,16 @@ public:
TEST_CASE( ifelse3 );
TEST_CASE( ifelse4 );
TEST_CASE( ifelse5 );
TEST_CASE( forwhile1 );
TEST_CASE( forwhile2 );
TEST_CASE( switch1 );
// TODO: TEST_CASE( switch2 );
TEST_CASE( mismatch1 );
}
void simple1()
@ -246,6 +256,102 @@ public:
"}\n" );
ASSERT_EQUALS( std::string(""), errout.str() );
}
void forwhile1()
{
check("void f()\n"
"{\n"
" char *str = strdup(\"hello\");\n"
" while (condition)\n"
" {\n"
" if (condition)\n"
" {\n"
" break;\n"
" }\n"
" }\n"
" free(str);\n"
"}\n");
ASSERT_EQUALS( std::string(""), errout.str() );
}
void forwhile2()
{
check("void f()\n"
"{\n"
" for (int i = 0; i < j; i++)\n"
" {\n"
" char *str = strdup(\"hello\");\n"
" if (condition)\n"
" continue;\n"
" free(str);\n"
" }\n"
"}\n");
ASSERT_EQUALS( std::string("[test.cpp:7]: Memory leak: str\n"), errout.str() );
}
void switch1()
{
check("void f()\n"
"{\n"
" char *str = new char[10];\n"
" switch (abc)\n"
" {\n"
" case 1:\n"
" break;\n"
" };\n"
" delete [] str;\n"
"}\n");
ASSERT_EQUALS( std::string(""), errout.str() );
}
void switch2()
{
check("void f()\n"
"{\n"
" char *str = new char[10];\n"
" switch (abc)\n"
" {\n"
" case 1:\n"
" delete [] str;\n"
" break;\n"
" default:\n"
" break;\n"
" };\n"
"}\n");
ASSERT_EQUALS( std::string("[test.cpp:12]: Memory leak"), errout.str() );
}
void mismatch1()
{
check( "void f()\n"
"{\n"
" int *a = new int[10];\n"
" free(a);\n"
"}\n");
ASSERT_EQUALS( std::string("[test.cpp:4]: Mismatching allocation and deallocation: a\n"), errout.str() );
}
};
REGISTER_FIXTURE( TestMemleak )

View File

@ -455,31 +455,8 @@ static void operator_eq()
}
//---------------------------------------------------------------------------
static void check_(void (chk)(),
const unsigned int line,
const char code[],
const char msg[])
{
ShowAll = false;
check( chk, line, code, msg );
ShowAll = true;
check( chk, line, code, msg );
}
static void memleak_in_function()
{
// There are 2 sections:
// * Simple testcases
// * if else
// * for/while
// * switch
// * mismatching allocation and deallocation
// * garbage collection
// * arrays
// * struct members
// * function calls
////////////////////////////////////////////////
@ -487,32 +464,6 @@ static void memleak_in_function()
////////////////////////////////////////////////
code = "void f()\n"
"{\n"
" char *str = strdup(\"hello\");\n"
" while (condition)\n"
" {\n"
" if (condition)\n"
" {\n"
" break;\n"
" }\n"
" }\n"
" free(str);\n"
"}\n";
check_( CheckMemoryLeak, __LINE__, code, "" );
code = "void f()\n"
"{\n"
" for (int i = 0; i < j; i++)\n"
" {\n"
" char *str = strdup(\"hello\");\n"
" if (condition)\n"
" continue;\n"
" free(str);\n"
" }\n"
"}\n";
check_( CheckMemoryLeak, __LINE__, code, "[test.cpp:7]: Memory leak: str\n" );
@ -520,40 +471,6 @@ static void memleak_in_function()
////////////////////////////////////////////////
// switch
////////////////////////////////////////////////
code = "void f()\n"
"{\n"
" char *str = new char[10];\n"
" switch (abc)\n"
" {\n"
" case 1:\n"
" break;\n"
" };\n"
" delete [] str;\n"
"}\n";
check_( CheckMemoryLeak, __LINE__, code, "" );
////////////////////////////////////////////////
// mismatching allocation and deallocation
////////////////////////////////////////////////
code = "void f()\n"
"{\n"
" int *a = new int[10];\n"
" free(a);\n"
"}\n";
check_( CheckMemoryLeak, __LINE__, code, "[test.cpp:4]: Mismatching allocation and deallocation: a\n" );