-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdevice_test.go
399 lines (329 loc) · 9.19 KB
/
device_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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package elmobd
import (
"testing"
)
/*==============================================================================
* Benchmarks
*/
var resultParseOBDResponse *Result
func benchParseOBDResponse(cmd OBDCommand, input []string, b *testing.B) {
var r *Result
for n := 0; n < b.N; n++ {
r, _ = parseOBDResponse(cmd, input)
}
resultParseOBDResponse = r
}
func BenchmarkParseOBDResponse4(b *testing.B) {
benchParseOBDResponse(
NewPartSupported(1),
[]string{"41 00 02 03 04 05"},
b,
)
}
func BenchmarkParseOBDResponse2(b *testing.B) {
benchParseOBDResponse(
NewEngineLoad(),
[]string{"41 04 03"},
b,
)
}
/*==============================================================================
* Tests
*/
func TestToCommand(t *testing.T) {
assertEqual(t, NewEngineLoad().ToCommand(), "01041")
assertEqual(t, NewVehicleSpeed().ToCommand(), "010D1")
assertEqual(t, NewDistSinceDTCClear().ToCommand(), "01311")
}
func TestIsSupported(t *testing.T) {
sc, err := NewSupportedCommands([]uint32{0x0, 0x0, 0x0, 0x0, 0x0})
assert(t, err == nil, "Supported commands was created successfully")
cmd1 := NewPartSupported(1)
assertEqual(t, sc.IsSupported(cmd1), true)
cmd2 := NewEngineLoad()
part, err := sc.GetPart(0)
assert(t, err == nil, "New part supported was created successfully")
part.SetRawValue(0x10000000)
assertEqual(t, sc.IsSupported(cmd2), true)
}
func TestGetPart(t *testing.T) {
sc, err := NewSupportedCommands([]uint32{0x0, 0x0, 0x0, 0x0})
assert(t, err == nil, "Supported commands was created successfully")
part, err := sc.GetPart(0)
assert(t, part != nil && err == nil, "Getting first part is successful")
part, err = sc.GetPart(3)
assert(t, part != nil && err == nil, "Getting last part is successful")
part, err = sc.GetPart(4)
assert(t, part == nil && err != nil, "Getting out of bounds path fails")
}
func TestGetPartByPID(t *testing.T) {
sc, err := NewSupportedCommands([]uint32{0x0, 0x0, 0x0})
assert(t, err == nil, "Supported commands was created successfully")
part, err := sc.GetPartByPID(0x0)
assert(t, part != nil && err == nil, "Getting PID 0x0 is successful")
assert(t, part.Index() == 1, "Part 1 is return for PID 0")
part, err = sc.GetPartByPID(0x20)
assert(t, part != nil && err == nil, "Getting PID 0x20 is successful")
assert(t, part.Index() == 1, "Part 1 is return for PID 0x20")
part, err = sc.GetPartByPID(0x21)
assert(t, part != nil && err == nil, "Getting PID 0x21 is successful")
assert(t, part.Index() == 2, "Part 2 is return for PID 0x21")
part, err = sc.GetPartByPID(0x40)
assert(t, part != nil && err == nil, "Getting PID 0x30 is successful")
assert(t, part.Index() == 2, "Part 2 is return for PID 0x40")
part, err = sc.GetPartByPID(0x41)
assert(t, part != nil && err == nil, "Getting PID 0x41 is successful")
assert(t, part.Index() == 3, "Part 3 is return for PID 0x41")
part, err = sc.GetPartByPID(0x60)
assert(t, part != nil && err == nil, "Getting PID 0x60 is successful")
assert(t, part.Index() == 3, "Part 3 is return for PID 0x60")
part, err = sc.GetPartByPID(0x61)
assert(t, part == nil && err != nil, "Getting PID 0x61 fails, since part 4 doesn't exist")
}
type DummyCommand struct {
baseCommand
}
func (cmd *DummyCommand) ValueAsLit() string {
return "0x0"
}
func (cmd *DummyCommand) SetValue(result *Result) error {
return nil
}
// TestIsSupportedWikipediaExample checks that the IsSupported function returns
// the correct results when using the example on the wikipedia-page about
// decoding Service 01 PID 00:
//
// > For example, if the car response is BE1FA813, it can be decoded like this:
// > So, supported PIDs are: 01, 03, 04, 05, 06, 07, 0C, 0D, 0E, 0F, 10, 11, 13,
// > 15, 1C, 1F and 20
//
// Source: https://en.wikipedia.org/wiki/OBD-II_PIDs#Service_01_PID_00
func TestIsSupportedWikipediaExample(t *testing.T) {
sc, err := NewSupportedCommands([]uint32{0xBE1FA813, 0x0, 0x0, 0x0, 0x0})
assert(t, err == nil, "Supported commands was created successfully")
supported := []OBDParameterID{
0x01, 0x03, 0x04, 0x05, 0x06, 0x07, 0x0D, 0x0E,
0x0F, 0x10, 0x11, 0x13, 0x15, 0x1C, 0x1F, 0x20,
}
unsupported := []OBDParameterID{
0x02, 0x08, 0x09, 0x0A, 0x0B, 0x12, 0x14, 0x16,
0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1D, 0x1E,
}
for _, pid := range supported {
cmd := &DummyCommand{
baseCommand{SERVICE_01_ID, pid, 1, "dummy"},
}
assertEqual(t, sc.IsSupported(cmd), true)
}
for _, pid := range unsupported {
cmd := &DummyCommand{
baseCommand{SERVICE_01_ID, pid, 1, "dummy"},
}
assertEqual(t, sc.IsSupported(cmd), false)
}
}
// TestIssue27Regression verifies that the IsSupported function returns the correct
// results when checking PIDs outside of part 1.
//
// The test simulates that we have received the result of 5 parts, where
// each part has the first and last bit active, and all other bits inactive.
//
// This means that the first PID of the part should be supported, as well as
// the last PID, which is the one you use to check which PIDs of
// the next part are supported.
//
// Source: https://github.com/rzetterberg/elmobd/issues/27
func TestIssue27Regression(t *testing.T) {
sc, err := NewSupportedCommands([]uint32{
// Result from calling:
// 0x00 "PIDs supported [01 - 20]"
// ----------------------------------
// > Bit encoded [A7..D0] == [PID $01..PID $20]
//
// A7 = 0x01 "Monitor status since DTCs cleared"
// D0 = 0x20 "PIDs supported [21 - 40]"
//
// A7 B7 C7 D7 D0
// | | | | |
// 10000000 00000000 00000000 00000001,
0x80000001,
// Result from calling:
// 0x20 "PIDs supported [21 - 40]"
// ----------------------------------
// > Bit encoded [A7..D0] == [PID $21..PID $40]
//
// A7 = 0x21 "Distance traveled with malfunction indicator lamp (MIL) on"
// D0 = 0x40 "PIDs supported [41 - 60]"
//
// A7 B7 C7 D7 D0
// | | | | |
// 10000000 00000000 00000000 00000001,
0x80000001,
// Result from calling:
// 0x40 "PIDs supported [41 - 60]"
// ----------------------------------
// > Bit encoded [A7..D0] == [PID $41..PID $60]
//
// A7 = 0x41 "Monitor status this drive cycle"
// D0 = 0x60 "PIDs supported [61 - 80]"
//
// A7 B7 C7 D7 D0
// | | | | |
// 10000000 00000000 00000000 00000001,
0x80000001,
// Result from calling:
// 0x60 "PIDs supported [61 - 80]"
// ----------------------------------
// > Bit encoded [A7..D0] == [PID $61..PID $80]
//
// A7 = 0x61 "Driver's demand engine - percent torque"
// D0 = 0x80 "PIDs supported [81 - A0]"
//
// A7 B7 C7 D7 D0
// | | | | |
// 10000000 00000000 00000000 00000001,
0x80000001,
// Result from calling:
// 0x80 "PIDs supported [81 - A0]"
// ----------------------------------
// > Bit encoded [A7..D0] == [PID $81..PID $A0]
//
// A7 = 0x81 "Engine run time for Auxiliary Emissions Control Device(AECD)"
// D0 = 0xA0 "PIDs supported [A1 - C0]"
//
// A7 B7 C7 D7 D0
// | | | | |
// 10000000 00000000 00000000 00000001,
0x80000001,
})
assert(t, err == nil, "Supported commands was created successfully")
supported := []OBDParameterID{
// Part 1 PIDs
0x01, 0x20,
// Part 2 PIDs
0x21, 0x40,
// Part 3 PIDs
0x41, 0x60,
// Part 4 PIDs
0x61, 0x80,
// Part 5 PIDs
0x81, 0xA0,
}
for _, pid := range supported {
cmd := &DummyCommand{
baseCommand{SERVICE_01_ID, pid, 1, "dummy"},
}
assertEqual(t, sc.IsSupported(cmd), true)
}
}
func TestParseOBDResponse(t *testing.T) {
type scenario struct {
command OBDCommand
outputs []string
}
scenarios := []scenario{
{
NewPartSupported(1),
[]string{"41 00 02 03 04 05"},
},
{
NewMonitorStatus(),
[]string{"41 01 FF 00 00 00"},
},
{
NewEngineLoad(),
[]string{"41 04 02"},
},
{
NewFuel(),
[]string{"41 2F 10"},
},
{
NewCoolantTemperature(),
[]string{"41 05 FF"},
},
{
NewShortFuelTrim1(),
[]string{"41 06 F2"},
},
{
NewLongFuelTrim1(),
[]string{"41 07 2F"},
},
{
NewShortFuelTrim2(),
[]string{"41 08 20"},
},
{
NewLongFuelTrim2(),
[]string{"41 09 01"},
},
{
NewFuelPressure(),
[]string{"41 0A BC"},
},
{
NewIntakeManifoldPressure(),
[]string{"41 0B C2"},
},
{
NewEngineRPM(),
[]string{"41 0C FF B2"},
},
{
NewVehicleSpeed(),
[]string{"41 0D A9"},
},
{
NewTimingAdvance(),
[]string{"41 0E 4F"},
},
{
NewIntakeAirTemperature(),
[]string{"41 0F EB"},
},
{
NewMafAirFlowRate(),
[]string{"41 10 C2 8B"},
},
{
NewThrottlePosition(),
[]string{"41 11 FF"},
},
// Regression tests for https://github.com/rzetterberg/elmobd/issues/5
{
NewEngineRPM(),
[]string{
"SEARCHING...",
"41 0C FF B2",
},
},
{
NewEngineRPM(),
[]string{
"BUS INIT",
"41 0C FF B2",
},
},
{
NewThrottlePosition(),
[]string{
"SEARCHING...",
"SEARCHING...",
"SEARCHING...",
"41 11 FF",
},
},
{
NewDistSinceDTCClear(),
[]string{"41 31 02 33"},
},
{
NewClearTroubleCodes(),
nil,
},
}
for _, curr := range scenarios {
assertOBDParseSuccess(t, curr.command, curr.outputs)
}
}