// BinaryOpGeneratorTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth /// Multi-channel test fixture — emits a fixed scalar per channel. /// Used to verify channel-aware broadcasting in BinaryOpGenerator. fileprivate final class StereoConstantGen: GeneratorBase, AudioGenerator { let leftValue: Double let rightValue: Double public override var outputChannelCount: Int { 2 } init(left: Double, right: Double) { self.leftValue = left self.rightValue = right super.init(channels: 2) } func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] { _checkCount(count) for i: Int in 0.. [[Double]] { _checkCount(count) return _result } } /// 4-channel test fixture used to verify Pan4-shape (4-row) broadcast paths. fileprivate final class QuadConstantGen: GeneratorBase, AudioGenerator { public override var outputChannelCount: Int { 4 } init(_ a: Double, _ b: Double, _ c: Double, _ d: Double) { super.init(channels: 4) for i: Int in 0..<_result[0].count { _result[0][i] = a _result[1][i] = b _result[2][i] = c _result[3][i] = d } } func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] { _checkCount(count) return _result } } @MainActor @Suite("BinaryOpGenerator") struct BinaryOpGeneratorTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 8) var graph: SynthGraph { makeSynthGraph(engine: engine) } init() throws { try engine.start() } @Test func multiplyTwoConstants() { let count: Int = 8 let gen: BinaryOpGenerator = (2.0 as Double) * ConstantAudioGen(3.0) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count) #expect(samples[0].prefix(count).allSatisfy { $0 == 6.0 }) } @Test func addTwoConstants() { let count: Int = 8 let gen: BinaryOpGenerator = ConstantAudioGen(1.0) + (0.5 as Double) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count) #expect(samples[0].prefix(count).allSatisfy { $0 == 1.5 }) } @Test func subtractConstants() { let count: Int = 8 let gen: BinaryOpGenerator = ConstantAudioGen(1.0) - (0.25 as Double) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count) #expect(samples[0].prefix(count).allSatisfy { $0 == 0.75 }) } @Test func divideConstants() { let count: Int = 8 let gen: BinaryOpGenerator = ConstantAudioGen(4.0) / (2.0 as Double) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count) #expect(samples[0].prefix(count).allSatisfy { $0 == 2.0 }) } @Test func resultRateIsAudio() { let gen: BinaryOpGenerator = ConstantAudioGen(1.0) * ConstantAudioGen(1.0) #expect(gen.rate == .audio) } @Test func countIsRespected() { let count: Int = 4 let gen: BinaryOpGenerator = ConstantAudioGen(1.0) + (0.0 as Double) let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count) // The pre-allocated result buffer is sized to lyrebirdDefaultResultBufferSize; // only the first `count` elements are valid for this call. #expect(samples[0].count >= count) for i: Int in 0..