Debian Codegraph @

asciiclient_test.go

raw symbols SCIP
1 // Copyright 2014 Quoc-Viet Nguyen. All rights reserved.
2 // This software may be modified and distributed under the terms
3 // of the BSD license. See the LICENSE file for details.
4
5 package modbus
6
7 import (
8 "bytes"
9 "testing"
10 )
11
12 func TestASCIIEncoding(t *testing.T) {
13 encoder := asciiPackager{}
14 encoder.SlaveId = 17
15
16 pdu := ProtocolDataUnit{}
17 pdu.FunctionCode = 3
18 pdu.Data = []byte{0, 107, 0, 3}
19
20 adu, err := encoder.Encode(&pdu)
21 if err != nil {
22 t.Fatal(err)
23 }
24 expected := []byte(":1103006B00037E\r\n")
25 if !bytes.Equal(expected, adu) {
26 t.Fatalf("adu actual: %v, expected %v", adu, expected)
27 }
28 }
29
30 func TestASCIIDecoding(t *testing.T) {
31 decoder := asciiPackager{}
32 decoder.SlaveId = 247
33 adu := []byte(":F7031389000A60\r\n")
34
35 pdu, err := decoder.Decode(adu)
36 if err != nil {
37 t.Fatal(err)
38 }
39
40 if 3 != pdu.FunctionCode {
41 t.Fatalf("Function code: expected %v, actual %v", 15, pdu.FunctionCode)
42 }
43 expected := []byte{0x13, 0x89, 0, 0x0A}
44 if !bytes.Equal(expected, pdu.Data) {
45 t.Fatalf("Data: expected %v, actual %v", expected, pdu.Data)
46 }
47 }
48
49 func BenchmarkASCIIEncoder(b *testing.B) {
50 encoder := asciiPackager{
51 SlaveId: 10,
52 }
53 pdu := ProtocolDataUnit{
54 FunctionCode: 1,
55 Data: []byte{2, 3, 4, 5, 6, 7, 8, 9},
56 }
57 for i := 0; i < b.N; i++ {
58 _, err := encoder.Encode(&pdu)
59 if err != nil {
60 b.Fatal(err)
61 }
62 }
63 }
64
65 func BenchmarkASCIIDecoder(b *testing.B) {
66 decoder := asciiPackager{
67 SlaveId: 10,
68 }
69 adu := []byte(":F7031389000A60\r\n")
70 for i := 0; i < b.N; i++ {
71 _, err := decoder.Decode(adu)
72 if err != nil {
73 b.Fatal(err)
74 }
75 }
76 }