1 ;;;; This software is part of the SBCL system. See the README file for
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
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.
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) (*)))
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
)
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))
49 (error "expected ~S in COMPILED-AREF ~S" expected-result form
))
51 (unless (eql expected-result
52 (funcall (checked-compile `(lambda ()
54 (declare (type ,type x
))
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
)
63 (error "error not thrown in FUNCALL COMPILE ~S" form
))
64 (when (ignore-errors (funcall (checked-compile `(lambda () (aref ,form
12))
66 (error "error not thrown in COMPILED-AREF ~S" form
))
68 (when (ignore-errors (funcall
69 (checked-compile `(lambda ()
71 (declare (type ,type x
))
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.
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
88 (declare (type (simple-array bit
(*)) rmdr
)
89 (optimize (speed 3) (safety 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
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
)
106 ((test-case (array subscript expected
)
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)))
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
)))
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))
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))
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
)
185 (assert (= (length (funcall f
)) 4)))
187 (let ((x (make-array nil
:initial-element
'foo
)))
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
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
199 (assert (eq x
(adjust-array x
'(5))))
200 (assert (eq :error
(handler-case
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
)))
208 ;;; MISC.527: bit-vector bitwise operations used LENGTH to get a size
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
))))))
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
)
234 ((lambda (x y
) (bit-not x
)) #.
(lambda (x y
)
237 for fun
= (checked-compile `(lambda (v)
238 (declare (type (array bit
(*)) v
))
239 (declare (optimize (speed 3) (safety 0)))
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
))))
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.
253 (let ((array (make-array 12)))
254 (assert (not (array-has-fill-pointer-p array
)))
255 (adjust-array array
12 :fill-pointer t
)
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)))
269 (setf (fill-pointer v
) 12)
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))))
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
)))
300 (with-test (:name
:out-of-bounds-error-details
)
303 (flet ((test (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
)))
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)
325 (with-test (:name
(:make-array-transform-unknown-type
:bug-1156095
))
328 (compile nil
`(lambda () (make-array '(1 2)
329 :element-type
',(gensym))))
332 (:no-error
(&rest args
)
333 (declare (ignore args
))
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.
341 (declare (notinline make-array
))
342 (make-array (1- array-total-size-limit
)
343 :initial-contents
'(a b c
) :initial-element
9))
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.
354 (declare (notinline make-array
))
355 (assert-error (make-array `(-1 -
1 ,(- (ash array-dimension-limit -
2) 4)))
358 (declare (inline make-array
))
359 (assert-error (make-array `(-1 -
1 ,(- (ash array-dimension-limit -
2) 4)))
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 (compile nil
`(lambda (x)
379 sb-int
:invalid-array-index-error
)
380 (assert (eql (funcall (compile nil
`(lambda (x)
381 (declare (optimize (safety 0)))
382 ;; Strins are null-terminated for C interoperability