aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2021-11-11 16:11:08 +0100
committerRon Evans <[email protected]>2021-11-24 21:09:29 +0100
commitc31aef06ba131051cba4a18f336eb7d820faa4e7 (patch)
tree219fe13aa3b09f331195686d664b58d0b64a52b8 /testdata
parent6bd18af5ef02eddb8507cdbe40ffbe8d594af910 (diff)
downloadtinygo-c31aef06ba131051cba4a18f336eb7d820faa4e7.tar.gz
tinygo-c31aef06ba131051cba4a18f336eb7d820faa4e7.zip
cgo: add support for C.CString and related functions
Diffstat (limited to 'testdata')
-rw-r--r--testdata/cgo/main.c2
-rw-r--r--testdata/cgo/main.go15
-rw-r--r--testdata/cgo/main.h2
-rw-r--r--testdata/cgo/out.txt7
4 files changed, 26 insertions, 0 deletions
diff --git a/testdata/cgo/main.c b/testdata/cgo/main.c
index d40833246..31b60704f 100644
--- a/testdata/cgo/main.c
+++ b/testdata/cgo/main.c
@@ -24,6 +24,8 @@ int cflagsConstant = SOME_CONSTANT;
int smallEnumWidth = sizeof(option2_t);
+char globalChars[] = {2, 0, 4, 8};
+
int fortytwo() {
return 42;
}
diff --git a/testdata/cgo/main.go b/testdata/cgo/main.go
index cc25152e4..e8438919f 100644
--- a/testdata/cgo/main.go
+++ b/testdata/cgo/main.go
@@ -135,6 +135,21 @@ func main() {
// void arraydecay(int *buf1, int *buf2[8], int *buf3[7][2]);
C.arraydecay((*C.int)(nil), (*[8]C.int)(nil), (*[7][2]C.int)(nil))
+ // Test CGo builtins like C.CString.
+ cstr := C.CString("string passed to C")
+ println("cstr length:", C.strlen(cstr))
+ gostr := C.GoString(cstr)
+ println("C.CString:", gostr)
+ charBuf := C.GoBytes(unsafe.Pointer(&C.globalChars[0]), 4)
+ println("C.charBuf:", charBuf[0], charBuf[1], charBuf[2], charBuf[3])
+ binaryString := C.GoStringN(&C.globalChars[0], 4)
+ println("C.CStringN:", len(binaryString), binaryString[0], binaryString[1], binaryString[2], binaryString[3])
+
+ // Test whether those builtins also work with zero length data.
+ println("C.GoString(nil):", C.GoString(nil))
+ println("len(C.GoStringN(nil, 0)):", len(C.GoStringN(nil, 0)))
+ println("len(C.GoBytes(nil, 0)):", len(C.GoBytes(nil, 0)))
+
// libc: test whether C functions work at all.
buf1 := []byte("foobar\x00")
buf2 := make([]byte, len(buf1))
diff --git a/testdata/cgo/main.h b/testdata/cgo/main.h
index 09e1b4f09..702dab0c0 100644
--- a/testdata/cgo/main.h
+++ b/testdata/cgo/main.h
@@ -141,6 +141,8 @@ extern int smallEnumWidth;
extern int cflagsConstant;
+extern char globalChars[4];
+
// test duplicate definitions
int add(int a, int b);
extern int global;
diff --git a/testdata/cgo/out.txt b/testdata/cgo/out.txt
index ca77594f5..375ad1f6f 100644
--- a/testdata/cgo/out.txt
+++ b/testdata/cgo/out.txt
@@ -61,5 +61,12 @@ option 2A: 20
option 3A: 21
enum width matches: true
CFLAGS value: 17
+cstr length: 18
+C.CString: string passed to C
+C.charBuf: 2 0 4 8
+C.CStringN: 4 2 0 4 8
+C.GoString(nil):
+len(C.GoStringN(nil, 0)): 0
+len(C.GoBytes(nil, 0)): 0
copied string: foobar
line written using C puts