Derives tests binary search java class binsearch this

  • No School
  • AA 1
  • 10

This preview shows page 4 - 7 out of 10 pages.

Derives Tests Binary search (Java) class BinSearch { // This is an encapsulation of a binary search function that takes an array of // ordered objects and a key and returns an object with 2 attributes namely // index - the value of the array index // found - a boolean indicating whether or not the key is in the array // An object is returned because it is not possible in Java to pass basic types by // reference to a function and so return two values // the key is -1 if the element is not found public static void search ( int key, int [] elemArray, Result r ) { int bottom = 0 ; int top = elemArray.length - 1 ; int mid ; r.found = false ; r.index = -1 ; while ( bottom <= top ) { mid = (top + bottom) / 2 ; if (elemArray [mid] == key) { r.index = mid ; r.found = true ; return ; } // if part else { if (elemArray [mid] < key) bottom = mid + 1 ; else top = mid - 1 ; } } //while loop } // search } //BinSearch ©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 20 Slide 24 Pre-conditions satisfied, key element in array Pre-conditions satisfied, key element not in array Pre-conditions unsatisfied, key element in array Pre-conditions unsatisfied, key element not in array Input array has a single value Input array has an even number of values Input array has an odd number of values Binary search - equiv. partitions
Image of page 4

Subscribe to view the full document.

©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 20 Slide 25 Binary search equiv. partitions Mid-point Elements < Mid Elements > Mid Equivalence class boundaries ©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 20 Slide 26 Binary search - test cases ©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 20 Slide 27 Path testing The objective of path testing is to ensure that the set of test cases is such that each path through the program is executed at least once The starting point for path testing is a program flow graph that shows nodes representing program decisions and arcs representing the flow of control Statements with conditions are therefore nodes in the flow graph ©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 20 Slide 28 Describes the program control flow. Each branch is shown as a separate path and loops are shown by arrows looping back to the loop condition node Used as a basis for computing the cyclomatic complexity Cyclomatic complexity = Number of edges - Number of nodes +2 Program flow graphs ©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 20 Slide 29 The number of tests to test all control statements equals the cyclomatic complexity Cyclomatic complexity equals number of conditions in a program Useful if used with care. Does not imply adequacy of testing. Although all paths are executed, all combinations of paths are not executed Cyclomatic complexity Binary search flow graph 1 2 3 4 6 5 7 while bottom <= top if (elemArray [mid] == key (if (elemArray [mid]< key 8 9 bottom > top
Image of page 5