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:
- Compute the program graph.
- Calculate the cyclomatic complexity.
- Select a basis set of paths.
- 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.
- From a graph,
1.1. counting regions
1.2. counting edges, nodes, exit points
- From source code with one exit point, counting conditionals
- 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