Skip FFI tests that rely on `-export-dynamic' behavior when not available.
[bpt/guile.git] / test-suite / tests / foreign.test
CommitLineData
01ad5a7b
LC
1;;;; foreign.test --- FFI. -*- mode: scheme; coding: utf-8; -*-
2;;;;
8364ae3f 3;;;; Copyright (C) 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
01ad5a7b
LC
4;;;;
5;;;; This library is free software; you can redistribute it and/or
6;;;; modify it under the terms of the GNU Lesser General Public
7;;;; License as published by the Free Software Foundation; either
8;;;; version 3 of the License, or (at your option) any later version.
9;;;;
10;;;; This library is distributed in the hope that it will be useful,
11;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13;;;; Lesser General Public License for more details.
14;;;;
15;;;; You should have received a copy of the GNU Lesser General Public
16;;;; License along with this library; if not, write to the Free Software
17;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19;;;
20;;; See also ../standalone/test-ffi for FFI tests.
21;;;
22
23(define-module (test-foreign)
24 #:use-module (system foreign)
07d22c02 25 #:use-module (rnrs bytevectors)
d4149a51 26 #:use-module (srfi srfi-1)
fb0b64c1 27 #:use-module (srfi srfi-26)
b22e94db 28 #:use-module (ice-9 format)
01ad5a7b
LC
29 #:use-module (test-suite lib))
30
31\f
0f1fd214
MG
32(with-test-prefix "dynamic-pointer"
33
34 (pass-if-exception
35 "error message"
36 '(misc-error . "^Symbol not found")
37 (dynamic-func "does_not_exist___" (dynamic-link))))
38
39\f
01ad5a7b
LC
40(with-test-prefix "null pointer"
41
6e097560
LC
42 (pass-if "pointer?"
43 (pointer? %null-pointer))
44
01ad5a7b 45 (pass-if "zero"
5b46a8c2 46 (= 0 (pointer-address %null-pointer)))
01ad5a7b 47
d4149a51
LC
48 (pass-if "null pointer identity"
49 (eq? %null-pointer (make-pointer 0)))
50
51 (pass-if "null-pointer? %null-pointer"
52 (null-pointer? %null-pointer))
01ad5a7b 53
5b46a8c2 54 (pass-if-exception "pointer->bytevector %null-pointer"
01ad5a7b 55 exception:null-pointer-error
5b46a8c2 56 (pointer->bytevector %null-pointer 7)))
01ad5a7b 57
d4149a51
LC
58\f
59(with-test-prefix "make-pointer"
60
6e097560
LC
61 (pass-if "pointer?"
62 (pointer? (make-pointer 123)))
63
d4149a51 64 (pass-if "address preserved"
cb2d8076
LC
65 (= 123 (pointer-address (make-pointer 123))))
66
67 (pass-if "equal?"
68 (equal? (make-pointer 123) (make-pointer 123)))
69
70 (pass-if "equal? modulo finalizer"
71 (let ((finalizer (dynamic-func "scm_is_pair" (dynamic-link))))
8364ae3f
LC
72 (if (not finalizer)
73 (throw 'unresolved) ; probably Windows
74 (equal? (make-pointer 123)
75 (make-pointer 123 finalizer)))))
cb2d8076 76
5765c5a8
LC
77 (pass-if "equal? modulo finalizer (set-pointer-finalizer!)"
78 (let ((finalizer (dynamic-func "scm_is_pair" (dynamic-link)))
79 (ptr (make-pointer 123)))
8364ae3f
LC
80 (if (not finalizer)
81 (throw 'unresolved) ; probably Windows
82 (begin
83 (set-pointer-finalizer! ptr finalizer)
84 (equal? (make-pointer 123) ptr)))))
5765c5a8 85
cb2d8076
LC
86 (pass-if "not equal?"
87 (not (equal? (make-pointer 123) (make-pointer 456)))))
d4149a51
LC
88
89\f
148c3317
AW
90(with-test-prefix "pointer<->scm"
91
92 (pass-if "immediates"
93 (equal? (pointer->scm (scm->pointer #\newline))
94 #\newline))
95
96 (pass-if "non-immediates"
97 (equal? (pointer->scm (scm->pointer "Hello, world!"))
98 "Hello, world!")))
99
100\f
de6fb187
LC
101(define-wrapped-pointer-type foo
102 foo?
1f4f7674
LC
103 wrap-foo unwrap-foo
104 (lambda (x p)
105 (format p "#<foo! ~a>" (pointer-address (unwrap-foo x)))))
106
107(with-test-prefix "define-wrapped-pointer-type"
108
109 (pass-if "foo?"
110 (foo? (wrap-foo %null-pointer)))
111
112 (pass-if "unwrap-foo"
113 (let ((p (make-pointer 123)))
114 (eq? p (unwrap-foo (wrap-foo p)))))
115
116 (pass-if "identity"
117 (let ((p1 (make-pointer 123))
118 (p2 (make-pointer 123)))
119 (eq? (wrap-foo p1)
120 (wrap-foo p2))))
121
122 (pass-if "printer"
123 (string=? "#<foo! 123>"
124 (with-output-to-string
125 (lambda ()
126 (write (wrap-foo (make-pointer 123))))))))
127
128\f
5b46a8c2 129(with-test-prefix "pointer<->bytevector"
d4149a51
LC
130
131 (pass-if "bijection"
132 (let ((bv #vu8(0 1 2 3 4 5 6 7)))
5b46a8c2 133 (equal? (pointer->bytevector (bytevector->pointer bv)
d4149a51
LC
134 (bytevector-length bv))
135 bv)))
136
137 (pass-if "pointer from bits"
138 (let* ((bytes (iota (sizeof '*)))
1f78c669
LC
139 (bv (u8-list->bytevector bytes))
140 (fold (case (native-endianness)
141 ((little) fold-right)
142 ((big) fold)
143 (else (error "unsupported endianness")))))
5b46a8c2 144 (= (pointer-address
d4149a51
LC
145 (make-pointer (bytevector-uint-ref bv 0 (native-endianness)
146 (sizeof '*))))
1f78c669
LC
147 (fold (lambda (byte address)
148 (+ byte (* 256 address)))
149 0
150 bytes))))
17fc9efe
LC
151
152 (pass-if "dereference-pointer"
153 (let* ((bytes (iota (sizeof '*)))
1f78c669
LC
154 (bv (u8-list->bytevector bytes))
155 (fold (case (native-endianness)
156 ((little) fold-right)
157 ((big) fold)
158 (else (error "unsupported endianness")))))
5b46a8c2
LC
159 (= (pointer-address
160 (dereference-pointer (bytevector->pointer bv)))
1f78c669
LC
161 (fold (lambda (byte address)
162 (+ byte (* 256 address)))
163 0
164 bytes)))))
7387c231
LC
165
166\f
fa2a89a6
LC
167(with-test-prefix "pointer<->string"
168
b22e94db
LC
169 (pass-if-exception "%default-port-conversion-strategy is error"
170 exception:encoding-error
171 (let ((s "χαοσ"))
172 (with-fluids ((%default-port-conversion-strategy 'error))
173 (string->pointer s "ISO-8859-1"))))
174
27ea5c3f 175 (pass-if "%default-port-conversion-strategy is escape"
b22e94db 176 (let ((s "teĥniko"))
27ea5c3f
LC
177 (equal? (with-fluids ((%default-port-conversion-strategy 'escape))
178 (pointer->string (string->pointer s "ISO-8859-1")))
179 (format #f "te\\u~4,'0xniko"
180 (char->integer #\ĥ)))))
181
182 (pass-if "%default-port-conversion-strategy is substitute"
183 (let ((s "teĥniko")
184 (member (negate (negate member))))
185 (member (with-fluids ((%default-port-conversion-strategy 'substitute))
186 (pointer->string (string->pointer s "ISO-8859-1")))
187 '("te?niko"
188
189 ;; This form is found on FreeBSD 8.2 and Darwin 10.8.0.
190 "te^hniko"))))
b22e94db 191
fa2a89a6
LC
192 (pass-if "bijection"
193 (let ((s "hello, world"))
194 (string=? s (pointer->string (string->pointer s)))))
195
196 (pass-if "bijection [latin1]"
197 (with-latin1-locale
198 (let ((s "Szép jó napot!"))
c6b08d21
AW
199 (string=? s (pointer->string (string->pointer s))))))
200
201 (pass-if "bijection, utf-8"
202 (let ((s "hello, world"))
203 (string=? s (pointer->string (string->pointer s "utf-8")
204 -1 "utf-8"))))
205
206 (pass-if "bijection, utf-8 [latin1]"
207 (let ((s "Szép jó napot!"))
208 (string=? s (pointer->string (string->pointer s "utf-8")
209 -1 "utf-8")))))
210
fa2a89a6
LC
211
212\f
9970cf67
LC
213(with-test-prefix "pointer->procedure"
214
215 (pass-if-exception "object instead of pointer"
216 exception:wrong-type-arg
217 (let ((p (pointer->procedure '* %null-pointer '(*))))
218 (p #t))))
219
220\f
33186356
LC
221(with-test-prefix "procedure->pointer"
222
223 (define qsort
224 ;; Bindings for libc's `qsort' function.
2ee07358
LC
225 (pointer->procedure void
226 (dynamic-func "qsort" (dynamic-link))
227 (list '* size_t size_t '*)))
33186356
LC
228
229 (define (dereference-pointer-to-byte ptr)
230 (let ((b (pointer->bytevector ptr 1)))
231 (bytevector-u8-ref b 0)))
232
233 (define input
234 '(7 1 127 3 5 4 77 2 9 0))
235
236 (pass-if "qsort"
237 (if (defined? 'procedure->pointer)
238 (let* ((called? #f)
239 (cmp (lambda (x y)
240 (set! called? #t)
241 (- (dereference-pointer-to-byte x)
242 (dereference-pointer-to-byte y))))
243 (ptr (procedure->pointer int cmp (list '* '*)))
244 (bv (u8-list->bytevector input)))
245 (qsort (bytevector->pointer bv) (bytevector-length bv) 1
246 (procedure->pointer int cmp (list '* '*)))
247 (and called?
248 (equal? (bytevector->u8-list bv)
249 (sort input <))))
250 (throw 'unresolved)))
251
252 (pass-if-exception "qsort, wrong return type"
253 exception:wrong-type-arg
254
255 (if (defined? 'procedure->pointer)
256 (let* ((cmp (lambda (x y) #f)) ; wrong return type
257 (ptr (procedure->pointer int cmp (list '* '*)))
258 (bv (u8-list->bytevector input)))
259 (qsort (bytevector->pointer bv) (bytevector-length bv) 1
260 (procedure->pointer int cmp (list '* '*)))
261 #f)
262 (throw 'unresolved)))
263
264 (pass-if-exception "qsort, wrong arity"
265 exception:wrong-num-args
266
267 (if (defined? 'procedure->pointer)
268 (let* ((cmp (lambda (x y z) #f)) ; wrong arity
269 (ptr (procedure->pointer int cmp (list '* '*)))
270 (bv (u8-list->bytevector input)))
271 (qsort (bytevector->pointer bv) (bytevector-length bv) 1
272 (procedure->pointer int cmp (list '* '*)))
273 #f)
fb0b64c1
LC
274 (throw 'unresolved)))
275
276 (pass-if "bijection"
277 (if (defined? 'procedure->pointer)
278 (let* ((proc (lambda (x y z)
279 (+ x y z 0.0)))
280 (ret double)
281 (args (list float int16 double))
2ee07358
LC
282 (proc* (pointer->procedure ret
283 (procedure->pointer ret proc args)
284 args))
fb0b64c1
LC
285 (arg1 (map (cut / <> 2.0) (iota 123)))
286 (arg2 (iota 123 32000))
287 (arg3 (map (cut / <> 4.0) (iota 123 100 4))))
288 (equal? (map proc arg1 arg2 arg3)
289 (map proc* arg1 arg2 arg3)))
443f25dc
LC
290 (throw 'unresolved)))
291
449c4d44
LC
292 (pass-if "procedures returning a pointer"
293 (if (defined? 'procedure->pointer)
294 (let* ((called? #f)
295 (proc (lambda (i) (set! called? #t) (make-pointer i)))
296 (pointer (procedure->pointer '* proc (list int)))
297 (proc* (pointer->procedure '* pointer (list int)))
298 (result (proc* 777)))
299 (and called? (equal? result (make-pointer 777))))
300 (throw 'unresolved)))
301
443f25dc
LC
302 (pass-if "procedures returning void"
303 (if (defined? 'procedure->pointer)
304 (let* ((called? #f)
305 (proc (lambda () (set! called? #t)))
306 (pointer (procedure->pointer void proc '()))
307 (proc* (pointer->procedure void pointer '())))
308 (proc*)
309 called?)
59a02733
LC
310 (throw 'unresolved)))
311
312 (pass-if "procedure is retained"
313 ;; The lambda passed to `procedure->pointer' must remain live.
314 (if (defined? 'procedure->pointer)
315 (let* ((ptr (procedure->pointer int
316 (lambda (x) (+ x 7))
317 (list int)))
318 (procs (unfold (cut >= <> 10000)
319 (lambda (i)
320 (pointer->procedure int ptr (list int)))
321 1+
322 0)))
323 (gc) (gc) (gc)
324 (every (cut = <> 9)
325 (map (lambda (f) (f 2)) procs)))
33186356
LC
326 (throw 'unresolved))))
327
328\f
7387c231
LC
329(with-test-prefix "structs"
330
16f06128
LC
331 (pass-if "sizeof { int8, double }"
332 (= (sizeof (list int8 double))
333 (+ (alignof double) (sizeof double))))
334
335 (pass-if "sizeof { short, int, long, pointer }"
336 (let ((layout (list short int long '*)))
337 (>= (sizeof layout)
338 (reduce + 0.0 (map sizeof layout)))))
339
d82f8518
LC
340 (pass-if "alignof { int8, double, int8 }"
341 ;; alignment of the most strictly aligned component
342 (let ((layout (list int8 double int8)))
343 (= (alignof layout) (alignof double))))
344
7387c231
LC
345 (pass-if "parse-c-struct"
346 (let ((layout (list int64 uint8))
347 (data (list -300 43)))
1f864a16
LC
348 (equal? (parse-c-struct (make-c-struct layout data)
349 layout)
350 data)))
351
352 (pass-if "alignment constraints honored"
353 (let ((layout (list int8 double))
354 (data (list -7 3.14)))
fb636a1c
LC
355 (equal? (parse-c-struct (make-c-struct layout data)
356 layout)
357 data)))
358
359 (pass-if "int8, pointer"
360 (let ((layout (list uint8 '*))
361 (data (list 222 (make-pointer 7777))))
362 (equal? (parse-c-struct (make-c-struct layout data)
363 layout)
364 data)))
365
366 (pass-if "unsigned-long, int8, size_t"
367 (let ((layout (list unsigned-long int8 size_t))
368 (data (list (expt 2 17) -128 (expt 2 18))))
369 (equal? (parse-c-struct (make-c-struct layout data)
370 layout)
371 data)))
372
373 (pass-if "long, int, pointer"
374 (let ((layout (list long int '*))
375 (data (list (- (expt 2 17)) -222 (make-pointer 777))))
42f7c01e
LC
376 (equal? (parse-c-struct (make-c-struct layout data)
377 layout)
378 data)))
379
380 (pass-if "int8, pointer, short, double"
381 (let ((layout (list int8 '* short double))
382 (data (list 77 %null-pointer -42 3.14)))
d82f8518
LC
383 (equal? (parse-c-struct (make-c-struct layout data)
384 layout)
385 data)))
386
387 (pass-if "int8, { int8, double, int8 }, int16"
388 (let ((layout (list int8 (list int8 double int8) int16))
389 (data (list 77 (list 42 4.2 55) 88)))
7387c231
LC
390 (equal? (parse-c-struct (make-c-struct layout data)
391 layout)
392 data))))