Introduction to Software Testing Chapter 2 Sec 2

  • Slides: 27
Download presentation
Introduction to Software Testing Chapter 2, Sec#: 2. 3 Graph Coverage for Source Code

Introduction to Software Testing Chapter 2, Sec#: 2. 3 Graph Coverage for Source Code Paul Ammann & Jeff Offutt http: //www. cs. gmu. edu/~offutt/softw aretest/

Overview • The most common application of graph criteria is the program source code

Overview • The most common application of graph criteria is the program source code • Graph : Usually the control flow graph (CFG) • Node coverage : Execute every statement • Edge coverage : Execute every branch • Loops : Looping structures such as for loops, while loops, etc. • Data flow coverage : Augment (enhance) the CFG – defs are statements that assign values to variables – uses are statements that use (access or read) variables’ values Introduction to Software Testing (Ch 2) © Ammann & Offutt 2

Control Flow Graphs • A CFG models all executions of a method by describing

Control Flow Graphs • A CFG models all executions of a method by describing control structures • Nodes : Statements or sequences of statements (basic blocks) • Edges : Transfers of control • Basic Block : A sequence of statements such that: – if the first statement is executed, all statements in that block will be executed too (no branches inside a basic block) – One entry point and one exit point • CFGs are sometimes annotated with extra information – branch predicates (i. e. x > 0) – defs – uses • Rules for translating statements into graphs … Introduction to Software Testing (Ch 2) © Ammann & Offutt 3

