diff --git a/Tests/UltimathnumTests/BinaryInteger+Random.swift b/Tests/UltimathnumTests/BinaryInteger+Random.swift index cc93f727..54bdd9ab 100644 --- a/Tests/UltimathnumTests/BinaryInteger+Random.swift +++ b/Tests/UltimathnumTests/BinaryInteger+Random.swift @@ -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. @@ -60,6 +60,41 @@ final class BinaryIntegerTestsOnRandom: XCTestCase { } } + func testRandomInRangeHasKnownBounds() { + func whereIs(_ type: T.Type, randomness: consuming FuzzerInt) where T: BinaryInteger { + func check(_ expectation: Range) { + 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..(_ type: T.Type, randomness: consuming FuzzerInt) where T: BinaryInteger { @@ -97,6 +132,38 @@ final class BinaryIntegerTestsOnRandom: XCTestCase { } } + func testRandomInClosedRangeHasKnownBounds() { + func whereIs(_ type: T.Type, randomness: consuming FuzzerInt) where T: BinaryInteger { + func check(_ expectation: ClosedRange) { + 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(_ type: T.Type, randomness: consuming FuzzerInt) where T: BinaryInteger { for index in 0 ..< (T.isArbitrary ? 128 : IX(size: T.self)!) {