Choosing test data is a skill of its own. Normal data shows the algorithm can work. Empty, single-item and repeated data show whether it still works at the edges, where most faults hide.
This lesson closes searching, sorting and files. It uses the testing language from validation, verification and testing and the trace tables from tracing a linear search.
Which awkward cases should I try?
- Empty: no items at all.
- One item: the smallest non-empty list.
- All equal: every value the same.
- Duplicates of the answer: the maximum or target appears more than once.
- Answer at an end: first or last position.
Worked example
The task: find the highest score in an array Scores of Count items, and say how many times it occurs. A first version, with an empty-data guard:
IF Count = 0 THEN
OUTPUT "No data"
ELSE
Max ← Scores[1]
Times ← 1
I ← 2
WHILE I <= Count
IF Scores[I] > Max THEN
Max ← Scores[I]
Times ← 1
ELSE
IF Scores[I] = Max THEN
Times ← Times + 1
ENDIF
ENDIF
I ← I + 1
ENDWHILE
OUTPUT Max, Times
ENDIF
Test: Scores = [8, 5, 8, 3, 8], Count = 5.
| I | Scores[I] | Max | Times |
|---|---|---|---|
| Start | 8 | 1 | |
| 2 | 5 | 8 | 1 |
| 3 | 8 | 8 | 2 |
| 4 | 3 | 8 | 2 |
| 5 | 8 | 8 | 3 |
Output: 8, 3. The highest is 8 and it occurs three times.
Test: empty, Count = 0. The guard catches it and outputs No data. Without the guard, Scores[1] would refer to an item that does not exist.
Test: one item, Scores = [9], Count = 1. Max = 9 and Times = 1. The loop starts with I = 2, which is greater than Count, so the body never runs. Output: 9, 1.
The mistake to watch for
A student changes the comparison to >= to “include equal values”:
IF Scores[I] >= Max THEN
Max ← Scores[I]
Times ← 1
On [8, 5, 8, 3, 8], at I = 3 the value 8 satisfies >=, so Times is reset to 1 instead of increasing to 2. At I = 5 it resets again. The final output is 8, 1, which is wrong.
The correction is to keep > for a new maximum and use a separate = branch to count repeats. The faulty version only shows up with duplicate data, which is why the duplicate test matters.
Check yourself
Use the algorithm from the worked example.
1. Scores = [4, 4], Count = 2. What is output?
Show answer
Start Max = 4, Times = 1. At I = 2, 4 equals Max, so Times becomes 2. Output 4, 2.
2. Scores = [2, 7, 7, 7], Count = 4. What is output?
Show answer
Start Max = 2. At I = 2, 7 > 2, so Max = 7 and Times = 1. At I = 3, Times = 2. At I = 4, Times = 3. Output 7, 3.
3. Name three test cases that would check an algorithm that finds the lowest value in a list.
Show answer
For example: an empty list, a one-item list, and a list where the lowest value appears twice. A list with the lowest value in the last position is also worthwhile.
Where this leads next
Put the whole module to work in the mixed practice set, and log each slip in the mistake log and retest queue.
If you tend to test only the example that came with the question, our teachers can help you build the edge-case habit in online one-to-one Computer Science tuition.