Avoid forward references to PARSE-mumble-TYPE condition classes.
[sbcl.git] / tests / array.pure.lisp
blobc217edda6129dcc7619ce023fd199443f40c7167
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; While most of SBCL is derived from the CMU CL system, the test
5 ;;;; files (like this one) were written from scratch after the fork
6 ;;;; from CMU CL.
7 ;;;;
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
12 (in-package :cl-user)
14 ;;; Array initialization has complicated defaulting for :ELEMENT-TYPE,
15 ;;; and both compile-time and run-time logic takes a whack at it.
16 (let ((testcases '(;; Bug 126, confusion between high-level default string
17 ;; initial element #\SPACE and low-level default array
18 ;; element #\NULL, is gone.
19 (#\null (make-array 11 :element-type 'character) simple-string)
20 (#\space (make-string 11 :initial-element #\space) string)
21 (#\* (make-string 11 :initial-element #\*))
22 (#\null (make-string 11))
23 (#\null (make-string 11 :initial-element #\null))
24 (#\x (make-string 11 :initial-element #\x))
25 ;; And the other tweaks made when fixing bug 126 didn't
26 ;; mess things up too badly either.
27 (0 (make-array 11) simple-vector)
28 (nil (make-array 11 :initial-element nil))
29 (12 (make-array 11 :initial-element 12))
30 (0 (make-array 11 :element-type '(unsigned-byte 4)) (simple-array (unsigned-byte 4) (*)))
31 (12 (make-array 11
32 :element-type '(unsigned-byte 4)
33 :initial-element 12)))))
34 (dolist (testcase testcases)
35 (destructuring-bind (expected-result form &optional type) testcase
36 (unless (eql expected-result (aref (eval form) 3))
37 (error "expected ~S in EVAL ~S" expected-result form))
38 (unless (eql expected-result
39 (aref (funcall (compile nil `(lambda () ,form))) 3))
40 (error "expected ~S in FUNCALL COMPILE ~S" expected-result form))
41 ;; also do some testing of compilation and verification that
42 ;; errors are thrown appropriately.
43 (unless (eql expected-result
44 (funcall (compile nil `(lambda () (aref ,form 3)))))
45 (error "expected ~S in COMPILED-AREF ~S" expected-result form))
46 (when type
47 (unless (eql expected-result
48 (funcall (compile nil `(lambda () (let ((x ,form))
49 (declare (type ,type x))
50 (aref x 3))))))
51 (error "expected ~S in COMPILED-DECLARED-AREF ~S" expected-result form)))
52 (when (ignore-errors (aref (eval form) 12))
53 (error "error not thrown in EVAL ~S" form))
54 (when (ignore-errors (aref (funcall (compile nil `(lambda () ,form))) 12))
55 (error "error not thrown in FUNCALL COMPILE ~S"))
56 (when (ignore-errors (funcall (compile nil `(lambda () (aref ,form 12)))))
57 (error "error not thrown in COMPILED-AREF ~S" form))
58 (when type
59 (when (ignore-errors (funcall
60 (compile nil `(lambda () (let ((x ,form))
61 (declare (type ,type x))
62 (aref x 12))))))
63 (error "error not thrown in COMPILED-DECLARED-AREF ~S" form))))))
65 ;;; On the SPARC, until sbcl-0.7.7.20, there was a bug in array
66 ;;; references for small vector elements (spotted by Raymond Toy); the
67 ;;; bug persisted on the PPC until sbcl-0.7.8.20.
68 (let (vector)
69 (loop for i below 64
70 for list = (make-list 64 :initial-element 1)
71 do (setf (nth i list) 0)
72 do (setf vector (make-array 64 :element-type 'bit
73 :initial-contents list))
74 do (assert (= (funcall
75 (compile nil
76 `(lambda (rmdr)
77 (declare (type (simple-array bit (*)) rmdr)
78 (optimize (speed 3) (safety 0)))
79 (aref rmdr ,i)))
80 vector)
81 0))))
83 ;;; Following refactoring of sequence functions to detect bad type
84 ;;; specifiers, REVERSE was left broken on vectors with fill pointers.
85 (let ((a (make-array 10
86 :fill-pointer 5
87 :element-type 'character
88 :initial-contents "abcdefghij")))
89 (assert (string= (reverse a) "edcba")))
91 ;;; ARRAY-IN-BOUNDS-P should work when given non-INDEXes as its
92 ;;; subscripts (and return NIL, of course)
93 (with-test (:name array-in-bounds-p)
94 (macrolet
95 ((test-case (array subscript expected)
96 `(progn
97 (assert (,(if expected 'progn 'not)
98 (array-in-bounds-p ,array ,subscript)))
99 (assert (,(if expected 'progn 'not)
100 (funcall (compile nil '(lambda (array subscript)
101 (array-in-bounds-p array subscript)))
102 ,array ,subscript))))))
103 (let ((a (make-array 10 :fill-pointer 5)))
104 (test-case a -1 nil)
105 (test-case a 3 t)
106 (test-case a 7 t)
107 (test-case a 11 nil)
108 (test-case a (1+ most-positive-fixnum) nil))))
110 ;;; arrays of bits should work:
111 (let ((a (make-array '(10 10) :element-type 'bit :adjustable t)))
112 (setf (bit a 0 0) 1)
113 (assert (= (bit a 0 0) 1)))
114 (let ((a (make-array '(10 10) :element-type 'bit)))
115 (setf (sbit a 0 0) 1)
116 (assert (= (sbit a 0 0) 1)))
118 (let ((x (copy-seq #*0011))
119 (y (copy-seq #*0101)))
120 (assert (equalp (bit-and x y nil) #*0001)))
122 ;;; arrays of NIL should work, FSVO "work".
123 (let ((a (make-array '(10 10) :element-type 'nil)))
124 (assert (= (array-total-size a) 100))
125 (assert (equal (array-dimensions a) '(10 10)))
126 (assert (eq (array-element-type a) 'nil)))
128 (assert (eq (upgraded-array-element-type 'nil) 'nil))
130 (multiple-value-bind (fun warn fail)
131 (compile nil '(lambda () (aref (make-array 0) 0)))
132 (declare (ignore warn fail))
133 #+nil (assert fail) ; doesn't work, (maybe because ASSERTED-TYPE is NIL?)
134 (assert-error (funcall fun) type-error))
136 (multiple-value-bind (fun warn fail)
137 (compile nil '(lambda () (aref (make-array 1) 1)))
138 (declare (ignore warn))
139 (assert fail)
140 (assert-error (funcall fun) type-error))
142 (multiple-value-bind (fun warn fail)
143 (compile nil '(lambda () (make-array 5 :element-type 'undefined-type)))
144 (declare (ignore fun fail))
145 (assert warn))
147 (flet ((opaque-identity (x) x))
148 (declare (notinline opaque-identity))
149 ;; we used to have leakage from cross-compilation hosts of the INDEX
150 ;; type, which prevented us from actually using all the large array
151 ;; dimensions that we promised. Let's make sure that we can create
152 ;; an array with more than 2^24 elements, since that was a symptom
153 ;; from the CLISP and OpenMCL hosts.
154 (let ((big-array (opaque-identity
155 (make-array (expt 2 26) :element-type 'bit))))
156 (assert (= (length big-array) (expt 2 26)))))
158 ;;; Bug reported by Kalle Olavi Niemitalo for CMUCL through Debian BTS
159 (let ((array (make-array nil :initial-contents nil)))
160 (assert (eql (aref array) nil)))
162 (let ((f (compile nil '(lambda ()
163 (let ((a (make-array '(4)
164 :element-type 'base-char
165 :initial-element #\z)))
166 (setf (aref a 0) #\a)
167 (setf (aref a 1) #\b)
168 (setf (aref a 2) #\c)
169 a)))))
170 (assert (= (length (funcall f)) 4)))
172 (let ((x (make-array nil :initial-element 'foo)))
173 (adjust-array x nil)
174 (assert (eql (aref x) 'foo)))
176 ;;; BUG 315: "no bounds check for access to displaced array"
177 ;;; reported by Bruno Haible sbcl-devel "various SBCL bugs" from CLISP
178 ;;; test suite.
179 (locally (declare (optimize (safety 3) (speed 0)))
180 (let* ((x (make-array 10 :fill-pointer 4 :element-type 'character
181 :initial-element #\space :adjustable t))
182 (y (make-array 10 :fill-pointer 4 :element-type 'character
183 :displaced-to x)))
184 (assert (eq x (adjust-array x '(5))))
185 (assert (eq :error (handler-case
186 (char y 0)
187 (sb-int:invalid-array-error (e)
188 (assert (eq y (type-error-datum e)))
189 (assert (equal `(vector character 10)
190 (type-error-expected-type e)))
191 :error))))))
193 ;;; MISC.527: bit-vector bitwise operations used LENGTH to get a size
194 ;;; of a vector
195 (flet ((bit-vector-equal (v1 v2)
196 (and (bit-vector-p v1) (bit-vector-p v2)
197 (equal (array-dimension v1 0) (array-dimension v2 0))
198 (loop for i below (array-dimension v1 0)
199 always (eql (aref v1 i) (aref v2 i))))))
200 (let* ((length 1024)
201 (v1 (make-array length :element-type 'bit :fill-pointer 0))
202 (v2 (make-array length :element-type 'bit :fill-pointer 1)))
203 (loop for i from 0 below length
204 for x1 in '#1=(0 0 1 1 . #1#)
205 and x2 in '#2=(0 1 0 1 . #2#)
206 do (setf (aref v1 i) x1)
207 do (setf (aref v2 i) x2))
208 (loop for (bf lf) in '((bit-and logand)
209 (bit-andc1 logandc1)
210 (bit-andc2 logandc2)
211 (bit-eqv logeqv)
212 (bit-ior logior)
213 (bit-nand lognand)
214 (bit-nor lognor)
215 (bit-orc1 logorc1)
216 (bit-orc2 logorc2)
217 (bit-xor logxor)
218 ((lambda (x y) (bit-not x)) #.(lambda (x y) (lognot x))))
219 for fun = (compile nil `(lambda (v)
220 (declare (type (array bit (*)) v))
221 (declare (optimize (speed 3) (safety 0)))
222 (,bf v ,v2)))
223 for r1 = (funcall fun v1)
224 and r2 = (coerce (loop for i below length
225 collect (logand 1 (funcall lf (aref v1 i) (aref v2 i))))
226 'bit-vector)
227 do (assert (bit-vector-equal r1 r2)))))
229 (with-test (:name (adjust-array fill-pointer))
230 ;; CLHS, ADJUST-ARRAY: An error of type error is signaled if
231 ;; fill-pointer is supplied and non-nil but array has no fill pointer.
232 (assert (eq :good
233 (handler-case
234 (let ((array (make-array 12)))
235 (assert (not (array-has-fill-pointer-p array)))
236 (adjust-array array 12 :fill-pointer t)
237 array)
238 (type-error ()
239 :good)))))
241 (with-test (:name (adjust-array :multidimensional))
242 (let ((ary (make-array '(2 2))))
243 ;; SBCL used to give multidimensional arrays a bogus fill-pointer
244 (assert (not (array-has-fill-pointer-p (adjust-array ary '(2 2)))))))
246 (with-test (:name :%set-fill-pointer/error)
247 (let ((v (make-array 3 :fill-pointer 0)))
248 (handler-case
249 (progn
250 (setf (fill-pointer v) 12)
251 (error "WTF"))
252 (error (e)
253 (assert (eql 12 (type-error-datum e)))
254 (assert (equal '(integer 0 3) (type-error-expected-type e)))))))
256 (with-test (:name array-storage-vector)
257 (let ((vec (vector 1 2 3)))
258 (assert (eq vec (sb-ext:array-storage-vector vec)))
259 (assert (equalp (vector 1 2 3 4)
260 (sb-ext:array-storage-vector
261 (make-array '(2 2) :initial-contents '((1 2) (3 4))))))
262 (assert (eq 'fixnum (array-element-type
263 (sb-ext:array-storage-vector (make-array '(3 4 5)
264 :element-type 'fixnum)))))
265 (assert (not (array-has-fill-pointer-p
266 (sb-ext::array-storage-vector
267 (make-array 5 :fill-pointer 4)))))))
269 (with-test (:name :invalid-array-index-error)
270 (let ((array (make-array '(3 3 3))))
271 (assert
272 (eq :right
273 (handler-case
274 (eval `(aref ,array 0 1 3))
275 (sb-int:invalid-array-index-error (e)
276 (when (and (eq array (sb-kernel::invalid-array-index-error-array e))
277 (= 3 (type-error-datum e))
278 (equal '(integer 0 (3)) (type-error-expected-type e)))
279 :right)))))))
281 (with-test (:name :out-of-bounds-error-details)
282 (assert (eq :good
283 (handler-case
284 (flet ((test (array i)
285 (aref array i)))
286 (test (eval '(vector 0 1 2 3)) 6))
287 (sb-int:invalid-array-index-error (e)
288 (when (and (equal '(integer 0 (4))
289 (type-error-expected-type e))
290 (eql 6 (type-error-datum e)))
291 :good))))))
293 (with-test (:name :odd-keys-for-make-array)
294 (assert (eq :good
295 (handler-case
296 (compile nil '(lambda (m) (make-array m 1)))
297 (simple-warning () :good)))))
300 (with-test (:name :bug-1096359)
301 (let ((a (make-array 1 :initial-element 5)))
302 (assert (equalp (adjust-array a 2 :initial-element 10)
303 #(5 10)))))
305 (with-test (:name (:make-array-transform-unknown-type :bug-1156095))
306 (assert
307 (handler-case
308 (compile nil `(lambda () (make-array '(1 2)
309 :element-type ',(gensym))))
310 (style-warning ()
312 (:no-error (&rest args)
313 (declare (ignore args))
314 nil))))
316 (with-test (:name :dont-make-array-bad-keywords)
317 ;; This used to get a heap exhaustion error because of trying
318 ;; to make the array before checking keyword validity.
319 (handler-case
320 (locally
321 (declare (notinline make-array))
322 (make-array (1- array-total-size-limit)
323 :initial-contents '(a b c) :initial-element 9))
324 (simple-error (c)
325 (assert
326 (string= (simple-condition-format-control c)
327 "Can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS")))))
329 (with-test (:name :make-array-sanity-check-dims-first)
330 ;; A full call to %MAKE-ARRAY will signal a TYPE-ERROR on these inputs
331 ;; instead of trying to consume a massive amount of memory.
332 ;; Additionally, the relevent IR1 transform should give up.
333 (locally
334 (declare (notinline make-array))
335 (assert-error (make-array `(-1 -1 ,(- (ash array-dimension-limit -2) 4)))
336 type-error))
337 (locally
338 (declare (inline make-array))
339 (assert-error (make-array `(-1 -1 ,(- (ash array-dimension-limit -2) 4)))
340 type-error)))
342 (with-test (:name :make-array-size-overflow)
343 ;; 1-bit fixnum tags make array limits overflow the word length
344 ;; when converted to bytes
345 (when (= sb-vm:n-fixnum-tag-bits 1)
346 (assert-error (make-array (1- array-total-size-limit)) error)))
348 (with-test (:name :adjust-non-adjustable-array)
349 (let* ((a (make-array '(2 3) :initial-contents '((0 1 2) (3 4 5))))
350 (b (adjust-array a '(2 2))))
351 (setf (aref a 0 0) 11)
352 (assert (zerop (aref b 0 0)))
353 (assert (not (eq a b)))))