514 lines
19 KiB
Swift
514 lines
19 KiB
Swift
// TemporalStreamTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
// MARK: - TemporalParameter Tests
|
||
|
||
@MainActor
|
||
@Suite("TemporalParameterTests")
|
||
struct TemporalParameterTests {
|
||
|
||
@Test("value resolves to fixed Double")
|
||
func valueResolves() {
|
||
let param: TemporalParameter = TemporalParameter.value(440.0)
|
||
#expect(param.resolve(elapsed: 0.0, counter: 0) == 440.0)
|
||
#expect(param.resolve(elapsed: 99.9, counter: 50) == 440.0)
|
||
}
|
||
|
||
@Test("envelope resolves at correct elapsed time")
|
||
func envelopeResolves() {
|
||
// Simple linear envelope: 0 at t=0, 1 at t=1
|
||
let env: Envelope = Envelope(levels: [0.0, 1.0], durations: [1.0], curves: [.lin])
|
||
let param: TemporalParameter = TemporalParameter.envelope(env)
|
||
let atStart: Double = param.resolve(elapsed: 0.0, counter: 0)
|
||
let atMid: Double = param.resolve(elapsed: 0.5, counter: 1)
|
||
let atEnd: Double = param.resolve(elapsed: 1.0, counter: 2)
|
||
#expect(atStart < 0.01)
|
||
#expect(atMid > 0.49 && atMid < 0.51)
|
||
#expect(atEnd > 0.99)
|
||
}
|
||
|
||
@Test("closure receives correct elapsed and counter")
|
||
func closureReceivesArgs() {
|
||
var capturedElapsed: Double = -1
|
||
var capturedCounter: Int = -1
|
||
let param: TemporalParameter = TemporalParameter.closure { elapsed, counter in
|
||
capturedElapsed = elapsed
|
||
capturedCounter = counter
|
||
return Double(counter) + elapsed
|
||
}
|
||
let result: Double = param.resolve(elapsed: 2.5, counter: 7)
|
||
#expect(capturedElapsed == 2.5)
|
||
#expect(capturedCounter == 7)
|
||
#expect(result == 9.5)
|
||
}
|
||
}
|
||
|
||
// MARK: - TemporalSequence Tests
|
||
|
||
@MainActor
|
||
@Suite("TemporalSequenceTests")
|
||
struct TemporalSequenceTests {
|
||
|
||
@Test("steps through values in order")
|
||
func stepsInOrder() {
|
||
let seq: TemporalSequence = TemporalSequence(values: [.value(1), .value(2), .value(3)], repeats: 1)
|
||
var rng: UInt64 = 12345
|
||
let a: Double? = seq.nextValue(elapsed: 0, counter: 0, rng: &rng)
|
||
let b: Double? = seq.nextValue(elapsed: 0, counter: 1, rng: &rng)
|
||
let c: Double? = seq.nextValue(elapsed: 0, counter: 2, rng: &rng)
|
||
#expect(a == 1.0)
|
||
#expect(b == 2.0)
|
||
#expect(c == 3.0)
|
||
}
|
||
|
||
@Test("wraps correctly")
|
||
func wrapsAtEnd() {
|
||
let seq: TemporalSequence = TemporalSequence(values: [.value(10), .value(20)], repeats: 2)
|
||
var rng: UInt64 = 1
|
||
_ = seq.nextValue(elapsed: 0, counter: 0, rng: &rng) // 10, index wraps after 20
|
||
_ = seq.nextValue(elapsed: 0, counter: 1, rng: &rng) // 20, cycle 1 done
|
||
let third: Double? = seq.nextValue(elapsed: 0, counter: 2, rng: &rng) // 10 again
|
||
#expect(third == 10.0)
|
||
}
|
||
|
||
@Test("exhausts after N cycles")
|
||
func exhaustsAfterNCycles() {
|
||
let seq: TemporalSequence = TemporalSequence(values: [.value(1), .value(2)], repeats: 2)
|
||
var rng: UInt64 = 1
|
||
var count: Int = 0
|
||
while let _ = seq.nextValue(elapsed: 0, counter: count, rng: &rng) { count += 1 }
|
||
#expect(count == 4) // 2 values × 2 cycles
|
||
#expect(seq.isExhausted)
|
||
}
|
||
|
||
@Test("repeats -1 is infinite")
|
||
func infiniteRepeats() {
|
||
let seq: TemporalSequence = TemporalSequence(values: [.value(99)], repeats: -1)
|
||
#expect(!seq.isExhausted)
|
||
var rng: UInt64 = 1
|
||
for i: Int in 0..<1000 {
|
||
let v: Double? = seq.nextValue(elapsed: 0, counter: i, rng: &rng)
|
||
#expect(v == 99.0)
|
||
}
|
||
#expect(!seq.isExhausted)
|
||
}
|
||
|
||
@Test("resolves envelope param at elapsed time")
|
||
func resolvesEnvelopeAtElapsed() {
|
||
let env: Envelope = Envelope(levels: [0.0, 100.0], durations: [1.0], curves: [.lin])
|
||
let seq: TemporalSequence = TemporalSequence(values: [.envelope(env)], repeats: -1)
|
||
var rng: UInt64 = 1
|
||
let atHalf: Double = seq.nextValue(elapsed: 0.5, counter: 0, rng: &rng) ?? -1
|
||
#expect(atHalf > 49 && atHalf < 51)
|
||
}
|
||
|
||
@Test("reset restarts from index 0")
|
||
func resetRestartsIndex() {
|
||
let seq: TemporalSequence = TemporalSequence(values: [.value(1), .value(2), .value(3)], repeats: 1)
|
||
var rng: UInt64 = 1
|
||
_ = seq.nextValue(elapsed: 0, counter: 0, rng: &rng) // 1
|
||
_ = seq.nextValue(elapsed: 0, counter: 1, rng: &rng) // 2
|
||
seq.reset()
|
||
let first: Double? = seq.nextValue(elapsed: 0, counter: 0, rng: &rng)
|
||
#expect(first == 1.0)
|
||
#expect(!seq.isExhausted)
|
||
}
|
||
}
|
||
|
||
// MARK: - TemporalRandomPick Tests
|
||
|
||
@MainActor
|
||
@Suite("TemporalRandomPickTests")
|
||
struct TemporalRandomPickTests {
|
||
|
||
@Test("picks from array")
|
||
func picksFromArray() {
|
||
let allowed: Set<Double> = [100, 200, 300]
|
||
let pick: TemporalRandomPick = TemporalRandomPick(values: [.value(100), .value(200), .value(300)], repeats: -1)
|
||
var rng: UInt64 = 42
|
||
for i: Int in 0..<20 {
|
||
let v: Double = pick.nextValue(elapsed: 0, counter: i, rng: &rng) ?? -1
|
||
#expect(allowed.contains(v))
|
||
}
|
||
}
|
||
|
||
@Test("exhausts after N calls")
|
||
func exhaustsAfterNCalls() {
|
||
let pick: TemporalRandomPick = TemporalRandomPick(values: [.value(1), .value(2)], repeats: 5)
|
||
var rng: UInt64 = 7
|
||
var count: Int = 0
|
||
while let _ = pick.nextValue(elapsed: 0, counter: count, rng: &rng) { count += 1 }
|
||
#expect(count == 5)
|
||
#expect(pick.isExhausted)
|
||
}
|
||
|
||
@Test("reset restarts count")
|
||
func resetRestartsCount() {
|
||
let pick: TemporalRandomPick = TemporalRandomPick(values: [.value(1)], repeats: 3)
|
||
var rng: UInt64 = 9
|
||
_ = pick.nextValue(elapsed: 0, counter: 0, rng: &rng)
|
||
_ = pick.nextValue(elapsed: 0, counter: 1, rng: &rng)
|
||
_ = pick.nextValue(elapsed: 0, counter: 2, rng: &rng)
|
||
#expect(pick.isExhausted)
|
||
pick.reset()
|
||
#expect(!pick.isExhausted)
|
||
let v: Double? = pick.nextValue(elapsed: 0, counter: 0, rng: &rng)
|
||
#expect(v == 1.0)
|
||
}
|
||
}
|
||
|
||
// MARK: - TemporalTendency Tests
|
||
|
||
@MainActor
|
||
@Suite("TemporalTendencyTests")
|
||
struct TemporalTendencyTests {
|
||
|
||
@Test("uniform output in [lo, hi]")
|
||
func uniformInBounds() {
|
||
let lo: Double = 100.0
|
||
let hi: Double = 200.0
|
||
let tendency: TemporalTendency = TemporalTendency(distribution: .uniform, parX: .value(lo), parY: .value(hi))
|
||
var rng: UInt64 = 1234
|
||
for i: Int in 0..<500 {
|
||
let v: Double = tendency.nextValue(elapsed: 0, counter: i, rng: &rng) ?? -1
|
||
#expect(v >= lo && v <= hi)
|
||
}
|
||
}
|
||
|
||
@Test("gauss output near mean (statistical)")
|
||
func gaussNearMean() {
|
||
let dev: Double = 10.0
|
||
let mean: Double = 440.0
|
||
let tendency: TemporalTendency = TemporalTendency(distribution: .gauss, parX: .value(dev), parY: .value(mean))
|
||
var rng: UInt64 = 9999
|
||
var sum: Double = 0.0
|
||
let sampleCount: Int = 500
|
||
for i: Int in 0..<sampleCount {
|
||
let v: Double = tendency.nextValue(elapsed: 0, counter: i, rng: &rng) ?? 0
|
||
sum += v
|
||
}
|
||
let computedMean: Double = sum / Double(sampleCount)
|
||
// Mean should be within 2 std errors of expected mean (very conservative)
|
||
let allowedDeviation: Double = 5.0
|
||
#expect(abs(computedMean - mean) < allowedDeviation)
|
||
}
|
||
|
||
@Test("betaRand output in [lo, hi]")
|
||
func betaRandInBounds() {
|
||
let lo: Double = 0.0
|
||
let hi: Double = 1.0
|
||
let tendency: TemporalTendency = TemporalTendency(
|
||
distribution: .betaRand,
|
||
parX: .value(lo),
|
||
parY: .value(hi),
|
||
parA: .value(2),
|
||
parB: .value(2)
|
||
)
|
||
var rng: UInt64 = 5555
|
||
for i: Int in 0..<200 {
|
||
let v: Double = tendency.nextValue(elapsed: 0, counter: i, rng: &rng) ?? -99
|
||
#expect(v >= lo && v <= hi)
|
||
}
|
||
}
|
||
|
||
@Test("cauchy posOnly output is non-negative")
|
||
func cauchyPosOnly() {
|
||
let tendency: TemporalTendency = TemporalTendency(
|
||
distribution: .cauchy,
|
||
parX: .value(0),
|
||
parY: .value(1),
|
||
parA: .value(1) // posOnly = true (> 0)
|
||
)
|
||
var rng: UInt64 = 7777
|
||
for i: Int in 0..<200 {
|
||
let v: Double = tendency.nextValue(elapsed: 0, counter: i, rng: &rng) ?? -1
|
||
#expect(v >= 0)
|
||
}
|
||
}
|
||
|
||
@Test("exhausts after repeats")
|
||
func exhaustsAfterRepeats() {
|
||
let tendency: TemporalTendency = TemporalTendency(
|
||
distribution: .uniform,
|
||
parX: .value(0),
|
||
parY: .value(1),
|
||
repeats: 10
|
||
)
|
||
var rng: UInt64 = 1
|
||
var count: Int = 0
|
||
while let _ = tendency.nextValue(elapsed: 0, counter: count, rng: &rng) { count += 1 }
|
||
#expect(count == 10)
|
||
#expect(tendency.isExhausted)
|
||
}
|
||
|
||
@Test("envelope param produces different values at different elapsed times")
|
||
func envelopeParamEvolvesCorrectly() {
|
||
// Envelope: mean goes from 200 to 800 over 1 second
|
||
let meanEnv: Envelope = Envelope(levels: [200.0, 800.0], durations: [1.0], curves: [.lin])
|
||
// Use tiny deviation so samples reliably cluster near the mean
|
||
let smallDev: Double = 0.0001
|
||
let tendency: TemporalTendency = TemporalTendency(
|
||
distribution: .gauss,
|
||
parX: .value(smallDev),
|
||
parY: .envelope(meanEnv),
|
||
repeats: -1
|
||
)
|
||
var rng: UInt64 = 1
|
||
// At elapsed=0 mean≈200, at elapsed=1 mean≈800
|
||
let earlyValue: Double = tendency.nextValue(elapsed: 0.0, counter: 0, rng: &rng) ?? 0
|
||
let lateValue: Double = tendency.nextValue(elapsed: 1.0, counter: 1, rng: &rng) ?? 0
|
||
#expect(earlyValue < 400)
|
||
#expect(lateValue > 600)
|
||
}
|
||
}
|
||
|
||
// MARK: - TemporalBrownian Tests
|
||
|
||
@MainActor
|
||
@Suite("TemporalBrownianTests")
|
||
struct TemporalBrownianTests {
|
||
|
||
@Test("output always in [lo, hi]")
|
||
func alwaysInBounds() {
|
||
let lo: Double = 0.0
|
||
let hi: Double = 1.0
|
||
let walk: TemporalBrownian = TemporalBrownian(lo: .value(lo), hi: .value(hi), stepSize: .value(0.5))
|
||
var rng: UInt64 = 42
|
||
for i: Int in 0..<500 {
|
||
let v: Double = walk.nextValue(elapsed: 0, counter: i, rng: &rng) ?? -99
|
||
#expect(v >= lo && v <= hi)
|
||
}
|
||
}
|
||
|
||
@Test("walk moves (not constant)")
|
||
func walkMoves() {
|
||
let walk: TemporalBrownian = TemporalBrownian(lo: .value(0), hi: .value(1), stepSize: .value(0.3))
|
||
var rng: UInt64 = 11
|
||
var prev: Double? = nil
|
||
var diffFound: Bool = false
|
||
for i: Int in 0..<100 {
|
||
let v: Double = walk.nextValue(elapsed: 0, counter: i, rng: &rng) ?? 0
|
||
if let p: Double = prev, abs(v - p) > 1e-10 { diffFound = true }
|
||
prev = v
|
||
}
|
||
#expect(diffFound)
|
||
}
|
||
|
||
@Test("reset re-centres without explicit start")
|
||
func resetRecenters() {
|
||
let lo: Double = 100.0
|
||
let hi: Double = 300.0
|
||
let walk: TemporalBrownian = TemporalBrownian(lo: .value(lo), hi: .value(hi), stepSize: .value(10))
|
||
var rng: UInt64 = 99
|
||
// Take some steps to move away from start
|
||
for i: Int in 0..<50 { _ = walk.nextValue(elapsed: 0, counter: i, rng: &rng) }
|
||
walk.reset()
|
||
// After reset, first call should initialise to midpoint = 200
|
||
let first: Double = walk.nextValue(elapsed: 0, counter: 0, rng: &rng) ?? -1
|
||
// delta is sampled in [-10, 10] so first value is within [190, 210]
|
||
let midpoint: Double = (lo + hi) * 0.5
|
||
let maxStep: Double = 10.0
|
||
#expect(first >= midpoint - maxStep && first <= midpoint + maxStep)
|
||
}
|
||
|
||
@Test("step size limits movement per call")
|
||
func stepSizeLimitsMovement() {
|
||
let maxStep: Double = 0.01
|
||
let walk: TemporalBrownian = TemporalBrownian(
|
||
lo: .value(0), hi: .value(1),
|
||
stepSize: .value(maxStep),
|
||
start: 0.5
|
||
)
|
||
var rng: UInt64 = 55
|
||
var prev: Double = 0.5
|
||
for i: Int in 0..<200 {
|
||
let v: Double = walk.nextValue(elapsed: 0, counter: i, rng: &rng) ?? prev
|
||
// unclamped delta <= maxStep, clamped result may differ, but change <= maxStep
|
||
#expect(abs(v - prev) <= maxStep + 1e-12)
|
||
prev = v
|
||
}
|
||
}
|
||
|
||
@Test("lo and hi as envelopes changes clamping over time")
|
||
func envelopeBoundsChangeClamping() {
|
||
// lo: 0→500, hi: 0→500 (same!) — walk must clamp to the single point
|
||
// Actually let's make lo=hi=250 at t=1 to force clamp to 250
|
||
let loEnv: Envelope = Envelope(levels: [200.0, 300.0], durations: [1.0], curves: [.lin])
|
||
let hiEnv: Envelope = Envelope(levels: [300.0, 300.0], durations: [1.0], curves: [.lin]) // hi constant at 300
|
||
let walk: TemporalBrownian = TemporalBrownian(
|
||
lo: .envelope(loEnv),
|
||
hi: .envelope(hiEnv),
|
||
stepSize: .value(1000), // huge step to ensure clamping is hit
|
||
start: 250
|
||
)
|
||
var rng: UInt64 = 77
|
||
// At elapsed=0: lo=200, hi=300 — walk stays in [200, 300]
|
||
let earlyV: Double = walk.nextValue(elapsed: 0.0, counter: 0, rng: &rng) ?? -1
|
||
#expect(earlyV >= 200 && earlyV <= 300)
|
||
// At elapsed=1: lo=300, hi=300 — walk must be exactly 300
|
||
let lateV: Double = walk.nextValue(elapsed: 1.0, counter: 1, rng: &rng) ?? -1
|
||
#expect(lateV == 300.0)
|
||
}
|
||
}
|
||
|
||
// MARK: - TemporalClosure Tests
|
||
|
||
@MainActor
|
||
@Suite("TemporalClosureTests")
|
||
struct TemporalClosureTests {
|
||
|
||
@Test("closure receives correct elapsed and counter")
|
||
func closureReceivesArgs() {
|
||
var calls: [(Double, Int)] = []
|
||
let alg: TemporalClosure = TemporalClosure { elapsed, counter, _ in
|
||
calls.append((elapsed, counter))
|
||
return Double(counter)
|
||
}
|
||
var rng: UInt64 = 1
|
||
_ = alg.nextValue(elapsed: 1.5, counter: 0, rng: &rng)
|
||
_ = alg.nextValue(elapsed: 2.5, counter: 1, rng: &rng)
|
||
#expect(calls.count == 2)
|
||
#expect(calls[0].0 == 1.5 && calls[0].1 == 0)
|
||
#expect(calls[1].0 == 2.5 && calls[1].1 == 1)
|
||
}
|
||
|
||
@Test("returning nil exhausts the stream")
|
||
func returningNilExhausts() {
|
||
var callCount: Int = 0
|
||
let alg: TemporalClosure = TemporalClosure { _, counter, _ -> Double? in
|
||
callCount += 1
|
||
return counter < 3 ? Double(counter) : nil
|
||
}
|
||
var rng: UInt64 = 1
|
||
var results: [Double] = []
|
||
for i: Int in 0..<10 {
|
||
if let v: Double = alg.nextValue(elapsed: 0, counter: i, rng: &rng) {
|
||
results.append(v)
|
||
} else {
|
||
break
|
||
}
|
||
}
|
||
#expect(results == [0, 1, 2])
|
||
#expect(alg.isExhausted)
|
||
// Further calls return nil
|
||
let extra: Double? = alg.nextValue(elapsed: 0, counter: 10, rng: &rng)
|
||
#expect(extra == nil)
|
||
}
|
||
|
||
@Test("reset clears exhaustion")
|
||
func resetClearsExhaustion() {
|
||
let alg: TemporalClosure = TemporalClosure { _, counter, _ -> Double? in
|
||
return counter == 0 ? 42.0 : nil
|
||
}
|
||
var rng: UInt64 = 1
|
||
_ = alg.nextValue(elapsed: 0, counter: 0, rng: &rng)
|
||
_ = alg.nextValue(elapsed: 0, counter: 1, rng: &rng)
|
||
#expect(alg.isExhausted)
|
||
alg.reset()
|
||
#expect(!alg.isExhausted)
|
||
let v: Double? = alg.nextValue(elapsed: 0, counter: 0, rng: &rng)
|
||
#expect(v == 42.0)
|
||
}
|
||
}
|
||
|
||
// MARK: - TemporalStream Integration Tests
|
||
|
||
@MainActor
|
||
@Suite("TemporalStreamTests")
|
||
struct TemporalStreamTests {
|
||
|
||
@Test("isExhausted mirrors algorithm")
|
||
func isExhaustedMirrorsAlgorithm() {
|
||
let seq: TemporalSequence = TemporalSequence(values: [.value(1), .value(2)], repeats: 1)
|
||
let stream: TemporalStream = TemporalStream(seq, seed: 1)
|
||
#expect(!stream.isExhausted)
|
||
_ = stream.next()
|
||
_ = stream.next()
|
||
#expect(stream.isExhausted)
|
||
#expect(stream.next() == nil)
|
||
}
|
||
|
||
@Test("reset clears start time so next call re-captures t0")
|
||
func resetClearsStartTime() {
|
||
let seq: TemporalSequence = TemporalSequence(values: [.value(5)], repeats: -1)
|
||
let stream: TemporalStream = TemporalStream(seq, seed: 42)
|
||
_ = stream.next()
|
||
stream.reset()
|
||
// After reset the algorithm is also reset so we can call again
|
||
let v: Double? = stream.next()
|
||
#expect(v == 5.0)
|
||
}
|
||
|
||
@Test("next returns nil when exhausted")
|
||
func nextReturnsNilWhenExhausted() {
|
||
let seq: TemporalSequence = TemporalSequence(values: [.value(1)], repeats: 1)
|
||
let stream: TemporalStream = TemporalStream(seq, seed: 1)
|
||
_ = stream.next()
|
||
#expect(stream.next() == nil)
|
||
#expect(stream.next() == nil)
|
||
}
|
||
|
||
@Test("counter increments correctly")
|
||
func counterIncrementsCorrectly() {
|
||
var receivedCounters: [Int] = []
|
||
let alg: TemporalClosure = TemporalClosure { _, counter, _ in
|
||
receivedCounters.append(counter)
|
||
return counter < 3 ? 1.0 : nil
|
||
}
|
||
let stream: TemporalStream = TemporalStream(alg, seed: 1)
|
||
while let _ = stream.next() {}
|
||
// Closure is called with 0, 1, 2 (return values) and then 3 (returns nil, exhausts).
|
||
// Counter only increments when result is non-nil, so closure sees 0, 1, 2, 3.
|
||
#expect(receivedCounters == [0, 1, 2, 3])
|
||
}
|
||
|
||
@Test("wall-clock elapsed is non-negative")
|
||
func wallClockElapsedNonNegative() {
|
||
var capturedElapsed: Double = -1
|
||
let alg: TemporalClosure = TemporalClosure { elapsed, _, _ in
|
||
capturedElapsed = elapsed
|
||
return nil
|
||
}
|
||
let stream: TemporalStream = TemporalStream(alg, seed: 1)
|
||
_ = stream.next()
|
||
#expect(capturedElapsed >= 0)
|
||
}
|
||
|
||
@Test("deterministic output with fixed seed")
|
||
func deterministicWithSeed() {
|
||
let makeStream: () -> TemporalStream = {
|
||
let pick: TemporalRandomPick = TemporalRandomPick(
|
||
values: [.value(1), .value(2), .value(3), .value(4)],
|
||
repeats: 10
|
||
)
|
||
return TemporalStream(pick, seed: 777)
|
||
}
|
||
let s1: TemporalStream = makeStream()
|
||
let s2: TemporalStream = makeStream()
|
||
var out1: [Double] = []
|
||
var out2: [Double] = []
|
||
while let v: Double = s1.next() { out1.append(v) }
|
||
while let v: Double = s2.next() { out2.append(v) }
|
||
#expect(out1 == out2)
|
||
}
|
||
|
||
@Test("beat-clock init compiles and produces a non-nil value")
|
||
func beatClockInitCompiles() {
|
||
// We can't easily test beat timing without a running engine, but we verify
|
||
// that the beat-clock code path compiles and executes without crashing.
|
||
let engine: SynthesisEngine = makeTestEngine()
|
||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120)
|
||
let seq: TemporalSequence = TemporalSequence(values: [.value(440)], repeats: 1)
|
||
let stream: TemporalStream = TemporalStream(seq, clock: clock, seed: 1)
|
||
let v: Double? = stream.next()
|
||
#expect(v == 440.0)
|
||
#expect(stream.isExhausted)
|
||
}
|
||
}
|