-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a method to allow for array shuffling (#31)
* shuffles an array of values * Test shuffled
- Loading branch information
1 parent
ca72a64
commit 5d6ecf0
Showing
4 changed files
with
45 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Foundation | ||
|
||
// MARK: Build | ||
|
||
public extension Gen { | ||
/// Returns: A generator which produces the result of shuffling the provided list | ||
/// | ||
/// - Warning: The values list must not be empty | ||
|
||
/// - Parameter values: The values which this generator will shuffle | ||
/// | ||
/// - Returns: The generator | ||
static func shuffled(_ values: [T]) -> Gen<[T]> { | ||
assert(!values.isEmpty, "`Gen.shuffled(values:)` was invoked with an empty list of values") | ||
|
||
return Gen<[T]> { ctx in | ||
values.shuffled(using: &ctx.rng) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Foundation | ||
@testable import Genything | ||
import XCTest | ||
|
||
final internal class GenShuffledTests: XCTestCase { | ||
func test_shuffled() { | ||
let source = [0,1,2,3,4,5,6,7,8,9] | ||
|
||
// Seed the context such that we can reproduce results with or without the generator | ||
let seed: UInt64 = 1234 | ||
let context = Context(using: LinearCongruentialRandomNumberGenerator(seed: seed), originalSeed: seed) | ||
|
||
// Take 100 shuffles | ||
let shuffledViaGeneratorList = Gen.shuffled(source) | ||
.take(100, context: context) | ||
|
||
// Make sure that these shuffles match doing normal shuffles without a generator | ||
var collectionsLCRNG = LinearCongruentialRandomNumberGenerator(seed: seed) | ||
for shuffledViaGenerator in shuffledViaGeneratorList { | ||
XCTAssertEqual(shuffledViaGenerator, source.shuffled(using: &collectionsLCRNG)) | ||
} | ||
} | ||
} |