Skip to content

Commit

Permalink
BUGFIX: tests with sysfs mocks, ReadBlockData, WriteBlockData
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas authored Oct 30, 2022
2 parents 11bc499 + 45abf11 commit 8cadea2
Show file tree
Hide file tree
Showing 22 changed files with 956 additions and 391 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ EXAMPLES := $(EXAMPLES_NO_GOCV)
including_except := $(shell go list ./... | grep -v platforms/opencv)

# Run tests on nearly all directories without test cache
# TODO: set back to parallel running (remove "-p 1"), when issue #878 is fixed
test:
go test -count=1 -v $(including_except)
go test -p 1 -count=1 -v $(including_except)

# Run tests with race detection
test_race:
go test -race $(including_except)

# Test, generate and show coverage in browser
# TODO: set back to parallel running (remove "-p 1"), when issue #878 is fixed
test_cover:
go test -v $(including_except) -coverprofile=coverage.txt ; \
go test -p 1 -v $(including_except) -coverprofile=coverage.txt ; \
go tool cover -html=coverage.txt ; \

robeaux:
Expand Down
52 changes: 35 additions & 17 deletions drivers/i2c/i2c_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"gobot.io/x/gobot/sysfs"
)

const dev = "/dev/i2c-1"

func syscallImpl(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) {
if (trap == syscall.SYS_IOCTL) && (a2 == sysfs.I2C_FUNCS) {
var funcPtr *uint64 = (*uint64)(unsafe.Pointer(a3))
Expand All @@ -31,38 +33,37 @@ func syscallImplFail(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errn
}

func initI2CDevice() I2cDevice {
fs := sysfs.NewMockFilesystem([]string{
"/dev/i2c-1",
})
fs := sysfs.NewMockFilesystem([]string{dev})
sysfs.SetFilesystem(fs)
sysfs.SetSyscall(&sysfs.MockSyscall{Impl: syscallImpl})

sysfs.SetSyscall(&sysfs.MockSyscall{
Impl: syscallImpl,
})
i, _ := sysfs.NewI2cDevice("/dev/i2c-1")
i, _ := sysfs.NewI2cDevice(dev)
return i
}

func cleanupI2CDevice() {
sysfs.SetFilesystem(&sysfs.NativeFilesystem{})
sysfs.SetSyscall(&sysfs.NativeSyscall{})
}

func initI2CDeviceAddressError() I2cDevice {
fs := sysfs.NewMockFilesystem([]string{
"/dev/i2c-1",
})
fs := sysfs.NewMockFilesystem([]string{dev})
sysfs.SetFilesystem(fs)
sysfs.SetSyscall(&sysfs.MockSyscall{Impl: syscallImplFail})

sysfs.SetSyscall(&sysfs.MockSyscall{
Impl: syscallImplFail,
})
i, _ := sysfs.NewI2cDevice("/dev/i2c-1")
i, _ := sysfs.NewI2cDevice(dev)
return i
}

func TestI2CAddress(t *testing.T) {
c := NewConnection(initI2CDevice(), 0x66)
defer cleanupI2CDevice()
gobottest.Assert(t, c.address, 0x66)
}

func TestI2CClose(t *testing.T) {
c := NewConnection(initI2CDevice(), 0x06)
defer cleanupI2CDevice()
gobottest.Assert(t, c.Close(), nil)
}

Expand All @@ -74,102 +75,119 @@ func TestI2CRead(t *testing.T) {

func TestI2CReadAddressError(t *testing.T) {
c := NewConnection(initI2CDeviceAddressError(), 0x06)
defer cleanupI2CDevice()
_, err := c.Read([]byte{})
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
}

func TestI2CWrite(t *testing.T) {
c := NewConnection(initI2CDevice(), 0x06)
defer cleanupI2CDevice()
i, _ := c.Write([]byte{0x01})
gobottest.Assert(t, i, 1)
}

func TestI2CWriteAddressError(t *testing.T) {
c := NewConnection(initI2CDeviceAddressError(), 0x06)
defer cleanupI2CDevice()
_, err := c.Write([]byte{0x01})
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
}

func TestI2CReadByte(t *testing.T) {
c := NewConnection(initI2CDevice(), 0x06)
defer cleanupI2CDevice()
v, _ := c.ReadByte()
gobottest.Assert(t, v, uint8(0))
gobottest.Assert(t, v, uint8(0xFC))
}

func TestI2CReadByteAddressError(t *testing.T) {
c := NewConnection(initI2CDeviceAddressError(), 0x06)
defer cleanupI2CDevice()
_, err := c.ReadByte()
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
}

func TestI2CReadByteData(t *testing.T) {
c := NewConnection(initI2CDevice(), 0x06)
defer cleanupI2CDevice()
v, _ := c.ReadByteData(0x01)
gobottest.Assert(t, v, uint8(0))
gobottest.Assert(t, v, uint8(0xFD))
}

func TestI2CReadByteDataAddressError(t *testing.T) {
c := NewConnection(initI2CDeviceAddressError(), 0x06)
defer cleanupI2CDevice()
_, err := c.ReadByteData(0x01)
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
}

func TestI2CReadWordData(t *testing.T) {
c := NewConnection(initI2CDevice(), 0x06)
defer cleanupI2CDevice()
v, _ := c.ReadWordData(0x01)
gobottest.Assert(t, v, uint16(0))
gobottest.Assert(t, v, uint16(0xFFFE))
}

func TestI2CReadWordDataAddressError(t *testing.T) {
c := NewConnection(initI2CDeviceAddressError(), 0x06)
defer cleanupI2CDevice()
_, err := c.ReadWordData(0x01)
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
}

func TestI2CWriteByte(t *testing.T) {
c := NewConnection(initI2CDevice(), 0x06)
defer cleanupI2CDevice()
err := c.WriteByte(0x01)
gobottest.Assert(t, err, nil)
}

func TestI2CWriteByteAddressError(t *testing.T) {
c := NewConnection(initI2CDeviceAddressError(), 0x06)
defer cleanupI2CDevice()
err := c.WriteByte(0x01)
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
}

func TestI2CWriteByteData(t *testing.T) {
c := NewConnection(initI2CDevice(), 0x06)
defer cleanupI2CDevice()
err := c.WriteByteData(0x01, 0x01)
gobottest.Assert(t, err, nil)
}

func TestI2CWriteByteDataAddressError(t *testing.T) {
c := NewConnection(initI2CDeviceAddressError(), 0x06)
defer cleanupI2CDevice()
err := c.WriteByteData(0x01, 0x01)
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
}

func TestI2CWriteWordData(t *testing.T) {
c := NewConnection(initI2CDevice(), 0x06)
defer cleanupI2CDevice()
err := c.WriteWordData(0x01, 0x01)
gobottest.Assert(t, err, nil)
}

func TestI2CWriteWordDataAddressError(t *testing.T) {
c := NewConnection(initI2CDeviceAddressError(), 0x06)
defer cleanupI2CDevice()
err := c.WriteWordData(0x01, 0x01)
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
}

func TestI2CWriteBlockData(t *testing.T) {
c := NewConnection(initI2CDevice(), 0x06)
defer cleanupI2CDevice()
err := c.WriteBlockData(0x01, []byte{0x01, 0x02})
gobottest.Assert(t, err, nil)
}

func TestI2CWriteBlockDataAddressError(t *testing.T) {
c := NewConnection(initI2CDeviceAddressError(), 0x06)
defer cleanupI2CDevice()
err := c.WriteBlockData(0x01, []byte{0x01, 0x02})
gobottest.Assert(t, err, errors.New("Setting address failed with syscall.Errno operation not permitted"))
}
Expand Down
Loading

0 comments on commit 8cadea2

Please sign in to comment.