Test gap in circular-buffer

Hi there! While solving circular-buffer on the C++ track I noticed my solution passed the full test suite despite having a bug in the clear function.

Every test case that calls clear does so either on a capacity-1 buffer (where the read and write positions can never differ, since every index is 0 mod 1) or on a freshly-constructed buffer (positions still aligned at 0). So an implementation whose clear only resets the element count, without realigning the read/write positions, passes the entire suite.

Demonstration (capacity 2, counter-only clear):

write(1)  → slot0=1, write_pos=1, count=1
read()→1  → read_pos=1, count=0
write(2)  → slot1=2, write_pos=0 (wrapped), count=1
clear()   → count=0, but read_pos=1 / write_pos=0 remain diverged
write(3)  → slot0=3, write_pos=1, count=1
read()    → returns stale item 2 from slot1 instead of 3   ← bug survives the suite

Proposed additional case (fails the buggy implementation above, passes a correct
one; happy to open a PR against exercises/circular-buffer/canonical-data.json if
maintainers agree it’s worth adding):

{
  "uuid": "0e8afd9e-b7fe-4bcb-b2a2-b0d63ea90cf7",
  "description": "clear works when read and write positions have diverged",
  "property": "run",
  "input": {
    "capacity": 2,
    "operations": [
      { "operation": "write", "item": 1, "should_succeed": true },
      { "operation": "read", "should_succeed": true, "expected": 1 },
      { "operation": "write", "item": 2, "should_succeed": true },
      { "operation": "clear" },
      { "operation": "write", "item": 3, "should_succeed": true },
      { "operation": "read", "should_succeed": true, "expected": 3 }
    ]
  },
  "expected": {}
}

The operation sequence is the shortest one I could find that forces the positions
apart before the clear (a plain write, clear isn’t enough at any capacity, since
positions only diverge after a wraparound or an interleaved read).

Happy to adjust the description wording or the scenario if there’s a preferred style.
Cheers!

Sounds like a great addition to me! Also thanks for the good description of the gap and how the proposed test addresses it.

1 Like

Thanks for the feedback! Will go ahead and make the pr

Please wait for at least 3 maintainers supporting your suggestion. Me is one of them.

I’m not sold on this being a problem that needs fixing. It seems to be targeting a pretty specific bug in a specific implementation. If this bug is somewhat niche, we don’t need to test for it. The tests aren’t meant to be fully exhaustive. The purpose of the test is to guide students to a reasonable solution.

+1 for adding this test. Clear should always realign the write/read positions, otherwise the state gets corrupted. Therefore, it’s a key part of the buffer’s behavior.