Video lecture
Watch on YouTube(opens in a new tab)Sub topic 10
Does It Solve the Problem? Checking Correctness
This chapter assumes you can read simple Python and that you have already written a specification with a precondition and a postcondition, and traced an algorithm by hand. We use Euclid's GCD algorithm as the running example.
What "correct" means
An algorithm is correct with respect to a specification when this holds: for every input that satisfies the precondition, the algorithm stops and produces an output that satisfies the postcondition.
Correctness is always measured against a specification. Code cannot be correct on its own terms. Take gcd(0, 0): it falls outside our precondition ("not both zero"), so whatever the algorithm does there is irrelevant. Correctness also says nothing about inputs the specification excludes. Those inputs are the caller's responsibility.
The definition has two parts:
| Part | Question it answers | Where it is covered |
|---|---|---|
| Partial correctness | If it stops, is the answer right? | This chapter and "Loop Invariants" |
| Termination | Does it stop on every valid input? | "Does It Stop? Arguing Termination" |
Total correctness requires both parts.
Tracing and testing are evidence, not proof
A trace of gcd(48, 18) shows that the algorithm works for one input. A test suite shows that it works for many inputs. Neither covers every valid input, so neither proves correctness. Testing can reveal a bug, but it cannot show that no bugs remain.
Testing still has real value when you check the postcondition directly instead of a few hand-picked answers. The test below compares Euclid's algorithm with a slow, obviously correct reference.
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
def gcd_reference(a, b):
# Directly from the definition: the largest d dividing both a and b
return max(d for d in range(1, max(a, b) + 1)
if a % d == 0 and b % d == 0)
for a in range(0, 60):
for b in range(0, 60):
if a == 0 and b == 0:
continue # excluded by the precondition
assert gcd(a, b) == gcd_reference(a, b), (a, b)
print("all checks passed")This test runs over every valid pair from 0 to 59, including the boundary cases where a or b is zero. Even so, it only covers 3,599 inputs out of infinitely many.
Arguing for all inputs
Covering every input takes a general argument. For Euclid's algorithm the argument uses one fact: when b > 0, gcd(a, b) = gcd(b, a mod b). Any number that divides both a and b also divides a mod b = a - qb, and the reverse also holds. So each loop iteration keeps the GCD of the pair unchanged. When the loop exits, b = 0, and gcd(a, 0) = a. The returned value is therefore the GCD of the original inputs. A property that every iteration preserves is called a loop invariant, and the "Loop Invariants" chapter turns this kind of argument into a reusable method.
Common errors
- Weakening the spec to fit the code. Changing the postcondition so that a buggy output counts as correct.
- Ignoring boundary inputs. The smallest valid inputs, such as
b = 0or a one-element list, are where off-by-one and initialisation bugs show up. - Bad initial values. For example, starting a maximum at
0gives the wrong answer for a list of negative numbers. - Checking examples instead of the postcondition. A test that only compares a few memorised answers can miss whole classes of bugs.
- Assuming termination. A loop that never exits returns nothing, so it never meets the postcondition.
Exercise
This function claims to satisfy "Precondition: xs is a non-empty list of numbers. Postcondition: returns the largest element of xs." Is it correct? Give a counterexample if not. def bad_max(xs): best = 0 for x in xs: if x > best: best = x return best
Show answer
It is not correct. For xs = [-3, -7], which satisfies the precondition, it returns 0. The postcondition fails because 0 is not an element of xs. Initialising best = xs[0] fixes the bug.
Exercise
Why does the test loop skip the pair (0, 0) instead of asserting something about it?
Show answer
The precondition excludes (0, 0), so the specification makes no promise about that input. Correctness only covers inputs that satisfy the precondition.