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!