cft and dft

Upload: manav-das

Post on 04-Apr-2018

240 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 CFT And DFT

    1/39

  • 7/31/2019 CFT And DFT

    2/39

    2

    1. Introduction

    -CFT and DFT help with interactions along the execution pathand interactions among data items in execution, respectively.

    -Control Flow Testing (CFT) and Data Flow Testing (DFT) are

    structural (i.e. White-box) techniques suitable for small units of source code, e.g., a function.

    -CFT and DFT use as test models control flow graphs (CFG) ,

    which give an abstract representation of the code.

    -CFG is also used as main model for code coverage analysis .

  • 7/31/2019 CFT And DFT

    3/39

  • 7/31/2019 CFT And DFT

    4/39

  • 7/31/2019 CFT And DFT

    5/39

    5

    -A control flow graph (CFG) describes code segments and their

    sequencing in a program. It is a directed graph in which:A node corresponds to a code segment; nodes are labeled using letters or numbers.

    An edge corresponds to a conditional transfer of control betweencode segments; edges are represented as arrows.

    Representation

    Decision node : more than one outgoing edge

    Junction node : more than one incoming edge

    Processing node : one incoming and one outgoing edge

    Entry node: the entry point of a method, represented with a nodewith no inbound edges.

    Exit node: the exit point of a method, represented with a nodewith no outbound edges.

  • 7/31/2019 CFT And DFT

    6/39

    6

    Flow graphs for canonical loop structures

    For Loop

    buffer= new char[nchar + 1];

    for (n=0; n< nchars; ++n) {

    buffer[n] = newChar;}

    A

    B, D

    C

    A

    B

    C

    D

  • 7/31/2019 CFT And DFT

    7/39

  • 7/31/2019 CFT And DFT

    8/39

    8

    Path Expressions

    -A path corresponds to a sequence of segments connected by arrows.A path is denoted by the nodes that comprise the path; e.g., path ABH .Loops are represented by segments within parentheses, followed by anasterisk to show that this group may iterate from zero to n times,

    e.g., path A(BCDEFG)*BH .

    A

    B

    C

    F

    E

    D

    H

    G

    Examples of Entry/Exit Paths:

    -ABH-ABCH-A(BCDEFG)*BH

    -A(BCDEG)*BH-A(BCDG)*BH-A(BCDG)*BCH

    -An entry-exit path is a path starting with the entry node and endingwith the exit node

  • 7/31/2019 CFT And DFT

    9/39

    9

    Compound Predicates

    -Modeled separately by specifying a node for each individual predicate involved.-All true-false combinations in a compound predicate should beanalyzed, and the effects of short circuit boolean evaluation shouldbe made explicit.

    For instance: C++, Java, and Objective-C use C semantics- short circuit boolean

    evaluation is automatically applied to all multiple-condition boolean expressions.A

    B

    C

    F

    E

    DH

    G

    Example:

    while ((result ==false) && (i < MAX)) {

    if ((userids[i]==uid) && (passwords[i] = pwd ))

    result=true;

    ++i; }

  • 7/31/2019 CFT And DFT

    10/39

    10

    Cond1

    R

    Default

    Cond3

    Cond2

    Q

    P

    Nextsegment

    if cond1 P else if cond2 Q else if cond3 R else Default

    -Modeled by specifying a separate node for each predicate , each conditional action , and the default action .

    Case and Multiple-if Statements

  • 7/31/2019 CFT And DFT

    11/39

    11

    -Modeled by specifying a node for the switch expression, and a separate node for each action .

    Computedgoto

    P

    Q

    R

    Nextsegment break;

    break;

    break;

    switch (e) { case 1: P; case 2: Q; case 3: R;

    }

    Switch Statements

  • 7/31/2019 CFT And DFT

    12/39

    12

    3. Test Coverage Analysis

    -A code coverage model calls out the parts of an implementation thatmust be exercised to satisfy an implementation-based test model.

    Coverage, as a metric, is the percentage of these parts exercised by a test suite.

    -Test coverage attempts to address questions about when to stop testing , or the amount of testing that is enough for a given program.

    -Ideal testing is to explore exhaustively the entire test domain ,which in general is impossible .Some code may never be executed due to the possibility of missing test cases.Effectiveness of test suites can be established only by knowing what code is,or isn't executed.

    Test Coverage Overview

  • 7/31/2019 CFT And DFT

    13/39

  • 7/31/2019 CFT And DFT

    14/39

  • 7/31/2019 CFT And DFT

    15/39

  • 7/31/2019 CFT And DFT

    16/39

    16

    Branch Coverage-Achieved when every branch from a node is executed at least onceby a test suite.

    Also known as decision coverage or all-edge coverage .

    Improve on statement coverage by requiring that each branch be taken at leastonce. Hence each outcome of a predicate expression is exercised at least once

    (i.e., true and false).

    However, it is limited by the fact that it treats a compound predicate as a single statement : branch coverage may be achieved without exercising all of theconditions

  • 7/31/2019 CFT And DFT

    17/39

  • 7/31/2019 CFT And DFT

    18/39

    18

    Condition coverage/Multiple condition coverage

    -Condition coverage requires that each condition be evaluated as true

    and false at least once.

    -Multiple-condition coverage requires that all true-false combinationsof simple conditions be exercised at least once.There are at most 2 n true-false combinations for a compound predicate with nsimple conditions.

    Multiple-condition coverage ensures that all statements, branches, and conditionsare covered, but not all paths .

    Reaching all condition combinations may be impossible due to short circuit

    evaluation.Mutually exclusive conditions spanning several predicate expressions maypreclude certain combinations.

  • 7/31/2019 CFT And DFT

    19/39

    19

    Basis-Path Model

    Cyclomatic Complexity Metric

    Where e and n stand for the number of edges and the number of nodes incorresponding CFG, respectively.

    -Defined as the number of edges minus the number of nodes plus 2:

    C = e n + 2

    -Also called McCabe complexity metricEvaluate the complexity of algorithms involved in a method.Give a count of the minimum number of test cases needed to test a methodcomprehensively.Low C value means reduced testing, and better understandability.

    -Example:CC= e- n + 2 = 8 7 + 2 = 3

  • 7/31/2019 CFT And DFT

    20/39

  • 7/31/2019 CFT And DFT

    21/39

    21

    The Basis-Path Test Model (ctd.)Example 1: Example 2:

    C=8-7+2=3Branch coverage can bebe achieved in two paths

    C=24-19+2=7Branch coverage canbe achieved in fourpaths

    According to how thepaths are selected, neitherbranch coverage norstatement coverage may beachieved using C paths.

  • 7/31/2019 CFT And DFT

    22/39

  • 7/31/2019 CFT And DFT

    23/39

    23

    Class BinSearch {

    public static void search(int key, int[] elemArray, Result r) {int bottom = 0;int top = elemArray.length 1;int mid;

    r.found = false; r.index=-1;

    while (bottom

  • 7/31/2019 CFT And DFT

    24/39

  • 7/31/2019 CFT And DFT

    25/39

  • 7/31/2019 CFT And DFT

    26/39

  • 7/31/2019 CFT And DFT

    27/39

    27

    Test Coverage, in Practice

    -In practice, the three basic criteria most commonly used arestatement coverage, branch coverage and condition coverage .

    It has been suggested that the combination of these three criteria canachieve 80-90 % or more coverage needs in most cases.

    -Important to note that test coverage is not enough by itself:100% test coverage cannot guarantee achieving error-free software.

    -However, test coverage in combination with appropriate teststrategies can mitigate the impact of uncovered code duringsoftware testing.

    Help the tester find some rational points at which to stop testing.

  • 7/31/2019 CFT And DFT

    28/39

  • 7/31/2019 CFT And DFT

    29/39

    29

    Data Dependency Analysis

    -Three basic types of data operations:1. Data definition : through data creation, initialization, assignment, all explicitly or

    through side effects such as shared memory locations, mailboxes, read/writeparameters etc.

    Abbreviated as D-operation or just D.

    Key characteristic: destructive operation; whatever is stored in the data item isdestroyed after this operation.

    2. Undefinition or Kill : occurs when the value assigned to a variable and correspondingmemory location become unbound.

    3. Data use in computation or in predicate, referred to as C-use or P-use , respectively.Both of these types of uses are collectively called U-operation, or just U.

    Main characteristic: non-destructive operation; the value of the data item remainsthe same after this operation.

    Data Operations

  • 7/31/2019 CFT And DFT

    30/39

    30

    Data Operations (ctd.)

    Example:

  • 7/31/2019 CFT And DFT

    31/39

  • 7/31/2019 CFT And DFT

    32/39

    32

    Data Flow Graph

    -Drawn with the objective of identifying data definitions and their

    uses.

    -Consist of a directed graph constructed as follows:A sequence of definitions and c-uses is associated with each node of the graph.

    A set of p-uses is associated with each edge of the graph.The entry node has a definition of each parameter and each nonlocal variable which

    occurs in the subprogram.

    The exit node has an undefinition of each local variable.

  • 7/31/2019 CFT And DFT

    33/39

    33

    Data Flow Graph (ctd.) Example:

  • 7/31/2019 CFT And DFT

    34/39

  • 7/31/2019 CFT And DFT

    35/39

    35

    Data Flow Coverage

    -Popular data flow coverage criteria include:

    All definitions (all-defs) criterion:-Require that for each definition of a variable x, the set of paths executed by thetest set T contains a def-clear path from the definition to at least one c-use orone p-use of x.

    -i.e., all definitions get used.

    All-c-uses criterion:-Require that for each definition of a variable x, and each c-use of x reachablefrom the definition, there is a def-clear path from the definition to the c-use.-i.e., all computations affected by each definition are exercised.

    All-p-uses criterion:-Require that for each definition of a variable x, and each p-use of x reachablefrom the definition, there is a def-clear path from the definition to the p-use.-i.e., all branches affected by each definition are exercised

  • 7/31/2019 CFT And DFT

    36/39

    36

    Data Flow Coverage (ctd.)

    -Data flow coverage criteria (ctd.):

    All uses criterion:-Require that for each definition of a variable x, and for each use of x reachablefrom the definition, there is def-clear path from the definition to the use node.

    -i.e., all uses of a definition should be covered.-Stronger than all-definitions criterion.

    All definition-use-paths criterion:- Require that for each definition of a variable x, and for each use of x reachable

    from the definition, all def-clear paths from the definition to the use node mustbe covered, such that each path is loop free, or contains at most one loop.

    -i.e., coverage of all possible definition-use paths that either are cycle-freeor have only simple cycles (i.e. one loop).

    -Stronger than all of the above criteria.

  • 7/31/2019 CFT And DFT

    37/39

  • 7/31/2019 CFT And DFT

    38/39

    38

    1 i = get_a_number_from_console();2 j = get_a_number_from_console();3 if (ab) {9 i=8;10 j=6;11 } else {12 printf("%d\n",j);13 }14 if (bb a

  • 7/31/2019 CFT And DFT

    39/39