-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathColorTemperatureRowMathTests.swift
More file actions
115 lines (98 loc) · 3.86 KB
/
ColorTemperatureRowMathTests.swift
File metadata and controls
115 lines (98 loc) · 3.86 KB
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
// Copyright (c) 2010-2026 Contributors to the openHAB project
//
// See the NOTICE file(s) distributed with this work for additional
// information.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0
//
// SPDX-License-Identifier: EPL-2.0
@testable import openHAB
import Testing
private enum TestValues {
static let oversizedKelvin = 15_000.0
static let miredWarmest = 370.0
static let miredCoolest = 153.0
static let convertedWarmestKelvin = 2702.7027027027025
static let convertedCoolestKelvin = 6535.947712418301
static let miredState = "250"
static let convertedStateKelvin = 4_000.0
static let invalidState = "not-a-number"
static let clampedServerValue = 6_500.0
static let oversizedServerValue = 9_000.0
static let minimumRangeKelvin = 1_000.0
static let narrowMaximumKelvin = 6_500.0
static let degenerateRangeKelvin = 4_000.0
static let gradientStartKelvin = 2_000.0
static let gradientEndKelvin = 3_000.0
static let gradientSteps = 4
static let gradientCount = 5
static let tolerance = 0.0001
static let zero = 0.0
}
@Suite
struct ColorTemperatureRowMathTests {
@Test
func normalizedRangeUsesFallbackForZeroSpan() {
let range = ColorTemperatureRowMath.normalizedRange(
minValue: TestValues.oversizedKelvin,
maxValue: TestValues.oversizedKelvin
)
#expect(range.lowerBound == ColorTemperatureRowMath.minimumKelvin)
#expect(range.upperBound == ColorTemperatureRowMath.maximumKelvin)
}
@Test
func normalizedRangeConvertsMiredBoundsToKelvin() {
let range = ColorTemperatureRowMath.normalizedRange(
minValue: TestValues.miredWarmest,
maxValue: TestValues.miredCoolest
)
#expect(abs(range.lowerBound - TestValues.convertedWarmestKelvin) < TestValues.tolerance)
#expect(abs(range.upperBound - TestValues.convertedCoolestKelvin) < TestValues.tolerance)
}
@Test
func normalizedTemperatureConvertsMiredStateAndClampsIntoRange() {
let range = ColorTemperatureRowMath.normalizedRange(
minValue: TestValues.miredWarmest,
maxValue: TestValues.miredCoolest
)
let temperature = ColorTemperatureRowMath.normalizedTemperature(
state: TestValues.miredState,
serverValue: nil,
range: range
)
#expect(temperature == TestValues.convertedStateKelvin)
}
@Test
func normalizedTemperatureFallsBackToClampedServerValue() {
let range = ColorTemperatureRowMath.normalizedRange(
minValue: TestValues.minimumRangeKelvin,
maxValue: TestValues.narrowMaximumKelvin
)
let temperature = ColorTemperatureRowMath.normalizedTemperature(
state: TestValues.invalidState,
serverValue: TestValues.oversizedServerValue,
range: range
)
#expect(temperature == TestValues.clampedServerValue)
}
@Test
func sliderFractionReturnsZeroForDegenerateRange() {
let fraction = ColorTemperatureRowMath.sliderFraction(
value: TestValues.gradientEndKelvin,
range: TestValues.degenerateRangeKelvin ... TestValues.degenerateRangeKelvin
)
#expect(fraction == TestValues.zero)
}
@Test
func gradientTemperaturesAlwaysIncludeBothBounds() {
let temperatures = ColorTemperatureRowMath.gradientTemperatures(
for: TestValues.gradientStartKelvin ... TestValues.gradientEndKelvin,
steps: TestValues.gradientSteps
)
#expect(temperatures.count == TestValues.gradientCount)
#expect(temperatures.first == TestValues.gradientStartKelvin)
#expect(temperatures.last == TestValues.gradientEndKelvin)
}
}