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)", "<>") + } + } + }