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