forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l3gd20h_driver_test.go
137 lines (112 loc) · 3.6 KB
/
l3gd20h_driver_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
package i2c
import (
"bytes"
"encoding/binary"
"errors"
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*HMC6352Driver)(nil)
// --------- HELPERS
func initTestL3GD20HDriver() (driver *L3GD20HDriver) {
driver, _ = initTestL3GD20HDriverWithStubbedAdaptor()
return
}
func initTestL3GD20HDriverWithStubbedAdaptor() (*L3GD20HDriver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor()
return NewL3GD20HDriver(adaptor), adaptor
}
// --------- TESTS
func TestNewL3GD20HDriver(t *testing.T) {
// Does it return a pointer to an instance of HMC6352Driver?
var d interface{} = NewL3GD20HDriver(newI2cTestAdaptor())
_, ok := d.(*L3GD20HDriver)
if !ok {
t.Errorf("NewL3GD20HDriver() should have returned a *L3GD20HDriver")
}
}
func TestL3GD20HDriver(t *testing.T) {
d := initTestL3GD20HDriver()
gobottest.Refute(t, d.Connection(), nil)
}
// Methods
func TestL3GD20HDriverStart(t *testing.T) {
d, _ := initTestL3GD20HDriverWithStubbedAdaptor()
gobottest.Assert(t, d.Start(), nil)
}
func TestL3GD20HStartConnectError(t *testing.T) {
d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestL3GD20HDriverStartWriteError(t *testing.T) {
d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
gobottest.Assert(t, d.Start(), errors.New("write error"))
}
func TestL3GD20HDriverHalt(t *testing.T) {
d := initTestL3GD20HDriver()
gobottest.Assert(t, d.Halt(), nil)
}
func TestL3GD20HDriverScale(t *testing.T) {
d := initTestL3GD20HDriver()
gobottest.Assert(t, d.Scale(), L3GD20HScale250dps)
gobottest.Assert(t, d.getSensitivity(), float32(0.00875))
d.SetScale(L3GD20HScale500dps)
gobottest.Assert(t, d.Scale(), L3GD20HScale500dps)
gobottest.Assert(t, d.getSensitivity(), float32(0.0175))
d.SetScale(L3GD20HScale2000dps)
gobottest.Assert(t, d.Scale(), L3GD20HScale2000dps)
gobottest.Assert(t, d.getSensitivity(), float32(0.07))
}
func TestL3GD20HDriverMeasurement(t *testing.T) {
d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
rawX := 5
rawY := 8
rawZ := -3
adaptor.i2cReadImpl = func(b []byte) (int, error) {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, int16(rawX))
binary.Write(buf, binary.LittleEndian, int16(rawY))
binary.Write(buf, binary.LittleEndian, int16(rawZ))
copy(b, buf.Bytes())
return buf.Len(), nil
}
d.Start()
x, y, z, err := d.XYZ()
gobottest.Assert(t, err, nil)
var sensitivity float32 = 0.00875
gobottest.Assert(t, x, float32(rawX)*sensitivity)
gobottest.Assert(t, y, float32(rawY)*sensitivity)
gobottest.Assert(t, z, float32(rawZ)*sensitivity)
}
func TestL3GD20HDriverMeasurementError(t *testing.T) {
d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
adaptor.i2cReadImpl = func(b []byte) (int, error) {
return 0, errors.New("read error")
}
d.Start()
_, _, _, err := d.XYZ()
gobottest.Assert(t, err, errors.New("read error"))
}
func TestL3GD20HDriverMeasurementWriteError(t *testing.T) {
d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
d.Start()
adaptor.i2cWriteImpl = func(b []byte) (int, error) {
return 0, errors.New("write error")
}
_, _, _, err := d.XYZ()
gobottest.Assert(t, err, errors.New("write error"))
}
func TestL3GD20HDriverSetName(t *testing.T) {
d := initTestL3GD20HDriver()
d.SetName("TESTME")
gobottest.Assert(t, d.Name(), "TESTME")
}
func TestL3GD20HDriverOptions(t *testing.T) {
d := NewL3GD20HDriver(newI2cTestAdaptor(), WithBus(2))
gobottest.Assert(t, d.GetBusOrDefault(1), 2)
}