CS447/ECE453/SE465 Software Testing Tutorial Winter 2008 Based on the tutorials by Prof. Kontogiannis, Winter 2006 Presented by Jiang Wu j32wu@cs.uwaterloo.ca CS447/ECE453/SE465 Prof. Alencar University of Waterloo 1 Outline of the 12 tutorials Graphs Tutorial 1 Example Problems Tutorial 2 Project Kickoff Tutorial 3 Functional Testing Tutorial 4, 5 Structural Testing Tutorial 6, 7, 8 Integration Testing Tutorial 9, 10 System testing Tutorial 11 Software reliability Tutorial 12 CS447/ECE453/SE465 Prof. Alencar Graphs Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 3 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 4 Undirected Graphs A graph G = (V, E) is composed of a finite (and non-empty) set V = {n1, n2,…,nm} of nodes, and a set E = {e1, e2,…,ep} of edges, where each edge ek = {ni, nj} for some nodes ni, nj V Nodes also called vertices Edges also called arcs Nodes also called endpoints of an arc ECE 453 University of Waterloo 5 Undirected Graphs n1 e1 e3 n5 n2 e5 e4 n4 e2 V = {n1, n2, n3, n4, n5, n6, n7} n3 e6 E = {e1, e2, e3, e4, e5, e6, e7} = {(n1, n2), (n2, n3), (n1, n5), (n4, n5), (n2, n4), (n3, n4), (n4, n6)} edge {n3, n4} e7 nodes n6 n7 Nodes are usually program statements Edges for flow of control, define/use relationships etc. ECE 453 University of Waterloo 6 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 7 Degree of a Node The degree of a node in a graph is the number of edges that have that node as an endpoint. We write deg(n) for the degree of node n. n1 e1 e3 n5 n2 e5 e4 e2 n3 e6 n4 e7 n6 ECE 453 n7 deg(n1) = 2 deg(n2) = 3 deg(n3) = 2 deg(n4) = 4 deg(n5) = 2 deg(n6) = 1 deg(n7) = 0 University of Waterloo 8 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 9 Incidence Matrices Alternative representation of graphs The incidence matrix of a graph G = (V, E) with m nodes and n edges is an m x n matrix, where the element in row i, column j is 1 if and only if node i is an endpoint of edge j, otherwise the element is 0 ECE 453 University of Waterloo 10 Incidence Matrices n1 e1 e3 n5 n2 e5 e4 e2 n3 e6 n4 e7 n6 n7 Sum of a row: degree Sum of a column: 2 ECE 453 University of Waterloo 11 Incidence Matrices The sum of the entries in any column is 2 It is a form of integrity checking The row sum is the degree of the node ECE 453 When zero, the node is isolated (unreachable code, objects never used) University of Waterloo 12 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 13 Adjacency Matrices Useful supplement to the incidence matrix The adjacency matrix of a graph G=(V, E) with m nodes is an m x m matrix, where the element in row i, column j is a 1 if and only if there is an edge between node i and node j, otherwise the element is 0 Symmetric: element (i, j) = element (j, i) The row sum is the degree of the node ECE 453 University of Waterloo 14 Adjacency Matrices n1 e1 e3 n5 n2 e5 e4 e2 n3 e6 n4 e7 n6 n7 Symmetric ECE 453 University of Waterloo 15 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 16 Paths A path is a sequence of edges such that, for any adjacent pair of edges ei, ej in the sequence, the edges share a common (node) endpoint Sequences of nodes or sequences of edges ECE 453 University of Waterloo 17 Paths n1 e1 e3 n5 n2 e5 e4 e2 n3 e6 n4 e7 n6 ECE 453 n7 University of Waterloo 18 Paths If we multiply an adjacency matrix with itself the product will show the number of all two edge paths of the graph. If we further multiply the product with the original adjacency matrix the new product will show the number of all three edge paths of the graph 2 = ECE 453 University of Waterloo 19 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 20 Connectedness Nodes ni and nj are connected if and only if they are in the same path Connectedness is an equivalence relation on the node set of a graph Reflexive: every node is in a path of length 0 with itself Symmetric: if (ni, nj) path, then (nj, ni) path Transitive: if (ni, nj) path and (nj, nw) path, then (ni, nw) path ECE 453 University of Waterloo 21 Connectedness Equivalence relations induce a partition Connectedness defines a partition on the node set of a graph A component of a graph is a maximal set of connected nodes n1 e1 e3 n5 n2 e5 e4 e2 n3 e6 {n1, n2, n3, n4, n5, n6}, {n7} n4 e7 n6 ECE 453 n7 {n1, n2} is not a component University of Waterloo 22 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 23 Condensation Graphs Given a graph G = (V, E), its condensation graph is formed by replacing each component by a condensing node n1 e1 e3 n5 n2 e5 e4 e2 n3 e6 → n4 e7 n6 n7 S1 = {n1, n2, n3, n4, n5, n6} S2 = {n7} ECE 453 University of Waterloo 24 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 25 Cyclomatic Number The cyclomatic number of a graph G is given by V(G) = e – n + p, e: number of edges, n: number of nodes, p: number of components Number of distinct regions in a graph n1 e1 e3 n5 n2 e5 e4 e2 n3 e6 Cyclomatic Number: 7 – 7 + 2 = 2 n4 e7 n6 ECE 453 n7 University of Waterloo 26 Cyclomatic Number 2 ECE 453 0 0 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 28 Directed Graphs A directed graph (digraph) D = (V, E) consists of a finite set V = {n1, n2,…,nm} of nodes, and a set E = {e1, e2,…,ep} of edges, where each edge ek = <ni, nj> is an ordered pair of nodes ni, nj V ni: initial (start) node, nj terminal (finish) node Software concepts represented with directed graphs: sequential behavior, timeordered events, messages etc. ECE 453 University of Waterloo 29 Directed Graphs e1 n1 e3 n5 n2 e5 e4 e2 n3 e6 V = {n1, n2, n3, n4, n5, n6, n7} E = {e1, e2, e3, e4, e5, e6, e7} = {<n1, n2>, <n2, n3>, <n5, n2>, <n4, n5>, <n2, n4>, <n3, n4>, <n4, n6>} n4 e7 n6 ECE 453 n7 University of Waterloo 30 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 31 Indegrees and Outdegrees The indegree of a node in a directed graph is the number of distinct edges that have the node as a terminal node (indeg(n)) The outdegree of a node in a directed graph is the number of distinct edges that have the node as an start point (outdeg(n)) deg(n) = indeg(n) + outdeg(n) ECE 453 University of Waterloo 32 Indegrees and Outdegrees e1 n1 e3 n5 n2 e5 e4 e2 n3 e6 n4 e7 n6 ECE 453 n7 indeg(n1) = 0 indeg(n2) = 2 indeg(n3) = 1 indeg(n4) = 2 indeg(n5) = 1 indeg(n6) = 1 indeg(n7) = 0 University of Waterloo outdeg(n1) = 1 outdeg(n2) = 2 outdeg(n3) = 1 outdeg(n4) = 2 outdeg(n5) = 1 outdeg(n6) = 0 outdeg(n7) = 0 33 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 34 Types of Nodes Source Node: indegree = 0 Sink Node: outdegree = 0 Transfer Node: indegree ≠ 0 && outdegree ≠ 0 Isolated Node: indegree = 0 && outdegree = 0 External Entities: Source and Sink Nodes ECE 453 University of Waterloo 35 Types of Nodes e1 n1 e3 n5 n2 e5 e4 e2 n3 e6 Sink Nodes: n6 Transfer Nodes: n2, n3, n4, n5 n4 e7 n6 ECE 453 Source Nodes: n1 Isolated Nodes: n7 n7 University of Waterloo 36 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 37 Adjacency Matrix of a Directed Graph The adjacency matrix of a directed graph D = (V, E) with m nodes is a m x m matrix A=(a(i, j)), where a(i, j) is a 1 if and only if there is an edge from node i and node j, otherwise the element is 0 Not necessarily symmetric Row sum = outdegree of node Column sum = indegree of node ECE 453 University of Waterloo 38 Adjacency Matrix of a Directed Graph e1 n1 e3 n5 n2 e5 e4 e2 n3 e6 n4 e7 n6 ECE 453 n7 University of Waterloo 39 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 40 Paths and Semi – Paths A (directed) path is a sequence of edges such that, for any adjacent pair of edges ei, ej, in the sequence, the terminal node of the first edge is the initial node of the second edge A cycle is a directed path that begins and ends at the same node A (directed) semi – path is a sequence of edges such that, for at least one adjacent pair of edges ei, ej in the sequence, the initial node of the first edge is the initial node of the second edge or the terminal node of the first edge is the terminal node of the second edge ECE 453 University of Waterloo 41 Paths and Semi – Paths Directed paths also called chains e1 n1 e3 n5 n2 e5 e4 e2 n3 e6 Cycle: n2 → n4 → n5 → n2 n4 Semi – Path: e1 , e3 e7 n6 ECE 453 Path: n1 → n2 → n3 n7 Semi – Path: e2 , e5 University of Waterloo 42 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 43 Reachability Matrix The reachability matrix of a directed graph D = (V, E) with m nodes is a m x m matrix R = (r(i, j)), where r(i, j) is a 1 if and only if there is a path from node i and node j, otherwise the element is 0 ECE 453 University of Waterloo 44 Reachability Matrix e1 n1 e3 n5 n2 e5 e4 e2 n3 e6 n4 e7 n6 ECE 453 n7 University of Waterloo 45 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 46 N – Connectedness Two nodes ni and nj in a directed graph are: 0-connected iff there is no path between ni and nj 1-connected iff there is a semi-path but NO path between ni and nj 2-connected iff there is a path between ni and nj 3-connected iff there is a path from nj to ni and a path from ni to nj ECE 453 University of Waterloo 47 N – Connectedness e1 n1 e3 n5 n2 e5 e4 e2 n3 e6 ECE 453 n1 and n8 are 1-connected n2 and n6 are 2-connected n4 n3 and n5 are 3-connected e7 n8 n1 and n7 are 0-connected n6 n7 University of Waterloo 48 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 49 Strong Components A strong component of a directed graph is a maximal set of 3-connected nodes Practically loops and isolated nodes are removed and replaced by strong components (directed acyclic graphs) ECE 453 University of Waterloo 50 Condensation Graphs e1 n1 e3 n5 n2 e5 e4 e2 n3 n1 e6 → n4 e1 S1 e7 e7 n6 n6 n7 S2 S1 = {n2, n3, n4, n5} S2 = {n7} ECE 453 University of Waterloo 51 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 52 Program Graphs A program graph is a directed graph in which: Traditional Definition: Nodes are program statements, and edges represent flow of control (there is an edge from node i to node j iff the statement corresponding to node j can be executed immediately after the statement corresponding to node i) Improved Definition: Nodes are either entire statements or fragments of a statement, and edges represent flow of control (there is an edge from node i to node j iff the statement or statement fragment corresponding to node j can be executed immediately after the statement or statement fragment corresponding to node i) Primarily used at the unit testing level ECE 453 University of Waterloo 53 Program Graphs ECE 453 University of Waterloo 54 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 55 Finite State Machines A Finite State Machine (FSM) is a directed graph (S, T, Ev, Act), in which S: set of nodes (states) T: set of edges (transitions) Ev, Act: sets of events and actions associated with the transitions in T Primarily used to describe system level behavior ECE 453 University of Waterloo 56 Finite State Machines Example: Lamp Node Transition Event Action ECE 453 University of Waterloo 57 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 58 Petri Nets A Petri net is a bipartite directed graph (P, T, In, Out), in which P (places), T (transitions): disjoint sets of nodes In, Out: sets of edges, where In P x T, and Out T x P Primarily used to describe system level behavior ECE 453 University of Waterloo 59 Petri Nets P = {p1, p2, p3, p4} T = {t1, t2, t3} In = {<p1, t1>, <p1, t2>, <p2, t2>, <p4, t3>} Out = {<t1, p3>, <t2, p4>, <t3, p3>} ECE 453 University of Waterloo 60 Petri Nets A marked Petri net is a 5-tuple (P, T, In, Out, M) in which (P, T, In, Out) is a Petri net and M (marking set) is a set of mappings of places to positive integers The number associated with a place refers to the number of tokens that are said to be “in” the place ECE 453 e.g. number of times a place has been used University of Waterloo 61 Petri Nets P = {p1, p2, p3, p4} T = {t1, t2, t3} In = {<p1, t1>, <p1, t2>, <p2, t2>, <p4, t3>} Out = {<t1, p3>, <t2, p4>, <t3, p3>} M = {2, 0, 1, 3} ECE 453 University of Waterloo 62 Petri Nets A transition in a Petri net is enabled if there is at least one token in each of its input places Enabled Transitions: t1, t3 ECE 453 University of Waterloo 63 Petri Nets When an enabled Petri net transition fires, one token is removed from each of its input places, and one token is added to each of its output places ECE 453 University of Waterloo 64 Petri Nets t3 fired → M = {<2, 0, 1, 3>, <2, 0, 2, 2>} ECE 453 University of Waterloo 65 Petri Nets Conservative Petri nets: total number of tokens never changes Petri net conflicts Transitions t1 and t2 are in conflict with respect to place p1 ECE 453 University of Waterloo 66 Petri Net Example http://www.informatik.unihamburg.de/TGI/PetriNets/introductions/a alst/elevator1.swf ECE 453 Agenda Undirected Graphs Directed Graphs Degree of a Node Incidence Matrices Adjacency Matrices Paths Connectedness Condensation Graphs Cyclomatic Number Indegrees and Outdegrees Types of Nodes Adjacency Matrix of a Directed Graph Paths and Semi – Paths Reachability Matrix N – Connectedness Strong Components Graphs for Testing ECE 453 Program Graphs Finite State Machines Petri Nets University of Waterloo 68 References Software Testing A Craftsman's Approach 2nd edition, Paul C. Jorgensen, CRC Press ECE 453 University of Waterloo 69