Skip to content

Commit c383a40

Browse files
aykevldeadprogram
authored andcommitted
cgo: do a basic test that math functions work
They should, but we weren't testing this. I discovered this while working on tinygo-org/macos-minimal-sdk#4 which will likely make math.h not work anymore. So I wanted to make sure we have a test in place before we update that dependency.
1 parent 7b7601d commit c383a40

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

testdata/cgo/main.c

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <math.h>
12
#include "main.h"
23

34
int global = 3;
@@ -67,3 +68,7 @@ void unionSetData(short f0, short f1, short f2) {
6768
void arraydecay(int buf1[5], int buf2[3][8], int buf3[4][7][2]) {
6869
// Do nothing.
6970
}
71+
72+
double doSqrt(double x) {
73+
return sqrt(x);
74+
}

testdata/cgo/main.go

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
/*
44
#include <stdio.h>
5+
#include <math.h>
56
int fortytwo(void);
67
#include "main.h"
78
#include "test.h"
@@ -171,6 +172,10 @@ func main() {
171172
C.strcpy((*C.char)(unsafe.Pointer(&buf2[0])), (*C.char)(unsafe.Pointer(&buf1[0])))
172173
println("copied string:", string(buf2[:C.strlen((*C.char)(unsafe.Pointer(&buf2[0])))]))
173174

175+
// libc: test libm functions (normally bundled in libc)
176+
println("CGo sqrt(3):", C.sqrt(3))
177+
println("C sqrt(3):", C.doSqrt(3))
178+
174179
// libc: test basic stdio functionality
175180
putsBuf := []byte("line written using C puts\x00")
176181
C.puts((*C.char)(unsafe.Pointer(&putsBuf[0])))

testdata/cgo/main.h

+2
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,5 @@ extern int global;
150150
// Test array decaying into a pointer.
151151
typedef int arraydecay_buf3[4][7][2];
152152
void arraydecay(int buf1[5], int buf2[3][8], arraydecay_buf3 buf3);
153+
154+
double doSqrt(double);

testdata/cgo/out.txt

+2
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ C.GoString(nil):
7474
len(C.GoStringN(nil, 0)): 0
7575
len(C.GoBytes(nil, 0)): 0
7676
copied string: foobar
77+
CGo sqrt(3): +1.732051e+000
78+
C sqrt(3): +1.732051e+000
7779
line written using C puts

0 commit comments

Comments
 (0)