CFG : The if, if-else Statement if (x < y) { y = 0;

CFG : The if, if-else Statement if (x < y) { y = 0; x = x + 1; } else { x = y; } decision node 1 x<y y=0 x=x+1 x >= y 2 3 Junction node 4 if (x < y) { y = 0; x = x + 1; } Introduction to Software Testing (Ch 2) x=y © Ammann & Offutt 1 x<y y=0 x=x+1 2 x >= y 3 4

CFG : The if-return Statement if (x < y) { return; } print (x);

CFG : The if-return Statement if (x < y) { return; } print (x); return; 1 x<y return 2 x >= y 3 print (x) return No edge from node 2 to 3. The return nodes must be distinct. Introduction to Software Testing (Ch 2) © Ammann & Offutt 5

Loops • Loops require “extra” nodes to be added • “extra” nodes: nodes that

Loops • Loops require “extra” nodes to be added • “extra” nodes: nodes that do not represent statements or basic blocks Introduction to Software Testing (Ch 2) © Ammann & Offutt 6

CFG : while Loops x = 0; while (x < y) { y =

CFG : while Loops x = 0; while (x < y) { y = f (x, y); x = x + 1; } x=0 1 dummy node 2 x<y x >= y 3 4 y =f(x, y) x=x+1 Introduction to Software Testing (Ch 2) © Ammann & Offutt

CFG : for Loops implicitly initializes loop x=0 1 2 for (x = 0;

CFG : for Loops implicitly initializes loop x=0 1 2 for (x = 0; x < y; x++) { y = f (x, y); } y = f (x, y) x<y x >= y 3 5 4 x=x+1 implicitly increments loop Introduction to Software Testing (Ch 2) © Ammann & Offutt 8

CFG : do-while Loop x=0 x = 0; do { y = f (x,

CFG : do-while Loop x=0 x = 0; do { y = f (x, y); x = x + 1; } while (x < y); println (y) Introduction to Software Testing (Ch 2) 1 2 x >= y 3 © Ammann & Offutt y = f (x, y) x = x+1 x<y 9

CFG : break and continue x = 0; while (x < y) { y

CFG : break and continue 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 2 x<y 3 x >= y y =f(x, y) y == 0 4 5 y>0 x=x+1 print(y); Introduction to Software Testing (Ch 2) x=0 7 break y<0 6 y = y*2 continue 8 © Ammann & Offutt 10

CFG : The case (switch) Structure read ( c ) ; switch ( c

CFG : The case (switch) Structure read ( c ) ; switch ( c ) { case ‘N’: y = 25; break; case ‘Y’: y = 50; break; default: y = 0; break; } print( y ); Introduction to Software Testing (Ch 2) 1 read ( c ); default c == ‘N’ c == ‘Y’ y = 25; break; 2 y = 50; break; 3 4 y = 0; break; 5 print (y); © Ammann & Offutt 11

Control Flow Graph: Stats Example public static void compute. Stats (int [ ] numbers)

Control Flow Graph: Stats Example public static void compute. Stats (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); Introduction to Software Testing (Ch 2), www. introsoftwaretesting. com © Ammann & Offutt 12 12

Control Flow Graph: Stats Example public static void compute. Stats (int [ ] numbers)

Control Flow Graph: Stats Example public static void compute. Stats (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; 1 2 3 i=0 i >= length varsum = 0; i < length for (int i = 0; i < length; i++) i++ 4 { 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); Introduction to Software Testing (Ch 2), www. introsoftwaretesting. com © Ammann & Offutt 5 i=0 6 i < length i >= length 7 8 i++ 13

Control Flow TRs and Test Paths – EC 1 Edge Coverage 2 A. B.

Control Flow TRs and Test Paths – EC 1 Edge Coverage 2 A. B. C. D. E. F. G. H. I. 3 4 5 6 7 TR ( 1, 2 ) ( 2, 3 ) ( 3, 4 ) ( 3, 5 ) ( 4, 3 ) ( 5, 6 ) ( 6, 7 ) ( 6, 8 ) ( 7, 6 ) Test Path [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ] 8 Introduction to Software Testing (Ch 2), www. introsoftwaretesting. com © Ammann & Offutt 14

Control Flow TRs and Test Paths – EPC 1 Edge-Pair Coverage 2 3 4

Control Flow TRs and Test Paths – EPC 1 Edge-Pair Coverage 2 3 4 5 6 7 Introduction to Software Testing (Ch 2) 8 A. B. C. D. E. F. G. H. I. J. K. L. TR [ 1, 2, 3 ] i. [ 2, 3, 4 ] ii. [ 2, 3, 5 ] iii. [ 3, 4, 3 ] [ 3, 5, 6 ] [ 4, 3, 5 ] TP [ 5, 6, 7 ] [ 5, 6, 8 ] [ 6, 7, 6 ] [ 7, 6, 8 ] [ 4, 3, 4 ] [ 7, 6, 7 ] Test Paths [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ] [ 1, 2, 3, 5, 6, 8 ] [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ] © Ammann & Offutt TRs toured sidetrips

Control Flow TRs and Test Paths – PPC Prime Path Coverage 1 2 3

Control Flow TRs and Test Paths – PPC Prime Path Coverage 1 2 3 4 5 6 7 Introduction to Software Testing (Ch 2) A. B. C. D. E. F. G. H. I. J. TR [ 3, 4, 3 ] [ 4, 3, 4 ] [ 7, 6, 7 ] [ 7, 6, 8 ] [ 6, 7, 6 ] [ 1, 2, 3, 4 ] [ 4, 3, 5, 6, 7 ] [ 4, 3, 5, 6, 8 ] [ 1, 2, 3, 5, 6, 7 ] [ 1, 2, 3, 5, 6, 8 ] Test Paths i. [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ] iii. [ 1, 2, 3, 4, 3, 5, 6, 8 ] iv. [ 1, 2, 3, 5, 6, 7, 6, 8 ] v. [ 1, 2, 3, 5, 6, 8 ] TP TRs toured sidetrips 8 © Ammann & Offutt 16

Data Flow Coverage for Source • def : a location where a value is

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 (pass by reference) – 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 Introduction to Software Testing (Ch 2) © Ammann & Offutt 17

Example Data Flow – Stats Example public static void compute. Stats (int [ ]

Example Data Flow – Stats Example public static void compute. Stats (int [ ] numbers) { int length = numbers. length; double med, var, sd, mean, sum, varsum; sum = 0. 0; for (int i = 0; i < length; i++) { sum += numbers [ i ]; } med = numbers [ length / 2 ]; mean = sum / (double) length; varsum = 0. o; for (int i = 0; i < length; i++) { varsum = varsum + ((numbers [ i ] - mean) * (numbers [ i ] - mean)); } var = varsum / ( length - 1 ); 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); Introduction to Software Testing (Ch 2) © Ammann & Offutt 18

Control Flow Graph: Stats Example ublic static void compute. Stats (int [ ] numbers)

Control Flow Graph: Stats Example ublic static void compute. Stats (int [ ] numbers) 1 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; 2 3 varsum = 0; for (int i = 0; i < length; i++) i++ 4 { 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); Introduction to Software Testing (Ch 2), www. introsoftwaretesting. com © Ammann & Offutt i=0 i >= length i < length 5 i=0 6 i < length i >= length 7 8 i++ 19

Control Flow Graph for Stats 1 ( numbers ) sum = 0 length =

Control Flow Graph for Stats 1 ( numbers ) sum = 0 length = numbers. length 2 i=0 3 i >= length i < length 4 5 sum += numbers [ i ] i++ med = numbers [ length / 2 ] mean = sum / (double) length varsum = 0 i=0 6 i >= length i < length varsum = … i++ 7 8 var = varsum / ( length - 1. 0 ) sd = Math. sqrt ( var ) print (length, mean, med, var, sd) 20

CFG for Stats – With Defs & Uses 1 def (1) = { numbers,

CFG for Stats – With Defs & Uses 1 def (1) = { numbers, sum, length } use(1) = { numbers } 2 def (2) = { i } 3 use (3, 5) = { i, length } use (3, 4) = { i, length } 4 5 def (5) = { med, mean, varsum, i } use (5) = { numbers, length, sum } def (4) = { sum, i } use (4) = { sum, numbers, i } 6 use (6, 8) = { i, length } use (6, 7) = { i, length } 7 def (7) = { varsum, i } use (7) = { varsum, numbers, i, mean } 8 def (8) = { var, sd } use (8) = { varsum, length, mean, med, var, sd } 21

Defs and Uses Tables for Stats Node 1 2 3 4 5 Def Use

Defs and Uses Tables for Stats Node 1 2 3 4 5 Def Use { numbers, sum, length } {i} { numbers } { sum, i } { med, mean, varsum, i } { numbers, i, sum } { numbers, length, sum } 6 7 8 { varsum, i } { var, sd } Introduction to Software Testing (Ch 2) { varsum, numbers, i, mean } { varsum, length, var, mean, med, var, sd } © Ammann & Offutt Edge Use (1, 2) (2, 3) (3, 4) (4, 3) { i, length } (3, 5) { i, length } (5, 6) (6, 7) { i, length } (7, 6) (6, 8) { i, length } 22

du-pairs for Stats Variable du-pairs defs come before uses, do not count as du-pairs

du-pairs for Stats Variable du-pairs defs come before uses, do not count as du-pairs numbers (1, 4) (1, 5) (1, 1) (1, 7) length (1, 5) (1, 8) (1, (3, 4)) (1, (3, 5)) (1, (6, 7)) (1, (6, 8)) med var sd mean sum varsum i Introduction to Software Testing (Ch 2) (5, 8) (8, 8) defs after use in loop, these are valid du-pairs (8, 8) (5, 7) (5, 8) No def-clear path … (1, 4) (1, 5) (4, 4) (4, 5) different scope for i (5, 7) (5, 8) (7, 7) (7, 8) (2, 4) (2, (3, 4)) (2, (3, 5)) (2, 7) (2, (6, 7)) (2, (6, 8)) (4, 4) (4, (3, 4)) (4, (3, 5)) (4, 7) (4, (6, 7)) (4, (6, 8)) (5, 7) (5, (6, 7)) (5, (6, 8)) No path through graph from (7, 7) (7, (6, 7)) (7, (6, 8)) nodes 5 and 7 to 4 or 3 © Ammann & Offutt 23

du-paths for Stats Variable numbers length med var sd sum du-pairs du-paths (1, 4)

du-paths for Stats Variable numbers length med var sd sum du-pairs du-paths (1, 4) (1, 5) (1, 7) [ 1, 2, 3, 4 ] [ 1, 2, 3, 5, 6, 7 ] (1, 5) (1, 8) (1, (3, 4)) (1, (3, 5)) (1, (6, 7)) (1, (6, 8)) [ 1, 2, 3, 5 ] [ 1, 2, 3, 5, 6, 8 ] [ 1, 2, 3, 4 ] [ 1, 2, 3, 5, 6, 7 ] [ 1, 2, 3, 5, 6, 8 ] (5, 8) (8, 8) (1, 4) (1, 5) (4, 4) (4, 5) [ 5, 6, 8 ] No path needed [ 1, 2, 3, 4 ] [ 1, 2, 3, 5 ] [ 4, 3, 4 ] [ 4, 3, 5 ] Introduction to Software Testing (Ch 2) Variable mean du-pairs (5, 7) (5, 8) du-paths [ 5, 6, 7 ] [ 5, 6, 8 ] varsum (5, 7) (5, 8) (7, 7) (7, 8) [ 5, 6, 7 ] [ 5, 6, 8 ] [ 7, 6, 7 ] [ 7, 6, 8 ] i (2, 4) (2, (3, 4)) (2, (3, 5)) (4, 4) (4, (3, 4)) (4, (3, 5)) (5, 7) (5, (6, 7)) (5, (6, 8)) (7, 7) (7, (6, 7)) (7, (6, 8)) [ 2, 3, 4 ] [ 2, 3, 5 ] [ 4, 3, 4 ] [ 4, 3, 5 ] [ 5, 6, 7 ] [ 5, 6, 8 ] [ 7, 6, 7 ] [ 7, 6, 8 ] © Ammann & Offutt 24

du-paths for Stats – No Duplicates There are 38 du-paths for Stats, but only

du-paths for Stats – No Duplicates There are 38 du-paths for Stats, but only 12 unique 1 2 3 4 5 6 7 Introduction to Software Testing (Ch 2) 8 © Ammann & Offutt 25

Test Cases and Test Paths Test Case : numbers = (44) ; length =

Test Cases and Test Paths Test Case : numbers = (44) ; length = 1 Test Path : [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ] du-paths covered (no sidetrips): [ 1, 2, 3, 4 ] [ 4, 3, 5 ] [ 5, 6, 7 ] [ 7, 6, 8 ] The five stars that require at least one iteration of a loop A fault was found Introduction to Software Testing (Ch 2) © Ammann & Offutt 26

Summary • Applying the graph test criteria to control flow graphs is relatively straightforward

Summary • Applying the graph test criteria to control flow graphs is relatively straightforward – Most of the developmental research work was done with CFGs • A few subtle decisions must be made to translate control structures into the graph • Some tools will assign each statement to a unique node – These slides and the book uses basic blocks – Coverage is the same, although the bookkeeping will differ Introduction to Software Testing (Ch 2) © Ammann & Offutt 27