open-merou

Open Mérou, a free iOS app for the Oceanic+ underwater iPhone housing
git clone https://git.schurger.org/open-merou.git
Log | Files | Refs | README | LICENSE

HousingDepthSourceTests.swift (4564B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import Foundation
      3 import RemoraDeco
      4 import Testing
      5 @testable import OpenMerou
      6 
      7 /// The adapter between the housing's external pressure (1625, Pa) and the dive logic of RemoraKit.
      8 struct HousingDepthSourceTests {
      9     let t0 = Date(timeIntervalSince1970: 1_000_000)
     10     /// Pa per metre of fresh water, as the basic depth estimate uses.
     11     let perMetre = 9_806.65
     12 
     13     @Test func surfaceIsTheLowestPressureSeenAndReadingsAreInBar() throws {
     14         var source = HousingDepthSource()
     15         _ = source.pressure(99_450, time: 0, now: t0)
     16         _ = source.pressure(99_400, time: 1, now: t0 + 1)
     17         let sample = source.pressure(99_400 + 10 * perMetre, time: 2, now: t0 + 2)
     18         let reading = try #require(sample)
     19         guard case .pressure(let time, let surface, let absolute, let submerged) = reading else {
     20             Issue.record("not a pressure reading")
     21             return
     22         }
     23         #expect(time == 2)
     24         #expect(abs(surface - 0.994) < 1e-9)
     25         #expect(abs((absolute ?? 0) - 1.974665) < 1e-6)
     26         #expect(submerged)
     27         #expect(source.surfacePa == 99_400)
     28         #expect(!source.surfaceAssumed)
     29         // The surface does not follow the pressure down the water column.
     30         _ = source.pressure(250_000, time: 3, now: t0 + 3)
     31         #expect(source.surfacePa == 99_400)
     32     }
     33 
     34     @Test func atTheSurfaceTheReadingIsNotSubmerged() throws {
     35         var source = HousingDepthSource()
     36         let sample = source.pressure(99_400, time: 0, now: t0)
     37         let reading = try #require(sample)
     38         #expect(reading == .pressure(time: 0, surface: 0.994, absolute: 0.994, submerged: false))
     39         // A few centimetres of water or a weather change is not a dive.
     40         _ = source.pressure(99_400 + 2_000, time: 1, now: t0 + 1)
     41         #expect(!source.submerged)
     42     }
     43 
     44     @Test func valuesThatCannotBeAPressureAreIgnored() {
     45         var source = HousingDepthSource()
     46         // 1625 is a hypothesis: zero, a counter, anything out of range must never reach the dive computer.
     47         for value in [0.0, 120.0, 49_999, 5_000_000] {
     48             #expect(source.pressure(value, time: 0, now: t0) == nil)
     49             #expect(source.implausible)
     50         }
     51         #expect(source.surfacePa == nil)
     52         #expect(source.depth == nil)
     53         #expect(source.pressure(99_400, time: 1, now: t0) != nil)
     54         #expect(!source.implausible)
     55     }
     56 
     57     @Test func startedUnderwaterAssumesTheStandardAtmosphere() throws {
     58         var source = HousingDepthSource()
     59         // First value at about 10 m: it cannot be a surface pressure (the kit refuses one above 1.2 bar).
     60         let sample = source.pressure(200_000, time: 0, now: t0)
     61         let reading = try #require(sample)
     62         #expect(source.surfaceAssumed)
     63         #expect(reading == .pressure(time: 0, surface: standardSurfacePressure, absolute: 2.0, submerged: true))
     64         // Back at the surface, the real pressure replaces the assumption.
     65         _ = source.pressure(99_400, time: 600, now: t0 + 600)
     66         #expect(source.surfacePa == 99_400)
     67         #expect(!source.surfaceAssumed)
     68     }
     69 
     70     @Test func linkLostUnderwaterSaysSubmergedWithoutMeasurement() {
     71         var source = HousingDepthSource()
     72         _ = source.pressure(99_400, time: 0, now: t0)
     73         #expect(source.linkLost(time: 1) == nil)                       // at the surface: nothing to say
     74         _ = source.pressure(99_400 + 1 * perMetre, time: 2, now: t0 + 2)
     75         #expect(source.linkLost(time: 3) == nil)                       // 1 m: out of the water for the watchdog
     76         _ = source.pressure(99_400 + 12 * perMetre, time: 4, now: t0 + 4)
     77         #expect(source.linkLost(time: 5) == .pressure(time: 5, surface: 0.994, absolute: nil, submerged: true))
     78     }
     79 
     80     @Test func basicDepthAndDiveTime() {
     81         var source = HousingDepthSource()
     82         _ = source.pressure(99_400, time: 0, now: t0)
     83         #expect(source.depth == 0)
     84         #expect(source.diveStart == nil)
     85         _ = source.pressure(99_400 + 0.4 * perMetre, time: 1, now: t0 + 1)
     86         #expect(source.diveStart == nil)
     87         _ = source.pressure(99_400 + 6 * perMetre, time: 2, now: t0 + 2)
     88         #expect(abs((source.depth ?? 0) - 6) < 1e-9)
     89         #expect(source.diveStart == t0 + 2)
     90         _ = source.pressure(99_400 + 0.3 * perMetre, time: 3, now: t0 + 3)
     91         #expect(source.diveStart == t0 + 2)                            // hysteresis: still in the dive
     92         _ = source.pressure(99_400, time: 4, now: t0 + 4)
     93         #expect(source.diveStart == nil)
     94     }
     95 }