subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

SubsurfaceVectorsTests.swift (12839B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import XCTest
      3 @testable import DivePlanner
      4 
      5 /// The plans of Subsurface's own test suite (`tests/testplan.cpp`), with the run times it records as "known
      6 /// Subsurface runtime". The port is expected to give the same, to the second.
      7 final class SubsurfaceVectorsTests: XCTestCase {
      8     private let air = CoreGas(o2: 210, he: 0)
      9     private let ean36 = CoreGas(o2: 360, he: 0)
     10     private let ean50 = CoreGas(o2: 500, he: 0)
     11     private let oxygen = CoreGas(o2: 1000, he: 0)
     12     private let tx18_45 = CoreGas(o2: 180, he: 450)
     13 
     14     /// `dive::gas_mod` at 1.6 bar, rounded to 3 m.
     15     private func mod(_ gas: CoreGas, _ environment: CoreEnvironment = CoreEnvironment()) -> Int {
     16         let depth = Double(environment.depth(relativeMbar: 1600 * 1000 / gas.o2 - environment.surfaceMbar))
     17         return Int(depth / 3000 + 0.1) * 3000
     18     }
     19 
     20     private func deco(_ gas: CoreGas) -> CoreCylinder { CoreCylinder(gas: gas, changeDepth: mod(gas)) }
     21 
     22     /// `setupPrefsVpmb`: 10 m/min everywhere (166 mm/s once divided), last stop at 3 m, conservatism 0.
     23     private var vpmbPrefs: CorePrefs {
     24         var prefs = CorePrefs()
     25         prefs.ascRate75 = 10000 / 60
     26         prefs.ascRate50 = 10000 / 60
     27         prefs.ascRateStops = 10000 / 60
     28         prefs.ascRateLast6m = 10000 / 60
     29         return prefs
     30     }
     31 
     32     private var vpmb: DecoState {
     33         var model = DecoState()
     34         model.vpmb = true
     35         model.conservatism = 0
     36         return model
     37     }
     38 
     39     /// `setupPrefs`: 30 ft/min, 10 ft/min above 6 m, last stop at 6 m.
     40     private var imperialPrefs: CorePrefs {
     41         var prefs = CorePrefs()
     42         prefs.ascRate75 = 9144 / 60
     43         prefs.ascRate50 = 9144 / 60
     44         prefs.ascRateStops = 9144 / 60
     45         prefs.ascRateLast6m = 3048 / 60
     46         prefs.lastStopAt6m = true
     47         return prefs
     48     }
     49 
     50     /// A square dive: straight down at `descent` mm/min, then the rest of `minutes` at the bottom.
     51     private func square(_ depth: Int, minutes: Int, descent: Int) -> [CoreWaypoint] {
     52         let drop = depth * 60 / descent
     53         return [CoreWaypoint(seconds: drop, depth: depth, cylinder: 0), CoreWaypoint(seconds: minutes * 60 - drop, depth: depth, cylinder: 0)]
     54     }
     55 
     56     private func switches(_ output: CoreOutput) -> [(depth: Int, cylinder: Int)] {
     57         var found = [(depth: Int, cylinder: Int)]()
     58         for (index, point) in output.track.enumerated() where index > 0 && output.track[index - 1].cylinder != point.cylinder && point.time >= output.bottomTime {
     59             found.append((point.depth, point.cylinder))
     60         }
     61         return found
     62     }
     63 
     64     func testBuhlmannMetric() {
     65         var model = DecoState()
     66         model.gfLow = 1
     67         model.gfHigh = 1
     68         let output = CorePlanner.plan(waypoints: square(79_000, minutes: 30, descent: 23_000),
     69                                       cylinders: [CoreCylinder(gas: CoreGas(o2: 150, he: 450)), deco(ean36), deco(oxygen)],
     70                                       prefs: imperialPrefs, model: model)
     71         XCTAssertEqual(output.runtime, 109 * 60)
     72         XCTAssertEqual(switches(output).map(\.depth), [33_000, 6000])
     73         XCTAssertEqual(switches(output).map(\.cylinder), [1, 2])
     74     }
     75 
     76     func testVpmb60m30minAir() {
     77         let output = CorePlanner.plan(waypoints: square(60_000, minutes: 30, descent: 99_000), cylinders: [CoreCylinder(gas: air)],
     78                                       prefs: vpmbPrefs, model: vpmb)
     79         XCTAssertEqual(output.runtime, 139 * 60 + 20)
     80     }
     81 
     82     func testVpmb60m30minEan50() {
     83         let output = CorePlanner.plan(waypoints: square(60_000, minutes: 30, descent: 99_000), cylinders: [CoreCylinder(gas: air), deco(ean50)],
     84                                       prefs: vpmbPrefs, model: vpmb)
     85         XCTAssertEqual(output.runtime, 96 * 60 + 20)
     86         XCTAssertEqual(switches(output).map(\.depth), [21_000])
     87     }
     88 
     89     func testVpmb60m30minTrimix() {
     90         let output = CorePlanner.plan(waypoints: square(60_000, minutes: 30, descent: 99_000), cylinders: [CoreCylinder(gas: tx18_45), deco(ean50)],
     91                                       prefs: vpmbPrefs, model: vpmb)
     92         XCTAssertEqual(output.runtime, 89 * 60 + 20)
     93         XCTAssertEqual(switches(output).map(\.depth), [21_000])
     94     }
     95 
     96     func testVpmb100m60min() {
     97         let output = CorePlanner.plan(waypoints: square(100_000, minutes: 60, descent: 99_000),
     98                                       cylinders: [CoreCylinder(gas: tx18_45), deco(ean50), deco(oxygen)], prefs: vpmbPrefs, model: vpmb)
     99         XCTAssertEqual(output.runtime, 315 * 60 + 20)
    100         XCTAssertEqual(switches(output).map(\.depth), [21_000, 6000])
    101     }
    102 
    103     func testVpmb100m10min() {
    104         let output = CorePlanner.plan(waypoints: square(100_000, minutes: 10, descent: 99_000),
    105                                       cylinders: [CoreCylinder(gas: tx18_45), deco(ean50), deco(oxygen)], prefs: vpmbPrefs, model: vpmb)
    106         XCTAssertEqual(output.runtime, 57 * 60 + 20)
    107         XCTAssertEqual(switches(output).map(\.depth), [21_000, 6000])
    108     }
    109 
    110     func testVpmbMultiLevelAir() {
    111         let drop = 20_000 * 60 / 99_000
    112         let waypoints = [CoreWaypoint(seconds: drop, depth: 20_000, cylinder: 0), CoreWaypoint(seconds: 600 - drop, depth: 20_000, cylinder: 0),
    113                          CoreWaypoint(seconds: 60, depth: 60_000, cylinder: 0), CoreWaypoint(seconds: 29 * 60, depth: 60_000, cylinder: 0)]
    114         let output = CorePlanner.plan(waypoints: waypoints, cylinders: [CoreCylinder(gas: air)], prefs: vpmbPrefs, model: vpmb)
    115         XCTAssertEqual(output.runtime, 169 * 60 + 20)
    116     }
    117 
    118     func testVpmb30m20min() {
    119         let output = CorePlanner.plan(waypoints: square(30_000, minutes: 20, descent: 18_000), cylinders: [CoreCylinder(gas: air)],
    120                                       prefs: vpmbPrefs, model: vpmb)
    121         XCTAssertEqual(output.runtime, 27 * 60 + 20)
    122     }
    123 
    124     /// Several gases in the part the diver enters, one of them breathed on the way up between two levels.
    125     func testVpmbSeveralGases() {
    126         let waypoints = [CoreWaypoint(seconds: 120, depth: 40_000, cylinder: 0), CoreWaypoint(seconds: 18 * 60, depth: 40_000, cylinder: 0),
    127                          CoreWaypoint(seconds: 10 * 60, depth: 10_000, cylinder: 1), CoreWaypoint(seconds: 5 * 60, depth: 10_000, cylinder: 0)]
    128         let output = CorePlanner.plan(waypoints: waypoints, cylinders: [CoreCylinder(gas: ean36), CoreCylinder(gas: CoreGas(o2: 110, he: 500))],
    129                                       prefs: vpmbPrefs, model: vpmb)
    130         XCTAssertEqual(output.runtime, 2480)
    131     }
    132 
    133     /// Closed circuit at a setpoint of 1.3 bar, then open-circuit bailout: trimix 19/33 at the bottom, EAN53 higher up.
    134     func testCcrBailout() {
    135         var model = DecoState()
    136         model.gfLow = 0.5
    137         model.gfHigh = 0.7
    138         var prefs = imperialPrefs
    139         prefs.bailout = true
    140         let ean53 = CoreGas(o2: 530, he: 0), tx19_33 = CoreGas(o2: 190, he: 330)
    141         XCTAssertEqual(mod(ean53), 18_000)
    142         XCTAssertEqual(mod(tx19_33), 72_000)
    143         let output = CorePlanner.plan(waypoints: [CoreWaypoint(seconds: 20 * 60, depth: 60_000, cylinder: 0, setpoint: 1300, mode: .closedCircuit)],
    144                                       cylinders: [CoreCylinder(gas: CoreGas(o2: 200, he: 210)), deco(ean53), deco(tx19_33)], prefs: prefs, model: model)
    145         XCTAssertEqual(output.runtime, 51 * 60)
    146         func cylinder(at time: Int) -> Int { output.track.last { $0.time <= time }?.cylinder ?? -1 }
    147         XCTAssertEqual(cylinder(at: 20 * 60 - 1), 0)
    148         XCTAssertEqual(cylinder(at: 20 * 60 + 1), 2)
    149         XCTAssertEqual(cylinder(at: 30 * 60), 1)
    150     }
    151 
    152     /// `testImperial`: the same plan as `testMetric` in feet (260 ft, stops every 10 ft, switches at 110 ft and 20 ft).
    153     func testBuhlmannImperial() {
    154         func feet(_ value: Double) -> Int { Int((value * 304.8).rounded(.toNearestOrEven)) }
    155         func modFeet(_ gas: CoreGas) -> Int {
    156             let environment = CoreEnvironment()
    157             let depth = Double(environment.depth(relativeMbar: 1600 * 1000 / gas.o2 - environment.surfaceMbar))
    158             return Int(depth / 3048 + 0.1) * 3048
    159         }
    160         var model = DecoState()
    161         model.gfLow = 1
    162         model.gfHigh = 1
    163         var prefs = imperialPrefs
    164         prefs.imperialStops = true
    165         let depth = feet(260)
    166         let drop = depth * 60 / feet(75)
    167         let output = CorePlanner.plan(waypoints: [CoreWaypoint(seconds: drop, depth: depth, cylinder: 0), CoreWaypoint(seconds: 30 * 60 - drop, depth: depth, cylinder: 0)],
    168                                       cylinders: [CoreCylinder(gas: CoreGas(o2: 150, he: 450)),
    169                                                   CoreCylinder(gas: ean36, changeDepth: modFeet(ean36)), CoreCylinder(gas: oxygen, changeDepth: modFeet(oxygen))],
    170                                       prefs: prefs, model: model)
    171         XCTAssertEqual(output.runtime, 111 * 60 - 2)
    172         XCTAssertEqual(switches(output).map(\.depth), [33_528, 6096])
    173     }
    174 
    175     /// Recreational mode: 20 m for 20 minutes on air is within the limit; the bottom time is extended to the
    176     /// no-stop limit or to what a 24 L cylinder allows, and the ascent has its safety stop.
    177     func testRecreational() {
    178         var model = DecoState()
    179         model.gfLow = 1
    180         model.gfHigh = 1
    181         var prefs = imperialPrefs
    182         prefs.recreational = true
    183         let cylinder = CoreCylinder(gas: air, sizeMl: 24_000, startMbar: 232_000)
    184         let output = CorePlanner.plan(waypoints: square(20_000, minutes: 20, descent: 23_000), cylinders: [cylinder], prefs: prefs, model: model)
    185         XCTAssertFalse(output.exceedsNoStopLimit)
    186         XCTAssertGreaterThan(output.bottomExtension, 0)
    187         XCTAssertTrue(output.hasSafetyStop)
    188         XCTAssertTrue(output.track.contains { $0.depth == 5000 })
    189         XCTAssertEqual(output.track.last?.depth, 0)
    190         XCTAssertEqual(output.runtime % 2, 0)
    191         // Whatever limits it, the cylinder keeps its reserve for the ascent.
    192         XCTAssertLessThan(output.gasUsed[0] / 24_000 * 1000, 232_000 - 40_000)
    193 
    194         // 40 m for an hour needs stops: recreational mode says so instead of extending.
    195         let deep = CorePlanner.plan(waypoints: square(40_000, minutes: 60, descent: 23_000), cylinders: [cylinder], prefs: prefs, model: model)
    196         XCTAssertTrue(deep.exceedsNoStopLimit)
    197         XCTAssertEqual(deep.bottomExtension, 0)
    198     }
    199 
    200     /// A dive before the plan leaves gas in the tissues: the same plan is longer after it, less so after a long interval.
    201     func testRepetitiveDive() {
    202         var model = DecoState()
    203         model.gfLow = 0.4
    204         model.gfHigh = 0.85
    205         let waypoints = square(40_000, minutes: 25, descent: 18_000)
    206         let cylinders = [CoreCylinder(gas: air)]
    207         let first = CorePlanner.plan(waypoints: waypoints, cylinders: cylinders, prefs: CorePrefs(), model: model)
    208         func prior(interval: Int) -> CorePriorDive {
    209             CorePriorDive(samples: first.track.map { CorePriorDive.Sample(time: $0.time, depth: $0.depth, gas: air) }, surfaceInterval: interval)
    210         }
    211         let soon = CorePlanner.plan(waypoints: waypoints, cylinders: cylinders, prefs: CorePrefs(), model: model, after: [prior(interval: 3600)])
    212         let later = CorePlanner.plan(waypoints: waypoints, cylinders: cylinders, prefs: CorePrefs(), model: model, after: [prior(interval: 6 * 3600)])
    213         let nextDay = CorePlanner.plan(waypoints: waypoints, cylinders: cylinders, prefs: CorePrefs(), model: model, after: [prior(interval: 72 * 3600)])
    214         XCTAssertGreaterThan(soon.runtime, first.runtime)
    215         XCTAssertGreaterThanOrEqual(soon.runtime, later.runtime)
    216         XCTAssertGreaterThanOrEqual(later.runtime, first.runtime)
    217         XCTAssertEqual(nextDay.runtime, first.runtime)
    218     }
    219 
    220     /// On the loop, the setpoint can be raised for the ascent: more oxygen, shorter stops.
    221     func testSetpointSwitch() {
    222         var model = DecoState()
    223         model.gfLow = 0.5
    224         model.gfHigh = 0.7
    225         let diluent = CoreGas(o2: 200, he: 210)
    226         let waypoints = [CoreWaypoint(seconds: 20 * 60, depth: 60_000, cylinder: 0, setpoint: 1000, mode: .closedCircuit)]
    227         let plain = CorePlanner.plan(waypoints: waypoints, cylinders: [CoreCylinder(gas: diluent)], prefs: imperialPrefs, model: model)
    228         let raised = CorePlanner.plan(waypoints: waypoints,
    229                                       cylinders: [CoreCylinder(gas: diluent), CoreCylinder(gas: diluent, changeDepth: 21_000, setpoint: 1500)],
    230                                       prefs: imperialPrefs, model: model)
    231         XCTAssertLessThan(raised.runtime, plain.runtime)
    232         XCTAssertEqual(raised.track.last { $0.depth == 21_000 }?.setpoint, 1500)
    233         XCTAssertTrue(plain.track.allSatisfy { $0.setpoint == 1000 })
    234     }
    235 }