Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/SPGlassMediaTests/GlassWaveformTests.swift8// Module : Tests9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import Testing43@testable import SPGlassMedia4445@Suite("Waveform")46struct GlassWaveformTests {47 @Test func emptyInputGivesNoBuckets() {48 #expect(GlassWaveformDownsampler.downsample([], maxBuckets: 100).isEmpty)49 #expect(GlassWaveformData.empty.isEmpty)50 #expect(GlassWaveformData.empty.peak == 0)51 #expect(GlassWaveformData.empty.playedBucketCount(progress: 0.5) == 0)52 }5354 @Test func shortInputIsOnlySanitised() {55 let result = GlassWaveformDownsampler.downsample([0.2, -0.5, 2, .nan, .infinity, -.infinity], maxBuckets: 100)56 #expect(result == [0.2, 0.5, 1, 0, 1, 1])57 }5859 @Test func peakAggregationKeepsTheLargestMagnitude() {60 let result = GlassWaveformDownsampler.downsample([0.1, 0.9, 0.2, -0.4, 0.3, 0.3], maxBuckets: 3, aggregation: .peak)61 #expect(result == [0.9, 0.4, 0.3])62 }6364 @Test func rmsAggregationIsTheRootMeanSquare() {65 let result = GlassWaveformDownsampler.downsample([0.6, 0.8, 0, 0], maxBuckets: 2, aggregation: .rms)66 #expect(result.count == 2)67 #expect(abs(result[0] - Float((0.5 as Double).squareRoot())) < 0.0001)68 #expect(result[1] == 0)69 }7071 @Test("A million samples give a bounded output", arguments: GlassWaveformAggregation.allCases)72 func largeInputIsBounded(aggregation: GlassWaveformAggregation) {73 let samples = (0..<1_000_000).map { Float(sin(Double($0) / 500)) * 1.5 }74 let result = GlassWaveformDownsampler.downsample(samples, maxBuckets: 200, aggregation: aggregation)75 #expect(result.count == 200)76 #expect(result.allSatisfy { $0 >= 0 && $0 <= 1 && $0.isFinite })77 let data = GlassWaveformData(samples: samples, maxBuckets: 200, aggregation: aggregation)78 #expect(data.sampleCount == 1_000_000)79 #expect(data.buckets.count == 200)80 }8182 @Test func theBucketCountIsLimited() {83 let samples = [Float](repeating: 0.5, count: 10_000)84 #expect(GlassWaveformDownsampler.downsample(samples, maxBuckets: 1_000_000).count == GlassWaveformDownsampler.bucketLimit)85 #expect(GlassWaveformDownsampler.downsample(samples, maxBuckets: 0).count == 1)86 #expect(GlassWaveformDownsampler.downsample(samples, maxBuckets: -5).count == 1)87 }8889 @Test func everySampleFallsInExactlyOneBucket() {90 // 7 samples in 3 buckets: a single 1 must appear in exactly one bucket wherever it is.91 for position in 0..<7 {92 var samples = [Float](repeating: 0, count: 7)93 samples[position] = 194 let result = GlassWaveformDownsampler.downsample(samples, maxBuckets: 3)95 #expect(result.filter { $0 == 1 }.count == 1)96 }97 }9899 @Test func progressColoursWholeBuckets() {100 let data = GlassWaveformData(samples: [Float](repeating: 0.5, count: 10))101 #expect(data.playedBucketCount(progress: nil) == 0)102 #expect(data.playedBucketCount(progress: 0) == 0)103 #expect(data.playedBucketCount(progress: 0.35) == 3)104 #expect(data.playedBucketCount(progress: 1) == 10)105 #expect(data.playedBucketCount(progress: 7) == 10)106 #expect(data.playedBucketCount(progress: -1) == 0)107 #expect(data.playedBucketCount(progress: .nan) == 0)108 }109110 @Test func theSummaryDescribesTheData() {111 let data = GlassWaveformData(samples: [0.1, 0.8, 0.3])112 #expect(data.peakPercent == 80)113 #expect(data.accessibilitySummary.contains("3"))114 #expect(data.accessibilitySummary.contains("80"))115 #expect(!GlassWaveformData.empty.accessibilitySummary.isEmpty)116 }117118 @Test func barsStayVisibleAndInsideTheCanvas() {119 #expect(GlassWaveformLayout.barHeight(amplitude: 0, available: 60) == 2)120 #expect(GlassWaveformLayout.barHeight(amplitude: 0.5, available: 60) == 30)121 #expect(GlassWaveformLayout.barHeight(amplitude: 4, available: 60) == 60)122 #expect(GlassWaveformLayout.barHeight(amplitude: .nan, available: 60) == 2)123 #expect(GlassWaveformLayout.barHeight(amplitude: 1, available: 1) == 1)124 }125}126