148 lines
5.8 KiB
Swift
148 lines
5.8 KiB
Swift
// EngineNodeCountTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("Engine node counts")
|
|
struct EngineNodeCountTests {
|
|
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 16)
|
|
|
|
init() throws {
|
|
try engine.start()
|
|
}
|
|
|
|
@Test func freshEngineCountsAreZero() throws {
|
|
#expect(engine.synthesizerCount == 0)
|
|
#expect(engine.groupCount == 0)
|
|
}
|
|
|
|
@Test func addingLeafSynthBumpsSynthesizerCount() throws {
|
|
try engine.add(ConstantSynth(value: 0.1, outputBus: 0))
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 1)
|
|
#expect(engine.groupCount == 0)
|
|
}
|
|
|
|
@Test func removingLeafSynthDecrementsSynthesizerCount() throws {
|
|
let s: ConstantSynth = ConstantSynth(value: 0.1, outputBus: 0)
|
|
try engine.add(s)
|
|
engine.processBlock(inputSamples: [])
|
|
engine.remove(s)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 0)
|
|
#expect(engine.groupCount == 0)
|
|
}
|
|
|
|
@Test func addingGroupBumpsGroupCount() throws {
|
|
let g: Group = Group()
|
|
try engine.add(g)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 0)
|
|
#expect(engine.groupCount == 1)
|
|
}
|
|
|
|
@Test func addingParallelGroupBumpsGroupCount() throws {
|
|
let pg: ParallelGroup = ParallelGroup(engine: engine)
|
|
pg.add(ConstantSynth(value: 0.1, outputBus: 0))
|
|
try engine.add(pg)
|
|
engine.processBlock(inputSamples: [])
|
|
// ParallelGroup is the group; its child synth is *internal* to the parallel
|
|
// group and is not counted at the engine level (it never traverses applyEvent).
|
|
#expect(engine.groupCount == 1)
|
|
#expect(engine.synthesizerCount == 0)
|
|
}
|
|
|
|
@Test func addingMixOfSynthsAndGroupsCountsBothIndependently() throws {
|
|
try engine.add(ConstantSynth(value: 0.1, outputBus: 0))
|
|
try engine.add(ConstantSynth(value: 0.2, outputBus: 0))
|
|
try engine.add(Group())
|
|
try engine.add(Group())
|
|
try engine.add(Group())
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 2)
|
|
#expect(engine.groupCount == 3)
|
|
}
|
|
|
|
@Test func freeingGroupDecrementsGroupCountAndItsDirectChildren() throws {
|
|
let g: Group = Group()
|
|
try engine.add(g)
|
|
try engine.add(ConstantSynth(value: 0.1, outputBus: 0), position: .tail(of: g))
|
|
try engine.add(ConstantSynth(value: 0.2, outputBus: 0), position: .tail(of: g))
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 2)
|
|
#expect(engine.groupCount == 1)
|
|
|
|
g.free(engine: engine)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 0)
|
|
#expect(engine.groupCount == 0)
|
|
}
|
|
|
|
/// Two-level nesting: freeing an outer group whose child is another group
|
|
/// containing a synth must decrement counters for both the inner group and
|
|
/// its grandchild synth. Mirrors the `LyrebirdEventList` tree shape
|
|
/// (`el.group → proc.group → routeSynth`) that previously left a phantom
|
|
/// `synthesizerCount` per process per kill cycle.
|
|
@Test func freeingOuterGroupDecrementsNestedGroupAndGrandchildSynth() throws {
|
|
let outer: Group = Group()
|
|
let inner: Group = Group()
|
|
try engine.add(outer)
|
|
try engine.add(inner, position: .tail(of: outer))
|
|
try engine.add(ConstantSynth(value: 0.1, outputBus: 0), position: .tail(of: inner))
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 1)
|
|
#expect(engine.groupCount == 2)
|
|
|
|
outer.free(engine: engine)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 0)
|
|
#expect(engine.groupCount == 0)
|
|
}
|
|
|
|
/// Three-level nesting with multiple grandchild synths confirms the subtree
|
|
/// walk descends past the immediate grandchildren too.
|
|
@Test func freeingOuterGroupDecrementsTripleNestedDescendants() throws {
|
|
let outer: Group = Group()
|
|
let middle: Group = Group()
|
|
let inner: Group = Group()
|
|
try engine.add(outer)
|
|
try engine.add(middle, position: .tail(of: outer))
|
|
try engine.add(inner, position: .tail(of: middle))
|
|
try engine.add(ConstantSynth(value: 0.1, outputBus: 0), position: .tail(of: inner))
|
|
try engine.add(ConstantSynth(value: 0.2, outputBus: 0), position: .tail(of: inner))
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 2)
|
|
#expect(engine.groupCount == 3)
|
|
|
|
outer.free(engine: engine)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 0)
|
|
#expect(engine.groupCount == 0)
|
|
}
|
|
|
|
/// Outer group containing both a direct-child synth and a nested group with
|
|
/// its own synth — verifies the direct-child decrement still works alongside
|
|
/// the recursive descent.
|
|
@Test func freeingOuterGroupWithMixedDirectAndNestedChildrenDecrementsAll() throws {
|
|
let outer: Group = Group()
|
|
let inner: Group = Group()
|
|
try engine.add(outer)
|
|
try engine.add(ConstantSynth(value: 0.1, outputBus: 0), position: .tail(of: outer))
|
|
try engine.add(inner, position: .tail(of: outer))
|
|
try engine.add(ConstantSynth(value: 0.2, outputBus: 0), position: .tail(of: inner))
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 2)
|
|
#expect(engine.groupCount == 2)
|
|
|
|
outer.free(engine: engine)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(engine.synthesizerCount == 0)
|
|
#expect(engine.groupCount == 0)
|
|
}
|
|
}
|