diff --git a/Sources/DevTesting/Documentation.docc/RandomValueGenerating.md b/Sources/DevTesting/Documentation.docc/RandomValueGenerating.md index cbbf3da..b3a2682 100644 --- a/Sources/DevTesting/Documentation.docc/RandomValueGenerating.md +++ b/Sources/DevTesting/Documentation.docc/RandomValueGenerating.md @@ -17,10 +17,14 @@ ### Numeric Types -- ``random(_:in:)-2kjt9`` -- ``random(_:in:)-45odm`` -- ``random(_:in:)-7nmrc`` -- ``random(_:in:)-6rzfj`` +- ``randomFloat64(in:)-(Range)`` +- ``randomFloat64(in:)-(ClosedRange)`` +- ``randomInt(in:)-(Range)`` +- ``randomInt(in:)-(ClosedRange)`` +- ``random(_:in:)-(_,Range)`` +- ``random(_:in:)-(_,ClosedRange)`` +- ``random(_:in:)-(_,Range)`` +- ``random(_:in:)-(_,ClosedRange)`` ### Strings @@ -42,3 +46,4 @@ - ``randomBool()`` - ``randomData(count:)`` - ``randomOptional(_:)`` +- ``randomUUID()`` diff --git a/Sources/DevTesting/Random Value Generation/RandomValueGenerating.swift b/Sources/DevTesting/Random Value Generation/RandomValueGenerating.swift index 1d7cd72..77ede52 100644 --- a/Sources/DevTesting/Random Value Generation/RandomValueGenerating.swift +++ b/Sources/DevTesting/Random Value Generation/RandomValueGenerating.swift @@ -25,7 +25,8 @@ private let randomizationLogger = Logger(subsystem: "DevTesting", category: "ran /// @Test /// mutating func testSomething() { /// let string = randomBasicLatinString() -/// let int = random(Int.self, in: 0 ... 10) +/// let int = randomInt(in: 0 ... 10) +/// let uin8 = random(UInt8.self, in: .min ... .max) /// let bool = randomBool() /// let optional = randomOptional(randomAlphanumericString()) /// @@ -128,7 +129,7 @@ extension RandomValueGenerating { /// 16 and 128 will be chosen. public mutating func randomData(count: Int? = nil) -> Data { return Data.random( - count: count ?? random(Int.self, in: 16 ... 128), + count: count ?? randomInt(in: 16 ... 128), using: &randomNumberGenerator ) } @@ -150,6 +151,18 @@ extension RandomValueGenerating { } + /// Returns a random `Float64` (`Double`) within the specified range. + /// + /// This function is provided as a convenience. It is equivalent to calling + /// + /// random(Float64.self, in: range) + /// + /// - Parameter range: The half-open range in which to create a random value. + public mutating func randomFloat64(in range: Range) -> Float64 { + return random(Float64.self, in: range) + } + + /// Returns a random binary floating point of the specified type within the specified range. /// /// - Parameters: @@ -164,6 +177,18 @@ extension RandomValueGenerating { } + /// Returns a random `Float64` (`Double`) within the specified range. + /// + /// This function is provided as a convenience. It is equivalent to calling + /// + /// random(Float64.self, in: range) + /// + /// - Parameter range: The closed range in which to create a random value. + public mutating func randomFloat64(in range: ClosedRange) -> Float64 { + return random(Float64.self, in: range) + } + + /// Returns a random integer of the specified type within the specified range. /// /// - Parameters: @@ -178,6 +203,18 @@ extension RandomValueGenerating { } + /// Returns a random `Int` within the specified range. + /// + /// This function is provided as a convenience. It is equivalent to calling + /// + /// random(Int.self, in: range) + /// + /// - Parameter range: The half-open range in which to create a random value. + public mutating func randomInt(in range: Range) -> Int { + return random(Int.self, in: range) + } + + /// Returns a random integer of the specified type within the specified range. /// /// - Parameters: @@ -192,6 +229,18 @@ extension RandomValueGenerating { } + /// Returns a random `Int` within the specified range. + /// + /// This function is provided as a convenience. It is equivalent to calling + /// + /// random(Int.self, in: range) + /// + /// - Parameter range: The closed range in which to create a random value. + public mutating func randomInt(in range: ClosedRange) -> Int { + return random(Int.self, in: range) + } + + // MARK: - Optionals /// Randomly returns a value or `nil`. diff --git a/Tests/DevTestingTests/Random Value Generation/RandomValueGeneratingTests.swift b/Tests/DevTestingTests/Random Value Generation/RandomValueGeneratingTests.swift index f95d1e3..614ad1f 100644 --- a/Tests/DevTestingTests/Random Value Generation/RandomValueGeneratingTests.swift +++ b/Tests/DevTestingTests/Random Value Generation/RandomValueGeneratingTests.swift @@ -103,6 +103,16 @@ struct RandomValueGeneratingTests { } + @Test + mutating func randomFloat64UsesRandomNumberGenerator_halfOpenRange() { + for _ in iterationRange { + let actualFloat64 = generator.randomFloat64(in: -100_000 ..< 100_000) + let expectedFloat64 = Float64.randomPrintable(in: -100_000 ..< 100_000, using: &rng) + #expect(actualFloat64 == expectedFloat64) + } + } + + @Test mutating func randomFloatUsesRandomNumberGenerator_closedRange() { for _ in iterationRange { @@ -121,6 +131,16 @@ struct RandomValueGeneratingTests { } + @Test + mutating func randomFloat64UsesRandomNumberGenerator_closedRange() { + for _ in iterationRange { + let actualFloat64 = generator.randomFloat64(in: -100_000 ... 100_000) + let expectedFloat64 = Float64.randomPrintable(in: -100_000 ... 100_000, using: &rng) + #expect(actualFloat64 == expectedFloat64) + } + } + + @Test mutating func randomIntegertUsesRandomNumberGenerator_halfOpenRange() { for _ in iterationRange { @@ -167,6 +187,16 @@ struct RandomValueGeneratingTests { } + @Test + mutating func randomIntUsesRandomNumberGenerator_halfOpenRange() { + for _ in iterationRange { + let actualInt = generator.randomInt(in: -1_000_000_000 ..< 1_000_000_000) + let expectedInt = Int.random(in: -1_000_000_000 ..< 1_000_000_000, using: &rng) + #expect(actualInt == expectedInt) + } + } + + @Test mutating func randomIntegerRandomNumberGenerator_closedRange() { for _ in iterationRange { @@ -213,6 +243,16 @@ struct RandomValueGeneratingTests { } + @Test + mutating func randomIntUsesRandomNumberGenerator_closedRange() { + for _ in iterationRange { + let actualInt = generator.randomInt(in: -1_000_000_000 ... 1_000_000_000) + let expectedInt = Int.random(in: -1_000_000_000 ... 1_000_000_000, using: &rng) + #expect(actualInt == expectedInt) + } + } + + @Test mutating func randomOptionalUsesRandomNumberGenerator() { for _ in iterationRange {