// SelectControlTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Select.Control") struct SelectControlTests { let engine: SynthesisEngine var graph: SynthGraph { makeSynthGraph(engine: engine) } let blockSize: Int = 64 let sampleRate: Double = 44100 init() throws { engine = makeTestEngine(blockSize: 64, sampleRate: 44100) try engine.start() } // MARK: - Rate @Test func rateIsControl() { let gen: Select.Control = Select.Control(which: 0, inputs: [1.0]) #expect(gen.rate == .control) } // MARK: - Output count @Test func outputCountMatchesRequest() { let gen: Select.Control = Select.Control(which: 0, inputs: [1.0, 2.0, 3.0]) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) #expect(Array(out[0].prefix(blockSize)).count == blockSize) } // MARK: - Index selection /// which = 0 picks inputs[0]. @Test func whichZeroPicksFirstInput() { let gen: Select.Control = Select.Control(which: 0, inputs: [0.1, 0.2, 0.3]) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 0.1) < 1e-12, "which=0 target should be 0.1, got \(gen.previousOutputValue)") } /// which = 1 picks inputs[1]. @Test func whichOnePicksSecondInput() { let gen: Select.Control = Select.Control(which: 1, inputs: [0.1, 0.2, 0.3]) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 0.2) < 1e-12, "which=1 target should be 0.2, got \(gen.previousOutputValue)") } // MARK: - Truncation and clamping /// Non-integer which truncates: which = 1.9 → index 1. @Test func nonIntegerWhichTruncates() { let gen: Select.Control = Select.Control(which: 1.9, inputs: [0.1, 0.2, 0.3]) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 0.2) < 1e-12, "which=1.9 (→ index 1) target should be 0.2, got \(gen.previousOutputValue)") } /// Negative which clamps to index 0. @Test func negativeWhichClampsToZero() { let gen: Select.Control = Select.Control(which: -5.0, inputs: [0.1, 0.2, 0.3]) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 0.1) < 1e-12, "which=-5 (clamp→0) target should be 0.1, got \(gen.previousOutputValue)") } /// which >= N clamps to N-1. @Test func overflowWhichClampsToLast() { let gen: Select.Control = Select.Control(which: 99.0, inputs: [0.1, 0.2, 0.3]) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 0.3) < 1e-12, "which=99 (clamp→2) target should be 0.3, got \(gen.previousOutputValue)") } // MARK: - Ramp /// Last sample of first block equals the selected target. @Test func rampsToTarget() { let gen: Select.Control = Select.Control(which: 2, inputs: [0.1, 0.5, 0.9]) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) let lastSample: Double = out[0][blockSize - 1] #expect(abs(lastSample - 0.9) < 1e-12, "last sample of ramp should equal target 0.9, got \(lastSample)") } // MARK: - mul/add @Test func mulAddApplied() { // which=0, inputs[0]=0.5 → base target = 0.5 // mul=2, add=1 → scaled target = 0.5*2+1 = 2.0 let gen: Select.Control = Select.Control(which: 0, inputs: [0.5, 0.8], mul: 2.0, add: 1.0) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 2.0) < 1e-12, "mulAdd: expected 2.0, got \(gen.previousOutputValue)") } // MARK: - Fan-out @Test func fanOutSameSegment() { let gen: Select.Control = Select.Control(which: 1, inputs: [0.3, 0.7]) let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) for i: Int in 0..