-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgpio_linux_test.go
More file actions
194 lines (174 loc) · 4.63 KB
/
gpio_linux_test.go
File metadata and controls
194 lines (174 loc) · 4.63 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
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
package gpio_test
import (
"flag"
"os"
"path/filepath"
"testing"
. "github.com/mkch/asserting"
"github.com/mkch/gpio"
)
// The GPIO chip device to use in tests.
var chipDev string
var inputLine uint
var outputLine uint
func TestMain(m *testing.M) {
flag.StringVar(&chipDev, "chip", "gpiochip0", "GPIO chip device used in tests, e.g. gpiochip0")
flag.UintVar(&inputLine, "in-line", 2, "The offset of input GPIO line on chip used in tests")
flag.UintVar(&outputLine, "out-line", 2, "The offset of output GPIO line on chip used in tests")
flag.Parse()
os.Exit(m.Run())
}
func TestOpenChip(t1 *testing.T) {
t := NewTB(t1)
chip, err := gpio.OpenChip(chipDev)
t.Assert(ValueErrorFatal(chip, err), NotEquals(nil).SetFatal())
t.AssertNoError(chip.Close())
}
func TestChipInfo(t1 *testing.T) {
t := NewTB(t1)
chip, err := gpio.OpenChip(chipDev)
t.Assert(ValueErrorFatal(chip, err), NotEquals(nil).SetFatal())
defer func() { t.AssertNoError(chip.Close()) }()
info, err := chip.Info()
t.Assert(ValueError(info, err), Matches(func(v interface{}) bool {
return v.(gpio.ChipInfo).Name == filepath.Base(chipDev)
}))
}
func TestLineInfo(t1 *testing.T) {
t := NewTB(t1)
chip, err := gpio.OpenChip(chipDev)
t.Assert(ValueErrorFatal(chip, err), NotEquals(nil).SetFatal())
defer func() { t.AssertNoError(chip.Close()) }()
t.Assert(ValueError(chip.LineInfo(uint32(inputLine))), NotEquals(nil).SetFatal())
}
func TestOpenInputLine(t1 *testing.T) {
t := NewTB(t1)
chip, err := gpio.OpenChip(chipDev)
t.Assert(ValueErrorFatal(chip, err), NotEquals(nil).SetFatal())
defer func() { t.AssertNoError(chip.Close()) }()
const consumer = "123456789abcdefghijklmnopqrstuv"
t.Assert(len(consumer), Equals(31).SetFatal())
type args struct {
flags gpio.LineFlag
consumer string
}
type wantInfo struct {
consumer string
activeLow bool
}
type testCase struct {
name string
args args
wantInfo wantInfo
}
tests := []testCase{
testCase{
name: "consumer",
args: args{
flags: gpio.Input,
consumer: consumer,
},
wantInfo: wantInfo{
consumer: consumer,
},
},
testCase{
name: "active-low",
args: args{
flags: gpio.Input | gpio.ActiveLow,
consumer: consumer,
},
wantInfo: wantInfo{
consumer: consumer,
activeLow: true,
},
},
}
for _, tt := range tests {
t1.Run(tt.name, func(t1 *testing.T) {
t := NewTB(t1)
line, err := chip.OpenLine(uint32(inputLine), 0, tt.args.flags, tt.args.consumer)
t.Assert(ValueError(line, err), NotEquals(nil).SetFatal())
gotInfo, err := chip.LineInfo(uint32(inputLine))
t.AssertNoError(err)
t.AssertEqual(gotInfo.Output(), false)
t.AssertEqual(gotInfo.Consumer, tt.wantInfo.consumer)
t.AssertEqual(gotInfo.ActiveLow(), tt.wantInfo.activeLow)
t.AssertEqual(gotInfo.OpenDrain(), false)
t.AssertEqual(gotInfo.OpenSource(), false)
t.AssertNoError(line.Close())
})
}
}
func TestOpenOutputLine(t1 *testing.T) {
t := NewTB(t1)
chip, err := gpio.OpenChip(chipDev)
t.Assert(ValueErrorFatal(chip, err), NotEquals(nil).SetFatal())
defer func() { t.AssertNoError(chip.Close()) }()
const consumer = "a"
type args struct {
flags gpio.LineFlag
consumer string
}
type wantInfo struct {
consumer string
activeLow bool
openDrain bool
openSource bool
}
type testCase struct {
name string
args args
wantInfo wantInfo
}
tests := []testCase{
testCase{
name: "consumer",
args: args{
flags: gpio.Output,
consumer: consumer,
},
wantInfo: wantInfo{
consumer: consumer,
},
},
testCase{
name: "active-low",
args: args{
consumer: consumer,
flags: gpio.Output | gpio.ActiveLow,
},
wantInfo: wantInfo{
consumer: consumer,
activeLow: true,
},
},
testCase{
name: "empty-consumer",
args: args{
flags: gpio.Output,
},
wantInfo: wantInfo{
// The kernel return ? if the line is occupied but without a consumer abel.
// This behavior is not documented.
consumer: "?",
},
},
}
for _, tt := range tests {
t1.Run(tt.name, func(t1 *testing.T) {
t := NewTB(t1)
line, err := chip.OpenLine(uint32(inputLine), 0, tt.args.flags, tt.args.consumer)
t.Assert(ValueError(line, err), NotEquals(nil).SetFatal())
t.AssertNoError(line.SetValue(1))
gotInfo, err := chip.LineInfo(uint32(inputLine))
t.AssertNoError(err)
t.AssertEqual(gotInfo.Output(), true)
t.AssertEqual(gotInfo.Consumer, tt.wantInfo.consumer)
t.AssertEqual(gotInfo.ActiveLow(), tt.wantInfo.activeLow)
t.AssertEqual(gotInfo.OpenDrain(), tt.wantInfo.openDrain)
t.AssertEqual(gotInfo.OpenSource(), tt.wantInfo.openSource)
t.AssertNoError(line.Close())
})
}
}