Topic 7 · Algorithm design and problem-solving
Test data and trace tables
Normal, abnormal, extreme, and boundary test data, and how to trace an algorithm one line at a time.
Types of test data
To test a program properly you need several kinds of data. Take a program that accepts whole-number percentages from 0 to 100:
| Type | Meaning | Examples for 0–100 | Expected result |
|---|---|---|---|
| Normal | Ordinary data that should be accepted | 45, 72 | Accepted |
| Abnormal | Data that should be rejected | −20, 150, "abc" | Rejected |
| Extreme | The largest and smallest values that should be accepted | 0, 100 | Accepted |
| Boundary | The values at each limit and just beyond it | 0 and −1, 100 and 101 | 0 and 100 accepted, −1 and 101 rejected |
Exam tip. For boundary data, always give a pair: the last accepted value and the first rejected value.
Trace tables
A trace table records the value of every variable as an algorithm runs, line by line. It lets you check what an algorithm does, find logic errors, and work out its output without a computer.
Here is an algorithm that adds up only the positive numbers it is given. Step through its trace with the inputs 5, −2, 7, 3.
DECLARE Total : INTEGER
DECLARE Count : INTEGER
DECLARE Number : INTEGER
Total <- 0
FOR Count <- 1 TO 4
INPUT Number
IF Number > 0
THEN
Total <- Total + Number
ENDIF
NEXT Count
OUTPUT Total| Count | Number | Total | OUTPUT |
|---|---|---|---|
| 0 | |||
| 1 | 5 | 5 | |
| 2 | −2 | 5 | |
| 3 | 7 | 12 | |
| 4 | 3 | 15 | |
| 15 |
Total stays at 5.Finding errors
A syntax error breaks the rules of the language, such as a missing ENDIF, and stops the program being translated. A logic error lets the program run but gives the wrong result.
This algorithm should output the largest of 10 numbers, but it has a logic error. Can you spot it?
DECLARE Largest : INTEGER
DECLARE Number : INTEGER
DECLARE Count : INTEGER
Largest <- 0
FOR Count <- 1 TO 10
INPUT Number
IF Number < Largest
THEN
Largest <- Number
ENDIF
NEXT Count
OUTPUT LargestWatch out. The comparison should be Number > Largest. Also, starting Largest at 0 fails if every number is negative. Starting with the first number input is safer.
Key terms
- Normal data
- Data within the accepted range that should be accepted.
- Abnormal data
- Data that should be rejected.
- Extreme data
- The largest and smallest values that should be accepted.
- Boundary data
- Values at the edge of the accepted range and just outside it.
- Trace table
- A table that records variable values as an algorithm runs.
- Logic error
- An error that lets a program run but produces the wrong result.
Check yourself
A program accepts ages 11 to 18. Give boundary test data.Show answer
10 and 11 (lower), 18 and 19 (upper). 11 and 18 are accepted, 10 and 19 rejected.
Is "twelve" normal, abnormal, extreme, or boundary data for an age field?Show answer
Abnormal: it is the wrong data type and should be rejected.
What is the difference between a syntax error and a logic error?Show answer
A syntax error breaks the language rules so the code will not translate. A logic error runs but gives the wrong result.