Detect Clear during full-buffer iteration #65
}
func TestIterNil(t *testing.T) {
var q *Deque[int]
for range q.Iter() {
t.Fatal("Iter yielded from nil deque")
}
for range q.RIter() {
t.Fatal("RIter yielded from nil deque")
}
}
func TestIterModifyAfterBreak(t *testing.T) {
var q Deque[int]
for i := range 8 {
q.PushBack(i)
}
for range q.Iter() {
q.PushBack(0)
break
}
for range q.RIter() {
q.PushBack(0)
break
}
if q.Len() != 10 {
t.Fatalf("expected length 10, got %d", q.Len())
}
}
return func(yield func(T) bool) {
if q.Len() == 0 {
return
}
return func(yield func(T) bool) {
if q.Len() == 0 {
return
}
Good catch, and the fix does what it intends: Clear on a full deque with head at 0 now panics during iteration instead of yielding zero values.
There are still cases where modification during iteration goes unnoticed:
Clear followed by re-pushing the same number of itemsClear on final iteration.I consider those as acceptable, because while they do alter the _contents_ of a Deque, they do not cause a problem for iteration over legitimate values.
Your fix corrects the case where iteration happens over values that should not be iterated over.
There should also be a check to iterate nothing for a nil Deque. I added that in the suggestions.
:warning: Please install the !'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 100.00%. Comparing base (
7ae3ab2) to head (dfa61c0).:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.
<details><summary>Additional details and impacted files</summary>
@@ Coverage Diff @@
## main #65 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 1 1
Lines 273 279 +6
=========================================
+ Hits 273 279 +6
</details>
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
I checked dfa61c0 with the nil and early-break additions: the full suite and race run both pass (60 cases), as do vet and build on Go 1.26.0. The iterators now skip nil deques. The remaining content changes you listed stay outside this check.
gammazero
rupayon123
Clearing a full deque during
IterorRItercan leave both ring indices unchanged. The current mutation guard then misses the clear and yields zeroed elements from the old buffer.Include the original element count in the existing guard for both directions. Detection still happens before the next yield; early-break and last-yield behavior stay the same.
Go 1.26.0 / macOS arm64 validation: four regressions fail before the fix; all eight new cases pass afterward. Full coverage and race suites pass all 58 leaf cases, with no skips and 100% statement coverage. Vet, build, formatting and tidy checks pass. Additional public checks cover delayed iterator use and the existing break/last-yield timing.
This closes the missing count check; it does not add a general mutation version or change value-only
Setbehavior. Equivalent-shape mutations retain the existing limitations. Hosted CI remains unverified locally.