Skip to content

Commit 7d1709b

Browse files
committed
Add overload of bitSet generator that accepts a bit mask
1 parent df8d263 commit 7d1709b

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

Sources/PropertyBased/Gen+Collection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,6 @@ extension Gen where Value: OptionSet & Sendable, Value.RawValue: FixedWidthInteg
226226
/// - Parameter options: The options to choose from.
227227
/// - Returns: A generator of option sets.
228228
public static func optionSet(of options: Value) -> Generator<Value, Shrink.Bitwise<Value.RawValue>> {
229-
Gen<Value.RawValue>.bitSet.map { Value(rawValue: $0 & options.rawValue) }
229+
Gen<Value.RawValue>.bitSet(mask: options.rawValue).map { Value(rawValue: $0) }
230230
}
231231
}

Sources/PropertyBased/Gen+Int.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,17 @@ extension Gen where Value: FixedWidthInteger & Sendable {
3939
///
4040
/// The associated shrinker will remove every bit, but will never set bits that are unset in the original value.
4141
@inlinable
42-
public static var bitSet: Generator<Value, Shrink.Bitwise<Value>> {
42+
public static var bitSet: Generator<Value, Shrink.Bitwise<Value>> { bitSet() }
43+
44+
/// Produces a generator of random integers within the given bit mask.
45+
///
46+
/// The associated shrinker will remove every bit, but will never set bits that are unset in the original value.
47+
/// - Parameter mask: The bit mask to apply to each generated value.
48+
/// - Returns: A generator of random values.
49+
@inlinable
50+
public static func bitSet(mask: Value = ~0) -> Generator<Value, Shrink.Bitwise<Value>> {
4351
return .init(
44-
run: { rng in Value.random(in: .min ... .max, using: &rng) },
52+
run: { rng in Value.random(in: .min ... .max, using: &rng) & mask },
4553
shrink: { .init($0) }
4654
)
4755
}

0 commit comments

Comments
 (0)