fe9c4f083c418008eb787f32667a384dc4404b86
[sbcl.git] / tests / array.pure.lisp
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.
11
12 (in-package :cl-user)
13
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))))))
64
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))))
82
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")))
90
91 ;;; ARRAY-IN-BOUNDS-P should work when given non-INDEXes as its
92 ;;; subscripts (and return NIL, of course)
93 (let ((a (make-array 10 :fill-pointer 5)))
94   (assert (not (array-in-bounds-p a -1)))
95   (assert (array-in-bounds-p a 3))
96   (assert (array-in-bounds-p a 7))
97   (assert (not (array-in-bounds-p a 11)))
98   (assert (not (array-in-bounds-p a (1+ most-positive-fixnum)))))
99
100 ;;; arrays of bits should work:
101 (let ((a (make-array '(10 10) :element-type 'bit :adjustable t)))
102   (setf (bit a 0 0) 1)
103   (assert (= (bit a 0 0) 1)))
104 (let ((a (make-array '(10 10) :element-type 'bit)))
105   (setf (sbit a 0 0) 1)
106   (assert (= (sbit a 0 0) 1)))
107
108 (let ((x (copy-seq #*0011))
109       (y (copy-seq #*0101)))
110   (assert (equalp (bit-and x y nil) #*0001)))
111
112 ;;; arrays of NIL should work, FSVO "work".
113 (let ((a (make-array '(10 10) :element-type 'nil)))
114   (assert (= (array-total-size a) 100))
115   (assert (equal (array-dimensions a) '(10 10)))
116   (assert (eq (array-element-type a) 'nil)))
117
118 (assert (eq (upgraded-array-element-type 'nil) 'nil))
119
120 (multiple-value-bind (fun warn fail)
121     (compile nil '(lambda () (aref (make-array 0) 0)))
122   #+nil (assert fail) ; doesn't work, (maybe because ASSERTED-TYPE is NIL?)
123   (assert (raises-error? (funcall fun) type-error)))
124
125 (multiple-value-bind (fun warn fail)
126     (compile nil '(lambda () (aref (make-array 1) 1)))
127   (assert fail)
128   (assert (raises-error? (funcall fun) type-error)))
129
130 (multiple-value-bind (fun warn fail)
131     (compile nil '(lambda () (make-array 5 :element-type 'undefined-type)))
132   (assert warn))
133
134 (flet ((opaque-identity (x) x))
135   (declare (notinline opaque-identity))
136   ;; we used to have leakage from cross-compilation hosts of the INDEX
137   ;; type, which prevented us from actually using all the large array
138   ;; dimensions that we promised.  Let's make sure that we can create
139   ;; an array with more than 2^24 elements, since that was a symptom
140   ;; from the CLISP and OpenMCL hosts.
141   (let ((big-array (opaque-identity
142                     (make-array (expt 2 26) :element-type 'bit))))
143     (assert (= (length big-array) (expt 2 26)))))
144
145 ;;; Bug reported by Kalle Olavi Niemitalo for CMUCL through Debian BTS
146 (let ((array (make-array nil :initial-contents nil)))
147   (assert (eql (aref array) nil)))
148
149 (let ((f (compile nil '(lambda ()
150                         (let ((a (make-array '(4)
151                                              :element-type 'base-char
152                                              :initial-element #\z)))
153                           (setf (aref a 0) #\a)
154                           (setf (aref a 1) #\b)
155                           (setf (aref a 2) #\c)
156                           a)))))
157   (assert (= (length (funcall f)) 4)))
158
159 (let ((x (make-array nil :initial-element 'foo)))
160   (adjust-array x nil)
161   (assert (eql (aref x) 'foo)))
162
163 ;;; BUG 315: "no bounds check for access to displaced array"
164 ;;;  reported by Bruno Haible sbcl-devel "various SBCL bugs" from CLISP
165 ;;;  test suite.
166 (multiple-value-bind (val err)
167     (ignore-errors
168       (locally (declare (optimize (safety 3) (speed 0)))
169         (let* ((x (make-array 10 :fill-pointer 4 :element-type 'character
170                               :initial-element #\space :adjustable t))
171                (y (make-array 10 :fill-pointer 4 :element-type 'character
172                               :displaced-to x)))
173           (adjust-array x '(5))
174           (char y 5))))
175   (assert (and (not val) (typep err 'sb-kernel:displaced-to-array-too-small-error))))
176
177 ;;; MISC.527: bit-vector bitwise operations used LENGTH to get a size
178 ;;; of a vector
179 (flet ((bit-vector-equal (v1 v2)
180          (and (bit-vector-p v1) (bit-vector-p v2)
181               (equal (array-dimension v1 0) (array-dimension v2 0))
182               (loop for i below (array-dimension v1 0)
183                     always (eql (aref v1 i) (aref v2 i))))))
184   (let* ((length 1024)
185          (v1 (make-array length :element-type 'bit :fill-pointer 0))
186          (v2 (make-array length :element-type 'bit :fill-pointer 1)))
187     (loop for i from 0 below length
188           for x1 in '#1=(0 0 1 1 . #1#)
189           and x2 in '#2=(0 1 0 1 . #2#)
190           do (setf (aref v1 i) x1)
191           do (setf (aref v2 i) x2))
192     (loop for (bf lf) in '((bit-and logand)
193                            (bit-andc1 logandc1)
194                            (bit-andc2 logandc2)
195                            (bit-eqv logeqv)
196                            (bit-ior logior)
197                            (bit-nand lognand)
198                            (bit-nor lognor)
199                            (bit-orc1 logorc1)
200                            (bit-orc2 logorc2)
201                            (bit-xor logxor)
202                            ((lambda (x y) (bit-not x)) #.(lambda (x y) (lognot x))))
203           for fun = (compile nil `(lambda (v)
204                                     (declare (type (array bit (*)) v))
205                                     (declare (optimize (speed 3) (safety 0)))
206                                     (,bf v ,v2)))
207           for r1 = (funcall fun v1)
208           and r2 = (coerce (loop for i below length
209                                  collect (logand 1 (funcall lf (aref v1 i) (aref v2 i))))
210                            'bit-vector)
211           do (assert (bit-vector-equal r1 r2)))))
212
213 ;;; CLHS, ADJUST-ARRAY: An error of type error is signaled if
214 ;;; fill-pointer is supplied and non-nil but array has no fill pointer.
215 (assert (eq :good
216             (handler-case
217                 (let ((array (make-array 12)))
218                   (assert (not (array-has-fill-pointer-p array)))
219                   (adjust-array array 12 :fill-pointer t)
220                   array)
221               (type-error ()
222                 :good))))
223
224 ;;; SIMPLE-VECTOR-COMPARE-AND-SWAP
225
226 (let ((v (vector 1)))
227   ;; basics
228   (assert (eql 1 (sb-kernel:simple-vector-compare-and-swap v 0 1 2)))
229   (assert (eql 2 (sb-kernel:simple-vector-compare-and-swap v 0 1 3)))
230   (assert (eql 2 (svref v 0)))
231   ;; bounds
232   (multiple-value-bind (res err)
233       (ignore-errors (sb-kernel:simple-vector-compare-and-swap v -1 1 2))
234     (assert (not res))
235     (assert (typep err 'type-error)))
236   (multiple-value-bind (res err)
237       (ignore-errors (sb-kernel:simple-vector-compare-and-swap v 1 1 2))
238     (assert (not res))
239     (assert (typep err 'type-error))))
240
241 ;; type of the first argument
242 (multiple-value-bind (res err)
243     (ignore-errors (sb-kernel:simple-vector-compare-and-swap "foo" 1 1 2))
244     (assert (not res))
245     (assert (typep err 'type-error)))