Commit | Line | Data |
---|---|---|
01ad5a7b LC |
1 | ;;;; foreign.test --- FFI. -*- mode: scheme; coding: utf-8; -*- |
2 | ;;;; | |
3 | ;;;; Copyright (C) 2010 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) | |
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 | ||
33 | (pass-if "zero" | |
5b46a8c2 | 34 | (= 0 (pointer-address %null-pointer))) |
01ad5a7b | 35 | |
d4149a51 LC |
36 | (pass-if "null pointer identity" |
37 | (eq? %null-pointer (make-pointer 0))) | |
38 | ||
39 | (pass-if "null-pointer? %null-pointer" | |
40 | (null-pointer? %null-pointer)) | |
01ad5a7b | 41 | |
5b46a8c2 | 42 | (pass-if-exception "pointer->bytevector %null-pointer" |
01ad5a7b | 43 | exception:null-pointer-error |
5b46a8c2 | 44 | (pointer->bytevector %null-pointer 7))) |
01ad5a7b | 45 | |
d4149a51 LC |
46 | \f |
47 | (with-test-prefix "make-pointer" | |
48 | ||
49 | (pass-if "address preserved" | |
cb2d8076 LC |
50 | (= 123 (pointer-address (make-pointer 123)))) |
51 | ||
52 | (pass-if "equal?" | |
53 | (equal? (make-pointer 123) (make-pointer 123))) | |
54 | ||
55 | (pass-if "equal? modulo finalizer" | |
56 | (let ((finalizer (dynamic-func "scm_is_pair" (dynamic-link)))) | |
57 | (equal? (make-pointer 123) | |
58 | (make-pointer 123 finalizer)))) | |
59 | ||
60 | (pass-if "not equal?" | |
61 | (not (equal? (make-pointer 123) (make-pointer 456))))) | |
d4149a51 LC |
62 | |
63 | \f | |
5b46a8c2 | 64 | (with-test-prefix "pointer<->bytevector" |
d4149a51 LC |
65 | |
66 | (pass-if "bijection" | |
67 | (let ((bv #vu8(0 1 2 3 4 5 6 7))) | |
5b46a8c2 | 68 | (equal? (pointer->bytevector (bytevector->pointer bv) |
d4149a51 LC |
69 | (bytevector-length bv)) |
70 | bv))) | |
71 | ||
72 | (pass-if "pointer from bits" | |
73 | (let* ((bytes (iota (sizeof '*))) | |
74 | (bv (u8-list->bytevector bytes))) | |
5b46a8c2 | 75 | (= (pointer-address |
d4149a51 LC |
76 | (make-pointer (bytevector-uint-ref bv 0 (native-endianness) |
77 | (sizeof '*)))) | |
17fc9efe LC |
78 | (fold-right (lambda (byte address) |
79 | (+ byte (* 256 address))) | |
80 | 0 | |
81 | bytes)))) | |
82 | ||
83 | (pass-if "dereference-pointer" | |
84 | (let* ((bytes (iota (sizeof '*))) | |
85 | (bv (u8-list->bytevector bytes))) | |
5b46a8c2 LC |
86 | (= (pointer-address |
87 | (dereference-pointer (bytevector->pointer bv))) | |
d4149a51 LC |
88 | (fold-right (lambda (byte address) |
89 | (+ byte (* 256 address))) | |
90 | 0 | |
91 | bytes))))) | |
7387c231 LC |
92 | |
93 | \f | |
fa2a89a6 LC |
94 | (with-test-prefix "pointer<->string" |
95 | ||
96 | (pass-if "bijection" | |
97 | (let ((s "hello, world")) | |
98 | (string=? s (pointer->string (string->pointer s))))) | |
99 | ||
100 | (pass-if "bijection [latin1]" | |
101 | (with-latin1-locale | |
102 | (let ((s "Szép jó napot!")) | |
103 | (string=? s (pointer->string (string->pointer s))))))) | |
104 | ||
105 | \f | |
33186356 LC |
106 | (with-test-prefix "procedure->pointer" |
107 | ||
108 | (define qsort | |
109 | ;; Bindings for libc's `qsort' function. | |
2ee07358 LC |
110 | (pointer->procedure void |
111 | (dynamic-func "qsort" (dynamic-link)) | |
112 | (list '* size_t size_t '*))) | |
33186356 LC |
113 | |
114 | (define (dereference-pointer-to-byte ptr) | |
115 | (let ((b (pointer->bytevector ptr 1))) | |
116 | (bytevector-u8-ref b 0))) | |
117 | ||
118 | (define input | |
119 | '(7 1 127 3 5 4 77 2 9 0)) | |
120 | ||
121 | (pass-if "qsort" | |
122 | (if (defined? 'procedure->pointer) | |
123 | (let* ((called? #f) | |
124 | (cmp (lambda (x y) | |
125 | (set! called? #t) | |
126 | (- (dereference-pointer-to-byte x) | |
127 | (dereference-pointer-to-byte y)))) | |
128 | (ptr (procedure->pointer int cmp (list '* '*))) | |
129 | (bv (u8-list->bytevector input))) | |
130 | (qsort (bytevector->pointer bv) (bytevector-length bv) 1 | |
131 | (procedure->pointer int cmp (list '* '*))) | |
132 | (and called? | |
133 | (equal? (bytevector->u8-list bv) | |
134 | (sort input <)))) | |
135 | (throw 'unresolved))) | |
136 | ||
137 | (pass-if-exception "qsort, wrong return type" | |
138 | exception:wrong-type-arg | |
139 | ||
140 | (if (defined? 'procedure->pointer) | |
141 | (let* ((cmp (lambda (x y) #f)) ; wrong return type | |
142 | (ptr (procedure->pointer int cmp (list '* '*))) | |
143 | (bv (u8-list->bytevector input))) | |
144 | (qsort (bytevector->pointer bv) (bytevector-length bv) 1 | |
145 | (procedure->pointer int cmp (list '* '*))) | |
146 | #f) | |
147 | (throw 'unresolved))) | |
148 | ||
149 | (pass-if-exception "qsort, wrong arity" | |
150 | exception:wrong-num-args | |
151 | ||
152 | (if (defined? 'procedure->pointer) | |
153 | (let* ((cmp (lambda (x y z) #f)) ; wrong arity | |
154 | (ptr (procedure->pointer int cmp (list '* '*))) | |
155 | (bv (u8-list->bytevector input))) | |
156 | (qsort (bytevector->pointer bv) (bytevector-length bv) 1 | |
157 | (procedure->pointer int cmp (list '* '*))) | |
158 | #f) | |
fb0b64c1 LC |
159 | (throw 'unresolved))) |
160 | ||
161 | (pass-if "bijection" | |
162 | (if (defined? 'procedure->pointer) | |
163 | (let* ((proc (lambda (x y z) | |
164 | (+ x y z 0.0))) | |
165 | (ret double) | |
166 | (args (list float int16 double)) | |
2ee07358 LC |
167 | (proc* (pointer->procedure ret |
168 | (procedure->pointer ret proc args) | |
169 | args)) | |
fb0b64c1 LC |
170 | (arg1 (map (cut / <> 2.0) (iota 123))) |
171 | (arg2 (iota 123 32000)) | |
172 | (arg3 (map (cut / <> 4.0) (iota 123 100 4)))) | |
173 | (equal? (map proc arg1 arg2 arg3) | |
174 | (map proc* arg1 arg2 arg3))) | |
33186356 LC |
175 | (throw 'unresolved)))) |
176 | ||
177 | \f | |
7387c231 LC |
178 | (with-test-prefix "structs" |
179 | ||
180 | (pass-if "parse-c-struct" | |
181 | (let ((layout (list int64 uint8)) | |
182 | (data (list -300 43))) | |
183 | (equal? (parse-c-struct (make-c-struct layout data) | |
184 | layout) | |
185 | data)))) |