From 297ffa296b51383235d43292ada0f96ed76c9907 Mon Sep 17 00:00:00 2001 From: Aashish <145881415+aashish254@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:27:56 +0545 Subject: [PATCH] [BitCollections] Fix BitArray.resizingShiftRight leaving stale bits _resizingShiftRight(by:) copied the surviving suffix down to index 0 and then removed `count &- amount` trailing bits instead of `amount`, so it truncated the result to `amount` stale bits rather than keeping `count - amount` valid ones. resizingShiftLeft(by: -n) was affected via the same path, and the API had no test coverage at all. Also fix the doc examples, which demonstrated the resizing shifts with the masking shift function names (and, in one case, a result whose bit count contradicted the documented growth by `amount`). Add regression tests asserting parity with the documented semantics (dropFirst / prepending false values) across word-boundary counts, negative amounts, boundary `amount` values, and the doc examples verbatim. --- .../BitArray/BitArray+Shifts.swift | 16 ++-- Tests/BitCollectionsTests/BitArrayTests.swift | 81 +++++++++++++++++++ 2 files changed, 89 insertions(+), 8 deletions(-) diff --git a/Sources/BitCollections/BitArray/BitArray+Shifts.swift b/Sources/BitCollections/BitArray/BitArray+Shifts.swift index 0693d17b..fe748f5c 100644 --- a/Sources/BitCollections/BitArray/BitArray+Shifts.swift +++ b/Sources/BitCollections/BitArray/BitArray+Shifts.swift @@ -20,12 +20,12 @@ extension BitArray { /// places to the right. /// /// var bits: BitArray = "1110110" - /// bits.maskingShiftLeft(by: 2) + /// bits.resizingShiftLeft(by: 2) /// // bits is now 111011000 - /// bits.maskingShiftLeft(by: -4) + /// bits.resizingShiftLeft(by: -4) /// // bits is now 11101 - /// bits.maskingShiftLeft(by: 8) - /// // bits is now 111010000000 + /// bits.resizingShiftLeft(by: 8) + /// // bits is now 1110100000000 public mutating func resizingShiftLeft(by amount: Int) { guard amount != 0 else { return } if amount > 0 { @@ -44,11 +44,11 @@ extension BitArray { /// then the resulting bit array will be empty. /// /// var bits: BitArray = "1110110" - /// bits.maskingShiftRight(by: 2) + /// bits.resizingShiftRight(by: 2) /// // bits is now 11101 - /// bits.maskingShiftRight(by: -4) + /// bits.resizingShiftRight(by: -4) /// // bits is now 111010000 - /// bits.maskingShiftRight(by: 10) + /// bits.resizingShiftRight(by: 10) /// // bits is now empty /// /// If `amount` is between 0 and `count`, then this has the same effect as @@ -75,7 +75,7 @@ extension BitArray { return } self._copy(from: unsafe Range(uncheckedBounds: (amount, count)), to: 0) - self._removeLast(count &- amount) + self._removeLast(amount) } } diff --git a/Tests/BitCollectionsTests/BitArrayTests.swift b/Tests/BitCollectionsTests/BitArrayTests.swift index 840dfdce..2d020052 100644 --- a/Tests/BitCollectionsTests/BitArrayTests.swift +++ b/Tests/BitCollectionsTests/BitArrayTests.swift @@ -1020,4 +1020,85 @@ final class BitArrayTests: CollectionTestCase { """) } + func test_resizingShiftRight_matches_removeFirst() { + for count in [1, 2, 31, 32, 33, 63, 64, 65, 127, 128, 129, 200] { + let bools = randomBoolArray(count: count) + for amount in 0 ... (count &+ 2) { + var a = BitArray(bools) + a.resizingShiftRight(by: amount) + expectEqual( + a.count, Swift.max(count &- amount, 0), + "count: \(count), amount: \(amount)") + expectEqualElements( + a, bools.dropFirst(Swift.min(amount, count)), + "count: \(count), amount: \(amount)") + } + } + } + + func test_resizingShiftRight_byNegativeAmount_matches_shiftLeft() { + for count in [1, 31, 32, 33, 64, 65, 129] { + let bools = randomBoolArray(count: count) + for amount in stride(from: -1, through: 0 &- count &- 2, by: -1) { + var a = BitArray(bools) + a.resizingShiftRight(by: amount) + expectEqual(a.count, count &- amount, "count: \(count), amount: \(amount)") + expectEqualElements( + a, Array(repeating: false, count: -amount) + bools, + "count: \(count), amount: \(amount)") + } + } + } + + func test_resizingShiftLeft_byPositiveAmount_inserts_false_values() { + for count in [0, 1, 31, 32, 33, 64, 65, 129] { + let bools = randomBoolArray(count: count) + for amount in 1 ... (count &+ 2) { + var a = BitArray(bools) + a.resizingShiftLeft(by: amount) + expectEqual(a.count, count &+ amount, "count: \(count), amount: \(amount)") + expectEqualElements( + a, Array(repeating: false, count: amount) + bools, + "count: \(count), amount: \(amount)") + } + } + } + + func test_resizingShiftLeft_byNegativeAmount_matches_removeFirst() { + for count in [1, 2, 31, 32, 33, 63, 64, 65, 127, 128, 129, 200] { + let bools = randomBoolArray(count: count) + for amount in stride(from: -1, through: 0 &- count &- 2, by: -1) { + var a = BitArray(bools) + a.resizingShiftLeft(by: amount) + expectEqual( + a.count, Swift.max(count &+ amount, 0), + "count: \(count), amount: \(amount)") + expectEqualElements( + a, bools.dropFirst(Swift.min(-amount, count)), + "count: \(count), amount: \(amount)") + } + } + } + + func test_resizingShift_docExamples() { + do { + var bits: BitArray = "1110110" + bits.resizingShiftLeft(by: 2) + expectEqual("\(bits)", "<111011000>") + bits.resizingShiftLeft(by: -4) + expectEqual("\(bits)", "<11101>") + bits.resizingShiftLeft(by: 8) + expectEqual("\(bits)", "<1110100000000>") + } + do { + var bits: BitArray = "1110110" + bits.resizingShiftRight(by: 2) + expectEqual("\(bits)", "<11101>") + bits.resizingShiftRight(by: -4) + expectEqual("\(bits)", "<111010000>") + bits.resizingShiftRight(by: 10) + expectEqual("\(bits)", "<>") + } + } + }