From 48891b025d3461e42db3e8ca2f2a3999b683854c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Bystr=C3=B6m=20Ericsson?= Date: Thu, 24 Oct 2024 07:30:17 +0200 Subject: [PATCH] Cleanup. Borrowing. Overallocation error messages (#120). --- Sources/CoreKit/BinaryInteger+Random.swift | 6 +++--- Sources/CoreKit/Randomness+Range.swift | 18 +++++++++++------- Sources/CoreKit/Stdlib/Swift+String.swift | 12 +++++++++++- Sources/TestKit2/Utilities+Shift.swift | 3 +-- .../BinaryInteger+Bitwise.swift | 8 ++++---- .../BinaryInteger+Multiplication.swift | 6 +----- 6 files changed, 31 insertions(+), 22 deletions(-) diff --git a/Sources/CoreKit/BinaryInteger+Random.swift b/Sources/CoreKit/BinaryInteger+Random.swift index 2574731a..fa8f10ee 100644 --- a/Sources/CoreKit/BinaryInteger+Random.swift +++ b/Sources/CoreKit/BinaryInteger+Random.swift @@ -32,8 +32,8 @@ extension BinaryInteger { /// @inlinable public static func random(in range: Range, using randomness: inout some Randomness) -> Optional { let distance = Magnitude.init(raw: range.upperBound.minus(range.lowerBound).value) - guard let distance = Nonzero(exactly: distance) else { return nil } - return Self(raw: randomness.next(upTo: distance)).plus(range.lowerBound).value + guard let distance = Nonzero(exactly:/**/distance) else { return nil } + return Self(raw: randomness.next(upTo:/**/distance)).plus(range.lowerBound).value } //=------------------------------------------------------------------------= @@ -60,7 +60,7 @@ extension BinaryInteger { /// @inlinable public static func random(through index: Shift, using randomness: inout some Randomness) -> Self { guard let index: IX = index.natural().optional() else { - Swift.preconditionFailure(String.indexOutOfBounds()) + Swift.preconditionFailure(String.overallocation()) } if let size = IX(size: Self.self) { diff --git a/Sources/CoreKit/Randomness+Range.swift b/Sources/CoreKit/Randomness+Range.swift index 3f044066..e13f6dd2 100644 --- a/Sources/CoreKit/Randomness+Range.swift +++ b/Sources/CoreKit/Randomness+Range.swift @@ -82,7 +82,7 @@ extension Randomness { /// @inlinable internal mutating func systems( through limit: T - ) -> T where T: UnsignedInteger { + ) -> T where T: UnsignedInteger { if let end = limit.incremented().optional() { return self.systems(upTo: Nonzero(unchecked: end)) @@ -104,7 +104,7 @@ extension Randomness { /// @inlinable internal mutating func systems( upTo limit: Nonzero - ) -> T where T: UnsignedInteger { + ) -> T where T: UnsignedInteger { // product.low = product % (2 ** T.size) // product.high = product / (2 ** T.size) var product = limit.value.multiplication(self.systems()) @@ -135,23 +135,27 @@ extension Randomness { /// /// Arbitrary integers accept-reject random bit patterns. /// - @inline(never) @inlinable internal mutating func arbitrary(upTo comparison: Signum, relativeTo limit: /*borrowing*/ T) -> T where T: UnsignedInteger { + @inline(never) @inlinable internal mutating func arbitrary( + upTo comparison: Signum, + relativeTo limit: borrowing T + ) -> T where T: UnsignedInteger { + if limit.isInfinite { - Swift.preconditionFailure(String.overflow()) + Swift.preconditionFailure(String.overallocation()) } if comparison.isNegative { Swift.assertionFailure(String.brokenInvariant()) } - if comparison.isZero { - Swift.assert(!limit.isZero) + if comparison.isZero, limit.isZero { + Swift.assertionFailure(String.brokenInvariant()) } return (limit).withUnsafeBinaryIntegerBody { let limit = (consume $0).normalized() // TODO: req. normalized body, maybe? - return T.arbitrary(uninitialized: limit.count, repeating: .zero) { body in + return T.arbitrary(uninitialized: limit.count, repeating: Bit.zero) { body in guard !body.isEmpty else { return } let lastIndex = body.count.decremented().unchecked() diff --git a/Sources/CoreKit/Stdlib/Swift+String.swift b/Sources/CoreKit/Stdlib/Swift+String.swift index f7d36a08..67c8085c 100644 --- a/Sources/CoreKit/Stdlib/Swift+String.swift +++ b/Sources/CoreKit/Stdlib/Swift+String.swift @@ -16,7 +16,7 @@ extension String { //=------------------------------------------------------------------------= // MARK: Initializers //=------------------------------------------------------------------------= - + /// A message describing the location of a broken invariant. @inlinable package static func brokenInvariant( function: StaticString = #function, @@ -43,4 +43,14 @@ extension String { ) -> String { "overflow in \(function) at \(file):\(line)" } + + + /// A message describing the location of an overallocation error. + @inlinable package static func overallocation( + function: StaticString = #function, + file: StaticString = #file, + line: UInt = #line + ) -> String { + "overallocation in \(function) at \(file):\(line)" + } } diff --git a/Sources/TestKit2/Utilities+Shift.swift b/Sources/TestKit2/Utilities+Shift.swift index a9b37c7f..0d3d3ee5 100644 --- a/Sources/TestKit2/Utilities+Shift.swift +++ b/Sources/TestKit2/Utilities+Shift.swift @@ -48,11 +48,10 @@ extension Shift where Target: SystemsInteger { // MARK: Metadata //=------------------------------------------------------------------------= - @inlinable public static var all: some Sequence { + @inlinable public static var all: LazyMapSequence, Self> { let range = IX.zero..(_ type: T.Type) throws where T: BinaryInteger { for _ in 0 ..< 4 { var value = T.entropic(through: Shift.max(or: 255), using: &randomness) - ((((value)))) |= 1 - var isOdd = (true) + value = value | 1 + var expectation = true try withOnlyOneCallToRequire(value) { require in for _ in U8.all { - require(value.lsb == Bit(isOdd)) + require(value.lsb == Bit(expectation)) value = value.incremented().value - isOdd.toggle() + expectation.toggle() } } } diff --git a/Tests/UltimathnumTests/BinaryInteger+Multiplication.swift b/Tests/UltimathnumTests/BinaryInteger+Multiplication.swift index 52271442..d8b0d536 100644 --- a/Tests/UltimathnumTests/BinaryInteger+Multiplication.swift +++ b/Tests/UltimathnumTests/BinaryInteger+Multiplication.swift @@ -429,8 +429,8 @@ import TestKit2 for _ in 0 ..< 32 { let lhs = T.entropic(through: Shift.max(or: 255), using: &randomness) let rhs = T.zero.toggled() - var expectation = lhs.negated() + if !T.isSigned, lhs <= T.lsb { expectation.error = false // 0 or 1 times T.max } @@ -478,7 +478,6 @@ import TestKit2 for _ in 0 ..< 32 { let lhs = T.entropic(through: Shift.max(or: 255), using: &randomness) let rhs = T.entropic(through: Shift.max(or: 255), using: &randomness) - let expectation = lhs.times(rhs) as Fallible if let expectation = expectation.optional() { @@ -499,7 +498,6 @@ import TestKit2 func whereIs(_ type: T.Type) throws where T: BinaryInteger { for _ in 0 ..< 32 { let lhs = T.entropic(through: Shift.max(or: 255), using: &randomness) - let expectation = lhs.squared() as Fallible if let expectation = expectation.optional() { @@ -525,7 +523,6 @@ import TestKit2 for _ in 0 ..< 32 { let lhs = T.entropic(size: 256, using: &randomness) let rhs = T.entropic(size: 256, using: &randomness) - let expectation = lhs.times(rhs) as Fallible try #require(expectation.optional() == lhs.times(rhs) as T) @@ -548,7 +545,6 @@ import TestKit2 func whereIs(_ type: T.Type) throws where T: ArbitraryIntegerAsSigned { for _ in 0 ..< 32 { let lhs = T.entropic(size: 256, using: &randomness) - let expectation = lhs.squared() as Fallible try #require(expectation.optional() == lhs.times(lhs) as T)