/
ffi_test.go
184 lines (151 loc) · 3.45 KB
/
ffi_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package ffi_test
import (
"math"
"path"
"reflect"
"runtime"
"testing"
ffi "github.com/sbinet/go-ffi"
)
func eq(t *testing.T, ref, chk interface{}) {
_, file, line, _ := runtime.Caller(1)
file = path.Base(file)
if !reflect.DeepEqual(ref, chk) {
t.Errorf("%s:%d: expected [%v], got [%v]", file, line, ref, chk)
}
}
type info struct {
fct string // fct name
arg float64
res float64 // expected value
}
func TestFFIMathf(t *testing.T) {
lib, err := ffi.NewLibrary(libm_name)
if err != nil {
t.Errorf("%v", err)
}
tests := []info{
{"cos", 0., math.Cos(0.)},
{"cos", math.Pi / 2., math.Cos(math.Pi / 2.)},
{"sin", 0., math.Sin(0.)},
{"sin", math.Pi / 2., math.Sin(math.Pi / 2.)},
}
for _, info := range tests {
f, err := lib.Fct(info.fct, ffi.C_double, []ffi.Type{ffi.C_double})
if err != nil {
t.Errorf("could not locate function [%s]: %v", info.fct, err)
}
out := f(info.arg).Float()
if math.Abs(out-info.res) > 1e-16 {
t.Errorf("expected [%v], got [%v] (fct=%v(%v))", info.res, out, info.fct, info.arg)
}
}
err = lib.Close()
if err != nil {
t.Errorf("error closing [%s]: %v", libm_name, err)
}
}
func TestFFIMathi(t *testing.T) {
lib, err := ffi.NewLibrary(libm_name)
if err != nil {
t.Errorf("%v", err)
}
f, err := lib.Fct("abs", ffi.C_int, []ffi.Type{ffi.C_int})
if err != nil {
t.Errorf("could not locate function [abs]: %v", err)
}
{
out := f(10).Int()
if out != 10 {
t.Errorf("expected [10], got [%v] (fct=abs(10))", out)
}
}
{
out := f(-10).Int()
if out != 10 {
t.Errorf("expected [10], got [%v] (fct=abs(-10))", out)
}
}
err = lib.Close()
if err != nil {
t.Errorf("error closing [%s]: %v", libm_name, err)
}
}
func TestFFIStrCmp(t *testing.T) {
lib, err := ffi.NewLibrary(libc_name)
if err != nil {
t.Errorf("%v", err)
}
//int strcmp(const char* cs, const char* ct);
f, err := lib.Fct("strcmp", ffi.C_int, []ffi.Type{ffi.C_pointer, ffi.C_pointer})
if err != nil {
t.Errorf("could not locate function [strcmp]: %v", err)
}
{
s1 := "foo"
s2 := "foo"
out := f(s1, s2).Int()
if out != 0 {
t.Errorf("expected [0], got [%v]", out)
}
}
{
s1 := "foo"
s2 := "foo1"
out := f(s1, s2).Int()
if out == 0 {
t.Errorf("expected [!0], got [%v]", out)
}
}
err = lib.Close()
if err != nil {
t.Errorf("error closing [%s]: %v", libc_name, err)
}
}
func TestFFIStrLen(t *testing.T) {
lib, err := ffi.NewLibrary(libc_name)
if err != nil {
t.Errorf("%v", err)
}
//size_t strlen(const char* cs);
f, err := lib.Fct("strlen", ffi.C_int, []ffi.Type{ffi.C_pointer})
if err != nil {
t.Errorf("could not locate function [strlen]: %v", err)
}
{
str := `foo-bar-\nfoo foo`
out := int(f(str).Int())
if out != len(str) {
t.Errorf("expected [%d], got [%d]", len(str), out)
}
}
err = lib.Close()
if err != nil {
t.Errorf("error closing [%s]: %v", libc_name, err)
}
}
func TestFFIStrCat(t *testing.T) {
lib, err := ffi.NewLibrary(libc_name)
if err != nil {
t.Errorf("%v", err)
}
//char* strcat(char* s, const char* ct);
f, err := lib.Fct("strcat", ffi.C_pointer, []ffi.Type{ffi.C_pointer, ffi.C_pointer})
if err != nil {
t.Errorf("could not locate function [strlen]: %v", err)
}
{
s1 := "foo"
s2 := "bar"
out := f(s1, s2).String()
//FIXME
if out != "foobar" && false {
t.Errorf("expected [foobar], got [%s] (s1=%s, s2=%s)", out, s1, s2)
}
}
err = lib.Close()
if err != nil {
t.Errorf("error closing [%s]: %v", libc_name, err)
}
}
// EOF