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

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

View File

@ -32,6 +32,8 @@ extern std::list<STATEMENT> Statements;
void CreateStatementList();
void OutputStatementList( std::ostream &ostr );
//---------------------------------------------------------------------------
#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 "Statements.h"
#include "CommonCheck.h"
#include "CheckBufferOverrun.h"
@ -20,6 +21,7 @@ bool CheckCodingStyle = false;
//---------------------------------------------------------------------------
static unsigned int FailCount, SuccessCount;
//---------------------------------------------------------------------------
static void internal_statementlist();
static void buffer_overrun();
static void constructors();
static void operator_eq();
@ -31,6 +33,7 @@ static void memleak_in_class();
int main()
{
Files.push_back( std::string("test.cpp") );
internal_statementlist();
buffer_overrun();
constructors();
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()
{
// test1: numeric array index