diff --git a/Sources/PropertyBased/Gen+Collection.swift b/Sources/PropertyBased/Gen+Collection.swift index f339f23..f79f576 100644 --- a/Sources/PropertyBased/Gen+Collection.swift +++ b/Sources/PropertyBased/Gen+Collection.swift @@ -226,6 +226,6 @@ extension Gen where Value: OptionSet & Sendable, Value.RawValue: FixedWidthInteg /// - Parameter options: The options to choose from. /// - Returns: A generator of option sets. public static func optionSet(of options: Value) -> Generator> { - Gen.bitSet.map { Value(rawValue: $0 & options.rawValue) } + Gen.bitSet(mask: options.rawValue).map { Value(rawValue: $0) } } } diff --git a/Sources/PropertyBased/Gen+Int.swift b/Sources/PropertyBased/Gen+Int.swift index eae3d31..3e5fac1 100644 --- a/Sources/PropertyBased/Gen+Int.swift +++ b/Sources/PropertyBased/Gen+Int.swift @@ -39,9 +39,17 @@ extension Gen where Value: FixedWidthInteger & Sendable { /// /// 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> { + public static var bitSet: Generator> { 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> { return .init( - run: { rng in Value.random(in: .min ... .max, using: &rng) }, + run: { rng in Value.random(in: .min ... .max, using: &rng) & mask }, shrink: { .init($0) } ) }