Testing an algorithm with different starting values

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

Testing an algorithm with different starting values

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. An algorithm says: IF age >= 13 THEN say 'Teen ticket' ELSE say 'Child ticket'. Which test values check BOTH paths?
A Test with age 15 and age 20
B Test with age 8 and age 15
2. Why do we test an algorithm with several different starting values?
A To check that every decision path and repeat works
B To make the algorithm run faster
3. Before you run a test, what should you write down?
A Only the result the algorithm gives
B The result you expect to get
4. An algorithm says: REPEAT UNTIL count = 0. You test it with a starting count of 0. What should you expect?
A The loop runs zero times
B The loop runs one time
5. You expected the answer 12, but the algorithm gave 10. What does this tell you?
A There may be a mistake to find and fix
B The test passed, so nothing is wrong

Match the Pairs

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

Left columnRight column
1Test 1: input 15, expect "Pass" Test 2: input 3, expect "Try again" AA test case for each path of an IF ... ELSE, so both the THEN path and the ELSE path get checked
2Test: REPEAT count times, count = 0 Expect: loop body never runs BExpected: 10 Actual: 8 Result: FAIL
3Comparing expected and actual results: if they do not match, the algorithm has a mistake to fix CThe loop is never tested with a start value of 0, so we do not know if it correctly runs zero times. Add a test with n = 0.
4A test for every path: pick starting values so each branch of the decision is used DIF score >= 50 THEN Test with score = 70 ELSE Test with score = 20
5IF age >= 12 THEN ... ELSE ... Test: age = 15 only EOnly the THEN path is tested. The ELSE path is never tried, so a mistake there would be missed. Add a test such as age = 8.
6REPEAT n times Test: n = 5 only Expect: it runs 5 times FA test case for a loop that runs zero times, to check the steps inside are skipped

Definitions

test case

Fill in the Blanks

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

1.
REPEAT count TIMES OUTPUT "Hi" TEST count ← _, expected: no "Hi"
2.
SET expected ← 10 SET actual ← 10 IF actual _ expected THEN OUTPUT "Pass"
3.
IF temp > 30 THEN OUTPUT "Hot" ELSE OUTPUT "Mild" Test temp ← 20 uses the ____ path
4.
IF n > 0 THEN OUTPUT "Positive" ELSE OUTPUT "Not positive" TEST _ ← 0, expected "Not positive"
5.
FOR EACH test IN testList IF actual _ expected THEN OUTPUT "Fix the algorithm"

Spot the Error

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

Question 1
A RULE: 50 or more is a Pass
B TEST score 50, EXPECT Fail
C TEST score 49, EXPECT Fail
Question 2
A SET n TO 0
B REPEAT n + 1 TIMES
C SAY Hello
D EXPECT: no Hello when n is 0
Question 3
A TEST lives=3, EXPECT Lose
B IF lives = 0 THEN
C SAY Lose
D ELSE SAY Play on
Question 4
A EXPECTED: 12
B ACTUAL: 10
C They differ: the test passed
D Fix the algorithm, then retest
Question 5
A IF temp > 30 SAY Hot
B ELSE IF temp > 15 SAY Mild
C ELSE SAY Cold
D TESTS: 35, 40, 50

Answer Key

Testing an algorithm with different starting values
Section A: Quick Questions
Q1
B — Test with age 8 and age 15
Q2
A — To check that every decision path and repeat works
Q3
B — The result you expect to get
Q4
A — The loop runs zero times
Q5
A — There may be a mistake to find and fix
Section B: Match the Pairs
Q1
Test 1: input 15, expect "Pass" Test 2: input 3, expect "Try again" → A test case for each path of an IF ... ELSE, so both the THEN path and the ELSE path get checked
Q2
Test: REPEAT count times, count = 0 Expect: loop body never runs → A test case for a loop that runs zero times, to check the steps inside are skipped
Q3
Comparing expected and actual results: if they do not match, the algorithm has a mistake to fix → Expected: 10 Actual: 8 Result: FAIL
Q4
A test for every path: pick starting values so each branch of the decision is used → IF score >= 50 THEN Test with score = 70 ELSE Test with score = 20
Q5
IF age >= 12 THEN ... ELSE ... Test: age = 15 only → Only the THEN path is tested. The ELSE path is never tried, so a mistake there would be missed. Add a test such as age = 8.
Q6
REPEAT n times Test: n = 5 only Expect: it runs 5 times → The loop is never tested with a start value of 0, so we do not know if it correctly runs zero times. Add a test with n = 0.
Section C: Definitions
Q1
test case — A chosen starting value you try in an algorithm, with the result you expect. Use one for every path and for a loop that runs zero times, then compare expected and actual results.
Section D: Fill in the Blanks
Q1
REPEAT count TIMES OUTPUT "Hi" TEST count ← 0, expected: no "Hi"
Q2
SET expected ← 10 SET actual ← 10 IF actual = expected THEN OUTPUT "Pass"
Q3
IF temp > 30 THEN OUTPUT "Hot" ELSE OUTPUT "Mild" Test temp ← 20 uses the ELSE path
Q4
IF n > 0 THEN OUTPUT "Positive" ELSE OUTPUT "Not positive" TEST n ← 0, expected "Not positive"
Q5
FOR EACH test IN testList IF actual ≠ expected THEN OUTPUT "Fix the algorithm"
Section E: Spot the Error
Q1
B — The rule says 50 or more is a Pass, so the test case with score 50 should expect Pass, not Fail.
Q2
B — Repeating n + 1 times would say Hello once even when n is 0. The loop should repeat n TIMES so it runs zero times for 0.
Q3
A — With lives=3 the algorithm goes to the ELSE path and says Play on. To test the Lose path, the starting value must be lives=0.
Q4
C — When the expected and actual results are different, the test has failed and shows a bug. It should say there is a bug.
Q5
D — 35, 40 and 50 all take the Hot path, so Mild and Cold are never tested. Choose one value for each path, such as 35, 20 and 5.