Judging what code does from runtime input and output evidence

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

Judging what code does from runtime input and output evidence

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. You are given a function you cannot read. Which first step best matches black-box testing?
A Guess what it does from its name and trust that guess
B Run it with chosen inputs and record each output in a table
2. Which set of test inputs is more informative for an unknown function that takes a number?
A -5, 0, 1, 10, 100
B 4, 4, 4, 4, 4
3. You run unknown code with inputs 2, 3 and 4 and get outputs 4, 9 and 16. What can you reasonably infer?
A It probably squares the input, but you should test more values to be sure
B It definitely squares every possible input, so no more testing is needed
4. Which is the best way to record evidence from black-box testing?
A A paragraph describing the code you think is inside
B An input-output table with one row per test run
5. Your tests of an unknown function only used inputs from 1 to 10. Which statement is correct?
A You cannot say how it behaves for 0, negative numbers or very large numbers
B You know how it behaves for every number because the pattern held

Match the Pairs

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

Left columnRight column
1mystery(4) -> 16, mystery(5) -> 25, mystery(-3) -> 9 A# 3 tests fit n * 2, but I did not test negatives
2mystery(0), mystery(-1), mystery(1000) BInformative inputs: a zero, a negative and a large value that test edge cases and reveal the limits of the behaviour.
3Record each chosen input and the output it produced in a table, so you can look for a pattern. COne test result is used to claim a rule that holds for all inputs. Squaring also gives 4 from 2, so more varied inputs are needed.
4Infer the process from the pattern, but state the limit: you only know the inputs you tried. DThree small positive inputs are claimed to prove behaviour for every number. The evidence only covers the inputs tried, so zero, negatives and large values are untested.
5mystery(2) -> 4 so mystery always doubles its input EAn input-output table: the outputs are each input squared, so the process is probably n * n.
6Tried mystery(1), mystery(2), mystery(3): it is fully tested and correct for every number F| input | output | | 2 | 8 | | 3 | 27 |

Definitions

black-box testing

Fill in the Blanks

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

1.
___ x in [0, 1, 5, -2]: print(x, mystery(x))
2.
result = mystery(x) _____(x, "->", result)
3.
table = {} result = mystery(x) table[x] = ______
4.
if mystery(3) == 9 ___ mystery(5) == 25: print("Looks like squaring")
5.
tested = [1, 2, 5, 10] if 0 ______ tested: print("Limit: 0 never tested")

Spot the Error

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

Question 1
A tests = [0, 1, 5, -3]
B for x in tests:
C print(x, mystery(tests))
Question 2
A results = {}
B for x in [0, 1, 5, -3]:
C out = mystery(x)
D results[out] = x
E print(results)
Question 3
A x = int(input('Enter: '))
B y = mystery(x)
C print('In:', y, 'Out:', x)
Question 4
A # Black-box test of mystery()
B for n in [3, 3, 3, 3]:
C print(n, mystery(n))
D # Table shows: out is n + 2
Question 5
A rows = []
B for n in [0, 1, 5, -2]:
C rows.append((n, f(n)))
D print(rows[0])

Answer Key

Judging what code does from runtime input and output evidence
Section A: Quick Questions
Q1
B — Run it with chosen inputs and record each output in a table
Q2
A — -5, 0, 1, 10, 100
Q3
A — It probably squares the input, but you should test more values to be sure
Q4
B — An input-output table with one row per test run
Q5
A — You cannot say how it behaves for 0, negative numbers or very large numbers
Section B: Match the Pairs
Q1
mystery(4) -> 16, mystery(5) -> 25, mystery(-3) -> 9 → An input-output table: the outputs are each input squared, so the process is probably n * n.
Q2
mystery(0), mystery(-1), mystery(1000) → Informative inputs: a zero, a negative and a large value that test edge cases and reveal the limits of the behaviour.
Q3
Record each chosen input and the output it produced in a table, so you can look for a pattern. → | input | output | | 2 | 8 | | 3 | 27 |
Q4
Infer the process from the pattern, but state the limit: you only know the inputs you tried. → # 3 tests fit n * 2, but I did not test negatives
Q5
mystery(2) -> 4 so mystery always doubles its input → One test result is used to claim a rule that holds for all inputs. Squaring also gives 4 from 2, so more varied inputs are needed.
Q6
Tried mystery(1), mystery(2), mystery(3): it is fully tested and correct for every number → Three small positive inputs are claimed to prove behaviour for every number. The evidence only covers the inputs tried, so zero, negatives and large values are untested.
Section C: Definitions
Q1
black-box testing — Running unfamiliar code with chosen inputs and recording the outputs to infer what it does without reading the source. The evidence only covers the inputs you tried.
Section D: Fill in the Blanks
Q1
for x in [0, 1, 5, -2]: print(x, mystery(x))
Q2
result = mystery(x) print(x, "->", result)
Q3
table = {} result = mystery(x) table[x] = result
Q4
if mystery(3) == 9 and mystery(5) == 25: print("Looks like squaring")
Q5
tested = [1, 2, 5, 10] if 0 not in tested: print("Limit: 0 never tested")
Section E: Spot the Error
Q1
C — Each test must run mystery() on one input at a time. Passing the whole list tests mixes every input together. It should be mystery(x) so each input has its own output.
Q2
D — The table should map each input to its output. Here the key and value are swapped, so the evidence is recorded backwards. It should be results[x] = out.
Q3
C — The labels are attached to the wrong values. The input x is shown as Out and the result y as In, which gives misleading evidence. It should be print('In:', x, 'Out:', y).
Q4
B — Repeating the same input gives no new information, so you cannot infer a pattern. Informative inputs vary: zero, a small number, a larger number and a negative, such as [0, 1, 5, -2].
Q5
D — Only the first input-output pair is shown, so most of the evidence is thrown away. The whole table should be printed with print(rows).