๐Ÿ”Ž findMin() โ€” Interactive: Prime-Path Coverage & Bug Reveal

Week 3 Practice ยท Q8 (test cases โ†’ prime paths) ยท Q9 (which test reveals the bug)

CFG of findMin() โ€” watch the input trace its path

min starts at 0. Each element: if min > a[i] โ†’ node 4 (update), else โ†’ node 5. Then i++, back to 2.

1 ยท Run an input array

min=0i=0
Pick an array and press Play.

2 ยท Prime-path coverage of your set  0 / 12

3 ยท Test the 4 options

Click an option to load its whole set of input arrays and see coverage. โœ… Answer = c).

The code under test


        
The bug: min starts at 0 โ€” a value that may not be in the array. If every element is > 0, the if never fires, min stays 0, and 0 is returned as the "minimum".

Run a JUnit test

select a test
min=0
expected โ€“
actual โ€“
โœ… Answer: c) testCase3() {6} โ€” the only all-positive input. It triggers the fault (min stays 0), and the assertion assertEquals(6, 0) fails โ†’ bug revealed. Toggle ๐Ÿž buggy โ†’ โœ… fixed above and re-run: testCase3 turns green.

โœ๏ธ Q8 โ€” "which test cases cover all prime paths?" (pen & paper)

  • Don't re-derive 12 prime paths. Instead check the SET has 4 signature inputs:
  • โ‘  an empty array [] โ†’ loop runs 0 times โ†’ covers 1โ†’2โ†’6. No empty array โ‡’ option is WRONG. (kills b)
  • โ‘ก an input whose first element updates min (first element < 0, since min starts at 0) โ†’ covers 1โ†’2โ†’3โ†’4โ†’5. No such case โ‡’ WRONG. (kills a โ€” its arrays all start with 0)
  • โ‘ข an input whose first element does NOT update (first element โ‰ฅ 0) โ†’ covers 1โ†’2โ†’3โ†’5.
  • โ‘ฃ multi-element arrays that loop both the update (3โ†’4) and no-update (3โ†’5) branches โ†’ covers the loop prime paths.
  • Trace shortcut per array: write 1 2, then for each element: node 3, then if min > a[i] add 4 & set min=a[i], else skip; then 5 2. End with 6.
  • Golden rule: min starts at 0 โ†’ an element updates min only when it is below the current min (initially, when it's negative).
  • Winner = the option with empty [] + one negative-first + one positive-first + a mixed array. That's c).

โœ๏ธ Q9 โ€” "which test reveals the bug?" (pen & paper) โ€” the RIP trick

  • Step 1 โ€“ Find the fault. Here: int min = 0; (should be iarr[0]).
  • Step 2 โ€“ Ask "what input makes 0 the wrong answer?" โ†’ an array where every element > 0 (so 0 is smaller than all, but 0 isn't in the array).
  • Step 3 โ€“ A test reveals a bug only if RIP all hold:
    • Reachability โ€” the faulty line runs (always here),
    • Infection โ€” internal state goes wrong (min stays 0 instead of the true min),
    • Propagation โ€” the wrong value reaches the assertion & makes it FAIL.
  • Elimination: any array containing a value โ‰ค 0 accidentally updates min to the real minimum โ†’ bug hidden (passes). An empty array returns 0 and the test expects 0 โ†’ passes. So scan for the all-positive array.
  • Here only {6} is all-positive โ†’ returns 0 โ‰  6 โ†’ testCase3() reveals it.
  • Remember: a passing test never proves correctness. You need a test whose correct output the buggy code cannot produce.

๐Ÿง  The reusable pattern

  • This "min = 0" / "max = 0" / "sum-init" bug is a classic: an accumulator initialized to a constant instead of the first element. The revealing test is always the one where that constant is out of the data's range.
  • For coverage questions: inputs โ†’ execution paths โ†’ check the requirement set. Never eyeball inputs; always trace the path.