Exemplo n.º 1
0
func BenchmarkMojomToVdlTranscoding(b *testing.B) {
	data := mojomBytesCustomer()
	t := vdl.TypeOf(customer)
	for i := 0; i < b.N; i++ {
		var c Customer
		transcoder.ValueFromMojo(&c, data, t)
	}
}
Exemplo n.º 2
0
// __VDLInit performs vdl initialization.  It is safe to call multiple times.
// If you have an init ordering issue, just insert the following line verbatim
// into your source files in this package, right after the "package foo" clause:
//
//    var _ = __VDLInit()
//
// The purpose of this function is to ensure that vdl initialization occurs in
// the right order, and very early in the init sequence.  In particular, vdl
// registration and package variable initialization needs to occur before
// functions like vdl.TypeOf will work properly.
//
// This function returns a dummy value, so that it can be used to initialize the
// first var in the file, to take advantage of Go's defined init order.
func __VDLInit() struct{} {
	if __VDLInitCalled {
		return struct{}{}
	}
	__VDLInitCalled = true

	// Register types.
	vdl.Register((*ComplexErrorParam)(nil))

	// Initialize type definitions.
	__VDLType_struct_1 = vdl.TypeOf((*ComplexErrorParam)(nil)).Elem()
	__VDLType_list_2 = vdl.TypeOf((*[]uint32)(nil))

	// Set error format strings.
	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(ErrNoFortunes.ID), "{1:}{2:} no fortunes added")
	i18n.Cat().SetWithBase(i18n.LangID("en"), i18n.MsgID(ErrComplex.ID), "{1:}{2:} this is a complex error with params {3} {4} {5}")

	return struct{}{}
}
Exemplo n.º 3
0
// __VDLInit performs vdl initialization.  It is safe to call multiple times.
// If you have an init ordering issue, just insert the following line verbatim
// into your source files in this package, right after the "package foo" clause:
//
//    var _ = __VDLInit()
//
// The purpose of this function is to ensure that vdl initialization occurs in
// the right order, and very early in the init sequence.  In particular, vdl
// registration and package variable initialization needs to occur before
// functions like vdl.TypeOf will work properly.
//
// This function returns a dummy value, so that it can be used to initialize the
// first var in the file, to take advantage of Go's defined init order.
func __VDLInit() struct{} {
	if __VDLInitCalled {
		return struct{}{}
	}
	__VDLInitCalled = true

	// Register types.
	vdl.Register((*LockStatus)(nil))

	// Initialize type definitions.
	__VDLType_int32_1 = vdl.TypeOf((*LockStatus)(nil))

	return struct{}{}
}
Exemplo n.º 4
0
func TestVomToMojoToVom(t *testing.T) {
	for _, test := range testCases {
		testName := test.Name + " vom->mojo->vom"

		data, err := transcoder.ToMojom(test.VdlValue)
		if err != nil {
			t.Errorf("%s: error in VomToMojo: %v", testName, err)
			continue
		}

		var out interface{}
		if err := transcoder.ValueFromMojo(&out, data, vdl.TypeOf(test.VdlValue)); err != nil {
			t.Errorf("%s: error in MojoToVom: %v (was transcoding from %x)", testName, err, data)
			continue
		}

		if got, want := out, test.VdlValue; !reflect.DeepEqual(got, want) {
			t.Errorf("%s: result doesn't match expectation. got %#v, but want %#v", testName, got, want)
		}
	}
}
Exemplo n.º 5
0
func TestComputeStructLayout(t *testing.T) {
	testCases := []struct {
		t      *vdl.Type
		layout structLayout
	}{
		{
			vdl.TypeOf(struct {
				A uint32
			}{}),
			structLayout{
				structLayoutField{0, 0, 0},
			},
		},
		{
			vdl.TypeOf(struct {
				A uint32
				B string
			}{}),
			structLayout{
				structLayoutField{0, 0, 0},
				structLayoutField{1, 8, 0},
			},
		},
		{
			vdl.TypeOf(struct {
				A uint32
				B string
				C uint32
			}{}),
			structLayout{
				structLayoutField{0, 0, 0},
				structLayoutField{2, 4, 0},
				structLayoutField{1, 8, 0},
			},
		},
		{
			vdl.TypeOf(struct {
				A uint32
				B string
				C bool
				D float32
			}{}),
			structLayout{
				structLayoutField{0, 0, 0},
				structLayoutField{2, 4, 0},
				structLayoutField{1, 8, 0},
				structLayoutField{3, 16, 0},
			},
		},
		{
			vdl.TypeOf(struct {
				A uint32
				C string
				B float32
				D bool
			}{}),
			structLayout{
				structLayoutField{0, 0, 0},
				structLayoutField{2, 4, 0},
				structLayoutField{1, 8, 0},
				structLayoutField{3, 16, 0},
			},
		},
		{
			vdl.TypeOf(struct {
				A uint16
				B bool
				C string
				D float32
				E bool
			}{}),
			structLayout{
				structLayoutField{0, 0, 0},
				structLayoutField{1, 2, 0},
				structLayoutField{4, 2, 1},
				structLayoutField{3, 4, 0},
				structLayoutField{2, 8, 0},
			},
		},
	}

	for _, test := range testCases {
		layout := computeStructLayout(test.t)
		if got, want := layout, test.layout; !reflect.DeepEqual(got, want) {
			t.Errorf("struct layout for type %v was %v but %v was expected", test.t, got, want)
		}
		for _, o := range test.layout {
			byteOffset, bitOffset := layout.MojoOffsetsFromVdlIndex(o.vdlStructIndex)
			if got, want := byteOffset, o.byteOffset; got != want {
				t.Errorf("byte offset doesn't match. got %v, want %v", got, want)
			}
			if got, want := bitOffset, o.bitOffset; got != want {
				t.Errorf("bit offset doesn't match. got %v, want %v", got, want)
			}
		}
	}
}