-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGen+Collection.swift
More file actions
231 lines (215 loc) · 7.88 KB
/
Copy pathGen+Collection.swift
File metadata and controls
231 lines (215 loc) · 7.88 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Adapted from https://github.com/pointfreeco/swift-gen
// Copyright (c) 2019 Point-Free, Inc. MIT License
#if compiler(>=6.2)
@_documentation(visibility: internal)
public typealias SendableHashableType = Hashable & SendableMetatype
#else
@_documentation(visibility: internal)
public typealias SendableHashableType = Hashable
#endif
extension Gen {
/// Produces a generator of random elements of the given collection.
///
/// - Parameter collection: A collection.
@inlinable
public static func element<C>(of collection: C) -> Generator<Value, Shrink.None<Value>>
where C: Collection & Sendable, Value == C.Element? {
return Generator { rng in collection.randomElement(using: &rng) }
}
/// Produces a generator of shuffled arrays of the given collection.
///
/// - Parameter collection: A collection.
@inlinable
public static func shuffled<C>(_ collection: C) -> Generator<Value, Shrink.None<Value>>
where C: Collection & Sendable, Value == [C.Element] {
return Generator { rng in collection.shuffled(using: &rng) }
}
}
extension Generator where InputValue: Sendable, ResultValue: Collection & Sendable {
/// Produces a generator of random elements of this generator's collection.
@inlinable
public var element: Generator<ResultValue.Element?, Shrink.None<ResultValue.Element?>> {
return .init(run: { rng in
let value = self.run(using: &rng)
return value.randomElement(using: &rng)
})
}
}
extension Gen where Value: CaseIterable & Sendable, Value.AllCases: Sendable {
/// Produces a generator of random cases of a given enum.
///
/// The enum must conform to `CaseIterable`.
///
/// ### Example
/// ```swift
/// enum Direction: CaseIterable {
/// case north, south, east, west
/// }
/// let gen = Gen<Direction>.case
/// ```
@inlinable
public static var `case`: Generator<Value, Shrink.None<Value>> {
precondition(!Value.allCases.isEmpty, "CaseIterable must have at least one case")
return .init { rng in
Value.allCases.randomElement(using: &rng)!
}
}
}
extension Generator {
@_documentation(visibility: internal)
public typealias ArrayShrink = Shrink.Appended<
Shrink.Omit<[InputValue]>, Shrink.ElementWise<InputValue, [InputValue], ShrinkSequence>
>
/// Produces a new generator of arrays of this generator's values.
///
/// ### Example
/// ```swift
/// Gen.bool.array(of: 1 ... 10)
/// ```
///
/// - Parameter count: The size of the random array.
/// - Returns: A generator of arrays.
@inlinable
public func array(of count: ClosedRange<Int>) -> Generator<[ResultValue], ArrayShrink> {
return .init(
run: { rng in
let itemCount = Int.random(in: count, using: &rng)
var collection: [InputValue] = []
collection.reserveCapacity(itemCount)
for _ in 0..<itemCount {
collection.append(self.runFull(&rng).input)
}
return collection
},
shrink: {
Shrink.shrinkArray($0, shrinker: _shrinker, lowerBound: count.lowerBound)
},
finalResult: {
return $0.compactMap(self._mapFilter)
})
}
/// Produces a new generator of arrays of this generator's values.
///
/// ### Example
/// ```swift
/// Gen.bool.array(of: 5)
/// ```
///
/// - Parameter count: The size of the random array.
/// - Returns: A generator of arrays.
@inlinable
public func array(of count: Int) -> Generator<[ResultValue], ArrayShrink> {
return array(of: count...count)
}
/// Produces a new generator of dictionaries of this generator's pairs.
///
/// ### Example
/// ```swift
/// zip(
/// Gen.int(),
/// Gen.letter.string(of: 10)
/// ).dictionary(ofAtMost: 1 ... 10)
/// ```
///
/// - Parameter count: The size of the random dictionary. If duplicate keys are generated, the dictionary will have a smaller size.
/// - Returns: A generator of dictionaries.
@inlinable
public func dictionary<K: SendableHashableType, V>(ofAtMost count: ClosedRange<Int>) -> Generator<
[K: V], ArrayShrink
>
where ResultValue == (K, V) {
return array(of: count).map {
Dictionary($0, uniquingKeysWith: { a, _ in a })
}
}
/// Produces a new generator of dictionaries of this generator's pairs.
///
/// ### Example
/// ```swift
/// zip(
/// Gen.int(),
/// Gen.letter.string(of: 10)
/// ).dictionary(ofAtMost: 5)
/// ```
///
/// - Parameter count: The size of the random dictionary. If duplicate keys are generated, the dictionary will have a smaller size.
/// - Returns: A generator of dictionaries.
@inlinable
public func dictionary<K: SendableHashableType, V>(ofAtMost count: Int) -> Generator<[K: V], ArrayShrink>
where ResultValue == (K, V) {
return dictionary(ofAtMost: count...count)
}
}
extension Generator where ResultValue: SendableHashableType {
/// Produces a new generator of sets of this generator's values.
///
/// ### Example
/// ```swift
/// Gen.int().set(ofAtMost: 1 ... 10)
/// ```
///
/// - Parameter count: The amount of items to generate. If duplicate items are generated, the set will have a smaller size.
/// - Returns: A generator of sets.
@inlinable
public func set(ofAtMost count: ClosedRange<Int>) -> Generator<Set<ResultValue>, ArrayShrink> {
return array(of: count).map { Set($0) }
}
/// Produces a new generator of sets of this generator's values.
///
/// ### Example
/// ```swift
/// Gen.int().set(ofAtMost: 5)
/// ```
///
/// - Parameter count: The amount of items to generate. If duplicate items are generated, the set will have a smaller size.
/// - Returns: A generator of sets.
@inlinable
public func set(ofAtMost count: Int) -> Generator<Set<ResultValue>, ArrayShrink> {
return set(ofAtMost: count...count)
}
}
extension Gen where Value: OptionSet & Sendable, Value.RawValue: FixedWidthInteger & Sendable {
/// Produces a generator of sets for an OptionSet.
///
/// This generator will generate sets that may exceed the static properties declared in this option set.
/// To specify an upper limit, use ``optionSet(of:)``.
///
/// ### Example
/// ```swift
/// struct Alignment: OptionSet {
/// var rawValue: UInt8
///
/// static let top = Self(rawValue: 1 << 0)
/// static let bottom = Self(rawValue: 1 << 1)
/// static let leading = Self(rawValue: 1 << 2)
/// static let trailing = Self(rawValue: 1 << 3)
/// }
/// let gen = Gen<Alignment>.optionSet
/// ```
/// ## See Also
/// - ``optionSet(of:)``
public static var optionSet: Generator<Value, Shrink.Bitwise<Value.RawValue>> {
Gen<Value.RawValue>.bitSet.map { Value(rawValue: $0) }
}
/// Produces a generator of sets for an OptionSet.
///
/// ### Example
/// ```swift
/// struct Alignment: OptionSet {
/// var rawValue: UInt8
///
/// static let top = Self(rawValue: 1 << 0)
/// static let bottom = Self(rawValue: 1 << 1)
/// static let leading = Self(rawValue: 1 << 2)
/// static let trailing = Self(rawValue: 1 << 3)
/// static let all: Self = [.top, .bottom, .leading, .trailing]
/// }
/// let gen = Gen.optionSet(of: Alignment.all)
/// ```
///
/// - Parameter options: The options to choose from.
/// - Returns: A generator of option sets.
public static func optionSet(of options: Value) -> Generator<Value, Shrink.Bitwise<Value.RawValue>> {
Gen<Value.RawValue>.bitSet(mask: options.rawValue).map { Value(rawValue: $0) }
}
}