summaryrefslogtreecommitdiff
path: root/test-suite/tests/foreign.test
blob: 28d7b5df8de502710a63327873a9b6b3fd039424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
;;;; foreign.test --- FFI.           -*- mode: scheme; coding: utf-8; -*-
;;;;
;;;; 	Copyright (C) 2010, 2011, 2012, 2013, 2017, 2021 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

;;;
;;; See also ../standalone/test-ffi for FFI tests.
;;;

(define-module (test-foreign)
  #:use-module (system foreign-library)
  #:use-module (system foreign)
  #:use-module (rnrs bytevectors)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (ice-9 format)
  #:use-module (test-suite lib))


(with-test-prefix "foreign-library-pointer"

  (pass-if-exception
   "error message"
   ;; The error comes from dlsym, which is system-dependent.
   '(misc-error . "")
   (foreign-library-pointer #f "does_not_exist___")))


(with-test-prefix "null pointer"

  (pass-if "pointer?"
    (pointer? %null-pointer))

  (pass-if "zero"
    (= 0 (pointer-address %null-pointer)))

  (pass-if "null pointer identity"
    (eq? %null-pointer (make-pointer 0)))

  (pass-if "null-pointer? %null-pointer"
    (null-pointer? %null-pointer))

  (pass-if-exception "dereference-pointer %null-pointer"
    exception:null-pointer-error
    (dereference-pointer %null-pointer))

  (pass-if-exception "pointer->bytevector %null-pointer"
    exception:null-pointer-error
    (pointer->bytevector %null-pointer 7)))


(with-test-prefix "make-pointer"

  (pass-if "pointer?"
    (pointer? (make-pointer 123)))

  (pass-if "address preserved"
    (= 123 (pointer-address (make-pointer 123))))

  (pass-if "equal?"
    (equal? (make-pointer 123) (make-pointer 123)))

  (pass-if "equal? modulo finalizer"
    (let ((finalizer (false-if-exception
                      (foreign-library-pointer #f "scm_is_pair"))))
      (if (not finalizer)
          (throw 'unresolved)               ;  Windows or a static build
          (equal? (make-pointer 123)
                  (make-pointer 123 finalizer)))))

  (pass-if "equal? modulo finalizer (set-pointer-finalizer!)"
    (let ((finalizer (false-if-exception
                      (foreign-library-pointer #f "scm_is_pair")))
          (ptr       (make-pointer 123)))
      (if (not finalizer)
          (throw 'unresolved)                ; Windows or a static build
          (begin
            (set-pointer-finalizer! ptr finalizer)
            (equal? (make-pointer 123) ptr)))))

  (pass-if "not equal?"
    (not (equal? (make-pointer 123) (make-pointer 456)))))


(with-test-prefix "pointer<->scm"

  (pass-if "immediates"
    (equal? (pointer->scm (scm->pointer #\newline))
            #\newline))

  (pass-if "non-immediates"
    (equal? (pointer->scm (scm->pointer "Hello, world!"))
            "Hello, world!")))


(define-wrapped-pointer-type foo
  foo?
  wrap-foo unwrap-foo
  (lambda (x p)
    (format p "#<foo! ~a>" (pointer-address (unwrap-foo x)))))

(with-test-prefix "define-wrapped-pointer-type"

  (pass-if "foo?"
    (foo? (wrap-foo %null-pointer)))

  (pass-if "unwrap-foo"
    (let ((p (make-pointer 123)))
      (eq? p (unwrap-foo (wrap-foo p)))))

  (pass-if "identity"
    (let ((p1 (make-pointer 123))
          (p2 (make-pointer 123)))
      (eq? (wrap-foo p1)
           (wrap-foo p2))))

  (pass-if "printer"
    (string=? "#<foo! 123>"
              (with-output-to-string
                (lambda ()
                  (write (wrap-foo (make-pointer 123))))))))


(with-test-prefix "pointer<->bytevector"

  (pass-if "bijection"
    (let ((bv #vu8(0 1 2 3 4 5 6 7)))
      (equal? (pointer->bytevector (bytevector->pointer bv)
                                   (bytevector-length bv))
              bv)))

  (pass-if "pointer from bits"
    (let* ((bytes (iota (sizeof '*)))
           (bv    (u8-list->bytevector bytes))
           (fold  (case (native-endianness)
                    ((little) fold-right)
                    ((big)    fold)
                    (else     (error "unsupported endianness")))))
      (= (pointer-address
          (make-pointer (bytevector-uint-ref bv 0 (native-endianness)
                                             (sizeof '*))))
         (fold (lambda (byte address)
                 (+ byte (* 256 address)))
               0
               bytes))))

  (pass-if "dereference-pointer"
    (let* ((bytes (iota (sizeof '*)))
           (bv    (u8-list->bytevector bytes))
           (fold  (case (native-endianness)
                    ((little) fold-right)
                    ((big)    fold)
                    (else     (error "unsupported endianness")))))
      (= (pointer-address
          (dereference-pointer (bytevector->pointer bv)))
         (fold (lambda (byte address)
                 (+ byte (* 256 address)))
               0
               bytes)))))


(with-test-prefix "pointer<->string"

  (pass-if-exception "%default-port-conversion-strategy is error"
    exception:encoding-error
    (let ((s "χαοσ"))
      (with-fluids ((%default-port-conversion-strategy 'error))
        (string->pointer s "ISO-8859-1"))))

  (pass-if "%default-port-conversion-strategy is escape"
    (let ((s "teĥniko"))
      (equal? (with-fluids ((%default-port-conversion-strategy 'escape))
                (pointer->string (string->pointer s "ISO-8859-1")))
              (format #f "te\\u~4,'0xniko"
                      (char->integer #\ĥ)))))

  (pass-if "%default-port-conversion-strategy is substitute"
    (let ((s "teĥniko")
          (member (negate (negate member))))
      (member (with-fluids ((%default-port-conversion-strategy 'substitute))
                (pointer->string (string->pointer s "ISO-8859-1")))
              '("te?niko"

                ;; This form is found on FreeBSD 8.2 and Darwin 10.8.0.
                "te^hniko"))))

  (pass-if "bijection"
    (let ((s "hello, world"))
      (string=? s (pointer->string (string->pointer s)))))

  (pass-if "bijection [latin1]"
    (with-latin1-locale
      (let ((s "Szép jó napot!"))
        (string=? s (pointer->string (string->pointer s))))))

  (pass-if "bijection, utf-8"
    (let ((s "hello, world"))
      (string=? s (pointer->string (string->pointer s "utf-8")
                                   -1 "utf-8"))))

  (pass-if "bijection, utf-8 [latin1]"
    (let ((s "Szép jó napot!"))
      (string=? s (pointer->string (string->pointer s "utf-8")
                                   -1 "utf-8")))))



(with-test-prefix "pointer->procedure"

  (pass-if-exception "object instead of pointer"
    exception:wrong-type-arg
    (let ((p (pointer->procedure '* %null-pointer '(*))))
      (p #t))))


(with-test-prefix "procedure->pointer"

  (define qsort
    ;; Bindings for libc's `qsort' function.
    ;; On some platforms, such as MinGW, `qsort' is visible only if
    ;; linking with `-export-dynamic'.  Just skip these tests when it's
    ;; not visible.
    (false-if-exception
     (foreign-library-function
      (cond
       ((string-contains %host-type "cygwin")
        ;; On Cygwin, load-foreign-library does not search recursively
        ;; into linked DLLs. Thus, one needs to link to the core C
        ;; library DLL explicitly.
        "cygwin1")
       (else #f))
      "qsort" #:arg-types (list '* size_t size_t '*))))

  (define (dereference-pointer-to-byte ptr)
    (let ((b (pointer->bytevector ptr 1)))
      (bytevector-u8-ref b 0)))

  (define input
    '(7 1 127 3 5 4 77 2 9 0))

  (pass-if "qsort"
    (if (and qsort (defined? 'procedure->pointer))
        (let* ((called? #f)
               (cmp     (lambda (x y)
                          (set! called? #t)
                          (- (dereference-pointer-to-byte x)
                             (dereference-pointer-to-byte y))))
               (ptr     (procedure->pointer int cmp (list '* '*)))
               (bv      (u8-list->bytevector input)))
          (qsort (bytevector->pointer bv) (bytevector-length bv) 1
                 (procedure->pointer int cmp (list '* '*)))
          (and called?
               (equal? (bytevector->u8-list bv)
                       (sort input <))))
        (throw 'unresolved)))

  (pass-if-exception "qsort, wrong return type"
    exception:wrong-type-arg

    (if (and qsort (defined? 'procedure->pointer))
        (let* ((cmp     (lambda (x y) #f)) ; wrong return type
               (ptr     (procedure->pointer int cmp (list '* '*)))
               (bv      (u8-list->bytevector input)))
          (qsort (bytevector->pointer bv) (bytevector-length bv) 1
                 (procedure->pointer int cmp (list '* '*)))
          #f)
        (throw 'unresolved)))

  (pass-if-exception "qsort, wrong arity"
    exception:wrong-num-args

    (if (and qsort (defined? 'procedure->pointer))
        (let* ((cmp     (lambda (x y z) #f)) ; wrong arity
               (ptr     (procedure->pointer int cmp (list '* '*)))
               (bv      (u8-list->bytevector input)))
          (qsort (bytevector->pointer bv) (bytevector-length bv) 1
                 (procedure->pointer int cmp (list '* '*)))
          #f)
        (throw 'unresolved)))

  (pass-if "bijection"
    (if (defined? 'procedure->pointer)
        (let* ((proc  (lambda (x y z)
                        (+ x y z 0.0)))
               (ret   double)
               (args  (list float int16 double))
               (proc* (pointer->procedure ret
                                          (procedure->pointer ret proc args)
                                          args))
               (arg1  (map (cut / <> 2.0) (iota 123)))
               (arg2  (iota 123 32000))
               (arg3  (map (cut / <> 4.0) (iota 123 100 4))))
          (equal? (map proc arg1 arg2 arg3)
                  (map proc* arg1 arg2 arg3)))
        (throw 'unresolved)))

  (pass-if "procedures returning a pointer"
    (if (defined? 'procedure->pointer)
        (let* ((called? #f)
               (proc    (lambda (i) (set! called? #t) (make-pointer i)))
               (pointer (procedure->pointer '* proc (list int)))
               (proc*   (pointer->procedure '* pointer (list int)))
               (result  (proc* 777)))
          (and called? (equal? result (make-pointer 777))))
        (throw 'unresolved)))

  (pass-if "procedures returning void"
    (if (defined? 'procedure->pointer)
        (let* ((called? #f)
               (proc    (lambda () (set! called? #t)))
               (pointer (procedure->pointer void proc '()))
               (proc*   (pointer->procedure void pointer '())))
          (proc*)
          called?)
        (throw 'unresolved)))

  (pass-if "procedure is retained"
    ;; The lambda passed to `procedure->pointer' must remain live.
    (if (defined? 'procedure->pointer)
        (let* ((ptr   (procedure->pointer int
                                          (lambda (x) (+ x 7))
                                          (list int)))
               (procs (unfold (cut >= <> 10000)
                              (lambda (i)
                                (pointer->procedure int ptr (list int)))
                              1+
                              0)))
          (gc) (gc) (gc)
          (every (cut = <> 9)
                 (map (lambda (f) (f 2)) procs)))
        (throw 'unresolved)))

  (pass-if "arity"
    (if (and qsort (defined? 'procedure->pointer))
        (equal? '(4 0 #f) (procedure-minimum-arity qsort))
        (throw 'unresolved))))


(with-test-prefix "structs"

  (pass-if "sizeof { int8, double }"
    (= (sizeof (list int8 double))
       (+ (alignof double) (sizeof double))))

  (pass-if "sizeof { double, int8 }"
    (= (sizeof (list double int8))
       (+ (alignof double) (sizeof double))))

  (pass-if "sizeof { short, int, long, pointer }"
    (let ((layout (list short int long '*)))
      (>= (sizeof layout)
          (reduce + 0.0 (map sizeof layout)))))

  (pass-if "alignof { int8, double, int8 }"
    ;; alignment of the most strictly aligned component
    (let ((layout (list int8 double int8)))
      (= (alignof layout) (alignof double))))

  (pass-if "parse-c-struct"
    (let ((layout (list int64 uint8))
          (data   (list -300 43)))
      (equal? (parse-c-struct (make-c-struct layout data)
                              layout)
              data)))

  (pass-if "alignment constraints honored"
    (let ((layout (list int8 double))
          (data   (list -7 3.14)))
      (equal? (parse-c-struct (make-c-struct layout data)
                              layout)
              data)))

  (pass-if "int8, pointer"
    (let ((layout (list uint8 '*))
          (data   (list 222 (make-pointer 7777))))
      (equal? (parse-c-struct (make-c-struct layout data)
                              layout)
              data)))

  (pass-if "unsigned-long, int8, size_t"
    (let ((layout (list unsigned-long int8 size_t))
          (data   (list (expt 2 17) -128 (expt 2 18))))
      (equal? (parse-c-struct (make-c-struct layout data)
                              layout)
              data)))

  (pass-if "long, int, pointer"
    (let ((layout (list long int '*))
          (data   (list (- (expt 2 17)) -222 (make-pointer 777))))
      (equal? (parse-c-struct (make-c-struct layout data)
                              layout)
              data)))

  (pass-if "int8, pointer, short, double"
    (let ((layout (list int8 '* short double))
          (data   (list 77 %null-pointer -42 3.14)))
      (equal? (parse-c-struct (make-c-struct layout data)
                              layout)
              data)))

  (pass-if "int8, { int8, double, int8 }, int16"
    (let ((layout (list int8 (list int8 double int8) int16))
          (data   (list 77 (list 42 4.2 55) 88)))
      (equal? (parse-c-struct (make-c-struct layout data)
                              layout)
              data)))

  (when (defined? 'complex-float)
    (pass-if "complex types"
      (let ((layout (list complex-float int complex-double))
            (data '(1+3i 99 3-1i)))
        (equal? data (parse-c-struct (make-c-struct layout data)
                                     layout))))))


(with-test-prefix "lib->cyg"
  (pass-if "name is #f"
    (equal? #f (lib->cyg #f)))

  (pass-if "name too short"
    (string=? (lib->cyg "a") "a"))

  (pass-if "name starts with 'lib'"
    (string=? (lib->cyg "libfoo.dll") "cygfoo.dll"))

  (pass-if "name contains 'lib'"
    (string=? (lib->cyg "foolib.dll") "foolib.dll"))

  (pass-if "name doesn't contain 'lib'"
    (string=? (lib->cyg "foobar.dll") "foobar.dll"))

  (pass-if "name in path too short"
    (string=? (lib->cyg "/lib/a") "/lib/a"))

  (pass-if "name in path starts with 'lib'"
    (string=? (lib->cyg "/lib/libfoo.dll") "/lib/cygfoo.dll"))

  (pass-if "name in path contains 'lib'"
    (string=? (lib->cyg "/lib/foolib.dll") "/lib/foolib.dll"))

  (pass-if "name in path doesn't contain 'lib'"
    (string=? (lib->cyg "/lib/foobar.dll") "/lib/foobar.dll"))

  (pass-if "name in windows path starts with 'lib'"
    (string=? (lib->cyg "c:\\lib\\libfoo.dll") "c:\\lib\\cygfoo.dll"))

  (pass-if "name in windows path doesn't contain 'lib'"
    (string=? (lib->cyg "c:\\lib\\foobar.dll") "c:\\lib\\foobar.dll")))