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