callbacks.lisp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
  2. ;;;
  3. ;;; callbacks.lisp --- Tests on callbacks.
  4. ;;;
  5. ;;; Copyright (C) 2005-2006, Luis Oliveira <loliveira(@)common-lisp.net>
  6. ;;;
  7. ;;; Permission is hereby granted, free of charge, to any person
  8. ;;; obtaining a copy of this software and associated documentation
  9. ;;; files (the "Software"), to deal in the Software without
  10. ;;; restriction, including without limitation the rights to use, copy,
  11. ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
  12. ;;; of the Software, and to permit persons to whom the Software is
  13. ;;; furnished to do so, subject to the following conditions:
  14. ;;;
  15. ;;; The above copyright notice and this permission notice shall be
  16. ;;; included in all copies or substantial portions of the Software.
  17. ;;;
  18. ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. ;;; DEALINGS IN THE SOFTWARE.
  26. ;;;
  27. (in-package #:cffi-tests)
  28. (defcfun "expect_char_sum" :int (f :pointer))
  29. (defcfun "expect_unsigned_char_sum" :int (f :pointer))
  30. (defcfun "expect_short_sum" :int (f :pointer))
  31. (defcfun "expect_unsigned_short_sum" :int (f :pointer))
  32. (defcfun "expect_int_sum" :int (f :pointer))
  33. (defcfun "expect_unsigned_int_sum" :int (f :pointer))
  34. (defcfun "expect_long_sum" :int (f :pointer))
  35. (defcfun "expect_unsigned_long_sum" :int (f :pointer))
  36. (defcfun "expect_float_sum" :int (f :pointer))
  37. (defcfun "expect_double_sum" :int (f :pointer))
  38. (defcfun "expect_pointer_sum" :int (f :pointer))
  39. (defcfun "expect_strcat" :int (f :pointer))
  40. #-cffi-sys::no-long-long
  41. (progn
  42. (defcfun "expect_long_long_sum" :int (f :pointer))
  43. (defcfun "expect_unsigned_long_long_sum" :int (f :pointer)))
  44. #+(and scl long-float)
  45. (defcfun "expect_long_double_sum" :int (f :pointer))
  46. (defcallback sum-char :char ((a :char) (b :char))
  47. "Test if the named block is present and the docstring too."
  48. ;(format t "~%}}} a: ~A, b: ~A {{{~%" a b)
  49. (return-from sum-char (+ a b)))
  50. (defcallback sum-unsigned-char :unsigned-char
  51. ((a :unsigned-char) (b :unsigned-char))
  52. ;(format t "~%}}} a: ~A, b: ~A {{{~%" a b)
  53. (+ a b))
  54. (defcallback sum-short :short ((a :short) (b :short))
  55. ;(format t "~%}}} a: ~A, b: ~A {{{~%" a b)
  56. (+ a b))
  57. (defcallback sum-unsigned-short :unsigned-short
  58. ((a :unsigned-short) (b :unsigned-short))
  59. ;(format t "~%}}} a: ~A, b: ~A {{{~%" a b)
  60. (+ a b))
  61. (defcallback sum-int :int ((a :int) (b :int))
  62. (+ a b))
  63. (defcallback sum-unsigned-int :unsigned-int
  64. ((a :unsigned-int) (b :unsigned-int))
  65. (+ a b))
  66. (defcallback sum-long :long ((a :long) (b :long))
  67. (+ a b))
  68. (defcallback sum-unsigned-long :unsigned-long
  69. ((a :unsigned-long) (b :unsigned-long))
  70. (+ a b))
  71. #-cffi-sys::no-long-long
  72. (progn
  73. (defcallback sum-long-long :long-long
  74. ((a :long-long) (b :long-long))
  75. (+ a b))
  76. (defcallback sum-unsigned-long-long :unsigned-long-long
  77. ((a :unsigned-long-long) (b :unsigned-long-long))
  78. (+ a b)))
  79. (defcallback sum-float :float ((a :float) (b :float))
  80. ;(format t "~%}}} a: ~A, b: ~A {{{~%" a b)
  81. (+ a b))
  82. (defcallback sum-double :double ((a :double) (b :double))
  83. ;(format t "~%}}} a: ~A, b: ~A {{{~%" a b)
  84. (+ a b))
  85. #+(and scl long-float)
  86. (defcallback sum-long-double :long-double ((a :long-double) (b :long-double))
  87. ;(format t "~%}}} a: ~A, b: ~A {{{~%" a b)
  88. (+ a b))
  89. (defcallback sum-pointer :pointer ((ptr :pointer) (offset :int))
  90. (inc-pointer ptr offset))
  91. (defcallback lisp-strcat :string ((a :string) (b :string))
  92. (concatenate 'string a b))
  93. (deftest callbacks.char
  94. (expect-char-sum (get-callback 'sum-char))
  95. 1)
  96. (deftest callbacks.unsigned-char
  97. (expect-unsigned-char-sum (get-callback 'sum-unsigned-char))
  98. 1)
  99. (deftest callbacks.short
  100. (expect-short-sum (callback sum-short))
  101. 1)
  102. (deftest callbacks.unsigned-short
  103. (expect-unsigned-short-sum (callback sum-unsigned-short))
  104. 1)
  105. (deftest callbacks.int
  106. (expect-int-sum (callback sum-int))
  107. 1)
  108. (deftest callbacks.unsigned-int
  109. (expect-unsigned-int-sum (callback sum-unsigned-int))
  110. 1)
  111. (deftest callbacks.long
  112. (expect-long-sum (callback sum-long))
  113. 1)
  114. (deftest callbacks.unsigned-long
  115. (expect-unsigned-long-sum (callback sum-unsigned-long))
  116. 1)
  117. #-cffi-sys::no-long-long
  118. (progn
  119. (deftest (callbacks.long-long :expected-to-fail (alexandria:featurep :openmcl))
  120. (expect-long-long-sum (callback sum-long-long))
  121. 1)
  122. (deftest callbacks.unsigned-long-long
  123. (expect-unsigned-long-long-sum (callback sum-unsigned-long-long))
  124. 1))
  125. (deftest callbacks.float
  126. (expect-float-sum (callback sum-float))
  127. 1)
  128. (deftest callbacks.double
  129. (expect-double-sum (callback sum-double))
  130. 1)
  131. #+(and scl long-float)
  132. (deftest callbacks.long-double
  133. (expect-long-double-sum (callback sum-long-double))
  134. 1)
  135. (deftest callbacks.pointer
  136. (expect-pointer-sum (callback sum-pointer))
  137. 1)
  138. (deftest callbacks.string
  139. (expect-strcat (callback lisp-strcat))
  140. 1)
  141. #-cffi-sys::no-foreign-funcall
  142. (defcallback return-a-string-not-nil :string ()
  143. "abc")
  144. #-cffi-sys::no-foreign-funcall
  145. (deftest callbacks.string-not-docstring
  146. (foreign-funcall-pointer (callback return-a-string-not-nil) () :string)
  147. "abc")
  148. (defcallback check-for-nil :boolean ((pointer :pointer))
  149. (null pointer))
  150. #-cffi-sys::no-foreign-funcall
  151. (deftest callbacks.nil-for-null
  152. (foreign-funcall-pointer (callback check-for-nil) nil
  153. :pointer (null-pointer) :boolean)
  154. nil)
  155. ;;; This one tests mem-aref too.
  156. (defcfun "qsort" :void
  157. (base :pointer)
  158. (nmemb :int)
  159. (size :int)
  160. (fun-compar :pointer))
  161. (defcallback < :int ((a :pointer) (b :pointer))
  162. (let ((x (mem-ref a :int))
  163. (y (mem-ref b :int)))
  164. (cond ((> x y) 1)
  165. ((< x y) -1)
  166. (t 0))))
  167. (deftest callbacks.qsort
  168. (with-foreign-object (array :int 10)
  169. ;; Initialize array.
  170. (loop for i from 0 and n in '(7 2 10 4 3 5 1 6 9 8)
  171. do (setf (mem-aref array :int i) n))
  172. ;; Sort it.
  173. (qsort array 10 (foreign-type-size :int) (callback <))
  174. ;; Return it as a list.
  175. (loop for i from 0 below 10
  176. collect (mem-aref array :int i)))
  177. (1 2 3 4 5 6 7 8 9 10))
  178. ;;; void callback
  179. (defparameter *int* -1)
  180. (defcfun "pass_int_ref" :void (f :pointer))
  181. ;;; CMUCL chokes on this one for some reason.
  182. #-(and darwin cmucl)
  183. (defcallback read-int-from-pointer :void ((a :pointer))
  184. (setq *int* (mem-ref a :int)))
  185. #+(and darwin cmucl)
  186. (pushnew 'callbacks.void rtest::*expected-failures*)
  187. (deftest callbacks.void
  188. (progn
  189. (pass-int-ref (callback read-int-from-pointer))
  190. *int*)
  191. 1984)
  192. ;;; test funcalling of a callback and also declarations inside
  193. ;;; callbacks.
  194. #-cffi-sys::no-foreign-funcall
  195. (progn
  196. (defcallback sum-2 :int ((a :int) (b :int) (c :int))
  197. (declare (ignore c))
  198. (+ a b))
  199. (deftest callbacks.funcall.1
  200. (foreign-funcall-pointer (callback sum-2) () :int 2 :int 3 :int 1 :int)
  201. 5)
  202. (defctype foo-float :float)
  203. (defcallback sum-2f foo-float
  204. ((a foo-float) (b foo-float) (c foo-float) (d foo-float) (e foo-float))
  205. "This one ignores the middle 3 arguments."
  206. (declare (ignore b c))
  207. (declare (ignore d))
  208. (+ a e))
  209. (deftest callbacks.funcall.2
  210. (foreign-funcall-pointer (callback sum-2f) () foo-float 1.0 foo-float 2.0
  211. foo-float 3.0 foo-float 4.0 foo-float 5.0
  212. foo-float)
  213. 6.0))
  214. ;;; (cb-test :no-long-long t)
  215. (defcfun "call_sum_127_no_ll" :long (cb :pointer))
  216. ;;; CMUCL and CCL choke on this one.
  217. #-(or cmucl clozure
  218. #.(cl:if (cl:>= cl:lambda-parameters-limit 127) '(:or) '(:and)))
  219. (defcallback sum-127-no-ll :long
  220. ((a1 :unsigned-long) (a2 :pointer) (a3 :long) (a4 :double)
  221. (a5 :unsigned-long) (a6 :float) (a7 :float) (a8 :int) (a9 :unsigned-int)
  222. (a10 :double) (a11 :double) (a12 :double) (a13 :pointer)
  223. (a14 :unsigned-short) (a15 :unsigned-short) (a16 :pointer) (a17 :long)
  224. (a18 :long) (a19 :int) (a20 :short) (a21 :unsigned-short)
  225. (a22 :unsigned-short) (a23 :char) (a24 :long) (a25 :pointer) (a26 :pointer)
  226. (a27 :char) (a28 :unsigned-char) (a29 :unsigned-long) (a30 :short)
  227. (a31 :int) (a32 :int) (a33 :unsigned-char) (a34 :short) (a35 :long)
  228. (a36 :long) (a37 :pointer) (a38 :unsigned-short) (a39 :char) (a40 :double)
  229. (a41 :unsigned-short) (a42 :pointer) (a43 :short) (a44 :unsigned-long)
  230. (a45 :unsigned-short) (a46 :float) (a47 :unsigned-char) (a48 :short)
  231. (a49 :float) (a50 :short) (a51 :char) (a52 :unsigned-long)
  232. (a53 :unsigned-long) (a54 :char) (a55 :float) (a56 :long) (a57 :pointer)
  233. (a58 :short) (a59 :float) (a60 :unsigned-int) (a61 :float)
  234. (a62 :unsigned-int) (a63 :double) (a64 :unsigned-int) (a65 :unsigned-char)
  235. (a66 :int) (a67 :long) (a68 :char) (a69 :short) (a70 :double) (a71 :int)
  236. (a72 :pointer) (a73 :char) (a74 :unsigned-short) (a75 :pointer)
  237. (a76 :unsigned-short) (a77 :pointer) (a78 :unsigned-long) (a79 :double)
  238. (a80 :pointer) (a81 :long) (a82 :float) (a83 :unsigned-short)
  239. (a84 :unsigned-short) (a85 :pointer) (a86 :float) (a87 :int)
  240. (a88 :unsigned-int) (a89 :double) (a90 :float) (a91 :long) (a92 :pointer)
  241. (a93 :unsigned-short) (a94 :float) (a95 :unsigned-char) (a96 :unsigned-char)
  242. (a97 :float) (a98 :unsigned-int) (a99 :float) (a100 :unsigned-short)
  243. (a101 :double) (a102 :unsigned-short) (a103 :unsigned-long)
  244. (a104 :unsigned-int) (a105 :unsigned-long) (a106 :pointer)
  245. (a107 :unsigned-char) (a108 :char) (a109 :char) (a110 :unsigned-short)
  246. (a111 :unsigned-long) (a112 :float) (a113 :short) (a114 :pointer)
  247. (a115 :long) (a116 :unsigned-short) (a117 :short) (a118 :double)
  248. (a119 :short) (a120 :int) (a121 :char) (a122 :unsigned-long) (a123 :long)
  249. (a124 :int) (a125 :pointer) (a126 :double) (a127 :unsigned-char))
  250. (let ((args (list a1 (pointer-address a2) a3 (floor a4) a5 (floor a6)
  251. (floor a7) a8 a9 (floor a10) (floor a11) (floor a12)
  252. (pointer-address a13) a14 a15 (pointer-address a16) a17 a18
  253. a19 a20 a21 a22 a23 a24 (pointer-address a25)
  254. (pointer-address a26) a27 a28 a29 a30 a31 a32 a33 a34 a35
  255. a36 (pointer-address a37) a38 a39 (floor a40) a41
  256. (pointer-address a42) a43 a44 a45 (floor a46) a47 a48
  257. (floor a49) a50 a51 a52 a53 a54 (floor a55) a56
  258. (pointer-address a57) a58 (floor a59) a60 (floor a61) a62
  259. (floor a63) a64 a65 a66 a67 a68 a69 (floor a70) a71
  260. (pointer-address a72) a73 a74 (pointer-address a75) a76
  261. (pointer-address a77) a78 (floor a79) (pointer-address a80)
  262. a81 (floor a82) a83 a84 (pointer-address a85) (floor a86)
  263. a87 a88 (floor a89) (floor a90) a91 (pointer-address a92)
  264. a93 (floor a94) a95 a96 (floor a97) a98 (floor a99) a100
  265. (floor a101) a102 a103 a104 a105 (pointer-address a106) a107
  266. a108 a109 a110 a111 (floor a112) a113 (pointer-address a114)
  267. a115 a116 a117 (floor a118) a119 a120 a121 a122 a123 a124
  268. (pointer-address a125) (floor a126) a127)))
  269. #-(and)
  270. (loop for i from 1 and arg in args do
  271. (format t "a~A: ~A~%" i arg))
  272. (reduce #'+ args)))
  273. #+(or openmcl cmucl (and darwin (or allegro lispworks)))
  274. (push 'callbacks.bff.1 regression-test::*expected-failures*)
  275. #+#.(cl:if (cl:>= cl:lambda-parameters-limit 127) '(:and) '(:or))
  276. (deftest callbacks.bff.1
  277. (call-sum-127-no-ll (callback sum-127-no-ll))
  278. 2008547941)
  279. ;;; (cb-test)
  280. #-(or cffi-sys::no-long-long
  281. #.(cl:if (cl:>= cl:lambda-parameters-limit 127) '(or) '(and)))
  282. (progn
  283. (defcfun "call_sum_127" :long-long (cb :pointer))
  284. ;;; CMUCL and CCL choke on this one.
  285. #-(or cmucl clozure)
  286. (defcallback sum-127 :long-long
  287. ((a1 :short) (a2 :char) (a3 :pointer) (a4 :float) (a5 :long) (a6 :double)
  288. (a7 :unsigned-long-long) (a8 :unsigned-short) (a9 :unsigned-char)
  289. (a10 :char) (a11 :char) (a12 :unsigned-short) (a13 :unsigned-long-long)
  290. (a14 :unsigned-short) (a15 :long-long) (a16 :unsigned-short)
  291. (a17 :unsigned-long-long) (a18 :unsigned-char) (a19 :unsigned-char)
  292. (a20 :unsigned-long-long) (a21 :long-long) (a22 :char) (a23 :float)
  293. (a24 :unsigned-int) (a25 :float) (a26 :float) (a27 :unsigned-int)
  294. (a28 :float) (a29 :char) (a30 :unsigned-char) (a31 :long) (a32 :long-long)
  295. (a33 :unsigned-char) (a34 :double) (a35 :long) (a36 :double)
  296. (a37 :unsigned-int) (a38 :unsigned-short) (a39 :long-long)
  297. (a40 :unsigned-int) (a41 :int) (a42 :unsigned-long-long) (a43 :long)
  298. (a44 :short) (a45 :unsigned-int) (a46 :unsigned-int)
  299. (a47 :unsigned-long-long) (a48 :unsigned-int) (a49 :long) (a50 :pointer)
  300. (a51 :unsigned-char) (a52 :char) (a53 :long-long) (a54 :unsigned-short)
  301. (a55 :unsigned-int) (a56 :float) (a57 :unsigned-char) (a58 :unsigned-long)
  302. (a59 :long-long) (a60 :float) (a61 :long) (a62 :float) (a63 :int)
  303. (a64 :float) (a65 :unsigned-short) (a66 :unsigned-long-long) (a67 :short)
  304. (a68 :unsigned-long) (a69 :long) (a70 :char) (a71 :unsigned-short)
  305. (a72 :long-long) (a73 :short) (a74 :double) (a75 :pointer)
  306. (a76 :unsigned-int) (a77 :char) (a78 :unsigned-int) (a79 :pointer)
  307. (a80 :pointer) (a81 :unsigned-char) (a82 :pointer) (a83 :unsigned-short)
  308. (a84 :unsigned-char) (a85 :long) (a86 :pointer) (a87 :char) (a88 :long)
  309. (a89 :unsigned-short) (a90 :unsigned-char) (a91 :double)
  310. (a92 :unsigned-long-long) (a93 :unsigned-short) (a94 :unsigned-short)
  311. (a95 :unsigned-int) (a96 :long) (a97 :char) (a98 :long) (a99 :char)
  312. (a100 :short) (a101 :unsigned-short) (a102 :unsigned-long)
  313. (a103 :unsigned-long) (a104 :short) (a105 :long-long) (a106 :long-long)
  314. (a107 :long-long) (a108 :double) (a109 :unsigned-short)
  315. (a110 :unsigned-char) (a111 :short) (a112 :unsigned-char) (a113 :long)
  316. (a114 :long-long) (a115 :unsigned-long-long) (a116 :unsigned-int)
  317. (a117 :unsigned-long) (a118 :unsigned-char) (a119 :long-long)
  318. (a120 :unsigned-char) (a121 :unsigned-long-long) (a122 :double)
  319. (a123 :unsigned-char) (a124 :long-long) (a125 :unsigned-char)
  320. (a126 :char) (a127 :long-long))
  321. (+ a1 a2 (pointer-address a3) (values (floor a4)) a5 (values (floor a6))
  322. a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22
  323. (values (floor a23)) a24 (values (floor a25)) (values (floor a26))
  324. a27 (values (floor a28)) a29 a30 a31 a32 a33 (values (floor a34))
  325. a35 (values (floor a36)) a37 a38 a39 a40 a41 a42 a43 a44 a45 a46 a47
  326. a48 a49 (pointer-address a50) a51 a52 a53 a54 a55 (values (floor a56))
  327. a57 a58 a59 (values (floor a60)) a61 (values (floor a62)) a63
  328. (values (floor a64)) a65 a66 a67 a68 a69 a70 a71 a72 a73
  329. (values (floor a74)) (pointer-address a75) a76 a77 a78
  330. (pointer-address a79) (pointer-address a80) a81 (pointer-address a82)
  331. a83 a84 a85 (pointer-address a86) a87 a88 a89 a90 (values (floor a91))
  332. a92 a93 a94 a95 a96 a97 a98 a99 a100 a101 a102 a103 a104 a105 a106 a107
  333. (values (floor a108)) a109 a110 a111 a112 a113 a114 a115 a116 a117 a118
  334. a119 a120 a121 (values (floor a122)) a123 a124 a125 a126 a127))
  335. #+(or openmcl cmucl)
  336. (push 'callbacks.bff.2 rtest::*expected-failures*)
  337. (deftest callbacks.bff.2
  338. (call-sum-127 (callback sum-127))
  339. 8166570665645582011))
  340. ;;; regression test: (callback non-existant-callback) should throw an error
  341. (deftest callbacks.non-existant
  342. (not (null (nth-value 1 (ignore-errors (callback doesnt-exist)))))
  343. t)
  344. ;;; Handling many arguments of type double. Many lisps (used to) fail
  345. ;;; this one on darwin/ppc. This test might be bogus due to floating
  346. ;;; point arithmetic rounding errors.
  347. ;;;
  348. ;;; CMUCL chokes on this one.
  349. #-(and darwin cmucl)
  350. (defcallback double26 :double
  351. ((a1 :double) (a2 :double) (a3 :double) (a4 :double) (a5 :double)
  352. (a6 :double) (a7 :double) (a8 :double) (a9 :double) (a10 :double)
  353. (a11 :double) (a12 :double) (a13 :double) (a14 :double) (a15 :double)
  354. (a16 :double) (a17 :double) (a18 :double) (a19 :double) (a20 :double)
  355. (a21 :double) (a22 :double) (a23 :double) (a24 :double) (a25 :double)
  356. (a26 :double))
  357. (let ((args (list a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15
  358. a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26)))
  359. #-(and)
  360. (loop for i from 1 and arg in args do
  361. (format t "a~A: ~A~%" i arg))
  362. (reduce #'+ args)))
  363. (defcfun "call_double26" :double (f :pointer))
  364. #+(and darwin (or allegro cmucl))
  365. (pushnew 'callbacks.double26 rtest::*expected-failures*)
  366. (deftest callbacks.double26
  367. (call-double26 (callback double26))
  368. 81.64d0)
  369. #+(and darwin cmucl)
  370. (pushnew 'callbacks.double26.funcall rtest::*expected-failures*)
  371. #-cffi-sys::no-foreign-funcall
  372. (deftest callbacks.double26.funcall
  373. (foreign-funcall-pointer
  374. (callback double26) () :double 3.14d0 :double 3.14d0
  375. :double 3.14d0 :double 3.14d0 :double 3.14d0 :double 3.14d0
  376. :double 3.14d0 :double 3.14d0 :double 3.14d0 :double 3.14d0
  377. :double 3.14d0 :double 3.14d0 :double 3.14d0 :double 3.14d0
  378. :double 3.14d0 :double 3.14d0 :double 3.14d0 :double 3.14d0
  379. :double 3.14d0 :double 3.14d0 :double 3.14d0 :double 3.14d0
  380. :double 3.14d0 :double 3.14d0 :double 3.14d0 :double 3.14d0
  381. :double)
  382. 81.64d0)
  383. ;;; Same as above, for floats.
  384. #-(and darwin cmucl)
  385. (defcallback float26 :float
  386. ((a1 :float) (a2 :float) (a3 :float) (a4 :float) (a5 :float)
  387. (a6 :float) (a7 :float) (a8 :float) (a9 :float) (a10 :float)
  388. (a11 :float) (a12 :float) (a13 :float) (a14 :float) (a15 :float)
  389. (a16 :float) (a17 :float) (a18 :float) (a19 :float) (a20 :float)
  390. (a21 :float) (a22 :float) (a23 :float) (a24 :float) (a25 :float)
  391. (a26 :float))
  392. (let ((args (list a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15
  393. a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26)))
  394. #-(and)
  395. (loop for i from 1 and arg in args do
  396. (format t "a~A: ~A~%" i arg))
  397. (reduce #'+ args)))
  398. (defcfun "call_float26" :float (f :pointer))
  399. #+(and darwin (or lispworks openmcl cmucl))
  400. (pushnew 'callbacks.float26 regression-test::*expected-failures*)
  401. (deftest callbacks.float26
  402. (call-float26 (callback float26))
  403. 130.0)
  404. #+(and darwin (or lispworks openmcl cmucl))
  405. (pushnew 'callbacks.float26.funcall regression-test::*expected-failures*)
  406. #-cffi-sys::no-foreign-funcall
  407. (deftest callbacks.float26.funcall
  408. (foreign-funcall-pointer
  409. (callback float26) () :float 5.0 :float 5.0
  410. :float 5.0 :float 5.0 :float 5.0 :float 5.0
  411. :float 5.0 :float 5.0 :float 5.0 :float 5.0
  412. :float 5.0 :float 5.0 :float 5.0 :float 5.0
  413. :float 5.0 :float 5.0 :float 5.0 :float 5.0
  414. :float 5.0 :float 5.0 :float 5.0 :float 5.0
  415. :float 5.0 :float 5.0 :float 5.0 :float 5.0
  416. :float)
  417. 130.0)
  418. ;;; Defining a callback as a non-toplevel form. Not portable. Doesn't
  419. ;;; work for CMUCL or Allegro.
  420. #-(and)
  421. (let ((n 42))
  422. (defcallback non-toplevel-cb :int ()
  423. n))
  424. #-(and)
  425. (deftest callbacks.non-toplevel
  426. (foreign-funcall (callback non-toplevel-cb) :int)
  427. 42)
  428. ;;;# Stdcall
  429. #+(and x86 (not cffi-sys::no-stdcall))
  430. (progn
  431. (defcallback (stdcall-cb :convention :stdcall) :int
  432. ((a :int) (b :int) (c :int))
  433. (+ a b c))
  434. (defcfun "call_stdcall_fun" :int
  435. (f :pointer))
  436. (deftest callbacks.stdcall.1
  437. (call-stdcall-fun (callback stdcall-cb))
  438. 42))
  439. ;;; RT: many of the %DEFCALLBACK implementations wouldn't handle
  440. ;;; uninterned symbols.
  441. (deftest callbacks.uninterned
  442. (values (defcallback #1=#:foo :void ())
  443. (pointerp (callback #1#)))
  444. #1# t)