1 ;;;; tests related to sequences
3 ;;;; This file is impure because we want to be able to use DEFUN.
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; While most of SBCL is derived from the CMU CL system, the test
9 ;;;; files (like this one) were written from scratch after the fork
12 ;;;; This software is in the public domain and is provided with
13 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
14 ;;;; more information.
16 (load "test-util.lisp")
17 (load "assertoid.lisp")
20 (:use
:cl
:assertoid
:test-util
))
22 (in-package :seq-test
)
24 ;;; user-defined mock sequence class for testing generic versions of
25 ;;; sequence functions.
26 (defclass list-backed-sequence
(standard-object
28 ((elements :initarg
:elements
:type list
:accessor %elements
)))
30 (defmethod sequence:make-sequence-like
((sequence list-backed-sequence
) length
32 initial-element initial-contents
)
33 (declare (ignore initial-element initial-contents
))
34 (make-instance 'list-backed-sequence
35 :elements
(apply #'sequence
:make-sequence-like
38 (defmethod sequence:length
((sequence list-backed-sequence
))
39 (length (%elements sequence
)))
41 (defmethod sequence:elt
42 ((sequence list-backed-sequence
) index
)
43 (nth index
(%elements sequence
)))
45 (defmethod (setf sequence
:elt
)
46 (new-value (sequence list-backed-sequence
) index
)
47 (setf (nth index
(%elements sequence
)) new-value
))
49 ;;; helper functions for exercising SEQUENCE code on data of many
50 ;;; specialized types, and in many different optimization scenarios
51 (defun for-every-seq-1 (base-seq snippet
)
54 (every (lambda (el) (typep el eltype
)) base-seq
))
55 (make-sequence-for-type (type)
57 ((member list list-backed-sequence
)
58 (coerce base-seq type
))
59 ((cons (eql simple-array
) (cons * (cons (eql 1) null
)))
60 (destructuring-bind (eltype one
) (rest type
)
61 (declare (ignore one
))
62 (when (entirely eltype
)
63 (coerce base-seq type
))))
65 (destructuring-bind (eltype) (rest type
)
66 (when (entirely eltype
)
67 (let ((initial-element
68 (cond ((subtypep eltype
'character
)
70 ((subtypep eltype
'number
)
82 (dolist (seq-type '(list
85 (simple-array character
1)
87 (simple-array (signed-byte 4) 1)
88 (vector (signed-byte 4))
89 list-backed-sequence
))
90 (dolist (declaredness '(nil t
))
91 (dolist (optimization '(((speed 3) (space 0))
94 ((speed 0) (space 1))))
95 (let ((seq (make-sequence-for-type seq-type
))
96 (lambda-expr `(lambda (seq)
97 (declare (sb-ext:muffle-conditions
98 sb-ext
:compiler-note
))
100 `((declare (type ,seq-type seq
))))
101 (declare (optimize ,@optimization
))
105 ;(format t "~&~S~%" lambda-expr)
106 (let ((fun (checked-compile lambda-expr
)))
107 ;(format t "~&~S ~S~%~S~%~S ~S~%"
108 ; base-seq snippet seq-type declaredness optimization)
109 ;(format t "~&(TYPEP SEQ 'SIMPLE-ARRAY)=~S~%"
110 ; (typep seq 'simple-array))
111 (unless (funcall fun seq
)
112 (error "~@<failed test:~2I ~_BASE-SEQ=~S ~_SNIPPET=~S ~_SEQ-TYPE=~S ~_DECLAREDNESS=~S ~_OPTIMIZATION=~S~:@>"
117 optimization
)))))))))
118 (defun for-every-seq (base-seq snippets
)
119 (dolist (snippet snippets
)
120 (for-every-seq-1 base-seq snippet
)))
122 ;;; a wrapper to hide declared type information from the compiler, so
123 ;;; we don't get stopped by compiler warnings about e.g. compiling
124 ;;; (POSITION 1 #() :KEY #'ABS) when #() has been coerced to a string.
125 (defun indiscriminate (fun)
126 (lambda (&rest rest
) (apply fun rest
)))
128 ;;; asymmetric test arg order example from ANSI FIND definition page
129 (assert (eql #\space
; original example, depends on ASCII character ordering
130 (find #\d
"here are some letters that can be looked at"
132 (assert (eql #\e
; modified example, depends only on standard a-z ordering
133 (find #\f "herearesomeletters" :test
#'char
>)))
134 (assert (eql 4 ; modified more, avoids charset technicalities completely
135 (find 5 '(6 4) :test
'>)))
137 (with-test (:name sequence
:emptyp
)
139 '((eq t
(sequence:emptyp seq
))))
141 '((eq nil
(sequence:emptyp seq
)))))
143 ;;; tests of FIND, POSITION, FIND-IF, and POSITION-IF (and a few for
144 ;;; deprecated FIND-IF-NOT and POSITION-IF-NOT too)
146 '((null (find 1 seq
))
147 (null (find 1 seq
:from-end t
))
148 (null (position 1 seq
:key
(indiscriminate #'abs
)))
149 (null (position nil seq
:test
(constantly t
)))
150 (null (position nil seq
:test nil
))
151 (null (position nil seq
:test-not nil
))
152 (null (find-if #'1+ seq
:key
(indiscriminate #'log
)))
153 (null (position-if #'identity seq
:from-end t
))
154 (null (find-if-not #'packagep seq
))
155 (null (position-if-not #'packagep seq
:key nil
))))
157 '((null (find 2 seq
))
158 ;; Get the argument ordering for asymmetric tests like #'> right.
159 ;; (bug reported and fixed by Alexey Dejneka sbcl-devel 2001-10-17)
160 (eql 1 (find 2 seq
:test
#'>))
161 (find 2 seq
:key
#'1+)
162 (find 1 seq
:from-end t
)
163 (null (find 1 seq
:from-end t
:start
1))
164 (null (find 0 seq
:from-end t
))
165 (eql 0 (position 1 seq
:key
#'abs
))
166 (null (position nil seq
:test
'equal
))
167 (eql 1 (find-if #'1- seq
:key
#'log
))
168 (eql 0 (position-if #'identity seq
:from-end t
))
169 (null (find-if-not #'sin seq
))
170 (eql 0 (position-if-not #'packagep seq
:key
'identity
))))
171 (for-every-seq #(1 2 3 2 1)
173 (find 3 seq
:from-end
'yes
)
174 (eql 1 (position 1.5 seq
:test
#'<))
175 (eql 0 (position 0 seq
:key
'1-
))
176 (eql 4 (position 0 seq
:key
'1-
:from-end t
))
177 (eql 2 (position 4 seq
:key
'1+))
178 (eql 2 (position 4 seq
:key
'1+ :from-end t
))
179 (eql 1 (position 2 seq
))
180 (eql 1 (position 2 seq
:start
1))
181 (null (find 2 seq
:start
1 :end
1))
182 (eql 3 (position 2 seq
:start
2))
183 (eql 3 (position 2 seq
:key nil
:from-end t
))
184 (eql 2 (position 3 seq
:test
'=))
185 (eql 0 (position 3 seq
:test-not
'equalp
))
186 (eql 2 (position 3 seq
:test
'equal
:from-end t
))
187 (null (position 4 seq
:test
#'eql
))
188 (null (find-if #'packagep seq
))
189 (eql 1 (find-if #'plusp seq
))
190 (eql 3 (position-if #'plusp seq
:key
#'1-
:from-end t
))
191 (eql 1 (position-if #'evenp seq
))
192 (eql 3 (position-if #'evenp seq
:from-end t
))
193 (eql 2 (position-if #'plusp seq
:from-end nil
:key
'1-
:start
2))
194 (eql 3 (position-if #'plusp seq
:from-end t
:key
'1-
:start
2))
195 (null (position-if #'plusp seq
:from-end t
:key
'1-
:start
2 :end
2))
196 (null (find-if-not #'plusp seq
))
197 (eql 0 (position-if-not #'evenp seq
))
198 (eql 0 (search #(1) seq
))
199 (eql 1 (search #(4 5) seq
:key
'oddp
))
200 (eql 1 (search #(-2) seq
:test
(lambda (a b
) (= (- a
) b
))))
201 (eql 4 (search #(1) seq
:start2
1))
202 (null (search #(3) seq
:start2
3))
203 (eql 2 (search #(3) seq
:start2
2))
204 (eql 0 (search #(1 2) seq
))
205 (null (search #(2 1 3) seq
))
206 (eql 0 (search #(0 1 2 4) seq
:start1
1 :end1
3))
207 (eql 3 (search #(0 2 1 4) seq
:start1
1 :end1
3))
208 (eql 4 (search #(1) seq
:from-end t
))
209 (eql 0 (search #(1 2) seq
:from-end t
))
210 (null (search #(1 2) seq
:from-end t
:start2
1))
211 (eql 0 (search #(0 1 2 4) seq
:from-end t
:start1
1 :end1
3))
212 (eql 3 (search #(0 2 1 4) seq
:from-end t
:start1
1 :end1
3))
213 (null (search #(2 1 3) seq
:from-end t
))))
214 (for-every-seq "string test"
215 '((null (find 0 seq
))
216 (null (find #\D seq
:key
#'char-upcase
))
217 (find #\E seq
:key
#'char-upcase
)
218 (null (find #\e seq
:key
#'char-upcase
))
219 (eql 3 (position #\i seq
))
220 (eql 0 (position #\s seq
:key
#'char-downcase
))
221 (eql 1 (position #\s seq
:key
#'char-downcase
:test
#'char
/=))
222 (eql 9 (position #\s seq
:from-end t
:test
#'char
=))
223 (eql 10 (position #\s seq
:from-end t
:test
#'char
/=))
224 (eql 4 (position #\N seq
:from-end t
:key
'char-upcase
:test
#'char-equal
))
225 (eql 5 (position-if (lambda (c) (equal #\g c
)) seq
))
226 (eql 5 (position-if (lambda (c) (equal #\g c
)) seq
:from-end t
))
227 (find-if #'characterp seq
)
228 (find-if (lambda (c) (typep c
'base-char
)) seq
:from-end t
)
229 (null (find-if 'upper-case-p seq
))))
232 (with-test (:name
:subseq
)
233 (let ((avec (make-array 10
235 :initial-contents
'(0 1 2 3 iv v vi vii iix ix
))))
236 ;; These first five always worked AFAIK.
237 (assert (equalp (subseq avec
0 3) #(0 1 2)))
238 (assert (equalp (subseq avec
3 3) #()))
239 (assert (equalp (subseq avec
1 3) #(1 2)))
240 (assert (equalp (subseq avec
1) #(1 2 3)))
241 (assert (equalp (subseq avec
1 4) #(1 2 3)))
242 ;; SBCL bug found ca. 2002-05-01 by OpenMCL's correct handling of
243 ;; SUBSEQ, CSR's driving portable cross-compilation far enough to
244 ;; reach the SUBSEQ calls in assem.lisp, and WHN's sleazy
245 ;; translation of old CMU CL new-assem.lisp into sufficiently grotty
246 ;; portable Lisp that it passed suitable illegal values to SUBSEQ to
247 ;; exercise the bug:-|
249 ;; SUBSEQ should check its END value against logical LENGTH, not
250 ;; physical ARRAY-DIMENSION 0.
252 ;; fixed in sbcl-0.7.4.22 by WHN
253 (assert (null (ignore-errors (aref (subseq avec
1 5) 0))))))
256 (defun test-fill-typecheck (x)
257 (declare (optimize (safety 3) (space 2) (speed 1)))
258 (fill (make-string 10) x
))
260 (assert (string= (test-fill-typecheck #\
@) "@@@@@@@@@@"))
261 ;;; BUG 186, fixed in sbcl-0.7.5.5
262 (assert (null (ignore-errors (test-fill-typecheck 4097))))
264 ;;; MAKE-SEQUENCE, COERCE, CONCATENATE, MERGE, MAP and requested
265 ;;; result type (BUGs 46a, 46b, 66)
266 (with-test (:name
:sequence-functions
)
267 (macrolet ((assert-type-error (form)
268 `(assert-error ,form type-error
)))
269 (dolist (type-stub '((simple-vector)
271 (vector (signed-byte 8))
272 (vector (unsigned-byte 16))
273 (vector (signed-byte 32))
274 (simple-bit-vector)))
275 (declare (optimize safety
))
276 (format t
"~&~S~%" type-stub
)
278 (assert (= (length (make-sequence `(,@type-stub
) 10)) 10))
279 (assert (= (length (make-sequence `(,@type-stub
10) 10)) 10))
280 (assert-type-error (make-sequence `(,@type-stub
10) 11))
282 (assert (= (length (coerce '(0 0 0) `(,@type-stub
))) 3))
283 (assert (= (length (coerce #(0 0 0) `(,@type-stub
3))) 3))
284 (assert-type-error (coerce #*111 `(,@type-stub
4)))
286 (assert (= (length (concatenate `(,@type-stub
) #(0 0 0) #*111)) 6))
287 (assert (equalp (concatenate `(,@type-stub
) #(0 0 0) #*111)
288 (coerce #(0 0 0 1 1 1) `(,@type-stub
))))
289 (assert (= (length (concatenate `(,@type-stub
6) #(0 0 0) #*111)) 6))
290 (assert (equalp (concatenate `(,@type-stub
6) #(0 0 0) #*111)
291 (coerce #(0 0 0 1 1 1) `(,@type-stub
6))))
292 (assert-type-error (concatenate `(,@type-stub
5) #(0 0 0) #*111))
294 (macrolet ((test (type)
295 `(merge ,type
(copy-seq #(0 1 0)) (copy-seq #*111) #'>)))
296 (assert (= (length (test `(,@type-stub
))) 6))
297 (assert (equalp (test `(,@type-stub
))
298 (coerce #(1 1 1 0 1 0) `(,@type-stub
))))
299 (assert (= (length (test `(,@type-stub
6))) 6))
300 (assert (equalp (test `(,@type-stub
6))
301 (coerce #(1 1 1 0 1 0) `(,@type-stub
6))))
302 (assert-type-error (test `(,@type-stub
4))))
304 (assert (= (length (map `(,@type-stub
) #'logxor
#(0 0 1 1) '(0 1 0 1))) 4))
305 (assert (equalp (map `(,@type-stub
) #'logxor
#(0 0 1 1) '(0 1 0 1))
306 (coerce #(0 1 1 0) `(,@type-stub
))))
307 (assert (= (length (map `(,@type-stub
4) #'logxor
#(0 0 1 1) '(0 1 0 1)))
309 (assert (equalp (map `(,@type-stub
4) #'logxor
#(0 0 1 1) '(0 1 0 1))
310 (coerce #(0 1 1 0) `(,@type-stub
4))))
311 (assert-type-error (map `(,@type-stub
5) #'logxor
#(0 0 1 1) '(0 1 0 1))))
312 ;; some more CONCATENATE tests for strings
314 (declare (optimize safety
))
315 (assert (string= (concatenate 'string
"foo" " " "bar") "foo bar"))
316 (assert (string= (concatenate '(string 7) "foo" " " "bar") "foo bar"))
317 (assert-type-error (concatenate '(string 6) "foo" " " "bar"))
318 (assert (string= (concatenate '(string 6) "foo" #(#\b #\a #\r)) "foobar"))
319 (assert (string= (concatenate '(string 6) #(#\b #\a #\r) "foo") "barfoo"))
320 (assert-type-error (concatenate '(string 7) "foo" #(#\b #\a #\r))))
321 ;; Non-VECTOR ARRAY types aren't allowed as vector type specifiers.
323 (declare (optimize safety
))
324 (assert-type-error (concatenate 'simple-array
"foo" "bar"))
325 (assert-type-error (map 'simple-array
#'identity
'(1 2 3)))
326 (assert (equalp #(11 13)
327 (map '(simple-array fixnum
(*)) #'+ '(1 2 3) '(10 11))))
328 (assert-type-error (coerce '(1 2 3) 'simple-array
))
329 (assert-type-error (merge 'simple-array
(list 1 3) (list 2 4) '<))
330 (assert (equalp #(3 2 1) (coerce '(3 2 1) '(vector fixnum
))))
331 (assert-type-error (map 'array
#'identity
'(1 2 3)))
332 (assert-type-error (map '(array fixnum
) #'identity
'(1 2 3)))
333 (assert (equalp #(1 2 3) (coerce '(1 2 3) '(vector fixnum
))))
334 ;; but COERCE has an exemption clause:
335 (assert (string= "foo" (coerce "foo" 'simple-array
)))
336 ;; ... though not in all cases.
337 (assert-type-error (coerce '(#\f #\o
#\o
) 'simple-array
)))))
339 ;; CONCATENATE used to fail for generic sequences for result-type NULL.
340 (with-test (:name
(concatenate :result-type-null
:bug-1162301
))
341 (assert (sequence:emptyp
(concatenate 'null
)))
344 '((sequence:emptyp
(concatenate 'null seq
))
345 (sequence:emptyp
(concatenate 'null seq seq
))
346 (sequence:emptyp
(concatenate 'null seq
#()))
347 (sequence:emptyp
(concatenate 'null seq
""))))
350 (mapcar (lambda (form)
351 `(typep (nth-value 1 (ignore-errors ,form
)) 'type-error
))
352 '((concatenate 'null seq
)
353 (concatenate 'null seq seq
)
354 (concatenate 'null seq
#())
355 (concatenate 'null seq
"2")))))
357 ;;; As pointed out by Raymond Toy on #lisp IRC, MERGE had some issues
358 ;;; with user-defined types until sbcl-0.7.8.11
359 (deftype list-typeoid
() 'list
)
360 (with-test (:name
:merge-user-types
)
361 (assert (equal '(1 2 3 4) (merge 'list-typeoid
(list 1 3) (list 2 4) '<)))
362 ;;; and also with types that weren't precicely LIST
363 (assert (equal '(1 2 3 4) (merge 'cons
(list 1 3) (list 2 4) '<))))
365 ;;; but wait, there's more! The NULL and CONS types also have implicit
366 ;;; length requirements:
367 (with-test (:name
:sequence-functions-list-types
)
368 (macrolet ((assert-type-error (form)
369 `(assert (typep (nth-value 1 (ignore-errors ,form
))
372 (declare (optimize safety
))
374 (assert-type-error (make-sequence 'cons
0))
375 (assert-type-error (make-sequence 'null
1))
376 (assert-type-error (make-sequence '(cons t null
) 0))
377 (assert-type-error (make-sequence '(cons t null
) 2))
378 ;; KLUDGE: I'm not certain that this test actually tests for what
379 ;; it should test, in that the type deriver and optimizers might
380 ;; be too smart for the good of an exhaustive test system.
381 ;; However, it makes me feel good. -- CSR, 2002-10-18
382 (assert (null (make-sequence 'null
0)))
383 (assert (= (length (make-sequence 'cons
3)) 3))
384 (assert (= (length (make-sequence '(cons t null
) 1)) 1))
385 ;; and NIL is not a valid type for MAKE-SEQUENCE
386 (assert-type-error (make-sequence 'nil
0))
388 (assert-type-error (coerce #(1) 'null
))
389 (assert-type-error (coerce #() 'cons
))
390 (assert-type-error (coerce #() '(cons t null
)))
391 (assert-type-error (coerce #(1 2) '(cons t null
)))
392 (assert (null (coerce #() 'null
)))
393 (assert (= (length (coerce #(1) 'cons
)) 1))
394 (assert (= (length (coerce #(1) '(cons t null
))) 1))
395 (assert-type-error (coerce #() 'nil
))
397 (assert-type-error (merge 'null
(list 1 3) (list 2 4) '<))
398 (assert-type-error (merge 'cons
() () '<))
399 (assert (null (merge 'null
() () '<)))
400 (assert (= (length (merge 'cons
(list 1 3) (list 2 4) '<)) 4))
401 (assert (= (length (merge '(cons t
(cons t
(cons t
(cons t null
))))
402 (list 1 3) (list 2 4)
405 (assert-type-error (merge 'nil
() () '<))
407 (assert-type-error (concatenate 'cons
#() ()))
408 (assert-type-error (concatenate '(cons t null
) #(1 2 3) #(4 5 6)))
409 (assert (= (length (concatenate 'cons
#() '(1) "2 3")) 4))
410 (assert (= (length (concatenate '(cons t cons
) '(1) "34")) 3))
411 (assert-type-error (concatenate 'nil
'(3)))
412 ;; FIXME: tests for MAP to come when some brave soul implements
413 ;; the analogous type checking for MAP/%MAP.
416 ;;; ELT should signal an error of type TYPE-ERROR if its index
417 ;;; argument isn't a valid sequence index for sequence:
418 (defun test-elt-signal (x)
420 (assert-error (test-elt-signal "foo") type-error
)
421 (assert (eql (test-elt-signal "foob") #\b))
423 (declare (optimize (safety 3)))
424 (assert-error (elt (list 1 2 3) 3) type-error
))
426 ;;; confusion in the refactoring led to this signalling an unbound
427 ;;; variable, not a type error.
428 (defun svrefalike (x)
430 (assert-error (svrefalike #*0) type-error
)
432 ;;; checks for uniform bounding index handling.
434 ;;; This used to be SAFETY 3 only, but bypassing these checks with
435 ;;; above-zero speed when SPEED > SAFETY is not The SBCL Way.
437 ;;; KLUDGE: not all in one big form because that causes SBCL to spend
438 ;;; an absolute age trying to compile it.
439 (defmacro sequence-bounding-indices-test
(&body body
)
442 ;; See Issues 332 [and 333(!)] in the CLHS
443 (declare (optimize (speed 3) (safety 1)))
444 (let ((string (make-array 10
447 :element-type
'base-char
)))
449 (format t
"... BASE-CHAR")
452 (setf (fill-pointer string
) 10)
454 (setf (fill-pointer string
) 5)))
455 (declare (ignorable #'reset
))
458 ;; See Issues 332 [and 333(!)] in the CLHS
459 (declare (optimize (speed 3) (safety 1)))
460 (let ((string (make-array 10
463 :element-type
'character
)))
465 (format t
"... CHARACTER")
468 (setf (fill-pointer string
) 10)
470 (setf (fill-pointer string
) 5)))
471 (declare (ignorable #'reset
))
474 (declaim (notinline opaque-identity
))
475 (defun opaque-identity (x) x
)
477 (sequence-bounding-indices-test
478 (format t
"~&/Accessor SUBSEQ")
479 (assert (string= (subseq string
0 5) "aaaaa"))
480 (assert-error (subseq string
0 6))
481 (assert-error (subseq string
(opaque-identity -
1) 5))
482 (assert-error (subseq string
4 2))
483 (assert-error (subseq string
6))
484 (assert (string= (setf (subseq string
0 5) "abcde") "abcde"))
485 (assert (string= (subseq string
0 5) "abcde"))
487 (assert-error (setf (subseq string
0 6) "abcdef"))
488 (assert-error (setf (subseq string
(opaque-identity -
1) 5) "abcdef"))
489 (assert-error (setf (subseq string
4 2) ""))
490 (assert-error (setf (subseq string
6) "ghij")))
492 ;;; Function COUNT, COUNT-IF, COUNT-IF-NOT
493 (sequence-bounding-indices-test
494 (format t
"~&/Function COUNT, COUNT-IF, COUNT-IF-NOT")
495 (assert (= (count #\a string
:start
0 :end nil
) 5))
496 (assert (= (count #\a string
:start
0 :end
5) 5))
497 (assert-error (count #\a string
:start
0 :end
6))
498 (assert-error (count #\a string
:start
(opaque-identity -
1) :end
5))
499 (assert-error (count #\a string
:start
4 :end
2))
500 (assert-error (count #\a string
:start
6 :end
9))
501 (assert (= (count-if #'alpha-char-p string
:start
0 :end nil
) 5))
502 (assert (= (count-if #'alpha-char-p string
:start
0 :end
5) 5))
504 (count-if #'alpha-char-p string
:start
0 :end
6))
506 (count-if #'alpha-char-p string
:start
(opaque-identity -
1) :end
5))
508 (count-if #'alpha-char-p string
:start
4 :end
2))
510 (count-if #'alpha-char-p string
:start
6 :end
9))
511 (assert (= (count-if-not #'alpha-char-p string
:start
0 :end nil
) 0))
512 (assert (= (count-if-not #'alpha-char-p string
:start
0 :end
5) 0))
514 (count-if-not #'alpha-char-p string
:start
0 :end
6))
516 (count-if-not #'alpha-char-p string
:start
(opaque-identity -
1) :end
5))
518 (count-if-not #'alpha-char-p string
:start
4 :end
2))
520 (count-if-not #'alpha-char-p string
:start
6 :end
9)))
523 (sequence-bounding-indices-test
524 (format t
"~&/Function FILL")
525 (assert (string= (fill string
#\b :start
0 :end
5) "bbbbb"))
526 (assert (string= (fill string
#\c
:start
0 :end nil
) "ccccc"))
527 (assert-error (fill string
#\d
:start
0 :end
6))
528 (assert-error (fill string
#\d
:start
(opaque-identity -
1) :end
5))
529 (assert-error (fill string
#\d
:start
4 :end
2))
530 (assert-error (fill string
#\d
:start
6 :end
9)))
532 ;;; Function FIND, FIND-IF, FIND-IF-NOT
533 (sequence-bounding-indices-test
534 (format t
"~&/Function FIND, FIND-IF, FIND-IF-NOT")
535 (assert (char= (find #\a string
:start
0 :end nil
) #\a))
536 (assert (char= (find #\a string
:start
0 :end
5) #\a))
537 (assert-error (find #\a string
:start
0 :end
6))
538 (assert-error (find #\a string
:start
(opaque-identity -
1) :end
5))
539 (assert-error (find #\a string
:start
4 :end
2))
540 (assert-error (find #\a string
:start
6 :end
9))
541 (assert (char= (find-if #'alpha-char-p string
:start
0 :end nil
) #\a))
542 (assert (char= (find-if #'alpha-char-p string
:start
0 :end
5) #\a))
544 (find-if #'alpha-char-p string
:start
0 :end
6))
546 (find-if #'alpha-char-p string
:start
(opaque-identity -
1) :end
5))
548 (find-if #'alpha-char-p string
:start
4 :end
2))
550 (find-if #'alpha-char-p string
:start
6 :end
9))
551 (assert (eq (find-if-not #'alpha-char-p string
:start
0 :end nil
) nil
))
552 (assert (eq (find-if-not #'alpha-char-p string
:start
0 :end
5) nil
))
554 (find-if-not #'alpha-char-p string
:start
0 :end
6))
556 (find-if-not #'alpha-char-p string
:start
(opaque-identity -
1) :end
5))
558 (find-if-not #'alpha-char-p string
:start
4 :end
2))
560 (find-if-not #'alpha-char-p string
:start
6 :end
9)))
562 ;;; Function MISMATCH
563 (sequence-bounding-indices-test
564 (format t
"~&/Function MISMATCH")
565 (assert (null (mismatch string
"aaaaa" :start1
0 :end1 nil
)))
566 (assert (= (mismatch "aaab" string
:start2
0 :end2
4) 3))
567 (assert-error (mismatch "aaaaaa" string
:start2
0 :end2
6))
568 (assert-error (mismatch string
"aaaaaa" :start1
(opaque-identity -
1) :end1
5))
569 (assert-error (mismatch string
"" :start1
4 :end1
2))
570 (assert-error (mismatch "aaaa" string
:start2
6 :end2
9)))
572 ;;; Function PARSE-INTEGER
573 (sequence-bounding-indices-test
574 (format t
"~&/Function PARSE-INTEGER")
575 (setf (fill-pointer string
) 10)
576 (setf (subseq string
0 10) "1234567890")
577 (setf (fill-pointer string
) 5)
578 (assert (= (parse-integer string
:start
0 :end
5) 12345))
579 (assert (= (parse-integer string
:start
0 :end nil
) 12345))
580 (assert-error (parse-integer string
:start
0 :end
6))
581 (assert-error (parse-integer string
:start
(opaque-identity -
1) :end
5))
582 (assert-error (parse-integer string
:start
4 :end
2))
583 (assert-error (parse-integer string
:start
6 :end
9)))
585 ;;; Function PARSE-NAMESTRING
586 (sequence-bounding-indices-test
587 (format t
"~&/Function PARSE-NAMESTRING")
588 (setf (fill-pointer string
) 10)
589 (setf (subseq string
0 10)
592 (setf (fill-pointer string
) 5)
593 (assert (truename (parse-namestring string nil
*default-pathname-defaults
*
595 (assert (truename (parse-namestring string nil
*default-pathname-defaults
*
597 (assert-error (parse-namestring string nil
598 *default-pathname-defaults
*
600 (assert-error (parse-namestring string nil
601 *default-pathname-defaults
*
602 :start
(opaque-identity -
1) :end
5))
603 (assert-error (parse-namestring string nil
604 *default-pathname-defaults
*
606 (assert-error (parse-namestring string nil
607 *default-pathname-defaults
*
610 ;;; Function POSITION, POSITION-IF, POSITION-IF-NOT
611 (sequence-bounding-indices-test
612 (format t
"~&/Function POSITION, POSITION-IF, POSITION-IF-NOT")
614 (assert (= (position #\a string
:start
0 :end nil
) 0))
615 (assert (= (position #\a string
:start
0 :end
5) 0))
616 (assert-error (position #\a string
:start
0 :end
6))
617 (assert-error (position #\a string
:start
(opaque-identity -
1) :end
5))
618 (assert-error (position #\a string
:start
4 :end
2))
619 (assert-error (position #\a string
:start
6 :end
9))
620 (assert (= (position-if #'alpha-char-p string
:start
0 :end nil
) 0))
621 (assert (= (position-if #'alpha-char-p string
:start
0 :end
5) 0))
623 (position-if #'alpha-char-p string
:start
0 :end
6))
625 (position-if #'alpha-char-p string
:start
(opaque-identity -
1) :end
5))
627 (position-if #'alpha-char-p string
:start
4 :end
2))
629 (position-if #'alpha-char-p string
:start
6 :end
9))
630 (assert (eq (position-if-not #'alpha-char-p string
:start
0 :end nil
) nil
))
631 (assert (eq (position-if-not #'alpha-char-p string
:start
0 :end
5) nil
))
633 (position-if-not #'alpha-char-p string
:start
0 :end
6))
635 (position-if-not #'alpha-char-p string
:start
(opaque-identity -
1) :end
5))
637 (position-if-not #'alpha-char-p string
:start
4 :end
2))
639 (position-if-not #'alpha-char-p string
:start
6 :end
9)))
641 ;;; Function READ-FROM-STRING
642 (sequence-bounding-indices-test
643 (format t
"~&/Function READ-FROM-STRING")
644 (setf (subseq string
0 5) "(a b)")
645 (assert (equal (read-from-string string nil nil
:start
0 :end
5) '(a b
)))
646 (assert (equal (read-from-string string nil nil
:start
0 :end nil
) '(a b
)))
647 (assert-error (read-from-string string nil nil
:start
0 :end
6))
648 (assert-error (read-from-string string nil nil
:start
(opaque-identity -
1) :end
5))
649 (assert-error (read-from-string string nil nil
:start
4 :end
2))
650 (assert-error (read-from-string string nil nil
:start
6 :end
9)))
653 (sequence-bounding-indices-test
654 (format t
"~&/Function REDUCE")
655 (setf (subseq string
0 5) "abcde")
656 (assert (equal (reduce #'list
* string
:from-end t
:start
0 :end nil
)
657 '(#\a #\b #\c
#\d .
#\e
)))
658 (assert (equal (reduce #'list
* string
:from-end t
:start
0 :end
5)
659 '(#\a #\b #\c
#\d .
#\e
)))
660 (assert-error (reduce #'list
* string
:start
0 :end
6))
661 (assert-error (reduce #'list
* string
:start
(opaque-identity -
1) :end
5))
662 (assert-error (reduce #'list
* string
:start
4 :end
2))
663 (assert-error (reduce #'list
* string
:start
6 :end
9)))
665 ;;; Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, DELETE, DELETE-IF,
667 (sequence-bounding-indices-test
668 (format t
"~&/Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, ...")
669 (assert (equal (remove #\a string
:start
0 :end nil
) ""))
670 (assert (equal (remove #\a string
:start
0 :end
5) ""))
671 (assert-error (remove #\a string
:start
0 :end
6))
672 (assert-error (remove #\a string
:start
(opaque-identity -
1) :end
5))
673 (assert-error (remove #\a string
:start
4 :end
2))
674 (assert-error (remove #\a string
:start
6 :end
9))
675 (assert (equal (remove-if #'alpha-char-p string
:start
0 :end nil
) ""))
676 (assert (equal (remove-if #'alpha-char-p string
:start
0 :end
5) ""))
678 (remove-if #'alpha-char-p string
:start
0 :end
6))
680 (remove-if #'alpha-char-p string
:start
(opaque-identity -
1) :end
5))
682 (remove-if #'alpha-char-p string
:start
4 :end
2))
684 (remove-if #'alpha-char-p string
:start
6 :end
9))
685 (assert (equal (remove-if-not #'alpha-char-p string
:start
0 :end nil
)
687 (assert (equal (remove-if-not #'alpha-char-p string
:start
0 :end
5)
690 (remove-if-not #'alpha-char-p string
:start
0 :end
6))
692 (remove-if-not #'alpha-char-p string
:start
(opaque-identity -
1) :end
5))
694 (remove-if-not #'alpha-char-p string
:start
4 :end
2))
696 (remove-if-not #'alpha-char-p string
:start
6 :end
9)))
697 (sequence-bounding-indices-test
698 (format t
"~&/... DELETE, DELETE-IF, DELETE-IF-NOT")
699 (assert (equal (delete #\a string
:start
0 :end nil
) ""))
701 (assert (equal (delete #\a string
:start
0 :end
5) ""))
703 (assert-error (delete #\a string
:start
0 :end
6))
705 (assert-error (delete #\a string
:start
(opaque-identity -
1) :end
5))
707 (assert-error (delete #\a string
:start
4 :end
2))
709 (assert-error (delete #\a string
:start
6 :end
9))
711 (assert (equal (delete-if #'alpha-char-p string
:start
0 :end nil
) ""))
713 (assert (equal (delete-if #'alpha-char-p string
:start
0 :end
5) ""))
716 (delete-if #'alpha-char-p string
:start
0 :end
6))
719 (delete-if #'alpha-char-p string
:start
(opaque-identity -
1) :end
5))
722 (delete-if #'alpha-char-p string
:start
4 :end
2))
725 (delete-if #'alpha-char-p string
:start
6 :end
9))
727 (assert (equal (delete-if-not #'alpha-char-p string
:start
0 :end nil
)
730 (assert (equal (delete-if-not #'alpha-char-p string
:start
0 :end
5)
734 (delete-if-not #'alpha-char-p string
:start
0 :end
6))
737 (delete-if-not #'alpha-char-p string
:start
(opaque-identity -
1) :end
5))
740 (delete-if-not #'alpha-char-p string
:start
4 :end
2))
743 (delete-if-not #'alpha-char-p string
:start
6 :end
9)))
745 ;;; Function REMOVE-DUPLICATES, DELETE-DUPLICATES
746 (sequence-bounding-indices-test
747 (format t
"~&/Function REMOVE-DUPLICATES, DELETE-DUPLICATES")
748 (assert (string= (remove-duplicates string
:start
0 :end
5) "a"))
749 (assert (string= (remove-duplicates string
:start
0 :end nil
) "a"))
750 (assert-error (remove-duplicates string
:start
0 :end
6))
751 (assert-error (remove-duplicates string
:start
(opaque-identity -
1) :end
5))
752 (assert-error (remove-duplicates string
:start
4 :end
2))
753 (assert-error (remove-duplicates string
:start
6 :end
9))
754 (assert (string= (delete-duplicates string
:start
0 :end
5) "a"))
756 (assert (string= (delete-duplicates string
:start
0 :end nil
) "a"))
758 (assert-error (delete-duplicates string
:start
0 :end
6))
760 (assert-error (delete-duplicates string
:start
(opaque-identity -
1) :end
5))
762 (assert-error (delete-duplicates string
:start
4 :end
2))
764 (assert-error (delete-duplicates string
:start
6 :end
9)))
767 (sequence-bounding-indices-test
768 (format t
"~&/Function REPLACE")
769 (assert (string= (replace string
"bbbbb" :start1
0 :end1
5) "bbbbb"))
770 (assert (string= (replace (copy-seq "ccccc")
772 :start2
0 :end2 nil
) "bbbbb"))
773 (assert-error (replace string
"ccccc" :start1
0 :end1
6))
774 (assert-error (replace string
"ccccc" :start2
(opaque-identity -
1) :end2
5))
775 (assert-error (replace string
"ccccc" :start1
4 :end1
2))
776 (assert-error (replace string
"ccccc" :start1
6 :end1
9)))
779 (sequence-bounding-indices-test
780 (format t
"~&/Function SEARCH")
781 (assert (= (search "aa" string
:start2
0 :end2
5) 0))
782 (assert (null (search string
"aa" :start1
0 :end2 nil
)))
783 (assert-error (search "aa" string
:start2
0 :end2
6))
784 (assert-error (search "aa" string
:start2
(opaque-identity -
1) :end2
5))
785 (assert-error (search "aa" string
:start2
4 :end2
2))
786 (assert-error (search "aa" string
:start2
6 :end2
9)))
788 ;;; Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE,
789 ;;; NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE
790 (defmacro string-case-frob
(fn)
792 (assert-error (,fn string
:start
0 :end
6))
793 (assert-error (,fn string
:start
(opaque-identity -
1) :end
5))
794 (assert-error (,fn string
:start
4 :end
2))
795 (assert-error (,fn string
:start
6 :end
9))))
797 (sequence-bounding-indices-test
798 (format t
"~&/Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE, ...")
799 (string-case-frob string-upcase
)
800 (string-case-frob string-downcase
)
801 (string-case-frob string-capitalize
)
802 (format t
"~&/... NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE")
803 (string-case-frob nstring-upcase
)
804 (string-case-frob nstring-downcase
)
805 (string-case-frob nstring-capitalize
))
807 ;;; Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=,
808 ;;; STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, STRING-GREATERP,
809 ;;; STRING-NOT-GREATERP, STRING-NOT-LESSP
810 (defmacro string-predicate-frob
(fn)
812 (,fn string
"abcde" :start1
0 :end1
5)
813 (,fn
"fghij" string
:start2
0 :end2 nil
)
814 (assert-error (,fn string
"klmno"
816 (assert-error (,fn
"pqrst" string
817 :start2
(opaque-identity -
1) :end2
5))
818 (assert-error (,fn
"uvwxy" string
820 (assert-error (,fn string
"z" :start2
6 :end2
9))))
821 (sequence-bounding-indices-test
822 (format t
"~&/Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=, ...")
823 (string-predicate-frob string
=)
824 (string-predicate-frob string
/=)
825 (string-predicate-frob string
<)
826 (string-predicate-frob string
>)
827 (string-predicate-frob string
<=)
828 (string-predicate-frob string
>=))
829 (sequence-bounding-indices-test
830 (format t
"~&/... STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, ...")
831 (string-predicate-frob string-equal
)
832 (string-predicate-frob string-not-equal
)
833 (string-predicate-frob string-lessp
))
834 (sequence-bounding-indices-test
835 (format t
"~&/... STRING-GREATERP, STRING-NOT-GREATERP, STRING-NOT-LESSP")
836 (string-predicate-frob string-greaterp
)
837 (string-predicate-frob string-not-greaterp
)
838 (string-predicate-frob string-not-lessp
))
840 ;;; Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT,
841 ;;; NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
842 (sequence-bounding-indices-test
843 (format t
"~&/Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT, ...")
844 (assert (string= (substitute #\b #\a string
:start
0 :end
5) "bbbbb"))
845 (assert (string= (substitute #\c
#\a string
:start
0 :end nil
)
847 (assert-error (substitute #\b #\a string
:start
0 :end
6))
848 (assert-error (substitute #\b #\a string
:start
(opaque-identity -
1) :end
5))
849 (assert-error (substitute #\b #\a string
:start
4 :end
2))
850 (assert-error (substitute #\b #\a string
:start
6 :end
9))
851 (assert (string= (substitute-if #\b #'alpha-char-p string
854 (assert (string= (substitute-if #\c
#'alpha-char-p string
857 (assert-error (substitute-if #\b #'alpha-char-p string
859 (assert-error (substitute-if #\b #'alpha-char-p string
860 :start
(opaque-identity -
1) :end
5))
861 (assert-error (substitute-if #\b #'alpha-char-p string
863 (assert-error (substitute-if #\b #'alpha-char-p string
865 (assert (string= (substitute-if-not #\b #'alpha-char-p string
868 (assert (string= (substitute-if-not #\c
#'alpha-char-p string
871 (assert-error (substitute-if-not #\b #'alpha-char-p string
873 (assert-error (substitute-if-not #\b #'alpha-char-p string
874 :start
(opaque-identity -
1) :end
5))
875 (assert-error (substitute-if-not #\b #'alpha-char-p string
877 (assert-error (substitute-if-not #\b #'alpha-char-p string
879 (sequence-bounding-indices-test
880 (format t
"~&/... NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT")
881 (assert (string= (nsubstitute #\b #\a string
:start
0 :end
5) "bbbbb"))
883 (assert (string= (nsubstitute #\c
#\a string
:start
0 :end nil
)
886 (assert-error (nsubstitute #\b #\a string
:start
0 :end
6))
888 (assert-error (nsubstitute #\b #\a string
:start
(opaque-identity -
1) :end
5))
890 (assert-error (nsubstitute #\b #\a string
:start
4 :end
2))
892 (assert-error (nsubstitute #\b #\a string
:start
6 :end
9))
894 (assert (string= (nsubstitute-if #\b #'alpha-char-p string
898 (assert (string= (nsubstitute-if #\c
#'alpha-char-p string
902 (assert-error (nsubstitute-if #\b #'alpha-char-p string
905 (assert-error (nsubstitute-if #\b #'alpha-char-p string
906 :start
(opaque-identity -
1) :end
5))
908 (assert-error (nsubstitute-if #\b #'alpha-char-p string
911 (assert-error (nsubstitute-if #\b #'alpha-char-p string
914 (assert (string= (nsubstitute-if-not #\b #'alpha-char-p string
918 (assert (string= (nsubstitute-if-not #\c
#'alpha-char-p string
922 (assert-error (nsubstitute-if-not #\b #'alpha-char-p string
925 (assert-error (nsubstitute-if-not #\b #'alpha-char-p string
926 :start
(opaque-identity -
1) :end
5))
928 (assert-error (nsubstitute-if-not #\b #'alpha-char-p string
931 (assert-error (nsubstitute-if-not #\b #'alpha-char-p string
933 ;;; Function WRITE-STRING, WRITE-LINE
934 (sequence-bounding-indices-test
935 (format t
"~&/Function WRITE-STRING, WRITE-LINE")
936 (write-string string
*standard-output
* :start
0 :end
5)
937 (write-string string
*standard-output
* :start
0 :end nil
)
938 (assert-error (write-string string
*standard-output
*
940 (assert-error (write-string string
*standard-output
*
941 :start
(opaque-identity -
1) :end
5))
942 (assert-error (write-string string
*standard-output
*
944 (assert-error (write-string string
*standard-output
*
946 (write-line string
*standard-output
* :start
0 :end
5)
947 (write-line string
*standard-output
* :start
0 :end nil
)
948 (assert-error (write-line string
*standard-output
*
950 (assert-error (write-line string
*standard-output
*
951 :start
(opaque-identity -
1) :end
5))
952 (assert-error (write-line string
*standard-output
*
954 (assert-error (write-line string
*standard-output
*
957 ;;; Macro WITH-INPUT-FROM-STRING
958 (sequence-bounding-indices-test
959 (format t
"~&/Macro WITH-INPUT-FROM-STRING")
960 (with-input-from-string (s string
:start
0 :end
5)
961 (assert (char= (read-char s
) #\a)))
962 (with-input-from-string (s string
:start
0 :end nil
)
963 (assert (char= (read-char s
) #\a)))
965 (with-input-from-string (s string
:start
0 :end
6)
968 (with-input-from-string (s string
:start
(opaque-identity -
1) :end
5)
971 (with-input-from-string (s string
:start
4 :end
2)
974 (with-input-from-string (s string
:start
6 :end
9)
977 ;;; testing bit-bashing according to _The Practice of Programming_
978 (defun fill-bytes-for-testing (bitsize)
979 "Return a list of 'bytes' of type (MOD BITSIZE)."
980 (remove-duplicates (list 0
981 (1- (ash 1 (1- bitsize
)))
983 (1- (ash 1 bitsize
)))))
985 (defun fill-with-known-value (value size
&rest vectors
)
986 (dolist (vec vectors
)
988 (setf (aref vec i
) value
))))
990 (defun collect-fill-amounts (n-power)
992 (loop for i from
0 upto n-power
993 collect
(1- (expt 2 i
))
995 collect
(1+ (expt 2 i
)))))
997 (defun test-fill-bashing (bitsize padding-amount n-power
)
998 (let* ((size (+ (* padding-amount
2) (expt 2 n-power
) (* padding-amount
2)))
999 (standard (make-array size
:element-type
`(unsigned-byte ,bitsize
)))
1000 (bashed (make-array size
:element-type
`(unsigned-byte ,bitsize
)))
1001 (fill-amounts (collect-fill-amounts n-power
))
1002 (bash-function (intern (format nil
"UB~A-BASH-FILL" bitsize
)
1003 (find-package "SB-KERNEL"))))
1004 (format t
"~&/Function ~A..." bash-function
)
1005 (loop for offset from padding-amount below
(* 2 padding-amount
) do
1006 (dolist (c (fill-bytes-for-testing bitsize
))
1007 (dolist (n fill-amounts
)
1008 (fill-with-known-value (mod (lognot c
) (ash 1 bitsize
)) n
1011 ;; a) the standard slow way
1012 (locally (declare (notinline fill
))
1013 (fill standard c
:start offset
:end
(+ offset n
)))
1014 ;; b) the blazingly fast way
1015 (let ((value (loop for i from
0 by bitsize
1016 until
(= i sb-vm
:n-word-bits
)
1018 (funcall bash-function value bashed offset n
))
1020 (when (mismatch standard bashed
)
1021 (format t
"Test with offset ~A, fill ~A and length ~A failed.~%"
1023 (format t
"Mismatch: ~A ~A~%"
1024 (subseq standard
0 (+ offset n
1))
1025 (subseq bashed
0 (+ offset n
1)))
1026 (return-from test-fill-bashing nil
))))
1027 finally
(return t
))))
1029 (defun test-copy-bashing (bitsize padding-amount n-power
)
1030 (let* ((size (+ (* padding-amount
2) (expt 2 n-power
) (* padding-amount
2)))
1031 (standard-dst (make-array size
:element-type
`(unsigned-byte ,bitsize
)))
1032 (bashed-dst (make-array size
:element-type
`(unsigned-byte ,bitsize
)))
1033 (source (make-array size
:element-type
`(unsigned-byte ,bitsize
)))
1034 (fill-amounts (collect-fill-amounts n-power
))
1035 (bash-function (intern (format nil
"UB~A-BASH-COPY" bitsize
)
1036 (find-package "SB-KERNEL"))))
1037 (format t
"~&/Function ~A..." bash-function
)
1038 (do ((source-offset padding-amount
(1+ source-offset
)))
1039 ((>= source-offset
(* padding-amount
2))
1042 (do ((target-offset padding-amount
(1+ target-offset
)))
1043 ((>= target-offset
(* padding-amount
2)))
1044 (dolist (c (fill-bytes-for-testing bitsize
))
1045 (dolist (n fill-amounts
)
1046 (fill-with-known-value (mod (lognot c
) (ash 1 bitsize
)) size
1047 source standard-dst bashed-dst
)
1048 ;; fill with test data
1049 (fill source c
:start source-offset
:end
(+ source-offset n
))
1050 ;; copy filled test data to test vectors
1052 (replace standard-dst source
1053 :start1 target-offset
:end1
(+ target-offset n
)
1054 :start2 source-offset
:end2
(+ source-offset n
))
1055 ;; b) the blazingly fast way
1056 (funcall bash-function source source-offset
1057 bashed-dst target-offset n
)
1059 (when (mismatch standard-dst bashed-dst
)
1060 (format t
"Test with target-offset ~A, source-offset ~A, fill ~A, and length ~A failed.~%"
1061 target-offset source-offset c n
)
1062 (format t
"Mismatch:~% correct ~A~% actual ~A~%"
1065 (return-from test-copy-bashing nil
))))))))
1067 ;; Too slow for the interpreter
1068 #+#.
(cl:if
(cl:eq sb-ext
:*evaluator-mode
* :compile
) '(and) '(or))
1069 (loop for i
= 1 then
(* i
2) do
1070 ;; the bare '13' here is fairly arbitrary, except that it's been
1071 ;; reduced from '32', which made the tests take aeons; '8' provides
1072 ;; a good range of lengths over which to fill and copy, which
1073 ;; should tease out most errors in the code (if any exist). (It
1074 ;; also makes this part of the test suite finish reasonably
1076 (assert (time (test-fill-bashing i
13 8)))
1077 (assert (time (test-copy-bashing i
13 8)))
1078 until
(= i sb-vm
:n-word-bits
))
1080 (defun test-inlined-bashing (bitsize)
1081 ;; We have to compile things separately for each bitsize so the
1082 ;; compiler will work out the array type and trigger the REPLACE
1086 (let* ((n-elements-per-word ,(truncate sb-vm
:n-word-bits bitsize
))
1087 (size (* 3 n-elements-per-word
))
1088 (standard-dst (make-array size
:element-type
'(unsigned-byte ,bitsize
)))
1089 (bashed-dst (make-array size
:element-type
'(unsigned-byte ,bitsize
)))
1090 (source (make-array size
:element-type
'(unsigned-byte ,bitsize
))))
1091 (declare (type (simple-array (unsigned-byte ,bitsize
) (*))
1092 source standard-dst bashed-dst
))
1094 (offset n-elements-per-word
(1+ offset
)))
1095 ((>= offset
(* 2 n-elements-per-word
)) t
)
1096 (dolist (c (fill-bytes-for-testing ,bitsize
))
1097 (fill-with-known-value (mod (lognot c
) (ash 1 ,bitsize
)) size
1098 source standard-dst bashed-dst
)
1099 ;; fill with test-data
1100 (fill source c
:start offset
:end
(+ offset n-elements-per-word
))
1101 ;; copy filled data to test vectors
1103 ;; a) the slow way (which is actually fast, since this
1104 ;; should be transformed into UB*-BASH-COPY)
1105 (replace standard-dst source
1106 :start1
(- offset n-elements-per-word i
)
1107 :start2
(- offset n-elements-per-word i
)
1108 :end1 offset
:end2 offset
)
1109 ;; b) the fast way--we fold the
1110 ;; :START{1,2} arguments above ourselves
1111 ;; to trigger the REPLACE transform
1112 (replace bashed-dst source
1113 :start1
0 :start2
0 :end1 offset
:end2 offset
)
1115 (when (or (mismatch standard-dst bashed-dst
)
1116 ;; trigger COPY-SEQ transform
1117 (mismatch (copy-seq standard-dst
) bashed-dst
)
1118 ;; trigger SUBSEQ transform
1119 (mismatch (subseq standard-dst
(- offset n-elements-per-word i
))
1121 (format t
"Test with target-offset ~A, source-offset ~A, fill ~A, and length ~A failed.~%"
1123 (format t
"Mismatch:~% correct ~A~% actual ~A~%"
1126 (return-from nil nil
))))))))
1127 (funcall (checked-compile lambda-form
))))
1129 #+#.
(cl:if
(cl:eq sb-ext
:*evaluator-mode
* :compile
) '(and) '(or))
1130 (loop for i
= 1 then
(* i
2) do
1131 (assert (test-inlined-bashing i
))
1132 until
(= i sb-vm
:n-word-bits
))
1134 ;;; tests from the Sacla test suite via Eric Marsden, 2007-05-07
1135 (remove-duplicates (vector 1 2 2 1) :test-not
(lambda (a b
) (not (= a b
))))
1137 (delete-duplicates (vector #\a #\b #\c
#\a)
1138 :test-not
(lambda (a b
) (not (char-equal a b
))))
1141 (let ((l (list 1 2 3)))
1142 (assert (eq l
(fill l
0 :start
1 :end
2)))
1143 (assert (equal l
'(1 0 3)))
1144 (assert (eq l
(fill l
'x
:start
2 :end
3)))
1145 (assert (equal l
'(1 0 x
)))
1146 (assert (eq l
(fill l
'y
:start
1)))
1147 (assert (equal l
'(1 y y
)))
1148 (assert (eq l
(fill l
'z
:end
2)))
1149 (assert (equal l
'(z z y
)))
1150 (assert (eq l
(fill l
1)))
1151 (assert (equal l
'(1 1 1)))
1152 (assert-error (fill l
0 :start
4))
1153 (assert-error (fill l
0 :end
4))
1154 (assert-error (fill l
0 :start
2 :end
1)))
1156 ;;; Both :TEST and :TEST-NOT provided
1157 (with-test (:name
:test-and-test-not-to-adjoin
)
1158 (multiple-value-bind (fun failure-p warnings
)
1159 (checked-compile `(lambda (item test test-not
)
1160 (adjoin item
'(1 2 3 :foo
)
1162 :test-not test-not
))
1164 (declare (ignore failure-p
))
1165 (assert (= 1 (length warnings
)))
1166 (assert-error (funcall fun
1 #'eql
(complement #'eql
)))))
1168 ;;; tests of deftype types equivalent to STRING or SIMPLE-STRING
1169 (deftype %string
() 'string
)
1170 (deftype %simple-string
() 'simple-string
)
1171 (deftype string-3
() '(string 3))
1172 (deftype simple-string-3
() '(simple-string 3))
1174 (with-test (:name
:user-defined-string-types-map-etc
)
1175 (dolist (type '(%string %simple-string string-3 simple-string-3
))
1176 (assert (string= "foo" (coerce '(#\f #\o
#\o
) type
)))
1177 (assert (string= "foo" (map type
'identity
#(#\f #\o
#\o
))))
1178 (assert (string= "foo" (merge type
(copy-seq '(#\o
)) (copy-seq '(#\f #\o
))
1180 (assert (string= "foo" (concatenate type
'(#\f) "oo")))
1181 (assert (string= "ooo" (make-sequence type
3 :initial-element
#\o
)))))
1182 (with-test (:name
:user-defined-string-types-map-etc-error
)
1183 (dolist (type '(string-3 simple-string-3
))
1184 (assert-error (coerce '(#\q
#\u
#\u
#\x
) type
))
1185 (assert-error (map type
'identity
#(#\q
#\u
#\u
#\x
)))
1186 (assert-error (merge type
(copy-seq '(#\q
#\x
)) (copy-seq "uu") 'char
<))
1187 (assert-error (concatenate type
"qu" '(#\u
#\x
)))
1188 (assert-error (make-sequence type
4 :initial-element
#\u
))))
1190 (defun test-bit-position (size set start end from-end res
)
1191 (let ((v (make-array size
:element-type
'bit
:initial-element
0)))
1194 (dolist (f (list (checked-compile
1195 `(lambda (b v s e fe
)
1196 (position b
(the bit-vector v
) :start s
:end e
:from-end fe
)))
1198 `(lambda (b v s e fe
)
1200 (position 1 (the bit-vector v
) :start s
:end e
:from-end fe
)))
1202 `(lambda (b v s e fe
)
1203 (position b
(the vector v
) :start s
:end e
:from-end fe
)))))
1204 (let ((got (funcall f
1 v start end from-end
)))
1205 (unless (eql res got
)
1206 (cerror "Continue" "POSITION 1, Wanted ~S, got ~S.~% size = ~S, set = ~S, from-end = ~S"
1208 size set from-end
)))))
1209 (let ((v (make-array size
:element-type
'bit
:initial-element
1)))
1212 (dolist (f (list (checked-compile
1213 `(lambda (b v s e fe
)
1214 (position b
(the bit-vector v
) :start s
:end e
:from-end fe
)))
1216 `(lambda (b v s e fe
)
1218 (position 0 (the bit-vector v
) :start s
:end e
:from-end fe
)))
1220 `(lambda (b v s e fe
)
1221 (position b
(the vector v
) :start s
:end e
:from-end fe
)))))
1222 (let ((got (funcall f
0 v start end from-end
)))
1223 (unless (eql res got
)
1224 (cerror "Continue" "POSITION 0, Wanted ~S, got ~S.~% size = ~S, set = ~S, from-end = ~S"
1226 size set from-end
))))))
1228 (defun random-test-bit-position (n)
1230 do
(let* ((vector (make-array (+ 2 (random 5000)) :element-type
'bit
))
1231 (offset (random (1- (length vector
))))
1232 (size (1+ (random (- (length vector
) offset
))))
1233 (disp (make-array size
:element-type
'bit
:displaced-to vector
1234 :displaced-index-offset offset
)))
1235 (assert (plusp size
))
1237 do
(setf (bit vector
(random (length vector
))) 1))
1239 (declare (bit-vector orig
))
1240 (let ((copy (coerce orig
'simple-vector
))
1241 (p0 (random (length orig
)))
1242 (p1 (1+ (random (length orig
)))))
1243 (multiple-value-bind (s e
)
1247 (assert (eql (position 1 copy
:start s
:end e
)
1248 (position 1 orig
:start s
:end e
)))
1249 (assert (eql (position 1 copy
:start s
:end e
:from-end t
)
1250 (position 1 orig
:start s
:end e
:from-end t
)))))))
1254 (with-test (:name
:bit-position
)
1255 (test-bit-position 0 (list) 0 0 nil nil
)
1256 (test-bit-position 0 (list) 0 0 t nil
)
1257 (test-bit-position 1 (list 0) 0 0 nil nil
)
1258 (test-bit-position 1 (list 0) 0 0 t nil
)
1259 (test-bit-position 1 (list 0) 0 1 nil
0)
1260 (test-bit-position 1 (list 0) 0 1 t
0)
1261 (test-bit-position 10 (list 0 1) 0 1 nil
0)
1262 (test-bit-position 10 (list 0 1) 1 1 nil nil
)
1263 (test-bit-position 10 (list 0 1) 0 1 t
0)
1264 (test-bit-position 10 (list 0 1) 1 1 t nil
)
1265 (test-bit-position 10 (list 0 3) 1 4 nil
3)
1266 (test-bit-position 10 (list 0 3) 1 4 t
3)
1267 (test-bit-position 10 (list 0 3 6) 1 10 nil
3)
1268 (test-bit-position 10 (list 0 3 6) 1 10 t
6)
1269 (test-bit-position 1000 (list 128 700) 20 500 nil
128)
1270 (test-bit-position 1000 (list 128 700) 20 500 t
128)
1271 (test-bit-position 1000 (list 423 762) 200 800 nil
423)
1272 (test-bit-position 1000 (list 423 762) 200 800 t
762)
1273 (test-bit-position 1000 (list 298 299) 100 400 nil
298)
1274 (test-bit-position 1000 (list 298 299) 100 400 t
299))
1276 (with-test (:name
(:bit-position
:random-test
))
1277 (random-test-bit-position 10000))
1279 ;; REVERSE and NREVERSE should assert that the returned value
1280 ;; from a generic sequence operation is of type SEQUENCE.
1281 (defclass bogus-reversal-seq
(sequence standard-object
) ())
1282 (defmethod sequence:reverse
((self bogus-reversal-seq
))
1284 (with-test (:name
:generic-sequence-reverse
)
1285 (assert-error (reverse (make-instance 'bogus-reversal-seq
))))
1287 (with-test (:name
:abstract-base-sequence-satisfies-sequencep
)
1288 (assert (typep (sb-pcl::class-prototype
(find-class 'sequence
)) 'sequence
)))
1290 (defvar *macro-invocations
* 0)
1291 ;; in case someone adds more tests after this, don't mess up OPAQUE-IDENTITY
1292 (defun opaque-id-again (x) x
)
1293 (define-compiler-macro opaque-id-again
(x) (incf *macro-invocations
*) x
)
1294 (with-test (:name
:mapfoo-admits-compiler-macros
)
1295 (checked-compile '(lambda (l) (mapcar #'opaque-id-again l
)))
1296 (assert (= *macro-invocations
* 1))
1297 (checked-compile '(lambda (l) (some #'opaque-id-again l
)))
1298 (assert (= *macro-invocations
* 2)))