-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathi2c_test.go
114 lines (106 loc) · 2.7 KB
/
i2c_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
// Copyright 2016 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package sysfs
import (
"testing"
"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/conn/v3/physic"
)
func TestNewI2C(t *testing.T) {
if b, err := NewI2C(-1); b != nil || err == nil {
t.Fatal("invalid bus")
}
}
func TestI2C_faked(t *testing.T) {
// Create a fake I2C to test methods.
bus := I2C{f: &ioctlClose{}, busNumber: 24}
if s := bus.String(); s != "I2C24" {
t.Fatal(s)
}
if bus.Tx(0x401, nil, nil) == nil {
t.Fatal("empty Tx")
}
if err := bus.Tx(1, nil, nil); err != nil {
t.Fatal(err)
}
if err := bus.Tx(1, []byte{0}, nil); err != nil {
t.Fatal(err)
}
if err := bus.Tx(1, nil, []byte{0}); err != nil {
t.Fatal(err)
}
if err := bus.Tx(1, []byte{0}, []byte{0}); err != nil {
t.Fatal(err)
}
if bus.SetSpeed(0) == nil {
t.Fatal("0 is invalid")
}
if bus.SetSpeed(physic.Hertz) == nil {
t.Fatal("can't set speed")
}
bus.SCL()
bus.SDA()
if err := bus.Close(); err != nil {
t.Fatal(err)
}
}
func TestI2C_functionality(t *testing.T) {
expected := "I2C|10BIT_ADDR|PROTOCOL_MANGLING|SMBUS_PEC|NOSTART|SMBUS_BLOCK_PROC_CALL|SMBUS_QUICK|SMBUS_READ_BYTE|SMBUS_WRITE_BYTE|SMBUS_READ_BYTE_DATA|SMBUS_WRITE_BYTE_DATA|SMBUS_READ_WORD_DATA|SMBUS_WRITE_WORD_DATA|SMBUS_PROC_CALL|SMBUS_READ_BLOCK_DATA|SMBUS_WRITE_BLOCK_DATA|SMBUS_READ_I2C_BLOCK|SMBUS_WRITE_I2C_BLOCK"
if s := functionality(0xFFFFFFFF).String(); s != expected {
t.Fatal(s)
}
}
func TestDriver_Init(t *testing.T) {
d := driverI2C{}
if _, err := d.Init(); err == nil {
// It will fail on non-linux.
defer func() {
for _, name := range d.buses {
if err := i2creg.Unregister(name); err != nil {
t.Fatal(err)
}
}
}()
if len(d.buses) != 0 {
// It may fail due to ACL.
b, _ := i2creg.Open("")
if b != nil {
// If opening succeeded, closing must always succeed.
if err := b.Close(); err != nil {
t.Fatal(err)
}
}
}
}
if d.Prerequisites() != nil {
t.Fatal("unexpected prerequisite")
}
if drvI2C.setSpeed != nil {
t.Fatal("unexpected setSpeed")
}
defer func() {
drvI2C.setSpeed = nil
}()
if I2CSetSpeedHook(nil) == nil {
t.Fatal("must fail on nil hook")
}
if err := I2CSetSpeedHook(func(f physic.Frequency) error { return nil }); err != nil {
t.Fatal(err)
}
if I2CSetSpeedHook(func(f physic.Frequency) error { return nil }) == nil {
t.Fatal("second I2CSetSpeedHook must fail")
}
}
func BenchmarkI2C(b *testing.B) {
b.ReportAllocs()
i := ioctlClose{}
bus := I2C{f: &i}
var w [16]byte
var r [16]byte
for i := 0; i < b.N; i++ {
if err := bus.Tx(0x01, w[:], r[:]); err != nil {
b.Fatal(err)
}
}
}