// SpectralComplexClosureTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("SpectralComplexClosure") struct SpectralComplexClosureTests { // MARK: - Helpers private static func fillKnownPolar(_ buf: SpectralBuffer) { buf.dcValue = 0.5 buf.nyquistValue = -0.25 let halfSize: Int = buf.fftSize / 2 for k: Int in 1.. 1e-6 || abs(ctx.imag[i]) > 1e-6 { anyNonZero = true } } } proc.process(spectral: buf, engine: makeTestEngine()) #expect(!anyNonZero, "Zero-magnitude bins should yield zero Cartesian values") } // MARK: - Mutation round-trip @Test func zeroClearsSingleBinInPolar() { let fftSize: Int = 64 let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) let halfSize: Int = fftSize / 2 for k: Int in 1..= warmUpBlocks { for i: Int in 0..= warmUpBlocks { for i: Int in 0.. = UnsafeMutablePointer.allocate(capacity: halfSize) let latchImag: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: halfSize) latchReal.initialize(repeating: 0, count: halfSize) latchImag.initialize(repeating: 0, count: halfSize) defer { latchReal.deinitialize(count: halfSize) latchReal.deallocate() latchImag.deinitialize(count: halfSize) latchImag.deallocate() } var latched: Bool = false let freeze: SpectralComplexClosure = SpectralComplexClosure( fftSize: fftSize, hopSize: hopSize, sampleRate: engine.sampleRate ) { ctx in if !latched { // Capture first frame into pre-allocated scratch. for i: Int in 0.. 0.001, "Frozen complex spectrum produced near-silence (RMS \(rms)); expected sustained tone") } }