UNIHIVE

Study Hub UNIPORT COS 102

COS 102 · Problem Solving

Testing, debugging and refinement

Choosing test data, tracing to find errors, and improving a working solution.

Our notes

2 min read

Testing

Testing runs a program with chosen inputs and compares its output with the expected output worked out by hand. Good test data includes:

  • Normal data: typical values (a score of 65).
  • Boundary data: values at the edges (0, 39, 40, 100).
  • Erroneous data: values that should be rejected (−5, 150, "abc").

A test plan lists each test, its input, the expected result and the actual result.

Worked example: test plan for a grading program

TestInputExpectedWhy
175Anormal
270Aboundary of A
369Bjust below A
440Elowest pass
539Fjust below pass
6101"invalid score"outside range

If test 2 prints B, the program probably uses > 70 instead of >= 70.

Debugging

Debugging is finding and fixing the cause of an error.

  1. Reproduce the error with a small input.
  2. Trace the program by hand (a trace table) or add print() lines to see the values of variables at each step.
  3. Form a hypothesis about the cause, change one thing, and test again.
  4. Re-run all earlier tests to make sure the fix broke nothing else (regression testing).

Worked example: find the bug

total = 0
for i in range(1, 5):
    total = total + i
print("Sum of 1 to 5 is", total)

It prints 10, but 1 + 2 + 3 + 4 + 5 = 15. A trace shows i takes 1, 2, 3, 4 only: range(1, 5) stops before 5. Fix: range(1, 6). Now it prints 15 ✓.

Evaluation and refinement

Once a program works, ask:

  • Does it meet all the requirements, including unusual cases?
  • Is it easy to read (clear names, comments)?
  • Is it efficient enough for real data sizes (binary search instead of linear for a long sorted list)?
  • Does it handle bad input politely instead of crashing?

Stepwise refinement also works the other way: start with a rough outline ("read scores; compute grades; print report") and refine each step into more detail until every line can be coded.

Documentation

Write what the program does, how to use it, its inputs and outputs, and its limits. Comments inside the code explain tricky parts. Others (and you, months later) will need it.

Exams usually ask

  • Distinguish syntax, runtime and logic errors, with examples.
  • Prepare test data (normal, boundary, erroneous) for a given program.

Common mistake

Testing only with "nice" values. Most bugs live at the boundaries.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All COS 102 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.