Skip to content

Commit

Permalink
test encoding and decoding of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
siscia committed Mar 8, 2022
1 parent 8c2c58c commit b703000
Showing 1 changed file with 96 additions and 2 deletions.
98 changes: 96 additions & 2 deletions ua/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type B struct {
S []*A
}

type C struct {
A [2]int32
}

func TestCodec(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -112,6 +116,30 @@ func TestCodec(t *testing.T) {
0x67, 0x45, 0x00, 0x00,
},
},
{
name: "[2]uint32",
v: &struct{ V [2]uint32 }{[2]uint32{0x1234, 0x4567}},
b: []byte{
// length
0x02, 0x00, 0x00, 0x00,
// elem 1
0x34, 0x12, 0x00, 0x00,
// elem 2
0x67, 0x45, 0x00, 0x00,
},
},
{
name: "[2]uint32{1}",
v: &struct{ V [2]uint32 }{[2]uint32{1}},
b: []byte{
// length
0x02, 0x00, 0x00, 0x00,
// elem 1
0x01, 0x00, 0x00, 0x00,
// zero element of the array
0x00, 0x00, 0x00, 0x00,
},
},
{
name: "string",
v: &struct{ V string }{"abc"},
Expand Down Expand Up @@ -180,6 +208,17 @@ func TestCodec(t *testing.T) {
0x03, 0x00, 0x00, 0x00,
},
},
{
name: "[2]uint32",
v: &struct{ V [2]uint32 }{[2]uint32{}},
b: []byte{
// length
0x02, 0x00, 0x00, 0x00,
// values
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
},
},
{
name: "[]*A",
v: &struct{ V []*A }{
Expand All @@ -206,7 +245,7 @@ func TestCodec(t *testing.T) {
},
},
b: []byte{
// B.A.N
// B.A.V
0x34, 0x12, 0x00, 0x00,
// B.A.S == nil
0xff, 0xff, 0xff, 0xff,
Expand All @@ -222,7 +261,7 @@ func TestCodec(t *testing.T) {
},
},
b: []byte{
// B.A.N
// B.A.V
0x90, 0x78, 0x00, 0x00,
// len(B.A.S)
0x02, 0x00, 0x00, 0x00,
Expand All @@ -232,6 +271,45 @@ func TestCodec(t *testing.T) {
0x67, 0x45, 0x00, 0x00,
},
},
{
name: "&C",
v: &C{A: [2]int32{1, 2}},
b: []byte{
// len(C.A)
0x02, 0x00, 0x00, 0x00,
// C.A[0]
0x01, 0x00, 0x00, 0x00,
// C.A[1]
0x02, 0x00, 0x00, 0x00,
},
},
{
name: "[3]C",
v: &struct{ V [3]C }{[3]C{
{},
{A: [2]int32{7}},
{A: [2]int32{0, 9}},
}},
b: []byte{
// len(V)
0x03, 0x00, 0x00, 0x00,
// len(V[0])
0x02, 0x00, 0x00, 0x00,
// V[0]
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// len(V[1])
0x02, 0x00, 0x00, 0x00,
// V[1]
0x07, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// len(V[2])
0x02, 0x00, 0x00, 0x00,
// V[2]
0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00,
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -266,3 +344,19 @@ func TestCodec(t *testing.T) {
})
}
}

func TestFailDecodeArray(t *testing.T) {
b := []byte{
// len
0x03, 0x00, 0x00, 0x00,
// Values
0x00, 0x00, 0x00, 0x00, // 0
0x00, 0x00, 0x00, 0x00, // 1
0x07, 0x00, 0x00, 0x00, // 7
}
var a [2]int32
_, err := Decode(b, &a)
if err == nil {
t.Fatalf("was expecting error for tryig to decode a stream of bytes with length 3 into an array of size 2")
}
}

0 comments on commit b703000

Please sign in to comment.