mirror of
https://github.com/apple/swift-collections
synced 2026-11-29 05:14:20 +00:00
[BitSet] isEqualSet(to: Range<Int>) is wrong for an empty range
`_toUInt()` returns nil whenever a bound is negative, and the guard maps that straight to `false`. That is right for a nonempty range -- a bit set can never hold a negative value -- but an empty range equals the empty set regardless of where its bounds sit, so `BitSet().isEqualSet(to: -5 ..< -5)` answered false. Five of the six `Range<Int>` predicates already handle this: isSubset and isDisjoint clamp, isStrictSubset composes those two, and isSuperset and isStrictSuperset test `other.isEmpty` before converting. isEqualSet was the one that did not, which let a bit set be both a subset and a superset of a range while reporting that it was not equal to it.
This commit is contained in:
1 parent
26f299bd49
commit
85342a6a49
2 files changed
+16
No files matched your search
@@ -41,6 +41,7 @@ extension BitSet {
|
||||
/// - Complexity: O(min(*max*, `other.upperBound`), where *max* is the largest
|
||||
/// member of `self`.
|
||||
public func isEqualSet(to other: Range<Int>) -> Bool {
|
||||
if other.isEmpty { return isEmpty }
|
||||
guard let other = other._toUInt() else { return false }
|
||||
return _read { $0.isEqualSet(to: other) }
|
||||
}
|
||||
|
||||
@@ -1135,6 +1135,21 @@ final class BitSetTest: CollectionTestCase {
|
||||
expectFalse(c.isEqualSet(to: i ..< (j + 1)))
|
||||
}
|
||||
}
|
||||
// An empty range is equal to the empty set, whatever its bounds are.
|
||||
// (Ranges with negative bounds can never round-trip through `_toUInt`.)
|
||||
expectTrue(BitSet().isEqualSet(to: 0 ..< 0))
|
||||
expectTrue(BitSet().isEqualSet(to: -5 ..< -5))
|
||||
expectTrue(BitSet().isEqualSet(to: Int.min ..< Int.min))
|
||||
expectFalse(BitSet([1]).isEqualSet(to: -5 ..< -5))
|
||||
|
||||
// A nonempty range holding negative values is never equal to a bit set.
|
||||
expectFalse(BitSet().isEqualSet(to: -5 ..< 0))
|
||||
expectFalse(BitSet([0, 1, 2]).isEqualSet(to: -5 ..< 3))
|
||||
expectFalse(BitSet([0, 1, 2]).isEqualSet(to: -1 ..< 3))
|
||||
|
||||
// The counted variant forwards to the same implementation.
|
||||
expectTrue(BitSet.Counted().isEqualSet(to: -5 ..< -5))
|
||||
expectFalse(BitSet.Counted([0, 1, 2]).isEqualSet(to: -5 ..< 3))
|
||||
}
|
||||
|
||||
func test_isEqual_to_counted_BitSet() {
|
||||
|
||||
Reference in new issue
Block a user