Skip to content

Commit 6bf3b91

Browse files
committed
Unit linux test fix
1 parent d433579 commit 6bf3b91

1 file changed

Lines changed: 28 additions & 21 deletions

File tree

Sources/LoopAlgorithmToPython/LoopAlgorithmToPython.swift

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,32 @@ private func signalHandler(signal: Int32) {
2323
}
2424

2525
#if os(Linux) || os(Windows)
26-
// Non-Apple platforms use raw Double
27-
public typealias AlgorithmValue = Double
28-
#else
29-
// Apple platforms use LoopQuantity
30-
public typealias AlgorithmValue = LoopQuantity
31-
#endif
26+
// If the Linux library expects LoopQuantity but doesn't provide it,
27+
// we define a compatible version here.
28+
public struct LoopQuantity: Codable {
29+
public let unit: LoopUnit
30+
public let doubleValue: Double
31+
32+
public init(unit: LoopUnit, doubleValue: Double) {
33+
self.unit = unit
34+
self.doubleValue = doubleValue
35+
}
3236

33-
extension AbsoluteScheduleValue where T == AlgorithmValue {
34-
static func universal(startDate: Date, endDate: Date, value: Double, unit: String) -> AbsoluteScheduleValue<AlgorithmValue> {
35-
#if os(Linux) || os(Windows)
36-
return AbsoluteScheduleValue(startDate: startDate, endDate: endDate, value: value)
37-
#else
38-
return AbsoluteScheduleValue(startDate: startDate, endDate: endDate, value: LoopQuantity(unit: LoopUnit(from: unit), doubleValue: value))
39-
#endif
37+
public func doubleValue(for unit: LoopUnit) -> Double {
38+
return doubleValue
4039
}
4140
}
4241

42+
public struct LoopUnit: Codable {
43+
public let unitString: String
44+
public init(from string: String) { self.unitString = string }
45+
public static let gram = LoopUnit(from: "g")
46+
}
47+
#endif
48+
49+
// This makes AlgorithmValue always LoopQuantity on ALL platforms
50+
public typealias AlgorithmValue = LoopQuantity
51+
4352
@_cdecl("initializeExceptionHandler")
4453
public func initializeExceptionHandler() {
4554
#if os(macOS) || os(iOS)
@@ -438,27 +447,25 @@ public func getDynamicCarbsOnBoard(jsonData: UnsafePointer<Int8>?) -> Double {
438447
let endDate = dateFormatter.date(from: input.inputICE.last!.startAt)!
439448

440449
// This uses the compatibility layer to work on all platforms
441-
let carbRatio = [AbsoluteScheduleValue<AlgorithmValue>.universal(
450+
let carbRatio = [AbsoluteScheduleValue(
442451
startDate: startDate,
443452
endDate: endDate,
444-
value: input.carbRatio,
445-
unit: "g/U"
453+
value: LoopQuantity(unit: LoopUnit(from: "g/U"), doubleValue: input.carbRatio)
446454
)]
447455

448-
let isf = [AbsoluteScheduleValue<AlgorithmValue>.universal(
456+
let isf = [AbsoluteScheduleValue(
449457
startDate: startDate,
450458
endDate: endDate,
451-
value: input.sensitivity,
452-
unit: "mg/dL/U"
459+
value: LoopQuantity(unit: LoopUnit(from: "mg/dL/U"), doubleValue: input.sensitivity)
453460
)]
461+
454462
let statuses = [carbEntries[0]].map(
455463
to: inputICE,
456464
carbRatio: carbRatio,
457465
insulinSensitivity: isf,
458466
initialAbsorptionTimeOverrun: 2.0,
459467
absorptionModel: PiecewiseLinearAbsorption()
460-
)
461-
// TODO: Add a function for different absorbrionmodels
468+
) // TODO: Add a function for different absorbrionmodels
462469

463470
// The output here is a list of CarbValues with startDate, endDate (equal to startDate), and value (ICE?)
464471
let carbsOnBoard = statuses.dynamicCarbsOnBoard(

0 commit comments

Comments
 (0)