forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuardrailTests.swift
304 lines (269 loc) · 17.8 KB
/
GuardrailTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// GuardrailTests.swift
// GuardrailTests
//
// Created by Michael Pangburn on 7/30/20.
// Copyright © 2020 LoopKit Authors. All rights reserved.
//
import XCTest
import HealthKit
@testable import LoopKit
class GuardrailTests: XCTestCase {
let correctionRangeSchedule120 = GlucoseRangeSchedule(unit: .milligramsPerDeciliter, dailyItems: [RepeatingScheduleValue(startTime: 0, value: DoubleRange(120...130))])
let preMealTargetRange120 = DoubleRange(120...130).quantityRange(for: .milligramsPerDeciliter)
let workoutTargetRange120 = DoubleRange(120...130).quantityRange(for: .milligramsPerDeciliter)
let correctionRangeSchedule80 = GlucoseRangeSchedule(unit: .milligramsPerDeciliter, dailyItems: [RepeatingScheduleValue(startTime: 0, value: DoubleRange(80...100))])
let preMealTargetRange85 = DoubleRange(85...100).quantityRange(for: .milligramsPerDeciliter)
let workoutTargetRange90 = DoubleRange(90...100).quantityRange(for: .milligramsPerDeciliter)
func testSuspendThresholdUnits() {
XCTAssertTrue(Guardrail.suspendThreshold.absoluteBounds.contains(HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 67)))
XCTAssertTrue(Guardrail.suspendThreshold.absoluteBounds.contains(HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 110)))
XCTAssertTrue(Guardrail.suspendThreshold.absoluteBounds.contains(HKQuantity(unit: .millimolesPerLiter, doubleValue: 6.1)))
XCTAssertTrue(Guardrail.suspendThreshold.recommendedBounds.contains(HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 74)))
XCTAssertTrue(Guardrail.suspendThreshold.recommendedBounds.contains(HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 80)))
XCTAssertTrue(Guardrail.suspendThreshold.absoluteBounds.contains(HKQuantity(unit: .millimolesPerLiter, doubleValue: 4.44)))
}
func testMaxSuspensionThresholdValue() {
let correctionRangeInputs = [ nil, correctionRangeSchedule120, correctionRangeSchedule80 ]
let preMealInputs = [ nil, preMealTargetRange120, preMealTargetRange85 ]
let workoutInputs = [ nil, workoutTargetRange120, workoutTargetRange90 ]
let expected: [Double] = [ 110.9, 110.9, 90,
110.9, 110.9, 90,
85, 85, 85,
110.9, 110.9, 90,
110.9, 110.9, 90,
85, 85, 85,
80, 80, 80,
80, 80, 80,
80, 80, 80 ]
var index = 0
for correctionRange in correctionRangeInputs {
for preMeal in preMealInputs {
for workout in workoutInputs {
let maxSuspendThresholdValue = Guardrail.maxSuspendThresholdValue(correctionRangeSchedule: correctionRange, preMealTargetRange: preMeal, workoutTargetRange: workout).doubleValue(for: .milligramsPerDeciliter)
XCTAssertEqual(expected[index], maxSuspendThresholdValue, "Index \(index) failed")
index += 1
}
}
}
}
func testMinCorrectionRangeValue() {
let suspendThresholdInputs: [Double?] = [ nil, 80, 88 ]
let expected: [Double] = [ 86.1, 86.1, 88 ]
for (index, suspendThreshold) in suspendThresholdInputs.enumerated() {
XCTAssertEqual(expected[index], Guardrail.minCorrectionRangeValue(suspendThreshold: suspendThreshold.map { GlucoseThreshold(unit: .milligramsPerDeciliter, value: $0) }).doubleValue(for: .milligramsPerDeciliter), "Index \(index) failed")
}
}
func testCorrectionRange() {
let guardrail = Guardrail.correctionRange
let expectedAndTest: [(SafetyClassification, Double)] = [
(SafetyClassification.withinRecommendedRange, 100),
(SafetyClassification.withinRecommendedRange, 115),
(SafetyClassification.outsideRecommendedRange(.belowRecommended), 99),
(SafetyClassification.outsideRecommendedRange(.aboveRecommended), 116),
(SafetyClassification.outsideRecommendedRange(.maximum), 180.9),
(SafetyClassification.outsideRecommendedRange(.minimum), 86.1),
]
for test in expectedAndTest {
XCTAssertEqual(test.0, guardrail.classification(for: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: test.1)), "for \(test.1)")
}
}
func testWorkoutCorrectionRange() {
let correctionRangeInputs = [ 70...80, 70...85, 70...90 ]
let suspendThresholdInputs: [Double?] = [ nil, 81, 91 ]
let expectedLow: [Double] = [ 86.1, 86.1, 91,
86.1, 86.1, 91,
90, 90, 91 ]
let expectedMin: [Double] = [ 86.1, 86.1, 91, 86.1, 86.1, 91, 86.1, 86.1, 91 ]
var index = 0
for correctionRange in correctionRangeInputs {
for suspendThreshold in suspendThresholdInputs {
let guardrail = Guardrail.correctionRangeOverride(for: .workout, correctionRangeScheduleRange: correctionRange.range(withUnit: .milligramsPerDeciliter), suspendThreshold: suspendThreshold.map { GlucoseThreshold(unit: .milligramsPerDeciliter, value: $0) })
XCTAssertEqual(expectedLow[index], guardrail.recommendedBounds.lowerBound.doubleValue(for: .milligramsPerDeciliter), "Index \(index) failed")
XCTAssertEqual(expectedMin[index], guardrail.absoluteBounds.lowerBound.doubleValue(for: .milligramsPerDeciliter), "Index \(index) failed")
index += 1
}
}
}
func testPreMealCorrectionRange() {
let correctionRangeInputs = [ 60...80, 100...110, 150...180 ]
let suspendThresholdInputs: [Double?] = [ nil, 90 ]
let expectedRecommendedHigh: [Double] = [ 66.1, 90,
100, 100,
130, 130 ]
let expectedMin: [Double] = [ 66.1, 90, 66.1, 90, 66.1, 90 ]
var index = 0
for correctionRange in correctionRangeInputs {
for suspendThreshold in suspendThresholdInputs {
let guardrail = Guardrail.correctionRangeOverride(for: .preMeal, correctionRangeScheduleRange: correctionRange.range(withUnit: .milligramsPerDeciliter), suspendThreshold: suspendThreshold.map { GlucoseThreshold(unit: .milligramsPerDeciliter, value: $0) })
XCTAssertEqual(expectedRecommendedHigh[index], guardrail.recommendedBounds.upperBound.doubleValue(for: .milligramsPerDeciliter), "Index \(index) failed")
XCTAssertEqual(expectedMin[index], guardrail.absoluteBounds.lowerBound.doubleValue(for: .milligramsPerDeciliter), "Index \(index) failed")
XCTAssertEqual(guardrail.absoluteBounds.lowerBound.doubleValue(for: .milligramsPerDeciliter),
guardrail.recommendedBounds.lowerBound.doubleValue(for: .milligramsPerDeciliter), "Index \(index) failed")
index += 1
}
}
}
func testCarbRatioGuardrail() {
XCTAssertEqual(2.0...150.0, Guardrail.carbRatio.absoluteBounds.range(withUnit: .gramsPerUnit))
XCTAssertEqual(4...28, Guardrail.carbRatio.recommendedBounds.range(withUnit: .gramsPerUnit))
}
func testBasalRateGuardrail() {
let supportedBasalRates = (2...600).map { Double($0) / 20 }
let guardrail = Guardrail.basalRate(supportedBasalRates: supportedBasalRates)
XCTAssertEqual(0.1...30.0, guardrail.absoluteBounds.range(withUnit: .internationalUnitsPerHour))
XCTAssertEqual(0.1...30.0, guardrail.recommendedBounds.range(withUnit: .internationalUnitsPerHour))
}
func testBasalRateGuardrailClampedLow() {
let supportedBasalRates = [-1, 0.01, 1.0, 30.0]
let guardrail = Guardrail.basalRate(supportedBasalRates: supportedBasalRates)
XCTAssertEqual(0.01...30.0, guardrail.absoluteBounds.range(withUnit: .internationalUnitsPerHour))
XCTAssertEqual(0.01...30.0, guardrail.recommendedBounds.range(withUnit: .internationalUnitsPerHour))
}
func testBasalRateGuardrailClampedHigh() {
let supportedBasalRates = (2...800).map { Double($0) / 20 }
let guardrail = Guardrail.basalRate(supportedBasalRates: supportedBasalRates)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnitsPerHour), 0.1...30.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnitsPerHour), 0.1...30.0)
}
func testBasalRateGuardrailZeroDropsFirst() {
let supportedBasalRates = [-1, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
let guardrail = Guardrail.basalRate(supportedBasalRates: supportedBasalRates)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnitsPerHour), 0.0...5.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnitsPerHour), 0.0...5.0)
}
func testMaxBasalRateGuardrail() {
let supportedBasalRates = (1...600).map { Double($0) / 20 }
let scheduledBasalRange = 0.05...0.78125
let lowestCarbRatio = 10.0
let guardrail = Guardrail.maximumBasalRate(supportedBasalRates: supportedBasalRates, scheduledBasalRange: scheduledBasalRange, lowestCarbRatio: lowestCarbRatio)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnitsPerHour), 0.78125...7.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnitsPerHour), 1.6...5.0)
}
func testMaxBasalRateGuardrailHighCarbRatio() {
let supportedBasalRates = (1...600).map { Double($0) / 20 }
let scheduledBasalRange = 0.05...0.78125
let lowestCarbRatio = 150.0
let guardrail = Guardrail.maximumBasalRate(supportedBasalRates: supportedBasalRates, scheduledBasalRange: scheduledBasalRange, lowestCarbRatio: lowestCarbRatio)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnitsPerHour), 0.78125...5.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnitsPerHour), 1.6...5.0)
}
func testMaxBasalRateGuardrailHigherCarbRatioClampsRecommendedBounds() {
let supportedBasalRates = (1...600).map { Double($0) / 20 }
let scheduledBasalRange = 0.05...0.78125
let lowestCarbRatio = 15.0
let guardrail = Guardrail.maximumBasalRate(supportedBasalRates: supportedBasalRates, scheduledBasalRange: scheduledBasalRange, lowestCarbRatio: lowestCarbRatio)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnitsPerHour), 0.78125...5.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnitsPerHour), 1.6...5.0)
}
func testMaxBasalRateGuardrailNoCarbRatio() {
let supportedBasalRates = (1...600).map { Double($0) / 20 }
let scheduledBasalRange = 0.05...0.78125
let guardrail = Guardrail.maximumBasalRate(supportedBasalRates: supportedBasalRates, scheduledBasalRange: scheduledBasalRange, lowestCarbRatio: nil)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnitsPerHour), 0.78125...30.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnitsPerHour), 1.6...5.0)
}
func testMaxBasalRateGuardrailFewSupportedBasalRates() {
let supportedBasalRates = [0.05, 1.0]
let scheduledBasalRange = 0.05...0.78125
let lowestCarbRatio = 10.0
let guardrail = Guardrail.maximumBasalRate(supportedBasalRates: supportedBasalRates, scheduledBasalRange: scheduledBasalRange, lowestCarbRatio: lowestCarbRatio)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnitsPerHour), 0.78125...1.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnitsPerHour), 1.0...1.0)
}
func testMaxBasalRateGuardrailHighestScheduledBasalZero() {
let supportedBasalRates = [0.0, 1.0]
let scheduledBasalRange = 0.0...0.0
let lowestCarbRatio = 10.0
let guardrail = Guardrail.maximumBasalRate(supportedBasalRates: supportedBasalRates, scheduledBasalRange: scheduledBasalRange, lowestCarbRatio: lowestCarbRatio)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnitsPerHour), 0.0...1.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnitsPerHour), 0.0...0.0)
}
func testMaxBasalRateGuardrailNoScheduledBasalRates() {
let supportedBasalRates = [0, 0.05, 1.0]
let lowestCarbRatio = 10.0
let guardrail = Guardrail.maximumBasalRate(supportedBasalRates: supportedBasalRates, scheduledBasalRange: nil, lowestCarbRatio: lowestCarbRatio)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnitsPerHour), 0.05...1.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnitsPerHour), 0.05...1.0)
}
func testSelectableBasalRatesGuardrail() {
let supportedBasalRates = [0, 0.05, 1.0]
let scheduledBasalRange = 0.05...0.78125
let lowestCarbRatio = 10.0
let selectableMaxBasalRates = Guardrail.selectableMaxBasalRates(supportedBasalRates: supportedBasalRates, scheduledBasalRange: scheduledBasalRange, lowestCarbRatio: lowestCarbRatio)
XCTAssertEqual([1.0], selectableMaxBasalRates)
}
func testSelectableBasalRatesGuardrailNoScheduledBasalRates() {
let supportedBasalRates = [0, 0.05, 1.0]
let lowestCarbRatio = 10.0
let selectableMaxBasalRates = Guardrail.selectableMaxBasalRates(supportedBasalRates: supportedBasalRates, scheduledBasalRange: nil, lowestCarbRatio: lowestCarbRatio)
XCTAssertEqual([0.05, 1.0], selectableMaxBasalRates)
}
func testMaxBolusGuardrailInsideLimits() {
let supportedBolusVolumes = [0.05, 1.0, 2.0]
let guardrail = Guardrail.maximumBolus(supportedBolusVolumes: supportedBolusVolumes)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnit()), 0.05...2.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnit()), 1.0...2.0)
}
func testMaxBolusGuardrailClamped() {
let supportedBolusVolumes = [0.05, 1.0, 2.0, 20.0.nextDown, 20.0, 25.0, 30.0, 30.0.nextUp]
let guardrail = Guardrail.maximumBolus(supportedBolusVolumes: supportedBolusVolumes)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnit()), 0.05...30.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnit()), 1.0...20.0.nextDown)
}
func testMaxBolusGuardrailDropsZeroVolume() {
let supportedBolusVolumes = [0.0, 0.05, 1.0, 2.0, 20.0.nextDown, 20.0, 25.0, 30.0, 30.0.nextUp]
let guardrail = Guardrail.maximumBolus(supportedBolusVolumes: supportedBolusVolumes)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnit()), 0.05...30.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnit()), 1.0...20.0.nextDown)
}
func testMaxBolusGuardrailDropsAllZeroVolumes() {
let supportedBolusVolumes = [0.0, 0.0, 0.05, 1.0, 2.0, 20.0.nextDown, 20.0, 25.0, 30.0, 30.0.nextUp]
let guardrail = Guardrail.maximumBolus(supportedBolusVolumes: supportedBolusVolumes)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnit()), 0.05...30.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnit()), 1.0...20.0.nextDown)
}
func testMaxBolusGuardrailDropsNegatives() {
let supportedBolusVolumes = [-2.0, -1.0, 0.05, 1.0, 2.0, 20.0.nextDown, 20.0, 25.0, 30.0, 30.0.nextUp]
let guardrail = Guardrail.maximumBolus(supportedBolusVolumes: supportedBolusVolumes)
XCTAssertEqual(guardrail.absoluteBounds.range(withUnit: .internationalUnit()), 0.05...30.0)
XCTAssertEqual(guardrail.recommendedBounds.range(withUnit: .internationalUnit()), 1.0...20.0.nextDown)
}
func testSelectableBolusVolumes() {
let supportedBolusVolumes = [0.0, 0.05, 1.0, 2.0, 30.nextUp]
let selectableBolusVolumes = Guardrail.selectableBolusVolumes(supportedBolusVolumes: supportedBolusVolumes)
XCTAssertEqual([0.05, 1.0, 2.0], selectableBolusVolumes)
}
func testAllValuesOfQuantity() {
var guardrail = Guardrail.carbRatio
var unit: HKUnit = .gramsPerUnit
var increment = 0.1
var allValues = guardrail.allValues(forUnit: unit)
var expectedValues = Array(stride(
from: guardrail.absoluteBounds.lowerBound.doubleValue(for: unit, withRounding: true),
through: guardrail.absoluteBounds.upperBound.doubleValue(for: unit, withRounding: true),
by: increment
)).map { unit.round(value: $0, fractionDigits: unit.maxFractionDigits) }
XCTAssertEqual(allValues, expectedValues)
guardrail = Guardrail.insulinSensitivity
unit = HKUnit.milligramsPerDeciliter.unitDivided(by: .internationalUnit())
increment = 1
allValues = guardrail.allValues(forUnit: unit)
expectedValues = Array(stride(
from: guardrail.absoluteBounds.lowerBound.doubleValue(for: unit, withRounding: true),
through: guardrail.absoluteBounds.upperBound.doubleValue(for: unit, withRounding: true),
by: increment
)).map { unit.round(value: $0, fractionDigits: unit.maxFractionDigits) }
XCTAssertEqual(allValues, expectedValues)
}
}
fileprivate extension ClosedRange where Bound == HKQuantity {
func range(withUnit unit: HKUnit) -> ClosedRange<Double> {
lowerBound.doubleValue(for: unit)...upperBound.doubleValue(for: unit)
}
}
fileprivate extension ClosedRange where Bound == Int {
func range(withUnit unit: HKUnit) -> ClosedRange<HKQuantity> {
HKQuantity(unit: unit, doubleValue: Double(lowerBound))...HKQuantity(unit: unit, doubleValue: Double(upperBound))
}
}