string->pointer and pointer->string have optional encoding arg
[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
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 '*)))
127 (bv (u8-list->bytevector bytes)))
5b46a8c2 128 (= (pointer-address
d4149a51
LC
129 (make-pointer (bytevector-uint-ref bv 0 (native-endianness)
130 (sizeof '*))))
17fc9efe
LC
131 (fold-right (lambda (byte address)
132 (+ byte (* 256 address)))
133 0
134 bytes))))
135
136 (pass-if "dereference-pointer"
137 (let* ((bytes (iota (sizeof '*)))
138 (bv (u8-list->bytevector bytes)))
5b46a8c2
LC
139 (= (pointer-address
140 (dereference-pointer (bytevector->pointer bv)))
d4149a51
LC
141 (fold-right (lambda (byte address)
142 (+ byte (* 256 address)))
143 0
144 bytes)))))
7387c231
LC
145
146\f
fa2a89a6
LC
147(with-test-prefix "pointer<->string"
148
149 (pass-if "bijection"
150 (let ((s "hello, world"))
151 (string=? s (pointer->string (string->pointer s)))))
152
153 (pass-if "bijection [latin1]"
154 (with-latin1-locale
155 (let ((s "Szép jó napot!"))
c6b08d21
AW
156 (string=? s (pointer->string (string->pointer s))))))
157
158 (pass-if "bijection, utf-8"
159 (let ((s "hello, world"))
160 (string=? s (pointer->string (string->pointer s "utf-8")
161 -1 "utf-8"))))
162
163 (pass-if "bijection, utf-8 [latin1]"
164 (let ((s "Szép jó napot!"))
165 (string=? s (pointer->string (string->pointer s "utf-8")
166 -1 "utf-8")))))
167
fa2a89a6
LC
168
169\f
9970cf67
LC
170(with-test-prefix "pointer->procedure"
171
172 (pass-if-exception "object instead of pointer"
173 exception:wrong-type-arg
174 (let ((p (pointer->procedure '* %null-pointer '(*))))
175 (p #t))))
176
177\f
33186356
LC
178(with-test-prefix "procedure->pointer"
179
180 (define qsort
181 ;; Bindings for libc's `qsort' function.
2ee07358
LC
182 (pointer->procedure void
183 (dynamic-func "qsort" (dynamic-link))
184 (list '* size_t size_t '*)))
33186356
LC
185
186 (define (dereference-pointer-to-byte ptr)
187 (let ((b (pointer->bytevector ptr 1)))
188 (bytevector-u8-ref b 0)))
189
190 (define input
191 '(7 1 127 3 5 4 77 2 9 0))
192
193 (pass-if "qsort"
194 (if (defined? 'procedure->pointer)
195 (let* ((called? #f)
196 (cmp (lambda (x y)
197 (set! called? #t)
198 (- (dereference-pointer-to-byte x)
199 (dereference-pointer-to-byte y))))
200 (ptr (procedure->pointer int cmp (list '* '*)))
201 (bv (u8-list->bytevector input)))
202 (qsort (bytevector->pointer bv) (bytevector-length bv) 1
203 (procedure->pointer int cmp (list '* '*)))
204 (and called?
205 (equal? (bytevector->u8-list bv)
206 (sort input <))))
207 (throw 'unresolved)))
208
209 (pass-if-exception "qsort, wrong return type"
210 exception:wrong-type-arg
211
212 (if (defined? 'procedure->pointer)
213 (let* ((cmp (lambda (x y) #f)) ; wrong return type
214 (ptr (procedure->pointer int cmp (list '* '*)))
215 (bv (u8-list->bytevector input)))
216 (qsort (bytevector->pointer bv) (bytevector-length bv) 1
217 (procedure->pointer int cmp (list '* '*)))
218 #f)
219 (throw 'unresolved)))
220
221 (pass-if-exception "qsort, wrong arity"
222 exception:wrong-num-args
223
224 (if (defined? 'procedure->pointer)
225 (let* ((cmp (lambda (x y z) #f)) ; wrong arity
226 (ptr (procedure->pointer int cmp (list '* '*)))
227 (bv (u8-list->bytevector input)))
228 (qsort (bytevector->pointer bv) (bytevector-length bv) 1
229 (procedure->pointer int cmp (list '* '*)))
230 #f)
fb0b64c1
LC
231 (throw 'unresolved)))
232
233 (pass-if "bijection"
234 (if (defined? 'procedure->pointer)
235 (let* ((proc (lambda (x y z)
236 (+ x y z 0.0)))
237 (ret double)
238 (args (list float int16 double))
2ee07358
LC
239 (proc* (pointer->procedure ret
240 (procedure->pointer ret proc args)
241 args))
fb0b64c1
LC
242 (arg1 (map (cut / <> 2.0) (iota 123)))
243 (arg2 (iota 123 32000))
244 (arg3 (map (cut / <> 4.0) (iota 123 100 4))))
245 (equal? (map proc arg1 arg2 arg3)
246 (map proc* arg1 arg2 arg3)))
443f25dc
LC
247 (throw 'unresolved)))
248
249 (pass-if "procedures returning void"
250 (if (defined? 'procedure->pointer)
251 (let* ((called? #f)
252 (proc (lambda () (set! called? #t)))
253 (pointer (procedure->pointer void proc '()))
254 (proc* (pointer->procedure void pointer '())))
255 (proc*)
256 called?)
33186356
LC
257 (throw 'unresolved))))
258
259\f
7387c231
LC
260(with-test-prefix "structs"
261
16f06128
LC
262 (pass-if "sizeof { int8, double }"
263 (= (sizeof (list int8 double))
264 (+ (alignof double) (sizeof double))))
265
266 (pass-if "sizeof { short, int, long, pointer }"
267 (let ((layout (list short int long '*)))
268 (>= (sizeof layout)
269 (reduce + 0.0 (map sizeof layout)))))
270
d82f8518
LC
271 (pass-if "alignof { int8, double, int8 }"
272 ;; alignment of the most strictly aligned component
273 (let ((layout (list int8 double int8)))
274 (= (alignof layout) (alignof double))))
275
7387c231
LC
276 (pass-if "parse-c-struct"
277 (let ((layout (list int64 uint8))
278 (data (list -300 43)))
1f864a16
LC
279 (equal? (parse-c-struct (make-c-struct layout data)
280 layout)
281 data)))
282
283 (pass-if "alignment constraints honored"
284 (let ((layout (list int8 double))
285 (data (list -7 3.14)))
fb636a1c
LC
286 (equal? (parse-c-struct (make-c-struct layout data)
287 layout)
288 data)))
289
290 (pass-if "int8, pointer"
291 (let ((layout (list uint8 '*))
292 (data (list 222 (make-pointer 7777))))
293 (equal? (parse-c-struct (make-c-struct layout data)
294 layout)
295 data)))
296
297 (pass-if "unsigned-long, int8, size_t"
298 (let ((layout (list unsigned-long int8 size_t))
299 (data (list (expt 2 17) -128 (expt 2 18))))
300 (equal? (parse-c-struct (make-c-struct layout data)
301 layout)
302 data)))
303
304 (pass-if "long, int, pointer"
305 (let ((layout (list long int '*))
306 (data (list (- (expt 2 17)) -222 (make-pointer 777))))
42f7c01e
LC
307 (equal? (parse-c-struct (make-c-struct layout data)
308 layout)
309 data)))
310
311 (pass-if "int8, pointer, short, double"
312 (let ((layout (list int8 '* short double))
313 (data (list 77 %null-pointer -42 3.14)))
d82f8518
LC
314 (equal? (parse-c-struct (make-c-struct layout data)
315 layout)
316 data)))
317
318 (pass-if "int8, { int8, double, int8 }, int16"
319 (let ((layout (list int8 (list int8 double int8) int16))
320 (data (list 77 (list 42 4.2 55) 88)))
7387c231
LC
321 (equal? (parse-c-struct (make-c-struct layout data)
322 layout)
323 data))))