Sub topic 10
Picking Inputs Worth Tracing
This chapter assumes you can build a trace table and follow a loop iteration by iteration, as covered in the earlier chapters. A trace only finds the errors that its input actually triggers. If you trace FindMax with [3, 7, 2, 9] and get 9, you have shown that the algorithm works for that one array. Choosing inputs on purpose is how a ten-minute trace finds real bugs.
Why typical inputs hide bugs
A "typical" input usually has several elements in no special order with positive values. Most code paths behave well on it. Bugs tend to sit at the boundaries: the first or last iteration, a loop that runs zero times, or a starting value that happens to work only for certain data.
Consider this flawed version of FindMax:
FUNCTION FindMaxBuggy(A, n)
max ← 0
FOR i ← 1 TO n DO
IF A[i] > max THEN
max ← A[i]
END IF
END FOR
RETURN max
END FUNCTIONTracing [3, 7, 2, 9] returns 9, which is correct. Tracing [-4, -1, -8] returns 0, a value that is not in the array. The bug is the assumption that 0 is smaller than every element, and only a negative input exposes it.
Categories of inputs to consider
Work through these categories and keep the ones that matter for your algorithm.
| Category | Example for FindMax | What it tests |
|---|---|---|
| Smallest valid size | `[5]`, n = 1 | A loop that runs zero times |
| Empty input | n = 0 | Whether the precondition is stated and enforced |
| Target at first position | `[9, 3, 7]` | Correct initialization |
| Target at last position | `[3, 7, 9]` | The final iteration is not skipped |
| All equal values | `[4, 4, 4]` | `>` versus `≥` in comparisons |
| Negative or zero values | `[-4, -1, -8]` | Assumptions hidden in initial values |
| Already sorted / reverse sorted | `[1, 2, 3]`, `[3, 2, 1]` | Best and worst paths through branches |
For FindMax with n = 1, the loop FOR i ← 2 TO 1 runs zero times, and the function returns A[1]. That is correct, but you only know it after checking the case.
Boundaries of loop counters
For loops that depend on a number n, trace n = 0, n = 1, and a small general value such as n = 3. Take the summing loop from the desk-checking chapter, which uses WHILE i < n. With n = 1 it returns 0 when the expected answer is 1. That exposes the off-by-one error in a single iteration, faster than tracing n = 3.
Keep the set small
Each input should test something the others do not. Five well-chosen arrays of length 1 to 4 find more errors than one long array, and they are much faster to trace by hand. After you fix a bug, retrace every input in the set, because a fix at one boundary can break another.
Exercise
A procedure Find(A, n, x) should return the index of the first element of the 1-based array A that equals x, or 0 if x is not present. List the inputs you would trace and what each one tests.
Show answer
n = 0 (empty array, must return 0); n = 1 with x present and with x absent (the loop runs once); x at position 1 (tests early exit or initialization); x at position n (tests that the last iteration runs); x absent from a longer array (tests the "not found" return value); x appearing twice, for example A = [2, 5, 5], x = 5 (must return 2, not 3, which checks that the procedure returns the first match).