Problem Solving — CS & DS

Lecture 08 — Testing Before Trusting

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

The triangle classifier: building the test table

cs-029

Classify triangles: scalene, isosceles, equilateral, impossible (degenerate), invalid. Design the test table before the classifier.

Expected: six verdict lines matching the reference

Cost: O(1) per input; the intellectual work is the table, not the branches.

Worked example

Black box vs white box on the fee function

cs-031

fee(x): 0 below 3, 2 up to 5, 5 above. A buggy version charges 0 at exactly x = 3. Which suite finds it - black box or white box?

def classify(a, b, c):
    sides = sorted([a, b, c])
    if sides[0] + sides[1] <= sides[2]:
        return "invalid"
    if a == b == c:
        return "equilateral"
    if a == b or b == c or a == c:
        return "isosceles"
    return "scalene"

print(classify(3, 4, 5), classify(5, 5, 5), classify(4, 4, 7), classify(1, 2, 8))

Expected: rows ending with a disagreement exactly at x = 3

Cost: Suite size stays small (4-8 rows) because white-box targeting hits the seams instead of sampling blindly.

Case attempt · lecture 08
Case cs-029 · Beginner

Triangle Classifier

Classify a triangle from three sides. Design the full test set: equilateral, isosceles, scalene, degenerate, impossible — before looking at any implementation.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-029 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-029 — not embedded in this deck.

Case attempt · lecture 08
Case cs-030 · Foundational

The Age Gate

A system admits visitors 12 and older on weekends, 16 and older on weekdays. Derive test cases from the requirement, including the boundary birthdays.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-030 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-030 — not embedded in this deck.

Case attempt · lecture 08
Case cs-031 · Intermediate

Black Box vs White Box

Given a small function with tricky internal branches, compare the tests a black-box tester writes with the tests the code's structure demands. Which defect escapes which suite?

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-031 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-031 — not embedded in this deck.

Case attempt · lecture 08
Case cs-032 · Advanced

The Flaky Function

A function uses randomness; tests pass sometimes. Discuss asserting distributions instead of exact values, seeding, and statistical tests for randomized code.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-032 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-032 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Name the three test-case classes and one example of each.
  2. Black-box vs white-box in one sentence each.
  3. What makes a randomness test fair?
Expected answers (instructor): normal (typical use), boundary (edges: 0, 120), adversarial (empty, negatives, junk) black-box: from the spec only; white-box: from the code's known branches control the seed or test the distribution over many runs, never a single outcome
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF