FFI: Return the right alignment for structures.
[bpt/guile.git] / test-suite / tests / foreign.test
1 ;;;; foreign.test --- FFI. -*- mode: scheme; coding: utf-8; -*-
2 ;;;;
3 ;;;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
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)
25 #:use-module (rnrs bytevectors)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-26)
28 #:use-module (test-suite lib))
29
30 \f
31 (with-test-prefix "null pointer"
32
33 (pass-if "pointer?"
34 (pointer? %null-pointer))
35
36 (pass-if "zero"
37 (= 0 (pointer-address %null-pointer)))
38
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))
44
45 (pass-if-exception "pointer->bytevector %null-pointer"
46 exception:null-pointer-error
47 (pointer->bytevector %null-pointer 7)))
48
49 \f
50 (with-test-prefix "make-pointer"
51
52 (pass-if "pointer?"
53 (pointer? (make-pointer 123)))
54
55 (pass-if "address preserved"
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)))))
68
69 \f
70 (define-wrapped-pointer-type foo
71 foo?
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
98 (with-test-prefix "pointer<->bytevector"
99
100 (pass-if "bijection"
101 (let ((bv #vu8(0 1 2 3 4 5 6 7)))
102 (equal? (pointer->bytevector (bytevector->pointer bv)
103 (bytevector-length bv))
104 bv)))
105
106 (pass-if "pointer from bits"
107 (let* ((bytes (iota (sizeof '*)))
108 (bv (u8-list->bytevector bytes)))
109 (= (pointer-address
110 (make-pointer (bytevector-uint-ref bv 0 (native-endianness)
111 (sizeof '*))))
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)))
120 (= (pointer-address
121 (dereference-pointer (bytevector->pointer bv)))
122 (fold-right (lambda (byte address)
123 (+ byte (* 256 address)))
124 0
125 bytes)))))
126
127 \f
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
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
148 (with-test-prefix "procedure->pointer"
149
150 (define qsort
151 ;; Bindings for libc's `qsort' function.
152 (pointer->procedure void
153 (dynamic-func "qsort" (dynamic-link))
154 (list '* size_t size_t '*)))
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)
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))
209 (proc* (pointer->procedure ret
210 (procedure->pointer ret proc args)
211 args))
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)))
217 (throw 'unresolved))))
218
219 \f
220 (with-test-prefix "structs"
221
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
231 (pass-if "alignof { int8, double, int8 }"
232 ;; alignment of the most strictly aligned component
233 (let ((layout (list int8 double int8)))
234 (= (alignof layout) (alignof double))))
235
236 (pass-if "parse-c-struct"
237 (let ((layout (list int64 uint8))
238 (data (list -300 43)))
239 (equal? (parse-c-struct (make-c-struct layout data)
240 layout)
241 data)))
242
243 (pass-if "alignment constraints honored"
244 (let ((layout (list int8 double))
245 (data (list -7 3.14)))
246 (equal? (parse-c-struct (make-c-struct layout data)
247 layout)
248 data)))
249
250 (pass-if "int8, pointer"
251 (let ((layout (list uint8 '*))
252 (data (list 222 (make-pointer 7777))))
253 (equal? (parse-c-struct (make-c-struct layout data)
254 layout)
255 data)))
256
257 (pass-if "unsigned-long, int8, size_t"
258 (let ((layout (list unsigned-long int8 size_t))
259 (data (list (expt 2 17) -128 (expt 2 18))))
260 (equal? (parse-c-struct (make-c-struct layout data)
261 layout)
262 data)))
263
264 (pass-if "long, int, pointer"
265 (let ((layout (list long int '*))
266 (data (list (- (expt 2 17)) -222 (make-pointer 777))))
267 (equal? (parse-c-struct (make-c-struct layout data)
268 layout)
269 data)))
270
271 (pass-if "int8, pointer, short, double"
272 (let ((layout (list int8 '* short double))
273 (data (list 77 %null-pointer -42 3.14)))
274 (equal? (parse-c-struct (make-c-struct layout data)
275 layout)
276 data)))
277
278 (pass-if "int8, { int8, double, int8 }, int16"
279 (let ((layout (list int8 (list int8 double int8) int16))
280 (data (list 77 (list 42 4.2 55) 88)))
281 (equal? (parse-c-struct (make-c-struct layout data)
282 layout)
283 data))))