Syllabus Lesson 87 of 141 · Make the bug louder
Make the bug louder

Catch the variants

Why it matters Asked for a test, an AI assistant usually writes one or two happy-path examples. They pass on the code you have, which feels like safety. The question that matters is how many wrong versions they would also pass.

A calendar service merges busy periods before showing free time. The function is merge(intervals):

merge([(9, 11), (10, 12), (14, 15)])   # -> [(9, 12), (14, 15)]
merge([(1, 2), (2, 3)])                # -> [(1, 3)]   touching periods merge too

Six hidden versions of merge each have one realistic bug. You do not write merge. You write the test that catches them.

How to think about it

Go through the rules one at a time and ask, for each: "what would a version that gets this rule wrong return, and which input would show it?" One check per rule beats ten checks of the same rule.

Your turn

Write my_test(merge). It receives one version of the function and must raise (an assert is enough) if that version breaks any rule, and return quietly if it is correct. The rules for merge: intervals are (start, end) tuples with start <= end, in any order; overlapping or touching intervals (one ends where the next starts) are merged; the result is a new list of tuples sorted by start; the input list is not changed; an empty list gives []. Pass bar: your test must pass the correct version and catch at least 4 of the 6 broken versions.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.