Skip to content

Commit

Permalink
[Tests] BinaryInteger.random(in:using:).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Jul 26, 2024
1 parent ab296e7 commit fc44e91
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion Tests/UltimathnumTests/BinaryInteger+Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import TestKit
final class BinaryIntegerTestsOnRandom: XCTestCase {

//=------------------------------------------------------------------------=
// MARK: Tests
// MARK: Tests x Range
//=------------------------------------------------------------------------=

/// - Note: The bounds may be infinite, but not their distance.
Expand Down Expand Up @@ -60,6 +60,41 @@ final class BinaryIntegerTestsOnRandom: XCTestCase {
}
}

func testRandomInRangeHasKnownBounds() {
func whereIs<T>(_ type: T.Type, randomness: consuming FuzzerInt) where T: BinaryInteger {
func check(_ expectation: Range<T>) {
guard !expectation.isEmpty else { return }
let last: T = expectation.upperBound - 1

var min = false
var max = false

while !(min && max) {
let random = T.random(in: expectation, using: &randomness)!
guard expectation.contains(random) else { break }
if random == expectation.lowerBound { min = true }
if random == (((((((((last))))))))) { max = true }
}

Test().yay(min && max)
}

for index: IX in 0 ... 7 {
let min = T.random(through: Shift(Count(index)), using: &randomness)
let max = T.random(through: Shift(Count(index)), using: &randomness)
check(min <= max ? min..<max : max..<min)
}
}

for type in binaryIntegers {
whereIs(type, randomness: fuzzer)
}
}

//=------------------------------------------------------------------------=
// MARK: Tests x Closed Range
//=------------------------------------------------------------------------=

/// - Note: The bounds may be infinite, but not their distance.
func testRandomInClosedRange() {
func whereIs<T>(_ type: T.Type, randomness: consuming FuzzerInt) where T: BinaryInteger {
Expand Down Expand Up @@ -97,6 +132,38 @@ final class BinaryIntegerTestsOnRandom: XCTestCase {
}
}

func testRandomInClosedRangeHasKnownBounds() {
func whereIs<T>(_ type: T.Type, randomness: consuming FuzzerInt) where T: BinaryInteger {
func check(_ expectation: ClosedRange<T>) {
var min = false
var max = false

while !(min && max) {
let random = T.random(in: expectation, using: &randomness)
guard expectation.contains(random) else { break }
if random == expectation.lowerBound { min = true }
if random == expectation.upperBound { max = true }
}

Test().yay(min && max)
}

for index: IX in 0 ... 7 {
let min = T.random(through: Shift(Count(index)), using: &randomness)
let max = T.random(through: Shift(Count(index)), using: &randomness)
check(min <= max ? min...max : max...min)
}
}

for type in binaryIntegers {
whereIs(type, randomness: fuzzer)
}
}

//=------------------------------------------------------------------------=
// MARK: Tests x Bit Index
//=------------------------------------------------------------------------=

func testRandomThroughBitIndex() {
func whereIs<T>(_ type: T.Type, randomness: consuming FuzzerInt) where T: BinaryInteger {
for index in 0 ..< (T.isArbitrary ? 128 : IX(size: T.self)!) {
Expand Down

0 comments on commit fc44e91

Please sign in to comment.