Example #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())
	}
}
Example #2
0
func TestSerialize(t *testing.T) {
	var msg example_msgs.AllFieldTypes

	msg.H.Seq = 0x89ABCDEF
	msg.H.Stamp = ros.NewTime(0x89ABCDEF, 0x01234567)
	msg.H.FrameID = "frame_id"
	msg.B = 0x01
	msg.I8 = 0x01
	msg.I16 = 0x0123
	msg.I32 = 0x01234567
	msg.I64 = 0x0123456789ABCDEF
	msg.U8 = 0x01
	msg.U16 = 0x0123
	msg.U32 = 0x01234567
	msg.U64 = 0x0123456789ABCDEF
	msg.F32 = 3.141592653589793238462643383
	msg.F64 = 3.1415926535897932384626433832795028842
	msg.T = ros.NewTime(0x89ABCDEF, 0x01234567)
	msg.D = ros.NewDuration(0x89ABCDEF, 0x01234567)
	msg.S = "Hello, world!"
	msg.C = std_msgs.ColorRGBA{R: 1.0, G: 0.5, B: 0.25, A: 0.125}

	msg.DynAry = append(msg.DynAry, 0x01234567)
	msg.DynAry = append(msg.DynAry, 0x89ABCDEF)
	msg.FixAry[0] = 0x01234567
	msg.FixAry[1] = 0x89ABCDEF

	var buf bytes.Buffer
	err := msg.Serialize(&buf)
	if err != nil {
		t.Error(err)
	}

	result := buf.Bytes()
	expected := getTestData()
	CheckBytes(t, expected, result)
}