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
|
;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
;;;
;;; misc-types.lisp --- Various tests on the type system.
;;;
;;; Copyright (C) 2005-2006, Luis Oliveira <loliveira(@)common-lisp.net>
;;;
;;; Permission is hereby granted, free of charge, to any person
;;; obtaining a copy of this software and associated documentation
;;; files (the "Software"), to deal in the Software without
;;; restriction, including without limitation the rights to use, copy,
;;; modify, merge, publish, distribute, sublicense, and/or sell copies
;;; of the Software, and to permit persons to whom the Software is
;;; furnished to do so, subject to the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;;; DEALINGS IN THE SOFTWARE.
;;;
(in-package #:cffi-tests)
(defcfun ("my_strdup" strdup) :string+ptr (str :string))
(deftest misc-types.string+ptr
(destructuring-bind (string pointer)
(strdup "foo")
(foreign-free pointer)
string)
"foo")
#-(and)
(deftest misc-types.string+ptr.ub8
(destructuring-bind (string pointer)
(strdup (make-array 3 :element-type '(unsigned-byte 8)
:initial-contents (map 'list #'char-code "foo")))
(foreign-free pointer)
string)
"foo")
#-(and)
(deftest misc-types.string.ub8.1
(let ((array (make-array 7 :element-type '(unsigned-byte 8)
:initial-contents '(84 117 114 97 110 103 97))))
(with-foreign-string (foreign-string array)
(foreign-string-to-lisp foreign-string)))
"Turanga")
#-(and)
(deftest misc-types.string.ub8.2
(let ((str (foreign-string-alloc
(make-array 7 :element-type '(unsigned-byte 8)
:initial-contents '(84 117 114 97 110 103 97)))))
(prog1 (foreign-string-to-lisp str)
(foreign-string-free str)))
"Turanga")
(defcfun "equalequal" :boolean
(a (:boolean :int))
(b (:boolean :unsigned-int)))
(defcfun "bool_and" (:boolean :char)
(a (:boolean :unsigned-char))
(b (:boolean :char)))
(defcfun "bool_xor" (:boolean :unsigned-long)
(a (:boolean :long))
(b (:boolean :unsigned-long)))
(deftest misc-types.boolean.1
(list (equalequal nil nil)
(equalequal t t)
(equalequal t 23)
(bool-and 'a 'b)
(bool-and "foo" nil)
(bool-xor t nil)
(bool-xor nil nil))
(t t t t nil t nil))
;;; Regression test: boolean type only worked with canonicalized
;;; built-in integer types. Should work for any type that canonicalizes
;;; to a built-in integer type.
(defctype int-for-bool :int)
(defcfun ("equalequal" equalequal2) :boolean
(a (:boolean int-for-bool))
(b (:boolean :uint)))
(deftest misc-types.boolean.2
(equalequal2 nil t)
nil)
(defctype my-string :string+ptr)
(defun funkify (str)
(concatenate 'string "MORE " (string-upcase str)))
(defun 3rd-person (value)
(list (concatenate 'string "Strdup says: " (first value))
(second value)))
;; (defctype funky-string
;; (:wrapper my-string
;; :to-c #'funkify
;; :from-c (lambda (value)
;; (list
;; (concatenate 'string "Strdup says: "
;; (first value))
;; (second value))))
;; "A useful type.")
(defctype funky-string (:wrapper my-string :to-c funkify :from-c 3rd-person))
(defcfun ("my_strdup" funky-strdup) funky-string
(str funky-string))
(deftest misc-types.wrapper
(destructuring-bind (string ptr)
(funky-strdup "code")
(foreign-free ptr)
string)
"Strdup says: MORE CODE")
(deftest misc-types.sized-ints
(mapcar #'foreign-type-size
'(:int8 :uint8 :int16 :uint16 :int32 :uint32 :int64 :uint64))
(1 1 2 2 4 4 8 8))
(define-foreign-type error-error ()
()
(:actual-type :int)
(:simple-parser error-error))
(defmethod translate-to-foreign (value (type error-error))
(declare (ignore value))
(error "translate-to-foreign invoked."))
(defmethod translate-from-foreign (value (type error-error))
(declare (ignore value))
(error "translate-from-foreign invoked."))
(eval-when (:load-toplevel :compile-toplevel :execute)
(defmethod expand-to-foreign (value (type error-error))
value)
(defmethod expand-from-foreign (value (type error-error))
value))
(defcfun ("abs" expand-abs) error-error
(n error-error))
(defcvar ("var_int" *expand-var-int*) error-error)
(defcfun ("expect_int_sum" expand-expect-int-sum) :boolean
(cb :pointer))
(defcallback expand-int-sum error-error ((x error-error) (y error-error))
(+ x y))
;;; Ensure that macroexpansion-time translators are called where this
;;; is guaranteed (defcfun, defcvar, foreign-funcall and defcallback)
(deftest misc-types.expand.1
(expand-abs -1)
1)
#-cffi-sys::no-foreign-funcall
(deftest misc-types.expand.2
(foreign-funcall "abs" error-error -1 error-error)
1)
(deftest misc-types.expand.3
(let ((old (mem-ref (get-var-pointer '*expand-var-int*) :int)))
(unwind-protect
(progn
(setf *expand-var-int* 42)
*expand-var-int*)
(setf (mem-ref (get-var-pointer '*expand-var-int*) :int) old)))
42)
(deftest misc-types.expand.4
(expand-expect-int-sum (callback expand-int-sum))
t)
(define-foreign-type translate-tracker ()
()
(:actual-type :int)
(:simple-parser translate-tracker))
(declaim (special .fto-called.))
(defmethod free-translated-object (value (type translate-tracker) param)
(declare (ignore value param))
(setf .fto-called. t))
(define-foreign-type expand-tracker ()
()
(:actual-type :int)
(:simple-parser expand-tracker))
(defmethod free-translated-object (value (type expand-tracker) param)
(declare (ignore value param))
(setf .fto-called. t))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmethod expand-to-foreign (value (type expand-tracker))
(declare (ignore value))
(call-next-method)))
(defcfun ("abs" ttracker-abs) :int
(n translate-tracker))
(defcfun ("abs" etracker-abs) :int
(n expand-tracker))
;; free-translated-object must be called when there is no etf
(deftest misc-types.expand.5
(let ((.fto-called. nil))
(ttracker-abs -1)
.fto-called.)
t)
;; free-translated-object must be called when there is an etf, but
;; they answer *runtime-translator-form*
(deftest misc-types.expand.6
(let ((.fto-called. nil))
(etracker-abs -1)
.fto-called.)
t)
|