CSC 288 · Structured Programming
Our notes
2 min readKinds of error
- Syntax (compile-time) errors: breaking the language rules (missing semicolon, undeclared name). The compiler reports them.
- Run-time errors: the program stops while running (division by zero, file not found, array index out of range).
- Logic errors: the program runs but gives wrong answers (using > instead of >=). The hardest to find.
Test construction
A test plan lists, for each test: the purpose, the input data, the expected output (worked out beforehand) and the actual output.
Choose test data of three kinds:
- Normal: typical valid values (score 65).
- Boundary (extreme): at and just around the limits (0, 39, 40, 100).
- Erroneous: invalid values that must be rejected (−5, 101, "abc").
Worked example: test data for the grading program
| Input | Expected | Reason |
|---|---|---|
| 75 | A | normal |
| 70 | A | boundary, lowest A |
| 69 | B | boundary, just below A |
| 40 | E | lowest pass |
| 39 | F | just below pass |
| 0 | F | lowest valid |
| 100 | A | highest valid |
| 101 | rejected | invalid |
Many bugs live at boundaries, which is why most of the tests are there.
Black-box and white-box testing
- Black-box (functional): test from the specification without looking at the code: equivalence classes and boundary values.
- White-box (structural): design tests from the code so every statement and every branch runs at least once (statement and branch coverage).
Levels of testing
Unit (each module, with drivers and stubs), integration (modules together), system (the whole program), acceptance (by the user).
Debugging
- Reproduce the error reliably.
- Narrow it down: print the values of key variables (trace statements), or use the debugger to step through, set breakpoints and watch variables (Turbo and Free Pascal IDEs have these).
- Form a hypothesis, fix, and re-run all the tests (regression testing), since a fix can break something else.
Code inspection and walkthroughs
Programmers read the code together, line by line, against a checklist (initialised variables, loop bounds, parameter types). Inspections find many errors before the program is ever run, cheaply.
Verification
Testing can show the presence of bugs, never their absence (Dijkstra). Verification proves a program meets its specification using assertions and invariants (see the loop invariant topic). It is used for small, critical pieces of code.
Exams usually ask
- Distinguish syntax, run-time and logic errors with examples.
- Construct a test plan with normal, boundary and erroneous data.
- Compare black-box and white-box testing; describe debugging methods.
Common mistake
Testing only with "nice" data. A program that works for 65 may fail at 40 or crash on letters; boundary and invalid data find the real bugs.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
No reading links here yet
Our team adds free books, videos and practice for each topic, and opens every link before it goes up.