532 lines
22 KiB
Swift
532 lines
22 KiB
Swift
// 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..<count {
|
||
_result[0][i] = leftValue
|
||
_result[1][i] = rightValue
|
||
}
|
||
return _result
|
||
}
|
||
}
|
||
|
||
/// 3-channel test fixture used to verify 3×3 element-wise composition.
|
||
fileprivate final class TripleConstantGen: GeneratorBase, AudioGenerator {
|
||
public override var outputChannelCount: Int { 3 }
|
||
|
||
init(_ a: Double, _ b: Double, _ c: Double) {
|
||
super.init(channels: 3)
|
||
for i: Int in 0..<_result[0].count {
|
||
_result[0][i] = a
|
||
_result[1][i] = b
|
||
_result[2][i] = c
|
||
}
|
||
}
|
||
func _computeSamples(graph: SynthGraph, count: Int) -> [[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..<count {
|
||
#expect(samples[0][i] == 1.0)
|
||
}
|
||
}
|
||
|
||
@Test func floatValueUsesOp() {
|
||
let gen: BinaryOpGenerator = ConstantAudioGen(3.0) * (2.0 as Double)
|
||
#expect(gen.floatValue(graph: graph) == 6.0)
|
||
}
|
||
|
||
@Test func chainedOps() {
|
||
let count: Int = 8
|
||
let gen: BinaryOpGenerator = (ConstantAudioGen(2.0) * (3.0 as Double)) + (1.0 as Double)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 7.0 })
|
||
}
|
||
|
||
// Order-sensitive operands: a flipped vDSP subtract/divide argument order
|
||
// would produce the reciprocal/negated value, so these lock the orientation.
|
||
|
||
@Test func scalarMinusGenerator() {
|
||
let count: Int = 8
|
||
let gen: BinaryOpGenerator = (1.0 as Double) - ConstantAudioGen(0.25)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 0.75 })
|
||
}
|
||
|
||
@Test func scalarDividedByGenerator() {
|
||
let count: Int = 8
|
||
let gen: BinaryOpGenerator = (1.0 as Double) / ConstantAudioGen(4.0)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 0.25 })
|
||
}
|
||
|
||
@Test func generatorMinusGenerator() {
|
||
let count: Int = 8
|
||
let gen: BinaryOpGenerator = ConstantAudioGen(3.0) - ConstantAudioGen(2.0)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 1.0 })
|
||
}
|
||
|
||
@Test func generatorDividedByGenerator() {
|
||
let count: Int = 8
|
||
let gen: BinaryOpGenerator = ConstantAudioGen(8.0) / ConstantAudioGen(2.0)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 4.0 })
|
||
}
|
||
}
|
||
|
||
// MARK: - Multi-channel broadcast
|
||
|
||
@MainActor
|
||
@Suite("BinaryOpGenerator multi-channel broadcast")
|
||
struct BinaryOpGeneratorMultiChannelTests {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||
var graph: SynthGraph {
|
||
makeSynthGraph(engine: engine)
|
||
}
|
||
|
||
init() throws {
|
||
try engine.start()
|
||
}
|
||
|
||
@Test("stereo × scalar produces 2-row result, both channels scaled")
|
||
func stereoTimesScalar() {
|
||
let count: Int = 8
|
||
let stereo: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||
let gen: BinaryOpGenerator = stereo * (0.5 as Double)
|
||
#expect(gen.outputChannelCount == 2)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples.count == 2)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 0.5 })
|
||
#expect(samples[1].prefix(count).allSatisfy { $0 == 1.0 })
|
||
}
|
||
|
||
@Test("scalar × stereo produces 2-row result")
|
||
func scalarTimesStereo() {
|
||
let count: Int = 8
|
||
let stereo: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||
let gen: BinaryOpGenerator = (3.0 as Double) * stereo
|
||
#expect(gen.outputChannelCount == 2)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 3.0 })
|
||
#expect(samples[1].prefix(count).allSatisfy { $0 == 6.0 })
|
||
}
|
||
|
||
@Test("mono generator + stereo generator broadcasts mono to both channels")
|
||
func monoPlusStereoBroadcasts() {
|
||
let count: Int = 8
|
||
let mono: ConstantAudioGen = ConstantAudioGen(10.0)
|
||
let stereo: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||
let gen: BinaryOpGenerator = mono + stereo
|
||
#expect(gen.outputChannelCount == 2)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 11.0 })
|
||
#expect(samples[1].prefix(count).allSatisfy { $0 == 12.0 })
|
||
}
|
||
|
||
@Test("stereo + mono broadcasts mono on rhs")
|
||
func stereoPlusMonoBroadcasts() {
|
||
let count: Int = 8
|
||
let stereo: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||
let mono: ConstantAudioGen = ConstantAudioGen(10.0)
|
||
let gen: BinaryOpGenerator = stereo + mono
|
||
#expect(gen.outputChannelCount == 2)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 11.0 })
|
||
#expect(samples[1].prefix(count).allSatisfy { $0 == 12.0 })
|
||
}
|
||
|
||
@Test("stereo + stereo is element-wise")
|
||
func stereoPlusStereoElementWise() {
|
||
let count: Int = 8
|
||
let a: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||
let b: StereoConstantGen = StereoConstantGen(left: 10.0, right: 20.0)
|
||
let gen: BinaryOpGenerator = a + b
|
||
#expect(gen.outputChannelCount == 2)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 11.0 })
|
||
#expect(samples[1].prefix(count).allSatisfy { $0 == 22.0 })
|
||
}
|
||
|
||
@Test("stereo × stereo element-wise")
|
||
func stereoTimesStereoElementWise() {
|
||
let count: Int = 8
|
||
let a: StereoConstantGen = StereoConstantGen(left: 2.0, right: 3.0)
|
||
let b: StereoConstantGen = StereoConstantGen(left: 4.0, right: 5.0)
|
||
let gen: BinaryOpGenerator = a * b
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 8.0 })
|
||
#expect(samples[1].prefix(count).allSatisfy { $0 == 15.0 })
|
||
}
|
||
|
||
@Test("ChannelSelect on multi-row BinaryOpGenerator extracts correct channel")
|
||
func channelSelectOnBinaryOp() {
|
||
let count: Int = 8
|
||
let stereo: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||
let scaled: BinaryOpGenerator = stereo * (0.5 as Double)
|
||
let right: ChannelSelect.Audio = scaled.channel(1)
|
||
let samples: [[Double]] = right.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 1.0 })
|
||
}
|
||
|
||
@Test("floatValue on N-row BinaryOpGenerator returns channel-0 sample-0")
|
||
func floatValueReturnsFirstChannelFirstSample() {
|
||
let stereo: StereoConstantGen = StereoConstantGen(left: 7.0, right: 100.0)
|
||
let gen: BinaryOpGenerator = stereo * (2.0 as Double)
|
||
#expect(gen.floatValue(graph: graph) == 14.0)
|
||
}
|
||
|
||
@Test("outputChannelCount is max of operands")
|
||
func outputChannelCountMaxOfOperands() {
|
||
let stereoA: StereoConstantGen = StereoConstantGen(left: 1, right: 2)
|
||
let stereoB: StereoConstantGen = StereoConstantGen(left: 3, right: 4)
|
||
let monoA: ConstantAudioGen = ConstantAudioGen(1.0)
|
||
let monoB: ConstantAudioGen = ConstantAudioGen(2.0)
|
||
#expect((stereoA * monoA).outputChannelCount == 2)
|
||
#expect((monoA * stereoA).outputChannelCount == 2)
|
||
#expect((stereoA * stereoB).outputChannelCount == 2)
|
||
#expect((monoA * monoB).outputChannelCount == 1)
|
||
#expect((stereoA * (0.5 as Double)).outputChannelCount == 2)
|
||
}
|
||
|
||
@Test("3-channel × 3-channel element-wise")
|
||
func tripleTimesTripleElementWise() {
|
||
let count: Int = 8
|
||
let a: TripleConstantGen = TripleConstantGen(1.0, 2.0, 3.0)
|
||
let b: TripleConstantGen = TripleConstantGen(10.0, 20.0, 30.0)
|
||
let gen: BinaryOpGenerator = a * b
|
||
#expect(gen.outputChannelCount == 3)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 10.0 })
|
||
#expect(samples[1].prefix(count).allSatisfy { $0 == 40.0 })
|
||
#expect(samples[2].prefix(count).allSatisfy { $0 == 90.0 })
|
||
}
|
||
|
||
@Test("scalar × 3-channel broadcasts")
|
||
func scalarTimesTriple() {
|
||
let count: Int = 8
|
||
let triple: TripleConstantGen = TripleConstantGen(1.0, 2.0, 3.0)
|
||
let gen: BinaryOpGenerator = triple * (10.0 as Double)
|
||
#expect(gen.outputChannelCount == 3)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 10.0 })
|
||
#expect(samples[1].prefix(count).allSatisfy { $0 == 20.0 })
|
||
#expect(samples[2].prefix(count).allSatisfy { $0 == 30.0 })
|
||
}
|
||
|
||
@Test("mono + 4-channel broadcasts mono to all four channels")
|
||
func monoPlusQuadBroadcasts() {
|
||
let count: Int = 8
|
||
let mono: ConstantAudioGen = ConstantAudioGen(100.0)
|
||
let quad: QuadConstantGen = QuadConstantGen(1.0, 2.0, 3.0, 4.0)
|
||
let gen: BinaryOpGenerator = mono + quad
|
||
#expect(gen.outputChannelCount == 4)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples.count == 4)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 101.0 })
|
||
#expect(samples[1].prefix(count).allSatisfy { $0 == 102.0 })
|
||
#expect(samples[2].prefix(count).allSatisfy { $0 == 103.0 })
|
||
#expect(samples[3].prefix(count).allSatisfy { $0 == 104.0 })
|
||
}
|
||
|
||
@Test("4-channel × scalar scales every channel")
|
||
func quadTimesScalar() {
|
||
let count: Int = 8
|
||
let quad: QuadConstantGen = QuadConstantGen(1.0, 2.0, 3.0, 4.0)
|
||
let gen: BinaryOpGenerator = quad * (0.5 as Double)
|
||
#expect(gen.outputChannelCount == 4)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 0.5 })
|
||
#expect(samples[1].prefix(count).allSatisfy { $0 == 1.0 })
|
||
#expect(samples[2].prefix(count).allSatisfy { $0 == 1.5 })
|
||
#expect(samples[3].prefix(count).allSatisfy { $0 == 2.0 })
|
||
}
|
||
|
||
@Test("4-channel + 4-channel element-wise (matches Pan4 + Pan4 shape)")
|
||
func quadPlusQuadElementWise() {
|
||
let count: Int = 8
|
||
let a: QuadConstantGen = QuadConstantGen(1.0, 2.0, 3.0, 4.0)
|
||
let b: QuadConstantGen = QuadConstantGen(10.0, 20.0, 30.0, 40.0)
|
||
let gen: BinaryOpGenerator = a + b
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == 11.0 })
|
||
#expect(samples[1].prefix(count).allSatisfy { $0 == 22.0 })
|
||
#expect(samples[2].prefix(count).allSatisfy { $0 == 33.0 })
|
||
#expect(samples[3].prefix(count).allSatisfy { $0 == 44.0 })
|
||
}
|
||
|
||
// The 2 × 3 mismatch trap (`precondition` at `BinaryOpGenerator.init`) is correctness-
|
||
// critical but cannot be tested via Swift Testing — `precondition` terminates the
|
||
// process rather than throwing. The trap's behavior is exercised manually and verified
|
||
// by code review. The construction `StereoConstantGen() * TripleConstantGen()` would
|
||
// trap with the message "BinaryOpGenerator: incompatible channel counts (2 vs 3) ...".
|
||
}
|
||
|
||
// MARK: - Existential operator overloads (any ValidGeneratorInput op any ValidGeneratorInput)
|
||
|
||
@MainActor
|
||
@Suite("BinaryOpGenerator existential operators")
|
||
struct BinaryOpGeneratorExistentialTests {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||
var graph: SynthGraph {
|
||
makeSynthGraph(engine: engine)
|
||
}
|
||
|
||
init() throws {
|
||
try engine.start()
|
||
}
|
||
|
||
// These tests cover the `any ValidGeneratorInput op any ValidGeneratorInput` overloads.
|
||
// All four operators are exercised with an existential-typed LHS so the compiler
|
||
// cannot resolve to a stdlib or concrete-Generator overload.
|
||
|
||
@Test("existential * scalar produces correct output")
|
||
func existentialTimesScalar() {
|
||
let count: Int = 8
|
||
let freq: any ValidGeneratorInput = 440.0
|
||
let scaled: Double = 1.5
|
||
let gen: BinaryOpGenerator = freq * scaled // (any ValidGeneratorInput) * (Double as any ValidGeneratorInput)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
let expected: Double = 440.0 * 1.5
|
||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||
}
|
||
|
||
@Test("existential + scalar produces correct output")
|
||
func existentialPlusScalar() {
|
||
let count: Int = 8
|
||
let freq: any ValidGeneratorInput = 440.0
|
||
let gen: BinaryOpGenerator = freq + (10.0 as Double)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
let expected: Double = 450.0
|
||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||
}
|
||
|
||
@Test("existential - scalar produces correct output")
|
||
func existentialMinusScalar() {
|
||
let count: Int = 8
|
||
let freq: any ValidGeneratorInput = 440.0
|
||
let gen: BinaryOpGenerator = freq - (40.0 as Double)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
let expected: Double = 400.0
|
||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||
}
|
||
|
||
@Test("existential / scalar produces correct output")
|
||
func existentialDividedByScalar() {
|
||
let count: Int = 8
|
||
let freq: any ValidGeneratorInput = 440.0
|
||
let gen: BinaryOpGenerator = freq / (2.0 as Double)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
let expected: Double = 220.0
|
||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||
}
|
||
|
||
@Test("existential op existential both non-generator produces BinaryOpGenerator")
|
||
func existentialOpExistential() {
|
||
let count: Int = 8
|
||
let a: any ValidGeneratorInput = 3.0
|
||
let b: any ValidGeneratorInput = 4.0
|
||
let gen: BinaryOpGenerator = a * b
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
let expected: Double = 12.0
|
||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||
}
|
||
|
||
/// Regression guard: concrete `Double op Double` must still resolve to `Double`
|
||
/// (stdlib overload), not `BinaryOpGenerator`. If the existential overloads above
|
||
/// introduced ambiguity, this line would fail to compile with a type error.
|
||
@Test("Double op Double still resolves to Double (stdlib wins)")
|
||
func doubleOpDoubleRemainsDouble() {
|
||
let a: Double = 440.0
|
||
let b: Double = 1.5
|
||
// These must be Double, not BinaryOpGenerator.
|
||
// If our overloads caused ambiguity, the type annotations below would fail to compile.
|
||
let product: Double = a * b
|
||
let sum: Double = a + b
|
||
let diff: Double = a - b
|
||
let quot: Double = a / b
|
||
#expect(product == 440.0 * 1.5)
|
||
#expect(sum == 440.0 + 1.5)
|
||
#expect(diff == 440.0 - 1.5)
|
||
#expect(quot == 440.0 / 1.5)
|
||
}
|
||
|
||
@Test("existential op produces a BinaryOpGenerator that is a Generator")
|
||
func existentialOpResultIsGenerator() {
|
||
let freq: any ValidGeneratorInput = 440.0
|
||
let gen: BinaryOpGenerator = freq * (2.0 as Double)
|
||
// The result must conform to Generator so it can be wired into a signal graph.
|
||
#expect(gen is any Generator)
|
||
#expect(gen.rate == .audio)
|
||
}
|
||
}
|
||
|
||
// MARK: - ConstantGeneratorInput
|
||
|
||
@MainActor
|
||
@Suite("ConstantGeneratorInput")
|
||
struct ConstantGeneratorInputTests {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||
var graph: SynthGraph {
|
||
makeSynthGraph(engine: engine)
|
||
}
|
||
|
||
init() throws {
|
||
try engine.start()
|
||
}
|
||
|
||
@Test("calculatedSamples returns all-constant block")
|
||
func calculatedSamplesIsConstant() {
|
||
let constantValue: Double = 440.0
|
||
let count: Int = 8
|
||
let gen: ConstantGeneratorInput = ConstantGeneratorInput(constantValue)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
#expect(samples[0].prefix(count).allSatisfy { $0 == constantValue })
|
||
}
|
||
|
||
@Test("floatValue returns the constant directly")
|
||
func floatValueReturnsConstant() {
|
||
let constantValue: Double = 220.0
|
||
let gen: ConstantGeneratorInput = ConstantGeneratorInput(constantValue)
|
||
#expect(gen.floatValue(graph: graph) == constantValue)
|
||
}
|
||
|
||
@Test("rate is audio")
|
||
func rateIsAudio() {
|
||
let gen: ConstantGeneratorInput = ConstantGeneratorInput(1.0)
|
||
#expect(gen.rate == .audio)
|
||
}
|
||
|
||
@Test("needsInterpolation is false")
|
||
func needsInterpolationIsFalse() {
|
||
let gen: ConstantGeneratorInput = ConstantGeneratorInput(1.0)
|
||
// ConstantGeneratorInput is an AudioGenerator → needsInterpolation() == false.
|
||
#expect(gen.needsInterpolation() == false)
|
||
}
|
||
|
||
@Test("ConstantGeneratorInput * scalar resolves via Category A overload")
|
||
func constGenInputTimesScalar() {
|
||
let count: Int = 8
|
||
let gen: BinaryOpGenerator = ConstantGeneratorInput(440.0) * (2.0 as Double)
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
let expected: Double = 880.0
|
||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||
}
|
||
|
||
@Test("ConstantGeneratorInput + OscSin wires correctly")
|
||
func constGenInputPlusOscSin() {
|
||
// Verifies ConstantGeneratorInput participates in the Generator op Generator path.
|
||
let count: Int = 8
|
||
let offset: ConstantGeneratorInput = ConstantGeneratorInput(100.0)
|
||
let osc: ConstantAudioGen = ConstantAudioGen(440.0)
|
||
let gen: BinaryOpGenerator = offset + osc // Generator + Generator → BinaryOpGenerator
|
||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
let expected: Double = 540.0
|
||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||
}
|
||
|
||
@Test("fan-out cache: calculatedSamples called twice returns same block")
|
||
func fanOutCacheHit() {
|
||
let count: Int = 8
|
||
let gen: ConstantGeneratorInput = ConstantGeneratorInput(440.0)
|
||
let first: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
let second: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||
// Same segment serial → cache hit → identical buffer reference returned.
|
||
for i: Int in 0..<count {
|
||
#expect(first[0][i] == second[0][i])
|
||
}
|
||
}
|
||
}
|