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