mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
Merge pull request #736 from Denimworld12/fix-bitset-counted-update-count
[BitCollections] Fix BitSet.Counted.update(with:) not maintaining the cached count
This commit is contained in:
2 files changed
+27
-1
No files matched your search
@@ -338,7 +338,9 @@ extension BitSet.Counted: SetAlgebra {
|
||||
|
||||
@discardableResult
|
||||
public mutating func update(with newMember: Int) -> Int? {
|
||||
_bits.update(with: newMember)
|
||||
let old = _bits.update(with: newMember)
|
||||
if old == nil { _count += 1 }
|
||||
return old
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
|
||||
@@ -237,6 +237,30 @@ final class BitSetCountedTests: CollectionTestCase {
|
||||
expectFalse(a.isStrictSuperset(of: Set(c)))
|
||||
}
|
||||
|
||||
func test_update() {
|
||||
let count = 100
|
||||
withEvery("seed", in: 0 ..< 10) { seed in
|
||||
var rng = RepeatableRandomNumberGenerator(seed: seed)
|
||||
var actual: BitSet.Counted = []
|
||||
var expected: Set<Int> = []
|
||||
let input = (0 ..< count).shuffled(using: &rng)
|
||||
withEvery("i", in: input.indices) { i in
|
||||
let v = input[i]
|
||||
// Inserting a new member.
|
||||
expectEqual(actual.update(with: v), expected.update(with: v))
|
||||
expectEqual(actual.count, expected.count)
|
||||
// Updating an existing member must not change the count.
|
||||
expectEqual(actual.update(with: v), expected.update(with: v))
|
||||
expectEqual(actual.count, expected.count)
|
||||
if i % 25 == 0 {
|
||||
expectEqualElements(actual, expected.sorted())
|
||||
}
|
||||
}
|
||||
expectEqualElements(actual, expected.sorted())
|
||||
expectEqual(actual.count, expected.count)
|
||||
}
|
||||
}
|
||||
|
||||
func test_isDisjoint() {
|
||||
let a: BitSet.Counted = [1, 2, 3, 4]
|
||||
|
||||
|
||||
Reference in new issue
Block a user