@@ -4,12 +4,19 @@ import (
44 "fmt"
55 "testing"
66
7+ "github.com/advancedclimatesystems/io/dac"
78 "github.com/advancedclimatesystems/io/iotest"
89 "github.com/stretchr/testify/assert"
910 "golang.org/x/exp/io/i2c"
1011)
1112
12- func TestNewMax581x (t * testing.T ) {
13+ func TestDACinterface (t * testing.T ) {
14+ assert .Implements (t , (* dac .DAC )(nil ), new (MAX5813 ))
15+ assert .Implements (t , (* dac .DAC )(nil ), new (MAX5814 ))
16+ assert .Implements (t , (* dac .DAC )(nil ), new (MAX5815 ))
17+ }
18+
19+ func TestNewMAX581x (t * testing.T ) {
1320 conn , _ := i2c .Open (iotest .NewI2CDriver (iotest .NewI2CConn ()), 0x1 )
1421
1522 max5813 , _ := NewMAX5813 (conn , 3 )
@@ -100,6 +107,75 @@ func TestMAX581xSetVoltage(t *testing.T) {
100107 }
101108}
102109
110+ func TestMAX581xConn (t * testing.T ) {
111+ c , _ := i2c .Open (iotest .NewI2CDriver (iotest .NewI2CConn ()), 0x1 )
112+ dac , _ := NewMAX5813 (c , 2.048 )
113+ assert .Equal (t , c , dac .Conn ())
114+ }
115+
116+ // TestMAX581xSetInputCodeWithInvalidChannel calls dac.SetInputCode with a
117+ // input code that lies outside the range of the DAC.
118+ func TestMAX581xSetInputCodeWithInvalidCode (t * testing.T ) {
119+ var tests = []struct {
120+ dac dac.DAC
121+ code int
122+ }{
123+ {MAX5813 {}, - 1 },
124+ {MAX5813 {}, 256 },
125+ {MAX5814 {}, - 1 },
126+ {MAX5814 {}, 1024 },
127+ {MAX5815 {}, - 1 },
128+ {MAX5815 {}, 4096 },
129+ }
130+
131+ for _ , test := range tests {
132+ assert .EqualError (
133+ t ,
134+ test .dac .SetInputCode (test .code , 1 ),
135+ fmt .Sprintf ("digital input code %d is out of range of 0 <= code < 1" , test .code ))
136+ }
137+ }
138+
139+ // TestMAX581xSetInputCodeWithInvalidChannel calls dac.SetInputCode with a
140+ // channel that isn't in the range of the DAC.
141+ func TestMAX581xSetInputCodeWithInvalidChannel (t * testing.T ) {
142+ dac := max581x {}
143+
144+ for _ , channel := range []int {- 1 , 4 } {
145+ assert .EqualError (
146+ t ,
147+ dac .SetInputCode (512 , channel ),
148+ fmt .Sprintf ("%d is not a valid channel" , channel ))
149+ }
150+ }
151+
152+ // TestMAX581xWithFailingConnection test if all DAC's return errors when the
153+ // connection fails.
154+ func TestMAX581xWithFailingConnection (t * testing.T ) {
155+ c := iotest .NewI2CConn ()
156+ c .TxFunc (func (w , _ []byte ) error {
157+ return fmt .Errorf ("som error occured" )
158+ })
159+ conn , _ := i2c .Open (iotest .NewI2CDriver (c ), 0x1 )
160+
161+ _ , err := NewMAX5813 (conn , 2.048 )
162+ assert .NotNil (t , err )
163+
164+ _ , err = NewMAX5814 (conn , 2.048 )
165+ assert .NotNil (t , err )
166+
167+ _ , err = NewMAX5815 (conn , 2.048 )
168+ assert .NotNil (t , err )
169+
170+ dac := max581x {
171+ conn : conn ,
172+ vref : 2.048 ,
173+ resolution : 8 ,
174+ }
175+
176+ assert .NotNil (t , dac .SetInputCode (512 , 1 ))
177+ }
178+
103179func ExampleMAX5813 () {
104180 d , err := i2c .Open (& i2c.Devfs {
105181 Dev : "/dev/i2c-0" ,
0 commit comments