Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Flow Graphs : The if Statement 1 if (x < y) { y = 0; x = x + 1; } else { x = y; } 4 1 23 x >= yx < y x = y y = 0 x = x + 1 if (x < y) { y = 0;

Similar presentations


Presentation on theme: "Control Flow Graphs : The if Statement 1 if (x < y) { y = 0; x = x + 1; } else { x = y; } 4 1 23 x >= yx < y x = y y = 0 x = x + 1 if (x < y) { y = 0;"— Presentation transcript:

1 Control Flow Graphs : The if Statement 1 if (x < y) { y = 0; x = x + 1; } else { x = y; } 4 1 23 x >= yx < y x = y y = 0 x = x + 1 if (x < y) { y = 0; x = x + 1; } 3 1 2 x >= y x < y y = 0 x = x + 1

2 Control Flow Graphs : The if-Return Statement 2 if (x < y) { return; } print (x); return; 3 1 2 x >= y x < y return print (x) return

3 Control Flow Graphs : while and for Loops 3 x = 0; while (x < y) { y = f (x, y); x = x + 1; } 1 x = 0 x >= yx < y x >= yx < y 43 y =f(x,y) x = x + 1 2 for (x = 0; x < y; x++) { y = f (x, y); } 2 35 x >= yx < y y = f (x, y) 4 1 x = x + 1

4 Control Flow Graphs : do Loop, break and continue 4 x = 0; do { y = f (x, y); x = x + 1; } while (x < y); println (y) 1 x = 0 2 x >= y x < y y = f (x, y) x = x+1 3 x = 0; while (x < y) { y = f (x, y); if (y == 0) { break; } else if y < 0) { y = y*2; continue; } x = x + 1; } print (y); 1 x = 0 83 x = x + 1 break y < 0 24567 y =f(x,y) y == 0 y = y*2 continue

5 Tests with Data Flow Criteria  Data flow criteria require tests that tour subpaths from specific definitions of variables to specific uses  Nodes where a variable is assigned a value are called definitions (or defs)  Nodes where the value of a variable is accessed are called uses.

6 The Application of Graph Criteria: Source Code  It is usually defined with the control flow graph (CFG)  Node coverage is used to execute every statement  Edge coverage is used to execute every branch  Loops are used to execute looping structures such as for loops, while loops, etc.  Data flow coverage is the extended form of control flow graph (CFG) with defs and uses  defs are statements that assign values to variables  uses are statements that use variables

7 Tests with Data Flow Criteria  Data flow criteria require tests that tour subpaths from specific definitions of variables to specific uses  Nodes where a variable is assigned a value are called definitions (or defs)  Nodes where the value of a variable is accessed are called uses.

8 8 Data Flow Coverage for Source  def : A location where a value is stored into memory  x appears on the left side of an assignment ( x = 44 ;  x is an actual parameter in a call and the method changes its value  x is a formal parameter of a method (implicit def when method starts)  x is an input to a program  use : A location where variable’s value is accessed  x appears on the right side of an assignment  x appears in a conditional test  x is an actual parameter to a method  x is an output of the program  x is an output of a method in a return statement  If a def and a use appear on the same node, then it is only a DU-pair if the def occurs after the use and the node is in a loop

9  A definition d for a variable x reaches a use u if there is a path from d to u that has no other definitions of x (def-clear).  The all-uses (AU) criterion requires tests to tour at least one subpath from each definition to each reachable use. // EXAMPLE: return index of the last element in x that equals y. // if y is not in x, return -1. public int findLast (int []x, int y) { for (int i = x.length-1; i>=0; i--) { if (x[i] == y) return i; } return -1;

10 Annotated Control Graph  Nodes 4 and 6 are final nodes, corresponding to the return statements.  Node 2 is introduced to capture the for loop; it has no executable statements.  DU (def-use) pairs are shown as a variable name followed by the def node, then the use node. use(3)= def (5) = { i } use (5) = { i } Def -Use Pairs = { (1, 1,x), (1,3,x), (1,3,y), (1, 2,i), (1, 3,i), (1,5,i), (1,6,i), (5, 2,i), (5, 3,i), ( 5, 6,i), (5, 5,i)}

11 public static void computeStats (int [ ] numbers) { int length = numbers.length; double med, var, sd, mean, sum, varsum; sum = 0; for (int i = 0; i < length; i++) { sum += numbers [ i ]; } med = numbers [ length / 2]; mean = sum / (double) length; varsum = 0; for (int i = 0; i < length; i++) { varsum = varsum + ((numbers [ I ] - mean) * (numbers [ I ] - mean)); } var = varsum / ( length - 1.0 ); sd = Math.sqrt ( var ); System.out.println ("length: " + length); System.out.println ("mean: " + mean); System.out.println ("median: " + med); System.out.println ("variance: " + var); System.out.println ( "standard deviation: " + sd); } 11 i = 0 1 2 3 5 4 6 7 8 i >= length i < length i++


Download ppt "Control Flow Graphs : The if Statement 1 if (x < y) { y = 0; x = x + 1; } else { x = y; } 4 1 23 x >= yx < y x = y y = 0 x = x + 1 if (x < y) { y = 0;"

Similar presentations


Ads by Google