Why decomposition helps: testing each part separately

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Why decomposition helps: testing each part separately

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Why is it easier to find an error when a program is split into sub-problems?
A The whole program must be rewritten each time so the error disappears
B Each sub-problem can be tested on its own, so the error is traced to one part
2. A programmer has written a subroutine that calculates a total price. When should it be tested?
A On its own, before it is joined to the rest of the program
B Only after the whole program is finished and joined together
3. Test results show that the calculateAverage function gives the wrong answer, but all other subroutines pass. Where should the programmer look?
A Inside calculateAverage only
B Through every line of the whole program
4. A bug is fixed inside one subroutine of a decomposed program. What needs to be rewritten?
A The whole program from the start
B Only that subroutine
5. Which statement about testing a program that has NOT been decomposed is more likely to be true?
A An error is harder to trace because it could be anywhere in one long program
B An error is easier to trace because each part has its own test

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1OUTPUT CalcTotal(5, 2.50) AOnly the faulty subroutine needs fixing. The other tested parts work and do not need to be rewritten.
2CALL ShowMenu() BIf it fails, the error could be anywhere in the program, so it is hard to find which part causes it. Test each sub-problem on its own first.
3A failing test points to one part: if CalcTotal gives the wrong answer, the fault is inside that function only CFUNCTION CalcTotal(Qty, Price) RETURNS REAL RETURN Qty + Price ENDFUNCTION
4Fixing a part does not need rewriting everything: only the faulty line in one subroutine is changed DRETURN Qty * Price
5Write the whole program, then test it once at the end ERun one procedure on its own to check it works before the main program uses it
6CalcTotal gives a wrong answer, so rewrite the whole program from the start FTest one function alone by calling it with sample values and checking the result against the expected total of 12.50

Definitions

test each sub-problem separately

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
FUNCTION Double(n : INTEGER) RETURNS INTEGER RETURN n * 2 OUTPUT ______(5)
2.
expected ← 10 actual _ Double(5) OUTPUT actual = expected
3.
_________ ShowTotal(a, b : INTEGER) OUTPUT a + b ENDPROCEDURE
4.
FUNCTION Add(a, b : INTEGER) RETURNS INTEGER RETURN a _ b OUTPUT Add(2, 3) // expect 5
5.
CALL ReadMark() ____ CheckMark() // only this is fixed CALL ShowResult()

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A FUNCTION Area(w, h)
B RETURN w * h
C ENDFUNCTION
D IF Area(2, 3) = 5 THEN
E OUTPUT "Area passed"
F ENDIF
Question 2
A PROCEDURE ShowTotal(t)
B OUTPUT "Total: ", t
C ENDFUNCTION
D CALL ShowTotal(10)
Question 3
A PROCEDURE TestAverage()
B result = Average(4, 6)
C OUTPUT "Got: ", result
D ENDPROCEDURE
Question 4
A // Test CalcTax on its own
B price ← 100
C tax ← CalcTax(tax)
D OUTPUT "Want 20, got ", tax
Question 5
A PROCEDURE Total(a, b)
B RETURN a + b
C ENDFUNCTION
D OUTPUT Total(2, 3)

Answer Key

Why decomposition helps: testing each part separately
Section A: Quick Questions
Q1
B — Each sub-problem can be tested on its own, so the error is traced to one part
Q2
A — On its own, before it is joined to the rest of the program
Q3
A — Inside calculateAverage only
Q4
B — Only that subroutine
Q5
A — An error is harder to trace because it could be anywhere in one long program
Section B: Match the Pairs
Q1
OUTPUT CalcTotal(5, 2.50) → Test one function alone by calling it with sample values and checking the result against the expected total of 12.50
Q2
CALL ShowMenu() → Run one procedure on its own to check it works before the main program uses it
Q3
A failing test points to one part: if CalcTotal gives the wrong answer, the fault is inside that function only → FUNCTION CalcTotal(Qty, Price) RETURNS REAL RETURN Qty + Price ENDFUNCTION
Q4
Fixing a part does not need rewriting everything: only the faulty line in one subroutine is changed → RETURN Qty * Price
Q5
Write the whole program, then test it once at the end → If it fails, the error could be anywhere in the program, so it is hard to find which part causes it. Test each sub-problem on its own first.
Q6
CalcTotal gives a wrong answer, so rewrite the whole program from the start → Only the faulty subroutine needs fixing. The other tested parts work and do not need to be rewritten.
Section C: Definitions
Q1
test each sub-problem separately — Each sub-problem is tested on its own before being joined to the rest, so an error is traced to the part that causes it and only that part needs fixing, not the whole program.
Section D: Fill in the Blanks
Q1
FUNCTION Double(n : INTEGER) RETURNS INTEGER RETURN n * 2 OUTPUT Double(5)
Q2
expected ← 10 actual ← Double(5) OUTPUT actual = expected
Q3
PROCEDURE ShowTotal(a, b : INTEGER) OUTPUT a + b ENDPROCEDURE
Q4
FUNCTION Add(a, b : INTEGER) RETURNS INTEGER RETURN a + b OUTPUT Add(2, 3) // expect 5
Q5
CALL ReadMark() CALL CheckMark() // only this is fixed CALL ShowResult()
Section E: Spot the Error
Q1
D — The test expects the wrong answer. 2 * 3 is 6, so a correct Area function would be reported as failing. A test of one subroutine must check against the right expected value (6) so a failure really points to that part.
Q2
C — A PROCEDURE must be closed with ENDPROCEDURE, not ENDFUNCTION. Until this part is fixed it cannot be tested on its own, and only this procedure needs correcting.
Q3
B — Assignment in Cambridge pseudocode uses ←, not =. The test procedure must store the result of Average in result so that this one subroutine can be checked by itself.
Q4
C — tax has no value yet, so it cannot be passed in. The test should pass price (100) to CalcTax. A wrong input makes the test meaningless and hides whether CalcTax itself works.
Q5
A — Total returns a value, so it must start with FUNCTION to match ENDFUNCTION. A PROCEDURE does not return a value, so this part would fail its test. Only this one header needs fixing.