Skip to content

Commit 9bb1b2d

Browse files
committed
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 377415a commit 9bb1b2d

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

testdata/cgo/main.c

Lines changed: 5 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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"
@@ -174,6 +175,10 @@ func main() {
174175
// libc: test basic stdio functionality
175176
putsBuf := []byte("line written using C puts\x00")
176177
C.puts((*C.char)(unsafe.Pointer(&putsBuf[0])))
178+
179+
// libc: test libm functions (normally bundled in libc)
180+
println("CGo sqrt(3):", C.sqrt(3))
181+
println("C sqrt(3):", C.doSqrt(3))
177182
}
178183

179184
func printUnion(union C.joined_t) C.joined_t {

testdata/cgo/main.h

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,5 @@ len(C.GoStringN(nil, 0)): 0
7575
len(C.GoBytes(nil, 0)): 0
7676
copied string: foobar
7777
line written using C puts
78+
CGo sqrt(3): +1.732051e+000
79+
C sqrt(3): +1.732051e+000

0 commit comments

Comments
 (0)