1 ;;;; tests related to sequences
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.
16 (load "compiler-test-util.lisp")
18 ;;; As reported by Paul Dietz from his ansi-test suite for gcl, REMOVE
19 ;;; malfunctioned when given :START, :END and :FROM-END arguments.
20 ;;; Make sure it doesn't happen again.
21 (let* ((orig '(1 2 3 2 6 1 2 4 1 3 2 7))
23 (y (remove 3 x
:from-end t
:start
1 :end
5))
24 (z (remove 2 x
:from-end t
:start
1 :end
5)))
25 (assert (equalp orig x
))
26 (assert (equalp y
'(1 2 2 6 1 2 4 1 3 2 7)))
27 (assert (equalp z
'(1 3 6 1 2 4 1 3 2 7))))
29 ;;; Similarly, NSUBSTITUTE and friends were getting things wrong with
30 ;;; :START, :END and :FROM-END:
32 (loop for i from
0 to
9 always
33 (loop for j from i to
10 always
34 (loop for c from
0 to
(- j i
) always
35 (let* ((orig '(a a a a a a a a a a
))
37 (y (nsubstitute 'x
'a x
:start i
:end j
:count c
)))
38 (equal y
(nconc (make-list i
:initial-element
'a
)
39 (make-list c
:initial-element
'x
)
40 (make-list (- 10 (+ i c
))
41 :initial-element
'a
))))))))
44 (loop for i from
0 to
9 always
45 (loop for j from i to
10 always
46 (loop for c from
0 to
(- j i
) always
47 (let* ((orig '(a a a a a a a a a a
))
49 (y (nsubstitute-if 'x
(lambda (x) (eq x
'a
)) x
51 :count c
:from-end t
)))
52 (equal y
(nconc (make-list (- j c
) :initial-element
'a
)
53 (make-list c
:initial-element
'x
)
55 :initial-element
'a
))))))))
57 (loop for i from
0 to
9 always
58 (loop for j from i to
10 always
59 (loop for c from
0 to
(- j i
) always
60 (let* ((orig '(a a a a a a a a a a
))
62 (y (nsubstitute-if-not 'x
(lambda (x)
65 :count c
:from-end t
)))
66 (equal y
(nconc (make-list (- j c
) :initial-element
'a
)
67 (make-list c
:initial-element
'x
)
69 :initial-element
'a
))))))))
71 ;;; And equally similarly, REMOVE-DUPLICATES misbehaved when given
74 (let ((orig (list 0 1 2 0 1 2 0 1 2 0 1 2)))
75 (assert (equalp (remove-duplicates orig
:start
3 :end
9) '(0 1 2 0 1 2 0 1 2)))
76 (assert (equalp (delete-duplicates orig
:start
3 :end
9) '(0 1 2 0 1 2 0 1 2))))
79 (assert (= 1 (count 1 '(1 2 3))))
80 (assert (= 2 (count 'z
#(z 1 2 3 z
))))
81 (assert (= 0 (count 'y
'(z 1 2 3 z
))))
83 ;;; tests of COUNT-IF and COUNT-IF-NOT
84 (macrolet (;; the guts of CCI, abstracted over whether we're testing
85 ;; COUNT-IF or COUNT-IF-NOT
86 (%cci
(expected count-if test sequence-as-list
&rest keys
)
87 `(let* ((list ',sequence-as-list
)
88 (simple-vector (coerce list
'simple-vector
))
89 (length (length list
))
90 (vector (make-array (* 2 length
) :fill-pointer length
)))
91 (replace vector list
:end1 length
)
92 (dolist (seq (list list simple-vector vector
))
93 (assert (= ,expected
(,count-if
,test seq
,@keys
))))))
95 (cci (expected test sequence-as-list
&rest keys
)
97 (format t
"~&SEQUENCE-AS-LIST=~S~%" ',sequence-as-list
)
108 (cci 1 #'consp
(1 (12) 1))
109 (cci 3 #'consp
(1 (2) 3 (4) (5) 6))
110 (cci 3 #'consp
(1 (2) 3 (4) (5) 6) :from-end t
)
111 (cci 2 #'consp
(1 (2) 3 (4) (5) 6) :start
2)
112 (cci 0 #'consp
(1 (2) 3 (4) (5) 6) :start
2 :end
3)
113 (cci 1 #'consp
(1 (2) 3 (4) (5) 6) :start
1 :end
3)
114 (cci 1 #'consp
(1 (2) 3 (4) (5) 6) :start
1 :end
2)
115 (cci 0 #'consp
(1 (2) 3 (4) (5) 6) :start
2 :end
2)
116 (cci 2 #'zerop
(0 10 0 11 12))
117 (cci 1 #'zerop
(0 10 0 11 12) :start
1)
118 (cci 2 #'minusp
(0 10 0 11 12) :key
#'1-
)
119 (cci 1 #'minusp
(0 10 0 11 12) :key
#'1-
:end
2))
120 (multiple-value-bind (v e
)
121 (ignore-errors (count-if #'zerop
'(0 a
0 b c
) :start
1))
123 (assert (eql (type-error-datum e
) 'a
)))
124 (multiple-value-bind (v e
)
125 (ignore-errors (count-if #'zerop
#(0 a
0 b c
) :start
1 :from-end
11))
127 (assert (eql (type-error-datum e
) 'c
)))
129 ;;; :COUNT may be negative and BIGNUM
130 (assert (equal (remove 1 '(1 2 3 1) :count
1) '(2 3 1)))
131 (assert (equal (remove 1 '(1 2 3 1) :count
(* 2 most-positive-fixnum
)) '(2 3)))
132 (assert (equal (remove 1 '(1 2 3 1) :count
(* -
2 most-positive-fixnum
)) '(1 2 3 1)))
134 ;;; bug reported by Wolfgang Jenkner on sbcl-devel 2003-01-04:
135 ;;; embedded calls of SORT do not work
136 (assert (equal (sort (list 0 0 0) (lambda (x y
) (sort (list 0 0 0) #'<) nil
))
138 (assert (equal (sort (list 0 0 0 0 0)
140 (declare (ignore x y
))
142 (sort (make-list 11 :initial-element
1)
145 (declare (ignore x y
))
146 (when (= (decf counter
) 0)
147 (return-from compare nil
))
151 ;;; miscellaneous sanity checks on stuff which could've been broken by
152 ;;; changes in MERGE-LIST* in sbcl-0.7.11.*
153 (assert (equal (merge 'list
() () '<) ()))
154 (assert (equal (merge 'list
() (list 1) #'< :key
'identity
) '(1)))
155 (assert (equal (merge 'list
(list 2) () '>) '(2)))
156 (assert (equal (merge 'list
(list 1 2 4) (list 2 3 7) '<) '(1 2 2 3 4 7)))
157 (assert (equal (merge 'list
(list 1 2 4) (list -
2 3 7) #'<) '(-2 1 2 3 4 7)))
158 (assert (equal (merge 'list
(list 1 2 4) (vector -
2 3 7) '< :key
'abs
)
160 (assert (equal (merge 'list
(list 1 -
2 4) (list -
2 3 7) '< :key
#'abs
)
162 (assert (equal (stable-sort (list 1 10 2 12 13 3) '<) '(1 2 3 10 12 13)))
163 (assert (equal (stable-sort (list 1 10 2 12 13 3) #'< :key
'-
)
165 (assert (equal (stable-sort (list 1 10 2 12 13 3) '> :key
#'-
)
167 (assert (equal (stable-sort (list 1 2 3 -
3 -
2 -
1) '< :key
'abs
)
170 ;;; CSR broke FILL by not returning the sequence argument in a transform.
171 (let* ((s1 (copy-seq "abcde"))
174 (assert (string= s2
"zzzzz")))
176 ;;; POSITION on displaced arrays with non-zero offset has been broken
177 ;;; for quite a while...
178 (let ((fn (compile nil
'(lambda (x) (position x
)))))
180 (y (make-array 2 :displaced-to x
:displaced-index-offset
1)))
181 (assert (= (position 2 y
) 0))))
183 ;;; (SIMPLE-STRING) is a legal type specifier for creation functions
184 (let ((a (make-sequence '(simple-string) 5))
185 (b (concatenate '(simple-string) "a" "bdec"))
186 (c (map '(simple-string) 'identity
"abcde"))
187 (d (merge '(simple-string) (copy-seq "acd") (copy-seq "be") 'char
>))
188 (e (coerce '(#\a #\b #\c
#\e
#\d
) '(simple-string))))
189 (assert (= (length a
) 5))
190 (assert (string= b
"abdec"))
191 (assert (string= c
"abcde"))
192 (assert (string= d
"beacd"))
193 (assert (string= e
"abced")))
195 ;;; COPY-SEQ "should be prepared to signal an error if sequence is not
196 ;;; a proper sequence".
197 (locally (declare (optimize safety
))
198 (multiple-value-bind (seq err
) (ignore-errors (copy-seq '(1 2 3 .
4)))
200 (assert (typep err
'type-error
))))
202 ;;; UBX-BASH-COPY transform had an inconsistent return type
203 (let ((sb-c::*check-consistency
* t
))
204 (handler-bind ((warning #'error
))
207 (declare (type fixnum l
))
209 (b1 (make-array bsize
:element-type
'(unsigned-byte 8)))
210 (b2 (make-array l
:element-type
'(unsigned-byte 8))))
211 (replace b1 b2
:start2
0 :end2 l
))))))
213 (with-test (:name
:bug-452008
)
214 ;; FIND & POSITION on lists should check bounds and (in safe code) detect
215 ;; circular and dotted lists.
216 (dolist (policy '(((speed 3) (safety 2)) ; transformed and safe
217 ((speed 3) (safety 1)) ; transformed and unsafe
218 ((speed 0) (safety 0)))) ; untransformed - safe regardless
219 (flet ((test (type expr
)
220 (let ((lambda `(lambda () (declare (optimize ,@policy
)) ,expr
)))
221 #+nil
(let ((*print-circle
* t
)) (format t
"~&test: ~S~%" lambda
))
222 (let ((got (handler-case (funcall (compile nil lambda
))
223 (error (e) (if (typep e type
) :error
:lose
))
224 (:no-error
(res) (list :no-error res
)))))
225 (unless (eq :error got
)
226 (error "wanted an error, got ~S~%" got
))))))
227 (test 'sb-kernel
:bounding-indices-bad-error
228 '(find :foo
'(1 2 3 :foo
) :start
1 :end
5 :from-end t
))
229 (test 'sb-kernel
:bounding-indices-bad-error
230 '(position :foo
'(1 2 3 :foo
) :start
1 :end
5 :from-end t
))
231 (test 'sb-kernel
:bounding-indices-bad-error
232 '(find :foo
'(1 2 3 :foo
) :start
3 :end
0 :from-end t
))
233 (test 'sb-kernel
:bounding-indices-bad-error
234 '(position :foo
'(1 2 3 :foo
) :start
3 :end
0 :from-end t
))
235 (unless (equal policy
'((speed 3) (safety 1)))
237 '(let ((list (list 1 2 3 :foo
)))
238 (find :bar
(nconc list list
))))
240 '(let ((list (list 1 2 3 :foo
)))
241 (position :bar
(nconc list list
))))))))
243 (with-test (:name
:bug-554385
)
244 ;; FIND-IF shouldn't look through the entire list.
245 (assert (= 2 (find-if #'evenp
'(1 2 1 1 1 1 1 1 1 1 1 1 :foo
))))
246 ;; Even though the end bounds are incorrect, the
247 ;; element is found before that's an issue.
248 (assert (eq :foo
(find :foo
'(1 2 3 :foo
) :start
1 :end
5)))
249 (assert (= 3 (position :foo
'(1 2 3 :foo
) :start
1 :end
5))))
251 (with-test (:name
(:search
:empty-seq
))
253 (funcall (compile nil
255 (declare (optimize (speed 3)) (simple-vector x
))
259 (funcall (compile nil
261 (declare (optimize (speed 3)) (simple-vector x
))
262 (search x
#(t t t
))))
265 (funcall (compile nil
267 (declare (optimize (speed 3)) (simple-vector x
))
268 (search x
#(t t t
) :end1
0)))
271 (funcall (compile nil
273 (declare (optimize (speed 3)) (simple-vector x
))
274 (search x
#(t t t
) :key nil
)))
277 (funcall (compile nil
279 (declare (optimize (speed 3)) (simple-vector x
))
280 (search x
#(t t t
) :key k
)))
284 (funcall (compile nil
286 (declare (optimize (speed 3)) (simple-vector x
))
287 (search x
#(t t t
) :start2
1 :end2
0 :end1
0)))
289 (sb-kernel:bounding-indices-bad-error
()
293 (declare (optimize speed
))
294 (search #() #(1 1) :start2
1 :end2
1)))))
297 (declare (optimize speed
))
298 (search #(1) #(1 1) :start1
1 :start2
2)))))
301 (declare (optimize speed
))
302 (search #() #(1 1) :from-end t
))))))
304 (with-test (:name
:sort-smoke-test
)
305 (flet ((iota (n type
&aux
(i 0))
306 (map-into (make-sequence type n
)
310 (let ((vector (let ((i 0))
311 (map-into (make-array n
)
314 (dotimes (i n
(coerce vector type
))
315 (let ((j (+ i
(random (- n i
)))))
316 (rotatef (aref vector i
) (aref vector j
))))))
318 (let* ((nonce (list nil
))
321 (prog1 (or (eql prev nonce
)
325 (dolist (type '(simple-vector list
))
326 (dolist (size '(7 8 9 13 1023 1024 1025 1536))
327 (loop for repeat below
5 do
329 (sort (funcall (case repeat
332 (reverse (iota n type
))))
337 (with-test (:name
:stable-sort-smoke-test
)
338 (flet ((iota (n type
&aux
(i 0))
339 (map-into (make-sequence type n
)
343 (let ((max (truncate (expt n
1/4)))
345 (map-into (make-sequence type n
)
347 (cons (random max
) (incf i
))))))
349 (let* ((nonce (list nil
))
352 (prog1 (or (eql prev nonce
)
353 (< (car prev
) (car x
))
354 (and (= (car prev
) (car x
))
355 (< (cdr prev
) (cdr x
))))
358 (dolist (type '(simple-vector list
))
359 (dolist (size '(0 1 2 3 4 5 6 7 8
360 9 10 11 12 13 14 15 16 17
361 1023 1024 1025 1536))
362 (loop for repeat below
5 do
365 (stable-sort (funcall (case repeat
369 #'< :key
#'car
))))))))
371 (with-test (:name
:&more-elt-index-too-large
)
372 (assert-error (funcall
373 (compile nil
'(lambda (&rest args
)
374 (declare (optimize safety
))
376 sb-kernel
:index-too-large-error
))
378 (with-test (:name
:do-sequence-on-literals
)
379 (assert (= (sequence:dosequence
(e #(1 2 3)) (return e
))
382 (with-test (:name
:search-transform-notes
)
384 (compile nil
`(lambda (s)
385 (declare (optimize (speed 3) (safety 0))
386 (type simple-string s
))
388 sb-ext
:compiler-note
))
390 (with-test (:name
:concatenate-two-constants
)
391 (assert (equal (funcall
392 (lambda () (declare (optimize (speed 3)))
393 (concatenate 'string
"a" "b")))
396 (with-test (:name
:bug-330299-make-sequence-transform
)
397 ;; test case from bug report.
398 ;; erroneous situation is caught by MAKE-ARRAY
402 (make-sequence 'bit-vector size
:initial-element
#\
0))))
403 ;; This is transformed, but MAKE-ARRAY does *not* consider it a problem
404 ;; since #\x is in the upgraded array type. That's too bad, because
405 ;; it's still poor style.
410 (make-sequence '(member #\a #\b) size
:initial-element
#\x
))))
411 ;; additional tests where the transform gives up but warns
415 (make-sequence '(vector (integer 1 15) 5) n
416 :initial-element
#\x
))))
420 (make-sequence '(vector (integer 1 15) 5) n
)))))
422 ;; Precisely type-check result of full call to MAP.
423 (with-test (:name
:notinlined-map-maximally-safe
)
425 (locally (declare (notinline map
)) (map '(cons symbol
) '+ '(1 2) '(3 4)))
428 (locally (declare (notinline map
))
429 (map '(cons t
(cons t null
)) '+ '(1 2 3) '(10 10 10)))
432 (defstruct ship size name
)
433 (with-test (:name
:find-derive-type
)
434 (let ((f (compile nil
'(lambda (x list
)
435 (ship-size (find x list
:key
'ship-name
))))))
436 ;; The test of SHIP-P in the SHIP-SIZE call is optimized into (NOT NULL).
437 ;; Therefore the code header for F does not reference #<LAYOUT for SHIP>
438 (assert (not (ctu:find-code-constants f
:type
'sb-kernel
:layout
)))
439 ;; And the function is safe.
440 (assert-error (funcall f nil nil
) type-error
)))