Files
Lyrebird/Tests/LyrebirdTests/DistributionKindGoldenValueTests.swift
T
josh 560724d116 v1.3.0 release: graceful-release fix, quickstart template + demo, documentation accuracy pass
- Fixed: SynthesizerBase subclasses' release/free now dispatch correctly (gated
  synths were hard-freed instead of released).
- New: quickstart project template + LyrebirdQuickstartDemo.
- Docs: every example typechecked or provably illustrative (426->542); ~20
  wrong-API examples fixed; root docs brought under the compile guard.
- Fixed: NRT/Score demo Package.swift build from terminal again.

See RELEASES.md for details.
2026-07-17 14:05:01 -07:00

156 lines
6.5 KiB
Swift

// 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..<Self.sampleCount {
#expect(abs(lp[i] - u[i] * u[i]) < Self.exactTolerance,
"lpRand should be the squared uniform")
#expect(abs(hp[i] - (1.0 - u[i] * u[i])) < Self.exactTolerance,
"hpRand should mirror the squared uniform")
}
}
// The remaining documented distributions. Every one of these sequences was wrong before
// being regenerated from the real API — the doc had drifted while its prose stayed right.
@Test func expRandMatchesDocs() {
expect("expRand", collect(TemporalTendency(distribution: .expRand,
parX: .value(1), parY: .value(100))),
[30.743, 1.234, 39.596, 23.035, 10.818])
}
@Test func exponentialMatchesDocs() {
expect("exponential", collect(TemporalTendency(distribution: .exponential, parX: .value(1))),
[0.296, 3.085, 0.225, 0.384, 0.660])
}
@Test func gammaMatchesDocs() {
expect("gamma", collect(TemporalTendency(distribution: .gamma, parX: .value(2))),
[2.812, 0.776, 1.756, 1.326, 3.649])
}
@Test func laplaceMatchesDocs() {
expect("laplace", collect(TemporalTendency(distribution: .laplace, parX: .value(1))),
[0.669, -2.392, 0.910, 0.450, 0.035])
}
@Test func alaplaceMatchesDocs() {
expect("alaplace", collect(TemporalTendency(distribution: .alaplace, parX: .value(1))),
[0.527, -3.898, 1.332, -0.039, -0.433])
}
@Test func hcosMatchesDocs() {
expect("hcos", collect(TemporalTendency(distribution: .hcos, parX: .value(1))),
[0.162, -0.363, 0.204, 0.118, 0.011])
}
@Test func logisticMatchesDocs() {
expect("logistic", collect(TemporalTendency(distribution: .logistic, parX: .value(1))),
[1.066, -3.039, 1.379, 0.759, 0.068])
}
@Test func poissonMatchesDocs() {
expect("poisson", collect(TemporalTendency(distribution: .poisson, parX: .value(3))),
[1.000, 4.000, 4.000, 1.000, 4.000])
}
@Test func arcsinMatchesDocs() {
expect("arcsin", collect(TemporalTendency(distribution: .arcsin, parX: .value(1))),
[0.693, -0.990, 0.807, 0.539, 0.054])
}
@Test func betaRandMatchesDocs() {
expect("betaRand", collect(TemporalTendency(distribution: .betaRand,
parX: .value(0), parY: .value(1),
parA: .value(2), parB: .value(5))),
[0.482, 0.301, 0.471, 0.415, 0.066])
}
@Test func cauchyMatchesDocs() {
expect("cauchy", collect(TemporalTendency(distribution: .cauchy,
parX: .value(0), parY: .value(1))),
[0.962, -6.916, 1.366, 0.640, 0.054])
}
@Test func gaussMatchesDocs() {
expect("gauss", collect(TemporalTendency(distribution: .gauss,
parX: .value(1), parY: .value(0))),
[0.738, -0.281, -0.238, 0.068, 0.328])
}
}