157 lines
5.3 KiB
Swift
157 lines
5.3 KiB
Swift
// ValueComputableTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("ValueComputable")
|
|
struct ValueComputableTests {
|
|
|
|
// MARK: - Numeric scalars
|
|
|
|
@Test func doubleReturnsSelfFromCompute() {
|
|
let v: Double = 3.14
|
|
#expect(v.compute(index: 0, timestamp: 0.0) == 3.14)
|
|
#expect(v.compute(index: 42, timestamp: 99.0) == 3.14)
|
|
}
|
|
|
|
@Test func doubleTruncatesForIntCompute() {
|
|
let v: Double = 3.9
|
|
#expect(v.intCompute(index: 0, timestamp: 0.0) == 3)
|
|
}
|
|
|
|
@Test func intReturnsAsDoubleFromCompute() {
|
|
let v: Int = 42
|
|
#expect(v.compute(index: 0, timestamp: 0.0) == 42.0)
|
|
}
|
|
|
|
@Test func intReturnsSelfFromIntCompute() {
|
|
let v: Int = 42
|
|
#expect(v.intCompute(index: 0, timestamp: 0.0) == 42)
|
|
}
|
|
|
|
// MARK: - Closure wrapper
|
|
|
|
@Test func closureWrapperEvaluatesBody() {
|
|
let vc: ValueComputableClosure = ValueComputableClosure { i, t in
|
|
Double(i) + t
|
|
}
|
|
#expect(vc.compute(index: 5, timestamp: 0.5) == 5.5)
|
|
#expect(vc.compute(index: 0, timestamp: 0.0) == 0.0)
|
|
}
|
|
|
|
@Test func closureWrapperIntComputeDefaultTruncates() {
|
|
let vc: ValueComputableClosure = ValueComputableClosure { _, _ in 7.9 }
|
|
#expect(vc.intCompute(index: 0, timestamp: 0.0) == 7)
|
|
}
|
|
|
|
// MARK: - Envelope
|
|
|
|
@Test func envelopeComputeMatchesValueAtTime() {
|
|
let env: Envelope = Envelope(
|
|
levels: [0.0, 1.0, 0.0],
|
|
durations: [1.0, 1.0],
|
|
curves: [.lin, .lin]
|
|
)
|
|
#expect(env.compute(index: 0, timestamp: 0.5) == 0.5)
|
|
#expect(env.compute(index: 99, timestamp: 1.0) == 1.0)
|
|
#expect(env.compute(index: 0, timestamp: 1.5) == 0.5)
|
|
}
|
|
|
|
@Test func envelopeClampsBeforeAndAfter() {
|
|
let env: Envelope = Envelope(
|
|
levels: [0.0, 1.0],
|
|
durations: [1.0],
|
|
curves: [.lin]
|
|
)
|
|
#expect(env.compute(index: 0, timestamp: -10.0) == 0.0)
|
|
#expect(env.compute(index: 0, timestamp: 100.0) == 1.0)
|
|
}
|
|
|
|
// MARK: - Pattern<Double>
|
|
|
|
@Test func patternDoubleCyclesThroughValues() {
|
|
let pat: Pattern<Double> = Pattern.sequence([1.0, 2.0, 3.0])
|
|
#expect(pat.compute(index: 0, timestamp: 0.0) == 1.0)
|
|
#expect(pat.compute(index: 1, timestamp: 0.0) == 2.0)
|
|
#expect(pat.compute(index: 2, timestamp: 0.0) == 3.0)
|
|
#expect(pat.compute(index: 3, timestamp: 0.0) == 1.0)
|
|
}
|
|
|
|
@Test func patternDoubleExhaustionReturnsZero() {
|
|
let pat: Pattern<Double> = Pattern.sequence([10.0], repeats: 1)
|
|
_ = pat.compute(index: 0, timestamp: 0.0)
|
|
#expect(pat.compute(index: 1, timestamp: 0.0) == 0.0)
|
|
}
|
|
|
|
// MARK: - Pattern<Int>
|
|
|
|
@Test func patternIntCyclesViaIntCompute() {
|
|
let pat: Pattern<Int> = Pattern.sequence([10, 20, 30])
|
|
#expect(pat.intCompute(index: 0, timestamp: 0.0) == 10)
|
|
#expect(pat.intCompute(index: 1, timestamp: 0.0) == 20)
|
|
#expect(pat.intCompute(index: 2, timestamp: 0.0) == 30)
|
|
}
|
|
|
|
@Test func patternIntExhaustionReturnsZeroInt() {
|
|
let pat: Pattern<Int> = Pattern.sequence([42], repeats: 1)
|
|
_ = pat.intCompute(index: 0, timestamp: 0.0)
|
|
#expect(pat.intCompute(index: 1, timestamp: 0.0) == 0)
|
|
}
|
|
|
|
@Test func patternIntComputeReturnsAsDouble() {
|
|
let pat: Pattern<Int> = Pattern.sequence([7, 8])
|
|
#expect(pat.compute(index: 0, timestamp: 0.0) == 7.0)
|
|
#expect(pat.compute(index: 1, timestamp: 0.0) == 8.0)
|
|
}
|
|
|
|
// MARK: - Pattern<Envelope>
|
|
|
|
@Test func patternOfEnvelopesPollsEachInTurn() {
|
|
let envA: Envelope = Envelope(
|
|
levels: [0.0, 10.0],
|
|
durations: [1.0],
|
|
curves: [.lin]
|
|
)
|
|
let envB: Envelope = Envelope(
|
|
levels: [100.0, 0.0],
|
|
durations: [1.0],
|
|
curves: [.lin]
|
|
)
|
|
let pat: Pattern<Envelope> = Pattern.sequence([envA, envB])
|
|
#expect(pat.compute(index: 0, timestamp: 0.5) == 5.0)
|
|
#expect(pat.compute(index: 0, timestamp: 0.5) == 50.0)
|
|
#expect(pat.compute(index: 0, timestamp: 0.5) == 5.0)
|
|
}
|
|
|
|
// MARK: - Nested Pattern<Pattern<Double>>
|
|
|
|
@Test func nestedPatternRecurses() {
|
|
let inner1: Pattern<Double> = Pattern.sequence([10.0, 11.0])
|
|
let inner2: Pattern<Double> = Pattern.sequence([20.0])
|
|
let outer: Pattern<Pattern<Double>> = Pattern.sequence([inner1, inner2])
|
|
#expect(outer.compute(index: 0, timestamp: 0.0) == 10.0)
|
|
#expect(outer.compute(index: 0, timestamp: 0.0) == 20.0)
|
|
#expect(outer.compute(index: 0, timestamp: 0.0) == 11.0)
|
|
}
|
|
|
|
// MARK: - Existential storage round-trip
|
|
|
|
@Test func existentialAnyValueComputableDispatches() {
|
|
let params: [any ValueComputable] = [
|
|
440.0,
|
|
42,
|
|
ValueComputableClosure { i, _ in Double(i) * 2.0 },
|
|
Pattern<Double>.sequence([7.0, 8.0])
|
|
]
|
|
#expect(params[0].compute(index: 0, timestamp: 0.0) == 440.0)
|
|
#expect(params[1].intCompute(index: 0, timestamp: 0.0) == 42)
|
|
#expect(params[2].compute(index: 5, timestamp: 0.0) == 10.0)
|
|
#expect(params[3].compute(index: 0, timestamp: 0.0) == 7.0)
|
|
}
|
|
}
|