prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
// DemandBrownianTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandBrownian")
|
||||
struct DemandBrownianTests {
|
||||
|
||||
@Test func walkStaysWithinBounds() {
|
||||
let d: DemandBrownian = DemandBrownian(
|
||||
low: -10.0,
|
||||
high: 10.0,
|
||||
step: 5.0,
|
||||
length: 10_000,
|
||||
seed: 99
|
||||
)
|
||||
while let v: Double = d.nextValue() {
|
||||
#expect(v >= -10.0 && v <= 10.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func startDefaultsToMidpoint() {
|
||||
let d: DemandBrownian = DemandBrownian(
|
||||
low: 0.0,
|
||||
high: 100.0,
|
||||
step: 0.0,
|
||||
length: 1,
|
||||
seed: 1
|
||||
)
|
||||
#expect(d.nextValue() == 50.0)
|
||||
}
|
||||
|
||||
@Test func sameSeedSameStartProducesIdenticalWalk() {
|
||||
let a: DemandBrownian = DemandBrownian(
|
||||
low: -5.0, high: 5.0, step: 0.5, start: 0.0, length: 100, seed: 2024
|
||||
)
|
||||
let b: DemandBrownian = DemandBrownian(
|
||||
low: -5.0, high: 5.0, step: 0.5, start: 0.0, length: 100, seed: 2024
|
||||
)
|
||||
for _ in 0..<100 {
|
||||
#expect(a.nextValue() == b.nextValue())
|
||||
}
|
||||
}
|
||||
|
||||
@Test func resetRestoresStartPosition() {
|
||||
let d: DemandBrownian = DemandBrownian(
|
||||
low: -10.0, high: 10.0, step: 1.0, start: 7.0, length: -1, seed: 5
|
||||
)
|
||||
for _ in 0..<50 { _ = d.nextValue() }
|
||||
d.reset()
|
||||
if let v: Double = d.nextValue() {
|
||||
#expect(v >= 6.0 && v <= 8.0)
|
||||
} else {
|
||||
Issue.record("Expected non-nil value after reset")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// DemandConcatenateTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandConcatenate")
|
||||
struct DemandConcatenateTests {
|
||||
|
||||
@Test func playsEachInTurn() {
|
||||
let d: DemandConcatenate = DemandConcatenate(demands: [
|
||||
DemandSequence(values: [1.0, 2.0], repeats: 1),
|
||||
DemandSequence(values: [10.0, 20.0, 30.0], repeats: 1),
|
||||
DemandSequence(values: [100.0], repeats: 1)
|
||||
])
|
||||
var collected: [Double] = []
|
||||
while let v: Double = d.nextValue() { collected.append(v) }
|
||||
#expect(collected == [1.0, 2.0, 10.0, 20.0, 30.0, 100.0])
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func emptyListExhaustsImmediately() {
|
||||
let d: DemandConcatenate = DemandConcatenate(demands: [])
|
||||
#expect(d.nextValue() == nil)
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func skipsExhaustedSubSources() {
|
||||
let d: DemandConcatenate = DemandConcatenate(demands: [
|
||||
DemandSequence(values: [], repeats: 1),
|
||||
DemandSequence(values: [5.0], repeats: 1),
|
||||
DemandSequence(values: [], repeats: 1),
|
||||
DemandSequence(values: [10.0], repeats: 1)
|
||||
])
|
||||
var collected: [Double] = []
|
||||
while let v: Double = d.nextValue() { collected.append(v) }
|
||||
#expect(collected == [5.0, 10.0])
|
||||
}
|
||||
|
||||
@Test func resetReplaysAll() {
|
||||
let d: DemandConcatenate = DemandConcatenate(demands: [
|
||||
DemandSequence(values: [1.0, 2.0], repeats: 1),
|
||||
DemandSequence(values: [3.0, 4.0], repeats: 1)
|
||||
])
|
||||
var first: [Double] = []
|
||||
while let v: Double = d.nextValue() { first.append(v) }
|
||||
#expect(first == [1.0, 2.0, 3.0, 4.0])
|
||||
d.reset()
|
||||
var second: [Double] = []
|
||||
while let v: Double = d.nextValue() { second.append(v) }
|
||||
#expect(second == first)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,342 @@
|
||||
// DemandGeneratorTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - DemandSequence
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandSequence")
|
||||
struct DSequenceTests {
|
||||
|
||||
@Test func returnsValuesInOrder() {
|
||||
let seq: DemandSequence = DemandSequence(values: [1.0, 2.0, 3.0])
|
||||
#expect(seq.nextValue() == 1.0)
|
||||
#expect(seq.nextValue() == 2.0)
|
||||
#expect(seq.nextValue() == 3.0)
|
||||
}
|
||||
|
||||
@Test func wrapsAfterFullCycle() {
|
||||
let seq: DemandSequence = DemandSequence(values: [10.0, 20.0])
|
||||
_ = seq.nextValue(); _ = seq.nextValue() // complete cycle 1
|
||||
#expect(seq.nextValue() == 10.0) // cycle 2 starts
|
||||
}
|
||||
|
||||
@Test func infiniteRepeatsNeverExhausts() {
|
||||
let seq: DemandSequence = DemandSequence(values: [1.0, 2.0], repeats: -1)
|
||||
for _ in 0..<100 { _ = seq.nextValue() }
|
||||
#expect(!seq.isExhausted)
|
||||
#expect(seq.nextValue() != nil)
|
||||
}
|
||||
|
||||
@Test func exhaustsAfterNRepeats() {
|
||||
let seq: DemandSequence = DemandSequence(values: [1.0, 2.0, 3.0], repeats: 2)
|
||||
var count: Int = 0
|
||||
while let _ = seq.nextValue() { count += 1 }
|
||||
#expect(count == 6) // 3 values × 2 repeats
|
||||
#expect(seq.isExhausted)
|
||||
#expect(seq.nextValue() == nil)
|
||||
}
|
||||
|
||||
@Test func zeroRepeatsExhaustedImmediately() {
|
||||
let seq: DemandSequence = DemandSequence(values: [1.0, 2.0], repeats: 0)
|
||||
#expect(seq.isExhausted)
|
||||
#expect(seq.nextValue() == nil)
|
||||
}
|
||||
|
||||
@Test func resetRestartsFromBeginning() {
|
||||
let seq: DemandSequence = DemandSequence(values: [1.0, 2.0], repeats: 1)
|
||||
_ = seq.nextValue(); _ = seq.nextValue() // exhaust
|
||||
#expect(seq.isExhausted)
|
||||
seq.reset()
|
||||
#expect(!seq.isExhausted)
|
||||
#expect(seq.nextValue() == 1.0)
|
||||
}
|
||||
|
||||
@Test func emptyValuesAlwaysNil() {
|
||||
let seq: DemandSequence = DemandSequence(values: [])
|
||||
#expect(seq.nextValue() == nil)
|
||||
#expect(!seq.isExhausted) // empty ≠ exhausted
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - DemandRandom
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandRandom")
|
||||
struct DRandomTests {
|
||||
|
||||
@Test func returnsValuesFromPool() {
|
||||
let pool: [Double] = [10.0, 20.0, 30.0, 40.0]
|
||||
let rand: DemandRandom = DemandRandom(values: pool, repeats: 20)
|
||||
for _ in 0..<20 {
|
||||
guard let v: Double = rand.nextValue() else { Issue.record("unexpectedly nil"); return }
|
||||
#expect(pool.contains(v))
|
||||
}
|
||||
}
|
||||
|
||||
@Test func infiniteRepeatsNeverExhausts() {
|
||||
let rand: DemandRandom = DemandRandom(values: [1.0, 2.0, 3.0], repeats: -1)
|
||||
for _ in 0..<100 { _ = rand.nextValue() }
|
||||
#expect(!rand.isExhausted)
|
||||
#expect(rand.nextValue() != nil)
|
||||
}
|
||||
|
||||
@Test func exhaustsAfterNResponses() {
|
||||
let rand: DemandRandom = DemandRandom(values: [1.0, 2.0, 3.0], repeats: 5)
|
||||
var count: Int = 0
|
||||
while let _ = rand.nextValue() { count += 1 }
|
||||
#expect(count == 5)
|
||||
#expect(rand.isExhausted)
|
||||
}
|
||||
|
||||
@Test func resetRestartsResponseCount() {
|
||||
let rand: DemandRandom = DemandRandom(values: [1.0, 2.0], repeats: 3)
|
||||
for _ in 0..<3 { _ = rand.nextValue() }
|
||||
#expect(rand.isExhausted)
|
||||
rand.reset()
|
||||
#expect(!rand.isExhausted)
|
||||
var count: Int = 0
|
||||
while let _ = rand.nextValue() { count += 1 }
|
||||
#expect(count == 3)
|
||||
}
|
||||
|
||||
@Test func deterministicWithSameSeed() {
|
||||
let seed: UInt64 = 12345
|
||||
let r1: DemandRandom = DemandRandom(values: [1.0, 2.0, 3.0, 4.0, 5.0], repeats: 10, seed: seed)
|
||||
let r2: DemandRandom = DemandRandom(values: [1.0, 2.0, 3.0, 4.0, 5.0], repeats: 10, seed: seed)
|
||||
for _ in 0..<10 {
|
||||
#expect(r1.nextValue() == r2.nextValue())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Demand.Audio
|
||||
|
||||
@MainActor
|
||||
@Suite("Demand.Audio")
|
||||
struct DemandAudioTests {
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let d: Demand.Audio = Demand.Audio(trig: 0.0, demand: DemandSequence(values: [1.0]))
|
||||
#expect(d.rate == .audio)
|
||||
}
|
||||
|
||||
@Test func holdsZeroBeforeFirstTrigger() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
let d: Demand.Audio = Demand.Audio(trig: 0.0, demand: DemandSequence(values: [10.0, 20.0]))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [Double] = d.calculatedSamples(graph: graph, count: 16)[0]
|
||||
for s: Double in samples { #expect(s == 0.0) }
|
||||
}
|
||||
|
||||
@Test func advancesOnRisingEdge() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
// Trigger goes high at sample 4, stays high.
|
||||
let trig: Closure = Closure { _, i in i >= 4 ? 1.0 : 0.0 }
|
||||
let d: Demand.Audio = Demand.Audio(trig: trig, demand: DemandSequence(values: [99.0, 200.0]))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [Double] = d.calculatedSamples(graph: graph, count: 16)[0]
|
||||
// Samples 0-3: 0 (no trigger), sample 4+: 99.0
|
||||
for i: Int in 0..<4 { #expect(samples[i] == 0.0) }
|
||||
for i: Int in 4..<16 { #expect(samples[i] == 99.0) }
|
||||
}
|
||||
|
||||
@Test func multipleTriggersInOneBlock() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
// Two rising edges: at sample 2 (2-3 high) and sample 8 (8-9 high).
|
||||
let trig: Closure = Closure { _, i in
|
||||
(i == 2 || i == 3 || i == 8 || i == 9) ? 1.0 : 0.0
|
||||
}
|
||||
let d: Demand.Audio = Demand.Audio(trig: trig, demand: DemandSequence(values: [10.0, 20.0, 30.0]))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [Double] = d.calculatedSamples(graph: graph, count: 16)[0]
|
||||
#expect(samples[0] == 0.0) // before first trigger
|
||||
#expect(samples[2] == 10.0) // first trigger → 10
|
||||
#expect(samples[7] == 10.0) // still holding 10
|
||||
#expect(samples[8] == 20.0) // second trigger → 20
|
||||
}
|
||||
|
||||
@Test func holdsLastValueAfterExhaustion() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
// Trigger rises at sample 0 (prevTrig = 0 initially), stays high.
|
||||
let trig: Closure = Closure { _, _ in 1.0 }
|
||||
let d: Demand.Audio = Demand.Audio(trig: trig, demand: DemandSequence(values: [42.0], repeats: 1))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [Double] = d.calculatedSamples(graph: graph, count: 16)[0]
|
||||
// Rising edge at sample 0 → 42.0. DemandSequence exhausted. No further rising edges (stays high).
|
||||
for s: Double in samples.prefix(16) { #expect(s == 42.0) }
|
||||
|
||||
// Advance to next block; trig stays high → no new rising edge → still 42.0.
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples2: [Double] = d.calculatedSamples(graph: graph2, count: 16)[0]
|
||||
for s: Double in samples2.prefix(16) { #expect(s == 42.0) }
|
||||
}
|
||||
|
||||
@Test func resetInputClearsHeldValue() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
var block: Int = 0
|
||||
// Block 0: trigger at sample 0 → held = 77.0.
|
||||
// Block 1: reset at sample 0, no trigger → held clears to 0.
|
||||
let trig: Closure = Closure { _, i in block == 0 && i == 0 ? 1.0 : 0.0 }
|
||||
let reset: Closure = Closure { _, i in block == 1 && i == 0 ? 1.0 : 0.0 }
|
||||
let d: Demand.Audio = Demand.Audio(trig: trig, reset: reset, demand: DemandSequence(values: [77.0, 88.0]))
|
||||
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let s1: [Double] = d.calculatedSamples(graph: g1, count: 8)[0]
|
||||
#expect(s1[0] == 77.0)
|
||||
|
||||
block = 1
|
||||
advanceBlock(engine)
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let s2: [Double] = d.calculatedSamples(graph: g2, count: 8)[0]
|
||||
// reset fired at sample 0 → _heldValue = 0; seq also reset to index 0.
|
||||
for s: Double in s2 { #expect(s == 0.0) }
|
||||
}
|
||||
|
||||
@Test func resetThenTriggerSameSample() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
let seq: DemandSequence = DemandSequence(values: [10.0, 20.0, 30.0])
|
||||
// Consume first value so reset effect is observable.
|
||||
_ = seq.nextValue() // 10.0 consumed; seq at index 1
|
||||
|
||||
// Reset and trigger both cross at sample 0 simultaneously.
|
||||
let combined: Closure = Closure { _, i in i == 0 ? 1.0 : 0.0 }
|
||||
let d: Demand.Audio = Demand.Audio(trig: combined, reset: combined, demand: seq)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [Double] = d.calculatedSamples(graph: graph, count: 16)[0]
|
||||
// reset fires first → seq.reset() → index back to 0
|
||||
// trigger then pulls → 10.0 (first value again, not 20.0)
|
||||
#expect(samples[0] == 10.0)
|
||||
}
|
||||
|
||||
@Test func blockBoundaryTrigger() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
var trigHigh: Bool = false
|
||||
let trig: Closure = Closure { _, _ in trigHigh ? 1.0 : 0.0 }
|
||||
let d: Demand.Audio = Demand.Audio(trig: trig, demand: DemandSequence(values: [55.0]))
|
||||
|
||||
// Block 1: trig = 0 → _prevTrig persists as 0 on d after this block.
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let s1: [Double] = d.calculatedSamples(graph: g1, count: 8)[0]
|
||||
for s: Double in s1.prefix(8) { #expect(s == 0.0) }
|
||||
|
||||
// Block 2: trig goes high from sample 0. Same instance d — _prevTrig = 0 → rising edge.
|
||||
trigHigh = true
|
||||
advanceBlock(engine)
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let s2: [Double] = d.calculatedSamples(graph: g2, count: 8)[0]
|
||||
// _prevTrig = 0 (carried from block 1), trig[0] = 1 → edge detected on same instance.
|
||||
#expect(s2[0] == 55.0)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Demand.Control
|
||||
|
||||
@MainActor
|
||||
@Suite("Demand.Control")
|
||||
struct DemandControlTests {
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let d: Demand.Control = Demand.Control(trig: 0.0, demand: DemandSequence(values: [1.0]))
|
||||
#expect(d.rate == .control)
|
||||
}
|
||||
|
||||
@Test func holdsZeroBeforeFirstTrigger() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
let d: Demand.Control = Demand.Control(trig: 0.0, demand: DemandSequence(values: [10.0]))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = d.calculatedSamples(graph: graph, count: 16)
|
||||
#expect(d.previousOutputValue == 0.0)
|
||||
}
|
||||
|
||||
@Test func advancesOnRisingEdgeMidBlock() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
// Trigger crosses at sample 8.
|
||||
let trig: Closure = Closure { _, i in i >= 8 ? 1.0 : 0.0 }
|
||||
let d: Demand.Control = Demand.Control(trig: trig, demand: DemandSequence(values: [100.0]))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = d.calculatedSamples(graph: graph, count: 16)
|
||||
// Rising edge mid-block → heldValue = 100.0 → previousOutputValue = 100.0
|
||||
#expect(d.previousOutputValue == 100.0)
|
||||
}
|
||||
|
||||
@Test func outputIsRampToNewValue() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 4)
|
||||
// trig always high → rising edge at sample 0 (prevTrig = 0 initially).
|
||||
// _rampTarget = 0, _heldValue becomes 40.0. Ramp 0 → 40 over 4 samples.
|
||||
let trig: Closure = Closure { _, _ in 1.0 }
|
||||
let d: Demand.Control = Demand.Control(trig: trig, demand: DemandSequence(values: [40.0]))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [Double] = d.calculatedSamples(graph: graph, count: 4)[0]
|
||||
#expect(samples[0] == 10.0)
|
||||
#expect(samples[1] == 20.0)
|
||||
#expect(samples[2] == 30.0)
|
||||
#expect(samples[3] == 40.0)
|
||||
}
|
||||
|
||||
@Test func holdsAfterExhaustion() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
// Rising edge at sample 0 → 7.0. DemandSequence exhausted. Trig stays high → no new edge.
|
||||
let trig: Closure = Closure { _, _ in 1.0 }
|
||||
let d: Demand.Control = Demand.Control(trig: trig, demand: DemandSequence(values: [7.0], repeats: 1))
|
||||
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = d.calculatedSamples(graph: g1, count: 8)
|
||||
#expect(d.previousOutputValue == 7.0)
|
||||
|
||||
advanceBlock(engine)
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = d.calculatedSamples(graph: g2, count: 8)
|
||||
#expect(d.previousOutputValue == 7.0)
|
||||
}
|
||||
|
||||
@Test func resetClearsHeldValueAndRamps() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 4)
|
||||
// Reset at sample 0 (prevReset = 0 initially) → demand.reset(), heldValue = 0.
|
||||
// Ramp: _rampTarget = 0, _heldValue = 0 → flat at 0.
|
||||
let resetSig: Closure = Closure { _, _ in 1.0 }
|
||||
let d: Demand.Control = Demand.Control(trig: 0.0, reset: resetSig, demand: DemandSequence(values: [50.0]))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = d.calculatedSamples(graph: graph, count: 4)
|
||||
#expect(d.previousOutputValue == 0.0)
|
||||
}
|
||||
|
||||
@Test func floatValueDoesNotAdvanceSequence() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
// Single puller owns the sequence. trig always-high → rising edge at sample 0.
|
||||
let d: Demand.Control = Demand.Control(
|
||||
trig: Closure { _, _ in 1.0 },
|
||||
demand: DemandSequence(values: [11.0, 22.0, 33.0])
|
||||
)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// floatValue returns previousOutputValue (0) without calling nextValue on seq.
|
||||
let fv: Double = d.floatValue(graph: graph)
|
||||
#expect(fv == 0.0)
|
||||
// calculatedSamples triggers on the same block → must pull 11.0, not 22.0.
|
||||
_ = d.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(d.previousOutputValue == 11.0)
|
||||
}
|
||||
|
||||
@Test func resetThenTriggerSameBlock() {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
let seq: DemandSequence = DemandSequence(values: [10.0, 20.0, 30.0])
|
||||
// Advance seq past first value so reset effect is observable.
|
||||
_ = seq.nextValue() // 10.0 consumed; seq at index 1
|
||||
|
||||
// Reset fires at sample 2, trigger at sample 6 (both are rising edges from 0).
|
||||
let reset: Closure = Closure { _, i in i == 2 ? 1.0 : 0.0 }
|
||||
let trig: Closure = Closure { _, i in i == 6 ? 1.0 : 0.0 }
|
||||
let d: Demand.Control = Demand.Control(trig: trig, reset: reset, demand: seq)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = d.calculatedSamples(graph: graph, count: 16)
|
||||
// reset fires first (sample 2) → seq.reset(), heldValue = 0, didTrigger cleared.
|
||||
// trigger fires after (sample 6) → pulls 10.0 (first value of fresh sequence).
|
||||
#expect(d.previousOutputValue == 10.0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// DemandGeometricTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandGeometric")
|
||||
struct DemandGeometricTests {
|
||||
|
||||
@Test func emitsGeometricSequence() {
|
||||
let d: DemandGeometric = DemandGeometric(start: 1.0, ratio: 2.0, length: 4)
|
||||
#expect(d.nextValue() == 1.0)
|
||||
#expect(d.nextValue() == 2.0)
|
||||
#expect(d.nextValue() == 4.0)
|
||||
#expect(d.nextValue() == 8.0)
|
||||
#expect(d.nextValue() == nil)
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func fractionalRatioDecays() {
|
||||
let d: DemandGeometric = DemandGeometric(start: 100.0, ratio: 0.5, length: 3)
|
||||
#expect(d.nextValue() == 100.0)
|
||||
#expect(d.nextValue() == 50.0)
|
||||
#expect(d.nextValue() == 25.0)
|
||||
}
|
||||
|
||||
@Test func resetRestoresInitialValue() {
|
||||
let d: DemandGeometric = DemandGeometric(start: 3.0, ratio: 2.0, length: -1)
|
||||
for _ in 0..<5 { _ = d.nextValue() }
|
||||
d.reset()
|
||||
#expect(d.nextValue() == 3.0)
|
||||
#expect(d.nextValue() == 6.0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// DemandNonRepeatingRandomTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandNonRepeatingRandom")
|
||||
struct DemandNonRepeatingRandomTests {
|
||||
|
||||
@Test func neverRepeatsSameIndexConsecutively() {
|
||||
let d: DemandNonRepeatingRandom = DemandNonRepeatingRandom(
|
||||
values: [1.0, 2.0, 3.0, 4.0, 5.0],
|
||||
repeats: 1_000,
|
||||
seed: 42
|
||||
)
|
||||
var prev: Double? = nil
|
||||
while let v: Double = d.nextValue() {
|
||||
if let p: Double = prev {
|
||||
#expect(v != p, "Consecutive repeat: \(p) → \(v)")
|
||||
}
|
||||
prev = v
|
||||
}
|
||||
}
|
||||
|
||||
@Test func singleValueAlwaysReturnsThatValue() {
|
||||
let d: DemandNonRepeatingRandom = DemandNonRepeatingRandom(
|
||||
values: [7.0],
|
||||
repeats: 5,
|
||||
seed: 1
|
||||
)
|
||||
for _ in 0..<5 { #expect(d.nextValue() == 7.0) }
|
||||
#expect(d.nextValue() == nil)
|
||||
}
|
||||
|
||||
@Test func sameSeedProducesIdenticalSequence() {
|
||||
let a: DemandNonRepeatingRandom = DemandNonRepeatingRandom(
|
||||
values: [10.0, 20.0, 30.0, 40.0], repeats: 50, seed: 99
|
||||
)
|
||||
let b: DemandNonRepeatingRandom = DemandNonRepeatingRandom(
|
||||
values: [10.0, 20.0, 30.0, 40.0], repeats: 50, seed: 99
|
||||
)
|
||||
for _ in 0..<50 {
|
||||
#expect(a.nextValue() == b.nextValue())
|
||||
}
|
||||
}
|
||||
|
||||
@Test func resetClearsExhaustionAndLastPick() {
|
||||
let d: DemandNonRepeatingRandom = DemandNonRepeatingRandom(
|
||||
values: [1.0, 2.0, 3.0], repeats: 3, seed: 1
|
||||
)
|
||||
for _ in 0..<3 { _ = d.nextValue() }
|
||||
#expect(d.isExhausted)
|
||||
d.reset()
|
||||
#expect(!d.isExhausted)
|
||||
#expect(d.nextValue() != nil)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// DemandSeriesTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandSeries")
|
||||
struct DemandSeriesTests {
|
||||
|
||||
@Test func emitsArithmeticSequence() {
|
||||
let d: DemandSeries = DemandSeries(start: 0.0, step: 2.0, length: 4)
|
||||
#expect(d.nextValue() == 0.0)
|
||||
#expect(d.nextValue() == 2.0)
|
||||
#expect(d.nextValue() == 4.0)
|
||||
#expect(d.nextValue() == 6.0)
|
||||
#expect(d.nextValue() == nil)
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func infiniteLengthNeverExhausts() {
|
||||
let d: DemandSeries = DemandSeries(start: 1.0, step: 1.0, length: -1)
|
||||
for _ in 0..<1_000 { _ = d.nextValue() }
|
||||
#expect(!d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func resetRestartsFromStart() {
|
||||
let d: DemandSeries = DemandSeries(start: 5.0, step: 1.0, length: 2)
|
||||
_ = d.nextValue(); _ = d.nextValue()
|
||||
#expect(d.isExhausted)
|
||||
d.reset()
|
||||
#expect(!d.isExhausted)
|
||||
#expect(d.nextValue() == 5.0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// DemandShuffleTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandShuffle")
|
||||
struct DemandShuffleTests {
|
||||
|
||||
@Test func returnsAllValuesExactlyOncePerCycle() {
|
||||
let d: DemandShuffle = DemandShuffle(
|
||||
values: [1.0, 2.0, 3.0, 4.0, 5.0],
|
||||
repeats: 1,
|
||||
seed: 42
|
||||
)
|
||||
var collected: [Double] = []
|
||||
while let v: Double = d.nextValue() { collected.append(v) }
|
||||
#expect(collected.sorted() == [1.0, 2.0, 3.0, 4.0, 5.0])
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func samePermutationReplaysOnReset() {
|
||||
let d: DemandShuffle = DemandShuffle(
|
||||
values: [1.0, 2.0, 3.0, 4.0],
|
||||
repeats: -1,
|
||||
seed: 7
|
||||
)
|
||||
let first: [Double] = (0..<4).compactMap { _ in d.nextValue() }
|
||||
d.reset()
|
||||
let second: [Double] = (0..<4).compactMap { _ in d.nextValue() }
|
||||
#expect(first == second)
|
||||
}
|
||||
|
||||
@Test func sameSeedProducesSamePermutation() {
|
||||
let a: DemandShuffle = DemandShuffle(values: [10.0, 20.0, 30.0, 40.0], repeats: 1, seed: 99)
|
||||
let b: DemandShuffle = DemandShuffle(values: [10.0, 20.0, 30.0, 40.0], repeats: 1, seed: 99)
|
||||
for _ in 0..<4 {
|
||||
#expect(a.nextValue() == b.nextValue())
|
||||
}
|
||||
}
|
||||
|
||||
@Test func resetClearsExhaustion() {
|
||||
let d: DemandShuffle = DemandShuffle(
|
||||
values: [1.0, 2.0, 3.0],
|
||||
repeats: 1,
|
||||
seed: 5
|
||||
)
|
||||
while d.nextValue() != nil {}
|
||||
#expect(d.isExhausted)
|
||||
d.reset()
|
||||
#expect(!d.isExhausted)
|
||||
#expect(d.nextValue() != nil)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// DemandStutterTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandStutter")
|
||||
struct DemandStutterTests {
|
||||
|
||||
@Test func repeatsEachValueByCount() {
|
||||
let d: DemandStutter = DemandStutter(
|
||||
demand: DemandSequence(values: [10.0, 20.0, 30.0], repeats: 1),
|
||||
count: 2
|
||||
)
|
||||
var collected: [Double] = []
|
||||
while let v: Double = d.nextValue() { collected.append(v) }
|
||||
#expect(collected == [10.0, 10.0, 20.0, 20.0, 30.0, 30.0])
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func countOneIsPassThrough() {
|
||||
let d: DemandStutter = DemandStutter(
|
||||
demand: DemandSequence(values: [1.0, 2.0, 3.0], repeats: 1),
|
||||
count: 1
|
||||
)
|
||||
var collected: [Double] = []
|
||||
while let v: Double = d.nextValue() { collected.append(v) }
|
||||
#expect(collected == [1.0, 2.0, 3.0])
|
||||
}
|
||||
|
||||
@Test func upstreamExhaustionLatchesEngine() {
|
||||
let d: DemandStutter = DemandStutter(
|
||||
demand: DemandSequence(values: [5.0], repeats: 1),
|
||||
count: 3
|
||||
)
|
||||
#expect(d.nextValue() == 5.0)
|
||||
#expect(d.nextValue() == 5.0)
|
||||
#expect(d.nextValue() == 5.0)
|
||||
#expect(d.nextValue() == nil)
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func resetRestartsUpstreamAndCount() {
|
||||
let d: DemandStutter = DemandStutter(
|
||||
demand: DemandSequence(values: [7.0, 8.0], repeats: 1),
|
||||
count: 2
|
||||
)
|
||||
while d.nextValue() != nil {} // drain to trigger latch
|
||||
#expect(d.isExhausted)
|
||||
d.reset()
|
||||
#expect(!d.isExhausted)
|
||||
#expect(d.nextValue() == 7.0)
|
||||
#expect(d.nextValue() == 7.0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// DemandSwitchTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandSwitch")
|
||||
struct DemandSwitchTests {
|
||||
|
||||
@Test func pullsByIndex() {
|
||||
let d: DemandSwitch = DemandSwitch(
|
||||
demands: [
|
||||
DemandSequence(values: [1.0, 2.0, 3.0], repeats: -1),
|
||||
DemandSequence(values: [10.0, 20.0, 30.0], repeats: -1)
|
||||
],
|
||||
index: DemandSequence(values: [0.0, 1.0, 0.0, 1.0], repeats: 1)
|
||||
)
|
||||
#expect(d.nextValue() == 1.0)
|
||||
#expect(d.nextValue() == 10.0)
|
||||
#expect(d.nextValue() == 2.0)
|
||||
#expect(d.nextValue() == 20.0)
|
||||
#expect(d.nextValue() == nil)
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func indexExhaustionLatches() {
|
||||
let d: DemandSwitch = DemandSwitch(
|
||||
demands: [DemandSequence(values: [100.0], repeats: -1)],
|
||||
index: DemandSequence(values: [0.0], repeats: 1)
|
||||
)
|
||||
#expect(d.nextValue() == 100.0)
|
||||
#expect(d.nextValue() == nil)
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func subSourceExhaustionLatches() {
|
||||
let d: DemandSwitch = DemandSwitch(
|
||||
demands: [DemandSequence(values: [7.0, 8.0], repeats: 1)],
|
||||
index: DemandSequence(values: [0.0], repeats: -1)
|
||||
)
|
||||
#expect(d.nextValue() == 7.0)
|
||||
#expect(d.nextValue() == 8.0)
|
||||
#expect(d.nextValue() == nil)
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func outOfRangeIndexLatches() {
|
||||
let d: DemandSwitch = DemandSwitch(
|
||||
demands: [DemandSequence(values: [1.0], repeats: -1)],
|
||||
index: DemandSequence(values: [0.0, 99.0], repeats: 1)
|
||||
)
|
||||
#expect(d.nextValue() == 1.0)
|
||||
#expect(d.nextValue() == nil)
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func negativeFractionalIndexFloorsToLatch() {
|
||||
// Index -0.5 floors to -1 (out of range) and latches, rather than
|
||||
// truncating toward zero (which would silently pick demands[0]).
|
||||
let d: DemandSwitch = DemandSwitch(
|
||||
demands: [DemandSequence(values: [42.0], repeats: -1)],
|
||||
index: DemandSequence(values: [-0.5], repeats: 1)
|
||||
)
|
||||
#expect(d.nextValue() == nil)
|
||||
#expect(d.isExhausted)
|
||||
}
|
||||
|
||||
@Test func resetRestartsAll() {
|
||||
let d: DemandSwitch = DemandSwitch(
|
||||
demands: [
|
||||
DemandSequence(values: [1.0, 2.0], repeats: -1),
|
||||
DemandSequence(values: [10.0, 20.0], repeats: -1)
|
||||
],
|
||||
index: DemandSequence(values: [0.0, 1.0], repeats: 1)
|
||||
)
|
||||
_ = d.nextValue(); _ = d.nextValue(); _ = d.nextValue()
|
||||
#expect(d.isExhausted)
|
||||
d.reset()
|
||||
#expect(!d.isExhausted)
|
||||
#expect(d.nextValue() == 1.0)
|
||||
#expect(d.nextValue() == 10.0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// DemandUniformRandomTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandUniformRandom")
|
||||
struct DemandUniformRandomTests {
|
||||
|
||||
@Test func samplesStayWithinRange() {
|
||||
let d: DemandUniformRandom = DemandUniformRandom(
|
||||
low: -5.0,
|
||||
high: 5.0,
|
||||
length: 1_000,
|
||||
seed: 42
|
||||
)
|
||||
var count: Int = 0
|
||||
while let v: Double = d.nextValue() {
|
||||
#expect(v >= -5.0 && v <= 5.0)
|
||||
count += 1
|
||||
}
|
||||
#expect(count == 1_000)
|
||||
}
|
||||
|
||||
@Test func sameSeedProducesIdenticalSequence() {
|
||||
let a: DemandUniformRandom = DemandUniformRandom(low: 0.0, high: 1.0, length: 50, seed: 99)
|
||||
let b: DemandUniformRandom = DemandUniformRandom(low: 0.0, high: 1.0, length: 50, seed: 99)
|
||||
for _ in 0..<50 {
|
||||
#expect(a.nextValue() == b.nextValue())
|
||||
}
|
||||
}
|
||||
|
||||
@Test func resetClearsExhaustion() {
|
||||
let d: DemandUniformRandom = DemandUniformRandom(low: 0.0, high: 1.0, length: 3, seed: 7)
|
||||
for _ in 0..<3 { _ = d.nextValue() }
|
||||
#expect(d.isExhausted)
|
||||
d.reset()
|
||||
#expect(!d.isExhausted)
|
||||
#expect(d.nextValue() != nil)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// DemandWeightedRandomTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandWeightedRandom")
|
||||
struct DemandWeightedRandomTests {
|
||||
|
||||
@Test func zeroWeightValueNeverPicked() {
|
||||
let d: DemandWeightedRandom = DemandWeightedRandom(
|
||||
values: [1.0, 2.0, 3.0],
|
||||
weights: [1.0, 0.0, 1.0],
|
||||
repeats: 2_000,
|
||||
seed: 42
|
||||
)
|
||||
while let v: Double = d.nextValue() {
|
||||
#expect(v != 2.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func biasedDistributionFavoursHighWeight() {
|
||||
// 9:1 ratio → ~90 % of picks favour the first value.
|
||||
let n: Int = 5_000
|
||||
let d: DemandWeightedRandom = DemandWeightedRandom(
|
||||
values: [100.0, 200.0],
|
||||
weights: [9.0, 1.0],
|
||||
repeats: n,
|
||||
seed: 99
|
||||
)
|
||||
var lowCount: Int = 0
|
||||
while let v: Double = d.nextValue() {
|
||||
if v == 100.0 { lowCount += 1 }
|
||||
}
|
||||
let fraction: Double = Double(lowCount) / Double(n)
|
||||
#expect(abs(fraction - 0.9) < 0.03)
|
||||
}
|
||||
|
||||
@Test func sameSeedProducesIdenticalSequence() {
|
||||
let a: DemandWeightedRandom = DemandWeightedRandom(
|
||||
values: [10.0, 20.0, 30.0],
|
||||
weights: [1.0, 2.0, 3.0],
|
||||
repeats: 50,
|
||||
seed: 77
|
||||
)
|
||||
let b: DemandWeightedRandom = DemandWeightedRandom(
|
||||
values: [10.0, 20.0, 30.0],
|
||||
weights: [1.0, 2.0, 3.0],
|
||||
repeats: 50,
|
||||
seed: 77
|
||||
)
|
||||
for _ in 0..<50 {
|
||||
#expect(a.nextValue() == b.nextValue())
|
||||
}
|
||||
}
|
||||
|
||||
@Test func resetClearsExhaustion() {
|
||||
let d: DemandWeightedRandom = DemandWeightedRandom(
|
||||
values: [1.0, 2.0],
|
||||
weights: [1.0, 1.0],
|
||||
repeats: 3,
|
||||
seed: 13
|
||||
)
|
||||
for _ in 0..<3 { _ = d.nextValue() }
|
||||
#expect(d.isExhausted)
|
||||
d.reset()
|
||||
#expect(!d.isExhausted)
|
||||
#expect(d.nextValue() != nil)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user