Exemplo n.º 1
0
func TestNewPointerType(t *testing.T) {
	s_t, err := ffi.NewStructType("s_0", []ffi.Field{{"a", ffi.C_int32}})
	if err != nil {
		t.Errorf(err.Error())
	}

	p_s_t, err := ffi.NewPointerType(s_t)
	if err != nil {
		t.Errorf(err.Error())
	}

	for _, table := range []struct {
		name string
		elem ffi.Type
	}{
		{"int8*", ffi.C_int8},
		{"int16*", ffi.C_int16},
		{"int32*", ffi.C_int32},
		{"int64*", ffi.C_int64},
		{"uint8*", ffi.C_uint8},
		{"uint16*", ffi.C_uint16},
		{"uint32*", ffi.C_uint32},
		{"uint64*", ffi.C_uint64},

		{"float*", ffi.C_float},
		{"double*", ffi.C_double},

		{"s_0*", s_t},
		{"s_0**", p_s_t},
	} {
		typ, err := ffi.NewPointerType(table.elem)
		if err != nil {
			t.Errorf(err.Error())
		}
		eq(t, table.name, typ.Name())
		eq(t, table.elem, typ.Elem())
		eq(t, ffi.C_pointer.Size(), typ.Size())
		eq(t, ffi.Ptr, typ.Kind())
	}
}
Exemplo n.º 2
0
func TestNewSliceType(t *testing.T) {

	capSize := 2 * unsafe.Sizeof(reflect.SliceHeader{}.Cap)

	s_t, err := ffi.NewStructType("s_0", []ffi.Field{{"a", ffi.C_int32}})
	if err != nil {
		t.Errorf(err.Error())
	}

	p_s_t, err := ffi.NewPointerType(s_t)
	if err != nil {
		t.Errorf(err.Error())
	}

	for _, table := range []struct {
		name string
		elem ffi.Type
	}{
		{"uint8[]", ffi.C_uint8},
		{"uint16[]", ffi.C_uint16},
		{"uint32[]", ffi.C_uint32},
		{"uint64[]", ffi.C_uint64},
		{"int8[]", ffi.C_int8},
		{"int16[]", ffi.C_int16},
		{"int32[]", ffi.C_int32},
		{"int64[]", ffi.C_int64},

		{"float[]", ffi.C_float},
		{"double[]", ffi.C_double},

		{"s_0[]", s_t},
		{"s_0*[]", p_s_t},
	} {
		typ, err := ffi.NewSliceType(table.elem)
		if err != nil {
			t.Errorf(err.Error())
		}
		eq(t, table.name, typ.Name())
		eq(t, table.elem, typ.Elem())
		eq(t, capSize+ffi.C_pointer.Size(), typ.Size())
		//eq(t, table.n, typ.Len())
		eq(t, ffi.Slice, typ.Kind())
	}
}
Exemplo n.º 3
0
func TestNewArrayType(t *testing.T) {

	s_t, err := ffi.NewStructType("s_0", []ffi.Field{{"a", ffi.C_int32}})
	if err != nil {
		t.Errorf(err.Error())
	}

	p_s_t, err := ffi.NewPointerType(s_t)
	if err != nil {
		t.Errorf(err.Error())
	}

	for _, table := range []struct {
		name string
		n    int
		elem ffi.Type
	}{
		{"uint8[10]", 10, ffi.C_uint8},
		{"uint16[10]", 10, ffi.C_uint16},
		{"uint32[10]", 10, ffi.C_uint32},
		{"uint64[10]", 10, ffi.C_uint64},
		{"int8[10]", 10, ffi.C_int8},
		{"int16[10]", 10, ffi.C_int16},
		{"int32[10]", 10, ffi.C_int32},
		{"int64[10]", 10, ffi.C_int64},

		{"float[10]", 10, ffi.C_float},
		{"double[10]", 10, ffi.C_double},

		{"s_0[10]", 10, s_t},
		{"s_0*[10]", 10, p_s_t},
	} {
		typ, err := ffi.NewArrayType(table.n, table.elem)
		if err != nil {
			t.Errorf(err.Error())
		}
		eq(t, table.name, typ.Name())
		eq(t, table.elem, typ.Elem())
		eq(t, uintptr(table.n)*table.elem.Size(), typ.Size())
		eq(t, table.n, typ.Len())
		eq(t, ffi.Array, typ.Kind())
	}
}