Article
Does it always work? Testing an algorithm
A friend gives you a recipe for pancakes. You cook it once and it tastes great. Does that prove the recipe always works? Not yet: what if you use a bigger pan, or no eggs? An algorithm is the same. Working once is not enough.
What "correct" means
An algorithm is a list of steps that solves a problem. It is correct if it gives the right result for every valid input, and then stops. A valid input is any input the algorithm is meant to handle. For "find the largest number in a list", every list with at least one number is valid.
Notice two things. The word is every, not most. And the algorithm must stop: a loop that never ends is not correct.
Testing an algorithm
To test an algorithm, you run it on inputs you chose and compare its result with the right answer. You work out the right answer by hand first, so you are not just trusting the program.
One test
Choose an input
including tricky ones
Work out the right answer by hand
Run the algorithm
Compare
same: pass. different: a bug
Which inputs should you choose? Not only easy ones. Also pick edge cases: inputs at the border of what is allowed, where bugs like to hide. For a list, good edge cases are a list with one item, a list where all items are equal, one with negative numbers or zero, and one where the answer is the first or the last item.
Here is an algorithm that should find the largest item. Can you see a problem?
PROCEDURE largest(aList)
{
best ← 0
FOR EACH item IN aList
{
IF(item > best)
{
best ← item
}
}
RETURN(best)
}Test it. Run the code, then read the table.
| Input | Right answer | Result | Verdict |
|---|---|---|---|
[3, 8, 5] | 8 | 8 | pass |
[7] | 7 | 7 | pass |
[9, 4, 1] | 9 | 9 | pass |
[-5, -2, -9] | -2 | 0 | fail |
The last list has only negative numbers. best starts at 0, and nothing is bigger than 0, so the procedure returns 0. But 0 is not even in the list. The fix is to start with the first item: best ← aList[1] (in Python, a_list[0]). Change the code above and run it again.
Check your understanding
A tester runs an algorithm on five different inputs. All five results are right. What can the tester conclude?
One failure proves it wrong. Passing proves nothing.
An input that makes an algorithm give a wrong result is a counterexample. One counterexample is enough to prove the algorithm is not correct.
The opposite does not work. Three passing tests did not show that largest was correct. If you had run 1,000 tests with positive numbers, all would have passed, and the bug would still be there. So:
- A failed test proves the algorithm is wrong.
- Passed tests only show that it works on those inputs.
- The only way to prove an algorithm correct by testing is to test every valid input. That is possible only when there are very few inputs, for example two true/false values (4 inputs).
Because of this, good testers choose inputs that could fail: edge cases and unusual cases, not ten copies of the same easy case.
Comparing two algorithms
Sometimes two algorithms look different, or look almost the same, and you must decide if they give the same results. The rule is the same: they are the same only if they give the same result for every valid input. To show they are different, find one input where the results differ.
Both of these count the items bigger than 10. The only difference is > against ≥:
The first list gives 2 and 2: no difference. The second gives 1 and 2. The 10 is the input that tells them apart. A test that does not contain a 10 can never find the difference.
Check your understanding
This procedure should return the smallest item of a list of numbers. Which input shows that it is incorrect?
PROCEDURE smallest(aList)
{
best ← 0
FOR EACH item IN aList
{
IF(item < best)
{
best ← item
}
}
RETURN(best)
}Common mistakes
- "It passed my tests, so it is correct." Passing tests never prove correctness.
- Testing only typical inputs. Bugs usually hide at the edges: one item, zero, negative numbers, equal values.
- Working out the right answer from the program's output. Work it out by hand first.
- Testing with an invalid input. If the problem says a list has at least one item, an empty list is not a fair test.
- Forgetting that the algorithm must stop. An infinite loop is wrong for every input it never finishes on.
On the exam
Expect questions such as "Which input shows that the algorithm is incorrect?" or "Which test would find the error?". Trace each answer option through the code. The right option is the one where the code's result is not the right answer. When asked if two algorithms give the same result, look for the one input that separates them (often a value that sits exactly on a > / ≥ border, or a list with one item).
Key words
Correct: gives the right result for every valid input, and stops.
Valid input: an input the algorithm is meant to handle.
Test: running an algorithm on a chosen input and comparing with the right answer.
Edge case: an input at the border of what is allowed, such as a list with one item.
Counterexample: an input for which the algorithm gives a wrong result.