1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Basic test cases for cgo.
15 #define SHIFT(x, y) ((x)<<(y))
16 #define KILO SHIFT(1, 10)
17 #define UINT32VAL 0xc008427bU
24 typedef unsigned char cgo_uuid_t[20];
26 void uuid_generate(cgo_uuid_t x) {
34 extern enum E myConstFunc(struct S* const ctx, int const id, struct S **const filter);
36 enum E myConstFunc(struct S *const ctx, int const id, struct S **const filter) { return 0; }
43 struct ibv_async_event {
50 xxpthread_mutex_t mutex;
53 int add(int x, int y) {
65 const EINVAL = C.EINVAL /* test #define */
71 C.uuid_generate(&uuid[0])
74 func Strtol(s string, base int) (int, error) {
76 n, err := C.strtol(p, nil, C.int(base))
77 C.free(unsafe.Pointer(p))
81 func Atol(s string) int {
84 C.free(unsafe.Pointer(p))
88 func testConst(t *testing.T) {
89 C.myConstFunc(nil, 0, nil)
92 func testEnum(t *testing.T) {
93 if C.Enum1 != 1 || C.Enum2 != 2 {
94 t.Error("bad enum", C.Enum1, C.Enum2)
98 func testAtol(t *testing.T) {
101 t.Error("Atol 123: ", l)
105 func testErrno(t *testing.T) {
106 p := C.CString("no-such-file")
108 f, err := C.fopen(p, m)
109 C.free(unsafe.Pointer(p))
110 C.free(unsafe.Pointer(m))
113 t.Fatalf("C.fopen: should fail")
115 if err != syscall.ENOENT {
116 t.Fatalf("C.fopen: unexpected error: %v", err)
120 func testMultipleAssign(t *testing.T) {
121 p := C.CString("234")
122 n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10)
123 if runtime.GOOS == "openbsd" {
124 // Bug in OpenBSD strtol(3) - base > 36 succeeds.
125 if (n != 0 && n != 239089) || m != 234 {
126 t.Fatal("Strtol x2: ", n, m)
128 } else if n != 0 || m != 234 {
129 t.Fatal("Strtol x2: ", n, m)
131 C.free(unsafe.Pointer(p))
140 type Context struct {
141 ctx *C.struct_ibv_context
144 func benchCgoCall(b *testing.B) {
147 for i := 0; i < b.N; i++ {
153 func testUnsignedInt(t *testing.T) {
154 a := (int64)(C.UINT32VAL)
155 b := (int64)(0xc008427b)
157 t.Errorf("Incorrect unsigned int - got %x, want %x", a, b)
161 // Static (build-time) test that syntax traversal visits all operands of s[i:j:k].
162 func sliceOperands(array [2000]int) {
163 _ = array[C.KILO:C.KILO:C.KILO] // no type error