285 lines
9.3 KiB
Swift
285 lines
9.3 KiB
Swift
// GroupSemanticsTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// MARK: - Test fixture: gate-capable synthesizer
|
|
|
|
/// Synthesizer that tracks gate-zero calls (simulates EnvelopeGen release).
|
|
final class GatedSynth: Synthesizer {
|
|
var releaseCallCount: Int = 0
|
|
var freeCallCount: Int = 0
|
|
|
|
func build() {}
|
|
func evaluate(graph: SynthGraph, count: Int) {}
|
|
|
|
func release(engine: SynthesisEngine) throws {
|
|
releaseCallCount += 1
|
|
}
|
|
}
|
|
|
|
/// Synthesizer with no gate — release() throws noGate (uses Synthesizer default).
|
|
final class NoGateSynth: Synthesizer {
|
|
var freeWasCalled: Bool = false
|
|
func build() {}
|
|
func evaluate(graph: SynthGraph, count: Int) {}
|
|
}
|
|
|
|
// MARK: -
|
|
|
|
@Suite("Group Semantics")
|
|
struct GroupSemanticsTests {
|
|
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
|
|
|
// MARK: - count Atomic
|
|
|
|
@Test func countStartsAtZero() throws {
|
|
let group: Group = Group()
|
|
#expect(group.count == 0)
|
|
}
|
|
|
|
@Test func countIncreasesOnAddToTail() throws {
|
|
let group: Group = Group()
|
|
group.addToTail(PreparedNode(TrackingNode()))
|
|
group.addToTail(PreparedNode(TrackingNode()))
|
|
#expect(group.count == 2)
|
|
}
|
|
|
|
@Test func countDecreasesOnRemove() throws {
|
|
let group: Group = Group()
|
|
let node: TrackingNode = TrackingNode()
|
|
group.addToTail(PreparedNode(node))
|
|
group.remove(node)
|
|
#expect(group.count == 0)
|
|
}
|
|
|
|
// MARK: - addBefore / addAfter
|
|
|
|
@Test func addBeforeInsertsBeforeTarget() throws {
|
|
let group: Group = Group()
|
|
let a: TrackingNode = TrackingNode()
|
|
let b: TrackingNode = TrackingNode()
|
|
group.addToTail(PreparedNode(a))
|
|
// insert b before a
|
|
let nodeB: PreparedNode = PreparedNode(b)
|
|
let inserted: Bool = group.addBefore(nodeB, target: a)
|
|
#expect(inserted == true)
|
|
#expect(group.count == 2)
|
|
// b should be at head (evaluates first)
|
|
var callOrder: [ObjectIdentifier] = []
|
|
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
|
|
// Swap evaluate to capture order: re-implement via evaluate call
|
|
// Verify by checking head pointer directly
|
|
#expect(group.head?.synth === b)
|
|
}
|
|
|
|
@Test func addAfterInsertsAfterTarget() throws {
|
|
let group: Group = Group()
|
|
let a: TrackingNode = TrackingNode()
|
|
let b: TrackingNode = TrackingNode()
|
|
group.addToTail(PreparedNode(a))
|
|
let nodeB: PreparedNode = PreparedNode(b)
|
|
let inserted: Bool = group.addAfter(nodeB, target: a)
|
|
#expect(inserted == true)
|
|
#expect(group.count == 2)
|
|
// b should be at tail
|
|
#expect(group.tail?.synth === b)
|
|
}
|
|
|
|
@Test func addBeforeReturnsFalseIfTargetNotFound() throws {
|
|
let group: Group = Group()
|
|
let outsider: TrackingNode = TrackingNode()
|
|
let newNode: PreparedNode = PreparedNode(TrackingNode())
|
|
let result: Bool = group.addBefore(newNode, target: outsider)
|
|
#expect(result == false)
|
|
#expect(group.count == 0)
|
|
}
|
|
|
|
@Test func addAfterReturnsFalseIfTargetNotFound() throws {
|
|
let group: Group = Group()
|
|
let outsider: TrackingNode = TrackingNode()
|
|
let newNode: PreparedNode = PreparedNode(TrackingNode())
|
|
let result: Bool = group.addAfter(newNode, target: outsider)
|
|
#expect(result == false)
|
|
#expect(group.count == 0)
|
|
}
|
|
|
|
@Test func addBeforeSearchesNestedGroup() throws {
|
|
let outer: Group = Group()
|
|
let inner: Group = Group()
|
|
let target: TrackingNode = TrackingNode()
|
|
outer.addToTail(PreparedNode(inner))
|
|
inner.addToTail(PreparedNode(target))
|
|
|
|
let newNode: PreparedNode = PreparedNode(TrackingNode())
|
|
let result: Bool = outer.addBefore(newNode, target: target)
|
|
#expect(result == true)
|
|
// inner now has 2 children
|
|
#expect(inner.count == 2)
|
|
#expect(inner.head?.synth === newNode.synth)
|
|
}
|
|
|
|
@Test func addAfterSearchesNestedGroup() throws {
|
|
let outer: Group = Group()
|
|
let inner: Group = Group()
|
|
let target: TrackingNode = TrackingNode()
|
|
outer.addToTail(PreparedNode(inner))
|
|
inner.addToTail(PreparedNode(target))
|
|
|
|
let newNode: PreparedNode = PreparedNode(TrackingNode())
|
|
let result: Bool = outer.addAfter(newNode, target: target)
|
|
#expect(result == true)
|
|
#expect(inner.count == 2)
|
|
#expect(inner.tail?.synth === newNode.synth)
|
|
}
|
|
|
|
// MARK: - NodePosition via engine
|
|
|
|
@Test func engineAddPositionHeadInsertsAtHead() throws {
|
|
let group: Group = Group()
|
|
try engine.add(group)
|
|
engine.processBlock(inputSamples: [])
|
|
|
|
let a: TrackingNode = TrackingNode()
|
|
let b: TrackingNode = TrackingNode()
|
|
try engine.add(a, position: .tail(of: group))
|
|
try engine.add(b, position: .head(of: group))
|
|
engine.processBlock(inputSamples: [])
|
|
|
|
// b at head, a at tail
|
|
#expect(group.head?.synth === b)
|
|
#expect(group.tail?.synth === a)
|
|
}
|
|
|
|
@Test func engineAddPositionAfterInsertsAfterTarget() throws {
|
|
let group: Group = Group()
|
|
try engine.add(group)
|
|
engine.processBlock(inputSamples: [])
|
|
|
|
let a: TrackingNode = TrackingNode()
|
|
let b: TrackingNode = TrackingNode()
|
|
try engine.add(a, position: .tail(of: group))
|
|
engine.processBlock(inputSamples: []) // ensure `a` is in tree before `.after(a)`
|
|
|
|
try engine.add(b, position: .after(a))
|
|
engine.processBlock(inputSamples: [])
|
|
|
|
#expect(group.tail?.synth === b)
|
|
}
|
|
|
|
@Test func engineAddPositionBeforeFallsBackToMainGroupHeadWhenMissing() throws {
|
|
let outsider: TrackingNode = TrackingNode()
|
|
let newSynth: TrackingNode = TrackingNode()
|
|
try engine.add(newSynth, position: .before(outsider))
|
|
engine.processBlock(inputSamples: [])
|
|
// Target not found → falls back to mainGroup head (not rootGroup tail).
|
|
#expect(engine.mainGroup.contains(newSynth))
|
|
}
|
|
|
|
// MARK: - Synthesizer.release default
|
|
|
|
@Test func defaultReleaseThrowsNoGate() throws {
|
|
let synth: TrackingNode = TrackingNode()
|
|
var threw: Bool = false
|
|
do {
|
|
try synth.release(engine: engine)
|
|
} catch SynthesizerError.noGate {
|
|
threw = true
|
|
} catch {}
|
|
#expect(threw == true)
|
|
}
|
|
|
|
// MARK: - Group.release cascade
|
|
|
|
@Test func groupReleaseCascadesGateToGatedChildren() throws {
|
|
let group: Group = Group()
|
|
let gated: GatedSynth = GatedSynth()
|
|
group.addToTail(PreparedNode(gated))
|
|
|
|
group.release(engine: engine)
|
|
|
|
#expect(gated.releaseCallCount == 1)
|
|
}
|
|
|
|
@Test func groupReleaseNonGatedChildRemovedFromTreeAfterProcessBlock() throws {
|
|
let group: Group = Group()
|
|
let noGate: TrackingNode = TrackingNode() // default release throws noGate → group calls free
|
|
try engine.add(group)
|
|
engine.processBlock(inputSamples: [])
|
|
|
|
// Manually add noGate to group's linked list so it appears in the tree
|
|
group.addToTail(PreparedNode(noGate))
|
|
#expect(group.count == 1)
|
|
|
|
// release: noGate throws noGate → engine.remove(noGate) event posted
|
|
group.release(engine: engine)
|
|
|
|
// Process: applies remove event for noGate
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(group.count == 0)
|
|
}
|
|
|
|
@Test func groupReleaseIsIdempotent() throws {
|
|
let group: Group = Group()
|
|
let gated: GatedSynth = GatedSynth()
|
|
group.addToTail(PreparedNode(gated))
|
|
|
|
group.release(engine: engine)
|
|
group.release(engine: engine) // second call should be no-op
|
|
|
|
#expect(gated.releaseCallCount == 1)
|
|
}
|
|
|
|
// MARK: - Group.free
|
|
|
|
@Test func groupFreePostsEventAndRemovesFromTree() throws {
|
|
let group: Group = Group()
|
|
try engine.add(group)
|
|
engine.processBlock(inputSamples: [])
|
|
// engine.add now places into mainGroup, not rootGroup directly.
|
|
#expect(engine.mainGroup.contains(group))
|
|
|
|
group.free(engine: engine)
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(!engine.mainGroup.contains(group))
|
|
}
|
|
|
|
@Test func groupFreeRemovesAllChildren() throws {
|
|
let group: Group = Group()
|
|
let child: TrackingNode = TrackingNode()
|
|
group.addToTail(PreparedNode(child))
|
|
|
|
try engine.add(group)
|
|
engine.processBlock(inputSamples: [])
|
|
|
|
group.free(engine: engine)
|
|
engine.processBlock(inputSamples: [])
|
|
|
|
// Group is gone from mainGroup (where engine.add now places it)
|
|
#expect(!engine.mainGroup.contains(group))
|
|
// Group is now empty (children drained by applyEvent)
|
|
#expect(group.count == 0)
|
|
}
|
|
|
|
// MARK: - removeRecursive
|
|
|
|
@Test func removeRecursiveFindsNestedSynth() throws {
|
|
let outer: Group = Group()
|
|
let inner: Group = Group()
|
|
let target: TrackingNode = TrackingNode()
|
|
outer.addToTail(PreparedNode(inner))
|
|
inner.addToTail(PreparedNode(target))
|
|
|
|
let removed: PreparedNode? = outer.removeRecursive(target)
|
|
#expect(removed != nil)
|
|
#expect(inner.count == 0)
|
|
#expect(outer.count == 1) // inner still in outer
|
|
}
|
|
}
|