From e01391e56231fe580b90e9267e92d18f8f9e8884 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 31 Jan 2024 17:09:32 -0800 Subject: [PATCH] [BitArray] Fix logic error in toggleAll() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit D’oh — lucky the test coverage caught this; unlucky that the test wasn’t deterministic, and the chances of this occurring were quite low. --- .../BitCollections/BitArray/BitArray+BitwiseOperations.swift | 1 + Tests/BitCollectionsTests/BitArrayTests.swift | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/BitCollections/BitArray/BitArray+BitwiseOperations.swift b/Sources/BitCollections/BitArray/BitArray+BitwiseOperations.swift index 0b2c1db4..2db5d961 100644 --- a/Sources/BitCollections/BitArray/BitArray+BitwiseOperations.swift +++ b/Sources/BitCollections/BitArray/BitArray+BitwiseOperations.swift @@ -166,6 +166,7 @@ extension BitArray { public mutating func toggleAll(in range: Range) { precondition(range.upperBound <= count, "Range out of bounds") + guard !range.isEmpty else { return } _update { handle in let words = handle._mutableWords let start = _BitPosition(range.lowerBound) diff --git a/Tests/BitCollectionsTests/BitArrayTests.swift b/Tests/BitCollectionsTests/BitArrayTests.swift index 6072132f..e615d5ca 100644 --- a/Tests/BitCollectionsTests/BitArrayTests.swift +++ b/Tests/BitCollectionsTests/BitArrayTests.swift @@ -906,7 +906,7 @@ final class BitArrayTests: CollectionTestCase { } func test_toggleAll_range() { - withSome("count", in: 0 ..< 512, maxSamples: 50) { count in + withEvery("count", in: [0, 10, 64, 90, 127, 128, 129]) { count in let a = randomBoolArray(count: count) withSomeRanges("range", in: 0 ..< count, maxSamples: 100) { range in