Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Software validation: testing IIIPath Testing

Testing code coverage (path testing)


A white-box testing technique (!) Goal: Ensure every path in the program
I.e., there exists a test case for every possible path in the program flow graph

Generating test cases for path testing

Technique: Generate a test case for every possible (acyclic) path in the program flow graph

CE202 Software Engineering, 2011-12 1 Dr Amnon H. Eden, School of Computer Science and Electronic Engineering, University of Essex 2

Code coverage II
Program flow graph:
Nodes: Blocks of statements Edges: Potential transitions
Branching: Whenever there is more than one transition from the same node
If statements For/while statements Switch statements Dynamically-bound method calls (in O-O programs)

Example: path testing of Binary Search

public static int bin_search(int key, int[] elemArray)

{ // 1 // 2 // // // // 3 4 5 6

// Return index if found, -1 otherwise


int bottom = 0, top = elemArray.length 1, mid; while (bottom <= top) { mid = (top + bottom) / 2; if (elemArray [mid] == key) return mid ; else if (elemArray[mid] < key) bottom = mid + 1 ; else // elemArray[mid] > key top = mid - 1 ; } //while loop return -1; } // bin_search

// 7 // 8

Example: BinSearch.search flow graph


1

Example: BinSearch.search flow graph (Cont.)


Independent paths:
1, 2, 8, 9 1, 2, 3, 8, 9

bottom > top

while bottom <= top 2 bottom <= top

1, 2, 3, 4, 6, 7 (,2) 1, 2, 3, 4, 5, 7 (,2)

elemArray [mid] == key

T
4

3
elemArray [mid] != key

F
5 elemArray [mid]< key 6

Test cases should be derived so that all of these paths are executed A dynamic program analyser may be used to check that paths have been executed
6

7 9

Testing for Loops


What is the flow graph of linear_search?
public static int linear_search(int key, int[] elemArray) {

linear_search Call Graph


Coverage paths to test: 1, 2, 3 1, 2, 4, 5 1, 2, 4, 2, 4, 5
False

1
i=0

2
elemArray [i] < key

// Return index if found, -1 otherwise


for (int i=0; i<elemArray.length; i++) if (elemArray [i] == key) // Found it! return i ; return -1; // Not found } // linear_search

True

False

4
elemArray [i] == key

3
return -1

True

5
return i 7 8

Code-coverage of loops
Conclusion: there are (practically) infinitely-many possible paths within each loop. Question: Which paths should we test? Answer: Only two
While loop: Zero and one iterations For loop: One and two iterations

You might also like