Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Sources/DevTesting/Documentation.docc/RandomValueGenerating.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

### Numeric Types

- ``random(_:in:)-2kjt9``
- ``random(_:in:)-45odm``
- ``random(_:in:)-7nmrc``
- ``random(_:in:)-6rzfj``
- ``randomFloat64(in:)-(Range<Float64>)``
- ``randomFloat64(in:)-(ClosedRange<Float64>)``
- ``randomInt(in:)-(Range<Int>)``
- ``randomInt(in:)-(ClosedRange<Int>)``
- ``random(_:in:)-(_,Range<Integer>)``
- ``random(_:in:)-(_,ClosedRange<Integer>)``
- ``random(_:in:)-(_,Range<FloatingPoint>)``
- ``random(_:in:)-(_,ClosedRange<FloatingPoint>)``


### Strings
Expand All @@ -42,3 +46,4 @@
- ``randomBool()``
- ``randomData(count:)``
- ``randomOptional(_:)``
- ``randomUUID()``
Original file line number Diff line number Diff line change
Expand Up @@ -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())
///
Expand Down Expand Up @@ -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
)
}
Expand All @@ -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>) -> Float64 {
return random(Float64.self, in: range)
}


/// Returns a random binary floating point of the specified type within the specified range.
///
/// - Parameters:
Expand All @@ -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>) -> Float64 {
return random(Float64.self, in: range)
}


/// Returns a random integer of the specified type within the specified range.
///
/// - Parameters:
Expand All @@ -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>) -> Int {
return random(Int.self, in: range)
}


/// Returns a random integer of the specified type within the specified range.
///
/// - Parameters:
Expand All @@ -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>) -> Int {
return random(Int.self, in: range)
}


// MARK: - Optionals

/// Randomly returns a value or `nil`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Loading