Grade 9 · lesson 4 of 4

Repair a Function and Keep Its Test

Verify a correction against ordinary, boundary and previously failing cases.

Download the grade pack

Supplied materials

  • Paper and pencil; an adult may read aloud. No device, account or purchase is needed.

Learn the idea

A regression test is a saved test for behavior that should keep working after changes. Fixing one failing case can accidentally break another branch. A small test suite with explicit expected outputs gives evidence about the supplied cases; passing it does not prove correctness for every imaginable input.

Contract: for integer x from 0 through 10, label(x) returns low if x < 3, mid if 3 ≤ x < 7, and high otherwise. Faulty draft: if x < 3 return low; else if x ≤ 7 return mid; else high. The boundary test 7 → high fails. Repair the second comparison to x < 7, keeping the first branch unchanged.

  1. Write expected labels for 2, 3, 6, 7 and 8 before tracing.
  2. Run the faulty draft and mark each pass or fail.
  3. Repair ≤ 7 to < 7.
  4. Rerun all five cases and keep 7 → high as the regression test.
Worked example

See the method in action

Faulty outputs for 2,3,6,7,8: low, mid, mid, mid, high. Only 7 fails its expected high label. Repaired outputs: low, mid, mid, high, high; all five supplied tests pass.

Your turn

Check your reasoning

Your answer is temporary and is not saved to an account. Download your current answer before leaving or reloading. Written work on paper or in another app stays in your own file.

Which retained test directly guards against the repaired bug returning?

Feedback appears here after you check. This is practice, not a graded school assessment.

See the answer and explanation

7 → high

The original error was including 7 in mid. Keeping 7 → high detects that same error if it reappears.

Take it further

Change the second boundary to 8: low if x < 3, mid if 3 ≤ x < 8, otherwise high. Expected results at 7,8,9: mid, high, high. Update expected outputs deliberately because the contract changed.

Nothing here is saved to an account or sent to a teacher. The answer download includes your current choice and any checked feedback.

Check the subject sources

These LifeStarter passages, scenarios and exercises are authored for this Academy with AI assistance and review. External resources keep their publishers’ terms. A grade label suggests a starting level; it does not establish alignment with your school’s complete requirements.