Unit Testing: Moving memleak checks

This commit is contained in:
Daniel Marjamäki 2008-02-17 11:45:06 +00:00
parent 2dace08aae
commit c66846353c
9 changed files with 80 additions and 66 deletions

View File

@ -1 +0,0 @@
Memory leak for 'clKalle::str'

View File

@ -1,23 +0,0 @@
class clKalle
{
private:
char *str;
char *str2;
public:
clKalle();
~clKalle();
};
clKalle::clKalle()
{
str = new char[10];
str2 = new char[10];
}
clKalle::~clKalle()
{
delete [] str2;
}

View File

@ -1 +0,0 @@
[testmemleak2\testmemleak2.cpp:8]: Memory leak:str

View File

@ -1,12 +0,0 @@
bool f()
{
char *str = strdup("hello");
if (a==b)
{
return false;
}
free(str);
return true;
}

View File

@ -1 +0,0 @@
[testmemleak3\testmemleak3.cpp:11]: Memory leak:str

View File

@ -1,12 +0,0 @@
bool f()
{
char *str = strdup("hello");
if (a==b)
{
free(str);
return false;
}
return true;
}

View File

View File

@ -1,15 +0,0 @@
bool f()
{
TStringList *StringList = new TStringList;
if (asd)
{
delete StringList;
return false;
}
delete StringList;
return true;
}

View File

@ -25,6 +25,7 @@ static void constructors();
static void operator_eq();
static void mismatching_allocation_deallocation();
static void memleak_in_function();
static void memleak_in_class();
//---------------------------------------------------------------------------
int main()
@ -35,6 +36,7 @@ int main()
operator_eq();
memleak_in_function();
mismatching_allocation_deallocation();
memleak_in_class();
std::cout << "Success Rate: "
<< SuccessCount
<< " / "
@ -268,6 +270,7 @@ static void memleak_in_function()
// test3: check all execution paths
// test4: check all execution paths
// test5: check all execution paths
// test6: check all execution paths
const char test1[] = "void f()\n"
"{\n"
@ -334,6 +337,82 @@ static void memleak_in_function()
"}\n";
check( CheckMemoryLeak, __LINE__, test5, "" );
const char test6[] = "void f()\n"
"{\n"
" char *str = strdup(\"hello\");\n"
" if (a==b)\n"
" {\n"
" return;\n"
" }\n"
" free(str);\n"
"}\n";
check( CheckMemoryLeak, __LINE__, test6, "[test.cpp:6]: Memory leak:str\n" );
const char test7[] = "void f()\n"
"{\n"
" char *str = strdup(\"hello\");\n"
" if (a==b)\n"
" {\n"
" free(str);\n"
" return;\n"
" }\n"
"}\n";
check( CheckMemoryLeak, __LINE__, test7, "[test.cpp:9]: Memory leak:str\n" );
const char test8[] = "void f()\n"
"{\n"
" char *str = new char[10];\n"
" if (a==b)\n"
" {\n"
" delete [] str;\n"
" return;\n"
" }\n"
" delete [] str;\n"
"}\n";
check( CheckMemoryLeak, __LINE__, test8, "" );
}
//---------------------------------------------------------------------------
static void memleak_in_class()
{
const char test1[] = "class clKalle\n"
"{\n"
"private:\n"
" char *str1;\n"
" char *str2;\n"
"public:\n"
" clKalle();\n"
" ~clKalle();\n"
"};\n"
"\n"
"clKalle::clKalle()\n"
"{\n"
" str1 = new char[10];\n"
" str2 = new char[10];\n"
"}\n"
"\n"
"clKalle::~clKalle()\n"
"{\n"
" delete [] str2;\n"
"}\n";
check( CheckMemoryLeak, __LINE__, test1, "Memory leak for 'clKalle::str1'\n" );
}
//---------------------------------------------------------------------------