Unit Testing: Moved 'testmemcheck'
This commit is contained in:
parent
84286241ef
commit
2dace08aae
|
@ -498,6 +498,10 @@ static void _ClassMembers()
|
|||
|
||||
void CheckMemoryLeak()
|
||||
{
|
||||
// Create statement list.
|
||||
Statements.clear();
|
||||
CreateStatementList();
|
||||
|
||||
// Check for memory leaks inside functions..
|
||||
_InFunction();
|
||||
|
||||
|
|
6
main.cpp
6
main.cpp
|
@ -3,7 +3,6 @@
|
|||
#include <sstream>
|
||||
|
||||
#include "tokenize.h" // <- Tokenizer
|
||||
#include "Statements.h" // <- Statement list
|
||||
#include "CommonCheck.h"
|
||||
|
||||
#include "CheckMemoryLeak.h"
|
||||
|
@ -99,11 +98,6 @@ static void CppCheck(const char FileName[])
|
|||
|
||||
SimplifyTokenList();
|
||||
|
||||
|
||||
// Create a statement list. It's used by for example 'CheckMemoryLeak'
|
||||
CreateStatementList();
|
||||
|
||||
|
||||
// Memory leak
|
||||
CheckMemoryLeak();
|
||||
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
[testmemcheck1\testmemcheck1.cpp:5]: Mismatching allocation and deallocation 'a'
|
|
@ -1,7 +0,0 @@
|
|||
|
||||
void f()
|
||||
{
|
||||
int *a = new int[10];
|
||||
delete a;
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
|
||||
Fred *newfred()
|
||||
{
|
||||
Fred *f = new Fred();
|
||||
return f;
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
|
||||
|
||||
|
||||
|
||||
void f()
|
||||
{
|
||||
Kalle *kalle;
|
||||
if (somecondition)
|
||||
{
|
||||
kalle = new Kalle;
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
delete kalle;
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
[testmemcheck4\testmemcheck4.cpp:9]: Memory leak:str
|
|
@ -1,13 +0,0 @@
|
|||
|
||||
|
||||
void f()
|
||||
{
|
||||
for (int i = 0; i < j; i++)
|
||||
{
|
||||
char *str = strdup("hello");
|
||||
if (condition)
|
||||
continue;
|
||||
free(str);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
|
||||
|
||||
void f()
|
||||
{
|
||||
char *str = strdup("hello");
|
||||
while (condition)
|
||||
{
|
||||
if (condition)
|
||||
break;
|
||||
}
|
||||
free(str);
|
||||
}
|
||||
|
96
tests.cpp
96
tests.cpp
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include "CheckBufferOverrun.h"
|
||||
#include "CheckClass.h"
|
||||
#include "CheckMemoryLeak.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
@ -22,6 +23,8 @@ static unsigned int FailCount, SuccessCount;
|
|||
static void buffer_overrun();
|
||||
static void constructors();
|
||||
static void operator_eq();
|
||||
static void mismatching_allocation_deallocation();
|
||||
static void memleak_in_function();
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
int main()
|
||||
|
@ -30,6 +33,8 @@ int main()
|
|||
buffer_overrun();
|
||||
constructors();
|
||||
operator_eq();
|
||||
memleak_in_function();
|
||||
mismatching_allocation_deallocation();
|
||||
std::cout << "Success Rate: "
|
||||
<< SuccessCount
|
||||
<< " / "
|
||||
|
@ -241,3 +246,94 @@ void operator_eq()
|
|||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
static void mismatching_allocation_deallocation()
|
||||
{
|
||||
// TODO: This check must be created as I can't find it anywhere
|
||||
|
||||
/*
|
||||
const char test1[] = "void f()\n"
|
||||
"{\n"
|
||||
" int *a = new int[10];\n"
|
||||
" free(a);\n"
|
||||
"}\n";
|
||||
check( CheckMismatchingAllocationDeallocation, __LINE__, test1, "[test.cpp:4]: Mismatching allocation / deallocation\n" );
|
||||
*/
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
static void memleak_in_function()
|
||||
{
|
||||
// test1: 'new' but not 'delete'
|
||||
// test2: Return allocated memory
|
||||
// test3: check all execution paths
|
||||
// test4: check all execution paths
|
||||
// test5: check all execution paths
|
||||
|
||||
const char test1[] = "void f()\n"
|
||||
"{\n"
|
||||
" int *a = new int[10];\n"
|
||||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test1, "[test.cpp:4]: Memory leak:a\n" );
|
||||
|
||||
|
||||
|
||||
|
||||
const char test2[] = "Fred *NewFred()\n"
|
||||
"{\n"
|
||||
" Fred *f = new Fred;\n"
|
||||
" return f;\n"
|
||||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test2, "" );
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const char test3[] = "void f()\n"
|
||||
"{\n"
|
||||
" Kalle *kalle;\n"
|
||||
" if (somecondition)\n"
|
||||
" {\n"
|
||||
" kalle = new Kalle;\n"
|
||||
" }\n"
|
||||
" else\n"
|
||||
" {\n"
|
||||
" return;\n"
|
||||
" }\n"
|
||||
" delete kalle;\n"
|
||||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test3, "" );
|
||||
|
||||
|
||||
|
||||
const char test4[] = "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__, test4, "[test.cpp:7]: Memory leak:str\n" );
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const char test5[] = "void f()\n"
|
||||
"{\n"
|
||||
" char *str = strdup(\"hello\");\n"
|
||||
" while (condition)\n"
|
||||
" {\n"
|
||||
" if (condition)\n"
|
||||
" break;\n"
|
||||
" }\n"
|
||||
" free(str);\n"
|
||||
"}\n";
|
||||
check( CheckMemoryLeak, __LINE__, test5, "" );
|
||||
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
|
Loading…
Reference in New Issue