Zum Inhalt

apple/PhysarumApp/Tests/AudioFactorFrameTests.swift

import XCTest
import PhysarumEngine
@testable import Physarum

/// The T2b wire frame must map the 10 audio factors losslessly to/from `AudioFactors` and survive JSON.
final class AudioFactorFrameTests: XCTestCase {
    func testNeutralIsAllOnes() {
        let n = AudioFactorFrame.neutral
        for v in [n.moveSpeed, n.turnSpeed, n.deposit, n.sensorDistance, n.rhythmAmplitude, n.chaosStrength,
                  n.exposure, n.bloomIntensity, n.saturation, n.contrast] {
            XCTAssertEqual(v, 1, accuracy: 1e-6)
        }
    }

    func testMapsFromAudioFactors() {
        var f = AudioFactors()
        f[.moveSpeed] = 1.5
        f[.chaosStrength] = 0.25
        f[.exposure] = 2.0
        f[.contrast] = 0.5
        let frame = AudioFactorFrame(f)
        XCTAssertEqual(frame.moveSpeed, 1.5, accuracy: 1e-6)
        XCTAssertEqual(frame.chaosStrength, 0.25, accuracy: 1e-6)
        XCTAssertEqual(frame.exposure, 2.0, accuracy: 1e-6)
        XCTAssertEqual(frame.contrast, 0.5, accuracy: 1e-6)
        // the accessors round back into the same agent/post split the renderer consumes
        XCTAssertEqual(frame.agentUniform.moveSpeedFactor, 1.5, accuracy: 1e-6)
        XCTAssertEqual(frame.agentUniform.chaosStrengthFactor, 0.25, accuracy: 1e-6)
        XCTAssertEqual(frame.postExposure, 2.0, accuracy: 1e-6)
        XCTAssertEqual(frame.postContrast, 0.5, accuracy: 1e-6)
    }

    func testCodableRoundTrip() throws {
        var f = AudioFactors()
        f[.turnSpeed] = 1.3; f[.bloomIntensity] = 0.8
        let frame = AudioFactorFrame(f)
        let back = try JSONDecoder().decode(AudioFactorFrame.self, from: JSONEncoder().encode(frame))
        XCTAssertEqual(back, frame)
    }
}