1
2
3
4
5
6
7
8
9
10
11 package cgotest
12
13
937 import "C"
938
939 import (
940 "context"
941 "fmt"
942 "math"
943 "math/rand"
944 "os"
945 "os/signal"
946 "reflect"
947 "runtime"
948 "runtime/cgo"
949 "sync"
950 "syscall"
951 "testing"
952 "time"
953 "unsafe"
954 )
955
956
957
958 func testAlign(t *testing.T) {
959 var evt C.SDL_KeyboardEvent
960 C.makeEvent(&evt)
961 if C.same(&evt, evt.typ, evt.which, evt.state, evt.keysym.scancode, evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) == 0 {
962 t.Error("*** bad alignment")
963 C.cTest(&evt)
964 t.Errorf("Go: %#x %#x %#x %#x %#x %#x %#x\n",
965 evt.typ, evt.which, evt.state, evt.keysym.scancode,
966 evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode)
967 t.Error(evt)
968 }
969 }
970
971
972
973 const greeting = "hello, world"
974
975 type testPair struct {
976 Name string
977 Got, Want interface{}
978 }
979
980 var testPairs = []testPair{
981 {"GoString", C.GoString(C.greeting), greeting},
982 {"GoStringN", C.GoStringN(C.greeting, 5), greeting[:5]},
983 {"GoBytes", C.GoBytes(unsafe.Pointer(C.greeting), 5), []byte(greeting[:5])},
984 }
985
986 func testHelpers(t *testing.T) {
987 for _, pair := range testPairs {
988 if !reflect.DeepEqual(pair.Got, pair.Want) {
989 t.Errorf("%s: got %#v, want %#v", pair.Name, pair.Got, pair.Want)
990 }
991 }
992 }
993
994
995
996 const EINVAL = C.EINVAL
997
998 var KILO = C.KILO
999
1000 func uuidgen() {
1001 var uuid C.cgo_uuid_t
1002 C.uuid_generate(&uuid[0])
1003 }
1004
1005 func Strtol(s string, base int) (int, error) {
1006 p := C.CString(s)
1007 n, err := C.strtol(p, nil, C.int(base))
1008 C.free(unsafe.Pointer(p))
1009 return int(n), err
1010 }
1011
1012 func Atol(s string) int {
1013 p := C.CString(s)
1014 n := C.atol(p)
1015 C.free(unsafe.Pointer(p))
1016 return int(n)
1017 }
1018
1019 func testConst(t *testing.T) {
1020 C.myConstFunc(nil, 0, nil)
1021 }
1022
1023 func testEnum(t *testing.T) {
1024 if C.Enum1 != 1 || C.Enum2 != 2 {
1025 t.Error("bad enum", C.Enum1, C.Enum2)
1026 }
1027 }
1028
1029 func testNamedEnum(t *testing.T) {
1030 e := new(C.enum_E)
1031
1032 *e = C.Enum1
1033 if *e != 1 {
1034 t.Error("bad enum", C.Enum1)
1035 }
1036
1037 *e = C.Enum2
1038 if *e != 2 {
1039 t.Error("bad enum", C.Enum2)
1040 }
1041 }
1042
1043 func testCastToEnum(t *testing.T) {
1044 e := C.enum_E(C.Enum1)
1045 if e != 1 {
1046 t.Error("bad enum", C.Enum1)
1047 }
1048
1049 e = C.enum_E(C.Enum2)
1050 if e != 2 {
1051 t.Error("bad enum", C.Enum2)
1052 }
1053 }
1054
1055 func testAtol(t *testing.T) {
1056 l := Atol("123")
1057 if l != 123 {
1058 t.Error("Atol 123: ", l)
1059 }
1060 }
1061
1062 func testErrno(t *testing.T) {
1063 p := C.CString("no-such-file")
1064 m := C.CString("r")
1065 f, err := C.fopen(p, m)
1066 C.free(unsafe.Pointer(p))
1067 C.free(unsafe.Pointer(m))
1068 if err == nil {
1069 C.fclose(f)
1070 t.Fatalf("C.fopen: should fail")
1071 }
1072 if err != syscall.ENOENT {
1073 t.Fatalf("C.fopen: unexpected error: %v", err)
1074 }
1075 }
1076
1077 func testMultipleAssign(t *testing.T) {
1078 p := C.CString("234")
1079 n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10)
1080 if runtime.GOOS == "openbsd" {
1081
1082 if (n != 0 && n != 239089) || m != 234 {
1083 t.Fatal("Strtol x2: ", n, m)
1084 }
1085 } else if n != 0 || m != 234 {
1086 t.Fatal("Strtol x2: ", n, m)
1087 }
1088 C.free(unsafe.Pointer(p))
1089 }
1090
1091 var (
1092 cuint = (C.uint)(0)
1093 culong C.ulong
1094 cchar C.char
1095 )
1096
1097 type Context struct {
1098 ctx *C.struct_ibv_context
1099 }
1100
1101 func benchCgoCall(b *testing.B) {
1102 b.Run("add-int", func(b *testing.B) {
1103 const x = C.int(2)
1104 const y = C.int(3)
1105
1106 for i := 0; i < b.N; i++ {
1107 C.add(x, y)
1108 }
1109 })
1110
1111 b.Run("one-pointer", func(b *testing.B) {
1112 var a0 C.VkDeviceCreateInfo
1113 for i := 0; i < b.N; i++ {
1114 C.handleComplexPointer(&a0)
1115 }
1116 })
1117 b.Run("string-pointer-escape", func(b *testing.B) {
1118 for i := 0; i < b.N; i++ {
1119 var s string
1120 C.handleGoStringPointerEscape(unsafe.Pointer(&s))
1121 }
1122 })
1123 b.Run("string-pointer-noescape", func(b *testing.B) {
1124 for i := 0; i < b.N; i++ {
1125 var s string
1126 C.handleGoStringPointerNoescape(unsafe.Pointer(&s))
1127 }
1128 })
1129 b.Run("eight-pointers", func(b *testing.B) {
1130 var a0, a1, a2, a3, a4, a5, a6, a7 C.VkDeviceCreateInfo
1131 for i := 0; i < b.N; i++ {
1132 C.handleComplexPointer8(&a0, &a1, &a2, &a3, &a4, &a5, &a6, &a7)
1133 }
1134 })
1135 b.Run("eight-pointers-nil", func(b *testing.B) {
1136 var a0, a1, a2, a3, a4, a5, a6, a7 *C.VkDeviceCreateInfo
1137 for i := 0; i < b.N; i++ {
1138 C.handleComplexPointer8(a0, a1, a2, a3, a4, a5, a6, a7)
1139 }
1140 })
1141 b.Run("eight-pointers-array", func(b *testing.B) {
1142 var a [8]C.VkDeviceCreateInfo
1143 for i := 0; i < b.N; i++ {
1144 C.handleComplexPointer8(&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7])
1145 }
1146 })
1147 b.Run("eight-pointers-slice", func(b *testing.B) {
1148 a := make([]C.VkDeviceCreateInfo, 8)
1149 for i := 0; i < b.N; i++ {
1150 C.handleComplexPointer8(&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7])
1151 }
1152 })
1153 }
1154
1155
1156 func benchCallback(b *testing.B) {
1157 var x = false
1158 for i := 0; i < b.N; i++ {
1159 nestedCall(func() { x = true })
1160 }
1161 if !x {
1162 b.Fatal("nestedCall was not invoked")
1163 }
1164 }
1165
1166 var sinkString string
1167
1168 func benchGoString(b *testing.B) {
1169 for i := 0; i < b.N; i++ {
1170 sinkString = C.GoString(C.cstr)
1171 }
1172 const want = "abcefghijklmnopqrstuvwxyzABCEFGHIJKLMNOPQRSTUVWXYZ1234567890"
1173 if sinkString != want {
1174 b.Fatalf("%q != %q", sinkString, want)
1175 }
1176 }
1177
1178
1179 func sliceOperands(array [2000]int) {
1180 _ = array[C.KILO:C.KILO:C.KILO]
1181 }
1182
1183
1184 var testThreadLockFunc = func(*testing.T) {}
1185
1186
1187
1188 func TestComplexAlign(t *testing.T) {
1189 if C.cplxAlign.x != 3.14 {
1190 t.Errorf("got %v, expected 3.14", C.cplxAlign.x)
1191 }
1192 if C.cplxAlign.y != 2.17 {
1193 t.Errorf("got %v, expected 2.17", C.cplxAlign.y)
1194 }
1195 }
1196
1197
1198
1199 func testCheckConst(t *testing.T) {
1200
1201 p := C.malloc(C.size_t(unsafe.Sizeof(C.int(0))))
1202 defer C.free(p)
1203 C.CheckConstFunc(&C.CheckConstStruct{(*C.int)(p)}, C.CheckConstVal)
1204 }
1205
1206
1207
1208 func duplicateSymbols() {
1209 fmt.Printf("%v %v %v\n", C.base_symbol, C.alias_one, C.alias_two)
1210 }
1211
1212
1213
1214
1215 func testSetEnv(t *testing.T) {
1216 if runtime.GOOS == "windows" {
1217
1218
1219
1220
1221 t.Logf("skipping test")
1222 return
1223 }
1224 const key = "CGO_OS_TEST_KEY"
1225 const val = "CGO_OS_TEST_VALUE"
1226 os.Setenv(key, val)
1227 keyc := C.CString(key)
1228 defer C.free(unsafe.Pointer(keyc))
1229 v := C.getenv(keyc)
1230 if uintptr(unsafe.Pointer(v)) == 0 {
1231 t.Fatal("getenv returned NULL")
1232 }
1233 vs := C.GoString(v)
1234 if vs != val {
1235 t.Fatalf("getenv() = %q; want %q", vs, val)
1236 }
1237 }
1238
1239
1240
1241 func callBridge(f C.intFunc) int {
1242 return int(C.bridge_int_func(f))
1243 }
1244
1245 func callCBridge(f C.intFunc) C.int {
1246 return C.bridge_int_func(f)
1247 }
1248
1249 func testFpVar(t *testing.T) {
1250 const expected = 42
1251 f := C.intFunc(C.fortytwo)
1252 res1 := C.bridge_int_func(f)
1253 if r1 := int(res1); r1 != expected {
1254 t.Errorf("got %d, want %d", r1, expected)
1255 }
1256 res2 := callCBridge(f)
1257 if r2 := int(res2); r2 != expected {
1258 t.Errorf("got %d, want %d", r2, expected)
1259 }
1260 r3 := callBridge(f)
1261 if r3 != expected {
1262 t.Errorf("got %d, want %d", r3, expected)
1263 }
1264 }
1265
1266
1267 type AsyncEvent struct {
1268 event C.struct_ibv_async_event
1269 }
1270
1271
1272
1273 func test1635(t *testing.T) {
1274 C.scatter()
1275 if v := C.hola; v != 0 {
1276 t.Fatalf("C.hola is %d, should be 0", v)
1277 }
1278 if v := C.testHola(); v != 0 {
1279 t.Fatalf("C.testHola() is %d, should be 0", v)
1280 }
1281 }
1282
1283
1284
1285 func testUnsignedInt(t *testing.T) {
1286 a := (int64)(C.UINT32VAL)
1287 b := (int64)(0xc008427b)
1288 if a != b {
1289 t.Errorf("Incorrect unsigned int - got %x, want %x", a, b)
1290 }
1291 }
1292
1293
1294
1295 func test3250(t *testing.T) {
1296 if runtime.GOOS == "windows" {
1297 t.Skip("not applicable on windows")
1298 }
1299
1300 t.Skip("skipped, see golang.org/issue/5885")
1301 var (
1302 thres = 1
1303 sig = syscall_dot_SIGCHLD
1304 )
1305 type result struct {
1306 n int
1307 sig os.Signal
1308 }
1309 var (
1310 sigCh = make(chan os.Signal, 10)
1311 waitStart = make(chan struct{})
1312 waitDone = make(chan result)
1313 )
1314
1315 signal.Notify(sigCh, sig)
1316
1317 go func() {
1318 n := 0
1319 alarm := time.After(time.Second * 3)
1320 for {
1321 select {
1322 case <-waitStart:
1323 waitStart = nil
1324 case v := <-sigCh:
1325 n++
1326 if v != sig || n > thres {
1327 waitDone <- result{n, v}
1328 return
1329 }
1330 case <-alarm:
1331 waitDone <- result{n, sig}
1332 return
1333 }
1334 }
1335 }()
1336
1337 waitStart <- struct{}{}
1338 C.testSendSIG()
1339 r := <-waitDone
1340 if r.sig != sig {
1341 t.Fatalf("received signal %v, but want %v", r.sig, sig)
1342 }
1343 t.Logf("got %d signals\n", r.n)
1344 if r.n <= thres {
1345 t.Fatalf("expected more than %d", thres)
1346 }
1347 }
1348
1349
1350
1351 func testLibgcc(t *testing.T) {
1352 var table = []struct {
1353 in, out C.int
1354 }{
1355 {0, 0},
1356 {1, 1},
1357 {-42, 42},
1358 {1000300, 1000300},
1359 {1 - 1<<31, 1<<31 - 1},
1360 }
1361 for _, v := range table {
1362 if o := C.vabs(v.in); o != v.out {
1363 t.Fatalf("abs(%d) got %d, should be %d", v.in, o, v.out)
1364 return
1365 }
1366 }
1367 }
1368
1369
1370
1371 func test3729(t *testing.T) {
1372 if runtime.GOOS == "windows" {
1373 t.Skip("skipping on windows")
1374 }
1375
1376 _, e := C.g()
1377 if e != syscall.E2BIG {
1378 t.Errorf("got %q, expect %q", e, syscall.E2BIG)
1379 }
1380 _, e = C.g2(C.EINVAL, C._expA, C._expB, C._expC, C._expD)
1381 if e != syscall.EINVAL {
1382 t.Errorf("got %q, expect %q", e, syscall.EINVAL)
1383 }
1384 }
1385
1386
1387
1388 func testPrintf(t *testing.T) {
1389 C.say()
1390 }
1391
1392
1393
1394 var issue4054a = []int{C.A, C.B, C.C, C.D, C.E, C.F, C.G, C.H, C.I, C.J}
1395
1396
1397
1398 func test4339(t *testing.T) {
1399 C.handle4339(&C.exported4339)
1400 }
1401
1402
1403
1404 func testBoolAlign(t *testing.T) {
1405 b := C.c_bool(true, true, 10, true, false)
1406 if b != 10 {
1407 t.Fatalf("found %d expected 10\n", b)
1408 }
1409 b = C.c_bool(true, true, 5, true, true)
1410 if b != 5 {
1411 t.Fatalf("found %d expected 5\n", b)
1412 }
1413 b = C.c_bool(true, true, 3, true, false)
1414 if b != 3 {
1415 t.Fatalf("found %d expected 3\n", b)
1416 }
1417 b = C.c_bool(false, false, 1, true, false)
1418 if b != 1 {
1419 t.Fatalf("found %d expected 1\n", b)
1420 }
1421 b = C.c_bool(false, true, 200, true, false)
1422 if b != 200 {
1423 t.Fatalf("found %d expected 200\n", b)
1424 }
1425 }
1426
1427
1428
1429 func test4857() {
1430 _ = C.issue4857()
1431 }
1432
1433
1434
1435 func testCflags(t *testing.T) {
1436 is_windows := C.is_windows == 1
1437 if is_windows != (runtime.GOOS == "windows") {
1438 t.Errorf("is_windows: %v, runtime.GOOS: %s", is_windows, runtime.GOOS)
1439 }
1440 if C.common != 123 {
1441 t.Errorf("common: %v (expected 123)", C.common)
1442 }
1443 }
1444
1445
1446
1447 func test5227(t *testing.T) {
1448 C.init()
1449 }
1450
1451 func selectfont() C.Fontinfo {
1452 return C.SansTypeface
1453 }
1454
1455
1456
1457 func test5242(t *testing.T) {
1458 if got := C.issue5242(C.foo{}, C.bar{}); got != 5242 {
1459 t.Errorf("got %v", got)
1460 }
1461 }
1462
1463 func test5603(t *testing.T) {
1464 var x [5]int64
1465 exp := int64(C.issue5603exp)
1466 x[0] = int64(C.issue5603foo0())
1467 x[1] = int64(C.issue5603foo1(nil))
1468 x[2] = int64(C.issue5603foo2(nil, nil))
1469 x[3] = int64(C.issue5603foo3(nil, nil, nil))
1470 x[4] = int64(C.issue5603foo4(nil, nil, nil, nil))
1471 for i, v := range x {
1472 if v != exp {
1473 t.Errorf("issue5603foo%d() returns %v, expected %v", i, v, exp)
1474 }
1475 }
1476 }
1477
1478
1479
1480 func test5337(t *testing.T) {
1481 C.test5337()
1482 }
1483
1484
1485
1486 func test5740(t *testing.T) {
1487 if v := C.test5740a() + C.test5740b(); v != 5 {
1488 t.Errorf("expected 5, got %v", v)
1489 }
1490 }
1491
1492
1493
1494 func test5986(t *testing.T) {
1495 C.output5986()
1496 }
1497
1498
1499
1500 func test6128() {
1501
1502 _ = C.X
1503 }
1504
1505
1506
1507 func test6390(t *testing.T) {
1508 p1 := C.malloc(1024)
1509 if p1 == nil {
1510 t.Fatalf("C.malloc(1024) returned nil")
1511 }
1512 p2 := C.malloc(0)
1513 if p2 == nil {
1514 t.Fatalf("C.malloc(0) returned nil")
1515 }
1516 C.free(p1)
1517 C.free(p2)
1518 }
1519
1520 func test6472() {
1521
1522 s := new(C.z)
1523 println(s.y[0].x)
1524 }
1525
1526
1527
1528 func test6506() {
1529
1530 var x C.size_t
1531
1532 C.calloc(x, x)
1533 C.malloc(x)
1534 C.realloc(nil, x)
1535 C.memcpy(nil, nil, x)
1536 C.memcmp(nil, nil, x)
1537 C.memmove(nil, nil, x)
1538 C.strncpy(nil, nil, x)
1539 C.strncmp(nil, nil, x)
1540 C.strncat(nil, nil, x)
1541 x = C.strxfrm(nil, nil, x)
1542 C.memchr(nil, 0, x)
1543 x = C.strcspn(nil, nil)
1544 x = C.strspn(nil, nil)
1545 C.memset(nil, 0, x)
1546 x = C.strlen(nil)
1547 _ = x
1548 }
1549
1550
1551
1552 func testNaming(t *testing.T) {
1553 C.myfunc()
1554 C.myfunc_def()
1555 if v := C.myvar; v != 5 {
1556 t.Errorf("C.myvar = %d, want 5", v)
1557 }
1558 if v := C.myvar_def; v != 5 {
1559 t.Errorf("C.myvar_def = %d, want 5", v)
1560 }
1561 if s := C.GoString(C.mytext); s != "abcdef" {
1562 t.Errorf("C.mytext = %q, want %q", s, "abcdef")
1563 }
1564 if s := C.GoString(C.mytext_def); s != "abcdef" {
1565 t.Errorf("C.mytext_def = %q, want %q", s, "abcdef")
1566 }
1567 if c := C.myenum; c != 1234 {
1568 t.Errorf("C.myenum = %v, want 1234", c)
1569 }
1570 if c := C.myenum_def; c != 1234 {
1571 t.Errorf("C.myenum_def = %v, want 1234", c)
1572 }
1573 {
1574 const c = C.myenum
1575 if c != 1234 {
1576 t.Errorf("C.myenum as const = %v, want 1234", c)
1577 }
1578 }
1579 {
1580 const c = C.myenum_def
1581 if c != 1234 {
1582 t.Errorf("C.myenum as const = %v, want 1234", c)
1583 }
1584 }
1585 if c := C.myint_def; c != 12345 {
1586 t.Errorf("C.myint_def = %v, want 12345", c)
1587 }
1588 {
1589 const c = C.myint_def
1590 if c != 12345 {
1591 t.Errorf("C.myint as const = %v, want 12345", c)
1592 }
1593 }
1594
1595 if c := C.myfloat_def; c != 1.5 {
1596 t.Errorf("C.myint_def = %v, want 1.5", c)
1597 }
1598 {
1599 const c = C.myfloat_def
1600 if c != 1.5 {
1601 t.Errorf("C.myint as const = %v, want 1.5", c)
1602 }
1603 }
1604
1605 if s := C.mystring_def; s != "hello" {
1606 t.Errorf("C.mystring_def = %q, want %q", s, "hello")
1607 }
1608 }
1609
1610
1611
1612 func test6907(t *testing.T) {
1613 want := "yarn"
1614 if got := C.GoString(C.Issue6907CopyString(want)); got != want {
1615 t.Errorf("C.GoString(C.Issue6907CopyString(%q)) == %q, want %q", want, got, want)
1616 }
1617 }
1618
1619
1620
1621 func test7560(t *testing.T) {
1622
1623 if C.offset7560() != 1 {
1624 t.Skip("C compiler did not pack struct")
1625 }
1626
1627
1628
1629 var v C.misaligned
1630 rt := reflect.TypeOf(&v).Elem()
1631 if rt.NumField() != 2 || rt.Field(0).Name != "x" || rt.Field(1).Name != "_" {
1632 t.Errorf("unexpected fields in C.misaligned:\n")
1633 for i := 0; i < rt.NumField(); i++ {
1634 t.Logf("%+v\n", rt.Field(i))
1635 }
1636 }
1637 }
1638
1639
1640
1641 func f() {
1642 var x1 *C.typedef_test7786
1643 var x2 *C.struct_test7786
1644 x1 = x2
1645 x2 = x1
1646 C.f7786(x1)
1647 C.f7786(x2)
1648 C.g7786(x1)
1649 C.g7786(x2)
1650
1651 var b1 *C.typedef_body7786
1652 var b2 *C.struct_body7786
1653 b1 = b2
1654 b2 = b1
1655 C.b7786(b1)
1656 C.b7786(b2)
1657 C.c7786(b1)
1658 C.c7786(b2)
1659
1660 var u1 *C.typedef_union7786
1661 var u2 *C.union_union7786
1662 u1 = u2
1663 u2 = u1
1664 C.u7786(u1)
1665 C.u7786(u2)
1666 C.v7786(u1)
1667 C.v7786(u2)
1668 }
1669
1670
1671
1672 func test8092(t *testing.T) {
1673 tests := []struct {
1674 s string
1675 a, b *C.char
1676 }{
1677 {"text", &C.text[0], C.ctext()},
1678 {"data", &C.data[0], C.cdata()},
1679 }
1680 for _, test := range tests {
1681 if test.a != test.b {
1682 t.Errorf("%s: pointer mismatch: %v != %v", test.s, test.a, test.b)
1683 }
1684 if got := C.GoString(test.a); got != test.s {
1685 t.Errorf("%s: points at %#v, want %#v", test.s, got, test.s)
1686 }
1687 }
1688 }
1689
1690
1691
1692 func issue8368(one *C.struct_one, two *C.struct_two) {
1693 }
1694
1695 func issue8441(one *C.one, two *C.two) {
1696 issue8441(two.x, one.x)
1697 }
1698
1699
1700
1701 var _ = C.struct_issue8428one{
1702 b: C.char(0),
1703
1704
1705
1706 }
1707
1708 var _ = C.struct_issue8428two{
1709 p: unsafe.Pointer(nil),
1710 b: C.char(0),
1711 rest: [0]C.char{},
1712 }
1713
1714 var _ = C.struct_issue8428three{
1715 w: [1][2][3][0]C.char{},
1716 x: [2][3][0][1]C.char{},
1717 y: [3][0][1][2]C.char{},
1718 z: [0][1][2][3]C.char{},
1719 }
1720
1721
1722
1723 func test8811(t *testing.T) {
1724 C.issue8811Execute()
1725 }
1726
1727
1728
1729 func test9557(t *testing.T) {
1730
1731 foo := C.issue9557foo
1732 if v := foo.a; v != 42 {
1733 t.Fatalf("foo.a expected 42, but got %d", v)
1734 }
1735
1736
1737 if v := (*C.issue9557foo).a; v != 42 {
1738 t.Fatalf("(*C.issue9557foo).a expected 42, but is %d", v)
1739 }
1740
1741
1742 if v := C.issue9557foo.a; v != 42 {
1743 t.Fatalf("C.issue9557foo.a expected 42, but is %d", v)
1744 }
1745 }
1746
1747
1748
1749 func issue8331a() C.issue8331 {
1750 return issue8331Var
1751 }
1752
1753
1754
1755 func test10303(t *testing.T, n int) {
1756 if runtime.Compiler == "gccgo" {
1757 t.Skip("gccgo permits C pointers on the stack")
1758 }
1759
1760
1761
1762 if n > 0 {
1763 test10303(t, n-1)
1764 }
1765 if t.Failed() {
1766 return
1767 }
1768 var x, y, z, v, si C.int
1769 var s C.Struct
1770 C.setintstar(&x)
1771 C.setintptr(&y)
1772 C.setvoidptr(unsafe.Pointer(&v))
1773 s.P = &si
1774 C.setstruct(s)
1775
1776 if uintptr(unsafe.Pointer(&x))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1777 t.Error("C int* argument on stack")
1778 }
1779 if uintptr(unsafe.Pointer(&y))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1780 t.Error("C intptr argument on stack")
1781 }
1782 if uintptr(unsafe.Pointer(&v))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1783 t.Error("C void* argument on stack")
1784 }
1785 if uintptr(unsafe.Pointer(&si))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff {
1786 t.Error("C struct field pointer on stack")
1787 }
1788 }
1789
1790
1791
1792 func test11925(t *testing.T) {
1793 if C.sizeof_struct_a11925 != unsafe.Sizeof(C.struct_a11925{}) {
1794 t.Errorf("size of a changed: C %d, Go %d", C.sizeof_struct_a11925, unsafe.Sizeof(C.struct_a11925{}))
1795 }
1796 if C.sizeof_struct_b11925 != unsafe.Sizeof(C.struct_b11925{}) {
1797 t.Errorf("size of b changed: C %d, Go %d", C.sizeof_struct_b11925, unsafe.Sizeof(C.struct_b11925{}))
1798 }
1799 }
1800
1801
1802
1803 func test12030(t *testing.T) {
1804 buf := (*C.char)(C.malloc(256))
1805 defer C.free(unsafe.Pointer(buf))
1806 for _, f := range []float64{1.0, 2.0, 3.14} {
1807 C.issue12030conv(buf, C.double(f))
1808 got := C.GoString(buf)
1809 if want := fmt.Sprintf("d=%g", f); got != want {
1810 t.Fatalf("C.sprintf failed for %g: %q != %q", f, got, want)
1811 }
1812 }
1813 }
1814
1815
1816
1817 var _ C.complexfloat
1818 var _ C.complexdouble
1819
1820
1821
1822
1823
1824 var _, _ = C.abs(0)
1825
1826
1827
1828 func test14838(t *testing.T) {
1829 data := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
1830 cData := C.CBytes(data)
1831 defer C.free(cData)
1832
1833 if C.check_cbytes((*C.char)(cData), C.size_t(len(data))) == 0 {
1834 t.Fatalf("mismatched data: expected %v, got %v", data, (*(*[10]byte)(unsafe.Pointer(cData)))[:])
1835 }
1836 }
1837
1838
1839
1840 var sink C.int
1841
1842 func test17065(t *testing.T) {
1843 if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
1844 t.Skip("broken on darwin; issue 17065")
1845 }
1846 for i := range C.ii {
1847 sink = C.ii[i]
1848 }
1849 }
1850
1851
1852
1853 func test17537(t *testing.T) {
1854 v := C.S17537{i: 17537}
1855 if got, want := C.I17537(&v), C.int(17537); got != want {
1856 t.Errorf("got %d, want %d", got, want)
1857 }
1858
1859 p := (*C.char)(C.malloc(1))
1860 *p = 17
1861 if got, want := C.F17537(&p), C.int(17); got != want {
1862 t.Errorf("got %d, want %d", got, want)
1863 }
1864
1865 C.F18298(nil)
1866 var v18298 C.T18298_2
1867 C.G18298(C.T18298_1(v18298))
1868 }
1869
1870
1871
1872 func testAPI() {
1873 var cs *C.char
1874 cs = C.CString("hello")
1875 defer C.free(unsafe.Pointer(cs))
1876 var s string
1877 s = C.GoString((*C.char)(C.api_hello))
1878 s = C.GoStringN((*C.char)(C.api_hello), C.int(6))
1879 var b []byte
1880 b = C.GoBytes(unsafe.Pointer(C.api_hello), C.int(6))
1881 _, _ = s, b
1882 C.cstring_pointer_fun(nil)
1883 }
1884
1885
1886
1887 func test18126(t *testing.T) {
1888 p := C.malloc(1)
1889 _, err := C.Issue18126C(&p)
1890 C.free(p)
1891 _ = err
1892 }
1893
1894
1895
1896 func test18720(t *testing.T) {
1897 if got, want := C.HELLO_WORLD, "hello\000world"; got != want {
1898 t.Errorf("C.HELLO_WORLD == %q, expected %q", got, want)
1899 }
1900
1901 if got, want := C.VAR1, C.int(5); got != want {
1902 t.Errorf("C.VAR1 == %v, expected %v", got, want)
1903 }
1904
1905 if got, want := *C.ADDR, C.int(5); got != want {
1906 t.Errorf("*C.ADDR == %v, expected %v", got, want)
1907 }
1908
1909 if got, want := C.CALL, C.int(6); got != want {
1910 t.Errorf("C.CALL == %v, expected %v", got, want)
1911 }
1912
1913 if got, want := C.CALL, C.int(7); got != want {
1914 t.Errorf("C.CALL == %v, expected %v", got, want)
1915 }
1916
1917
1918 if got, want := C.SIZE_OF_FOO, 1; got != want {
1919 t.Errorf("C.SIZE_OF_FOO == %v, expected %v", got, want)
1920 }
1921 }
1922
1923
1924
1925 func test20129(t *testing.T) {
1926 if C.issue20129 != 0 {
1927 t.Fatal("test is broken")
1928 }
1929 C.issue20129Foo()
1930 if C.issue20129 != 1 {
1931 t.Errorf("got %v but expected %v", C.issue20129, 1)
1932 }
1933 C.issue20129Bar()
1934 if C.issue20129 != 2 {
1935 t.Errorf("got %v but expected %v", C.issue20129, 2)
1936 }
1937 }
1938
1939
1940
1941 func test20369(t *testing.T) {
1942 if C.XUINT64_MAX != math.MaxUint64 {
1943 t.Fatalf("got %v, want %v", uint64(C.XUINT64_MAX), uint64(math.MaxUint64))
1944 }
1945 }
1946
1947
1948
1949 var issue21668_X = C.x21668
1950
1951
1952
1953 func test21708(t *testing.T) {
1954 if got, want := C.CAST_TO_INT64, -1; got != want {
1955 t.Errorf("C.CAST_TO_INT64 == %v, expected %v", got, want)
1956 }
1957 }
1958
1959
1960
1961 func test21809(t *testing.T) {
1962 longVar := C.long(3)
1963 typedefVar := C.MySigned_t(4)
1964 typedefTypedefVar := C.MySigned2_t(5)
1965
1966
1967 if ret := C.takes_long(longVar); ret != 9 {
1968 t.Errorf("got %v but expected %v", ret, 9)
1969 }
1970 if ret := C.takes_long(typedefVar); ret != 16 {
1971 t.Errorf("got %v but expected %v", ret, 16)
1972 }
1973 if ret := C.takes_long(typedefTypedefVar); ret != 25 {
1974 t.Errorf("got %v but expected %v", ret, 25)
1975 }
1976
1977
1978 if ret := C.takes_typedef(longVar); ret != 9 {
1979 t.Errorf("got %v but expected %v", ret, 9)
1980 }
1981 if ret := C.takes_typedef(typedefVar); ret != 16 {
1982 t.Errorf("got %v but expected %v", ret, 16)
1983 }
1984 if ret := C.takes_typedef(typedefTypedefVar); ret != 25 {
1985 t.Errorf("got %v but expected %v", ret, 25)
1986 }
1987 }
1988
1989
1990
1991 func test22906(t *testing.T) {
1992 var x1 C.jobject = 0
1993 _ = x1
1994 var x2 C.jclass = 0
1995 _ = x2
1996 var x3 C.jthrowable = 0
1997 _ = x3
1998 var x4 C.jstring = 0
1999 _ = x4
2000 var x5 C.jarray = 0
2001 _ = x5
2002 var x6 C.jbooleanArray = 0
2003 _ = x6
2004 var x7 C.jbyteArray = 0
2005 _ = x7
2006 var x8 C.jcharArray = 0
2007 _ = x8
2008 var x9 C.jshortArray = 0
2009 _ = x9
2010 var x10 C.jintArray = 0
2011 _ = x10
2012 var x11 C.jlongArray = 0
2013 _ = x11
2014 var x12 C.jfloatArray = 0
2015 _ = x12
2016 var x13 C.jdoubleArray = 0
2017 _ = x13
2018 var x14 C.jobjectArray = 0
2019 _ = x14
2020 var x15 C.jweak = 0
2021 _ = x15
2022 }
2023
2024
2025
2026 var Vissue22958 C.issue22958Type
2027
2028 func test23356(t *testing.T) {
2029 if got, want := C.a(), C.int(5); got != want {
2030 t.Errorf("C.a() == %v, expected %v", got, want)
2031 }
2032 if got, want := C.r(), C.int(3); got != want {
2033 t.Errorf("C.r() == %v, expected %v", got, want)
2034 }
2035 }
2036
2037
2038
2039 func Issue23720F() {
2040 var x C.issue23720A
2041 C.issue23720F(x)
2042 }
2043
2044
2045
2046 func test24206(t *testing.T) {
2047 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
2048 t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH)
2049 }
2050
2051 if l := len(C.GoString(C.dangerousString1())); l != 123 {
2052 t.Errorf("Incorrect string length - got %d, want 123", l)
2053 }
2054 if l := len(C.GoString(C.dangerousString2())); l != 4096+123 {
2055 t.Errorf("Incorrect string length - got %d, want %d", l, 4096+123)
2056 }
2057 }
2058
2059
2060
2061 func issue25143sum(ns ...C.int) C.int {
2062 total := C.int(0)
2063 for _, n := range ns {
2064 total += n
2065 }
2066 return total
2067 }
2068
2069 func test25143(t *testing.T) {
2070 if got, want := issue25143sum(1, 2, 3), C.int(6); got != want {
2071 t.Errorf("issue25143sum(1, 2, 3) == %v, expected %v", got, want)
2072 }
2073 }
2074
2075
2076
2077
2078 func test26066(t *testing.T) {
2079 var i = int64(C.issue26066)
2080 if i != -1 {
2081 t.Errorf("got %d, want -1", i)
2082 }
2083 }
2084
2085
2086 var a C.TypeOne
2087 var b C.TypeTwo
2088
2089
2090
2091
2092
2093
2094
2095 func test27660(t *testing.T) {
2096 ctx, cancel := context.WithCancel(context.Background())
2097 defer cancel()
2098 ints := make([]int, 100)
2099 locks := make([]sync.Mutex, 100)
2100
2101
2102 for i := 0; i < 100; i++ {
2103 go func() {
2104 for ctx.Err() == nil {
2105
2106
2107 C.usleep(1000 )
2108 runtime.Gosched()
2109 }
2110 }()
2111 go func() {
2112
2113
2114
2115
2116 i := 0
2117 for ctx.Err() == nil {
2118 j := rand.Intn(100)
2119 locks[j].Lock()
2120 ints[j]++
2121 locks[j].Unlock()
2122
2123
2124
2125 if i%(1<<24) == 0 {
2126 runtime.Gosched()
2127 }
2128 i++
2129
2130 }
2131 }()
2132 time.Sleep(time.Millisecond)
2133 }
2134 }
2135
2136
2137
2138 func twoargsF() {
2139 var v struct{ p *byte }
2140 C.twoargs1(C.twoargs2(), C.twoargs3(unsafe.Pointer(&v)))
2141 }
2142
2143
2144
2145 func issue28545G(p **C.char) {
2146 C.issue28545F(p, -1, (0))
2147 C.issue28545F(p, 2+3, complex(1, 1))
2148 C.issue28545F(p, issue28772Constant, issue28772Constant2)
2149 }
2150
2151
2152
2153 const issue28772Constant = C.issue28772Constant
2154
2155
2156
2157 func offset(i int) uintptr {
2158 var pi C.innerPacked
2159 var po C.outerPacked
2160 var ui C.innerUnpacked
2161 var uo C.outerUnpacked
2162 switch i {
2163 case 0:
2164 return unsafe.Offsetof(pi.f2)
2165 case 1:
2166 return unsafe.Offsetof(po.g2)
2167 case 2:
2168 return unsafe.Offsetof(ui.f2)
2169 case 3:
2170 return unsafe.Offsetof(uo.g2)
2171 default:
2172 panic("can't happen")
2173 }
2174 }
2175
2176 func test28896(t *testing.T) {
2177 for i := 0; i < 4; i++ {
2178 c := uintptr(C.offset(C.int(i)))
2179 g := offset(i)
2180 if c != g {
2181 t.Errorf("%d: C: %d != Go %d", i, c, g)
2182 }
2183 }
2184 }
2185
2186
2187
2188
2189
2190
2191 func Issue29383(n, size uint) int {
2192 if ^C.size_t(0)/C.size_t(n) < C.size_t(size) {
2193 return 0
2194 }
2195 return 0
2196 }
2197
2198
2199
2200
2201
2202 var Vissue29748 = C.f29748(&C.S29748{
2203 nil,
2204 })
2205
2206 func Fissue299748() {
2207 C.f29748(&C.S29748{
2208 nil,
2209 })
2210 }
2211
2212
2213
2214 var issue29781X struct{ X int }
2215
2216 func issue29781F(...int) int { return 0 }
2217
2218 func issue29781G() {
2219 var p *C.char
2220 C.issue29781F(&p, C.ISSUE29781C+1)
2221 C.issue29781F(nil, (C.int)(
2222 0))
2223 C.issue29781F(&p, (C.int)(0))
2224 C.issue29781F(&p, (C.int)(
2225 0))
2226 C.issue29781F(&p, (C.int)(issue29781X.
2227 X))
2228 }
2229
2230
2231
2232 func test30065(t *testing.T) {
2233 var a [256]byte
2234 b := []byte("a")
2235 C.memcpy(unsafe.Pointer(&a), unsafe.Pointer(&b[0]), 1)
2236 if a[0] != 'a' {
2237 t.Errorf("&a failed: got %c, want %c", a[0], 'a')
2238 }
2239
2240 b = []byte("b")
2241 C.memcpy(unsafe.Pointer(&a[0]), unsafe.Pointer(&b[0]), 1)
2242 if a[0] != 'b' {
2243 t.Errorf("&a[0] failed: got %c, want %c", a[0], 'b')
2244 }
2245
2246 d := make([]byte, 256)
2247 b = []byte("c")
2248 C.memcpy(unsafe.Pointer(&d[0]), unsafe.Pointer(&b[0]), 1)
2249 if d[0] != 'c' {
2250 t.Errorf("&d[0] failed: got %c, want %c", d[0], 'c')
2251 }
2252 }
2253
2254
2255
2256
2257 func Issue31093() {
2258 C.issue31093F(C.ushort(0))
2259 }
2260
2261
2262
2263 func test32579(t *testing.T) {
2264 var s [1]C.struct_S32579
2265 C.memset(unsafe.Pointer(&s[0].data[0]), 1, 1)
2266 if s[0].data[0] != 1 {
2267 t.Errorf("&s[0].data[0] failed: got %d, want %d", s[0].data[0], 1)
2268 }
2269 }
2270
2271
2272
2273 func testHandle(t *testing.T) {
2274 ch := make(chan int)
2275
2276 for i := 0; i < 42; i++ {
2277 h := cgo.NewHandle(ch)
2278 go func() {
2279 C.cFunc37033(C.uintptr_t(h))
2280 }()
2281 if v := <-ch; issue37033 != v {
2282 t.Fatalf("unexpected receiving value: got %d, want %d", v, issue37033)
2283 }
2284 h.Delete()
2285 }
2286 }
2287
2288
2289
2290 var issue38649 C.netbsd_gid = 42
2291
2292
2293
2294 var issue39877 *C.void = nil
2295
2296
2297
2298
2299 func Issue40494() {
2300 C.issue40494(C.enum_Enum40494(C.X_40494), (*C.union_Union40494)(nil))
2301 }
2302
2303
2304 func test45451(t *testing.T) {
2305 var u *C.issue45451
2306 typ := reflect.ValueOf(u).Type().Elem()
2307
2308
2309 defer func() {
2310 if r := recover(); r == nil {
2311 t.Error("expected panic")
2312 }
2313 }()
2314
2315 _ = reflect.New(typ)
2316 t.Errorf("reflect.New(%v) should have panicked", typ)
2317 }
2318
2319
2320
2321 func func52542[T ~[]C.int]() {}
2322
2323 type type52542[T ~*C.float] struct{}
2324
View as plain text