-
Notifications
You must be signed in to change notification settings - Fork 369
/
asciiclient_test.go
76 lines (66 loc) · 1.54 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
// Copyright 2014 Quoc-Viet Nguyen. All rights reserved.
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
package modbus
import (
"bytes"
"testing"
)
func TestASCIIEncoding(t *testing.T) {
encoder := asciiPackager{}
encoder.SlaveId = 17
pdu := ProtocolDataUnit{}
pdu.FunctionCode = 3
pdu.Data = []byte{0, 107, 0, 3}
adu, err := encoder.Encode(&pdu)
if err != nil {
t.Fatal(err)
}
expected := []byte(":1103006B00037E\r\n")
if !bytes.Equal(expected, adu) {
t.Fatalf("adu actual: %v, expected %v", adu, expected)
}
}
func TestASCIIDecoding(t *testing.T) {
decoder := asciiPackager{}
decoder.SlaveId = 247
adu := []byte(":F7031389000A60\r\n")
pdu, err := decoder.Decode(adu)
if err != nil {
t.Fatal(err)
}
if 3 != pdu.FunctionCode {
t.Fatalf("Function code: expected %v, actual %v", 15, pdu.FunctionCode)
}
expected := []byte{0x13, 0x89, 0, 0x0A}
if !bytes.Equal(expected, pdu.Data) {
t.Fatalf("Data: expected %v, actual %v", expected, pdu.Data)
}
}
func BenchmarkASCIIEncoder(b *testing.B) {
encoder := asciiPackager{
SlaveId: 10,
}
pdu := ProtocolDataUnit{
FunctionCode: 1,
Data: []byte{2, 3, 4, 5, 6, 7, 8, 9},
}
for i := 0; i < b.N; i++ {
_, err := encoder.Encode(&pdu)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkASCIIDecoder(b *testing.B) {
decoder := asciiPackager{
SlaveId: 10,
}
adu := []byte(":F7031389000A60\r\n")
for i := 0; i < b.N; i++ {
_, err := decoder.Decode(adu)
if err != nil {
b.Fatal(err)
}
}
}