1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package model
- import (
- "testing"
- )
- func TestMarshalAndUnmarshal(t *testing.T) {
- var a = Ints{1, 2, 3}
- data := make([]byte, a.Size())
- n, err := a.MarshalTo(data)
- if n != 24 {
- t.Logf("marshal size must be 24")
- t.FailNow()
- }
- if err != nil {
- t.Fatalf("err:%v", err)
- }
- var b Ints
- err = b.Unmarshal(data)
- if err != nil {
- t.Fatalf("err:%v", err)
- }
- if b[0] != 1 || b[1] != 2 || b[2] != 3 {
- t.Logf("unmarshal failed!b:%v", b)
- t.FailNow()
- }
- }
- func TestUncompleteMarshal(t *testing.T) {
- var a = Ints{1, 2, 3}
- data := make([]byte, a.Size()-7)
- n, err := a.MarshalTo(data)
- if n != 16 {
- t.Logf("marshal size must be 16")
- t.FailNow()
- }
- if err != nil {
- t.Fatalf("err:%v", err)
- }
- var b Ints
- err = b.Unmarshal(data)
- if err != nil {
- t.Fatalf("err:%v", err)
- }
- if b[0] != 1 || b[1] != 2 {
- t.Logf("unmarshal failed!b:%v", b)
- t.FailNow()
- }
- }
- func TestNilMarshal(t *testing.T) {
- var a = Ints{1, 2, 3}
- var data []byte
- n, err := a.MarshalTo(data)
- if n != 0 {
- t.Logf("marshal size must be 0")
- t.FailNow()
- }
- if err != nil {
- t.Fatalf("err:%v", err)
- }
- var b Ints
- err = b.Unmarshal(data)
- if err != nil {
- t.Fatalf("err:%v", err)
- }
- if b != nil {
- t.Logf("unmarshal failed!b:%v", b)
- t.FailNow()
- }
- }
|