Skip to content

Commit

Permalink
Adds a method to allow for array shuffling (#31)
Browse files Browse the repository at this point in the history
* shuffles an array of values

* Test shuffled
  • Loading branch information
nicorichard authored Feb 14, 2022
1 parent ca72a64 commit 5d6ecf0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct BusinessListView: View {
addressLine1: Fake.Addresses.streetLine.generate(context: ctx),
addressLine2: addressLine2Gen.generate(context: ctx)
)
}.take(count: 50)
}.take(50)

var body: some View {
List {
Expand Down
20 changes: 20 additions & 0 deletions Sources/Genything/gen/build/Gen+shuffled.swift
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)
}
}
}
13 changes: 1 addition & 12 deletions Tests/GenythingTests/gen/build/GenOfTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import XCTest
@testable import Genything

final internal class GenOfTests: XCTestCase {
func test_from_createGenerator() {
func test_of_createGenerator() {
let gen = Gen.of([0, 1, 2])
let sample = gen.take()

Expand All @@ -11,15 +11,4 @@ final internal class GenOfTests: XCTestCase {
XCTAssert(sample.contains(2))
XCTAssert(sample.allSatisfy { $0 == 0 || $0 == 1 || $0 == 2 })
}


func test_exhaust() {
let samples = Gen.exhaust([0, Int.max], then: Gen.from(0...Int.max))
.samples()

XCTAssertEqual(0, samples[0])
XCTAssertEqual(Int.max, samples[1])

print("!@# samples \(samples)")
}
}
23 changes: 23 additions & 0 deletions Tests/GenythingTests/gen/build/GenShuffledTests.swift
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))
}
}
}

0 comments on commit 5d6ecf0

Please sign in to comment.