@@ -4,6 +4,37 @@ import (
44 "testing"
55)
66
7+ func TestV2Tag (t * testing.T ) {
8+ testV2Tags (t , 0x05 , []byte {0x05 })
9+ testV2Tags (t , 0x3F , []byte {0x3F })
10+ testV2Tags (t , 0x7F , []byte {0x80 , 0x7F })
11+ testV2Tags (t , 0xFF , []byte {0x81 , 0x7F })
12+ testV2Tags (t , 0xFFFF , []byte {0x83 , 0xFF , 0x7F })
13+ testV2Tags (t , 0xFFFFFF , []byte {0x87 , 0xFF , 0xFF , 0x7F })
14+ }
15+
16+ func TestV2AddNode (t * testing.T ) {
17+ parent , _ := NewPacket (0x01 )
18+
19+ child1 , _ := NewPacket (0x02 )
20+ child1 .SetUInt32 (3 )
21+
22+ parent .AddNode (child1 )
23+ compareBytes (t , parent , []byte {0x01 , 0x03 , 0x02 , 0x01 , 0x03 })
24+
25+ child2 , _ := NewPacket (0x03 )
26+ child2 .SetFloat64 (0.01171875 )
27+
28+ parent .AddNode (child2 )
29+ compareBytes (t , parent , []byte {0x01 , 0x07 , 0x02 , 0x01 , 0x03 , 0x03 , 0x02 , 0x3F , 0x88 })
30+
31+ child3 , _ := NewPacket (0x04 )
32+ child3 .SetFloat32 (68.123 )
33+
34+ parent .AddNode (child3 )
35+ compareBytes (t , parent , []byte {0x01 , 0x0D , 0x02 , 0x01 , 0x03 , 0x03 , 0x02 , 0x3F , 0x88 , 0x04 , 0x04 , 0x42 , 0x88 , 0x3E , 0xFA })
36+ }
37+
738func TestFromBytes (t * testing.T ) {
839 testFromBytes (t , []byte {0x03 , 0x01 , 0x02 }, & Packet {Tag : 3 , Length : 1 , valbuf : []byte {0x02 }})
940 testFromBytes (t , []byte {0x81 , 0x03 , 0x01 , 0x06 }, & Packet {Tag : 131 , Length : 1 , valbuf : []byte {0x06 }})
@@ -14,25 +45,6 @@ func TestFromBytes(t *testing.T) {
1445 testFromBytes (t , buf , & Packet {Tag : 131 , Length : 129 , valbuf : foo })
1546}
1647
17- func testFromBytes (t * testing.T , buffer []byte , expected * Packet ) {
18- res , err := FromBytes (buffer )
19- if err != nil {
20- t .Error (err )
21- } else {
22- if res .Tag != expected .Tag {
23- t .Errorf ("Tag expected=[% X], actual=[% X]" , expected .Tag , res .Tag )
24- }
25- if res .Length != expected .Length {
26- t .Errorf ("Length expected=[% X], actual=[% X]" , expected .Length , res .Length )
27- }
28- for i := range res .valbuf {
29- if res .valbuf [i ] != expected .valbuf [i ] {
30- t .Errorf ("valbuf on [%d] expected=[% X], actual=[% X]" , i , expected .valbuf [i ], res .valbuf [i ])
31- }
32- }
33- }
34- }
35-
3648func TestReadTag (t * testing.T ) {
3749 testReadTag (t , []byte {0x03 , 0x01 , 0x02 }, 0 , 3 , 1 )
3850 testReadTag (t , []byte {0xFF , 0x04 , 0x01 , 0x02 }, 1 , 4 , 2 )
@@ -74,11 +86,45 @@ func testReadLength(t *testing.T, buffer []byte, position int, expectValue uint6
7486 }
7587}
7688
77- func TestGetValueAsUInt32 (t * testing.T ) {
78- buf := []byte {0x03 , 0x01 , 0x02 }
79- p , _ := FromBytes (buf )
80- actual , _ := p .GetValueAsUInt32 ()
81- if actual != 2 {
82- t .Errorf ("expect=%d, actual=%d" , 2 , actual )
89+ func testFromBytes (t * testing.T , buffer []byte , expected * Packet ) {
90+ res , err := FromBytes (buffer )
91+ if err != nil {
92+ t .Error (err )
93+ } else {
94+ if res .Tag != expected .Tag {
95+ t .Errorf ("Tag expected=[% X], actual=[% X]" , expected .Tag , res .Tag )
96+ }
97+ if res .Length != expected .Length {
98+ t .Errorf ("Length expected=[% X], actual=[% X]" , expected .Length , res .Length )
99+ }
100+ for i := range res .valbuf {
101+ if res .valbuf [i ] != expected .valbuf [i ] {
102+ t .Errorf ("valbuf on [%d] expected=[% X], actual=[% X]" , i , expected .valbuf [i ], res .valbuf [i ])
103+ }
104+ }
105+ }
106+ }
107+
108+ func testV2Tags (t * testing.T , id uint64 , expected []byte ) {
109+ p , err := NewPacket (id )
110+ if err != nil {
111+ t .Errorf ("TestV2Tag err=%v" , err )
112+ }
113+ p .SetInt32 (0 )
114+ expected = append (expected , []byte {0x01 , 0x00 }... )
115+ compareBytes (t , p , expected )
116+ }
117+
118+ func compareBytes (t * testing.T , p * Packet , expected []byte ) []byte {
119+ result , err := p .Encode ()
120+ if err != nil {
121+ t .Errorf ("compareBytes error=%v" , err )
122+ }
123+ for i , p := range result {
124+ if p != expected [i ] {
125+ t .Errorf ("\n expected:[% X]\n actual:[% X]\n " , expected , result )
126+ break
127+ }
83128 }
129+ return result
84130}
0 commit comments