-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp_test.go
301 lines (276 loc) · 7.37 KB
/
temp_test.go
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
package wx
import "testing"
func TestTempUnitString(t *testing.T) {
tt := []struct {
name string
unit TempUnit
want string
}{
{"celsius", Celsius, "°C"},
{"fahrenheit", Fahrenheit, "°F"},
{"kelvin", Kelvin, "K"},
{"rankine", Rankine, "R"},
{"invalid", TempUnit{-1}, ""},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if tc.unit.String() != tc.want {
t.Errorf("got %v, want %v", tc.unit.String(), tc.want)
}
})
}
}
func TestNewTemp(t *testing.T) {
tt := []struct {
name string
measurement float64
unit TempUnit
valid bool
}{
{"valid celsius", 0, Celsius, true},
{"valid fahrenheit", 0, Fahrenheit, true},
{"valid kelvin", 0, Kelvin, true},
{"valid rankine", 0, Rankine, true},
{"invalid celsius", -274, Celsius, false},
{"invalid fahrenheit", -460, Fahrenheit, false},
{"invalid kelvin", -1, Kelvin, false},
{"invalid rankine", -1, Rankine, false},
{"invalid unit", 0, TempUnit{-1}, false},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
temp := NewTemp(tc.measurement, tc.unit)
if temp.valid != tc.valid {
t.Errorf("got %v, want %v", temp.valid, tc.valid)
}
})
}
}
func TestTempString(t *testing.T) {
tt := []struct {
name string
measurement float64
unit TempUnit
want string
}{
{"celsius", 0, Celsius, "0.0°C"},
{"fahrenheit", 0, Fahrenheit, "0.0°F"},
{"kelvin", 0, Kelvin, "0.0 K"},
{"rankine", 0, Rankine, "0.0 R"},
{"invalid", 0, TempUnit{-1}, "invalid temp unit"},
{"invalid celsius", -274, Celsius, "invalid °C"},
{"invalid fahrenheit", -460, Fahrenheit, "invalid °F"},
{"invalid kelvin", -1, Kelvin, "invalid K"},
{"invalid rankine", -1, Rankine, "invalid R"},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
temp := NewTemp(tc.measurement, tc.unit)
if temp.String() != tc.want {
t.Errorf("got %v, want %v", temp.String(), tc.want)
}
})
}
}
func TestTempValid(t *testing.T) {
tt := []struct {
name string
measurement float64
unit TempUnit
valid bool
}{
{"valid celsius", freezingC, Celsius, true},
{"valid fahrenheit", freezingF, Fahrenheit, true},
{"valid kelvin", absoluteZeroK, Kelvin, true},
{"valid rankine", absoluteZeroR, Rankine, true},
{"invalid celsius", -274, Celsius, false},
{"invalid fahrenheit", -460, Fahrenheit, false},
{"invalid kelvin", -1, Kelvin, false},
{"invalid rankine", -1, Rankine, false},
{"invalid unit", 0, TempUnit{-1}, false},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
temp := NewTemp(tc.measurement, tc.unit)
if temp.Valid() != tc.valid {
t.Errorf("got %v, want %v", temp.Valid(), tc.valid)
}
})
}
}
func TestTempC(t *testing.T) {
tt := []struct {
name string
measurement float64
unit TempUnit
want float64
}{
{"celsius", freezingC, Celsius, freezingC},
{"fahrenheit", freezingF, Fahrenheit, freezingC},
{"kelvin", 0, Kelvin, absoluteZeroC},
{"rankine", 0, Rankine, absoluteZeroC},
{"invalid", 0, TempUnit{-1}, 0},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
temp := NewTemp(tc.measurement, tc.unit)
if temp.C() != tc.want {
t.Errorf("got %v, want %v", temp.C(), tc.want)
}
})
}
}
func TestTempToC(t *testing.T) {
tt := []struct {
name string
measurement float64
unit TempUnit
want float64
}{
{"celsius", freezingC, Celsius, freezingC},
{"fahrenheit", freezingF, Fahrenheit, freezingC},
{"kelvin", absoluteZeroK, Kelvin, absoluteZeroC},
{"rankine", absoluteZeroR, Rankine, absoluteZeroC},
{"invalid", 0, TempUnit{-1}, 0},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
temp := NewTemp(tc.measurement, tc.unit)
if temp.ToC().C() != tc.want {
t.Errorf("got %v, want %v", temp.ToC().C(), tc.want)
}
})
}
}
func TestTempF(t *testing.T) {
tt := []struct {
name string
measurement float64
unit TempUnit
want float64
}{
{"celsius", freezingC, Celsius, freezingF},
{"fahrenheit", freezingF, Fahrenheit, freezingF},
{"kelvin", absoluteZeroK, Kelvin, absoluteZeroF},
{"rankine", absoluteZeroR, Rankine, absoluteZeroF},
{"invalid", 0, TempUnit{-1}, 0},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
temp := NewTemp(tc.measurement, tc.unit)
if temp.F() != tc.want {
t.Errorf("got %v, want %v", temp.F(), tc.want)
}
})
}
}
func TestTempToF(t *testing.T) {
tt := []struct {
name string
measurement float64
unit TempUnit
want float64
}{
{"celsius", freezingC, Celsius, freezingF},
{"fahrenheit", freezingF, Fahrenheit, freezingF},
{"kelvin", absoluteZeroK, Kelvin, absoluteZeroF},
{"rankine", absoluteZeroR, Rankine, absoluteZeroF},
{"invalid", 0, TempUnit{-1}, 0},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
temp := NewTemp(tc.measurement, tc.unit)
if temp.ToF().F() != tc.want {
t.Errorf("got %v, want %v", temp.ToF().F(), tc.want)
}
})
}
}
func TestTempK(t *testing.T) {
tt := []struct {
name string
measurement float64
unit TempUnit
want float64
}{
{"celsius", absoluteZeroC, Celsius, absoluteZeroK},
{"fahrenheit", absoluteZeroF, Fahrenheit, absoluteZeroK},
{"kelvin", absoluteZeroK, Kelvin, absoluteZeroK},
{"rankine", absoluteZeroR, Rankine, absoluteZeroK},
{"invalid", 0, TempUnit{-1}, 0},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
temp := NewTemp(tc.measurement, tc.unit)
if temp.K() != tc.want {
t.Errorf("got %v, want %v", temp.K(), tc.want)
}
})
}
}
func TestTempToK(t *testing.T) {
tt := []struct {
name string
measurement float64
unit TempUnit
want float64
}{
{"celsius", absoluteZeroC, Celsius, absoluteZeroK},
{"fahrenheit", absoluteZeroF, Fahrenheit, absoluteZeroK},
{"kelvin", absoluteZeroK, Kelvin, absoluteZeroK},
{"rankine", absoluteZeroR, Rankine, absoluteZeroK},
{"invalid", 0, TempUnit{-1}, 0},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
temp := NewTemp(tc.measurement, tc.unit)
if temp.ToK().K() != tc.want {
t.Errorf("got %v, want %v", temp.ToK().K(), tc.want)
}
})
}
}
func TestTempR(t *testing.T) {
tt := []struct {
name string
measurement float64
unit TempUnit
want float64
}{
{"celsius", absoluteZeroC, Celsius, absoluteZeroR},
{"fahrenheit", absoluteZeroF, Fahrenheit, absoluteZeroR},
{"kelvin", absoluteZeroK, Kelvin, absoluteZeroR},
{"rankine", absoluteZeroR, Rankine, absoluteZeroR},
{"invalid", 0, TempUnit{-1}, 0},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
temp := NewTemp(tc.measurement, tc.unit)
if temp.R() != tc.want {
t.Errorf("got %v, want %v", temp.R(), tc.want)
}
})
}
}
func TestTempToR(t *testing.T) {
tt := []struct {
name string
measurement float64
unit TempUnit
want float64
}{
{"celsius", absoluteZeroC, Celsius, absoluteZeroR},
{"fahrenheit", absoluteZeroF, Fahrenheit, absoluteZeroR},
{"kelvin", absoluteZeroK, Kelvin, absoluteZeroR},
{"rankine", absoluteZeroR, Rankine, absoluteZeroR},
{"invalid", 0, TempUnit{-1}, 0},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
temp := NewTemp(tc.measurement, tc.unit)
if temp.ToR().R() != tc.want {
t.Errorf("got %v, want %v", temp.ToR().R(), tc.want)
}
})
}
}