-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGen+Int.swift
More file actions
170 lines (162 loc) · 7.54 KB
/
Copy pathGen+Int.swift
File metadata and controls
170 lines (162 loc) · 7.54 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Adapted from https://github.com/pointfreeco/swift-gen
// Copyright (c) 2019 Point-Free, Inc. MIT License
extension Gen where Value: FixedWidthInteger & Sendable {
/// Produces a generator of random values within the specified range.
///
/// ### Example
/// ```swift
/// Gen<UInt>.value(in: 0 ... 255)
/// ```
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func value(in range: ClosedRange<Value> = Value.min...Value.max) -> Generator<
Value, Shrink.Integer<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<Int8>.value(in: 0...)
/// ```
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random values within the bounds of range.
@inlinable
public static func value(in range: some RangeExpression<Value>) -> Generator<Value, Shrink.Integer<Value>> {
return value(in: ClosedRange(range))
}
/// A generator of random integers within the full range, to be used for option sets.
///
/// The associated shrinker will remove every bit, but will never set bits that are unset in the original value.
@inlinable
public static var bitSet: Generator<Value, Shrink.Bitwise<Value>> { bitSet() }
/// Produces a generator of random integers within the given bit mask.
///
/// The associated shrinker will remove every bit, but will never set bits that are unset in the original value.
/// - Parameter mask: The bit mask to apply to each generated value.
/// - Returns: A generator of random values.
@inlinable
public static func bitSet(mask: Value = ~0) -> Generator<Value, Shrink.Bitwise<Value>> {
return .init(
run: { rng in Value.random(in: .min ... .max, using: &rng) & mask },
shrink: { .init($0) }
)
}
}
extension Gen where Value == Int {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func int(in range: some RangeExpression<Int> = .min ... .max) -> Generator<
Int, Shrink.Integer<Int>
> { value(in: range) }
}
extension Gen where Value == Int8 {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func int8(in range: some RangeExpression<Int8> = .min ... .max) -> Generator<
Int8, Shrink.Integer<Int8>
> { value(in: range) }
}
extension Gen where Value == Int16 {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func int16(in range: some RangeExpression<Int16> = .min ... .max) -> Generator<
Int16, Shrink.Integer<Int16>
> { value(in: range) }
}
extension Gen where Value == Int32 {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func int32(in range: some RangeExpression<Int32> = .min ... .max) -> Generator<
Int32, Shrink.Integer<Int32>
> { value(in: range) }
}
extension Gen where Value == Int64 {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func int64(in range: some RangeExpression<Int64> = .min ... .max) -> Generator<
Int64, Shrink.Integer<Int64>
> { value(in: range) }
}
extension Gen where Value == UInt {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func uint(in range: some RangeExpression<UInt> = .min ... .max) -> Generator<
UInt, Shrink.Integer<UInt>
> { value(in: range) }
}
extension Gen where Value == UInt8 {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func uint8(in range: some RangeExpression<UInt8> = .min ... .max) -> Generator<
UInt8, Shrink.Integer<UInt8>
> { value(in: range) }
}
extension Gen where Value == UInt16 {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func uint16(in range: some RangeExpression<UInt16> = .min ... .max) -> Generator<
UInt16, Shrink.Integer<UInt16>
> { value(in: range) }
}
extension Gen where Value == UInt32 {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func uint32(in range: some RangeExpression<UInt32> = .min ... .max) -> Generator<
UInt32, Shrink.Integer<UInt32>
> { value(in: range) }
}
extension Gen where Value == UInt64 {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func uint64(in range: some RangeExpression<UInt64> = .min ... .max) -> Generator<
UInt64, Shrink.Integer<UInt64>
> { value(in: range) }
}
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
extension Gen where Value == Int128 {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func int128(in range: some RangeExpression<Int128> = .min ... .max) -> Generator<
Int128, Shrink.Integer<Int128>
> { value(in: range) }
}
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
extension Gen where Value == UInt128 {
/// Produces a generator of random integers within the specified range.
///
/// - Parameter range: The range in which to create a random value.
/// - Returns: A generator of random integers within the bounds of range.
@inlinable public static func uint128(in range: some RangeExpression<UInt128> = .min ... .max) -> Generator<
UInt128, Shrink.Integer<UInt128>
> { value(in: range) }
}