McCabe's criterion example

Example 1

Graph

McCabe example.svg


Test requirements

The cyclomatic complexity of the graph is 7. So, seven test requirements, thus seven complete paths, must be devised for the graph.

Path 1: ABDEGKMQS McCabe example - Path 1.svg

Path 2: ACDEGKMQS McCabe example - Path 2.svg

Path 3: ABDFILORS McCabe example - Path 3.svg

Path 4: ABDEHKMQS McCabe example - Path 4.svg

Path 5: ABDEGKNQS McCabe example - Path 5.svg

Path 6: ACDFJLORS McCabe example - Path 6.svg

Path 7: ACDFILPRS McCabe example - Path 7.svg


Example 2

<source lang="java"> boolean evaluateBuySell (TickerSymbol ts) { s1; s2; s3; if (c1) {s4; s5; s6;} else {s7; s8;} while (c2) {

  s9; 
  s10; 
  switch (c3) { 
     case-A: 
        s20; 
        s21; 
        s22; 
        break; // End of Case-A 
     case-B: 
        s30; 
        s31; 
        if (c4) { 
           s32; 
           s33; 
           s34; 
        } 
        else { 
           s35; 
        } 
        break; // End of Case-B 
     case-C: 
        s40; 
        s41; 
        break; // End of Case-C 
     case-D: 
        s50; 
        break; // End of Case-D 
     } // End Switch 
     s60; 
     s61; 
     s62; 
     if (c5) {s70; s71; } 
     s80; 
     s81; 

} // End While s90; s91; s92; return result; </source>


Graph

McCabe example 2.svg McCabe example 2 (with letters).svg


Test requirements

  • Cyclomatic Complexity Calculation:

<source lang="c"> C = edges - nodes + 2 C = 22-16+2 C = 8 </source>

Basis paths:

  1. ABDP
  2. ACDP
  3. ABDEFGMODP
  4. ABDEFHKMODP
  5. ABDEFIMODP
  6. ABDEFJMODP
  7. ABDEFHLMODP
  8. ABDEFIMNODP

Test cases

Test case c1 c2 c3 c4 c5
1 False False N/A N/A N/A
2 True False N/A N/A N/A
3 False True A N/A False
4 False True B False False
5 False True C N/A False
6 False True D N/A False
7 False True B True False
8 False True C N/A True

Example 3

Source code

<source lang="java"> if (c1) {

  while (c2) { 
     if (c3) { 
        s1; s2; 
        if (c4) s3; 
        else s4; 
        break; // Skip to end of while
     }
     else 
        if (c5) { s5; } 
        else { 
           s6; 
           s7; 
           break;
        } 
  } // End of while 

} // End of if s8; if (c6)

  s9; 

s10; s11; </source>


Graph

McCabe example 3.jpg