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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
package binary
import (
"bytes"
"errors"
"fmt"
"io"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type s0 struct {
A string
B string
C int16
}
var (
s0v = &s0{"A", "B", 1}
s0b = []byte{0x1, 0x41, 0x1, 0x42, 0x1, 0x0}
)
func TestBinaryEncodeStruct(t *testing.T) {
b, err := Marshal(s0v)
assert.NoError(t, err)
assert.Equal(t, s0b, b)
}
func TestBinaryDecodeStruct(t *testing.T) {
s := &s0{}
err := Unmarshal(s0b, s)
assert.NoError(t, err)
assert.Equal(t, s0v, s)
}
func TestBinaryDecodeToValueErrors(t *testing.T) {
b := []byte{1, 0, 0, 0}
var v uint32
err := Unmarshal(b, v)
assert.Error(t, err)
err = Unmarshal(b, &v)
assert.NoError(t, err)
assert.Equal(t, uint32(1), v)
}
type s1 struct {
Name string
BirthDay time.Time
Phone string
Siblings int
Spouse bool
Money float64
Tags map[string]string
Aliases []string
}
var (
s1v = &s1{
Name: "Bob Smith",
BirthDay: time.Date(2013, 1, 2, 3, 4, 5, 6, time.UTC),
Phone: "5551234567",
Siblings: 2,
Spouse: false,
Money: 100.0,
Tags: map[string]string{"key": "value"},
Aliases: []string{"Bobby", "Robert"},
}
svb = []byte{0x9, 0x42, 0x6f, 0x62, 0x20, 0x53, 0x6d, 0x69, 0x74, 0x68, 0xf,
0x1, 0x0, 0x0, 0x0, 0xe, 0xc8, 0x75, 0x9a, 0xa5, 0x0, 0x0, 0x0, 0x6, 0xff,
0xff, 0xa, 0x35, 0x35, 0x35, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x2,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x59,
0x40, 0x1, 0x3, 0x6b, 0x65, 0x79, 0x5, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x2, 0x5,
0x42, 0x6f, 0x62, 0x62, 0x79, 0x6, 0x52, 0x6f, 0x62, 0x65, 0x72, 0x74}
)
func TestBinaryEncodeComplex(t *testing.T) {
b, err := Marshal(s1v)
assert.NoError(t, err)
assert.Equal(t, svb, b)
s := &s1{}
err = Unmarshal(svb, s)
assert.NoError(t, err)
assert.Equal(t, s1v, s)
}
type s2 struct {
b []byte
}
func (s *s2) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return errors.New("expected data to be length 1")
}
s.b = data
return nil
}
func (s *s2) MarshalBinary() (data []byte, err error) {
return s.b, nil
}
func TestBinaryMarshalUnMarshaler(t *testing.T) {
s2v := &s2{[]byte{0x13}}
b, err := Marshal(s2v)
assert.NoError(t, err)
assert.Equal(t, []byte{0x1, 0x13}, b)
}
func TestMarshalUnMarshalTypeAliases(t *testing.T) {
type Foo int64
f := Foo(32)
b, err := Marshal(f)
assert.NoError(t, err)
assert.Equal(t, []byte{0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, b)
}
func TestStructWithStruct(t *testing.T) {
type T1 struct {
ID uint64
Name string
Slice []int
}
type T2 uint64
type Struct struct {
V1 T1
V2 T2
V3 T1
}
s := Struct{V1: T1{1, "1", []int{1}}, V2: 2, V3: T1{3, "3", []int{3}}}
buf := new(bytes.Buffer)
enc := NewEncoder(buf)
err := enc.Encode(&s)
if err != nil {
t.Fatalf("error: %v\n", err)
}
v := Struct{}
dec := NewDecoder(buf)
err = dec.Decode(&v)
if err != nil {
t.Fatalf("error: %v\n", err)
}
if !reflect.DeepEqual(s, v) {
t.Fatalf("got= %#v\nwant=%#v\n", v, s)
}
}
func TestStructWithEmbeddedStruct(t *testing.T) {
type T1 struct {
ID uint64
Name string
Slice []int
}
type T2 uint64
type Struct struct {
T1
V2 T2
V3 T1
}
s := Struct{T1: T1{1, "1", []int{1}}, V2: 2, V3: T1{3, "3", []int{3}}}
buf := new(bytes.Buffer)
enc := NewEncoder(buf)
err := enc.Encode(&s)
if err != nil {
t.Fatalf("error: %v\n", err)
}
v := Struct{}
dec := NewDecoder(buf)
err = dec.Decode(&v)
if err != nil {
t.Fatalf("error: %v\n", err)
}
if !reflect.DeepEqual(s, v) {
t.Fatalf("got= %#v\nwant=%#v\n", v, s)
}
}
func TestArrayOfStructWithStruct(t *testing.T) {
type T1 struct {
ID uint64
Name string
Slice []int
}
type T2 uint64
type Struct struct {
V1 T1
V2 T2
V3 T1
}
s := [1]Struct{
{V1: T1{1, "1", []int{1}}, V2: 2, V3: T1{3, "3", []int{3}}},
}
buf := new(bytes.Buffer)
enc := NewEncoder(buf)
err := enc.Encode(&s)
if err != nil {
t.Fatalf("error: %v\n", err)
}
v := [1]Struct{}
dec := NewDecoder(buf)
err = dec.Decode(&v)
if err != nil {
t.Fatalf("error: %v\n", err)
}
if !reflect.DeepEqual(s, v) {
t.Fatalf("got= %#v\nwant=%#v\n", v, s)
}
}
func TestSliceOfStructWithStruct(t *testing.T) {
type T1 struct {
ID uint64
Name string
Slice []int
}
type T2 uint64
type Struct struct {
V1 T1
V2 T2
V3 T1
}
s := []Struct{
{V1: T1{1, "1", []int{1}}, V2: 2, V3: T1{3, "3", []int{3}}},
}
buf := new(bytes.Buffer)
enc := NewEncoder(buf)
err := enc.Encode(&s)
if err != nil {
t.Fatalf("error: %v\n", err)
}
v := []Struct{}
dec := NewDecoder(buf)
err = dec.Decode(&v)
if err != nil {
t.Fatalf("error: %v\n", err)
}
if !reflect.DeepEqual(s, v) {
t.Fatalf("got= %#v\nwant=%#v\n", v, s)
}
}
func TestMarshalNonPointer(t *testing.T) {
type S struct {
A int
}
s := S{A: 1}
data, err := Marshal(s)
if err != nil {
t.Fatal(err)
}
var res S
if err := Unmarshal(data, &res); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(res, s) {
t.Fatalf("expect %v got %v", s, res)
}
}
func BenchmarkEncodeStructI1(b *testing.B) {
type Struct struct {
S struct {
I int64
}
}
var s Struct
s.S.I = 1024
buf := new(bytes.Buffer)
b.ResetTimer()
for i := 0; i < b.N; i++ {
enc := NewEncoder(buf)
_ = enc.Encode(&s)
}
}
func BenchmarkEncodeStructI2(b *testing.B) {
type Struct struct {
S struct {
I int64
}
}
var s Struct
s.S.I = 1024
buf := new(bytes.Buffer)
enc := NewEncoder(buf)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = enc.Encode(&s)
}
}
func BenchmarkEncodeStructI3(b *testing.B) {
type Struct struct {
S struct {
I int64
}
}
var s Struct
s.S.I = 1024
buf := new(bytes.Buffer)
enc := NewEncoder(buf)
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := enc.Encode(&s)
if err != nil {
b.Fatalf("error: %v\n", err)
}
}
}
type bufferT struct {
buf []byte
}
func (buf *bufferT) Read(data []byte) (int, error) {
n := copy(data, buf.buf)
if n != len(data) {
panic(fmt.Errorf("read too few bytes. got=%d want=%d", n, len(data)))
}
return n, nil
}
func getTestBuffer(b *testing.B) io.Reader {
return &bufferT{
buf: []byte{0, 4, 0, 0, 0, 0, 0, 0},
}
}
func BenchmarkDecodeStructI1(b *testing.B) {
type Struct struct {
I int64
}
var s Struct
buf := getTestBuffer(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
dec := NewDecoder(buf)
_ = dec.Decode(&s)
}
}
func BenchmarkDecodeStructI2(b *testing.B) {
type Struct struct {
S struct {
I int64
}
}
var s Struct
buf := getTestBuffer(b)
dec := NewDecoder(buf)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = dec.Decode(&s)
}
}
func BenchmarkDecodeStructI3(b *testing.B) {
type Struct struct {
S struct {
I int64
}
}
var s Struct
buf := getTestBuffer(b)
dec := NewDecoder(buf)
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := dec.Decode(&s)
if err != nil {
b.Fatalf("error: %v\n", err)
}
}
}
|