140 likes | 235 Views
CS4311 Spring 2011 Unit Testing Dr. Guoqiang Hu Department of Computer Science UTEP. Statement Coverage Every statement gets executed at least once Every node in the CFG gets visited at least once Statement coverage is the most basic (weakest) coverage. 1. 6. 1. 6.
E N D
CS4311 Spring 2011 Unit Testing Dr. GuoqiangHuDepartment of Computer ScienceUTEP
Statement Coverage • Every statement gets executed at least once • Every node in the CFG gets visited at least once Statement coverage is the most basic (weakest) coverage. 1 6 1 6 Groups of 2 in 3 minutes: The number of paths needed for statement coverage? 2 7 2 7 3 8 3 8 4 9 4 9 4 1 5 5
Branch Coverage Also known as decision/edge coverage, stronger than statement coverage: • Every decision is made true and false at least once each • Every edge in a CFG of the program gets traversed at least once 1 6 1 6 Groups of 2 in 2 minutes: The number of paths needed for branch coverage? 2 7 2 7 3 8 3 8 4 9 4 9 5 2 5 5
Multiple Condition Coverage Every condition in a decision (a boolean expression) is made true and false by every possible combination. For example: (x and y) • x = true, y = true • x= false, y= true • x = true, y= false • x = false, y = false One way to determine the number of paths is to break the compound conditional into atomic conditionals when writing CFG
Condition Coverage: “AND Condition” Example 1 1. read (a,b); 2. if (a == 0 && b == 0) then { 3. c a + b } 4. else c a * b paths: 1,2A,2B,3,J 1,2A,2B,4,J 1,2A,4,J 2A 2B 4 3 Join
Condition Coverage: “OR Condition” Example 1. read (a,b); 2. if (a == 0 || b == 0) then { 3. c a + b; 4. while( c < 100) 5. c a + b;} Paths: 1,2A,3,4,J 1,2A,2B,3,4,5,4,J 1,2A,2B,J 1 2A 3 2B 4 5 Join
Path Coverage • A path is a sequence of statements from beginning to end • A path is sequence of branches from beginning to end • A path is a sequence of edges from beginning to end Every distinct path through code is executed at least once 1 6 1 6 Groups of 2 in 2 minutes: The number of paths needed for path coverage? 2 7 2 7 3 8 3 8 4 9 4 9 5 16 5 5
Path Coverage: Exercise 1,2,3 1. read (x) 2. read (z) 3. if x 0 then begin 4. y x * z; 5. x z end 6. else print ‘Invalid’ 7. if y > 1 then 8. print y 9. else print ‘Invalid’ Test Paths: 4 1, 2, 3, 4, 5, J1, 7, 8, J2 1, 2, 3, 4, 5, J1, 7, 9, J2 1, 2, 3, 6, J1, 7, 8, J2, 1, 2, 3, 6, J1, 7, 9, J2 Groups of 2 in 2 minutes: Find the number of paths, and the paths, needed for path coverage 4,5 6 Join1 7 8 9 Join2 Normally, it is not possible to calculate the total number of paths.
Path Coverage: Linearly Independent Paths A linearly independent path is an end-to-end path that introduces at least one new set of process statements or a new condition. Consider the following encoding: • Label each edge in the CFG with a positive integer • Describe a path using a vector where each element of the vector is the number of times the edge at that index is traversed For example: The path ABD (or, p13) can be written as: [1, 0, 1, 0] A 1 2 Let’s further consider: • Paths can be linearly combined by operations like: addition, subtraction, or scalar multiplication of path vectors • All the path vectors form a vector space • In this vector space, a basis set of vectors can be found, such that: in this basis set, each member cannot be derived from the other members; but, all the rest of vectors in the space can be derived from this basis set B C 4 3 D
Path Coverage: Cyclomatic Complexity Number • Software metric for the logical complexity of a program • Defines the number of independent paths in the basis set of a program • In a CFG, For E Edges and N Nodes, V(G) = E – N + 2 3,4 3,4 1 1 5 5 6 2 Join Join 3 N=3 E=2 E-N+2= 2-3+2= 1=V(G) N=1 E=0 E-N+2= 0-1+2= 1=V(G) N=4 E=4 E-N+2= 4-4+2= 2=V(G) N=3 E=3 E-N+2= 3-3+2= 2=V(G)
Path Coverage: Cyclomatic Complexity Number Exercise 1: Groups of 2 in 3 minutes: Find the cyclomatic complexity number and the independent paths 1 2,3 6 4,5 V(G) = 11 – 9 + 2 = 4 8 7 Independent paths: 1-2-3-6-7-9-10-1-11 1-2-3-6-8-9-10-1-11 1-2-3-4-5-10-1-11 1-11 9 10 11
Path Coverage: Cyclomatic Complexity Number 1,2,3 1. read (x) 2. read (z) 3. if x 0 then begin 4. y x * z; 5. x z end 6. else print ‘Invalid’ 7. if y > 1 then 8. print y 9. else print ‘Invalid’ Cyclomatic complexity: 3 Independent paths: 1, 2, 3, 4, 5, J1, 7, 8, J2 1, 2, 3, 4, 5, J1, 7, 9, J2 1, 2, 3, 6, J1, 7, 8, J2 Exercise 2 Groups of 2 in 3 minutes: Find the cyclomatic complexity number and the independent paths 4,5 6 Join1 7 8 9 Join2
Data Define-Use (D-U Paths) Coverage Every path from every definition of every variable to every use (reference) of that definition is exercised at least once. For example: Def: x, z Use: x 1,2,3 1. read (x) 2. read (z) 3. if x 0 then begin 4. y x * z; 5. x z end 6. else print ‘Invalid’ 7. if y > 1 then 8. print y 9. else print ‘Invalid’ D-U paths: 1, 2, 3, 4, 5, J1, 7, 8, J2 Def: y, x Use: x, z 4,5 6 Use: none Join1 7 Use: y 8 Use: y 9 Use: none Join2