-1
i = 0; 
n=4; //N-Number of nodes present in the graph 
while (i<n-1) do 
j = i + 1;
while (j<n) do 
if A[i]<A[j] then 
swap(A[i], A[j]); 
end do;
i=i+1;
end do;

I have to find cyclomatic-complexity for this code and then suggest some white box test cases and black box test cases. But I am having trouble making a CFG for the code.

  • Example seems to be taken live from web? With already present explanation for part of your question. Also, why are you to propose black box testing here if you have the code? Finally, please explain CFG, none of my ideas seem to fit the context. – LAFK 4Monica_banAI_modStrike Feb 09 '14 at 11:38

1 Answers1

1

Browsing for complexity related materials I came upon this question as well as the example tackled by on someone's blog and in someone's book. Seeing as the example is identical...

http://www.guru99.com/cyclomatic-complexity.html

http://books.google.pl/books?id=M-mhFtxaaskC&lpg=PA385&ots=jB8P0avJU7&d&hl=pl&pg=PR1#v=onepage&q&f=false -> go to page 384

Still, hoping to somewhat make the question useful to others.

Algorithm

Taken from http://www.cs.swan.ac.uk/~csmarkus/CS339/dissertations/GregoryL.pdf:

  1. Compute the program graph.
  2. Calculate the cyclomatic complexity.
  3. Select a basis set of paths.
  4. Generate test cases for each of these paths

First two points can be collapsed sometimes, as calculating complexity may not necessarily involve graph building. For basis set of paths however, you'll need to have the graph at least rudimentarily sketched in your mind.

Calculating

To calculate complexity you will need to make a decision how do you go about it.

  1. From a graph, 1.1. counting regions 1.2. counting edges, nodes, exit points
  2. From source code with one exit point, counting conditionals
  3. From source code with many exit points, counting conditionals, subtracting exit points and adding 2.

Complexity equals:

  • amount of regions - this is best seen at graph, and I did one on other SO question so will link there instead of copy/paste
  • Edges - Nodes + 2 * (exit points)
  • Edges - Nodes + (exit points) // provided strongly connected graph
  • (Decision points) + 1 // provided one entry point, one exit point
  • (Decision points) - (exit points) + 2 // many exit points

Decision points = IF, FOR, WHILE... Since `if (a OR/AND b) is equivalent to if a and/or if b, compound conditionals add 2 to complexity, so complexity of such if is 2 (since it's 2 ifs anyway).

Exemplary graphs and links to more reading: https://stackoverflow.com/a/21658235/999165.

Basis path testing - white box testing

McCabe noted that complexity number is a number if linearly independent circuits - paths through code, thus it's also a minimum test cases number. So, you'll need to construct as many test cases as many paths you have. Paths are chosen by going through graph as it were a maze:

Start with path counter at zero and: 1. go left up till sink node (exit point) 2. path counter + 1 3. go left again, at lowest decision point before the sink was reached, choose right instead of left 4. repeat steps 1 - 3 until all decision points are fully exhausted

The nodes you traverse make up your paths.

http://en.wikipedia.org/wiki/Basis_path_testing

Community
  • 1
  • 1
  • http://www.guru99.com/cyclomatic-complexity.html has exactly, what I was looking for. The complexity V(G) has an actual significance/relevance, if one would want to note - "the maximum number of independent paths" within a method/module. Or in other words, for an input to the method, how many control-flows possible (start-to-end). – parasrish Jan 05 '17 at 07:01