Basis path testing creates a test case for each linearly independent code path. It guarantees complete branch coverage but not complete path coverage.

The number of basis paths is the same as the cyclomatic complexity of the code (i.e., function or method). Cyclomatic complexity is a metric that captures the structural complexity of source code by measuring the number of linearly independent paths through the code. Industry studies show that the higher the cyclomatic complexity, the higher the probability of errors:

Very high cyclomatic numbers of more than 50 imply the application cannot be tested, while even higher numbers of more than 75 imply that every change may trigger a “bad fix.” (Hartz et al. 1996)

Consider the following code that computes the average (i.e., arithmetic mean) of 100 or fewer numbers:

PROCEDURE average;
  -- This procedure computes the average of 100 or fewer
  -- numbers that lie between bounding values; it also
  -- computes the sum and the total number valid.

  INTERFACE RETURNS average, total.input, total.valid;
  INTERFACE ACCEPTS value, minimum, maximum;

  TYPE value[1:100] IS SCALAR ARRAY;
  TYPE average, total.input, total.valid;
       minimum, maximum, sum IS SCALAR;
  TYPE i IS INTEGER;

  i = 1;
  total.input = total.valid = 0;
  sum = 0;

  DO WHILE value[i] <> -999 AND total.input < 100
     increment total.input by 1;

     IF value[i] >= minimum AND value[i] <= maximum
         THEN increment total.valid by 1;
                  sum = sum + value[i];
         ELSE skip
     ENDIF

     increment i by 1;
  ENDDO

  IF total.valid > 0
     THEN average = sum / total.valid;
     ELSE average = -999;
  ENDIF
END average

Answer the following questions based on the prior code (solutions):

  1. Draw a control flow graph (CFG) for the prior code.

    CFG for arithmetic mean

  2. Identify the basis paths in the graph.

    • 1–2–10–11–13
    • 1–2–10–12–13
    • 1–2–3–10–11–13
    • 1–2–3–4–5–8–9–2–…
    • 1–2–3–4–5–6–8–9–2–…
    • 1–2–3–4–5–6–7–8–9–2–…