1 ;;;; tests that dynamic-extent functionality works.
3 ;;;; This software is part of the SBCL system. See the README file for
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
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 (setq sb-c::*check-consistency* t)
16 (defmacro defun-with-dx (name arglist &body body)
18 (declare (optimize sb-c::stack-allocate-dynamic-extent))
22 (declaim (notinline opaque-identity))
23 (defun opaque-identity (x)
27 (defun-with-dx dxlength (&rest rest)
28 (declare (dynamic-extent rest))
31 (assert (= (dxlength 1 2 3) 3))
32 (assert (= (dxlength t t t t t t) 6))
33 (assert (= (dxlength) 0))
36 (destructuring-bind (a b c d e f &rest g) list
37 (+ a b c d e f (length g))))
39 (defun-with-dx dxcaller (&rest rest)
40 (declare (dynamic-extent rest))
42 (assert (= (dxcaller 1 2 3 4 5 6 7) 22))
44 (defun-with-dx dxcaller-align-1 (x &rest rest)
45 (declare (dynamic-extent rest))
47 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7) 39))
48 (assert (= (dxcaller-align-1 17 1 2 3 4 5 6 7 8) 40))
51 (defun-with-dx test-nip-values ()
52 (flet ((bar (x &rest y)
53 (declare (dynamic-extent y))
57 (multiple-value-call #'values
61 (assert (equal (multiple-value-list (test-nip-values)) '(1 5 a)))
63 ;;; LET-variable substitution
64 (defun-with-dx test-let-var-subst1 (x)
65 (let ((y (list x (1- x))))
66 (opaque-identity :foo)
67 (let ((z (the list y)))
68 (declare (dynamic-extent z))
70 (assert (eql (test-let-var-subst1 17) 2))
72 (defun-with-dx test-let-var-subst2 (x)
73 (let ((y (list x (1- x))))
74 (declare (dynamic-extent y))
75 (opaque-identity :foo)
76 (let ((z (the list y)))
78 (assert (eql (test-let-var-subst2 17) 2))
80 ;;; DX propagation through LET-return.
81 (defun-with-dx test-lvar-subst (x)
82 (let ((y (list x (1- x))))
83 (declare (dynamic-extent y))
84 (second (let ((z (the list y)))
85 (opaque-identity :foo)
87 (assert (eql (test-lvar-subst 11) 10))
89 ;;; this code is incorrect, but the compiler should not fail
90 (defun-with-dx test-let-var-subst-incorrect (x)
91 (let ((y (list x (1- x))))
92 (opaque-identity :foo)
93 (let ((z (the list y)))
94 (declare (dynamic-extent z))
95 (opaque-identity :bar)
100 (defun-with-dx test-alignment-dx-list (form)
101 (multiple-value-prog1 (eval form)
102 (let ((l (list 1 2 3 4)))
103 (declare (dynamic-extent l))
104 (setq *x* (copy-list l)))))
106 (let* ((res (loop for i below n collect i))
107 (form `(values ,@res)))
108 (assert (equal (multiple-value-list (test-alignment-dx-list form)) res))
109 (assert (equal *x* '(1 2 3 4)))))
113 (declaim (notinline true))
118 (defun-with-dx dxclosure (x)
121 (declare (dynamic-extent #'f))
124 (assert (eq t (dxclosure 13)))
127 (defmacro assert-no-consing (form &optional times)
128 `(%assert-no-consing (lambda () ,form) ,times))
129 (defun %assert-no-consing (thunk &optional times)
130 (let ((before (get-bytes-consed))
131 (times (or times 10000)))
132 (declare (type (integer 1 *) times))
135 (assert (< (- (get-bytes-consed) before) times))))
137 #+(or x86 x86-64 alpha ppc sparc mips)
139 (assert-no-consing (dxclosure 42))
140 (assert-no-consing (dxlength 1 2 3))
141 (assert-no-consing (dxlength t t t t t t))
142 (assert-no-consing (dxlength))
143 (assert-no-consing (dxcaller 1 2 3 4 5 6 7))
144 (assert-no-consing (test-nip-values))
145 (assert-no-consing (test-let-var-subst1 17))
146 (assert-no-consing (test-let-var-subst2 17))
147 (assert-no-consing (test-lvar-subst 11)))
150 ;;; Bugs found by Paul F. Dietz
157 (declare (optimize (speed 2) (space 0) (safety 0)
158 (debug 1) (compilation-speed 3)))
159 (let* ((v5 (cons b b)))
160 (declare (dynamic-extent v5))
168 ;;; bug reported by Svein Ove Aas
169 (defun svein-2005-ii-07 (x y)
170 (declare (optimize (speed 3) (space 2) (safety 0) (debug 0)))
171 (let ((args (list* y 1 2 x)))
172 (declare (dynamic-extent args))
173 (apply #'aref args)))
177 #3A(((1 1 1) (1 1 1) (1 1 1))
178 ((1 1 1) (1 1 1) (4 1 1))
179 ((1 1 1) (1 1 1) (1 1 1))))
182 ;;; bug reported by Brian Downing: stack-allocated arrays were not
183 ;;; filled with zeroes.
184 (defun-with-dx bdowning-2005-iv-16 ()
185 (let ((a (make-array 11 :initial-element 0)))
186 (declare (dynamic-extent a))
187 (assert (every (lambda (x) (eql x 0)) a))))
188 (bdowning-2005-iv-16)