Skip to content

Commit

Permalink
Cleanup. Borrowing. Overallocation error messages (#120).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Oct 24, 2024
1 parent 5f13a8e commit 48891b0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
6 changes: 3 additions & 3 deletions Sources/CoreKit/BinaryInteger+Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ extension BinaryInteger {
///
@inlinable public static func random(in range: Range<Self>, using randomness: inout some Randomness) -> Optional<Self> {
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
}

//=------------------------------------------------------------------------=
Expand All @@ -60,7 +60,7 @@ extension BinaryInteger {
///
@inlinable public static func random(through index: Shift<Magnitude>, 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) {
Expand Down
18 changes: 11 additions & 7 deletions Sources/CoreKit/Randomness+Range.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ extension Randomness {
///
@inlinable internal mutating func systems<T>(
through limit: T
) -> T where T: UnsignedInteger {
) -> T where T: UnsignedInteger {

if let end = limit.incremented().optional() {
return self.systems(upTo: Nonzero(unchecked: end))
Expand All @@ -104,7 +104,7 @@ extension Randomness {
///
@inlinable internal mutating func systems<T>(
upTo limit: Nonzero<T>
) -> 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())
Expand Down Expand Up @@ -135,23 +135,27 @@ extension Randomness {
///
/// Arbitrary integers accept-reject random bit patterns.
///
@inline(never) @inlinable internal mutating func arbitrary<T>(upTo comparison: Signum, relativeTo limit: /*borrowing*/ T) -> T where T: UnsignedInteger {
@inline(never) @inlinable internal mutating func arbitrary<T>(
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()
Expand Down
12 changes: 11 additions & 1 deletion Sources/CoreKit/Stdlib/Swift+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)"
}
}
3 changes: 1 addition & 2 deletions Sources/TestKit2/Utilities+Shift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ extension Shift where Target: SystemsInteger {
// MARK: Metadata
//=------------------------------------------------------------------------=

@inlinable public static var all: some Sequence<Self> {
@inlinable public static var all: LazyMapSequence<Range<IX>, Self> {
let range = IX.zero..<IX(size: Target.self)
return range.lazy.map {
Self(unchecked: Count(Natural(unchecked: $0)))
}
}

}
8 changes: 4 additions & 4 deletions Tests/UltimathnumTests/BinaryInteger+Bitwise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ import TestKit2
func whereIs<T>(_ 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()
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions Tests/UltimathnumTests/BinaryInteger+Multiplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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<T>

if let expectation = expectation.optional() {
Expand All @@ -499,7 +498,6 @@ import TestKit2
func whereIs<T>(_ 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<T>

if let expectation = expectation.optional() {
Expand All @@ -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<T>

try #require(expectation.optional() == lhs.times(rhs) as T)
Expand All @@ -548,7 +545,6 @@ import TestKit2
func whereIs<T>(_ 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<T>

try #require(expectation.optional() == lhs.times(lhs) as T)
Expand Down

0 comments on commit 48891b0

Please sign in to comment.