Repository Public
1//2// VQuant iOS3// File: CoreTests/ChartViewportTests.swift4// Description: Tests of the chart geometry: window clamping, zoom about an anchor, pan, index/position mapping,5// display aggregation factor and axis ticks, including degenerate inputs.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9//1011import Foundation12import Testing13@testable import VQuantCore1415@Suite("Fenêtre du graphique")16struct ChartViewportTests {17 @Test func openingWindow() {18 let small = ChartViewport(count: 120)19 #expect(small.start == 0 && small.length == 120 && small.showsEverything)20 let exact = ChartViewport(count: 240)21 #expect(exact.start == 0 && exact.length == 240)22 let long = ChartViewport(count: 5000)23 #expect(long.length == 240 && long.end == 5000 && !long.showsEverything)24 #expect(long.range == 4760..<5000)25 }2627 @Test func emptyAndSingleBar() {28 let none = ChartViewport(count: 0)29 #expect(none.isEmpty && none.range.isEmpty && none.length == 0)30 #expect(none.index(x: 10, width: 100) == nil)31 #expect(none.zoomed(by: 3, anchor: 0.5) == none && none.panned(byBars: 5) == none)32 #expect(none.alignedRange(factor: 4).isEmpty)33 #expect(ChartViewport(count: -3).isEmpty)34 let one = ChartViewport(count: 1)35 #expect(one.range == 0..<1 && one.length == 1)36 #expect(one.zoomed(by: 10, anchor: 0.5).length == 1) // fewer bars than the minimum: all stay37 #expect(one.index(x: 99, width: 100) == 0)38 #expect(one.x(index: 0, width: 100) == 50)39 }4041 @Test func zoomClampsAtBothEnds() {42 let v = ChartViewport(count: 1000, start: 400, length: 200)43 let inMax = v.zoomed(by: 1_000_000, anchor: 0.5)44 #expect(inMax.length == Double(ChartViewport.minimumVisible))45 let outMax = v.zoomed(by: 0.000_001, anchor: 0.5)46 #expect(outMax.length == 1000 && outMax.start == 0 && outMax.showsEverything)47 #expect(v.zoomed(by: 0, anchor: 0.5) == v)48 #expect(v.zoomed(by: -.infinity, anchor: 0.5) == v)49 #expect(v.zoomed(by: .nan, anchor: 0.5) == v)50 #expect(ChartViewport(count: 6).zoomed(by: 4, anchor: 0).length == 6)51 }5253 @Test(arguments: [0.0, 0.25, 0.5, 0.9, 1.0], [0.5, 1.5, 2.0, 4.0])54 func zoomKeepsTheAnchoredBarInPlace(anchor: Double, scale: Double) {55 let v = ChartViewport(count: 10_000, start: 5000, length: 400)56 let z = v.zoomed(by: scale, anchor: anchor)57 #expect(abs(z.length - 400 / scale) < 1e-9)58 #expect(abs((z.start + anchor * z.length) - (v.start + anchor * v.length)) < 1e-9)59 // the same bar is under the same horizontal position60 let width = 320.061 #expect(v.index(x: anchor * width * 0.999, width: width) == z.index(x: anchor * width * 0.999, width: width))62 }6364 @Test func zoomAnchorOutsideUnitIntervalIsClamped() {65 let v = ChartViewport(count: 1000, start: 400, length: 200)66 #expect(v.zoomed(by: 2, anchor: -3) == v.zoomed(by: 2, anchor: 0))67 #expect(v.zoomed(by: 2, anchor: 7) == v.zoomed(by: 2, anchor: 1))68 }6970 @Test func panClampsAtBothEnds() {71 let v = ChartViewport(count: 1000, start: 400, length: 200)72 #expect(v.panned(byBars: 50).start == 450)73 #expect(v.panned(byBars: -50).start == 350)74 let left = v.panned(byBars: -1e9)75 #expect(left.start == 0 && left.length == 200)76 let right = v.panned(byBars: 1e9)77 #expect(right.end == 1000 && right.length == 200)78 #expect(v.panned(byBars: .nan) == v)79 #expect(v.panned(byBars: 123).reset() == ChartViewport(count: 1000))80 }8182 @Test func outOfBoundsInitialisersAreNormalised() {83 let a = ChartViewport(count: 100, start: -50, length: 5000)84 #expect(a.start == 0 && a.length == 100)85 let b = ChartViewport(count: 100, start: 99, length: 2)86 #expect(b.length == 10 && b.end == 100)87 let c = ChartViewport(count: 100, start: .nan, length: .infinity)88 #expect(c.start == 0 && c.length == 100)89 }9091 @Test func indexAndPositionAgree() {92 let v = ChartViewport(count: 500, start: 100, length: 50)93 let width = 400.094 #expect(v.slot(width: width) == 8)95 for i in [100, 101, 125, 149] {96 let x = v.x(index: Double(i), width: width)97 #expect(x > 0 && x < width)98 #expect(v.index(x: x, width: width) == i)99 }100 #expect(v.index(x: -40, width: width) == 100) // clamped to the visible bars101 #expect(v.index(x: 9999, width: width) == 149)102 #expect(v.index(x: 10, width: 0) == nil)103 #expect(v.slot(width: 0) == 0)104 let fractional = ChartViewport(count: 500, start: 100.5, length: 50)105 #expect(fractional.range == 100..<151)106 }107108 @Test func alignedRangeCoversTheWindowWithWholeGroups() {109 let v = ChartViewport(count: 1003, start: 205, length: 300)110 for f in [1, 2, 3, 7, 50] {111 let r = v.alignedRange(factor: f)112 #expect(r.lowerBound % f == 0)113 #expect(r.lowerBound <= v.range.lowerBound && r.upperBound >= v.range.upperBound)114 #expect(r.upperBound <= 1003)115 #expect(r.upperBound % f == 0 || r.upperBound == 1003)116 }117 #expect(v.alignedRange(factor: 0) == v.range)118 // groups do not move when the window is panned by less than a group119 #expect(v.alignedRange(factor: 50).lowerBound == v.panned(byBars: 10).alignedRange(factor: 50).lowerBound)120 }121122 @Test func aggregationFactor() {123 #expect(ChartMath.aggregationFactor(visibleBars: 100, width: 300, minimumSlot: 3) == 1)124 #expect(ChartMath.aggregationFactor(visibleBars: 101, width: 300, minimumSlot: 3) == 2)125 #expect(ChartMath.aggregationFactor(visibleBars: 11_000, width: 330, minimumSlot: 3) == 100)126 #expect(ChartMath.aggregationFactor(visibleBars: 0, width: 300, minimumSlot: 3) == 1)127 #expect(ChartMath.aggregationFactor(visibleBars: 500, width: 0, minimumSlot: 3) == 1)128 #expect(ChartMath.aggregationFactor(visibleBars: 500, width: 300, minimumSlot: 0) == 1)129 #expect(ChartMath.aggregationFactor(visibleBars: 500, width: 1, minimumSlot: 3) == 500)130 for n in [1, 7, 240, 999, 12_345] {131 for w in [1.0, 77.0, 320.0, 1024.0] {132 let f = ChartMath.aggregationFactor(visibleBars: n, width: w, minimumSlot: 3)133 #expect(f >= 1)134 // marks fit: at most one mark per 3 points (or a single mark)135 let marks = (n + f - 1) / f136 #expect(Double(marks) * 3 <= w || marks == 1)137 }138 }139 }140141 @Test func ticksAreRoundMonotoneAndInsideTheRange() {142 let t = ChartMath.niceTicks(min: 341.2, max: 358.7)143 #expect(t == [345, 350, 355])144 for (lo, hi) in [(0.0, 1.0), (-3.7, 12.2), (0.000_12, 0.000_19), (7285.76, 7367.68), (1e9, 3.5e9), (99.99, 100.01)] {145 let ticks = ChartMath.niceTicks(min: lo, max: hi)146 #expect(ticks.count >= 2 && ticks.count <= 12)147 #expect(ticks.allSatisfy { $0 >= lo && $0 <= hi })148 #expect(zip(ticks, ticks.dropFirst()).allSatisfy { $0 < $1 })149 }150 #expect(ChartMath.niceTicks(min: 10, max: 0) == ChartMath.niceTicks(min: 0, max: 10))151 }152153 @Test func ticksOfDegenerateRanges() {154 #expect(ChartMath.niceTicks(min: 42, max: 42) == [42]) // flat prices155 #expect(ChartMath.niceTicks(min: .nan, max: 1).isEmpty)156 #expect(ChartMath.niceTicks(min: 0, max: .infinity).isEmpty)157 #expect(ChartMath.niceStep(span: 0, target: 5) == 0)158 #expect(ChartMath.niceStep(span: 10, target: 5) == 2)159 #expect(ChartMath.niceStep(span: 12, target: 5) == 2.5)160 #expect(ChartMath.niceStep(span: 100, target: 4) == 25)161 }162163 @Test func paddedRangeAndVerticalMapping() {164 let r = ChartMath.paddedRange(low: 100, high: 200, padding: 0.1)165 #expect(r == 90...210)166 let flat = ChartMath.paddedRange(low: 50, high: 50)167 #expect(flat.lowerBound < 50 && flat.upperBound > 50)168 let zero = ChartMath.paddedRange(low: 0, high: 0)169 #expect(zero == -1...1)170 #expect(ChartMath.paddedRange(low: .nan, high: 1) == 0...1)171 #expect(ChartMath.paddedRange(low: 9, high: 3, padding: 0) == 3...9)172 #expect(ChartMath.y(210, in: r, height: 120) == 0)173 #expect(ChartMath.y(90, in: r, height: 120) == 120)174 #expect(ChartMath.y(150, in: r, height: 120) == 60)175 #expect(ChartMath.y(5, in: 5...5, height: 80) == 40)176 }177}178