Skip to content

Commit

Permalink
Restructure tests. Remoe exposted fields from Chip and replace with g…
Browse files Browse the repository at this point in the history
…etter functions.
  • Loading branch information
gsexton committed Sep 2, 2024
1 parent f2576ad commit 3606c9b
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 680 deletions.
7 changes: 7 additions & 0 deletions gpioioctl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# GPIO IOCTL

This directory contains an implementation for linux GPIO manipulation
using the ioctl v2 interface.

Basic test is provided, but a much more complete smoke test is provided
in periph.io/x/cmd/periph-smoketest/gpiosmoketest
73 changes: 0 additions & 73 deletions gpioioctl/Testing.md

This file was deleted.

120 changes: 120 additions & 0 deletions gpioioctl/basic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package gpioioctl

// Copyright 2024 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.
//
// Basic tests. More complete test is contained in the
// periph.io/x/cmd/v3/periph-smoketest/gpiosmoketest
// folder.
import (
"testing"

"periph.io/x/conn/v3/driver/driverreg"
"periph.io/x/conn/v3/gpio/gpioreg"
)

var _test_line *GPIOLine

func init() {
_, _ = driverreg.Init()
}

func TestChips(t *testing.T) {
if len(Chips) <= 0 {
t.Fatal("Chips contains no entries.")
}
chip := Chips[0]
if len(chip.Name()) == 0 {
t.Error("chip.Name() is 0 length")
}
if len(chip.Path())==0 {
t.Error("chip path is 0 length")
}
if len(chip.Label())==0 {
t.Error("chip label is 0 length!")
}
if len(chip.Lines()) != chip.LineCount() {
t.Errorf("Incorrect line count. Found: %d for LineCount, Returned Lines length=%d", chip.LineCount(), len(chip.Lines()))
}
for _, line := range chip.Lines() {
if len(line.Consumer()) == 0 {
_test_line = line
break
}
}
if _test_line == nil {
t.Error("Error finding unused line for testing!")
}
for _, c := range Chips {
s := c.String()
if len(s) == 0 {
t.Error("Error calling chip.String(). No output returned!")
} else {
t.Log(s)
}

}

}

func TestGPIORegistryByName(t *testing.T) {
outLine := gpioreg.ByName(_test_line.Name())
if outLine == nil {
t.Fatalf("Error retrieving GPIO Line %s", _test_line.Name())
}
if outLine.Name() != _test_line.Name() {
t.Errorf("Error checking name. Expected %s, received %s", _test_line.Name(), outLine.Name())
}

if outLine.Number() < 0 || outLine.Number() >= len(Chips[0].Lines()) {
t.Errorf("Invalid chip number %d received for %s", outLine.Number(), _test_line.Name())
}
}

// Test the consumer field. Since this actually configures a line for output,
// it actually tests a fair amount of the code to request a line, and configure
// it.
func TestConsumer(t *testing.T) {

l := Chips[0].ByName(_test_line.Name())
if l == nil {
t.Fatalf("Error retrieving GPIO Line %s", _test_line.Name())
}
defer l.Close()
// Consumer isn't written until the line is configured.
err := l.Out(true)
if err != nil {
t.Errorf("l.Out() %s", err)
}
if l.Consumer() != string(consumer) {
t.Errorf("Incorrect consumer name. Expected consumer name %s on line. received empty %s", string(consumer), l.Consumer())
}
}

func TestNumber(t *testing.T) {
chip := Chips[0]
l := chip.ByName(_test_line.Name())
if l == nil {
t.Fatalf("Error retrieving GPIO Line %s", _test_line.Name())
}
if l.Number() < 0 || l.Number() >= chip.LineCount() {
t.Errorf("line.Number() returned value (%d) out of range", l.Number())
}
l2 := chip.ByNumber(l.Number())
if l2 == nil {
t.Errorf("retrieve Line from chip by number %d failed.", l.Number())
}

}

func TestString(t *testing.T) {
line := gpioreg.ByName(_test_line.Name())
if line == nil {
t.Fatalf("Error retrieving GPIO Line %s", _test_line.Name())
}
s := line.String()
if len(s) == 0 {
t.Errorf("GPIOLine.String() failed.")
}
}
2 changes: 1 addition & 1 deletion gpioioctl/example_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package gpioioctl_test

// Copyright 2024 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.


import (
"fmt"
"log"
Expand Down
Loading

0 comments on commit 3606c9b

Please sign in to comment.