1.4 Testing with defined inputsChoosing tests that actually find bugs

In the development process, testing uses defined inputs to make sure an algorithm or program produces the expected outcomes. Defined means chosen on purpose, ahead of time, along with the result you expect. Then you compare: expected versus actual. If they differ, you have found a bug, and you use the results of the test to revise the program.
Which inputs? The course is specific: defined inputs should demonstrate the different expected outcomes, and include values at or just beyond the extremes, the minimum and maximum, of the input data. In practice that means: one test for each kind of result the program can give, plus tests right at the edges, where a cutoff or a limit lives.
?Wait, what's the difference between > and ≥?
Watch it work. A store offers free shipping on orders of 50 dollars or more. Here's the procedure someone wrote.
1PROCEDURE shipping(total)2{3 IF (total > 50)14 {5 RETURN(0)6 }7 RETURN(7)28}
- line 3 Meant to be free shipping for 50 or more
- line 7 Every other order pays 7
Test it with "normal" orders, 30 and 80, and everything looks perfect: 30 pays 7, 80 ships free. Those tests are useful, because they show both expected outcomes, but they can't catch this bug. Only an order of exactly 50 does: the requirement says it ships free, and the code charges 7, because 50 > 50 is false.
| test input | expected | actual | result |
|---|---|---|---|
| 30 | 7 | 7 | pass |
| 80 | 0 | 0 | pass |
| 49 | 7 | 7 | pass |
| 50 | 0 | 7 | FAIL |
| 51 | 0 | 0 | pass |
?Wait, how do you know the expected outcome?
Two more kinds of tests catch a lot: unusual inputs, like an empty list, a zero, or a negative number, and extremes, like the smallest and largest values allowed. If a timer accepts 5 to 60 minutes, test 4, 5, 60 and 61. If a procedure takes a list, test an empty list and a list of one element.
I thought about it. Show me
1n ← 52REPEAT UNTIL (n = 0)13{4 DISPLAY(n)5 n ← n - 26}
- line 2 n goes 5, 3, 1, -1, ... and never equals 0
- Test by tracing: write a table with one row per check of the condition, tracking n and whether n = 0 is true.
Why?
REPEAT UNTIL checks its condition before every pass and stops the first time it's true. - Check 1: n is 5, not 0, so display 5 and n becomes 3. Check 2: display 3, n becomes 1. Check 3: display 1, n becomes -1.
- Check 4: n is -1. -1 = 0 is false, so it keeps going: -1, -3, -5, ... n skipped right over 0. The loop never ends.
Why?
Counting down by 2 from an odd number never lands on 0 exactly. This is a logic error that shows up as an infinite loop. - Fix: change the condition to REPEAT UNTIL (n ≤ 0). Now at n = -1 the condition is true, and the loop stops after displaying 5 3 1.
Why?
Using ≤ instead of = makes the stop condition catch any value at or past the goal, not just one exact value.
Answer: It displays 5 3 1 -1 -3 and never stops, because n jumps from 1 to -1 and never equals 0. Changing the condition to n ≤ 0 makes it display 5 3 1 and stop.
| check | n | n = 0? | displayed |
|---|---|---|---|
| 1 | 5 | false | 5 |
| 2 | 3 | false | 3 |
| 3 | 1 | false | 1 |
| 4 | -1 | false | -1 |
| 5 | -3 | false | -3 (and on, forever) |
Stuck? Get a hint
- Write the value of n at each check: 5, 3, 1, ...
- What comes after 1 when you subtract 2?
- Does n ever equal exactly 0? What condition would still be true once n has gone past 0?
- A20, 50, 90
- B64, 65, 66
- C0, 100
- D70, 80, 90
Show the answer
B. 64, 65 and 66 test just below, at, and just above the cutoff. A mistake like score > 65 would wrongly fail 65. The other sets never touch 65, so both versions of the comparison give the same results on them.
- AIf a program passes five tests, it has no logic errors.
- BGood tests use defined inputs with known expected outcomes, including values at and just beyond the extremes of the input.
- CTests are only needed after the program is finished.
- DTesting with random inputs is always better than choosing inputs on purpose.
Show the answer
B. Testing compares actual outcomes with expected outcomes for chosen inputs, and boundaries are where bugs hide. Passing a few tests never proves there are no errors, which is the trap in the first choice, and testing happens throughout development.

When a question asks for the "best set of test inputs", look for the set that sits on both sides of every cutoff. Big, normal-looking numbers almost never win.
- Testing uses defined inputs with expected outcomes. Compare expected and actual, then revise.
- Choose inputs that show every expected outcome, plus values at and just beyond the minimum and maximum.
- Test at each cutoff (just below, at, just above) and with unusual inputs like an empty list or zero.
- Program requirements tell you which inputs to test and what to expect.