97 lines
3.4 KiB
Swift
97 lines
3.4 KiB
Swift
// CoreAudioDriverTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// MARK: - CoreAudioDriverOptions defaults
|
|
|
|
@Suite("CoreAudioDriverOptions") struct CoreAudioDriverOptionsTests {
|
|
@Test func defaultMatchDeviceSampleRateIsFalse() {
|
|
let opts: CoreAudioDriverOptions = CoreAudioDriverOptions()
|
|
#expect(opts.matchDeviceSampleRate == false)
|
|
}
|
|
|
|
@Test func canEnableMatchDeviceSampleRate() {
|
|
let opts: CoreAudioDriverOptions = CoreAudioDriverOptions(matchDeviceSampleRate: true)
|
|
#expect(opts.matchDeviceSampleRate == true)
|
|
}
|
|
}
|
|
|
|
// MARK: - Sample rate validation (offline, no hardware required)
|
|
|
|
@Suite("CoreAudioDriver sample rate validation") struct CoreAudioSampleRateValidationTests {
|
|
@Test func doesNotThrowWhenRatesMatch() throws {
|
|
let opts: CoreAudioDriverOptions = CoreAudioDriverOptions()
|
|
try CoreAudioDriver._validateSampleRates(
|
|
requested: 44100.0, deviceRate: 44100.0, options: opts)
|
|
}
|
|
|
|
@Test func throwsSampleRateMismatchWhenRatesDiffer() {
|
|
let opts: CoreAudioDriverOptions = CoreAudioDriverOptions()
|
|
#expect(throws: CoreAudioError.self) {
|
|
try CoreAudioDriver._validateSampleRates(
|
|
requested: 44100.0, deviceRate: 48000.0, options: opts)
|
|
}
|
|
}
|
|
|
|
@Test func mismatchErrorCarriesRequestedAndDeviceRate() {
|
|
let opts: CoreAudioDriverOptions = CoreAudioDriverOptions()
|
|
var caught: CoreAudioError?
|
|
do {
|
|
try CoreAudioDriver._validateSampleRates(
|
|
requested: 44100.0, deviceRate: 48000.0, options: opts)
|
|
} catch let e as CoreAudioError {
|
|
caught = e
|
|
} catch {}
|
|
|
|
guard case .sampleRateMismatch(let requested, let deviceRate) = caught else {
|
|
Issue.record("Expected sampleRateMismatch, got \(String(describing: caught))")
|
|
return
|
|
}
|
|
#expect(requested == 44100.0)
|
|
#expect(deviceRate == 48000.0)
|
|
}
|
|
|
|
@Test func doesNotThrowWithMatchDeviceSampleRateWhenRatesDiffer() throws {
|
|
let opts: CoreAudioDriverOptions = CoreAudioDriverOptions(matchDeviceSampleRate: true)
|
|
// Should not throw — driver will attempt to set the hardware rate instead.
|
|
try CoreAudioDriver._validateSampleRates(
|
|
requested: 44100.0, deviceRate: 48000.0, options: opts)
|
|
}
|
|
|
|
@Test func toleratesFloatingPointNoise() throws {
|
|
let opts: CoreAudioDriverOptions = CoreAudioDriverOptions()
|
|
// Rates that differ by less than 0.01 are treated as equal.
|
|
try CoreAudioDriver._validateSampleRates(
|
|
requested: 44100.0, deviceRate: 44100.005, options: opts)
|
|
}
|
|
}
|
|
|
|
// MARK: - deviceSampleRate property
|
|
|
|
@Suite("CoreAudioDriver deviceSampleRate") struct CoreAudioDeviceSampleRateTests {
|
|
@Test func deviceSampleRateNilBeforeSetup() {
|
|
let driver: CoreAudioDriver = CoreAudioDriver()
|
|
#expect(driver.deviceSampleRate == nil)
|
|
}
|
|
}
|
|
|
|
// MARK: - Lifecycle (no hardware required)
|
|
|
|
@Suite("CoreAudioDriver lifecycle") struct CoreAudioDriverLifecycleTests {
|
|
@Test func stopBeforeSetupIsNoop() {
|
|
let driver: CoreAudioDriver = CoreAudioDriver()
|
|
driver.stop()
|
|
}
|
|
|
|
@Test func stopTwiceIsNoop() {
|
|
let driver: CoreAudioDriver = CoreAudioDriver()
|
|
driver.stop()
|
|
driver.stop()
|
|
}
|
|
}
|