Esempio n. 1
0
func TestDeserialize(t *testing.T) {
	source := getTestData()
	reader := bytes.NewReader(source)
	var msg example_msgs.AllFieldTypes
	err := msg.Deserialize(reader)
	if err != nil {
		t.Errorf("failed to deserialize message: %s", err)
	}

	if msg.H.Seq != 0x89ABCDEF {
		t.Errorf("msg.H.Seq incorrect; got=%+v", msg.H.Seq)
	}
	if msg.H.Stamp.Sec != 0x89ABCDEF || msg.H.Stamp.NSec != 0x01234567 {
		t.Errorf("msg.H.Stamp incorrect; got=%+v", msg.H.Stamp)
	}
	if msg.H.FrameID != "frame_id" {
		t.Errorf("msg.H.FrameID incorrect; got=%+v", msg.H.FrameID)
	}
	if msg.B != 0x01 {
		t.Errorf("msg.B incorrect; got=%+v", msg.B)
	}
	if msg.I8 != 0x01 {
		t.Errorf("msg.I8 incorrect; got=%+v", msg.I8)
	}
	if msg.I16 != 0x0123 {
		t.Errorf("msg.I16 incorrect; got=%+v", msg.I16)
	}
	if msg.I32 != 0x01234567 {
		t.Errorf("msg.I32 incorrect; got=%+v", msg.I32)
	}
	if msg.I64 != 0x0123456789ABCDEF {
		t.Errorf("msg.I64 incorrect; got=%+v", msg.I64)
	}
	if msg.U8 != 0x01 {
		t.Errorf("msg.U8 incorrect; got=%+v", msg.U8)
	}
	if msg.U16 != 0x0123 {
		t.Errorf("msg.U16 incorrect; got=%+v", msg.U16)
	}
	if msg.U32 != 0x01234567 {
		t.Errorf("msg.U32 incorrect; got=%+v", msg.U32)
	}
	if msg.U64 != 0x0123456789ABCDEF {
		t.Errorf("msg.U64 incorrect; got=%+v", msg.U64)
	}
	if msg.F32 != 3.141592653589793238462643383 {
		t.Errorf("msg.F32 incorrect; got=%+v", msg.F32)
	}
	if msg.F64 != 3.1415926535897932384626433832795028842 {
		t.Errorf("msg.F64 incorrect; got=%+v", msg.F64)
	}
	if msg.T.Sec != 0x89ABCDEF || msg.T.NSec != 0x01234567 {
		t.Errorf("msg.T incorrect; got=%+v", msg.T)
	}
	if msg.D.Sec != 0x89ABCDEF || msg.D.NSec != 0x01234567 {
		t.Errorf("msg.D incorrect; got=%+v", msg.D)
	}
	if msg.S != "Hello, world!" {
		t.Errorf("msg.S incorrect; got=%+v", msg.S)
	}
	if msg.C.R != 1.0 || msg.C.G != 0.5 || msg.C.B != 0.25 || msg.C.A != 0.125 {
		t.Errorf("msg.C incorrect; got=%+v", msg.C)
	}
	if msg.DynAry[0] != 0x01234567 || msg.DynAry[1] != 0x89ABCDEF {
		t.Errorf("msg.DynAry incorrect: got=%+v", msg.DynAry)
	}
	if msg.FixAry[0] != 0x01234567 || msg.FixAry[1] != 0x89ABCDEF {
		t.Errorf("msg.FixAry incorrect: got=%+v", msg.FixAry)
	}
	if reader.Len() != 0 {
		t.Errorf("reader has data remaining: %d", reader.Len())
	}
}