| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; Author: Juan Jose Garcia-Ripoll |
| 5 |
;;;; Author: Daniel Kochmański |
| 6 |
;;;; Created: Fri Apr 14 11:13:17 CEST 2006 |
| 7 |
;;;; Contains: Foreign Function Interface regression tests |
| 8 |
|
| 9 |
(in-package :cl-test) |
| 10 |
(suite 'ffi) |
| 11 |
|
| 12 |
;;; Date: 23/03/2006 |
| 13 |
;;; From: Klaus Falb |
| 14 |
;;; Fixed: 26/02/2006 (juanjo) |
| 15 |
;;; Description: |
| 16 |
;;; |
| 17 |
;;; Callback functions have to be declared static so that there |
| 18 |
;;; are no conflicts among callbacks in different files. |
| 19 |
;;; |
| 20 |
;;; Fixed: 13/04/2006 (juanjo) |
| 21 |
;;; Description: |
| 22 |
;;; |
| 23 |
;;; Header <internal.h> should be included as <ecl/internal.h> |
| 24 |
;;; |
| 25 |
#-ecl-bytecmp |
| 26 |
(test ffi.0001.callback |
| 27 |
(is |
| 28 |
(and (ensure-directories-exist "tmp/") |
| 29 |
(with-open-file (s "tmp/a.lsp" :direction :output |
| 30 |
:if-exists :supersede |
| 31 |
:if-does-not-exist :create) |
| 32 |
(print '(ffi:defcallback foo :void () nil) s)) |
| 33 |
(with-open-file (s "tmp/b.lsp" :direction :output |
| 34 |
:if-exists :supersede |
| 35 |
:if-does-not-exist :create) |
| 36 |
(print '(ffi:defcallback foo :void () nil) s)) |
| 37 |
(compile-file "tmp/a.lsp" :system-p t) |
| 38 |
(compile-file "tmp/b.lsp" :system-p t) |
| 39 |
(c:build-program "tmp/foo" :lisp-files |
| 40 |
(list (compile-file-pathname "tmp/a.lsp" :type :object) |
| 41 |
(compile-file-pathname "tmp/b.lsp" :type :object))) |
| 42 |
(probe-file (compile-file-pathname "tmp/foo" :type :program))))) |
| 43 |
|
| 44 |
;;; Date: 29/07/2008 |
| 45 |
;;; From: Juajo |
| 46 |
;;; Description: |
| 47 |
;;; Callback examples based on the C compiler |
| 48 |
;;; |
| 49 |
#-ecl-bytecmp |
| 50 |
(test ffi.0002.callback-sffi-example |
| 51 |
(is |
| 52 |
(and (ensure-directories-exist "tmp/") |
| 53 |
(with-open-file (s "tmp/c.lsp" :direction :output |
| 54 |
:if-exists :supersede |
| 55 |
:if-does-not-exist :create) |
| 56 |
(print |
| 57 |
'(defun callback-user (callback arg) |
| 58 |
(ffi:c-inline (callback arg) (:pointer-void :int) :int " |
| 59 |
int (*foo)(int) = (int (*)(int))#0; |
| 60 |
@(return) = foo(#1); |
| 61 |
" |
| 62 |
:one-liner nil :side-effects nil)) |
| 63 |
s) |
| 64 |
(print |
| 65 |
'(ffi:defcallback ffi-002-foo :int ((a :int)) |
| 66 |
(1+ a)) |
| 67 |
s)) |
| 68 |
(compile-file "tmp/c.lsp" :load t) |
| 69 |
(eql (callback-user (ffi:callback 'ffi-002-foo) 2) 3) |
| 70 |
t))) |
| 71 |
|
| 72 |
;;; Date: 29/07/2008 |
| 73 |
;;; From: Juajo |
| 74 |
;;; Description: |
| 75 |
;;; Callback examples based on the DFFI. Only work if this feature |
| 76 |
;;; has been linked in. |
| 77 |
;;; |
| 78 |
#+(and (or) dffi (not ecl-bytecmp)) |
| 79 |
(test ffi.0003.callback-dffi-example |
| 80 |
(is |
| 81 |
(and (ensure-directories-exist "tmp/") |
| 82 |
(with-open-file (s "tmp/c.lsp" :direction :output |
| 83 |
:if-exists :supersede |
| 84 |
:if-does-not-exist :create) |
| 85 |
(print |
| 86 |
'(defun callback-user (callback arg) |
| 87 |
(ffi:c-inline (callback arg) (:pointer-void :int) :int " |
| 88 |
int (*foo)(int) = (int (*)(int))#0; |
| 89 |
@(return) = foo(#1); |
| 90 |
" |
| 91 |
:one-liner nil :side-effects nil)) |
| 92 |
s)) |
| 93 |
(compile-file "tmp/c.lsp" :load t) |
| 94 |
(eval '(ffi:defcallback foo-002b :int ((a :int)) |
| 95 |
(1+ a))) |
| 96 |
(eql (callback-user (ffi:callback 'foo-002b) 2) 3) |
| 97 |
t))) |
| 98 |
|
| 99 |
;;; Date: 25/04/2010 (Juanjo) |
| 100 |
;;; Description: |
| 101 |
;;; Regression test to ensure that two foreign data compare |
| 102 |
;;; EQUAL when their addresses are the same. |
| 103 |
(test ffi.0004.foreign-data-equal |
| 104 |
(is |
| 105 |
(equal (ffi:make-pointer 1234 :void) |
| 106 |
(ffi:make-pointer 1234 :int)))) |
| 107 |
|
| 108 |
;;; Date: 2016-01-04 (jackdaniel) |
| 109 |
;;; Description: |
| 110 |
;;; Regression test to ensure, that the string is properly |
| 111 |
;;; recognized as an array |
| 112 |
(test ffi.0005.string-is-array |
| 113 |
(finishes |
| 114 |
(si::make-foreign-data-from-array "dan"))) |
| 115 |
|
| 116 |
;;; Date: 2019-12-16 |
| 117 |
;;; Description: |
| 118 |
;;; Regression test to ensure correct complex float handling by |
| 119 |
;;; the interface. On some platforms libffi is miscompiled to |
| 120 |
;;; mishandle complex float return values. See the commit message |
| 121 |
;;; in a commit ad5fe834. |
| 122 |
#+complex-float |
| 123 |
(defun ffi.0006.complex-floats () |
| 124 |
;; dffi |
| 125 |
(let* ((arg #C(10.0s0 0.5s0)) |
| 126 |
(expect (atanh arg))) |
| 127 |
(finishes (ffi:def-function "catanhf" ((x :csfloat)) |
| 128 |
:returning :csfloat |
| 129 |
:module :default)) |
| 130 |
(is (= expect (catanhf arg)))) |
| 131 |
;; sffi |
| 132 |
#-ecl-bytecmp |
| 133 |
(let* ((arg #C(10.0s0 0.5s0)) |
| 134 |
(expect (atanh arg))) |
| 135 |
(finishes (ffi:def-function "catanhf" ((x :csfloat)) |
| 136 |
:returning :csfloat)) |
| 137 |
(compile 'catanhf) |
| 138 |
(is (= expect (catanhf arg))))) |