Files
2026-06-21 11:45:58 -07:00

60 lines
1.8 KiB
Swift

// DemandStutterTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("DemandStutter")
struct DemandStutterTests {
@Test func repeatsEachValueByCount() {
let d: DemandStutter = DemandStutter(
demand: DemandSequence(values: [10.0, 20.0, 30.0], repeats: 1),
count: 2
)
var collected: [Double] = []
while let v: Double = d.nextValue() { collected.append(v) }
#expect(collected == [10.0, 10.0, 20.0, 20.0, 30.0, 30.0])
#expect(d.isExhausted)
}
@Test func countOneIsPassThrough() {
let d: DemandStutter = DemandStutter(
demand: DemandSequence(values: [1.0, 2.0, 3.0], repeats: 1),
count: 1
)
var collected: [Double] = []
while let v: Double = d.nextValue() { collected.append(v) }
#expect(collected == [1.0, 2.0, 3.0])
}
@Test func upstreamExhaustionLatchesEngine() {
let d: DemandStutter = DemandStutter(
demand: DemandSequence(values: [5.0], repeats: 1),
count: 3
)
#expect(d.nextValue() == 5.0)
#expect(d.nextValue() == 5.0)
#expect(d.nextValue() == 5.0)
#expect(d.nextValue() == nil)
#expect(d.isExhausted)
}
@Test func resetRestartsUpstreamAndCount() {
let d: DemandStutter = DemandStutter(
demand: DemandSequence(values: [7.0, 8.0], repeats: 1),
count: 2
)
while d.nextValue() != nil {} // drain to trigger latch
#expect(d.isExhausted)
d.reset()
#expect(!d.isExhausted)
#expect(d.nextValue() == 7.0)
#expect(d.nextValue() == 7.0)
}
}