Skip to content

Commit

Permalink
Add parameterization controls for arbitrary collection sizes (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicorichard authored May 24, 2022
1 parent 52fccbb commit 26491be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Sources/Genything/Arbitrary/Type/Swift+Arbitrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ extension Character: Arbitrary {
extension String: Arbitrary {
/// A generator of arbitrary `String`s of random sizes
public static var arbitrary: AnyGenerator<String> {
arbitrary()
AnyGenerator { $0 }.flatMap { rs in
arbitrary(in: 0 ... rs.maxArbitraryCollectionSize)
}
}

/// A generator of arbitrary `String`s of random sizes in`range`
public static func arbitrary(in range: ClosedRange<Int> = 0 ... 100) -> AnyGenerator<String> {
public static func arbitrary(in range: ClosedRange<Int>) -> AnyGenerator<String> {
Character.arbitrary
.expand(toSizeInRange: range)
.map { String($0) }
Expand All @@ -94,11 +96,13 @@ extension String: Arbitrary {
extension Array: Arbitrary where Element: Arbitrary {
/// A generator of arbitrary `Array`s of random sizes
public static var arbitrary: AnyGenerator<Array> {
arbitrary()
AnyGenerator { $0 }.flatMap { rs in
arbitrary(in: 0 ... rs.maxRecursiveArbitraryCollectionSize)
}
}

/// A generator of arbitrary `Array`s of random sizes in `range`
public static func arbitrary(in range: ClosedRange<Int> = 0 ... 33) -> AnyGenerator<Array> {
public static func arbitrary(in range: ClosedRange<Int>) -> AnyGenerator<Array> {
Element.arbitrary
.expand(toSizeInRange: range)
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/Genything/Random/RandomSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public class RandomSource {

/// A type-erased `RandomNumberGenerator`
public var rng: AnyRandomNumberGenerator

/// Controls the maximum size for unbounded arbitrary collections (such as Dictionaries or Arrays) of arbitrary values
/// Due to the fact that these collections contain other arbitary values, which may themselves contain further collections this parameter may need to be tweaked for performance reasons.
public var maxRecursiveArbitraryCollectionSize: Int = 33

/// Controls the maximum size for unbounded arbitrary collections such as Strings
public var maxArbitraryCollectionSize: Int = 100
}

// MARK: Convenience RandomSource Creators
Expand Down

0 comments on commit 26491be

Please sign in to comment.