1.0.9.42: remove stale CALLS support
[sbcl.git] / tests / callback.impure.lisp
blob34bb23f6a6b94c3cacc16f3910a354bd8927a6d7
1 ;;;; package lock tests with side effects
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (in-package :cl-user)
16 ;;; callbacks only on a few platforms
17 #-alien-callbacks
18 (quit :unix-status 104)
20 ;;; simple callback for a function
22 (defun thunk ()
23 (write-string "hi"))
25 (defvar *thunk*
26 (sb-alien::alien-callback (function c-string) #'thunk))
28 (assert (equal (with-output-to-string (*standard-output*)
29 (alien-funcall *thunk*))
30 "hi"))
32 ;;; simple callback for a symbol
34 (defun add-two-ints (arg1 arg2)
35 (+ arg1 arg2))
37 (defvar *add-two-ints*
38 (sb-alien::alien-callback (function int int int) 'add-two-ints))
40 (assert (= (alien-funcall *add-two-ints* 555 444444) 444999))
42 ;;; actually using a callback with foreign code
44 #+win32 (sb-alien:load-shared-object "ntdll.dll")
46 (define-alien-routine qsort void
47 (base (* t))
48 (nmemb int)
49 (size int)
50 (compar (function int (* double) (* double))))
52 (sb-alien::define-alien-callback double*-cmp int ((arg1 (* double)) (arg2 (* double)))
53 (let ((a1 (deref arg1))
54 (a2 (deref arg2)))
55 (cond ((= a1 a2) 0)
56 ((< a1 a2) -1)
57 (t 1))))
59 (let* ((vector (coerce '(0.1d0 0.5d0 0.2d0 1.2d0 1.5d0 2.5d0 0.0d0 0.1d0 0.2d0 0.3d0)
60 '(vector double-float)))
61 (sorted (sort (copy-seq vector) #'<)))
62 (gc :full t)
63 (sb-sys:with-pinned-objects (vector)
64 (qsort (sb-sys:vector-sap vector)
65 (length vector)
66 (alien-size double :bytes)
67 double*-cmp))
68 (assert (equalp vector sorted)))
70 ;;; returning floats
72 (sb-alien::define-alien-callback redefined-fun int ()
75 (eval
76 '(sb-alien::define-alien-callback redefined-fun int ()
77 42))
79 (assert (= 42 (alien-funcall redefined-fun)))
81 (sb-alien::define-alien-callback return-single float ((x float))
84 (sb-alien::define-alien-callback return-double double ((x double))
87 (defconstant spi (coerce pi 'single-float))
89 (assert (= spi (alien-funcall return-single spi)))
90 (assert (= pi (alien-funcall return-double pi)))
92 ;;; invalidation
94 (sb-alien::define-alien-callback to-be-invalidated int ()
97 (assert (= 5 (alien-funcall to-be-invalidated)))
99 (multiple-value-bind (p valid) (sb-alien::alien-callback-p to-be-invalidated)
100 (assert p)
101 (assert valid))
103 (sb-alien::invalidate-alien-callback to-be-invalidated)
105 (multiple-value-bind (p valid) (sb-alien::alien-callback-p to-be-invalidated)
106 (assert p)
107 (assert (not valid)))
109 (multiple-value-bind (res err)
110 (ignore-errors (alien-funcall to-be-invalidated))
111 (assert (and (not res) (typep err 'error))))
113 ;;; getting and setting the underlying function
115 (sb-alien::define-alien-callback foo int ()
118 (defvar *foo* #'foo)
120 (assert (eq #'foo (sb-alien::alien-callback-function foo)))
122 (defun bar ()
125 (setf (sb-alien::alien-callback-function foo) #'bar)
127 (assert (eq #'bar (sb-alien::alien-callback-function foo)))
129 (assert (= 26 (alien-funcall foo)))
131 ;;; callbacks with void return values
133 (with-test (:name void-return)
134 (sb-alien::alien-lambda void ()
135 (values)))
137 ;;; tests for a sign extension problem in callback argument handling on x86-64
139 (defvar *add-two-ints* (sb-alien::alien-callback (function int int int) #'+))
141 (with-test (:name :sign-extension)
142 (assert (= (alien-funcall *add-two-ints* #x-80000000 1) -2147483647)))
144 ;;; On x86 This'll signal a TYPE-ERROR "The value -2147483649 is not of type
145 ;;; (SIGNED-BYTE 32)". On x86-64 it'll wrap around to 2147483647, probably
146 ;;; due to the sign-extension done by the (INTEGER :NATURALIZE-GEN)
147 ;;; alien-type-method. I believe the former behaviour is the one we want.
148 ;;; -- JES, 2005-10-16
150 (with-test (:name :underflow-detection :fails-on :x86-64)
151 (assert (raises-error? (alien-funcall *add-two-ints* #x-80000000 -1))))
153 ;;; tests for handling 64-bit arguments - this was causing problems on
154 ;;; ppc - CLH, 2005-12-01
156 (defvar *add-two-long-longs*
157 (sb-alien::alien-callback
158 (function (integer 64) (integer 64) (integer 64)) 'add-two-ints))
159 (with-test (:name :long-long-callback-arg)
160 (assert (= (alien-funcall *add-two-long-longs*
161 (ash 1 60)
162 (- (ash 1 59)))
163 (ash 1 59))))
165 (defvar *add-two-unsigned-long-longs*
166 (sb-alien::alien-callback
167 (function (unsigned 64) (unsigned 64) (unsigned 64))
168 'add-two-ints))
169 (with-test (:name :unsigned-long-long-callback-arg)
170 (assert (= (alien-funcall *add-two-unsigned-long-longs*
171 (ash 1 62)
172 (ash 1 62))
173 (ash 1 63))))
175 ;;; test for callbacks of various arities
176 ;;; CLH 2005-12-21
178 (defmacro alien-apply-form (f args)
179 `(let ((a ,args))
180 `(alien-funcall ,,f ,@a)))
182 (defmacro alien-apply (f &rest args)
183 `(eval (alien-apply-form ,f ,@args)))
185 (defun iota (x) (if (equalp x 1) (list x) (cons x (iota (1- x)))))
187 (defparameter *type-abbreviations*
188 '((sb-alien:char . "c")
189 (sb-alien:unsigned-char . "uc")
190 (sb-alien:short . "h")
191 (sb-alien:unsigned-short . "uh")
192 (sb-alien:int . "i")
193 (sb-alien:unsigned-int . "ui")
194 ((sb-alien:integer 64) . "l")
195 ((sb-alien:unsigned 64) . "ul")
196 (sb-alien:float . "f")
197 (sb-alien:double . "d")))
199 (defun parse-callback-arg-spec (spec)
200 (let ((l (coerce spec 'list)))
201 (loop for g in l by #'cddr
202 collect (car (rassoc (string-downcase g) *type-abbreviations* :test #'equal)))))
204 (defmacro define-callback-adder (&rest types)
205 (let ((fname (format nil "*add-~{~A~^-~}*"
206 (mapcar
207 #'(lambda (x)
208 (cdr (assoc x *type-abbreviations*)))
209 (mapcar
210 #'(lambda (y) (find-symbol (string-upcase y) 'sb-alien))
211 (cdr types))))))
212 `(progn
213 (defparameter ,(intern
214 (string-upcase fname))
215 (sb-alien::alien-callback (function ,@types) '+)))))
217 (with-test (:name :define-2-int-callback)
218 (define-callback-adder int int int))
219 (with-test (:name :call-2-int-callback)
220 (assert (= (alien-apply *add-i-i* (iota 2)) 3)))
222 (with-test (:name :define-3-int-callback)
223 (define-callback-adder int int int int))
224 (with-test (:name :call-3-int-callback)
225 (assert (= (alien-apply *add-i-i-i* (iota 3)) 6)))
227 (with-test (:name :define-4-int-callback)
228 (define-callback-adder int int int int int))
229 (with-test (:name :call-4-int-callback)
230 (assert (= (alien-apply *add-i-i-i-i* (iota 4)) 10)))
232 (with-test (:name :define-5-int-callback)
233 (define-callback-adder int int int int int int))
234 (with-test (:name :call-5-int-callback)
235 (assert (= (alien-apply *add-i-i-i-i-i* (iota 5)) 15)))
237 (with-test (:name :define-6-int-callback)
238 (define-callback-adder int int int int int int int))
239 (with-test (:name :call-6-int-callback)
240 (assert (= (alien-apply *add-i-i-i-i-i-i* (iota 6)) 21)))
242 (with-test (:name :define-7-int-callback)
243 (define-callback-adder int int int int int int int int))
244 (with-test (:name :call-7-int-callback)
245 (assert (= (alien-apply *add-i-i-i-i-i-i-i* (iota 7)) 28)))
247 (with-test (:name :define-8-int-callback)
248 (define-callback-adder int int int int int int int int int))
249 (with-test (:name :call-8-int-callback)
250 (assert (= (alien-apply *add-i-i-i-i-i-i-i-i* (iota 8)) 36)))
252 (with-test (:name :define-9-int-callback)
253 (define-callback-adder int int int int int int int int int int))
254 (with-test (:name :call-9-int-callback)
255 (assert (= (alien-apply *add-i-i-i-i-i-i-i-i-i* (iota 9)) 45)))
257 (with-test (:name :define-10-int-callback)
258 (define-callback-adder int int int int int int int int int int int))
259 (with-test (:name :call-10-int-callback)
260 (assert (= (alien-apply *add-i-i-i-i-i-i-i-i-i-i* (iota 10)) 55)))
262 (with-test (:name :define-11-int-callback)
263 (define-callback-adder int int int int int int int int int int int int))
264 (with-test (:name :call-11-int-callback)
265 (assert (= (alien-apply *add-i-i-i-i-i-i-i-i-i-i-i* (iota 11)) 66)))
267 (with-test (:name :define-12-int-callback)
268 (define-callback-adder int int int int int int int int int int int int int))
269 (with-test (:name :call-12-int-callback)
270 (assert (= (alien-apply *add-i-i-i-i-i-i-i-i-i-i-i-i* (iota 12)) 78)))
272 (with-test (:name :define-2-float-callback)
273 (define-callback-adder float float float))
274 (with-test (:name :call-2-float-callback)
275 (assert (= (alien-apply *add-f-f* (iota 2.0s0)) 3.0s0)))
277 (with-test (:name :define-3-float-callback)
278 (define-callback-adder float float float float))
279 (with-test (:name :call-3-float-callback)
280 (assert (= (alien-apply *add-f-f-f* (iota 3.0s0)) 6.0s0)))
282 (with-test (:name :define-4-float-callback)
283 (define-callback-adder float float float float float))
284 (with-test (:name :call-4-float-callback)
285 (assert (= (alien-apply *add-f-f-f-f* (iota 4.0s0)) 10.0s0)))
287 (with-test (:name :define-5-float-callback)
288 (define-callback-adder float float float float float float))
289 (with-test (:name :call-5-float-callback)
290 (assert (= (alien-apply *add-f-f-f-f-f* (iota 5.0s0)) 15.0s0)))
292 (with-test (:name :define-6-float-callback)
293 (define-callback-adder float float float float float float float))
294 (with-test (:name :call-6-float-callback)
295 (assert (= (alien-apply *add-f-f-f-f-f-f* (iota 6.0s0)) 21.0s0)))
297 (with-test (:name :define-7-float-callback)
298 (define-callback-adder float float float float float float float float))
299 (with-test (:name :call-7-float-callback)
300 (assert (= (alien-apply *add-f-f-f-f-f-f-f* (iota 7.0s0)) 28.0s0)))
302 (with-test (:name :define-8-float-callback)
303 (define-callback-adder float float float float float float float float float))
304 (with-test (:name :call-8-float-callback)
305 (assert (= (alien-apply *add-f-f-f-f-f-f-f-f* (iota 8.0s0)) 36.0s0)))
307 (with-test (:name :define-9-float-callback)
308 (define-callback-adder float float float float float float float float float float))
309 (with-test (:name :call-9-float-callback)
310 (assert (= (alien-apply *add-f-f-f-f-f-f-f-f-f* (iota 9.0s0)) 45.0s0)))
312 (with-test (:name :define-10-float-callback)
313 (define-callback-adder float float float float float float float float float float float))
314 (with-test (:name :call-10-float-callback)
315 (assert (= (alien-apply *add-f-f-f-f-f-f-f-f-f-f* (iota 10.0s0)) 55.0s0)))
317 (with-test (:name :define-11-float-callback)
318 (define-callback-adder float float float float float float float float float float float float))
319 (with-test (:name :call-11-float-callback)
320 (assert (= (alien-apply *add-f-f-f-f-f-f-f-f-f-f-f* (iota 11.0s0)) 66.0s0)))
322 (with-test (:name :define-12-float-callback)
323 (define-callback-adder float float float float float float float float float float float float float))
324 (with-test (:name :call-12-float-callback)
325 (assert (= (alien-apply *add-f-f-f-f-f-f-f-f-f-f-f-f* (iota 12.0s0)) 78.0s0)))
327 (with-test (:name :define-2-double-callback)
328 (define-callback-adder double double double))
329 (with-test (:name :call-2-double-callback)
330 (assert (= (alien-apply *add-d-d* (iota 2.0d0)) 3.0d0)))
332 (with-test (:name :define-3-double-callback)
333 (define-callback-adder double double double double))
334 (with-test (:name :call-3-double-callback)
335 (assert (= (alien-apply *add-d-d-d* (iota 3.0d0)) 6.0d0)))
337 (with-test (:name :define-4-double-callback)
338 (define-callback-adder double double double double double))
339 (with-test (:name :call-4-double-callback)
340 (assert (= (alien-apply *add-d-d-d-d* (iota 4.0d0)) 10.0d0)))
342 (with-test (:name :define-5-double-callback)
343 (define-callback-adder double double double double double double))
344 (with-test (:name :call-5-double-callback)
345 (assert (= (alien-apply *add-d-d-d-d-d* (iota 5.0d0)) 15.0d0)))
347 (with-test (:name :define-6-double-callback)
348 (define-callback-adder double double double double double double double))
349 (with-test (:name :call-6-double-callback)
350 (assert (= (alien-apply *add-d-d-d-d-d-d* (iota 6.0d0)) 21.0d0)))
352 (with-test (:name :define-7-double-callback)
353 (define-callback-adder double double double double double double double double))
354 (with-test (:name :call-7-double-callback)
355 (assert (= (alien-apply *add-d-d-d-d-d-d-d* (iota 7.0d0)) 28.0d0)))
357 (with-test (:name :define-8-double-callback)
358 (define-callback-adder double double double double double double double double double))
359 (with-test (:name :call-8-double-callback)
360 (assert (= (alien-apply *add-d-d-d-d-d-d-d-d* (iota 8.0d0)) 36.0d0)))
362 (with-test (:name :define-9-double-callback)
363 (define-callback-adder double double double double double double double double double double))
364 (with-test (:name :call-9-double-callback)
365 (assert (= (alien-apply *add-d-d-d-d-d-d-d-d-d* (iota 9.0d0)) 45.0d0)))
367 (with-test (:name :define-10-double-callback)
368 (define-callback-adder double double double double double double double double double double double))
369 (with-test (:name :call-10-double-callback)
370 (assert (= (alien-apply *add-d-d-d-d-d-d-d-d-d-d* (iota 10.0d0)) 55.0d0)))
372 (with-test (:name :define-11-double-callback)
373 (define-callback-adder double double double double double double double double double double double double))
374 (with-test (:name :call-11-double-callback)
375 (assert (= (alien-apply *add-d-d-d-d-d-d-d-d-d-d-d* (iota 11.0d0)) 66.0d0)))
377 (with-test (:name :define-12-double-callback)
378 (define-callback-adder double double double double double double double double double double double double double))
379 (with-test (:name :call-12-double-callback)
380 (assert (= (alien-apply *add-d-d-d-d-d-d-d-d-d-d-d-d* (iota 12.0d0)) 78.0d0)))
382 (with-test (:name :define-int-float-callback)
383 (define-callback-adder float int float))
384 (with-test (:name :call-int-float-callback)
385 (assert (= (alien-funcall *add-i-f* 1 2.0s0) 3.0s0)))
387 (with-test (:name :define-float-int-callback)
388 (define-callback-adder float float int))
389 (with-test (:name :call-float-int-callback)
390 (assert (= (alien-funcall *add-f-i* 2.0s0 1) 3.0s0)))
392 (with-test (:name :define-int-double-callback)
393 (define-callback-adder double int double))
394 (with-test (:name :call-int-double-callback)
395 (assert (= (alien-funcall *add-i-d* 1 2.0d0) 3.0d0)))
397 (with-test (:name :define-double-int-callback)
398 (define-callback-adder double double int))
399 (with-test (:name :call-double-int-callback)
400 (assert (= (alien-funcall *add-d-i* 2.0d0 1) 3.0d0)))
402 (with-test (:name :define-double-float-callback)
403 (define-callback-adder double double float))
404 (with-test (:name :call-double-float-callback)
405 (assert (= (alien-funcall *add-d-f* 2.0d0 1.0s0) 3.0d0)))
407 (with-test (:name :define-float-double-callback)
408 (define-callback-adder double float double))
409 (with-test (:name :call-double-float-callback)
410 (assert (= (alien-funcall *add-f-d* 1.0s0 2.0d0) 3.0d0)))
412 (with-test (:name :define-double-float-int-callback)
413 (define-callback-adder double double float int))
414 (with-test (:name :call-double-float-int-callback)
415 (assert (= (alien-funcall *add-d-f-i* 2.0d0 1.0s0 1) 4.0d0)))