105 lines
3.5 KiB
Swift
105 lines
3.5 KiB
Swift
// DebugTapTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Synchronization
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// The debug tap's invocation is compiled out of release builds (`#if DEBUG` around the
|
|
// call site in `SynthesizerBase.evaluate`). These tests assert the tap fires, so they are
|
|
// meaningful only in a debug configuration — which is how `swift test` runs by default.
|
|
#if DEBUG
|
|
|
|
/// Records every `debugTap` invocation and the arguments it received.
|
|
///
|
|
/// Counters use `Atomic` because `debugTap` runs on the render thread; the offline driver
|
|
/// processes synchronously in-test, but atomics keep the fixture race-free regardless.
|
|
private final class DebugTapProbe: SynthesizerBase {
|
|
let bus: Int
|
|
let tapCount: Atomic<Int> = Atomic<Int>(0)
|
|
let lastOutputCount: Atomic<Int> = Atomic<Int>(-1)
|
|
let lastCount: Atomic<Int> = Atomic<Int>(-1)
|
|
|
|
init(bus: Int = 0) {
|
|
self.bus = bus
|
|
super.init()
|
|
}
|
|
|
|
override func build() {
|
|
// `output(bus:source:)` registers exactly once (Out.Audio self-registers via
|
|
// RootRegistering); an explicit `addOutputGenerator(Out.Audio(...))` would double-count.
|
|
output(bus: bus, source: 0.25)
|
|
}
|
|
|
|
override func debugTap(
|
|
graph: SynthGraph,
|
|
count: Int,
|
|
outputs: [any Generator]
|
|
) {
|
|
_ = tapCount.wrappingAdd(1, ordering: .relaxed)
|
|
lastOutputCount.store(outputs.count, ordering: .relaxed)
|
|
lastCount.store(count, ordering: .relaxed)
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
@Suite("SynthesizerBase debug tap")
|
|
struct DebugTapTests {
|
|
let engine: SynthesisEngine = makeTestEngine()
|
|
|
|
init() throws {
|
|
try engine.start()
|
|
}
|
|
|
|
@Test func tapDoesNotFireWhenDisabled() throws {
|
|
let probe: DebugTapProbe = DebugTapProbe()
|
|
try engine.add(probe)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(probe.tapCount.load(ordering: .relaxed) == 0)
|
|
}
|
|
|
|
@Test func tapFiresWhenEnabled() throws {
|
|
let probe: DebugTapProbe = DebugTapProbe()
|
|
probe.debugTapEnabled = true
|
|
try engine.add(probe)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(probe.tapCount.load(ordering: .relaxed) >= 1)
|
|
}
|
|
|
|
@Test func tapReceivesOutputsAndFullBlockCount() throws {
|
|
let probe: DebugTapProbe = DebugTapProbe()
|
|
probe.debugTapEnabled = true
|
|
try engine.add(probe)
|
|
engine.processBlock(inputSamples: [])
|
|
// One `Out.Audio` was registered in build().
|
|
#expect(probe.lastOutputCount.load(ordering: .relaxed) == 1)
|
|
// No scheduled events this block, so the segment spans the whole block.
|
|
#expect(probe.lastCount.load(ordering: .relaxed) == engine.blockSize)
|
|
}
|
|
|
|
@Test func gatingIsPerInstance() throws {
|
|
let on: DebugTapProbe = DebugTapProbe(bus: 0)
|
|
let off: DebugTapProbe = DebugTapProbe(bus: 1)
|
|
on.debugTapEnabled = true
|
|
try engine.add(on)
|
|
try engine.add(off)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(on.tapCount.load(ordering: .relaxed) >= 1)
|
|
#expect(off.tapCount.load(ordering: .relaxed) == 0)
|
|
}
|
|
|
|
@Test func flagRoundTripsThroughAtomicProperty() throws {
|
|
let probe: DebugTapProbe = DebugTapProbe()
|
|
#expect(probe.debugTapEnabled == false)
|
|
probe.debugTapEnabled = true
|
|
#expect(probe.debugTapEnabled == true)
|
|
probe.debugTapEnabled = false
|
|
#expect(probe.debugTapEnabled == false)
|
|
}
|
|
}
|
|
|
|
#endif
|