238 lines
8.7 KiB
Swift
238 lines
8.7 KiB
Swift
// HRIRFileTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
import Foundation
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
// MARK: - Test Data helpers
|
||
|
||
/// Appends a `UInt32` in little-endian byte order.
|
||
private func appendLE32(_ v: UInt32, to data: inout Data) {
|
||
var le: UInt32 = v.littleEndian
|
||
withUnsafeBytes(of: &le) { data.append(contentsOf: $0) }
|
||
}
|
||
|
||
/// Appends a `Double` (as its bit pattern) in little-endian byte order.
|
||
private func appendLEDouble(_ v: Double, to data: inout Data) {
|
||
var le: UInt64 = v.bitPattern.littleEndian
|
||
withUnsafeBytes(of: &le) { data.append(contentsOf: $0) }
|
||
}
|
||
|
||
/// Appends an `Int16` in little-endian byte order.
|
||
private func appendLE16(_ v: Int16, to data: inout Data) {
|
||
var le: Int16 = v.littleEndian
|
||
withUnsafeBytes(of: &le) { data.append(contentsOf: $0) }
|
||
}
|
||
|
||
/// Builds a minimal valid `.lbhr` blob with the given header values and
|
||
/// writes it to a temporary URL. Returns both the URL and the `Data`.
|
||
/// Caller is responsible for cleanup via `FileManager.default.removeItem(at:)`.
|
||
private func makeMinimalLBHR(
|
||
magic: [UInt8] = [0x4C, 0x42, 0x48, 0x52], // "LBHR"
|
||
version: UInt32 = 1,
|
||
sampleRate: UInt32 = 44_100,
|
||
irLength: UInt32 = 4,
|
||
positionCount: UInt32 = 1
|
||
) throws -> (url: URL, data: Data) {
|
||
let tmpURL: URL = FileManager.default.temporaryDirectory
|
||
.appendingPathComponent("test-\(UUID().uuidString).lbhr")
|
||
|
||
var bytes: Data = Data()
|
||
bytes.append(contentsOf: magic)
|
||
appendLE32(version, to: &bytes)
|
||
appendLE32(sampleRate, to: &bytes)
|
||
appendLE32(irLength, to: &bytes)
|
||
appendLE32(positionCount, to: &bytes)
|
||
appendLE32(0, to: &bytes) // reserved
|
||
|
||
// Position table entries: one per positionCount.
|
||
for i: Int in 0..<Int(positionCount) {
|
||
appendLEDouble(0.0, to: &bytes) // azimuth
|
||
appendLEDouble(0.0, to: &bytes) // elevation
|
||
appendLE32(UInt32(i * 2 * Int(irLength)), to: &bytes) // sampleOffset
|
||
appendLE32(0, to: &bytes) // reserved
|
||
}
|
||
|
||
// Samples: positionCount × 2 × irLength × i16 (all zeros).
|
||
let sampleCount: Int = Int(positionCount) * 2 * Int(irLength)
|
||
for _ in 0..<sampleCount {
|
||
appendLE16(0, to: &bytes)
|
||
}
|
||
|
||
try bytes.write(to: tmpURL)
|
||
return (tmpURL, bytes)
|
||
}
|
||
|
||
// MARK: - Suite
|
||
|
||
@Suite("HRIRFile")
|
||
struct HRIRFileTests {
|
||
|
||
// MARK: - Negative tests: invalid files are rejected
|
||
|
||
@Test func invalidMagicIsRejected() throws {
|
||
let (url, validData) = try makeMinimalLBHR()
|
||
defer { try? FileManager.default.removeItem(at: url) }
|
||
|
||
// Corrupt the first magic byte.
|
||
var bad: Data = validData
|
||
bad[0] = 0x00
|
||
try bad.write(to: url)
|
||
|
||
do {
|
||
_ = try HRIRFile.load(contentsOf: url)
|
||
Issue.record("Expected .invalidMagic error, but load succeeded")
|
||
} catch HRIRFile.LoadError.invalidMagic {
|
||
// expected
|
||
} catch {
|
||
Issue.record("Expected .invalidMagic, got \(error)")
|
||
}
|
||
}
|
||
|
||
@Test func truncatedFileIsRejected() throws {
|
||
let tmpURL: URL = FileManager.default.temporaryDirectory
|
||
.appendingPathComponent("truncated-\(UUID().uuidString).lbhr")
|
||
defer { try? FileManager.default.removeItem(at: tmpURL) }
|
||
|
||
// Write only the magic — not even the full header.
|
||
let short: Data = Data([0x4C, 0x42, 0x48, 0x52])
|
||
try short.write(to: tmpURL)
|
||
|
||
do {
|
||
_ = try HRIRFile.load(contentsOf: tmpURL)
|
||
Issue.record("Expected .truncated error, but load succeeded")
|
||
} catch HRIRFile.LoadError.truncated {
|
||
// expected
|
||
} catch {
|
||
Issue.record("Expected .truncated, got \(error)")
|
||
}
|
||
}
|
||
|
||
@Test func wrongVersionIsRejected() throws {
|
||
let (url, data) = try makeMinimalLBHR(version: 99)
|
||
defer { try? FileManager.default.removeItem(at: url) }
|
||
try data.write(to: url)
|
||
|
||
do {
|
||
_ = try HRIRFile.load(contentsOf: url)
|
||
Issue.record("Expected .unsupportedVersion error, but load succeeded")
|
||
} catch HRIRFile.LoadError.unsupportedVersion(let v) {
|
||
#expect(v == 99)
|
||
} catch {
|
||
Issue.record("Expected .unsupportedVersion, got \(error)")
|
||
}
|
||
}
|
||
|
||
@Test func positionCountZeroIsRejected() throws {
|
||
let (url, data) = try makeMinimalLBHR(positionCount: 0)
|
||
defer { try? FileManager.default.removeItem(at: url) }
|
||
try data.write(to: url)
|
||
|
||
do {
|
||
_ = try HRIRFile.load(contentsOf: url)
|
||
Issue.record("Expected .invalidPositionCount error, but load succeeded")
|
||
} catch HRIRFile.LoadError.invalidPositionCount(let n) {
|
||
#expect(n == 0)
|
||
} catch {
|
||
Issue.record("Expected .invalidPositionCount, got \(error)")
|
||
}
|
||
}
|
||
|
||
@Test func irLengthZeroIsRejected() throws {
|
||
let (url, data) = try makeMinimalLBHR(irLength: 0)
|
||
defer { try? FileManager.default.removeItem(at: url) }
|
||
try data.write(to: url)
|
||
|
||
do {
|
||
_ = try HRIRFile.load(contentsOf: url)
|
||
Issue.record("Expected .invalidIRLength error, but load succeeded")
|
||
} catch HRIRFile.LoadError.invalidIRLength(let n) {
|
||
#expect(n == 0)
|
||
} catch {
|
||
Issue.record("Expected .invalidIRLength, got \(error)")
|
||
}
|
||
}
|
||
|
||
@Test func sampleRateZeroIsRejected() throws {
|
||
let (url, data) = try makeMinimalLBHR(sampleRate: 0)
|
||
defer { try? FileManager.default.removeItem(at: url) }
|
||
try data.write(to: url)
|
||
|
||
do {
|
||
_ = try HRIRFile.load(contentsOf: url)
|
||
Issue.record("Expected .invalidSampleRate error, but load succeeded")
|
||
} catch HRIRFile.LoadError.invalidSampleRate(let r) {
|
||
#expect(r == 0)
|
||
} catch {
|
||
Issue.record("Expected .invalidSampleRate, got \(error)")
|
||
}
|
||
}
|
||
|
||
@Test func nonexistentFileThrowsIOError() {
|
||
let missing: URL = URL(fileURLWithPath: "/tmp/does-not-exist-\(UUID().uuidString).lbhr")
|
||
do {
|
||
_ = try HRIRFile.load(contentsOf: missing)
|
||
Issue.record("Expected .ioError for nonexistent file, but load succeeded")
|
||
} catch HRIRFile.LoadError.ioError {
|
||
// expected
|
||
} catch {
|
||
Issue.record("Expected .ioError, got \(error)")
|
||
}
|
||
}
|
||
|
||
// MARK: - Round-trip sanity: known values survive load unchanged
|
||
|
||
@Test func minimalValidFileParsesCorrectly() throws {
|
||
let tmpURL: URL = FileManager.default.temporaryDirectory
|
||
.appendingPathComponent("valid-\(UUID().uuidString).lbhr")
|
||
defer { try? FileManager.default.removeItem(at: tmpURL) }
|
||
|
||
let testIRLength: UInt32 = 4
|
||
let testPositionCount: UInt32 = 1
|
||
|
||
var bytes: Data = Data()
|
||
bytes.append(contentsOf: [0x4C, 0x42, 0x48, 0x52]) // "LBHR"
|
||
appendLE32(1, to: &bytes) // version
|
||
appendLE32(44_100, to: &bytes) // sampleRate
|
||
appendLE32(testIRLength, to: &bytes) // irLength
|
||
appendLE32(testPositionCount, to: &bytes) // positionCount
|
||
appendLE32(0, to: &bytes) // reserved
|
||
|
||
// Position: azimuth = π/2 (left), elevation = 0.
|
||
let testAzimuth: Double = Double.pi / 2.0
|
||
appendLEDouble(testAzimuth, to: &bytes)
|
||
appendLEDouble(0.0, to: &bytes)
|
||
appendLE32(0, to: &bytes) // sampleOffset
|
||
appendLE32(0, to: &bytes) // reserved
|
||
|
||
// L IR: [32767, -32768, 16384, -16384]
|
||
let lSamples: [Int16] = [32767, -32768, 16384, -16384]
|
||
for s: Int16 in lSamples { appendLE16(s, to: &bytes) }
|
||
// R IR: [0, 32767, -32767, 1]
|
||
let rSamples: [Int16] = [0, 32767, -32767, 1]
|
||
for s: Int16 in rSamples { appendLE16(s, to: &bytes) }
|
||
|
||
try bytes.write(to: tmpURL)
|
||
|
||
let hrir: HRIRFile = try HRIRFile.load(contentsOf: tmpURL)
|
||
#expect(hrir.sampleRate == 44_100)
|
||
#expect(hrir.irLength == Int(testIRLength))
|
||
#expect(hrir.positions.count == 1)
|
||
#expect(abs(hrir.positions[0].azimuth - testAzimuth) < 1e-12)
|
||
#expect(abs(hrir.positions[0].elevation) < 1e-12)
|
||
|
||
// Verify float conversion: 32767 / 32768.0 and -32768 / 32768.0 = -1.0.
|
||
let expectedL0: Float = Float(Int16(32767)) / 32768.0
|
||
let expectedL1: Float = Float(Int16(-32768)) / 32768.0
|
||
#expect(hrir.irs[0][0][0] == expectedL0)
|
||
#expect(hrir.irs[0][0][1] == expectedL1)
|
||
#expect(hrir.irs[0][1][0] == 0.0)
|
||
let expectedR1: Float = Float(Int16(32767)) / 32768.0
|
||
#expect(hrir.irs[0][1][1] == expectedR1)
|
||
}
|
||
}
|