Knowledge Base Nr: 00327 teststubb.cpp - http://www.swe-kaiser.de

Downloads:

c/cpp: Stubben für Modultest
verschiedene möglichkeiten sourcecode zu testzwecken zu stubben

  

################# while-schleifen testen

//////////////// code.cpp:
void func1()
{
while(1) //endlosschleife
{
...
}
}

void func2()
{
int i=0;
while (i < 5) //wird 5 mal durchlaufen
{
...
i++;
}
}

//////////////// stub.h:
#define while(v) while(TC_CompWhile(v))

extern bool TC_CompWhile(bool condition);

//////////////// test.cpp:
int tc_CompWhile_break;

bool TC_CompWhile(bool condition)
{
if (0 == tc_CompWhile_break) //timeout
return false;

tc_CompWhile_break--;

return condition; //originalbedingung
}

bool testfunc()
{
//endlsschleife testen
tc_CompWhile_break = 100;
func1();
if (tc_CompWhile_break != 0)
return false;

//wileschleife bedingung testen
tc_CompWhile_break = 100;
func2();
if (tc_CompWhile_break != 100-5)
return false;

retutn true;
}


################# lokale funktionen stubben

//////////////// code.cpp:
#ifdef XTEST_MODUL
#include "stub.h"
#endif

int globvar = 33;

#if defined(XTEST_MODUL) && defined(func2)
void Tc_func2(int p1, int p2)
#else
void func2(int p1, int p2)
#endif
{
...
globvar = 77;
}

void func1()
{
func2(11, 33);
}

//////////////// stub.h:
#define func2 func2


//////////////// test.cpp:

extern int globvar;

bool func2_called;
int func2_p1;
int func2_p2;

void func2(int p1, int p2)
{
func2_called = true;
func2_p1 = p1;
func2_p2 = p2;
}

bool testfunc()
{
tc_Glob_LockSharedResource_b = FALSE;

//variable testen
if (globvar != 33)
return false;

//testen ob func1 func2 mit den richtigen parametern aufruft
func2_called = false;
func1();
if (func2_called != true)
return false;
if (func2_p1 != 11)
return false;
if (func2_p2 != 33)
return false;

//originalfunktion testen
Tc_func2();
if (globvar != 77)
return false;

return true;
}