Skip to content
Snippets Groups Projects
Commit a5758f28 authored by Sebastien Binet's avatar Sebastien Binet
Browse files

dif: improve doc strings & Readout test coverage

parent 4d8950e9
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,7 @@ const (
hardrocASIC asicKind = 2
)
// Readout reads data out of a digital interface board (DIF).
type Readout struct {
msg log.MsgStream
dev *device
......@@ -40,6 +41,7 @@ type Readout struct {
temp [2]float32 // temperatures
}
// NewReadout creates a new DIF readout.
func NewReadout(name string, prodID uint32, msg log.MsgStream) (*Readout, error) {
dev, err := newDevice(0x0403, uint16(prodID))
if err != nil {
......@@ -59,7 +61,7 @@ func NewReadout(name string, prodID uint32, msg log.MsgStream) (*Readout, error)
rdo.reg.pd2daq = 0x4e
rdo.reg.daq2pd = 0x4e
rdo.reg.pd2pa = 0x4e
_, err = fmt.Sscanf(name, "FT101%d", &rdo.difID)
_, err = fmt.Sscanf(name, "FT101%03d", &rdo.difID)
if err != nil {
_ = dev.close()
return nil, xerrors.Errorf("could not find DIF-id from %q: %w", name, err)
......
......@@ -8,6 +8,7 @@ import (
"bytes"
"io"
"os"
"strings"
"testing"
"github.com/go-daq/tdaq/log"
......@@ -25,17 +26,46 @@ func TestReadout(t *testing.T) {
ftdiOpen = ftdiOpenImpl
}()
{
const name = "FT101xxx"
rdo, err := NewReadout(name, 0x6014, nil)
if err == nil {
rdo.close()
t.Fatalf("expected an error")
}
want := xerrors.Errorf("could not find DIF-id from %q: %s", name, xerrors.New("expected integer"))
if got, want := err.Error(), want.Error(); got != want {
t.Fatalf("invalid error:\ngot= %v\nwant=%v", got, want)
}
for _, tc := range []struct {
name string
err error
id uint32
}{
{
name: "FT101xxx",
err: xerrors.Errorf("could not find DIF-id from %q: %s", "FT101xxx", xerrors.New("expected integer")),
},
{
name: "FT101",
err: xerrors.Errorf("could not find DIF-id from %q: %s", "FT101", io.EOF),
},
{
name: "FT10142",
id: 42,
},
{
name: "FT101042",
id: 42,
},
} {
t.Run(tc.name, func(t *testing.T) {
rdo, err := NewReadout(tc.name, 0x6014, nil)
if err == nil && tc.err != nil {
rdo.close()
t.Fatalf("expected an error")
}
switch {
case tc.err != nil:
if got, want := err.Error(), tc.err.Error(); got != want {
t.Fatalf("invalid error:\ngot= %v\nwant=%v", got, want)
}
default:
defer rdo.close()
if rdo.difID != tc.id {
t.Fatalf("invalid DIF-id: got=%d, want=%d", rdo.difID, tc.id)
}
}
})
}
const (
......@@ -47,6 +77,9 @@ func TestReadout(t *testing.T) {
if err != nil {
t.Fatalf("could not create readout: %+v", err)
}
if got, want := rdo.difID, uint32(42); got != want {
t.Fatalf("invalid DIF-ID: got=%d, want=%d", got, want)
}
err = rdo.configureRegisters()
if err != nil {
......@@ -121,6 +154,23 @@ func TestReadout(t *testing.T) {
}
}
func TestInvalidReadout(t *testing.T) {
want := xerrors.Errorf("no such device")
ftdiOpen = func(vid, pid uint16) (ftdiDevice, error) { return nil, want }
defer func() {
ftdiOpen = ftdiOpenImpl
}()
rdo, err := NewReadout("FT101042", 0x6014, nil)
if err == nil {
_ = rdo.close()
t.Fatalf("expected an error, got=%v", err)
}
if got, want := err.Error(), want.Error(); !strings.Contains(got, want) {
t.Fatalf("invalid error.\ngot= %v\nwant=%v\n", got, want)
}
}
type fakeDevice struct {
buf io.ReadWriter
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment