// MusicMathTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("MusicMath") struct MusicMathTests { private let accuracy: Double = 1e-9 // MARK: - MIDI / Hz @Test func midiToHzKnownValues() { #expect(abs(midiToHz(69.0) - 440.0) < accuracy) #expect(abs(midiToHz(57.0) - 220.0) < accuracy) #expect(abs(midiToHz(81.0) - 880.0) < accuracy) #expect(abs(midiToHz(0.0) - 8.1757989156437) < 1e-6) } @Test func midiToHzIntOverload() { #expect(abs(midiToHz(69) - 440.0) < accuracy) #expect(abs(midiToHz(69) - midiToHz(69.0)) < accuracy) } @Test func midiToHzFractional() { let half: Double = midiToHz(69.5) #expect(half > 440.0) #expect(half < midiToHz(70.0)) } @Test func midiToHzCustomTuning() { #expect(abs(midiToHz(69.0, tuning: 432.0) - 432.0) < accuracy) #expect(abs(midiToHz(69, tuning: 415.0) - 415.0) < accuracy) } @Test func hzToMidiKnownValues() { #expect(abs(hzToMidi(440.0) - 69.0) < accuracy) #expect(abs(hzToMidi(220.0) - 57.0) < accuracy) #expect(abs(hzToMidi(880.0) - 81.0) < accuracy) } @Test func hzToMidiNonPositive() { #expect(hzToMidi(0.0) == -.infinity) #expect(hzToMidi(-1.0) == -.infinity) } @Test func midiHzRoundTrip() { for midi: Double in stride(from: 0.0, through: 127.0, by: 1.0) { #expect(abs(hzToMidi(midiToHz(midi)) - midi) < 1e-10) } } // MARK: - Amplitude / dB @Test func ampToDbKnownValues() { #expect(abs(ampToDb(1.0) - 0.0) < accuracy) #expect(abs(ampToDb(0.5) - (-6.0205999132796)) < 1e-6) #expect(abs(ampToDb(2.0) - 6.0205999132796) < 1e-6) } @Test func dbToAmpKnownValues() { #expect(abs(dbToAmp(0.0) - 1.0) < accuracy) #expect(abs(dbToAmp(-6.0205999132796) - 0.5) < 1e-6) } @Test func ampToDbNegativeInput() { #expect(ampToDb(-1.0).isNaN) #expect(ampToDb(-0.001).isNaN) } @Test func ampDbRoundTrip() { for amp: Double in [0.001, 0.01, 0.1, 0.5, 1.0, 2.0, 10.0] { #expect(abs(dbToAmp(ampToDb(amp)) - amp) < 1e-10) } } // MARK: - Power / dB @Test func powToDbKnownValues() { #expect(abs(powToDb(1.0) - 0.0) < accuracy) #expect(abs(powToDb(2.0) - 3.0102999566398) < 1e-6) } @Test func dbToPowKnownValues() { #expect(abs(dbToPow(0.0) - 1.0) < accuracy) } @Test func powToDbNegativeInput() { #expect(powToDb(-1.0).isNaN) } @Test func powDbRoundTrip() { for p: Double in [0.001, 0.1, 1.0, 10.0, 100.0] { #expect(abs(dbToPow(powToDb(p)) - p) < 1e-10) } } // MARK: - Intervals @Test func semitonesToRatioOctave() { #expect(abs(semitonesToRatio(12.0) - 2.0) < accuracy) #expect(abs(semitonesToRatio(-12.0) - 0.5) < accuracy) #expect(abs(semitonesToRatio(0.0) - 1.0) < accuracy) } @Test func ratioToSemitonesOctave() { #expect(abs(ratioToSemitones(2.0) - 12.0) < accuracy) #expect(abs(ratioToSemitones(0.5) - (-12.0)) < accuracy) } @Test func semitonesRatioRoundTrip() { for s: Double in [-24.0, -12.0, -1.0, 0.0, 1.0, 7.0, 12.0, 24.0] { #expect(abs(ratioToSemitones(semitonesToRatio(s)) - s) < 1e-10) } } @Test func centsToRatioOneSemitone() { #expect(abs(centsToRatio(100.0) - semitonesToRatio(1.0)) < 1e-10) } @Test func ratioToCentsOctave() { #expect(abs(ratioToCents(2.0) - 1200.0) < accuracy) } @Test func centsRatioRoundTrip() { for c: Double in [-1200.0, -100.0, 0.0, 50.0, 100.0, 1200.0] { #expect(abs(ratioToCents(centsToRatio(c)) - c) < 1e-10) } } // MARK: - midiToRatio / ratioToMidi @Test func midiToRatioOctaves() { #expect(abs(midiToRatio(12.0) - 2.0) < accuracy) #expect(abs(midiToRatio(-12.0) - 0.5) < accuracy) #expect(abs(midiToRatio(0.0) - 1.0) < accuracy) #expect(abs(midiToRatio(24.0) - 4.0) < accuracy) } @Test func midiToRatioIntOverload() { #expect(abs(midiToRatio(12) - 2.0) < accuracy) #expect(abs(midiToRatio(-12) - 0.5) < accuracy) #expect(abs(midiToRatio(0) - 1.0) < accuracy) } @Test func ratioToMidiOctaves() { #expect(abs(ratioToMidi(2.0) - 12.0) < accuracy) #expect(abs(ratioToMidi(0.5) - (-12.0)) < accuracy) #expect(abs(ratioToMidi(1.0)) < accuracy) } @Test func midiToRatioRoundTrip() { for s: Double in [-24.0, -12.0, -1.0, 0.0, 1.0, 7.0, 12.0, 24.0] { #expect(abs(ratioToMidi(midiToRatio(s)) - s) < 1e-10) } } @Test func midiToRatioMatchesSemitonesToRatio() { for s: Double in [-12.0, -1.0, 0.0, 1.0, 7.0, 12.0] { #expect(midiToRatio(s) == semitonesToRatio(s)) } } @Test func midiToRatioDoubleDotSyntax() { #expect(abs(12.0.midiToRatio - 2.0) < accuracy) #expect(abs((-12.0).midiToRatio - 0.5) < accuracy) #expect(abs(0.0.midiToRatio - 1.0) < accuracy) } @Test func ratioToMidiDoubleDotSyntax() { #expect(abs(2.0.ratioToMidi - 12.0) < accuracy) #expect(abs(0.5.ratioToMidi - (-12.0)) < accuracy) } @Test func midiToRatioIntDotSyntax() { #expect(abs(12.midiToRatio - 2.0) < accuracy) #expect(abs((-12).midiToRatio - 0.5) < accuracy) #expect(abs(0.midiToRatio - 1.0) < accuracy) #expect(abs(7.midiToRatio - semitonesToRatio(7.0)) < accuracy) } @Test func midiToRatioArrayOverload() { let result: [Double] = midiToRatio([0.0, 12.0, -12.0]) #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 2.0) < accuracy) #expect(abs(result[2] - 0.5) < accuracy) } @Test func ratioToMidiArrayOverload() { let result: [Double] = ratioToMidi([1.0, 2.0, 0.5]) #expect(abs(result[0]) < accuracy) #expect(abs(result[1] - 12.0) < accuracy) #expect(abs(result[2] - (-12.0)) < accuracy) } @Test func ratioToMidiNonPositive() { // ratio == 0 → log2(0) → -.infinity (matches ratioToSemitones). #expect(ratioToMidi(0.0) == -.infinity) // ratio < 0 → log2 of negative → NaN. #expect(ratioToMidi(-1.0).isNaN) } @Test func midiToRatioDoubleCollectionMethod() { let result: [Double] = [0.0, 12.0, -12.0].midiToRatio() #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 2.0) < accuracy) #expect(abs(result[2] - 0.5) < accuracy) } @Test func ratioToMidiDoubleCollectionMethod() { let result: [Double] = [1.0, 2.0, 0.5].ratioToMidi() #expect(abs(result[0]) < accuracy) #expect(abs(result[1] - 12.0) < accuracy) #expect(abs(result[2] - (-12.0)) < accuracy) } @Test func midiToRatioIntCollectionMethod() { let result: [Double] = [0, 12, -12].midiToRatio() #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 2.0) < accuracy) #expect(abs(result[2] - 0.5) < accuracy) } // MARK: - Tempo / time @Test func bpmToSecondsKnownValues() { #expect(abs(bpmToSeconds(120.0) - 0.5) < accuracy) #expect(abs(bpmToSeconds(60.0) - 1.0) < accuracy) #expect(abs(bpmToSeconds(240.0) - 0.25) < accuracy) } @Test func secondsToBpmKnownValues() { #expect(abs(secondsToBpm(0.5) - 120.0) < accuracy) #expect(abs(secondsToBpm(1.0) - 60.0) < accuracy) } @Test func bpmSecondsRoundTrip() { for bpm: Double in [60.0, 90.0, 120.0, 180.0] { #expect(abs(secondsToBpm(bpmToSeconds(bpm)) - bpm) < 1e-10) } } @Test func hzToPeriodKnownValues() { #expect(abs(hzToPeriod(1.0) - 1.0) < accuracy) #expect(abs(hzToPeriod(2.0) - 0.5) < accuracy) #expect(abs(hzToPeriod(440.0) - (1.0 / 440.0)) < accuracy) } @Test func periodToHzRoundTrip() { for hz: Double in [1.0, 100.0, 440.0, 1000.0, 22050.0] { #expect(abs(periodToHz(hzToPeriod(hz)) - hz) < 1e-10) } } // MARK: - Angles @Test func degreesToRadiansKnownValues() { #expect(abs(degreesToRadians(0.0) - 0.0) < accuracy) #expect(abs(degreesToRadians(180.0) - .pi) < accuracy) #expect(abs(degreesToRadians(360.0) - 2.0 * .pi) < accuracy) #expect(abs(degreesToRadians(90.0) - .pi / 2.0) < accuracy) #expect(abs(degreesToRadians(-90.0) - (-.pi / 2.0)) < accuracy) } @Test func radiansToDegreesKnownValues() { #expect(abs(radiansToDegrees(0.0) - 0.0) < accuracy) #expect(abs(radiansToDegrees(.pi) - 180.0) < accuracy) #expect(abs(radiansToDegrees(2.0 * .pi) - 360.0) < accuracy) #expect(abs(radiansToDegrees(.pi / 2.0) - 90.0) < accuracy) #expect(abs(radiansToDegrees(-.pi / 2.0) - (-90.0)) < accuracy) } @Test func degreesRadiansRoundTrip() { for deg: Double in stride(from: -720.0, through: 720.0, by: 15.0) { #expect(abs(radiansToDegrees(degreesToRadians(deg)) - deg) < 1e-10) } } @Test func arrayDegreesToRadians() { let result: [Double] = degreesToRadians([0.0, 90.0, 180.0, 360.0]) #expect(abs(result[0] - 0.0) < accuracy) #expect(abs(result[1] - .pi / 2.0) < accuracy) #expect(abs(result[2] - .pi) < accuracy) #expect(abs(result[3] - 2.0 * .pi) < accuracy) } @Test func arrayRadiansToDegrees() { let result: [Double] = radiansToDegrees([0.0, .pi / 2.0, .pi, 2.0 * .pi]) #expect(abs(result[0] - 0.0) < accuracy) #expect(abs(result[1] - 90.0) < accuracy) #expect(abs(result[2] - 180.0) < accuracy) #expect(abs(result[3] - 360.0) < accuracy) } @Test func collectionDoubleDegreesToRadians() { let result: [Double] = [0.0, 90.0, 180.0].degreesToRadians() #expect(abs(result[0] - 0.0) < accuracy) #expect(abs(result[1] - .pi / 2.0) < accuracy) #expect(abs(result[2] - .pi) < accuracy) } @Test func collectionDoubleRadiansToDegrees() { let result: [Double] = [0.0, .pi / 2.0, .pi].radiansToDegrees() #expect(abs(result[0] - 0.0) < accuracy) #expect(abs(result[1] - 90.0) < accuracy) #expect(abs(result[2] - 180.0) < accuracy) } // MARK: - linToExp / equalPowerFade @Test func linToExpBoundaries() { #expect(abs(linToExp(0.0, low: 200.0, high: 2000.0) - 200.0) < 1e-9) #expect(abs(linToExp(1.0, low: 200.0, high: 2000.0) - 2000.0) < 1e-9) } @Test func linToExpMidpoint() { // geometric mean of 100 and 1600 = 400 #expect(abs(linToExp(0.5, low: 100.0, high: 1600.0) - 400.0) < 1e-9) } @Test func equalPowerFadeEndpoints() { let (a0, b0) = equalPowerFade(0.0) #expect(abs(a0 - 1.0) < 1e-10) #expect(abs(b0 - 0.0) < 1e-10) let (a1, b1) = equalPowerFade(1.0) #expect(abs(a1 - 0.0) < 1e-10) #expect(abs(b1 - 1.0) < 1e-10) } @Test func equalPowerFadeMidpoint() { let (a, b) = equalPowerFade(0.5) #expect(abs(a - b) < 1e-10) #expect(abs(a * a + b * b - 1.0) < 1e-10) } // MARK: - Array overloads @Test func arrayMidiToHz() { let result: [Double] = midiToHz([57.0, 69.0, 81.0]) #expect(abs(result[0] - 220.0) < accuracy) #expect(abs(result[1] - 440.0) < accuracy) #expect(abs(result[2] - 880.0) < accuracy) } @Test func arrayHzToMidi() { let result: [Double] = hzToMidi([220.0, 440.0, 880.0]) #expect(abs(result[0] - 57.0) < accuracy) #expect(abs(result[1] - 69.0) < accuracy) #expect(abs(result[2] - 81.0) < accuracy) } @Test func arrayAmpToDb() { let result: [Double] = ampToDb([1.0, 0.5]) #expect(abs(result[0] - 0.0) < accuracy) #expect(abs(result[1] - (-6.0205999132796)) < 1e-6) } @Test func arrayDbToAmp() { #expect(abs(dbToAmp([0.0])[0] - 1.0) < accuracy) } @Test func arrayPowToDb() { #expect(abs(powToDb([1.0])[0] - 0.0) < accuracy) } @Test func arrayDbToPow() { #expect(abs(dbToPow([0.0])[0] - 1.0) < accuracy) } @Test func arraySemitonesToRatio() { let result: [Double] = semitonesToRatio([0.0, 12.0]) #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 2.0) < accuracy) } @Test func arrayRatioToSemitones() { let result: [Double] = ratioToSemitones([1.0, 2.0]) #expect(abs(result[0] - 0.0) < accuracy) #expect(abs(result[1] - 12.0) < accuracy) } @Test func arrayCentsToRatio() { let result: [Double] = centsToRatio([0.0, 1200.0]) #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 2.0) < accuracy) } @Test func arrayRatioToCents() { let result: [Double] = ratioToCents([1.0, 2.0]) #expect(abs(result[0] - 0.0) < accuracy) #expect(abs(result[1] - 1200.0) < accuracy) } @Test func arrayBpmToSeconds() { let result: [Double] = bpmToSeconds([120.0, 60.0]) #expect(abs(result[0] - 0.5) < accuracy) #expect(abs(result[1] - 1.0) < accuracy) } @Test func arraySecondsToBpm() { let result: [Double] = secondsToBpm([0.5, 1.0]) #expect(abs(result[0] - 120.0) < accuracy) #expect(abs(result[1] - 60.0) < accuracy) } @Test func arrayHzToPeriod() { let result: [Double] = hzToPeriod([1.0, 2.0]) #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 0.5) < accuracy) } @Test func arrayPeriodToHz() { let result: [Double] = periodToHz([1.0, 0.5]) #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 2.0) < accuracy) } // MARK: - Collection methods @Test func normalizeSumBasic() { let result: [Double] = [1.0, 2.0, 1.0].normalizeSum() #expect(abs(result[0] - 0.25) < accuracy) #expect(abs(result[1] - 0.5) < accuracy) #expect(abs(result[2] - 0.25) < accuracy) #expect(abs(result.reduce(0, +) - 1.0) < accuracy) } @Test func normalizeSumAlreadyNormalized() { let result: [Double] = [0.25, 0.5, 0.25].normalizeSum() #expect(abs(result.reduce(0, +) - 1.0) < accuracy) } @Test func normalizeSumZeroGuard() { let result: [Double] = [0.0, 0.0, 0.0].normalizeSum() #expect(result == [0.0, 0.0, 0.0]) } @Test func normalizeSumEmpty() { #expect([Double]().normalizeSum().isEmpty) } @Test func collectionDoubleMidiToHz() { let result: [Double] = [57.0, 69.0, 81.0].midiToHz() #expect(abs(result[0] - 220.0) < accuracy) #expect(abs(result[1] - 440.0) < accuracy) #expect(abs(result[2] - 880.0) < accuracy) } @Test func collectionDoubleHzToMidi() { let result: [Double] = [220.0, 440.0, 880.0].hzToMidi() #expect(abs(result[0] - 57.0) < accuracy) #expect(abs(result[1] - 69.0) < accuracy) #expect(abs(result[2] - 81.0) < accuracy) } @Test func collectionDoubleAmpToDb() { let result: [Double] = [1.0, 0.5].ampToDb() #expect(abs(result[0] - 0.0) < accuracy) #expect(abs(result[1] - (-6.0205999132796)) < 1e-6) } @Test func collectionDoubleDbToAmp() { let result: [Double] = [0.0, -6.0205999132796].dbToAmp() #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 0.5) < 1e-6) } @Test func collectionDoubleSemitonesToRatio() { let result: [Double] = [0.0, 12.0].semitonesToRatio() #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 2.0) < accuracy) } @Test func collectionDoubleBpmToSeconds() { let result: [Double] = [120.0, 60.0].bpmToSeconds() #expect(abs(result[0] - 0.5) < accuracy) #expect(abs(result[1] - 1.0) < accuracy) } // MARK: - integrate / differentiate @Test func integrateDouble() { let result: [Double] = [1.0, 2.0, 3.0].integrate() #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 3.0) < accuracy) #expect(abs(result[2] - 6.0) < accuracy) } @Test func integrateInt() { #expect([1, 2, 3].integrate() == [1, 3, 6]) } @Test func integrateEmpty() { #expect([Double]().integrate().isEmpty) } @Test func differentiateDouble() { let result: [Double] = [1.0, 3.0, 6.0].differentiate() #expect(result.count == 2) #expect(abs(result[0] - 2.0) < accuracy) #expect(abs(result[1] - 3.0) < accuracy) } @Test func differentiateInt() { #expect([1, 3, 6].differentiate() == [2, 3]) } @Test func differentiateEmpty() { #expect([Double]().differentiate().isEmpty) } @Test func differentiateSingleElement() { #expect([42.0].differentiate().isEmpty) } @Test func integrateInverseOfDifferentiate() { // integrate then differentiate recovers original minus first element let original: [Double] = [1.0, 2.0, 3.0, 4.0] let roundTrip: [Double] = original.integrate().differentiate() #expect(roundTrip.count == original.count - 1) for (a, b) in zip(roundTrip, original.dropFirst()) { #expect(abs(a - b) < accuracy) } } // MARK: - Collection methods @Test func collectionIntMidiToHz() { let result: [Double] = [57, 69, 81].midiToHz() #expect(abs(result[0] - 220.0) < accuracy) #expect(abs(result[1] - 440.0) < accuracy) #expect(abs(result[2] - 880.0) < accuracy) } // MARK: - interpolateAt @Test func interpolateAtFraction() { #expect(abs([0.0, 10.0].interpolateAt(0.2)! - 2.0) < accuracy) #expect(abs([0.0, 10.0].interpolateAt(0.5)! - 5.0) < accuracy) #expect(abs([0.0, 10.0].interpolateAt(0.9)! - 9.0) < accuracy) } @Test func interpolateAtExactIndices() { #expect(abs([0.0, 5.0, 10.0].interpolateAt(0.0)! - 0.0) < accuracy) #expect(abs([0.0, 5.0, 10.0].interpolateAt(1.0)! - 5.0) < accuracy) #expect(abs([0.0, 5.0, 10.0].interpolateAt(2.0)! - 10.0) < accuracy) } @Test func interpolateAtMidSegment() { // [0, 5, 10] — index 1.5 = midpoint between 5 and 10 = 7.5 #expect(abs([0.0, 5.0, 10.0].interpolateAt(1.5)! - 7.5) < accuracy) } @Test func interpolateAtClipHigh() { #expect(abs([0.0, 10.0].interpolateAt(1.1)! - 10.0) < accuracy) #expect(abs([0.0, 10.0].interpolateAt(99.0)! - 10.0) < accuracy) } @Test func interpolateAtClipLow() { #expect(abs([0.0, 10.0].interpolateAt(-1.1)! - 0.0) < accuracy) #expect(abs([0.0, 10.0].interpolateAt(-99.0)! - 0.0) < accuracy) } @Test func interpolateAtSingleElement() { #expect(abs([42.0].interpolateAt(0.0)! - 42.0) < accuracy) #expect(abs([42.0].interpolateAt(99.0)! - 42.0) < accuracy) #expect(abs([42.0].interpolateAt(-1.0)! - 42.0) < accuracy) } @Test func interpolateAtEmpty() { #expect([Double]().interpolateAt(0.5) == nil) } // MARK: - wrapAt / clipAt / foldAt @Test func wrapAtInBounds() { let a: [Int] = [10, 20, 30] #expect(a.wrapAt(0) == 10) #expect(a.wrapAt(1) == 20) #expect(a.wrapAt(2) == 30) } @Test func wrapAtOverflow() { let a: [Int] = [10, 20, 30] #expect(a.wrapAt(3) == 10) #expect(a.wrapAt(4) == 20) #expect(a.wrapAt(6) == 10) } @Test func wrapAtNegative() { let a: [Int] = [10, 20, 30] #expect(a.wrapAt(-1) == 30) #expect(a.wrapAt(-2) == 20) #expect(a.wrapAt(-3) == 10) #expect(a.wrapAt(-4) == 30) } @Test func wrapAtEmpty() { #expect([Int]().wrapAt(0) == nil) } @Test func clipAtInBounds() { let a: [Int] = [10, 20, 30] #expect(a.clipAt(0) == 10) #expect(a.clipAt(2) == 30) } @Test func clipAtOverflow() { let a: [Int] = [10, 20, 30] #expect(a.clipAt(3) == 30) #expect(a.clipAt(99) == 30) } @Test func clipAtNegative() { let a: [Int] = [10, 20, 30] #expect(a.clipAt(-1) == 10) #expect(a.clipAt(-99) == 10) } @Test func clipAtEmpty() { #expect([Int]().clipAt(0) == nil) } @Test func foldAtInBounds() { // [10, 20, 30, 40] — fold: 0 1 2 3 2 1 0 … let a: [Int] = [10, 20, 30, 40] #expect(a.foldAt(0) == 10) #expect(a.foldAt(1) == 20) #expect(a.foldAt(2) == 30) #expect(a.foldAt(3) == 40) } @Test func foldAtOverflow() { let a: [Int] = [10, 20, 30, 40] #expect(a.foldAt(4) == 30) // fold back: 3→40, 4→30 #expect(a.foldAt(5) == 20) #expect(a.foldAt(6) == 10) #expect(a.foldAt(7) == 20) // new period starts up again } @Test func foldAtNegative() { let a: [Int] = [10, 20, 30, 40] #expect(a.foldAt(-1) == 20) // folds from 0: -1 → index 1 #expect(a.foldAt(-2) == 30) #expect(a.foldAt(-3) == 40) } @Test func foldAtSingleElement() { #expect([42].foldAt(0) == 42) #expect([42].foldAt(99) == 42) #expect([42].foldAt(-5) == 42) } @Test func foldAtEmpty() { #expect([Int]().foldAt(0) == nil) } // MARK: - Collection scalar operators @Test func collectionIntMultiply() { let result: [Int] = [100, 100, 200] * 3 #expect(result == [300, 300, 600]) } @Test func collectionIntMultiplyLeading() { let result: [Int] = 3 * [100, 100, 200] #expect(result == [300, 300, 600]) } @Test func collectionIntAdd() { let result: [Int] = [1, 2, 3] + 10 #expect(result == [11, 12, 13]) } @Test func collectionIntSubtract() { let result: [Int] = [10, 20, 30] - 5 #expect(result == [5, 15, 25]) } @Test func collectionDoubleMultiply() { let result: [Double] = [0.5, 1.0, 2.0] * 2.0 #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 2.0) < accuracy) #expect(abs(result[2] - 4.0) < accuracy) } @Test func collectionDoubleDivide() { let result: [Double] = [1.0, 2.0, 4.0] / 2.0 #expect(abs(result[0] - 0.5) < accuracy) #expect(abs(result[1] - 1.0) < accuracy) #expect(abs(result[2] - 2.0) < accuracy) } @Test func collectionDoubleLeadingDivide() { let result: [Double] = 1.0 / [1.0, 2.0, 4.0] #expect(abs(result[0] - 1.0) < accuracy) #expect(abs(result[1] - 0.5) < accuracy) #expect(abs(result[2] - 0.25) < accuracy) } // MARK: - mapIntoRange @Test func mapIntoRangeIntSpecExamples() { #expect(1.mapIntoRange(low: 60, high: 77, modulo: 12) == 61) #expect(84.mapIntoRange(low: 60, high: 77, modulo: 12) == 72) } @Test func mapIntoRangeDoubleSpecExamples() { #expect(abs(61.1.mapIntoRange(low: 60, high: 77, modulo: 12) - 61.1) < accuracy) #expect(abs(84.0.mapIntoRange(low: 60, high: 77, modulo: 12) - 72.0) < accuracy) } @Test func mapIntoRangeAlreadyInRange() { #expect(70.mapIntoRange(low: 60, high: 77, modulo: 12) == 70) #expect(60.mapIntoRange(low: 60, high: 77, modulo: 12) == 60) #expect(77.mapIntoRange(low: 60, high: 77, modulo: 12) == 77) } @Test func mapIntoRangeNegativeInput() { // -50 + 12*k into [60,77]: -50→-38→…→70 (k=10) #expect((-50).mapIntoRange(low: 60, high: 77, modulo: 12) == 70) } @Test func mapIntoRangePitchClassPreserved() { // every result ≡ input (mod 12) for n: Int in stride(from: -36, through: 144, by: 1) { let mapped: Int = n.mapIntoRange(low: 60, high: 77, modulo: 12) #expect(((mapped - n) % 12) == 0) #expect(mapped >= 60 && mapped <= 77) } } @Test func mapIntoRangeNonPositiveModuloReturnsSelf() { #expect(5.mapIntoRange(low: 60, high: 77, modulo: 0) == 5) #expect(5.mapIntoRange(low: 60, high: 77, modulo: -12) == 5) #expect(abs(5.0.mapIntoRange(low: 60, high: 77, modulo: 0) - 5.0) < accuracy) } @Test func mapIntoRangeInvertedBoundsReturnsSelf() { #expect(5.mapIntoRange(low: 77, high: 60, modulo: 12) == 5) } @Test func mapIntoRangeNoRepresentativeReturnsSelf() { // [60,65] spans 5 < modulo 12. Pitch class of 8 (≡68,56,…) never lands in // [60,65], so self is returned unchanged. #expect(8.mapIntoRange(low: 60, high: 65, modulo: 12) == 8) #expect(abs(8.0.mapIntoRange(low: 60, high: 65, modulo: 12) - 8.0) < accuracy) // narrow range that DOES contain the pitch class still maps. #expect(1.mapIntoRange(low: 60, high: 65, modulo: 12) == 61) } }