cgo: add tests for most C types
Этот коммит содержится в:
родитель
26bdfa9c84
коммит
be7529b261
3 изменённых файлов: 167 добавлений и 1 удалений
|
@ -15,7 +15,7 @@ import (
|
||||||
func TestCGo(t *testing.T) {
|
func TestCGo(t *testing.T) {
|
||||||
var cflags = []string{"--target=armv6m-none-eabi"}
|
var cflags = []string{"--target=armv6m-none-eabi"}
|
||||||
|
|
||||||
for _, name := range []string{"basic"} {
|
for _, name := range []string{"basic", "types"} {
|
||||||
name := name // avoid a race condition
|
name := name // avoid a race condition
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
91
cgo/testdata/types.go
предоставленный
Обычный файл
91
cgo/testdata/types.go
предоставленный
Обычный файл
|
@ -0,0 +1,91 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Simple typedef.
|
||||||
|
typedef int myint;
|
||||||
|
|
||||||
|
// Structs, with or without name.
|
||||||
|
typedef struct {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
} point2d_t;
|
||||||
|
typedef struct point3d {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int z;
|
||||||
|
} point3d_t;
|
||||||
|
|
||||||
|
// Enums. These define constant numbers. All these constants must be given the
|
||||||
|
// correct number.
|
||||||
|
typedef enum option {
|
||||||
|
optionA,
|
||||||
|
optionB,
|
||||||
|
optionC = -5,
|
||||||
|
optionD,
|
||||||
|
optionE = 10,
|
||||||
|
optionF,
|
||||||
|
optionG,
|
||||||
|
} option_t;
|
||||||
|
|
||||||
|
// Anonymous enum.
|
||||||
|
typedef enum {
|
||||||
|
option2A = 20,
|
||||||
|
} option2_t;
|
||||||
|
|
||||||
|
// Various types that are usually translated directly to Go types, but storing
|
||||||
|
// them in a struct reveals them.
|
||||||
|
typedef struct {
|
||||||
|
float f;
|
||||||
|
double d;
|
||||||
|
int *ptr;
|
||||||
|
} types_t;
|
||||||
|
|
||||||
|
// Arrays.
|
||||||
|
typedef int myIntArray[10];
|
||||||
|
|
||||||
|
// Bitfields.
|
||||||
|
typedef struct {
|
||||||
|
unsigned char start;
|
||||||
|
unsigned char a : 5;
|
||||||
|
unsigned char b : 1;
|
||||||
|
unsigned char c : 2;
|
||||||
|
unsigned char :0; // new field
|
||||||
|
unsigned char d : 6;
|
||||||
|
unsigned char e : 3;
|
||||||
|
// Note that C++ allows bitfields bigger than the underlying type.
|
||||||
|
} bitfield_t;
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Simple typedefs.
|
||||||
|
_ C.myint
|
||||||
|
|
||||||
|
// Structs.
|
||||||
|
_ C.point2d_t
|
||||||
|
_ C.point3d_t
|
||||||
|
_ C.struct_point3d
|
||||||
|
|
||||||
|
// Enums (anonymous and named).
|
||||||
|
_ C.option_t
|
||||||
|
_ C.enum_option
|
||||||
|
_ C.option2_t
|
||||||
|
|
||||||
|
// Various types.
|
||||||
|
_ C.types_t
|
||||||
|
|
||||||
|
// Arrays.
|
||||||
|
_ C.myIntArray
|
||||||
|
_ C.myIntArrayPtr
|
||||||
|
)
|
||||||
|
|
||||||
|
// Test bitfield accesses.
|
||||||
|
func foo() {
|
||||||
|
var x C.bitfield_t
|
||||||
|
x.start = 3
|
||||||
|
x.a = 4
|
||||||
|
x.b = 1
|
||||||
|
x.c = 2
|
||||||
|
x.d = 10
|
||||||
|
x.e = 5
|
||||||
|
}
|
75
cgo/testdata/types.out.go
предоставленный
Обычный файл
75
cgo/testdata/types.out.go
предоставленный
Обычный файл
|
@ -0,0 +1,75 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "unsafe"
|
||||||
|
|
||||||
|
const C.option2A = 20
|
||||||
|
const C.optionA = 0
|
||||||
|
const C.optionB = 1
|
||||||
|
const C.optionC = -5
|
||||||
|
const C.optionD = -4
|
||||||
|
const C.optionE = 10
|
||||||
|
const C.optionF = 11
|
||||||
|
const C.optionG = 12
|
||||||
|
|
||||||
|
type C.int16_t = int16
|
||||||
|
type C.int32_t = int32
|
||||||
|
type C.int64_t = int64
|
||||||
|
type C.int8_t = int8
|
||||||
|
type C.uint16_t = uint16
|
||||||
|
type C.uint32_t = uint32
|
||||||
|
type C.uint64_t = uint64
|
||||||
|
type C.uint8_t = uint8
|
||||||
|
type C.uintptr_t = uintptr
|
||||||
|
type C.char uint8
|
||||||
|
type C.int int32
|
||||||
|
type C.long int32
|
||||||
|
type C.longlong int64
|
||||||
|
type C.schar int8
|
||||||
|
type C.short int16
|
||||||
|
type C.uchar uint8
|
||||||
|
type C.uint uint32
|
||||||
|
type C.ulong uint32
|
||||||
|
type C.ulonglong uint64
|
||||||
|
type C.ushort uint16
|
||||||
|
type C.bitfield_t = C.struct_1
|
||||||
|
type C.myIntArray = [10]C.int
|
||||||
|
type C.myint = C.int
|
||||||
|
type C.option2_t = C.uint
|
||||||
|
type C.option_t = C.enum_option
|
||||||
|
type C.point2d_t = struct {
|
||||||
|
x C.int
|
||||||
|
y C.int
|
||||||
|
}
|
||||||
|
type C.point3d_t = C.struct_point3d
|
||||||
|
type C.types_t = struct {
|
||||||
|
f float32
|
||||||
|
d float64
|
||||||
|
ptr *C.int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *C.struct_1) bitfield_a() C.uchar { return s.__bitfield_1 & 0x1f }
|
||||||
|
func (s *C.struct_1) set_bitfield_a(value C.uchar) { s.__bitfield_1 = s.__bitfield_1&^0x1f | value&0x1f<<0 }
|
||||||
|
func (s *C.struct_1) bitfield_b() C.uchar {
|
||||||
|
return s.__bitfield_1 >> 5 & 0x1
|
||||||
|
}
|
||||||
|
func (s *C.struct_1) set_bitfield_b(value C.uchar) { s.__bitfield_1 = s.__bitfield_1&^0x20 | value&0x1<<5 }
|
||||||
|
func (s *C.struct_1) bitfield_c() C.uchar {
|
||||||
|
return s.__bitfield_1 >> 6
|
||||||
|
}
|
||||||
|
func (s *C.struct_1) set_bitfield_c(value C.uchar,
|
||||||
|
|
||||||
|
) { s.__bitfield_1 = s.__bitfield_1&0x3f | value<<6 }
|
||||||
|
|
||||||
|
type C.struct_1 struct {
|
||||||
|
start C.uchar
|
||||||
|
__bitfield_1 C.uchar
|
||||||
|
|
||||||
|
d C.uchar
|
||||||
|
e C.uchar
|
||||||
|
}
|
||||||
|
type C.struct_point3d struct {
|
||||||
|
x C.int
|
||||||
|
y C.int
|
||||||
|
z C.int
|
||||||
|
}
|
||||||
|
type C.enum_option C.int
|
Загрузка…
Создание таблицы
Сослаться в новой задаче