This repository has been archived by the owner on May 26, 2021. It is now read-only.
forked from things-go/go-modbus
-
Notifications
You must be signed in to change notification settings - Fork 7
/
asciiclient_test.go
121 lines (116 loc) · 2.69 KB
/
asciiclient_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package modbus
import (
"reflect"
"testing"
)
func TestASCIIClientProvider_encodeASCIIFrame(t *testing.T) {
type args struct {
slaveID byte
pdu ProtocolDataUnit
}
tests := []struct {
name string
ascii *protocolFrame
args args
want []byte
wantErr bool
}{
{
"ASCII encode right 1",
&protocolFrame{adu: make([]byte, 0, asciiCharacterMaxSize)},
args{8, ProtocolDataUnit{1, []byte{2, 66, 1, 5}}},
[]byte(":080102420105AD\r\n"),
false,
},
{
"ASCII encode right 2",
&protocolFrame{adu: make([]byte, 0, asciiCharacterMaxSize)},
args{1, ProtocolDataUnit{3, []byte{8, 100, 10, 13}}},
[]byte(":010308640A0D79\r\n"),
false,
},
{
"ASCII encode error",
&protocolFrame{adu: make([]byte, 0, asciiCharacterMaxSize)},
args{1, ProtocolDataUnit{3, make([]byte, 254)}},
nil,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.ascii.encodeASCIIFrame(tt.args.slaveID, tt.args.pdu)
if (err != nil) != tt.wantErr {
t.Errorf("ASCIIClientProvider.encode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ASCIIClientProvider.encode() = %s, want %s", got, tt.want)
}
})
}
}
func TestASCIIClientProvider_decodeASCIIFrame(t *testing.T) {
type args struct {
adu []byte
}
tests := []struct {
name string
args args
slaveID uint8
pdu []byte
wantErr bool
}{
{
"ASCII decode 1",
args{[]byte(":080102420105AD\r\n")},
8,
[]byte{1, 2, 66, 1, 5},
false,
},
{
"ASCII decode 2",
args{[]byte(":010308640A0D79\r\n")},
1,
[]byte{3, 8, 100, 10, 13},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotslaveID, gotpdu, err := decodeASCIIFrame(tt.args.adu)
if (err != nil) != tt.wantErr {
t.Errorf("ASCIIClientProvider.decode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotslaveID != tt.slaveID {
t.Errorf("ASCIIClientProvider.decode() gotslaveID = %v, want %v", gotslaveID, tt.slaveID)
}
if !reflect.DeepEqual(gotpdu, tt.pdu) {
t.Errorf("ASCIIClientProvider.decode() gotpdu = %v, want %v", gotpdu, tt.pdu)
}
})
}
}
func BenchmarkASCIIClientProvider_encodeASCIIFrame(b *testing.B) {
p := protocolFrame{adu: make([]byte, 0, asciiCharacterMaxSize)}
pdu := ProtocolDataUnit{
1,
[]byte{2, 3, 4, 5, 6, 7, 8, 9},
}
for i := 0; i < b.N; i++ {
_, err := p.encodeASCIIFrame(10, pdu)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkASCIIClientProvider_decodeASCIIFrame(b *testing.B) {
adu := []byte(":010308640A0D79\r\n")
for i := 0; i < b.N; i++ {
_, _, err := decodeASCIIFrame(adu)
if err != nil {
b.Fatal(err)
}
}
}