1 Ivan Marsic Rutgers University LECTURE 15: Software Complexity Metrics.

Slides:



Advertisements
Similar presentations
Chapter 14 Software Testing Techniques - Testing fundamentals - White-box testing - Black-box testing - Object-oriented testing methods (Source: Pressman,
Advertisements

Path Analysis Why path analysis for test case design?
CYCLOMATIC COMPLEXITY 1. Invented by Thomas McCabe (1974) to measure the complexity of a program ’ s conditional logic Cyclomatic complexity of graph.
SOFTWARE TESTING. INTRODUCTION  Software Testing is the process of executing a program or system with the intent of finding errors.  It involves any.
CMSC 345, Version 11/07 SD Vick from S. Mitchell Software Testing.
CS447/ECE453/SE465 Prof. Alencar University of Waterloo 1 CS447/ECE453/SE465 Software Testing Tutorial Winter 2008 Based on the tutorials by Prof. Kontogiannis,
Creator: ACSession No: 13 Slide No: 1Reviewer: SS CSE300Advanced Software EngineeringFebruary 2006 Testing - Techniques CSE300 Advanced Software Engineering.
Software engineering for real-time systems
SIM5102 SOFTWARE EVALUATION
Chapter 8: Path Testing Csci 565.
IMSE Week 18 White Box or Structural Testing Reading:Sommerville (4th edition) ch 22 orPressman (4th edition) ch 16.
BASIS PATH TESTING ● By Tom McCabe ● McCabe, T., "A Software Complexity Measure," IEEE Trans. Software Engineering, vol. SE-2, December 1976, pp
Chapter 18 Testing Conventional Applications
Unit Testing CS 414 – Software Engineering I Don Bagert Rose-Hulman Institute of Technology January 16, 2003.
SOFTWARE TESTING WHITE BOX TESTING 1. GLASS BOX/WHITE BOX TESTING 2.
Path testing Path testing is a “design structural testing” in that it is based on detailed design & the source code of the program to be tested. The methodology.
A Complexity Measure THOMAS J. McCABE Presented by Sarochapol Rattanasopinswat.
Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Lecture 9 Instructor Paulo Alencar.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 20 Slide 1 Defect testing l Testing programs to establish the presence of system defects.
CS4311 Spring 2011 Unit Testing Dr. Guoqiang Hu Department of Computer Science UTEP.
Software Testing The process of operating a system or component under specified conditions, observing and recording the results, and making an evaluation.
Reference Paulo Alencar, University of Waterloo Frank Tsui, Southern Polytechnic State University.
Agenda Introduction Overview of White-box testing Basis path testing
1 Software Testing. 2 Path Testing 3 Structural Testing Also known as glass box, structural, clear box and white box testing. A software testing technique.
Lec 06 Path Testing Part II - 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 06 – Path Testing Part II 9/16/
White-box Testing.
Software Testing and Reliability Southern Methodist University CSE 7314.
1 Program Testing (Lecture 14) Prof. R. Mall Dept. of CSE, IIT, Kharagpur.
BASIS PATH TESTING.
Software Testing White Box Testing. Agenda What is White Box Testing Correctness Tests and Path Coverage Correctness Tests and Line Coverage McCabe Cyclomatic.
CSCI 115 Chapter 8 Topics in Graph Theory. CSCI 115 §8.1 Graphs.
Chapter 8 Path Testing. Path testing Path testing is a “design structural testing” in that it is based on detailed design & the source code of the program.
White Box Testing Arun Lakhotia University of Southwestern Louisiana P.O. Box Lafayette, LA 70504, USA
Overview Structural Testing Introduction – General Concepts
1 Ivan Marsic Rutgers University LECTURE 15: Software Complexity Metrics.
Theory and Practice of Software Testing
SOFTWARE TESTING. Introduction Software Testing is the process of executing a program or system with the intent of finding errors. It involves any activity.
Agent program is the one part(class)of Othello program. How many test cases do you have to test? Reversi [Othello]
White Box Testing by : Andika Bayu H.
Cyclomatic complexity (or conditional complexity) is a software metric (measurement). Its gives the number of indepented paths through strongly connected.
Cyclomatic Complexity Philippe CHARMAN Last update:
White-Box Testing Techniques I Prepared by Stephen M. Thebaut, Ph.D. University of Florida Software Testing and Verification Lecture 7.
CS223: Software Engineering Lecture 21: Unit Testing Metric.
Dynamic White-Box Testing What is code coverage? What are the different types of code coverage? How to derive test cases from control flows?
SOFTWARE TESTING LECTURE 9. OBSERVATIONS ABOUT TESTING “ Testing is the process of executing a program with the intention of finding errors. ” – Myers.
Software Testing. SE, Testing, Hans van Vliet, © Nasty question  Suppose you are being asked to lead the team to test the software that controls.
TQS - Teste e Qualidade de Software (Software Testing and Quality) Test Case Design: White Box Testing João Pascoal Faria.
Software TestIng White box testing.
TQS - Teste e Qualidade de Software (Software Testing and Quality) Test Case Design: White Box Testing João Pascoal Faria.
BASIS PATH TESTING.
TQS - Teste e Qualidade de Software (Software Testing and Quality) Test Case Design: White Box Testing João Pascoal Faria.
Software Testing.
White-Box Testing Pfleeger, S. Software Engineering Theory and Practice 2nd Edition. Prentice Hall, Ghezzi, C. et al., Fundamentals of Software Engineering.
Software Testing.
Cyclomatic complexity
Software Engineering (CSI 321)
Dijkstra’s shortest path Algorithm
When is testing sufficient?
Structural testing, Path Testing
Types of Testing Visit to more Learning Resources.
White Box Testing.
Chapter 9 Path Testing–Part 1
Cyclomatic Complexity
Software Testing (Lecture 11-a)
Cyclomatic Complexity
LECTURE 15: Software Complexity Metrics
Cyclomatic Complexity
White-Box Testing Techniques I
Unit III – Chapter 3 Path Testing.
Presentation transcript:

1 Ivan Marsic Rutgers University LECTURE 15: Software Complexity Metrics

2 Topics Measuring Software Complexity Cyclomatic Complexity

3 Measuring Software Complexity Software complexity is difficult to operationalize complexity so that it can be measured Computational complexity measure big O (or big Oh), O(n) –Measures software complexity from the machine’s viewpoint in terms of how the size of the input data affects an algorithm’s usage of computational resources (usually running time or memory) Complexity measure in software engineering should measure complexity from the viewpoint of human developers

4 Cyclomatic Complexity Invented by Thomas McCabe (1974) to measure the complexity of a program’s conditional logic –Counts the number of decisions in the program, under the assumption that decisions are difficult for people –Makes assumptions about decision-counting rules and linear dependence of the total count to complexity Cyclomatic complexity of graph G equals #edges - #nodes + 2 –V(G) = e – n + 2 Also corresponds to the number of linearly independent paths in a program (described later)

5 Converting Code to Graph if expression1 then statement2 else statement3 end if statement4 switch expr1 case 1: statement2 case 2: statm3 case 3: statm4 end switch statm5 (a) (b) do statement1 while expr2 end do statement3 (c) CODEFLOWCHARTGRAPH TF expr1 ? statm4 statm2statm expr1 ? statm5 statm3 statm2statm4 n1 n2n3 n4 n1 n2n4 n5 n3 T F expr2 ? statm3 statm1 n1 n2 n3 For a strongly connected graph: Create a virtual edge to connect the END node to the BEGIN node

6 Paths in Graphs (1) A graph is strongly connected if for any two nodes x, y there is a path from x to y and vice versa A path is represented as an n-element vector where n is the number of edges The i-th position in the vector is the number of occurrences of edge i in the path

7 Example Paths if expression1 then statement2 end if do statement3 while expr4 end do if expression5 then statement6 end if statement7 n1 n3 n2 n4 n5 n7 n6 e1 e2 e3 e4 e5 e6 e7 e8 e9 Paths: P1 = e1, e2, e4, e6, e7, e8 P2 = e1, e2, e4, e5, e4, e6, e7, e8 P3 = e3, e4, e6, e7, e8, e10 P4 = e6, e7, e8, e10, e3, e4 P5 = e1, e2, e4, e6, e9, e10 P6 = e4, e5 P7 = e3, e4, e6, e9, e10 P8 = e1, e2, e4, e5, e4, e6, e9, e10 1, 1, 0, 1, 0, 1, 1, 1, 0, 0 1, 1, 0, 2, 1, 1, 1, 1, 0, 0 0, 0, 1, 1, 0, 1, 1, 1, 0, 1 1, 1, 0, 1, 0, 1, 0, 0, 1, 1 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 0, 0, 1, 1, 0, 1, 0, 0, 1, 1 1, 1, 0, 2, 1, 1, 0, 0, 1, 1 e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 NOTE: A path does not need to start in node n1 and does not need to begin and end at the same node. E.g.,  Path P4 starts (and ends) at node n4  Path P1 starts at node n1 and ends at node n7

8 Paths in Graphs (2) A circuit is a path that begins and ends at the same node –e.g., P3 = begins and ends at node n1 –P6 = begins and ends at node n3 A cycle is a circuit with no node (other than the starting node) included more than once

9 Example Circuits & Cycles if expression1 then statement2 end if do statement3 while expr4 end do if expression5 then statement6 end if statement7 n1 n3 n2 n4 n5 n7 n6 e1 e2 e3 e4 e5 e6 e7 e8 e9 Cycles: P3 = e3, e4, e6, e7, e8, e10 P5 = e1, e2, e4, e6, e9, e10 P6 = e4, e5 P7 = e3, e4, e6, e9, 10 e10 Circuits: P3 = e3, e4, e6, e7, e8, e10 P4 = e6, e7, e8, e10, e3, e4 P5 = e1, e2, e4, e6, e9, e10 P6 = e4, e5 P7 = e3, e4, e6, e9, 10 P8 = e1, e2, e4, e5, e4, e6, e9, e10 P9 = e3, e4, e5, e4, e6, e9, 10 0, 0, 1, 1, 0, 1, 1, 1, 0, 1 1, 1, 0, 1, 0, 1, 0, 0, 1, 1 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 0, 0, 1, 1, 0, 1, 0, 0, 1, 1 1, 1, 0, 2, 1, 1, 0, 0, 1, 1 0, 0, 1, 2, 1, 1, 0, 0, 1, 1 e1 e2 e3 e4 e5 e6 e7 e8 e9 e10

10 Linearly Independent Paths A path p is said to be a linear combination of paths p 1, …, p n if there are integers a 1, …, a n such that p =  a i  p i A set of paths is linearly independent if no path in the set is a linear combination of any other paths in the set A basis set of cycles is a maximal linearly independent set of cycles –In a graph with e edges and n nodes, the basis has e  n + 1 cycles Every path is a linear combination of basis cycles

11 Baseline method for finding the basis set of cycles Start at the source node Follow the leftmost path until the sink node is reached Repeatedly retrace this path from the source node, but change decisions at every node with out-degree ≥2, starting with the decision node lowest in the path T.J. McCabe & A.H. Watson, Structured Testing: A Testing Methodology Using the Cyclomatic Complexity Metric, NIST Special Publication , 1996.

12 Linearly Independent Paths if expression1 then statement2 end if do statement3 while expr4 end do if expression5 then statement6 end if statement7 n1 n3 n2 n4 n5 n7 n6 e1 e2 e3 e4 e5 e6 e7 e8 e9 V(G) = e – n + 2 = 9 – = 4 EXAMPLE #2: 2  P3 – P5 + P6 = 2  P3 { 0, 0, 2, 2, 0, 2, 2, 2, 0, 2} – P5 { 1, 1, 0, 1, 0, 1, 0, 0, 1, 1} ___ {-1,-1, 2, 1, 0, 1, 2, 2,-1, 1} + P6 { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0} = P? {-1,-1, 2, 2, 1, 1, 2, 2,-1, 1} EXAMPLE #1: P5 + P6 = P8 P5 {1, 1, 0, 1, 0, 1, 0, 0, 1, 1} + P6 {0, 0, 0, 1, 1, 0, 0, 0, 0, 0} = P8 {1, 1, 0, 2, 1, 1, 0, 0, 1, 1} Cycles: P3 = e3, e4, e6, e7, e8, e10 P5 = e1, e2, e4, e6, e9, e10 P6 = e4, e5 P7 = e3, e4, e6, e9, 10 e10 Paths: P1 = e1, e2, e4, e6, e7, e8 P2 = e1, e2, e4, e5, e4, e6, e7, e8 P3 = e3, e4, e6, e7, e8, e10 P4 = e6, e7, e8, e10, e3, e4 P5 = e1, e2, e4, e6, e9, e10 P6 = e4, e5 P7 = e3, e4, e6, e9, 10 P8 = e1, e2, e4, e5, e4, e6, e9, e10 1, 1, 0, 1, 0, 1, 1, 1, 0, 0 1, 1, 0, 2, 1, 1, 1, 1, 0, 0 0, 0, 1, 1, 0, 1, 1, 1, 0, 1 1, 1, 0, 1, 0, 1, 0, 0, 1, 1 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 0, 0, 1, 1, 0, 1, 0, 0, 1, 1 1, 1, 0, 2, 1, 1, 0, 0, 1, 1 e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 Or, if we count e10, then e – n + 1 = 10 – = 4

13 Unit Testing : Path Coverage –Finds the number of distinct paths through the program to be traversed at least once Minimum number of tests necessary to cover all edges is equal to the number of independent paths through the control-flow graph

14 Issues (1) = CC = Cyclomatic complexity (CC) remains the same for a linear sequence of statements regardless of the sequence length —insensitive to complexity contributed by the multitude of statements Single statement:Two (or more) statements: statement stat-1 stat-2

15 Issues (2) = CC = T F expr ? TF expr ? Optional action versus alternative choices — the latter is psychologically more difficult Optional action:Alternative choices:

16 Issues (3) = CC = T F A ? D T A || D ? Simple condition:Compound condition: if (A) then D; if (A OR B) then D; BUT, compound condition can be written as a nested IF: if (A) then D; else if (B) then D; F T A ? D T F B ? D F

17 Issues (4) = CC = Counting a switch statement: —as a single decision proposed by W. J. Hansen, “Measurement of program complexity by the pair (cyclomatic number, operator count),” SIGPLAN Notices, vol.13, no.3, pp.29-33, March —as log 2 (N) relationship proposed by V. Basili and R. Reiter, “Evaluating automatable measures for software development,” Proceedings of the IEEE Workshop on Quantitative Software Models for Reliability, Complexity and Cost, pp , October Switch/Case statement:N  1 predicates: T expr=1 ? statm1 T expr=2 ? statm2 F 2 1N expr ? statm2 statm1statmN T expr=N ? statmN F

18 Issues (5) TF expr1 ? TF expr2 ? TF expr2 ? TF expr1 ? = CC = But, it is known that people find nested decisions more difficult … Two sequential decisions:Two nested decisions:

19 CC for Modular Programs (1) V = e – n + 2 = 12 – = 3 Adding a sequential node does not change CC: n1 n2 n7 n3 n4n5 n6 n0 n8 n1 n2 n7 n3 n4n5 n6 n0 n8 n9' n9"

20 CC for Modular Programs (2) V = e – n + 2 = 12 – = 3 Intuitive expectation: Modularization should not increase complexity n1 n2 n7 n3 n4n5 n6 n0 n8 n1 n2 n7 n3 n4n5 n6 n0 n8 n9 CALL A A: V = e – n + 2p = 10 – x 2 = 4

21 Alternative CC Measures Given p connected components of a graph: –V(G) = e – n + 2p(1) –V LI (G) = e – n + p + 1(2) –Eq. (2) is known as linearly-independent cyclomatic complexity –V LI does not change when program is modularized into p modules

22 CC for Modular Programs (3) V = e – n + 2 = 12 – = 3 V LI = e – n + p + 1 = 12 – = 3 Intuitive expectation: Modularization should not increase complexity n1 n2 n7 n3 n4n5 n6 n0 n8 n1 n2 n7 n3 n4n5 n6 n0 n8 n9 CALL A A: V = e – n + 2p = 10 – x 2 = 4 V LI = e – n + p + 1 = 10 – = 3

23 Practical SW Quality Issues No program module should exceed a cyclomatic complexity of 10 P. Jorgensen, Software Testing: A Craftman’s Approach, 2nd Edition, CRC Press Inc., pp , Software refactorings are aimed at reducing the complexity of a program’s conditional logic Refactoring: Improving the Design of Existing Code by Martin Fowler, et al. Addison-Wesley Professional, 1999.