Choosing test data for an algorithm

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

Choosing test data for an algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which describes good test data for an algorithm?
A Chosen inputs, each with the output you expect
B Any random numbers with no expected output
2. An algorithm has 3 branches (if, else if, else). How many tests do you need at least?
A 1 test is enough
B 3 tests, one for each branch
3. When should you work out the expected output?
A After you see what the algorithm gives you
B Before you run the algorithm
4. if mark >= 50 then print "Pass" else print "Fail". Which test data covers both branches?
A mark = 70 (expect Pass) and mark = 30 (expect Fail)
B mark = 70 and mark = 80 (both expect Pass)
5. After you run a test, what do you compare?
A The real result with the expected output
B The real result with the input value

Match the Pairs

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

Left columnRight column
1Test: mark = 75, expected = "Pass" AThe test cases have no expected output, so you cannot tell whether the real result is right or wrong.
2Tests: age = 8, age = 14, age = 30 BInput: 40 -> Expected: "Fail"
3Write down the expected output before you run the test, so you can compare it with the real result CAll three test values go down the same branch (Pass). Nothing tests the else branch, so a mistake there would never be found.
4An if-then-else has two branches, so you need at least two tests, one for each branch DTest data with one test for each branch: a child, a teen and an adult path
5if mark >= 50 then print "Pass" else print "Fail" Tests: 60, 70, 90 EOne test case: a chosen input paired with the output you expect before running the algorithm
6Tests: input = 5, input = 12 (no expected output written) Fif score >= 50 then: Tests: 80 and 20

Definitions

test data

Fill in the Blanks

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

1.
if mark >= 50 then print "Pass" else print "Fail" Test: mark = 30, ________ = "Fail"
2.
Algorithm has 3 branches (if, else if, else) One test per branch means _ tests
3.
if mark >= 50 then print "Pass" Test: ____ = 50, expected output "Pass"
4.
actual ← 20 expected ← 20 if actual _ expected then print "Match"
5.
if age >= 18 then fee ← 10 else fee ← 5 Test: age = 12, expected fee = _

Spot the Error

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

Question 1
A # Rule: mark >= 50 is a Fail
B # Test 1: 70, expect Pass
C # Test 2: 30, expect Fail
Question 2
A # Ticket: under 13 = Child
B # 13-17 = Teen, 18+ = Adult
C # Test 1: 8, expect Child
D # Test 2: 15, expect Adult
E # Test 3: 30, expect Adult
Question 3
A if n > 0 then print Positive
B else print Not positive
C Test 1: 5, expect Positive
D Test 2: 0, expect Positive
Question 4
A if x > 10 then print Big
B else print Small
C Test 1: 30, expect Small
D Test 2: 20, expect Big
Question 5
A # Free delivery if cost >= 50
B # Test 1: 60, expect Pay $5
C # Test 2: 20, expect Pay $5

Answer Key

Choosing test data for an algorithm
Section A: Quick Questions
Q1
A — Chosen inputs, each with the output you expect
Q2
B — 3 tests, one for each branch
Q3
B — Before you run the algorithm
Q4
A — mark = 70 (expect Pass) and mark = 30 (expect Fail)
Q5
A — The real result with the expected output
Section B: Match the Pairs
Q1
Test: mark = 75, expected = "Pass" → One test case: a chosen input paired with the output you expect before running the algorithm
Q2
Tests: age = 8, age = 14, age = 30 → Test data with one test for each branch: a child, a teen and an adult path
Q3
Write down the expected output before you run the test, so you can compare it with the real result → Input: 40 -> Expected: "Fail"
Q4
An if-then-else has two branches, so you need at least two tests, one for each branch → if score >= 50 then: Tests: 80 and 20
Q5
if mark >= 50 then print "Pass" else print "Fail" Tests: 60, 70, 90 → All three test values go down the same branch (Pass). Nothing tests the else branch, so a mistake there would never be found.
Q6
Tests: input = 5, input = 12 (no expected output written) → The test cases have no expected output, so you cannot tell whether the real result is right or wrong.
Section C: Definitions
Q1
test data — Chosen inputs, each with the output you expect, picked so every path (branch) through an algorithm gets tested. Compare the real result to the expected output.
Section D: Fill in the Blanks
Q1
if mark >= 50 then print "Pass" else print "Fail" Test: mark = 30, expected = "Fail"
Q2
Algorithm has 3 branches (if, else if, else) One test per branch means 3 tests
Q3
if mark >= 50 then print "Pass" Test: mark = 50, expected output "Pass"
Q4
actual ← 20 expected ← 20 if actual = expected then print "Match"
Q5
if age >= 18 then fee ← 10 else fee ← 5 Test: age = 12, expected fee = 5
Section E: Spot the Error
Q1
A — The rule does not match the tests. A mark of 50 or more should be a Pass, so the rule should say 'is a Pass'. Test data only works if the expected outputs follow the rule.
Q2
D — An input of 15 is in the 13-17 range, so the expected output is Teen, not Adult. Each test needs the correct expected output for its branch.
Q3
D — 0 is not greater than 0, so the else branch runs. The expected output should be 'Not positive'.
Q4
C — 30 is bigger than 10, so it would print Big. To test the else branch, use an input of 10 or less, such as 3. Then there is one test per branch.
Q5
B — 60 is at least 50, so the order gets free delivery. The expected output should be 'Free'.