robocar-pca9685/pkg/actuator/pca9685_test.go

90 lines
1.6 KiB
Go
Raw Normal View History

package actuator
import (
"go.uber.org/zap"
"periph.io/x/conn/v3/physic"
"testing"
)
func init() {
l, _ := zap.NewDevelopment()
zap.ReplaceGlobals(l)
}
func Test_convertToDuty(t *testing.T) {
type fields struct {
}
type args struct {
percent float32
leftPWM PWM
rightPWM PWM
centerPWM PWM
freq physic.Frequency
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
name: "center",
args: args{
percent: 0.,
leftPWM: 1000,
rightPWM: 2000,
centerPWM: 1500,
freq: 60 * physic.Hertz,
},
want: "9%",
},
{
name: "left",
args: args{
percent: -1.,
leftPWM: 1000,
rightPWM: 2000,
centerPWM: 1500,
freq: 60 * physic.Hertz,
},
want: "6%",
},
{
name: "right",
args: args{
percent: 1.,
leftPWM: 1000,
rightPWM: 2000,
centerPWM: 1500,
freq: 60 * physic.Hertz,
},
want: "12%",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2022-06-15 17:53:02 +00:00
c := Pca9685Controller{
logr: zap.S(),
pin: nil,
minPWM: tt.args.leftPWM,
maxPWM: tt.args.rightPWM,
neutralPWM: tt.args.centerPWM,
freq: tt.args.freq,
}
2022-06-15 17:53:02 +00:00
if got, err := c.convertToDuty(tt.args.percent); got.String() != tt.want {
if tt.wantErr && err == nil {
t.Errorf("an error is expected")
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
t.Errorf("convertToDuty() = %v, want %v", got, tt.want)
}
})
}
}