// DemandSwitchTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("DemandSwitch") struct DemandSwitchTests { @Test func pullsByIndex() { let d: DemandSwitch = DemandSwitch( demands: [ DemandSequence(values: [1.0, 2.0, 3.0], repeats: -1), DemandSequence(values: [10.0, 20.0, 30.0], repeats: -1) ], index: DemandSequence(values: [0.0, 1.0, 0.0, 1.0], repeats: 1) ) #expect(d.nextValue() == 1.0) #expect(d.nextValue() == 10.0) #expect(d.nextValue() == 2.0) #expect(d.nextValue() == 20.0) #expect(d.nextValue() == nil) #expect(d.isExhausted) } @Test func indexExhaustionLatches() { let d: DemandSwitch = DemandSwitch( demands: [DemandSequence(values: [100.0], repeats: -1)], index: DemandSequence(values: [0.0], repeats: 1) ) #expect(d.nextValue() == 100.0) #expect(d.nextValue() == nil) #expect(d.isExhausted) } @Test func subSourceExhaustionLatches() { let d: DemandSwitch = DemandSwitch( demands: [DemandSequence(values: [7.0, 8.0], repeats: 1)], index: DemandSequence(values: [0.0], repeats: -1) ) #expect(d.nextValue() == 7.0) #expect(d.nextValue() == 8.0) #expect(d.nextValue() == nil) #expect(d.isExhausted) } @Test func outOfRangeIndexLatches() { let d: DemandSwitch = DemandSwitch( demands: [DemandSequence(values: [1.0], repeats: -1)], index: DemandSequence(values: [0.0, 99.0], repeats: 1) ) #expect(d.nextValue() == 1.0) #expect(d.nextValue() == nil) #expect(d.isExhausted) } @Test func negativeFractionalIndexFloorsToLatch() { // Index -0.5 floors to -1 (out of range) and latches, rather than // truncating toward zero (which would silently pick demands[0]). let d: DemandSwitch = DemandSwitch( demands: [DemandSequence(values: [42.0], repeats: -1)], index: DemandSequence(values: [-0.5], repeats: 1) ) #expect(d.nextValue() == nil) #expect(d.isExhausted) } @Test func resetRestartsAll() { let d: DemandSwitch = DemandSwitch( demands: [ DemandSequence(values: [1.0, 2.0], repeats: -1), DemandSequence(values: [10.0, 20.0], repeats: -1) ], index: DemandSequence(values: [0.0, 1.0], repeats: 1) ) _ = d.nextValue(); _ = d.nextValue(); _ = d.nextValue() #expect(d.isExhausted) d.reset() #expect(!d.isExhausted) #expect(d.nextValue() == 1.0) #expect(d.nextValue() == 10.0) } }