代码拉取完成,页面将自动刷新
同步操作将从 xiaoma/go-modbus 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package modbus
import (
"reflect"
"testing"
)
func Test_protocolFrame_encodeTCPFrame(t *testing.T) {
newBuffer := func() *protocolFrame {
return &protocolFrame{make([]byte, 0, tcpAduMaxSize)}
}
type args struct {
tid uint16
slaveID byte
pdu ProtocolDataUnit
}
tests := []struct {
name string
this *protocolFrame
args args
want protocolTCPHeader
want1 []byte
wantErr bool
}{
{
"TCP encode",
newBuffer(),
args{
0,
0,
ProtocolDataUnit{1, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}}},
protocolTCPHeader{0, 0, 11, 0},
[]byte{0, 0, 0, 0, 0, 11, 0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := tt.this.encodeTCPFrame(tt.args.tid, tt.args.slaveID, tt.args.pdu)
if (err != nil) != tt.wantErr {
t.Errorf("protocolFrame.encodeTCPFrame() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("protocolFrame.encodeTCPFrame() got = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("protocolFrame.encodeTCPFrame() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
func TestTCPClientProvider_decodeTCPFrame(t *testing.T) {
type args struct {
adu []byte
}
tests := []struct {
name string
args args
head protocolTCPHeader
pdu []byte
wantErr bool
}{
{
"TCP decode",
args{[]byte{0, 0, 0, 0, 0, 11, 0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9}},
protocolTCPHeader{0, 0, 11, 0},
[]byte{1, 1, 2, 3, 4, 5, 6, 7, 8, 9},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotHead, gotPdu, err := decodeTCPFrame(tt.args.adu)
if (err != nil) != tt.wantErr {
t.Errorf("TCPClientProvider.decode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotHead, tt.head) {
t.Errorf("TCPClientProvider.decode() gotHead = %v, want %v", gotHead, tt.head)
}
if !reflect.DeepEqual(gotPdu, tt.pdu) {
t.Errorf("TCPClientProvider.decode() gotPdu = %v, want %v", gotPdu, tt.pdu)
}
})
}
}
func Test_verifyTCPFrame(t *testing.T) {
type args struct {
reqHead protocolTCPHeader
rspHead protocolTCPHeader
reqPDU ProtocolDataUnit
rspPDU ProtocolDataUnit
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"TCP verify same",
args{
protocolTCPHeader{1, 2, 6, 4},
protocolTCPHeader{1, 2, 6, 4},
ProtocolDataUnit{10, []byte{1, 2, 3, 4}},
ProtocolDataUnit{10, []byte{1, 2, 3, 4}},
},
false,
},
{
"TCP verify transactionID different",
args{
protocolTCPHeader{1, 2, 6, 4},
protocolTCPHeader{5, 2, 6, 4},
ProtocolDataUnit{10, []byte{1, 2, 3, 4}},
ProtocolDataUnit{10, []byte{1, 2, 3, 4}},
},
true,
},
{
"TCP verify protocolID different",
args{
protocolTCPHeader{1, 2, 6, 4},
protocolTCPHeader{1, 5, 6, 4},
ProtocolDataUnit{10, []byte{1, 2, 3, 4}},
ProtocolDataUnit{10, []byte{1, 2, 3, 4}},
},
true,
},
{
"serial verify slaveID different",
args{
protocolTCPHeader{1, 2, 6, 11},
protocolTCPHeader{1, 2, 6, 4},
ProtocolDataUnit{10, []byte{1, 2, 3, 4}},
ProtocolDataUnit{10, []byte{1, 2, 3, 4}},
},
true,
},
{
"serial verify functionCode different",
args{
protocolTCPHeader{1, 2, 6, 4},
protocolTCPHeader{1, 2, 6, 4},
ProtocolDataUnit{10, []byte{1, 2, 3, 4}},
ProtocolDataUnit{11, []byte{1, 2, 3, 4}},
},
true,
},
{
"serial verify pdu data zero length",
args{
protocolTCPHeader{1, 2, 6, 4},
protocolTCPHeader{1, 2, 6, 4},
ProtocolDataUnit{10, []byte{}},
ProtocolDataUnit{10, []byte{}},
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := verifyTCPFrame(tt.args.reqHead, tt.args.rspHead, tt.args.reqPDU, tt.args.rspPDU)
if (err != nil) != tt.wantErr {
t.Errorf("verifyTCPFrame() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func BenchmarkTCPClientProvider_encodeTCPFrame(b *testing.B) {
tcp := &protocolFrame{make([]byte, 0, tcpAduMaxSize)}
pdu := ProtocolDataUnit{
1,
[]byte{2, 3, 4, 5, 6, 7, 8, 9, 10},
}
for i := 0; i < b.N; i++ {
_, _, err := tcp.encodeTCPFrame(0, 0, pdu)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkTCPClientProvider_decodeTCPFrame(b *testing.B) {
adu := []byte{0, 1, 0, 0, 0, 9, 20, 1, 2, 3, 4, 5, 6, 7, 8}
for i := 0; i < b.N; i++ {
_, _, err := decodeTCPFrame(adu)
if err != nil {
b.Fatal(err)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。