Improve type checking when invoking foreign functions.
[bpt/guile.git] / test-suite / tests / foreign.test
CommitLineData
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
31(with-test-prefix "null pointer"
32
6e097560
LC
33 (pass-if "pointer?"
34 (pointer? %null-pointer))
35
01ad5a7b 36 (pass-if "zero"
5b46a8c2 37 (= 0 (pointer-address %null-pointer)))
01ad5a7b 38
d4149a51
LC
39 (pass-if "null pointer identity"
40 (eq? %null-pointer (make-pointer 0)))
41
42 (pass-if "null-pointer? %null-pointer"
43 (null-pointer? %null-pointer))
01ad5a7b 44
5b46a8c2 45 (pass-if-exception "pointer->bytevector %null-pointer"
01ad5a7b 46 exception:null-pointer-error
5b46a8c2 47 (pointer->bytevector %null-pointer 7)))
01ad5a7b 48
d4149a51
LC
49\f
50(with-test-prefix "make-pointer"
51
6e097560
LC
52 (pass-if "pointer?"
53 (pointer? (make-pointer 123)))
54
d4149a51 55 (pass-if "address preserved"
cb2d8076
LC
56 (= 123 (pointer-address (make-pointer 123))))
57
58 (pass-if "equal?"
59 (equal? (make-pointer 123) (make-pointer 123)))
60
61 (pass-if "equal? modulo finalizer"
62 (let ((finalizer (dynamic-func "scm_is_pair" (dynamic-link))))
63 (equal? (make-pointer 123)
64 (make-pointer 123 finalizer))))
65
66 (pass-if "not equal?"
67 (not (equal? (make-pointer 123) (make-pointer 456)))))
d4149a51
LC
68
69\f
de6fb187
LC
70(define-wrapped-pointer-type foo
71 foo?
1f4f7674
LC
72 wrap-foo unwrap-foo
73 (lambda (x p)
74 (format p "#<foo! ~a>" (pointer-address (unwrap-foo x)))))
75
76(with-test-prefix "define-wrapped-pointer-type"
77
78 (pass-if "foo?"
79 (foo? (wrap-foo %null-pointer)))
80
81 (pass-if "unwrap-foo"
82 (let ((p (make-pointer 123)))
83 (eq? p (unwrap-foo (wrap-foo p)))))
84
85 (pass-if "identity"
86 (let ((p1 (make-pointer 123))
87 (p2 (make-pointer 123)))
88 (eq? (wrap-foo p1)
89 (wrap-foo p2))))
90
91 (pass-if "printer"
92 (string=? "#<foo! 123>"
93 (with-output-to-string
94 (lambda ()
95 (write (wrap-foo (make-pointer 123))))))))
96
97\f
5b46a8c2 98(with-test-prefix "pointer<->bytevector"
d4149a51
LC
99
100 (pass-if "bijection"
101 (let ((bv #vu8(0 1 2 3 4 5 6 7)))
5b46a8c2 102 (equal? (pointer->bytevector (bytevector->pointer bv)
d4149a51
LC
103 (bytevector-length bv))
104 bv)))
105
106 (pass-if "pointer from bits"
107 (let* ((bytes (iota (sizeof '*)))
108 (bv (u8-list->bytevector bytes)))
5b46a8c2 109 (= (pointer-address
d4149a51
LC
110 (make-pointer (bytevector-uint-ref bv 0 (native-endianness)
111 (sizeof '*))))
17fc9efe
LC
112 (fold-right (lambda (byte address)
113 (+ byte (* 256 address)))
114 0
115 bytes))))
116
117 (pass-if "dereference-pointer"
118 (let* ((bytes (iota (sizeof '*)))
119 (bv (u8-list->bytevector bytes)))
5b46a8c2
LC
120 (= (pointer-address
121 (dereference-pointer (bytevector->pointer bv)))
d4149a51
LC
122 (fold-right (lambda (byte address)
123 (+ byte (* 256 address)))
124 0
125 bytes)))))
7387c231
LC
126
127\f
fa2a89a6
LC
128(with-test-prefix "pointer<->string"
129
130 (pass-if "bijection"
131 (let ((s "hello, world"))
132 (string=? s (pointer->string (string->pointer s)))))
133
134 (pass-if "bijection [latin1]"
135 (with-latin1-locale
136 (let ((s "Szép jó napot!"))
137 (string=? s (pointer->string (string->pointer s)))))))
138
139\f
9970cf67
LC
140(with-test-prefix "pointer->procedure"
141
142 (pass-if-exception "object instead of pointer"
143 exception:wrong-type-arg
144 (let ((p (pointer->procedure '* %null-pointer '(*))))
145 (p #t))))
146
147\f
33186356
LC
148(with-test-prefix "procedure->pointer"
149
150 (define qsort
151 ;; Bindings for libc's `qsort' function.
2ee07358
LC
152 (pointer->procedure void
153 (dynamic-func "qsort" (dynamic-link))
154 (list '* size_t size_t '*)))
33186356
LC
155
156 (define (dereference-pointer-to-byte ptr)
157 (let ((b (pointer->bytevector ptr 1)))
158 (bytevector-u8-ref b 0)))
159
160 (define input
161 '(7 1 127 3 5 4 77 2 9 0))
162
163 (pass-if "qsort"
164 (if (defined? 'procedure->pointer)
165 (let* ((called? #f)
166 (cmp (lambda (x y)
167 (set! called? #t)
168 (- (dereference-pointer-to-byte x)
169 (dereference-pointer-to-byte y))))
170 (ptr (procedure->pointer int cmp (list '* '*)))
171 (bv (u8-list->bytevector input)))
172 (qsort (bytevector->pointer bv) (bytevector-length bv) 1
173 (procedure->pointer int cmp (list '* '*)))
174 (and called?
175 (equal? (bytevector->u8-list bv)
176 (sort input <))))
177 (throw 'unresolved)))
178
179 (pass-if-exception "qsort, wrong return type"
180 exception:wrong-type-arg
181
182 (if (defined? 'procedure->pointer)
183 (let* ((cmp (lambda (x y) #f)) ; wrong return type
184 (ptr (procedure->pointer int cmp (list '* '*)))
185 (bv (u8-list->bytevector input)))
186 (qsort (bytevector->pointer bv) (bytevector-length bv) 1
187 (procedure->pointer int cmp (list '* '*)))
188 #f)
189 (throw 'unresolved)))
190
191 (pass-if-exception "qsort, wrong arity"
192 exception:wrong-num-args
193
194 (if (defined? 'procedure->pointer)
195 (let* ((cmp (lambda (x y z) #f)) ; wrong arity
196 (ptr (procedure->pointer int cmp (list '* '*)))
197 (bv (u8-list->bytevector input)))
198 (qsort (bytevector->pointer bv) (bytevector-length bv) 1
199 (procedure->pointer int cmp (list '* '*)))
200 #f)
fb0b64c1
LC
201 (throw 'unresolved)))
202
203 (pass-if "bijection"
204 (if (defined? 'procedure->pointer)
205 (let* ((proc (lambda (x y z)
206 (+ x y z 0.0)))
207 (ret double)
208 (args (list float int16 double))
2ee07358
LC
209 (proc* (pointer->procedure ret
210 (procedure->pointer ret proc args)
211 args))
fb0b64c1
LC
212 (arg1 (map (cut / <> 2.0) (iota 123)))
213 (arg2 (iota 123 32000))
214 (arg3 (map (cut / <> 4.0) (iota 123 100 4))))
215 (equal? (map proc arg1 arg2 arg3)
216 (map proc* arg1 arg2 arg3)))
33186356
LC
217 (throw 'unresolved))))
218
219\f
7387c231
LC
220(with-test-prefix "structs"
221
16f06128
LC
222 (pass-if "sizeof { int8, double }"
223 (= (sizeof (list int8 double))
224 (+ (alignof double) (sizeof double))))
225
226 (pass-if "sizeof { short, int, long, pointer }"
227 (let ((layout (list short int long '*)))
228 (>= (sizeof layout)
229 (reduce + 0.0 (map sizeof layout)))))
230
7387c231
LC
231 (pass-if "parse-c-struct"
232 (let ((layout (list int64 uint8))
233 (data (list -300 43)))
1f864a16
LC
234 (equal? (parse-c-struct (make-c-struct layout data)
235 layout)
236 data)))
237
238 (pass-if "alignment constraints honored"
239 (let ((layout (list int8 double))
240 (data (list -7 3.14)))
fb636a1c
LC
241 (equal? (parse-c-struct (make-c-struct layout data)
242 layout)
243 data)))
244
245 (pass-if "int8, pointer"
246 (let ((layout (list uint8 '*))
247 (data (list 222 (make-pointer 7777))))
248 (equal? (parse-c-struct (make-c-struct layout data)
249 layout)
250 data)))
251
252 (pass-if "unsigned-long, int8, size_t"
253 (let ((layout (list unsigned-long int8 size_t))
254 (data (list (expt 2 17) -128 (expt 2 18))))
255 (equal? (parse-c-struct (make-c-struct layout data)
256 layout)
257 data)))
258
259 (pass-if "long, int, pointer"
260 (let ((layout (list long int '*))
261 (data (list (- (expt 2 17)) -222 (make-pointer 777))))
42f7c01e
LC
262 (equal? (parse-c-struct (make-c-struct layout data)
263 layout)
264 data)))
265
266 (pass-if "int8, pointer, short, double"
267 (let ((layout (list int8 '* short double))
268 (data (list 77 %null-pointer -42 3.14)))
7387c231
LC
269 (equal? (parse-c-struct (make-c-struct layout data)
270 layout)
271 data))))