// PulseCountControlTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("PulseCount.Control") struct PulseCountControlTests { let engine: SynthesisEngine var graph: SynthGraph { makeSynthGraph(engine: engine) } let blockSize: Int = 64 init() throws { engine = makeTestEngine(blockSize: 64, sampleRate: 44100) try engine.start() } @Test func rateIsControl() { let gen: PulseCount.Control = PulseCount.Control(trig: 0.0) #expect(gen.rate == .control) } /// Without any trigger, previousOutputValue = 0. @Test func noTriggerCountStaysZero() { let gen: PulseCount.Control = PulseCount.Control(trig: 0.0) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(gen.previousOutputValue == 0.0) } /// Rising edges increment the count; block-final count is previousOutputValue. @Test func edgesIncrementCount() { let trig: Closure = Closure { _, i in (i == 10 || i == 20 || i == 30) ? 1.0 : 0.0 } let gen: PulseCount.Control = PulseCount.Control(trig: trig) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 3.0) < 1e-12, "expected count 3.0 got \(gen.previousOutputValue)") } /// Reset clears count to 0. @Test func resetClearsCount() { let trig: Closure = Closure { _, i in (i == 5 || i == 15 || i == 25) ? 1.0 : 0.0 } let reset: Closure = Closure { _, i in i == 40 ? 1.0 : 0.0 } let gen: PulseCount.Control = PulseCount.Control(trig: trig, reset: reset) _ = gen.calculatedSamples(graph: graph, count: blockSize) // Reset fires after trig edges; block-final count = 0. #expect(gen.previousOutputValue == 0.0, "after reset expected 0.0 got \(gen.previousOutputValue)") } /// Output block is flat (discrete). @Test func outputBlockIsFlat() { let trig: Closure = Closure { _, i in i == 10 ? 1.0 : 0.0 } let gen: PulseCount.Control = PulseCount.Control(trig: trig) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) let block: [Double] = Array(out[0].prefix(blockSize)) let finalValue: Double = gen.previousOutputValue for i: Int in 0..