You are on page 1of 4

Software Testing Lab 1

White Box Testing


//
// Find the first instance in "string" of each character in "what"
// Return the locations of the first instance of each what[i] in
where[i]
// ignoring the first "start" characters in string
// Return TRUE if any character found
// All strings are NULL-terminated
//

#include <string.h>
#include "findx.h"

int findx( char *string, int start, char *what, int where[] )
{

int i = 0; /* index into what[] */


int len = strlen(string);
int any = 0; /* any character found = FALSE */
int found = 0; /* current character found = FALSE */
int j; /* index into string[] */

char c=what[0];

while (c != '\000') {

found = 0; /* FALSE */
j = start;

do {

if (string[j++] == c) {
found = 1; /* TRUE */
any = 1; /* TRUE */
}

} while ( (!found) && (j < len) );

if (found)
where[i] = j-1;
else
where[i] = len;

c=what[++i];

return any;

1
Consider the program findx.cc on the previous page. Develop the following
tests for this program

1. Statement coverage
2. Branch coverage
3. D-D Path Testing
4. Condition Coverage Testing
5. Decision/Condition Coverage Testing
6. Multiple Condition Coverage Testing
7. Path Testing

Ensure that you document your tests as you develop them:

 Write down the test cases


 Write down the test data

CPPunit Example – Just for your own reference in the future


The following example is of a simple program and CPPunit test suite to test
that program. Type in the code and run it to observe how the tests are
engineered. We will use the program findx.cc again, but it should be saved as
a ‘.cpp’ file. The file findx.h contains

int findx( char *string, int start, char *what, int *where );

The syntax for a CPPunit test is


runtest <SUT> {<SUITE>}
where SUT is the nema of the software under test (including version
number) and {<SUITE>} is the (optional) name of the test suite to run: the
default being all tests. The name of this program is finderTest.cpp

#include <extensions/TestFactoryRegistry.h>
#include <extensions/HelperMacros.h>
#include <TestCase.h>
#include <Exception.h>
#include <Asserter.h>
#include <TestAssert.h>
#include <ui/text/TestRunner.h>

using namespace CppUnit;

#include "stdafx.h"
#include <iostream>

2
#include <stddef.h>
#include <time.h>
#include <string.h>
#include "findx.h"

// first start a test suite that defines the methods to call

class finderTest : public CppUnit::TestFixture {

CPPUNIT_TEST_SUITE( finderTest );
CPPUNIT_TEST( test1 );
CPPUNIT_TEST( test2 );
CPPUNIT_TEST_SUITE_END(); // end the test suite

private:
// put private attributes required for testing here
public:
// put your own test methods here - one per test case or test ID
is usual
// use CPPUNIT_ASSERT() to check results

// Test1 – this test should be passed

void test1()
{
int rv;
int where[6];

rv = findx( "abccde", 0, "c", &where[0] );

CPPUNIT_ASSERT( rv == 1 ); // program resturns true


CPPUNIT_ASSERT( where[0] == 2 ); //char c located at index 2
}

// Test2 - this test should fail

void test2()
{
int rv;
int where[6];

rv = findx( "abccde", 0, "x", &where[0] );

CPPUNIT_ASSERT( rv == 1 );
CPPUNIT_ASSERT( where[0] == 2 );
}
};
// the following line adds this test suite to the test factory
CPPUNIT_TEST_SUITE_REGISTRATION( finderTest );

// main program for CPPunit


int main( int argc, char *argv[] )
{
bool success;

3
TextUi::TestRunner runner;
TestFactoryRegistry &registry = TestFactoryRegistry::getRegistry();

char *testname;
char *sut;
time_t now=time(0);

// run all the tests if no params


if (argc==1) {
sut = "finder";
testname = "finderTest";
}
// print the test info
// default to all tests if none specified

std::cout << "Test log at " << ctime(&now) << std::endl;


std::cout << "Running cppunit tests for " << sut << std::endl;

std::cout << "Test suite selected: " << testname << std::endl;

// add all the unit tests from the registry to this runner

runner.addTest( registry.makeTest() );

// and run the (selected) tests

success = runner.run( testname );

// return 0 on success, and 1 on failure (for make)


if (success)
return 0;
else
return 1;
}
Type in this code and run this example. Then, add some more suitable test
cases, say 3 or 4 more, to augment the testing exercise.

You might also like