// DistributionKindGoldenValueTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth /// Pins the golden values documented in `DistributionKind`'s per-distribution examples. /// /// `scripts/check-doc-snippets.sh` typechecks those fences, but a typecheck cannot see a /// *value* drift: the example still compiles while its `// → …` comment silently becomes a /// lie. That happened — three of the four documented sequences (`.lpRand`, `.hpRand`, /// `.meanRand`) had drifted away from what the code actually produces, and the fences /// additionally passed `seed:` to `TemporalTendency`, which has no such parameter (the seed /// belongs to `TemporalStream`). /// /// If a distribution's maths or the PRNG changes, this suite fails — update the doc comment /// in `Sources/Lyrebird/Distributions/DistributionKind.swift` and these expectations /// together. @Suite("DistributionKind golden values (docs)") struct DistributionKindGoldenValueTests { /// The seed the doc comment names. Any change here invalidates every value below. private static let seed: UInt64 = 0xDEADCAFE12345678 private static let sampleCount: Int = 5 /// Values are documented to 3 decimal places, so compare at that resolution. private static let tolerance: Double = 0.0005 /// The algebraic identities below are exact, not rounded — hold them to full precision. private static let exactTolerance: Double = 1e-12 private func collect(_ kind: DistributionKind) -> [Double] { TemporalStream( TemporalTendency(distribution: kind, parX: .value(0), parY: .value(1)), seed: Self.seed ).collect(Self.sampleCount) } /// Collect from an explicitly-built tendency, for the distributions whose documented /// example uses parameters other than `parX: 0, parY: 1`. private func collect(_ t: TemporalTendency) -> [Double] { TemporalStream(t, seed: Self.seed).collect(Self.sampleCount) } private func expect(_ label: String, _ actual: [Double], _ expected: [Double]) { #expect(actual.count == expected.count) for (a, e) in zip(actual, expected) { #expect(abs(a - e) < Self.tolerance, "\(label) drifted from the documented sequence: got \(actual), doc says \(expected)") } } private func expect(_ kind: DistributionKind, _ expected: [Double]) { expect("\(kind)", collect(kind), expected) } @Test func uniformMatchesDocs() { expect(.uniform, [0.744, 0.046, 0.799, 0.681, 0.517]) } @Test func lpRandMatchesDocs() { expect(.lpRand, [0.553, 0.002, 0.638, 0.464, 0.267]) } @Test func hpRandMatchesDocs() { expect(.hpRand, [0.447, 0.998, 0.362, 0.536, 0.733]) } @Test func meanRandMatchesDocs() { expect(.meanRand, [0.395, 0.740, 0.400, 0.410, 0.899]) } /// The documented characterisations, checked as relationships rather than values: /// `.lpRand` is the squared uniform and `.hpRand` its mirror. This is what makes the /// prose ("squared uniform, biases toward lo") verifiable rather than merely asserted. @Test func lpAndHpRandAreSquaredUniformAndItsMirror() { let u: [Double] = collect(.uniform) let lp: [Double] = collect(.lpRand) let hp: [Double] = collect(.hpRand) for i: Int in 0..