Merge pull request #628 from lorentey/fix-rigidset-insert

[BasicContainers] RigidSet.insert(maximumCount:from:): Fix spurious capacity overflow caused by incorrect accounting
This commit is contained in:
Karoy Lorentey authored and GitHub committed 2026-04-22 15:48:02 -07:00
commit b0a7a4df1d
2 files changed
+31 -1

No files matched your search

@@ -198,10 +198,10 @@ extension RigidSet where Element: ~Copyable {
while remainder > 0 {
var span = drain.drainNext(maximumCount: remainder)
guard !span.isEmpty else { break }
remainder &-= span.count
while let next = span.popFirst() {
self.insert(next)
}
remainder &-= span.count
}
}
#endif
@@ -428,6 +428,36 @@ class RigidSetTests: CollectionTestCase {
}
}
}
func test_insert_drain_maximumCount() {
withEvery("capacity", in: [5, 10, 100]) { capacity in
withEvery("drainLength", in: [0, 1, 2, 4, 10, capacity]) { drainLength in
withEvery("maxCount", in: [0, 1, 2, 3, 5]) { maxCount in
withEvery("chunkSize", in: [1, 2, 3, 10]) { chunkSize in
withLifetimeTracking { tracker in
var s = RigidSet<LifetimeTracked<Int>>(capacity: capacity)
var i = 0
var drain = CustomDrain<LifetimeTracked<Int>>(
underestimatedCount: 0,
chunkSize: chunkSize
) {
guard i < drainLength else { return nil }
defer { i += 1 }
return tracker.instance(for: i)
}
s.insert(maximumCount: maxCount, from: &drain)
expectConsistentSet(s)
let expectedCount = Swift.min(maxCount, drainLength)
expectEqual(s.count, expectedCount)
expectEqual(i, expectedCount)
}
}
}
}
}
}
#endif
func test_remove_one() {