From 5d6ecf095435d2add75a81ab8a995356c2756102 Mon Sep 17 00:00:00 2001 From: Nico Richard Date: Mon, 14 Feb 2022 14:59:10 -0600 Subject: [PATCH] Adds a method to allow for array shuffling (#31) * shuffles an array of values * Test shuffled --- .../Business Cards/BusinessListView.swift | 2 +- .../Genything/gen/build/Gen+shuffled.swift | 20 ++++++++++++++++ .../GenythingTests/gen/build/GenOfTests.swift | 13 +---------- .../gen/build/GenShuffledTests.swift | 23 +++++++++++++++++++ 4 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 Sources/Genything/gen/build/Gen+shuffled.swift create mode 100644 Tests/GenythingTests/gen/build/GenShuffledTests.swift diff --git a/Example/GenythingExample/Pages/Business Cards/BusinessListView.swift b/Example/GenythingExample/Pages/Business Cards/BusinessListView.swift index 1c3e11d6..a779b3f6 100644 --- a/Example/GenythingExample/Pages/Business Cards/BusinessListView.swift +++ b/Example/GenythingExample/Pages/Business Cards/BusinessListView.swift @@ -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 { diff --git a/Sources/Genything/gen/build/Gen+shuffled.swift b/Sources/Genything/gen/build/Gen+shuffled.swift new file mode 100644 index 00000000..c9361ab1 --- /dev/null +++ b/Sources/Genything/gen/build/Gen+shuffled.swift @@ -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) + } + } +} diff --git a/Tests/GenythingTests/gen/build/GenOfTests.swift b/Tests/GenythingTests/gen/build/GenOfTests.swift index 6de2f921..d62b01ac 100644 --- a/Tests/GenythingTests/gen/build/GenOfTests.swift +++ b/Tests/GenythingTests/gen/build/GenOfTests.swift @@ -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() @@ -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)") - } } diff --git a/Tests/GenythingTests/gen/build/GenShuffledTests.swift b/Tests/GenythingTests/gen/build/GenShuffledTests.swift new file mode 100644 index 00000000..41960f6f --- /dev/null +++ b/Tests/GenythingTests/gen/build/GenShuffledTests.swift @@ -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)) + } + } +}