0.9.2.43:
[sbcl/simd.git] / tests / alien.impure.lisp
blob8984f7dc1c36f409a2563273aff1fcf3b2f8b5f0
1 ;;;; This file is for compiler tests which have side effects (e.g.
2 ;;;; executing DEFUN) but which don't need any special side-effecting
3 ;;;; environmental stuff (e.g. DECLAIM of particular optimization
4 ;;;; settings). Similar tests which *do* expect special settings may
5 ;;;; be in files compiler-1.impure.lisp, compiler-2.impure.lisp, etc.
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; While most of SBCL is derived from the CMU CL system, the test
11 ;;;; files (like this one) were written from scratch after the fork
12 ;;;; from CMU CL.
13 ;;;;
14 ;;;; This software is in the public domain and is provided with
15 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
16 ;;;; more information.
18 (cl:in-package :cl-user)
20 ;;; In sbcl-0.6.10, Douglas Brebner reported that (SETF EXTERN-ALIEN)
21 ;;; was messed up so badly that trying to execute expressions like
22 ;;; this signalled an error.
23 (setf (sb-alien:extern-alien "current_control_stack_pointer" sb-alien:unsigned)
24 (sb-alien:extern-alien "current_control_stack_pointer" sb-alien:unsigned))
26 ;;; bug 133, fixed in 0.7.0.5: Somewhere in 0.pre7.*, C void returns
27 ;;; were broken ("unable to use values types here") when
28 ;;; auto-PROCLAIM-of-return-value was added to DEFINE-ALIEN-ROUTINE.
29 (sb-alien:define-alien-routine ("free" free) void (ptr (* t) :in))
31 ;;; Types of alien functions were being incorrectly DECLAIMED when
32 ;;; docstrings were included in the definition until sbcl-0.7.6.15.
33 (sb-alien:define-alien-routine ("getenv" ftype-correctness) c-string
34 "docstring"
35 (name c-string))
37 (multiple-value-bind (function warningsp failurep)
38 (compile nil '(lambda () (ftype-correctness)))
39 (assert warningsp))
41 (multiple-value-bind (function warningsp failurep)
42 (compile nil '(lambda () (ftype-correctness "FOO")))
43 (assert (not warningsp)))
45 (multiple-value-bind (function warningsp failurep)
46 (compile nil '(lambda () (ftype-correctness "FOO" "BAR")))
47 (assert warningsp))
49 ;;; This used to break due to too eager auxiliary type twiddling in
50 ;;; parse-alien-record-type.
51 (defparameter *maybe* nil)
52 (defun with-alien-test-for-struct-plus-funcall ()
53 (with-alien ((x (struct bar (x unsigned) (y unsigned)))
54 ;; bogus definition, but we just need the symbol
55 (f (function int (* (struct bar))) :extern "printf"))
56 (when *maybe*
57 (alien-funcall f (addr x)))))
59 ;;; Mutually referent structures
60 (define-alien-type struct.1 (struct struct.1 (x (* (struct struct.2))) (y int)))
61 (define-alien-type struct.2 (struct struct.2 (x (* (struct struct.1))) (y int)))
62 (let ((s1 (make-alien struct.1))
63 (s2 (make-alien struct.2)))
64 (setf (slot s1 'x) s2
65 (slot s2 'x) s1
66 (slot (slot s1 'x) 'y) 1
67 (slot (slot s2 'x) 'y) 2)
68 (assert (= 1 (slot (slot s1 'x) 'y)))
69 (assert (= 2 (slot (slot s2 'x) 'y))))
71 ;;; "Alien bug" on sbcl-devel 2004-10-11 by Thomas F. Burdick caused
72 ;;; by recursive struct definition.
73 (let ((fname "alien-bug-2004-10-11.tmp.lisp"))
74 (unwind-protect
75 (progn
76 (with-open-file (f fname :direction :output)
77 (mapc (lambda (form) (print form f))
78 '((defpackage :alien-bug
79 (:use :cl :sb-alien))
80 (in-package :alien-bug)
81 (define-alien-type objc-class
82 (struct objc-class
83 (protocols
84 (* (struct protocol-list
85 (list (array (* (struct objc-class))))))))))))
86 (load fname)
87 (load fname)
88 (load (compile-file fname))
89 (load (compile-file fname)))
90 (delete-file (compile-file-pathname fname))
91 (delete-file fname)))
93 ;;; enumerations with only one enum resulted in division-by-zero
94 ;;; reported on sbcl-help 2004-11-16 by John Morrison
95 (define-alien-type enum.1 (enum nil (:val0 0)))
97 (define-alien-type enum.2 (enum nil (zero 0) (one 1) (two 2) (three 3)
98 (four 4) (five 5) (six 6) (seven 7)
99 (eight 8) (nine 9)))
100 (with-alien ((integer-array (array int 3)))
101 (let ((enum-array (cast integer-array (array enum.2 3))))
102 (setf (deref enum-array 0) 'three
103 (deref enum-array 1) 'four)
104 (setf (deref integer-array 2) (+ (deref integer-array 0)
105 (deref integer-array 1)))
106 (assert (eql (deref enum-array 2) 'seven))))
107 ;; The code that is used for mapping from integers to symbols depends on the
108 ;; `density' of the set of used integers, so test with a sparse set as well.
109 (define-alien-type enum.3 (enum nil (zero 0) (one 1) (k-one 1001) (k-two 1002)))
110 (with-alien ((integer-array (array int 3)))
111 (let ((enum-array (cast integer-array (array enum.3 3))))
112 (setf (deref enum-array 0) 'one
113 (deref enum-array 1) 'k-one)
114 (setf (deref integer-array 2) (+ (deref integer-array 0)
115 (deref integer-array 1)))
116 (assert (eql (deref enum-array 2) 'k-two))))
118 ;;; As reported by Baughn on #lisp, ALIEN-FUNCALL loops forever when
119 ;;; compiled with (DEBUG 3).
120 (sb-kernel::values-specifier-type-cache-clear)
121 (proclaim '(optimize (debug 3)))
122 (let ((f (compile nil '(lambda (v)
123 (sb-alien:alien-funcall (sb-alien:extern-alien "getenv"
124 (function (c-string) c-string))
125 v)))))
126 (assert (typep (funcall f "HOME") '(or string null))))
128 ;;; success
129 (quit :unix-status 104)