-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathstruct_test.go
171 lines (153 loc) · 3.57 KB
/
struct_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package bpl_test
import (
"bytes"
"encoding/binary"
"encoding/json"
"reflect"
"testing"
"github.com/goplus/bpl"
bin "github.com/goplus/bpl/binary"
"github.com/qiniu/x/bufiox"
)
type fixedType struct {
A int8
B uint16
C uint32
D float32
}
func TestFixedStruct(t *testing.T) {
v := fixedType{
A: 1,
B: 2,
C: 3,
D: 3.14,
}
b := new(bytes.Buffer)
err := binary.Write(b, binary.LittleEndian, &v)
if err != nil {
t.Fatal("binary.Write failed:", err)
}
if b.Len() != 11 {
t.Fatal("len != 11")
}
members := []bpl.Ruler{
&bpl.Member{Name: "a", Type: bpl.Int8},
&bpl.Member{Name: "b", Type: bpl.Uint16},
&bpl.Member{Name: "c", Type: bpl.Uint32},
&bpl.Member{Name: "d", Type: bpl.Float32},
}
struc := bpl.Struct(members)
if struc.SizeOf() != 11 {
t.Fatal("struct.size != 11 - ", struc.SizeOf())
}
in := bufiox.NewReaderBuffer(b.Bytes())
if in.Buffered() != 11 {
t.Fatal("len != 11")
}
ctx := bpl.NewContext()
ret, err := struc.Match(in, ctx)
if err != nil {
t.Fatal("struc.Match failed:", err)
}
text, err := json.Marshal(ret)
if err != nil {
t.Fatal("json.Marshal failed:", err)
}
if string(text) != `{"a":1,"b":2,"c":3,"d":3.14}` {
t.Fatal("json.Marshal result:", string(text))
}
}
type subType struct {
Foo string
}
type fooType struct {
A int8
B uint16
C uint32
D float32
E string
F subType
G float64
}
var (
rSubType = bpl.Seq(bpl.CString)
rFooType = bpl.Seq(bpl.Int8, bpl.Uint16, bpl.Uint32, bpl.Float32, bpl.CString, rSubType, bpl.Float64)
rFooType2 = bpl.And(bpl.Seq(bpl.Int8, bpl.Uint16), bpl.Uint32, bpl.Float32, bpl.CString, bpl.Seq(rSubType), bpl.Float64)
)
func TestStruct(t *testing.T) {
foo := &fooType{
A: 1, B: 2, C: 3, D: 3.14, E: "Hello", F: subType{Foo: "foo"}, G: 7.52,
// 1 + 2 + 4 + 4 + 6 + 4 + 8 = 29
}
b, err := bin.Marshal(&foo)
if err != nil {
t.Fatal("binary.Marshal failed:", err)
}
if len(b) != 29 {
t.Fatal("len(b) != 29, len:", len(b), "data:", string(b))
}
fooTyp := reflect.TypeOf(foo)
r, err := bpl.TypeFrom(fooTyp)
if err != nil {
t.Fatal("bpl.TypeFrom failed:", err)
}
in := bufiox.NewReaderBuffer(b)
ctx := bpl.NewContext()
v, err := r.Match(in, ctx)
if err != nil {
t.Fatal("Match failed:", err, "len:", len(b))
}
ret, err := json.Marshal(v)
if err != nil {
t.Fatal("json.Marshal failed:", err)
}
if string(ret) != `{"a":1,"b":2,"c":3,"d":3.14,"e":"Hello","f":{"foo":"foo"},"g":7.52}` {
t.Fatal("ret:", string(ret))
}
}
func TestSeq(t *testing.T) {
foo := &fooType{
A: 1, B: 2, C: 3, D: 3.14, E: "Hello", F: subType{Foo: "foo"}, G: 7.52,
// 1 + 2 + 4 + 4 + 6 + 4 + 8 = 29
}
b, err := bin.Marshal(&foo)
if err != nil {
t.Fatal("binary.Marshal failed:", err)
}
in := bufiox.NewReaderBuffer(b)
v, err := rFooType.Match(in, bpl.NewContext())
if err != nil {
t.Fatal("Match failed:", err, "len:", len(b))
}
ret, err := json.Marshal(v)
if err != nil {
t.Fatal("json.Marshal failed:", err)
}
if string(ret) != `[1,2,3,3.14,"Hello",["foo"],7.52]` {
t.Fatal("ret:", string(ret))
}
}
func TestSeq2(t *testing.T) {
foo := &fooType{
A: 1, B: 2, C: 3, D: 3.14, E: "Hello", F: subType{Foo: "foo"}, G: 7.52,
// 1 + 2 + 4 + 4 + 6 + 4 + 8 = 29
}
b, err := bin.Marshal(&foo)
if err != nil {
t.Fatal("binary.Marshal failed:", err)
}
in := bufiox.NewReaderBuffer(b)
ctx := bpl.NewContext()
_, err = rFooType2.Match(in, ctx)
if err != nil {
t.Fatal("Match failed:", err, "len:", len(b))
}
v := ctx.Dom()
ret, err := json.Marshal(v)
if err != nil {
t.Fatal("json.Marshal failed:", err)
}
if string(ret) != `[1,2,["foo"]]` {
t.Fatal("ret:", string(ret))
}
}