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