Commit | Line | Data |
---|---|---|
01ad5a7b LC |
1 | ;;;; foreign.test --- FFI. -*- mode: scheme; coding: utf-8; -*- |
2 | ;;;; | |
1f4f7674 | 3 | ;;;; Copyright (C) 2010, 2011 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) |
01ad5a7b LC |
28 | #:use-module (test-suite lib)) |
29 | ||
30 | \f | |
0f1fd214 MG |
31 | (with-test-prefix "dynamic-pointer" |
32 | ||
33 | (pass-if-exception | |
34 | "error message" | |
35 | '(misc-error . "^Symbol not found") | |
36 | (dynamic-func "does_not_exist___" (dynamic-link)))) | |
37 | ||
38 | \f | |
01ad5a7b LC |
39 | (with-test-prefix "null pointer" |
40 | ||
6e097560 LC |
41 | (pass-if "pointer?" |
42 | (pointer? %null-pointer)) | |
43 | ||
01ad5a7b | 44 | (pass-if "zero" |
5b46a8c2 | 45 | (= 0 (pointer-address %null-pointer))) |
01ad5a7b | 46 | |
d4149a51 LC |
47 | (pass-if "null pointer identity" |
48 | (eq? %null-pointer (make-pointer 0))) | |
49 | ||
50 | (pass-if "null-pointer? %null-pointer" | |
51 | (null-pointer? %null-pointer)) | |
01ad5a7b | 52 | |
5b46a8c2 | 53 | (pass-if-exception "pointer->bytevector %null-pointer" |
01ad5a7b | 54 | exception:null-pointer-error |
5b46a8c2 | 55 | (pointer->bytevector %null-pointer 7))) |
01ad5a7b | 56 | |
d4149a51 LC |
57 | \f |
58 | (with-test-prefix "make-pointer" | |
59 | ||
6e097560 LC |
60 | (pass-if "pointer?" |
61 | (pointer? (make-pointer 123))) | |
62 | ||
d4149a51 | 63 | (pass-if "address preserved" |
cb2d8076 LC |
64 | (= 123 (pointer-address (make-pointer 123)))) |
65 | ||
66 | (pass-if "equal?" | |
67 | (equal? (make-pointer 123) (make-pointer 123))) | |
68 | ||
69 | (pass-if "equal? modulo finalizer" | |
70 | (let ((finalizer (dynamic-func "scm_is_pair" (dynamic-link)))) | |
71 | (equal? (make-pointer 123) | |
72 | (make-pointer 123 finalizer)))) | |
73 | ||
74 | (pass-if "not equal?" | |
75 | (not (equal? (make-pointer 123) (make-pointer 456))))) | |
d4149a51 LC |
76 | |
77 | \f | |
148c3317 AW |
78 | (with-test-prefix "pointer<->scm" |
79 | ||
80 | (pass-if "immediates" | |
81 | (equal? (pointer->scm (scm->pointer #\newline)) | |
82 | #\newline)) | |
83 | ||
84 | (pass-if "non-immediates" | |
85 | (equal? (pointer->scm (scm->pointer "Hello, world!")) | |
86 | "Hello, world!"))) | |
87 | ||
88 | \f | |
de6fb187 LC |
89 | (define-wrapped-pointer-type foo |
90 | foo? | |
1f4f7674 LC |
91 | wrap-foo unwrap-foo |
92 | (lambda (x p) | |
93 | (format p "#<foo! ~a>" (pointer-address (unwrap-foo x))))) | |
94 | ||
95 | (with-test-prefix "define-wrapped-pointer-type" | |
96 | ||
97 | (pass-if "foo?" | |
98 | (foo? (wrap-foo %null-pointer))) | |
99 | ||
100 | (pass-if "unwrap-foo" | |
101 | (let ((p (make-pointer 123))) | |
102 | (eq? p (unwrap-foo (wrap-foo p))))) | |
103 | ||
104 | (pass-if "identity" | |
105 | (let ((p1 (make-pointer 123)) | |
106 | (p2 (make-pointer 123))) | |
107 | (eq? (wrap-foo p1) | |
108 | (wrap-foo p2)))) | |
109 | ||
110 | (pass-if "printer" | |
111 | (string=? "#<foo! 123>" | |
112 | (with-output-to-string | |
113 | (lambda () | |
114 | (write (wrap-foo (make-pointer 123)))))))) | |
115 | ||
116 | \f | |
5b46a8c2 | 117 | (with-test-prefix "pointer<->bytevector" |
d4149a51 LC |
118 | |
119 | (pass-if "bijection" | |
120 | (let ((bv #vu8(0 1 2 3 4 5 6 7))) | |
5b46a8c2 | 121 | (equal? (pointer->bytevector (bytevector->pointer bv) |
d4149a51 LC |
122 | (bytevector-length bv)) |
123 | bv))) | |
124 | ||
125 | (pass-if "pointer from bits" | |
126 | (let* ((bytes (iota (sizeof '*))) | |
1f78c669 LC |
127 | (bv (u8-list->bytevector bytes)) |
128 | (fold (case (native-endianness) | |
129 | ((little) fold-right) | |
130 | ((big) fold) | |
131 | (else (error "unsupported endianness"))))) | |
5b46a8c2 | 132 | (= (pointer-address |
d4149a51 LC |
133 | (make-pointer (bytevector-uint-ref bv 0 (native-endianness) |
134 | (sizeof '*)))) | |
1f78c669 LC |
135 | (fold (lambda (byte address) |
136 | (+ byte (* 256 address))) | |
137 | 0 | |
138 | bytes)))) | |
17fc9efe LC |
139 | |
140 | (pass-if "dereference-pointer" | |
141 | (let* ((bytes (iota (sizeof '*))) | |
1f78c669 LC |
142 | (bv (u8-list->bytevector bytes)) |
143 | (fold (case (native-endianness) | |
144 | ((little) fold-right) | |
145 | ((big) fold) | |
146 | (else (error "unsupported endianness"))))) | |
5b46a8c2 LC |
147 | (= (pointer-address |
148 | (dereference-pointer (bytevector->pointer bv))) | |
1f78c669 LC |
149 | (fold (lambda (byte address) |
150 | (+ byte (* 256 address))) | |
151 | 0 | |
152 | bytes))))) | |
7387c231 LC |
153 | |
154 | \f | |
fa2a89a6 LC |
155 | (with-test-prefix "pointer<->string" |
156 | ||
157 | (pass-if "bijection" | |
158 | (let ((s "hello, world")) | |
159 | (string=? s (pointer->string (string->pointer s))))) | |
160 | ||
161 | (pass-if "bijection [latin1]" | |
162 | (with-latin1-locale | |
163 | (let ((s "Szép jó napot!")) | |
c6b08d21 AW |
164 | (string=? s (pointer->string (string->pointer s)))))) |
165 | ||
166 | (pass-if "bijection, utf-8" | |
167 | (let ((s "hello, world")) | |
168 | (string=? s (pointer->string (string->pointer s "utf-8") | |
169 | -1 "utf-8")))) | |
170 | ||
171 | (pass-if "bijection, utf-8 [latin1]" | |
172 | (let ((s "Szép jó napot!")) | |
173 | (string=? s (pointer->string (string->pointer s "utf-8") | |
174 | -1 "utf-8"))))) | |
175 | ||
fa2a89a6 LC |
176 | |
177 | \f | |
9970cf67 LC |
178 | (with-test-prefix "pointer->procedure" |
179 | ||
180 | (pass-if-exception "object instead of pointer" | |
181 | exception:wrong-type-arg | |
182 | (let ((p (pointer->procedure '* %null-pointer '(*)))) | |
183 | (p #t)))) | |
184 | ||
185 | \f | |
33186356 LC |
186 | (with-test-prefix "procedure->pointer" |
187 | ||
188 | (define qsort | |
189 | ;; Bindings for libc's `qsort' function. | |
2ee07358 LC |
190 | (pointer->procedure void |
191 | (dynamic-func "qsort" (dynamic-link)) | |
192 | (list '* size_t size_t '*))) | |
33186356 LC |
193 | |
194 | (define (dereference-pointer-to-byte ptr) | |
195 | (let ((b (pointer->bytevector ptr 1))) | |
196 | (bytevector-u8-ref b 0))) | |
197 | ||
198 | (define input | |
199 | '(7 1 127 3 5 4 77 2 9 0)) | |
200 | ||
201 | (pass-if "qsort" | |
202 | (if (defined? 'procedure->pointer) | |
203 | (let* ((called? #f) | |
204 | (cmp (lambda (x y) | |
205 | (set! called? #t) | |
206 | (- (dereference-pointer-to-byte x) | |
207 | (dereference-pointer-to-byte y)))) | |
208 | (ptr (procedure->pointer int cmp (list '* '*))) | |
209 | (bv (u8-list->bytevector input))) | |
210 | (qsort (bytevector->pointer bv) (bytevector-length bv) 1 | |
211 | (procedure->pointer int cmp (list '* '*))) | |
212 | (and called? | |
213 | (equal? (bytevector->u8-list bv) | |
214 | (sort input <)))) | |
215 | (throw 'unresolved))) | |
216 | ||
217 | (pass-if-exception "qsort, wrong return type" | |
218 | exception:wrong-type-arg | |
219 | ||
220 | (if (defined? 'procedure->pointer) | |
221 | (let* ((cmp (lambda (x y) #f)) ; wrong return type | |
222 | (ptr (procedure->pointer int cmp (list '* '*))) | |
223 | (bv (u8-list->bytevector input))) | |
224 | (qsort (bytevector->pointer bv) (bytevector-length bv) 1 | |
225 | (procedure->pointer int cmp (list '* '*))) | |
226 | #f) | |
227 | (throw 'unresolved))) | |
228 | ||
229 | (pass-if-exception "qsort, wrong arity" | |
230 | exception:wrong-num-args | |
231 | ||
232 | (if (defined? 'procedure->pointer) | |
233 | (let* ((cmp (lambda (x y z) #f)) ; wrong arity | |
234 | (ptr (procedure->pointer int cmp (list '* '*))) | |
235 | (bv (u8-list->bytevector input))) | |
236 | (qsort (bytevector->pointer bv) (bytevector-length bv) 1 | |
237 | (procedure->pointer int cmp (list '* '*))) | |
238 | #f) | |
fb0b64c1 LC |
239 | (throw 'unresolved))) |
240 | ||
241 | (pass-if "bijection" | |
242 | (if (defined? 'procedure->pointer) | |
243 | (let* ((proc (lambda (x y z) | |
244 | (+ x y z 0.0))) | |
245 | (ret double) | |
246 | (args (list float int16 double)) | |
2ee07358 LC |
247 | (proc* (pointer->procedure ret |
248 | (procedure->pointer ret proc args) | |
249 | args)) | |
fb0b64c1 LC |
250 | (arg1 (map (cut / <> 2.0) (iota 123))) |
251 | (arg2 (iota 123 32000)) | |
252 | (arg3 (map (cut / <> 4.0) (iota 123 100 4)))) | |
253 | (equal? (map proc arg1 arg2 arg3) | |
254 | (map proc* arg1 arg2 arg3))) | |
443f25dc LC |
255 | (throw 'unresolved))) |
256 | ||
449c4d44 LC |
257 | (pass-if "procedures returning a pointer" |
258 | (if (defined? 'procedure->pointer) | |
259 | (let* ((called? #f) | |
260 | (proc (lambda (i) (set! called? #t) (make-pointer i))) | |
261 | (pointer (procedure->pointer '* proc (list int))) | |
262 | (proc* (pointer->procedure '* pointer (list int))) | |
263 | (result (proc* 777))) | |
264 | (and called? (equal? result (make-pointer 777)))) | |
265 | (throw 'unresolved))) | |
266 | ||
443f25dc LC |
267 | (pass-if "procedures returning void" |
268 | (if (defined? 'procedure->pointer) | |
269 | (let* ((called? #f) | |
270 | (proc (lambda () (set! called? #t))) | |
271 | (pointer (procedure->pointer void proc '())) | |
272 | (proc* (pointer->procedure void pointer '()))) | |
273 | (proc*) | |
274 | called?) | |
59a02733 LC |
275 | (throw 'unresolved))) |
276 | ||
277 | (pass-if "procedure is retained" | |
278 | ;; The lambda passed to `procedure->pointer' must remain live. | |
279 | (if (defined? 'procedure->pointer) | |
280 | (let* ((ptr (procedure->pointer int | |
281 | (lambda (x) (+ x 7)) | |
282 | (list int))) | |
283 | (procs (unfold (cut >= <> 10000) | |
284 | (lambda (i) | |
285 | (pointer->procedure int ptr (list int))) | |
286 | 1+ | |
287 | 0))) | |
288 | (gc) (gc) (gc) | |
289 | (every (cut = <> 9) | |
290 | (map (lambda (f) (f 2)) procs))) | |
33186356 LC |
291 | (throw 'unresolved)))) |
292 | ||
293 | \f | |
7387c231 LC |
294 | (with-test-prefix "structs" |
295 | ||
16f06128 LC |
296 | (pass-if "sizeof { int8, double }" |
297 | (= (sizeof (list int8 double)) | |
298 | (+ (alignof double) (sizeof double)))) | |
299 | ||
300 | (pass-if "sizeof { short, int, long, pointer }" | |
301 | (let ((layout (list short int long '*))) | |
302 | (>= (sizeof layout) | |
303 | (reduce + 0.0 (map sizeof layout))))) | |
304 | ||
d82f8518 LC |
305 | (pass-if "alignof { int8, double, int8 }" |
306 | ;; alignment of the most strictly aligned component | |
307 | (let ((layout (list int8 double int8))) | |
308 | (= (alignof layout) (alignof double)))) | |
309 | ||
7387c231 LC |
310 | (pass-if "parse-c-struct" |
311 | (let ((layout (list int64 uint8)) | |
312 | (data (list -300 43))) | |
1f864a16 LC |
313 | (equal? (parse-c-struct (make-c-struct layout data) |
314 | layout) | |
315 | data))) | |
316 | ||
317 | (pass-if "alignment constraints honored" | |
318 | (let ((layout (list int8 double)) | |
319 | (data (list -7 3.14))) | |
fb636a1c LC |
320 | (equal? (parse-c-struct (make-c-struct layout data) |
321 | layout) | |
322 | data))) | |
323 | ||
324 | (pass-if "int8, pointer" | |
325 | (let ((layout (list uint8 '*)) | |
326 | (data (list 222 (make-pointer 7777)))) | |
327 | (equal? (parse-c-struct (make-c-struct layout data) | |
328 | layout) | |
329 | data))) | |
330 | ||
331 | (pass-if "unsigned-long, int8, size_t" | |
332 | (let ((layout (list unsigned-long int8 size_t)) | |
333 | (data (list (expt 2 17) -128 (expt 2 18)))) | |
334 | (equal? (parse-c-struct (make-c-struct layout data) | |
335 | layout) | |
336 | data))) | |
337 | ||
338 | (pass-if "long, int, pointer" | |
339 | (let ((layout (list long int '*)) | |
340 | (data (list (- (expt 2 17)) -222 (make-pointer 777)))) | |
42f7c01e LC |
341 | (equal? (parse-c-struct (make-c-struct layout data) |
342 | layout) | |
343 | data))) | |
344 | ||
345 | (pass-if "int8, pointer, short, double" | |
346 | (let ((layout (list int8 '* short double)) | |
347 | (data (list 77 %null-pointer -42 3.14))) | |
d82f8518 LC |
348 | (equal? (parse-c-struct (make-c-struct layout data) |
349 | layout) | |
350 | data))) | |
351 | ||
352 | (pass-if "int8, { int8, double, int8 }, int16" | |
353 | (let ((layout (list int8 (list int8 double int8) int16)) | |
354 | (data (list 77 (list 42 4.2 55) 88))) | |
7387c231 LC |
355 | (equal? (parse-c-struct (make-c-struct layout data) |
356 | layout) | |
357 | data)))) |