Unit Testing: Moved the 'internaltesting'

This commit is contained in:
Daniel Marjamäki 2008-02-17 15:23:14 +00:00
parent 92b5af4abc
commit 5450b64a8a
15 changed files with 261 additions and 218 deletions

View File

@ -358,111 +358,117 @@ void CreateStatementList()
} }
if (Debug) if (Debug)
{
OutputStatementList( std::cout );
}
}
void OutputStatementList( std::ostream &ostr )
{ {
std::list<STATEMENT>::const_iterator it; std::list<STATEMENT>::const_iterator it;
for (it = Statements.begin(); it != Statements.end(); it++) for (it = Statements.begin(); it != Statements.end(); it++)
{ {
STATEMENT s = *it; STATEMENT s = *it;
std::cout << it->Token->linenr << " : "; // ostr << it->Token->linenr << " : ";
switch (s.Type) switch (s.Type)
{ {
case STATEMENT::OBRACE: case STATEMENT::OBRACE:
std::cout << "{"; ostr << "{";
break; break;
case STATEMENT::EBRACE: case STATEMENT::EBRACE:
std::cout << "}"; ostr << "}";
break; break;
case STATEMENT::DECL: case STATEMENT::DECL:
std::cout << "decl " << VariableNames[s.VarIndex]; ostr << "decl " << VariableNames[s.VarIndex];
break; break;
case STATEMENT::ASSIGN: case STATEMENT::ASSIGN:
std::cout << "assign " << VariableNames[s.VarIndex]; ostr << "assign " << VariableNames[s.VarIndex];
break; break;
case STATEMENT::MALLOC: case STATEMENT::MALLOC:
std::cout << "malloc " << VariableNames[s.VarIndex]; ostr << "malloc " << VariableNames[s.VarIndex];
break; break;
case STATEMENT::FREE: case STATEMENT::FREE:
std::cout << "free " << VariableNames[s.VarIndex]; ostr << "free " << VariableNames[s.VarIndex];
break; break;
case STATEMENT::NEW: case STATEMENT::NEW:
std::cout << "new " << VariableNames[s.VarIndex]; ostr << "new " << VariableNames[s.VarIndex];
break; break;
case STATEMENT::NEWARRAY: case STATEMENT::NEWARRAY:
std::cout << "new[] " << VariableNames[s.VarIndex]; ostr << "new[] " << VariableNames[s.VarIndex];
break; break;
case STATEMENT::DELETE: case STATEMENT::DELETE:
std::cout << "delete " << VariableNames[s.VarIndex]; ostr << "delete " << VariableNames[s.VarIndex];
break; break;
case STATEMENT::DELETEARRAY: case STATEMENT::DELETEARRAY:
std::cout << "delete[] " << VariableNames[s.VarIndex]; ostr << "delete[] " << VariableNames[s.VarIndex];
break; break;
case STATEMENT::USE: case STATEMENT::USE:
std::cout << "use " << VariableNames[s.VarIndex]; ostr << "use " << VariableNames[s.VarIndex];
break; break;
case STATEMENT::LOOP: case STATEMENT::LOOP:
std::cout << "loop"; ostr << "loop";
break; break;
case STATEMENT::ENDLOOP: case STATEMENT::ENDLOOP:
std::cout << "endloop"; ostr << "endloop";
break; break;
case STATEMENT::SWITCH: case STATEMENT::SWITCH:
std::cout << "switch"; ostr << "switch";
break; break;
case STATEMENT::ENDSWITCH: case STATEMENT::ENDSWITCH:
std::cout << "endswitch"; ostr << "endswitch";
break; break;
case STATEMENT::IF: case STATEMENT::IF:
std::cout << "if"; ostr << "if";
break; break;
case STATEMENT::ELSEIF: case STATEMENT::ELSEIF:
std::cout << "elseif"; ostr << "elseif";
break; break;
case STATEMENT::ELSE: case STATEMENT::ELSE:
std::cout << "else"; ostr << "else";
break; break;
case STATEMENT::ENDIF: case STATEMENT::ENDIF:
std::cout << "endif"; ostr << "endif";
break; break;
case STATEMENT::RETURN: case STATEMENT::RETURN:
std::cout << "return " << VariableNames[s.VarIndex]; ostr << "return " << VariableNames[s.VarIndex];
break; break;
case STATEMENT::CONTINUE: case STATEMENT::CONTINUE:
std::cout << "continue"; ostr << "continue";
break; break;
case STATEMENT::BREAK: case STATEMENT::BREAK:
std::cout << "break"; ostr << "break";
break; break;
default: default:
std::cout << "ERROR. Unknown code!!"; ostr << "ERROR. Unknown code!!";
break; break;
} }
std::cout << "\n"; ostr << "\n";
}
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -32,6 +32,8 @@ extern std::list<STATEMENT> Statements;
void CreateStatementList(); void CreateStatementList();
void OutputStatementList( std::ostream &ostr );
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif #endif
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -1,11 +0,0 @@
void f()
{
a = 1;
b[2] = 3;
c[4][5].min = 6;
d.min = 7;
}

View File

@ -1,6 +0,0 @@
4 : {
5 : assign a
6 : assign b[2]
7 : assign c[4][5].min
8 : assign d.min
9 : }

View File

@ -1,14 +0,0 @@
void f()
{
int a;
int b = 2, c, *d = NULL;
int e = g(p1,p2);
char str[10];
return a;
delete a;
}

View File

@ -1,16 +0,0 @@
4 : {
5 : decl a
6 : decl b
6 : assign b
6 : decl c
6 : decl d
6 : assign d
6 : use NULL
7 : decl e
7 : assign e
7 : use p1
7 : use p2
8 : decl str
10 : return a
11 : delete a
12 : }

View File

@ -1,17 +0,0 @@
void f()
{
if (ab)
{
}
else if (cd)
{
}
else
{
}
}

View File

@ -1,16 +0,0 @@
4 : {
5 : if
5 : use ab
5 : endif
6 : {
8 : }
9 : elseif
9 : use cd
9 : endif
10 : {
12 : }
13 : else
13 : endif
14 : {
16 : }
17 : }

View File

@ -1,13 +0,0 @@
void f()
{
for (int i = 0; i < j; i++)
{
if (condition)
continue;
break;
}
}

View File

@ -1,16 +0,0 @@
4 : {
5 : loop
5 : assign i
5 : use i
5 : use i
5 : use j
5 : use i
5 : endloop
6 : {
7 : if
7 : use condition
8 : continue
7 : endif
10 : break
11 : }
12 : }

View File

@ -1,10 +0,0 @@
void f()
{
a = new char[10];
fred = new Fred;
fred = new Fred();
}

View File

@ -1,7 +0,0 @@
4 : {
5 : new[] a
5 : use char[10]
6 : new fred
6 : use Fred
7 : new fred
8 : }

View File

@ -1,8 +0,0 @@
void f()
{
a = b;
c = func(d,e);
}

View File

@ -1,7 +0,0 @@
3 : {
4 : assign a
4 : use b
5 : assign c
5 : use d
5 : use e
6 : }

176
tests.cpp
View File

@ -4,6 +4,7 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "tokenize.h" // <- Tokenizer #include "tokenize.h" // <- Tokenizer
#include "Statements.h"
#include "CommonCheck.h" #include "CommonCheck.h"
#include "CheckBufferOverrun.h" #include "CheckBufferOverrun.h"
@ -20,6 +21,7 @@ bool CheckCodingStyle = false;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static unsigned int FailCount, SuccessCount; static unsigned int FailCount, SuccessCount;
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static void internal_statementlist();
static void buffer_overrun(); static void buffer_overrun();
static void constructors(); static void constructors();
static void operator_eq(); static void operator_eq();
@ -31,6 +33,7 @@ static void memleak_in_class();
int main() int main()
{ {
Files.push_back( std::string("test.cpp") ); Files.push_back( std::string("test.cpp") );
internal_statementlist();
buffer_overrun(); buffer_overrun();
constructors(); constructors();
operator_eq(); operator_eq();
@ -80,6 +83,179 @@ static void check(void (chk)(),
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static void statementlist()
{
CreateStatementList();
OutputStatementList( errout );
}
static void internal_statementlist()
{
const char code1[] = "void f()\n"
"{\n"
" a = 1;\n"
" b[2] = 3;\n"
" c[4][5].min = 6;\n"
" d.min = 7;\n"
"}\n";
const char sl1[] = "{\n"
"assign a\n"
"assign b[2]\n"
"assign c[4][5].min\n"
"assign d.min\n"
"}\n";
check( statementlist, __LINE__, code1, sl1 );
const char code2[] = "void f()\n"
"{\n"
" int a;\n"
" int b = 2, c, *d = NULL;\n"
" int e = g(p1,p2);\n"
" char str[10];\n"
" return a;\n"
" delete a;\n"
"}\n";
const char sl2[] = "{\n"
"decl a\n"
"decl b\n"
"assign b\n"
"decl c\n"
"decl d\n"
"assign d\n"
"use NULL\n"
"decl e\n"
"assign e\n"
"use p1\n"
"use p2\n"
"decl str\n"
"return a\n"
"delete a\n"
"}\n";
check( statementlist, __LINE__, code2, sl2 );
const char code3[] = "void f()\n"
"{\n"
" if (ab)\n"
" {\n"
" }\n"
" else if (cd)\n"
" {\n"
" }\n"
" else\n"
" {\n"
" }\n"
"}\n";
const char sl3[] = "{\n"
"if\n"
"use ab\n"
"endif\n"
"{\n"
"}\n"
"elseif\n"
"use cd\n"
"endif\n"
"{\n"
"}\n"
"else\n"
"endif\n"
"{\n"
"}\n"
"}\n";
check( statementlist, __LINE__, code3, sl3 );
const char code4[] = "void f()\n"
"{\n"
" for (int i = 0; i < j; i++)\n"
" {\n"
" if (condition)\n"
" continue;\n"
" break;\n"
" }\n"
"}\n";
const char sl4[] = "{\n"
"loop\n"
"assign i\n"
"use i\n"
"use i\n"
"use j\n"
"use i\n"
"endloop\n"
"{\n"
"if\n"
"use condition\n"
"continue\n"
"endif\n"
"break\n"
"}\n"
"}\n";
check( statementlist, __LINE__, code4, sl4 );
/* TODO: This should work
const char code5[] = "void f()\n"
"{\n"
" a = new char[10];\n"
" fred = new Fred;\n"
" fred = new Fred();\n"
"}\n";
const char sl5[] = "{\n"
"new[] a\n"
"use char[10]\n"
"new fred\n"
"use Fred\n"
"new fred\n"
"}\n";
check( statementlist, __LINE__, code5, sl5 );
*/
const char code6[] = "void f()\n"
"{\n"
" a = b;\n"
" c = func(d,e);\n"
"}\n";
const char sl6[] = "{\n"
"assign a\n"
"use b\n"
"assign c\n"
"use d\n"
"use e\n"
"}\n";
check( statementlist, __LINE__, code6, sl6 );
}
//---------------------------------------------------------------------------
static void buffer_overrun() static void buffer_overrun()
{ {
// test1: numeric array index // test1: numeric array index