Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/SPGlassDataTests/GlassChartTests.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 SPGlassData4445@Suite("GlassChart")46struct GlassChartTests {47 let english = Locale(identifier: "en_US")48 let utc = TimeZone(identifier: "UTC") ?? .gmt4950 func points(_ values: [Double], series: String? = nil) -> [GlassChartPoint] {51 values.enumerated().map { GlassChartPoint(label: "P\($0.offset + 1)", value: $0.element, series: series) }52 }5354 @Test func summaryReportsCountExtremesAndRisingTrend() throws {55 let summary = try #require(GlassChartLogic.summaries(points([4, 9, 2, 12])).first)56 #expect(summary.count == 4)57 #expect(summary.series == nil)58 #expect(summary.minimum.value == 2)59 #expect(summary.maximum.value == 12)60 #expect(summary.first.x == .category("P1"))61 #expect(summary.last.x == .category("P4"))62 #expect(summary.trend == .up)63 }6465 @Test func trendIsDownOrFlat() throws {66 #expect(try #require(GlassChartLogic.summaries(points([10, 12, 3])).first).trend == .down)67 #expect(try #require(GlassChartLogic.summaries(points([100, 140, 100.5])).first).trend == .flat)68 #expect(try #require(GlassChartLogic.summaries(points([7])).first).trend == .flat)69 #expect(try #require(GlassChartLogic.summaries(points([0, 0, 0])).first).trend == .flat)70 #expect(try #require(GlassChartLogic.summaries(points([-5, -9])).first).trend == .down)71 }7273 @Test func equalExtremesKeepTheEarliestPoint() throws {74 let summary = try #require(GlassChartLogic.summaries(points([3, 1, 3, 1])).first)75 #expect(summary.minimum.x == .category("P2"))76 #expect(summary.maximum.x == .category("P1"))77 }7879 @Test func nonFiniteValuesAreIgnored() throws {80 let data = points([.nan, 5, .infinity, 8, -.infinity])81 #expect(GlassChartLogic.plottable(data).map(\.value) == [5, 8])82 let summary = try #require(GlassChartLogic.summaries(data).first)83 #expect(summary.count == 2)84 #expect(summary.maximum.value == 8)85 #expect(GlassChartLogic.summaries(points([.nan])).isEmpty)86 #expect(GlassChartLogic.summaries([]).isEmpty)87 #expect(GlassChartLogic.description(of: [], locale: english) == "")88 }8990 @Test func seriesAreSummarisedSeparatelyInOrderOfAppearance() {91 let data = points([1, 2], series: "North") + points([9, 4], series: "South") + points([3], series: "North")92 let summaries = GlassChartLogic.summaries(data)93 #expect(summaries.map(\.series) == ["North", "South"])94 #expect(summaries.map(\.count) == [3, 2])95 #expect(summaries.map(\.trend) == [.up, .down])96 #expect(GlassChartLogic.seriesNames(data) == ["North", "South"])97 }9899 @Test func timeAxisNeedsOnlyDates() {100 let date = Date(timeIntervalSince1970: 1_780_000_000)101 #expect(GlassChartLogic.usesTimeAxis([GlassChartPoint(date: date, value: 1)]))102 #expect(!GlassChartLogic.usesTimeAxis([GlassChartPoint(date: date, value: 1), GlassChartPoint(label: "Q1", value: 2)]))103 #expect(!GlassChartLogic.usesTimeAxis([]))104 }105106 @Test func derivedIdentifiersAreUniquePerSeriesAndPosition() {107 let data = points([1, 2, 3], series: "A") + points([1, 2, 3], series: "B")108 #expect(Set(data.map(\.id)).count == 6)109 #expect(GlassChartPoint(id: "custom", label: "x", value: 1).id == "custom")110 }111112 @Test func valuesAreFormattedWithTheLocaleAndTheUnit() {113 #expect(GlassChartLogic.valueText(1234.567, locale: english) == "1,234.57")114 #expect(GlassChartLogic.valueText(12, unit: "ms", locale: english) == "12\u{00A0}ms")115 #expect(GlassChartLogic.valueText(12, unit: "", locale: english) == "12")116 #expect(GlassChartLogic.valueText(0.5, locale: Locale(identifier: "fr_CA")).contains("0,5"))117 }118119 @Test func englishDescription() {120 let text = GlassChartLogic.description(of: points([4, 9, 2, 12]), unit: "k$", locale: english, localization: "en")121 #expect(text == "4 values from P1 to P4. Minimum 2\u{00A0}k$, maximum 12\u{00A0}k$. Trend: Up.")122 }123124 @Test func frenchDescriptionNamesTheSeries() {125 let text = GlassChartLogic.description(of: points([8, 3], series: "Nord"), locale: Locale(identifier: "fr_CA"), localization: "fr")126 #expect(text.hasPrefix("Nord: 2 valeurs de P1 à P2."))127 #expect(text.contains("Minimum 3, maximum 8."))128 #expect(text.hasSuffix(GlassTrend.down.label(localization: "fr") + "."))129 }130131 @Test func dateLabelsUseTheGivenTimeZone() {132 // 2026-03-09 03:30 UTC is still March 8 in Toronto.133 let instant = Date(timeIntervalSince1970: 1_773_027_000)134 let toronto = TimeZone(identifier: "America/Toronto") ?? utc135 #expect(GlassChartX.date(instant).label(locale: english, timeZone: utc) == "Mar 9, 2026")136 #expect(GlassChartX.date(instant).label(locale: english, timeZone: toronto) == "Mar 8, 2026")137 #expect(GlassChartX.category("Q1").label(locale: english) == "Q1")138 }139140 @Test func trendThresholds() {141 #expect(GlassTrend.of(change: 0.2) == .up)142 #expect(GlassTrend.of(change: -0.2) == .down)143 #expect(GlassTrend.of(change: 0) == .flat)144 #expect(GlassTrend.of(change: 0.2, tolerance: 0.5) == .flat)145 #expect(GlassTrend.of(change: .nan) == .flat)146 #expect(GlassTrend.of(change: .infinity) == .flat)147 #expect(Set(GlassTrend.allCases.map(\.systemImage)).count == 3)148 #expect(GlassTrend.up.label(localization: "en") == "Up")149 }150}151