|
| 1 | +// |
| 2 | +// GenTests+Frequency.swift |
| 3 | +// PropertyBased |
| 4 | +// |
| 5 | +// Created by Lennard Sprong on 08/07/2025. |
| 6 | +// |
| 7 | + |
| 8 | +import Testing |
| 9 | + |
| 10 | +@testable import PropertyBased |
| 11 | + |
| 12 | +#if swift(>=6.2) |
| 13 | +@Suite struct GenFrequencyTests { |
| 14 | + enum Choice: Hashable { |
| 15 | + case plain |
| 16 | + case number(Int) |
| 17 | + case text(String) |
| 18 | + } |
| 19 | + |
| 20 | + // MARK: oneOf |
| 21 | + |
| 22 | + static let unpackedGen = Gen.oneOf( |
| 23 | + Gen.always(Choice.plain).eraseToAny(), |
| 24 | + Gen.int().map(Choice.number).eraseToAny(), |
| 25 | + Gen.lowercaseLetter.string(of: 8).map(Choice.text).eraseToAny(), |
| 26 | + ) |
| 27 | + |
| 28 | + static let packedGen = Gen.oneOf( |
| 29 | + Gen.always(Choice.plain), |
| 30 | + Gen.int().map(Choice.number), |
| 31 | + Gen.lowercaseLetter.string(of: 8).map(Choice.text), |
| 32 | + ) |
| 33 | + |
| 34 | + static let generators = [(0, unpackedGen), (1, packedGen)] |
| 35 | + |
| 36 | + @Test(arguments: generators) |
| 37 | + func testGenerateEnum(_ pair: (Int, Generator<Choice, AnySequence<(index: Int, value: Any)>>)) async { |
| 38 | + let gen = pair.1 |
| 39 | + await testGen(gen) |
| 40 | + |
| 41 | + await confirmation(expectedCount: 1...) { confirm1 in |
| 42 | + await confirmation(expectedCount: 1...) { confirm2 in |
| 43 | + await confirmation(expectedCount: 1...) { confirm3 in |
| 44 | + await propertyCheck(count: 200, input: gen) { item in |
| 45 | + switch item { |
| 46 | + case .plain: |
| 47 | + confirm1() |
| 48 | + case .number: |
| 49 | + confirm2() |
| 50 | + case .text: |
| 51 | + confirm3() |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test(arguments: generators) |
| 60 | + func testShrinkChoice(_ pair: (Int, Generator<Choice, AnySequence<(index: Int, value: Any)>>)) async throws { |
| 61 | + let gen = pair.1 |
| 62 | + let value = (index: 1, value: 500 as Any) |
| 63 | + let results = gen._shrinker(value).compactMap(gen._mapFilter) |
| 64 | + try #require(results.count > 1) |
| 65 | + #expect(results.first == .number(0)) |
| 66 | + #expect(!results.contains(.number(500))) |
| 67 | + } |
| 68 | + |
| 69 | + // MARK: frequency |
| 70 | + |
| 71 | + static let unpackedFreqGen = Gen<Choice>.frequency([ |
| 72 | + (1, Gen.int().map(Choice.number).eraseToAny()), |
| 73 | + (2.0, Gen.lowercaseLetter.string(of: 8).map(Choice.text).eraseToAny()), |
| 74 | + (0, Gen.always(Choice.plain).eraseToAny()), |
| 75 | + ]) |
| 76 | + |
| 77 | + static let packedFreqGen = Gen.frequency( |
| 78 | + (1, Gen.int().map(Choice.number)), |
| 79 | + (2, Gen.lowercaseLetter.string(of: 8).map(Choice.text)), |
| 80 | + (0, Gen.always(Choice.plain)), |
| 81 | + ) |
| 82 | + static let freqGenerators = [(0, unpackedFreqGen), (1, packedFreqGen)] |
| 83 | + |
| 84 | + @Test(arguments: freqGenerators) |
| 85 | + func testGenerateWithFrequency(_ pair: (Int, Generator<Choice, AnySequence<(index: Int, value: Any)>>)) async { |
| 86 | + let gen = pair.1 |
| 87 | + await testGen(gen) |
| 88 | + |
| 89 | + await propertyCheck(count: 200, input: gen) { item in |
| 90 | + #expect(item != .plain) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +#endif |
0 commit comments