mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-02 22:17:49 +00:00
6763521eff
This type should be used whenever a sensor (or actuator?) works with a
temperature. For example, this commit changes the signature:
ReadTemperature() (int32, error)
to the following:
ReadTemperature() (drivers.Temperature, error)
I believe this is much clearer in intent. It also makes it trivial to
introduce common conversions. For example, there are already Celsius()
and Fahrenheit() methods to convert to the given units, as a floating
point. More units could be added as needed, for example a CelsiusInt().
27 lines
561 B
Go
27 lines
561 B
Go
package drivers
|
|
|
|
import "testing"
|
|
|
|
func TestTemperature(t *testing.T) {
|
|
tests := []struct {
|
|
t Temperature
|
|
c float32 // Celsius
|
|
f float32 // Fahrenheit
|
|
}{
|
|
{-40000, -40, -40}, // -40°C
|
|
{0, 0, 32}, // 0°C
|
|
{20000, 20, 68}, // 20°C
|
|
{25000, 25, 77}, // 25°C
|
|
}
|
|
for _, tc := range tests {
|
|
c := tc.t.Celsius()
|
|
f := tc.t.Fahrenheit()
|
|
if c != tc.c {
|
|
t.Errorf("expected value %d to be %f°C, but got %f°C", tc.t, tc.c, c)
|
|
}
|
|
if f != tc.f {
|
|
t.Errorf("expected value %d to be %f°F, but got %f°F", tc.t, tc.f, f)
|
|
}
|
|
}
|
|
}
|