-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGen+Float.swift
More file actions
150 lines (138 loc) · 6.06 KB
/
Copy pathGen+Float.swift
File metadata and controls
150 lines (138 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Adapted from https://github.com/pointfreeco/swift-gen
// Copyright (c) 2019 Point-Free, Inc. MIT License
extension Gen where Value: BinaryFloatingPoint & Sendable, Value.RawSignificand: FixedWidthInteger {
/// Produces a generator of random values within the specified range.
///
/// ### Example
/// ```swift
/// Gen<Float16>.value(in: 0 ... 1)
/// ```
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func value(in range: ClosedRange<Value>) -> Generator<Value, Shrink.Floating<Value>> {
return .init(
run: { rng in Value.random(in: range, using: &rng) },
shrink: { $0.shrink(within: range) }
)
}
/// Produces a generator of random values within the specified range.
///
/// ### Example
/// ```swift
/// Gen<Float16>.value(in: 0 ..< 1)
/// ```
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func value(in range: Range<Value>) -> Generator<Value, Shrink.Floating<Value>> {
return .init(
run: { rng in Value.random(in: range, using: &rng) },
shrink: { $0.shrink(within: range) }
)
}
}
extension Gen where Value == Float {
/// Produces a generator of random floats within the specified range.
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func float(in range: ClosedRange<Float>) -> Generator<Float, Shrink.Floating<Float>> {
return value(in: range)
}
/// Produces a generator of random floats within the specified range.
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func float(in range: Range<Float>) -> Generator<Float, Shrink.Floating<Float>> {
return value(in: range)
}
}
extension Gen where Value == Double {
/// Produces a generator of random floats within the specified range.
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func double(in range: ClosedRange<Double>) -> Generator<Double, Shrink.Floating<Double>> {
return value(in: range)
}
/// Produces a generator of random floats within the specified range.
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func double(in range: Range<Double>) -> Generator<Double, Shrink.Floating<Double>> {
return value(in: range)
}
}
#if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
extension Gen where Value == Float16 {
/// Produces a generator of random floats within the specified range.
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func float16(in range: ClosedRange<Float16>) -> Generator<Float16, Shrink.Floating<Float16>> {
return value(in: range)
}
/// Produces a generator of random floats within the specified range.
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func float16(in range: Range<Float16>) -> Generator<Float16, Shrink.Floating<Float16>> {
return value(in: range)
}
}
#endif
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
extension Gen where Value == Float80 {
/// Produces a generator of random floats within the specified range.
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func float80(in range: ClosedRange<Float80>) -> Generator<Float80, Shrink.Floating<Float80>> {
return value(in: range)
}
/// Produces a generator of random floats within the specified range.
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func float80(in range: Range<Float80>) -> Generator<Float80, Shrink.Floating<Float80>> {
return value(in: range)
}
}
#endif
#if canImport(FoundationEssentials)
import FoundationEssentials
#elseif canImport(Foundation)
import Foundation
#endif
#if canImport(FoundationEssentials) || canImport(Foundation)
extension Gen where Value == CGFloat {
/// Produces a generator of random floats within the specified range.
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func cgFloat(in range: ClosedRange<CGFloat>) -> Generator<CGFloat, Shrink.Floating<CGFloat>> {
return value(in: range)
}
/// Produces a generator of random floats within the specified range.
///
/// - Parameter range: The range in which to create a random value. `range` must be finite.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func cgFloat(in range: Range<CGFloat>) -> Generator<CGFloat, Shrink.Floating<CGFloat>> {
return value(in: range)
}
}
#endif