Skip to content

Commit efc4719

Browse files
Prachi Gauriarprachigauriar
authored andcommitted
Add convenience functions for generating random Ints and Float64s
1 parent 85bacfe commit efc4719

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

Sources/DevTesting/Documentation.docc/RandomValueGenerating.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717

1818
### Numeric Types
1919

20-
- ``random(_:in:)-2kjt9``
21-
- ``random(_:in:)-45odm``
22-
- ``random(_:in:)-7nmrc``
23-
- ``random(_:in:)-6rzfj``
20+
- ``randomFloat64(in:)-(Range<Float64>)``
21+
- ``randomFloat64(in:)-(ClosedRange<Float64>)``
22+
- ``randomInt(in:)-(Range<Int>)``
23+
- ``randomInt(in:)-(ClosedRange<Int>)``
24+
- ``random(_:in:)-(_,Range<Integer>)``
25+
- ``random(_:in:)-(_,ClosedRange<Integer>)``
26+
- ``random(_:in:)-(_,Range<FloatingPoint>)``
27+
- ``random(_:in:)-(_,ClosedRange<FloatingPoint>)``
2428

2529

2630
### Strings
@@ -42,3 +46,4 @@
4246
- ``randomBool()``
4347
- ``randomData(count:)``
4448
- ``randomOptional(_:)``
49+
- ``randomUUID()``

Sources/DevTesting/Random Value Generation/RandomValueGenerating.swift

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ private let randomizationLogger = Logger(subsystem: "DevTesting", category: "ran
2525
/// @Test
2626
/// mutating func testSomething() {
2727
/// let string = randomBasicLatinString()
28-
/// let int = random(Int.self, in: 0 ... 10)
28+
/// let int = randomInt(in: 0 ... 10)
29+
/// let uin8 = random(UInt8.self, in: .min ... .max)
2930
/// let bool = randomBool()
3031
/// let optional = randomOptional(randomAlphanumericString())
3132
///
@@ -128,7 +129,7 @@ extension RandomValueGenerating {
128129
/// 16 and 128 will be chosen.
129130
public mutating func randomData(count: Int? = nil) -> Data {
130131
return Data.random(
131-
count: count ?? random(Int.self, in: 16 ... 128),
132+
count: count ?? randomInt(in: 16 ... 128),
132133
using: &randomNumberGenerator
133134
)
134135
}
@@ -150,6 +151,18 @@ extension RandomValueGenerating {
150151
}
151152

152153

154+
/// Returns a random `Float64` (`Double`) within the specified range.
155+
///
156+
/// This function is provided as a convenience. It is equivalent to calling
157+
///
158+
/// random(Float64.self, in: range)
159+
///
160+
/// - Parameter range: The half-open range in which to create a random value.
161+
public mutating func randomFloat64(in range: Range<Float64>) -> Float64 {
162+
return random(Float64.self, in: range)
163+
}
164+
165+
153166
/// Returns a random binary floating point of the specified type within the specified range.
154167
///
155168
/// - Parameters:
@@ -164,6 +177,18 @@ extension RandomValueGenerating {
164177
}
165178

166179

180+
/// Returns a random `Float64` (`Double`) within the specified range.
181+
///
182+
/// This function is provided as a convenience. It is equivalent to calling
183+
///
184+
/// random(Float64.self, in: range)
185+
///
186+
/// - Parameter range: The closed range in which to create a random value.
187+
public mutating func randomFloat64(in range: ClosedRange<Float64>) -> Float64 {
188+
return random(Float64.self, in: range)
189+
}
190+
191+
167192
/// Returns a random integer of the specified type within the specified range.
168193
///
169194
/// - Parameters:
@@ -178,6 +203,18 @@ extension RandomValueGenerating {
178203
}
179204

180205

206+
/// Returns a random `Int` within the specified range.
207+
///
208+
/// This function is provided as a convenience. It is equivalent to calling
209+
///
210+
/// random(Int.self, in: range)
211+
///
212+
/// - Parameter range: The half-open range in which to create a random value.
213+
public mutating func randomInt(in range: Range<Int>) -> Int {
214+
return random(Int.self, in: range)
215+
}
216+
217+
181218
/// Returns a random integer of the specified type within the specified range.
182219
///
183220
/// - Parameters:
@@ -192,6 +229,18 @@ extension RandomValueGenerating {
192229
}
193230

194231

232+
/// Returns a random `Int` within the specified range.
233+
///
234+
/// This function is provided as a convenience. It is equivalent to calling
235+
///
236+
/// random(Int.self, in: range)
237+
///
238+
/// - Parameter range: The closed range in which to create a random value.
239+
public mutating func randomInt(in range: ClosedRange<Int>) -> Int {
240+
return random(Int.self, in: range)
241+
}
242+
243+
195244
// MARK: - Optionals
196245

197246
/// Randomly returns a value or `nil`.

Tests/DevTestingTests/Random Value Generation/RandomValueGeneratingTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ struct RandomValueGeneratingTests {
103103
}
104104

105105

106+
@Test
107+
mutating func randomFloat64UsesRandomNumberGenerator_halfOpenRange() {
108+
for _ in iterationRange {
109+
let actualFloat64 = generator.randomFloat64(in: -100_000 ..< 100_000)
110+
let expectedFloat64 = Float64.randomPrintable(in: -100_000 ..< 100_000, using: &rng)
111+
#expect(actualFloat64 == expectedFloat64)
112+
}
113+
}
114+
115+
106116
@Test
107117
mutating func randomFloatUsesRandomNumberGenerator_closedRange() {
108118
for _ in iterationRange {
@@ -121,6 +131,16 @@ struct RandomValueGeneratingTests {
121131
}
122132

123133

134+
@Test
135+
mutating func randomFloat64UsesRandomNumberGenerator_closedRange() {
136+
for _ in iterationRange {
137+
let actualFloat64 = generator.randomFloat64(in: -100_000 ... 100_000)
138+
let expectedFloat64 = Float64.randomPrintable(in: -100_000 ... 100_000, using: &rng)
139+
#expect(actualFloat64 == expectedFloat64)
140+
}
141+
}
142+
143+
124144
@Test
125145
mutating func randomIntegertUsesRandomNumberGenerator_halfOpenRange() {
126146
for _ in iterationRange {
@@ -167,6 +187,16 @@ struct RandomValueGeneratingTests {
167187
}
168188

169189

190+
@Test
191+
mutating func randomIntUsesRandomNumberGenerator_halfOpenRange() {
192+
for _ in iterationRange {
193+
let actualInt = generator.randomInt(in: -1_000_000_000 ..< 1_000_000_000)
194+
let expectedInt = Int.random(in: -1_000_000_000 ..< 1_000_000_000, using: &rng)
195+
#expect(actualInt == expectedInt)
196+
}
197+
}
198+
199+
170200
@Test
171201
mutating func randomIntegerRandomNumberGenerator_closedRange() {
172202
for _ in iterationRange {
@@ -213,6 +243,16 @@ struct RandomValueGeneratingTests {
213243
}
214244

215245

246+
@Test
247+
mutating func randomIntUsesRandomNumberGenerator_closedRange() {
248+
for _ in iterationRange {
249+
let actualInt = generator.randomInt(in: -1_000_000_000 ... 1_000_000_000)
250+
let expectedInt = Int.random(in: -1_000_000_000 ... 1_000_000_000, using: &rng)
251+
#expect(actualInt == expectedInt)
252+
}
253+
}
254+
255+
216256
@Test
217257
mutating func randomOptionalUsesRandomNumberGenerator() {
218258
for _ in iterationRange {

0 commit comments

Comments
 (0)