1.0.3.20: better SEARCH transform
[sbcl.git] / tests / seq.impure.lisp
1 ;;;; tests related to sequences
2
3 ;;;; This file is impure because we want to be able to use DEFUN.
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
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
10 ;;;; from CMU CL.
11 ;;;;
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.
15
16 (load "assertoid.lisp")
17
18 (defpackage :seq-test
19   (:use :cl :assertoid))
20
21 (in-package :seq-test)
22
23 ;;; helper functions for exercising SEQUENCE code on data of many
24 ;;; specialized types, and in many different optimization scenarios
25 (defun for-every-seq-1 (base-seq snippet)
26   (dolist (seq-type '(list
27                       (simple-array t 1)
28                       (vector t)
29                       (simple-array character 1)
30                       (vector character)
31                       (simple-array (signed-byte 4) 1)
32                       (vector (signed-byte 4))))
33     (flet ((entirely (eltype)
34              (every (lambda (el) (typep el eltype)) base-seq)))
35       (dolist (declaredness '(nil t))
36         (dolist (optimization '(((speed 3) (space 0))
37                                 ((speed 2) (space 2))
38                                 ((speed 1) (space 2))
39                                 ((speed 0) (space 1))))
40           (let* ((seq (if (eq seq-type 'list)
41                           (coerce base-seq 'list)
42                           (destructuring-bind (type-first &rest type-rest)
43                               seq-type
44                             (ecase type-first
45                               (simple-array
46                                (destructuring-bind (eltype one) type-rest
47                                  (assert (= one 1))
48                                  (if (entirely eltype)
49                                      (coerce base-seq seq-type)
50                                      (return))))
51                               (vector
52                                (destructuring-bind (eltype) type-rest
53                                  (if (entirely eltype)
54                                      (let ((initial-element
55                                             (cond ((subtypep eltype 'character)
56                                                    #\!)
57                                                   ((subtypep eltype 'number)
58                                                    0)
59                                                   (t #'error))))
60                                        (replace (make-array
61                                                  (+ (length base-seq)
62                                                     (random 3))
63                                                  :element-type eltype
64                                                  :fill-pointer
65                                                  (length base-seq)
66                                                  :initial-element
67                                                  initial-element)
68                                                 base-seq))
69                                      (return))))))))
70                  (lambda-expr `(lambda (seq)
71                                  ,@(when declaredness
72                                      `((declare (type ,seq-type seq))))
73                                  (declare (optimize ,@optimization))
74                                  ,snippet)))
75             (format t "~&~S~%" lambda-expr)
76             (multiple-value-bind (fun warnings-p failure-p)
77                 (compile nil lambda-expr)
78               (when (or warnings-p failure-p)
79                 (error "~@<failed compilation:~2I ~_LAMBDA-EXPR=~S ~_WARNINGS-P=~S ~_FAILURE-P=~S~:@>"
80                        lambda-expr warnings-p failure-p))
81               (format t "~&~S ~S~%~S~%~S ~S~%"
82                       base-seq snippet seq-type declaredness optimization)
83               (format t "~&(TYPEP SEQ 'SIMPLE-ARRAY)=~S~%"
84                       (typep seq 'simple-array))
85               (unless (funcall fun seq)
86                 (error "~@<failed test:~2I ~_BASE-SEQ=~S ~_SNIPPET=~S ~_SEQ-TYPE=~S ~_DECLAREDNESS=~S ~_OPTIMIZATION=~S~:@>"
87                        base-seq
88                        snippet
89                        seq-type
90                        declaredness
91                        optimization)))))))))
92 (defun for-every-seq (base-seq snippets)
93   (dolist (snippet snippets)
94     (for-every-seq-1 base-seq snippet)))
95
96 ;;; a wrapper to hide declared type information from the compiler, so
97 ;;; we don't get stopped by compiler warnings about e.g. compiling
98 ;;; (POSITION 1 #() :KEY #'ABS) when #() has been coerced to a string.
99 (defun indiscriminate (fun)
100   (lambda (&rest rest) (apply fun rest)))
101
102 ;;; asymmetric test arg order example from ANSI FIND definition page
103 (assert (eql #\space ; original example, depends on ASCII character ordering
104              (find #\d "here are some letters that can be looked at"
105                    :test #'char>)))
106 (assert (eql #\e ; modified example, depends only on standard a-z ordering
107              (find #\f "herearesomeletters" :test #'char>)))
108 (assert (eql 4 ; modified more, avoids charset technicalities completely
109              (find 5 '(6 4) :test '>)))
110
111 ;;; tests of FIND, POSITION, FIND-IF, and POSITION-IF (and a few for
112 ;;; deprecated FIND-IF-NOT and POSITION-IF-NOT too)
113 (for-every-seq #()
114   '((null (find 1 seq))
115     (null (find 1 seq :from-end t))
116     (null (position 1 seq :key (indiscriminate #'abs)))
117     (null (position nil seq :test (constantly t)))
118     (null (position nil seq :test nil))
119     (null (position nil seq :test-not nil))
120     (null (find-if #'1+ seq :key (indiscriminate #'log)))
121     (null (position-if #'identity seq :from-end t))
122     (null (find-if-not #'packagep seq))
123     (null (position-if-not #'packagep seq :key nil))))
124 (for-every-seq #(1)
125   '((null (find 2 seq))
126     ;; Get the argument ordering for asymmetric tests like #'> right.
127     ;; (bug reported and fixed by Alexey Dejneka sbcl-devel 2001-10-17)
128     (eql 1 (find 2 seq :test #'>))
129     (find 2 seq :key #'1+)
130     (find 1 seq :from-end t)
131     (null (find 1 seq :from-end t :start 1))
132     (null (find 0 seq :from-end t))
133     (eql 0 (position 1 seq :key #'abs))
134     (null (position nil seq :test 'equal))
135     (eql 1 (find-if #'1- seq :key #'log))
136     (eql 0 (position-if #'identity seq :from-end t))
137     (null (find-if-not #'sin seq))
138     (eql 0 (position-if-not #'packagep seq :key 'identity))))
139 (for-every-seq #(1 2 3 2 1)
140   '((find 3 seq)
141     (find 3 seq :from-end 'yes)
142     (eql 1 (position 1.5 seq :test #'<))
143     (eql 0 (position 0 seq :key '1-))
144     (eql 4 (position 0 seq :key '1- :from-end t))
145     (eql 2 (position 4 seq :key '1+))
146     (eql 2 (position 4 seq :key '1+ :from-end t))
147     (eql 1 (position 2 seq))
148     (eql 1 (position 2 seq :start 1))
149     (null (find 2 seq :start 1 :end 1))
150     (eql 3 (position 2 seq :start 2))
151     (eql 3 (position 2 seq :key nil :from-end t))
152     (eql 2 (position 3 seq :test '=))
153     (eql 0 (position 3 seq :test-not 'equalp))
154     (eql 2 (position 3 seq :test 'equal :from-end t))
155     (null (position 4 seq :test #'eql))
156     (null (find-if #'packagep seq))
157     (eql 1 (find-if #'plusp seq))
158     (eql 3 (position-if #'plusp seq :key #'1- :from-end t))
159     (eql 1 (position-if #'evenp seq))
160     (eql 3 (position-if #'evenp seq :from-end t))
161     (eql 2 (position-if #'plusp seq :from-end nil :key '1- :start 2))
162     (eql 3 (position-if #'plusp seq :from-end t :key '1- :start 2))
163     (null (position-if #'plusp seq :from-end t :key '1- :start 2 :end 2))
164     (null (find-if-not #'plusp seq))
165     (eql 0 (position-if-not #'evenp seq))
166     (eql 0 (search #(1) seq))
167     (eql 1 (search #(4 5) seq :key 'oddp))
168     (eql 1 (search #(-2) seq :test (lambda (a b) (= (- a) b))))
169     (eql 4 (search #(1) seq :start2 1))
170     (null (search #(3) seq :start2 3))
171     (eql 2 (search #(3) seq :start2 2))
172     (eql 0 (search #(1 2) seq))
173     (null (search #(2 1 3) seq))
174     (eql 0 (search #(0 1 2 4) seq :start1 1 :end1 3))
175     (eql 3 (search #(0 2 1 4) seq :start1 1 :end1 3))
176     (eql 4 (search #(1) seq :from-end t))
177     (eql 0 (search #(1 2) seq :from-end t))
178     (null (search #(1 2) seq :from-end t :start2 1))
179     (eql 0 (search #(0 1 2 4) seq :from-end t :start1 1 :end1 3))
180     (eql 3 (search #(0 2 1 4) seq :from-end t :start1 1 :end1 3))
181     (null (search #(2 1 3) seq :from-end t))))
182 (for-every-seq "string test"
183   '((null (find 0 seq))
184     (null (find #\D seq :key #'char-upcase))
185     (find #\E seq :key #'char-upcase)
186     (null (find #\e seq :key #'char-upcase))
187     (eql 3 (position #\i seq))
188     (eql 0 (position #\s seq :key #'char-downcase))
189     (eql 1 (position #\s seq :key #'char-downcase :test #'char/=))
190     (eql 9 (position #\s seq :from-end t :test #'char=))
191     (eql 10 (position #\s seq :from-end t :test #'char/=))
192     (eql 4 (position #\N seq :from-end t :key 'char-upcase :test #'char-equal))
193     (eql 5 (position-if (lambda (c) (equal #\g c)) seq))
194     (eql 5 (position-if (lambda (c) (equal #\g c)) seq :from-end t))
195     (find-if #'characterp seq)
196     (find-if (lambda (c) (typep c 'base-char)) seq :from-end t)
197     (null (find-if 'upper-case-p seq))))
198
199 ;;; SUBSEQ
200 (let ((avec (make-array 10
201                         :fill-pointer 4
202                         :initial-contents '(0 1 2 3 iv v vi vii iix ix))))
203   ;; These first five always worked AFAIK.
204   (assert (equalp (subseq avec 0 3) #(0 1 2)))
205   (assert (equalp (subseq avec 3 3) #()))
206   (assert (equalp (subseq avec 1 3) #(1 2)))
207   (assert (equalp (subseq avec 1) #(1 2 3)))
208   (assert (equalp (subseq avec 1 4) #(1 2 3)))
209   ;; SBCL bug found ca. 2002-05-01 by OpenMCL's correct handling of
210   ;; SUBSEQ, CSR's driving portable cross-compilation far enough to
211   ;; reach the SUBSEQ calls in assem.lisp, and WHN's sleazy
212   ;; translation of old CMU CL new-assem.lisp into sufficiently grotty
213   ;; portable Lisp that it passed suitable illegal values to SUBSEQ to
214   ;; exercise the bug:-|
215   ;;
216   ;; SUBSEQ should check its END value against logical LENGTH, not
217   ;; physical ARRAY-DIMENSION 0.
218   ;;
219   ;; fixed in sbcl-0.7.4.22 by WHN
220   (assert (null (ignore-errors (aref (subseq avec 1 5) 0)))))
221
222 ;;; FILL
223 (defun test-fill-typecheck (x)
224   (declare (optimize (safety 3) (space 2) (speed 1)))
225   (fill (make-string 10) x))
226
227 (assert (string= (test-fill-typecheck #\@) "@@@@@@@@@@"))
228 ;;; BUG 186, fixed in sbcl-0.7.5.5
229 (assert (null (ignore-errors (test-fill-typecheck 4097))))
230
231 ;;; MAKE-SEQUENCE, COERCE, CONCATENATE, MERGE, MAP and requested
232 ;;; result type (BUGs 46a, 46b, 66)
233 (macrolet ((assert-type-error (form)
234              `(assert (typep (nth-value 1 (ignore-errors ,form))
235                              'type-error))))
236   (dolist (type-stub '((simple-vector)
237                        (vector *)
238                        (vector (signed-byte 8))
239                        (vector (unsigned-byte 16))
240                        (vector (signed-byte 32))
241                        (simple-bit-vector)))
242     (declare (optimize safety))
243     (format t "~&~S~%" type-stub)
244     ;; MAKE-SEQUENCE
245     (assert (= (length (make-sequence `(,@type-stub) 10)) 10))
246     (assert (= (length (make-sequence `(,@type-stub 10) 10)) 10))
247     (assert-type-error (make-sequence `(,@type-stub 10) 11))
248     ;; COERCE
249     (assert (= (length (coerce '(0 0 0) `(,@type-stub))) 3))
250     (assert (= (length (coerce #(0 0 0) `(,@type-stub 3))) 3))
251     (assert-type-error (coerce #*111 `(,@type-stub 4)))
252     ;; CONCATENATE
253     (assert (= (length (concatenate `(,@type-stub) #(0 0 0) #*111)) 6))
254     (assert (equalp (concatenate `(,@type-stub) #(0 0 0) #*111)
255                    (coerce #(0 0 0 1 1 1) `(,@type-stub))))
256     (assert (= (length (concatenate `(,@type-stub 6) #(0 0 0) #*111)) 6))
257     (assert (equalp (concatenate `(,@type-stub 6) #(0 0 0) #*111)
258                    (coerce #(0 0 0 1 1 1) `(,@type-stub 6))))
259     (assert-type-error (concatenate `(,@type-stub 5) #(0 0 0) #*111))
260     ;; MERGE
261     (macrolet ((test (type)
262                  `(merge ,type (copy-seq #(0 1 0)) (copy-seq #*111) #'>)))
263       (assert (= (length (test `(,@type-stub))) 6))
264       (assert (equalp (test `(,@type-stub))
265                       (coerce #(1 1 1 0 1 0) `(,@type-stub))))
266       (assert (= (length (test `(,@type-stub 6))) 6))
267       (assert (equalp (test `(,@type-stub 6))
268                       (coerce #(1 1 1 0 1 0) `(,@type-stub 6))))
269       (assert-type-error (test `(,@type-stub 4))))
270     ;; MAP
271     (assert (= (length (map `(,@type-stub) #'logxor #(0 0 1 1) '(0 1 0 1))) 4))
272     (assert (equalp (map `(,@type-stub) #'logxor #(0 0 1 1) '(0 1 0 1))
273                    (coerce #(0 1 1 0) `(,@type-stub))))
274     (assert (= (length (map `(,@type-stub 4) #'logxor #(0 0 1 1) '(0 1 0 1)))
275                4))
276     (assert (equalp (map `(,@type-stub 4) #'logxor #(0 0 1 1) '(0 1 0 1))
277                    (coerce #(0 1 1 0) `(,@type-stub 4))))
278     (assert-type-error (map `(,@type-stub 5) #'logxor #(0 0 1 1) '(0 1 0 1))))
279   ;; some more CONCATENATE tests for strings
280   (locally
281       (declare (optimize safety))
282     (assert (string= (concatenate 'string "foo" " " "bar") "foo bar"))
283     (assert (string= (concatenate '(string 7) "foo" " " "bar") "foo bar"))
284     (assert-type-error (concatenate '(string 6) "foo" " " "bar"))
285     (assert (string= (concatenate '(string 6) "foo" #(#\b #\a #\r)) "foobar"))
286     (assert-type-error (concatenate '(string 7) "foo" #(#\b #\a #\r))))
287   ;; Non-VECTOR ARRAY types aren't allowed as vector type specifiers.
288   (locally
289     (declare (optimize safety))
290     (assert-type-error (concatenate 'simple-array "foo" "bar"))
291     (assert-type-error (map 'simple-array #'identity '(1 2 3)))
292     (assert (equalp #(11 13)
293                     (map '(simple-array fixnum (*)) #'+ '(1 2 3) '(10 11))))
294     (assert-type-error (coerce '(1 2 3) 'simple-array))
295     (assert-type-error (merge 'simple-array (list 1 3) (list 2 4) '<))
296     (assert (equalp #(3 2 1) (coerce '(3 2 1) '(vector fixnum))))
297     (assert-type-error (map 'array #'identity '(1 2 3)))
298     (assert-type-error (map '(array fixnum) #'identity '(1 2 3)))
299     (assert (equalp #(1 2 3) (coerce '(1 2 3) '(vector fixnum))))
300     ;; but COERCE has an exemption clause:
301     (assert (string= "foo" (coerce "foo" 'simple-array)))
302     ;; ... though not in all cases.
303     (assert-type-error (coerce '(#\f #\o #\o) 'simple-array))))
304
305 ;;; As pointed out by Raymond Toy on #lisp IRC, MERGE had some issues
306 ;;; with user-defined types until sbcl-0.7.8.11
307 (deftype list-typeoid () 'list)
308 (assert (equal '(1 2 3 4) (merge 'list-typeoid (list 1 3) (list 2 4) '<)))
309 ;;; and also with types that weren't precicely LIST
310 (assert (equal '(1 2 3 4) (merge 'cons (list 1 3) (list 2 4) '<)))
311
312 ;;; but wait, there's more! The NULL and CONS types also have implicit
313 ;;; length requirements:
314 (macrolet ((assert-type-error (form)
315              `(assert (typep (nth-value 1 (ignore-errors ,form))
316                              'type-error))))
317   (locally
318       (declare (optimize safety))
319     ;; MAKE-SEQUENCE
320     (assert-type-error (make-sequence 'cons 0))
321     (assert-type-error (make-sequence 'null 1))
322     (assert-type-error (make-sequence '(cons t null) 0))
323     (assert-type-error (make-sequence '(cons t null) 2))
324     ;; KLUDGE: I'm not certain that this test actually tests for what
325     ;; it should test, in that the type deriver and optimizers might
326     ;; be too smart for the good of an exhaustive test system.
327     ;; However, it makes me feel good.  -- CSR, 2002-10-18
328     (assert (null (make-sequence 'null 0)))
329     (assert (= (length (make-sequence 'cons 3)) 3))
330     (assert (= (length (make-sequence '(cons t null) 1)) 1))
331     ;; and NIL is not a valid type for MAKE-SEQUENCE
332     (assert-type-error (make-sequence 'nil 0))
333     ;; COERCE
334     (assert-type-error (coerce #(1) 'null))
335     (assert-type-error (coerce #() 'cons))
336     (assert-type-error (coerce #() '(cons t null)))
337     (assert-type-error (coerce #(1 2) '(cons t null)))
338     (assert (null (coerce #() 'null)))
339     (assert (= (length (coerce #(1) 'cons)) 1))
340     (assert (= (length (coerce #(1) '(cons t null))) 1))
341     (assert-type-error (coerce #() 'nil))
342     ;; MERGE
343     (assert-type-error (merge 'null (list 1 3) (list 2 4) '<))
344     (assert-type-error (merge 'cons () () '<))
345     (assert (null (merge 'null () () '<)))
346     (assert (= (length (merge 'cons (list 1 3) (list 2 4) '<)) 4))
347     (assert (= (length (merge '(cons t (cons t (cons t (cons t null))))
348                               (list 1 3) (list 2 4)
349                               '<))
350                4))
351     (assert-type-error (merge 'nil () () '<))
352     ;; CONCATENATE
353     (assert-type-error (concatenate 'null '(1) "2"))
354     (assert-type-error (concatenate 'cons #() ()))
355     (assert-type-error (concatenate '(cons t null) #(1 2 3) #(4 5 6)))
356     (assert (null (concatenate 'null () #())))
357     (assert (= (length (concatenate 'cons #() '(1) "2 3")) 4))
358     (assert (= (length (concatenate '(cons t cons) '(1) "34")) 3))
359     (assert-type-error (concatenate 'nil '(3)))
360     ;; FIXME: tests for MAP to come when some brave soul implements
361     ;; the analogous type checking for MAP/%MAP.
362     ))
363 \f
364 ;;; ELT should signal an error of type TYPE-ERROR if its index
365 ;;; argument isn't a valid sequence index for sequence:
366 (defun test-elt-signal (x)
367   (elt x 3))
368 (assert (raises-error? (test-elt-signal "foo") type-error))
369 (assert (eql (test-elt-signal "foob") #\b))
370 (locally
371   (declare (optimize (safety 3)))
372   (assert (raises-error? (elt (list 1 2 3) 3) type-error)))
373 \f
374 ;;; confusion in the refactoring led to this signalling an unbound
375 ;;; variable, not a type error.
376 (defun svrefalike (x)
377   (svref x 0))
378 (assert (raises-error? (svrefalike #*0) type-error))
379 \f
380 ;;; checks for uniform bounding index handling under SAFETY 3 code.
381 ;;;
382 ;;; KLUDGE: not all in one big form because that causes SBCL to spend
383 ;;; an absolute age trying to compile it.
384 (defmacro sequence-bounding-indices-test (&body body)
385   `(progn
386     (locally
387     ;; See Issues 332 [and 333(!)] in the CLHS
388     (declare (optimize (safety 3)))
389     (let ((string (make-array 10
390                               :fill-pointer 5
391                               :initial-element #\a
392                               :element-type 'base-char)))
393         ,(car body)
394         (format t "... BASE-CHAR")
395         (finish-output)
396         (flet ((reset ()
397                  (setf (fill-pointer string) 10)
398                  (fill string #\a)
399                  (setf (fill-pointer string) 5)))
400           (declare (ignorable #'reset))
401           ,@(cdr body))))
402     (locally
403       ;; See Issues 332 [and 333(!)] in the CLHS
404       (declare (optimize (safety 3)))
405       (let ((string (make-array 10
406                                 :fill-pointer 5
407                                 :initial-element #\a
408                                 :element-type 'character)))
409         ,(car body)
410         (format t "... CHARACTER")
411         (finish-output)
412       (flet ((reset ()
413                (setf (fill-pointer string) 10)
414                (fill string #\a)
415                (setf (fill-pointer string) 5)))
416         (declare (ignorable #'reset))
417           ,@(cdr body))))))
418
419 (declaim (notinline opaque-identity))
420 (defun opaque-identity (x) x)
421 ;;; Accessor SUBSEQ
422 (sequence-bounding-indices-test
423  (format t "~&/Accessor SUBSEQ")
424  (assert (string= (subseq string 0 5) "aaaaa"))
425  (assert (raises-error? (subseq string 0 6)))
426  (assert (raises-error? (subseq string (opaque-identity -1) 5)))
427  (assert (raises-error? (subseq string 4 2)))
428  (assert (raises-error? (subseq string 6)))
429  (assert (string= (setf (subseq string 0 5) "abcde") "abcde"))
430  (assert (string= (subseq string 0 5) "abcde"))
431  (reset)
432  (assert (raises-error? (setf (subseq string 0 6) "abcdef")))
433  (assert (raises-error? (setf (subseq string (opaque-identity -1) 5) "abcdef")))
434  (assert (raises-error? (setf (subseq string 4 2) "")))
435  (assert (raises-error? (setf (subseq string 6) "ghij"))))
436
437 ;;; Function COUNT, COUNT-IF, COUNT-IF-NOT
438 (sequence-bounding-indices-test
439  (format t "~&/Function COUNT, COUNT-IF, COUNT-IF-NOT")
440  (assert (= (count #\a string :start 0 :end nil) 5))
441  (assert (= (count #\a string :start 0 :end 5) 5))
442  (assert (raises-error? (count #\a string :start 0 :end 6)))
443  (assert (raises-error? (count #\a string :start (opaque-identity -1) :end 5)))
444  (assert (raises-error? (count #\a string :start 4 :end 2)))
445  (assert (raises-error? (count #\a string :start 6 :end 9)))
446  (assert (= (count-if #'alpha-char-p string :start 0 :end nil) 5))
447  (assert (= (count-if #'alpha-char-p string :start 0 :end 5) 5))
448  (assert (raises-error?
449           (count-if #'alpha-char-p string :start 0 :end 6)))
450  (assert (raises-error?
451           (count-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
452  (assert (raises-error?
453           (count-if #'alpha-char-p string :start 4 :end 2)))
454  (assert (raises-error?
455           (count-if #'alpha-char-p string :start 6 :end 9)))
456  (assert (= (count-if-not #'alpha-char-p string :start 0 :end nil) 0))
457  (assert (= (count-if-not #'alpha-char-p string :start 0 :end 5) 0))
458  (assert (raises-error?
459           (count-if-not #'alpha-char-p string :start 0 :end 6)))
460  (assert (raises-error?
461           (count-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
462  (assert (raises-error?
463           (count-if-not #'alpha-char-p string :start 4 :end 2)))
464  (assert (raises-error?
465           (count-if-not #'alpha-char-p string :start 6 :end 9))))
466
467 ;;; Function FILL
468 (sequence-bounding-indices-test
469  (format t "~&/Function FILL")
470  (assert (string= (fill string #\b :start 0 :end 5) "bbbbb"))
471  (assert (string= (fill string #\c :start 0 :end nil) "ccccc"))
472  (assert (raises-error? (fill string #\d :start 0 :end 6)))
473  (assert (raises-error? (fill string #\d :start (opaque-identity -1) :end 5)))
474  (assert (raises-error? (fill string #\d :start 4 :end 2)))
475  (assert (raises-error? (fill string #\d :start 6 :end 9))))
476
477 ;;; Function FIND, FIND-IF, FIND-IF-NOT
478 (sequence-bounding-indices-test
479  (format t "~&/Function FIND, FIND-IF, FIND-IF-NOT")
480  (assert (char= (find #\a string :start 0 :end nil) #\a))
481  (assert (char= (find #\a string :start 0 :end 5) #\a))
482  (assert (raises-error? (find #\a string :start 0 :end 6)))
483  (assert (raises-error? (find #\a string :start (opaque-identity -1) :end 5)))
484  (assert (raises-error? (find #\a string :start 4 :end 2)))
485  (assert (raises-error? (find #\a string :start 6 :end 9)))
486  (assert (char= (find-if #'alpha-char-p string :start 0 :end nil) #\a))
487  (assert (char= (find-if #'alpha-char-p string :start 0 :end 5) #\a))
488  (assert (raises-error?
489           (find-if #'alpha-char-p string :start 0 :end 6)))
490  (assert (raises-error?
491           (find-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
492  (assert (raises-error?
493           (find-if #'alpha-char-p string :start 4 :end 2)))
494  (assert (raises-error?
495           (find-if #'alpha-char-p string :start 6 :end 9)))
496  (assert (eq (find-if-not #'alpha-char-p string :start 0 :end nil) nil))
497  (assert (eq (find-if-not #'alpha-char-p string :start 0 :end 5) nil))
498  (assert (raises-error?
499           (find-if-not #'alpha-char-p string :start 0 :end 6)))
500  (assert (raises-error?
501           (find-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
502  (assert (raises-error?
503           (find-if-not #'alpha-char-p string :start 4 :end 2)))
504  (assert (raises-error?
505           (find-if-not #'alpha-char-p string :start 6 :end 9))))
506
507 ;;; Function MISMATCH
508 (sequence-bounding-indices-test
509  (format t "~&/Function MISMATCH")
510  (assert (null (mismatch string "aaaaa" :start1 0 :end1 nil)))
511  (assert (= (mismatch "aaab" string :start2 0 :end2 4) 3))
512  (assert (raises-error? (mismatch "aaaaaa" string :start2 0 :end2 6)))
513  (assert (raises-error? (mismatch string "aaaaaa" :start1 (opaque-identity -1) :end1 5)))
514  (assert (raises-error? (mismatch string "" :start1 4 :end1 2)))
515  (assert (raises-error? (mismatch "aaaa" string :start2 6 :end2 9))))
516
517 ;;; Function PARSE-INTEGER
518 (sequence-bounding-indices-test
519  (format t "~&/Function PARSE-INTEGER")
520  (setf (fill-pointer string) 10)
521  (setf (subseq string 0 10) "1234567890")
522  (setf (fill-pointer string) 5)
523  (assert (= (parse-integer string :start 0 :end 5) 12345))
524  (assert (= (parse-integer string :start 0 :end nil) 12345))
525  (assert (raises-error? (parse-integer string :start 0 :end 6)))
526  (assert (raises-error? (parse-integer string :start (opaque-identity -1) :end 5)))
527  (assert (raises-error? (parse-integer string :start 4 :end 2)))
528  (assert (raises-error? (parse-integer string :start 6 :end 9))))
529
530 ;;; Function PARSE-NAMESTRING
531 (sequence-bounding-indices-test
532  (format t "~&/Function PARSE-NAMESTRING")
533  (setf (fill-pointer string) 10)
534  (setf (subseq string 0 10)
535        #-win32 "/dev/ /tmp"
536        #+win32 "C:/   NUL")
537  (setf (fill-pointer string) 5)
538  (assert (truename (parse-namestring string nil *default-pathname-defaults*
539                                      :start 0 :end 5)))
540  (assert (truename (parse-namestring string nil *default-pathname-defaults*
541                                      :start 0 :end nil)))
542  (assert (raises-error? (parse-namestring string nil
543                                           *default-pathname-defaults*
544                                           :start 0 :end 6)))
545  (assert (raises-error? (parse-namestring string nil
546                                           *default-pathname-defaults*
547                                           :start (opaque-identity -1) :end 5)))
548  (assert (raises-error? (parse-namestring string nil
549                                           *default-pathname-defaults*
550                                           :start 4 :end 2)))
551  (assert (raises-error? (parse-namestring string nil
552                                           *default-pathname-defaults*
553                                           :start 6 :end 9))))
554
555 ;;; Function POSITION, POSITION-IF, POSITION-IF-NOT
556 (sequence-bounding-indices-test
557  (format t "~&/Function POSITION, POSITION-IF, POSITION-IF-NOT")
558
559  (assert (= (position #\a string :start 0 :end nil) 0))
560  (assert (= (position #\a string :start 0 :end 5) 0))
561  (assert (raises-error? (position #\a string :start 0 :end 6)))
562  (assert (raises-error? (position #\a string :start (opaque-identity -1) :end 5)))
563  (assert (raises-error? (position #\a string :start 4 :end 2)))
564  (assert (raises-error? (position #\a string :start 6 :end 9)))
565  (assert (= (position-if #'alpha-char-p string :start 0 :end nil) 0))
566  (assert (= (position-if #'alpha-char-p string :start 0 :end 5) 0))
567  (assert (raises-error?
568           (position-if #'alpha-char-p string :start 0 :end 6)))
569  (assert (raises-error?
570           (position-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
571  (assert (raises-error?
572           (position-if #'alpha-char-p string :start 4 :end 2)))
573  (assert (raises-error?
574           (position-if #'alpha-char-p string :start 6 :end 9)))
575  (assert (eq (position-if-not #'alpha-char-p string :start 0 :end nil) nil))
576  (assert (eq (position-if-not #'alpha-char-p string :start 0 :end 5) nil))
577  (assert (raises-error?
578           (position-if-not #'alpha-char-p string :start 0 :end 6)))
579  (assert (raises-error?
580           (position-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
581  (assert (raises-error?
582           (position-if-not #'alpha-char-p string :start 4 :end 2)))
583  (assert (raises-error?
584           (position-if-not #'alpha-char-p string :start 6 :end 9))))
585
586 ;;; Function READ-FROM-STRING
587 (sequence-bounding-indices-test
588  (format t "~&/Function READ-FROM-STRING")
589  (setf (subseq string 0 5) "(a b)")
590  (assert (equal (read-from-string string nil nil :start 0 :end 5) '(a b)))
591  (assert (equal (read-from-string string nil nil :start 0 :end nil) '(a b)))
592  (assert (raises-error? (read-from-string string nil nil :start 0 :end 6)))
593  (assert (raises-error? (read-from-string string nil nil :start (opaque-identity -1) :end 5)))
594  (assert (raises-error? (read-from-string string nil nil :start 4 :end 2)))
595  (assert (raises-error? (read-from-string string nil nil :start 6 :end 9))))
596
597 ;;; Function REDUCE
598 (sequence-bounding-indices-test
599  (format t "~&/Function REDUCE")
600  (setf (subseq string 0 5) "abcde")
601  (assert (equal (reduce #'list* string :from-end t :start 0 :end nil)
602                 '(#\a #\b #\c #\d . #\e)))
603  (assert (equal (reduce #'list* string :from-end t :start 0 :end 5)
604                 '(#\a #\b #\c #\d . #\e)))
605  (assert (raises-error? (reduce #'list* string :start 0 :end 6)))
606  (assert (raises-error? (reduce #'list* string :start (opaque-identity -1) :end 5)))
607  (assert (raises-error? (reduce #'list* string :start 4 :end 2)))
608  (assert (raises-error? (reduce #'list* string :start 6 :end 9))))
609
610 ;;; Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, DELETE, DELETE-IF,
611 ;;; DELETE-IF-NOT
612 (sequence-bounding-indices-test
613  (format t "~&/Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, ...")
614  (assert (equal (remove #\a string :start 0 :end nil) ""))
615  (assert (equal (remove #\a string :start 0 :end 5) ""))
616  (assert (raises-error? (remove #\a string :start 0 :end 6)))
617  (assert (raises-error? (remove #\a string :start (opaque-identity -1) :end 5)))
618  (assert (raises-error? (remove #\a string :start 4 :end 2)))
619  (assert (raises-error? (remove #\a string :start 6 :end 9)))
620  (assert (equal (remove-if #'alpha-char-p string :start 0 :end nil) ""))
621  (assert (equal (remove-if #'alpha-char-p string :start 0 :end 5) ""))
622  (assert (raises-error?
623           (remove-if #'alpha-char-p string :start 0 :end 6)))
624  (assert (raises-error?
625           (remove-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
626  (assert (raises-error?
627           (remove-if #'alpha-char-p string :start 4 :end 2)))
628  (assert (raises-error?
629           (remove-if #'alpha-char-p string :start 6 :end 9)))
630  (assert (equal (remove-if-not #'alpha-char-p string :start 0 :end nil)
631                 "aaaaa"))
632  (assert (equal (remove-if-not #'alpha-char-p string :start 0 :end 5)
633                 "aaaaa"))
634  (assert (raises-error?
635           (remove-if-not #'alpha-char-p string :start 0 :end 6)))
636  (assert (raises-error?
637           (remove-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
638  (assert (raises-error?
639           (remove-if-not #'alpha-char-p string :start 4 :end 2)))
640  (assert (raises-error?
641           (remove-if-not #'alpha-char-p string :start 6 :end 9))))
642 (sequence-bounding-indices-test
643  (format t "~&/... DELETE, DELETE-IF, DELETE-IF-NOT")
644  (assert (equal (delete #\a string :start 0 :end nil) ""))
645  (reset)
646  (assert (equal (delete #\a string :start 0 :end 5) ""))
647  (reset)
648  (assert (raises-error? (delete #\a string :start 0 :end 6)))
649  (reset)
650  (assert (raises-error? (delete #\a string :start (opaque-identity -1) :end 5)))
651  (reset)
652  (assert (raises-error? (delete #\a string :start 4 :end 2)))
653  (reset)
654  (assert (raises-error? (delete #\a string :start 6 :end 9)))
655  (reset)
656  (assert (equal (delete-if #'alpha-char-p string :start 0 :end nil) ""))
657  (reset)
658  (assert (equal (delete-if #'alpha-char-p string :start 0 :end 5) ""))
659  (reset)
660  (assert (raises-error?
661           (delete-if #'alpha-char-p string :start 0 :end 6)))
662  (reset)
663  (assert (raises-error?
664           (delete-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
665  (reset)
666  (assert (raises-error?
667           (delete-if #'alpha-char-p string :start 4 :end 2)))
668  (reset)
669  (assert (raises-error?
670           (delete-if #'alpha-char-p string :start 6 :end 9)))
671  (reset)
672  (assert (equal (delete-if-not #'alpha-char-p string :start 0 :end nil)
673                 "aaaaa"))
674  (reset)
675  (assert (equal (delete-if-not #'alpha-char-p string :start 0 :end 5)
676                 "aaaaa"))
677  (reset)
678  (assert (raises-error?
679           (delete-if-not #'alpha-char-p string :start 0 :end 6)))
680  (reset)
681  (assert (raises-error?
682           (delete-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
683  (reset)
684  (assert (raises-error?
685           (delete-if-not #'alpha-char-p string :start 4 :end 2)))
686  (reset)
687  (assert (raises-error?
688           (delete-if-not #'alpha-char-p string :start 6 :end 9))))
689
690 ;;; Function REMOVE-DUPLICATES, DELETE-DUPLICATES
691 (sequence-bounding-indices-test
692  (format t "~&/Function REMOVE-DUPLICATES, DELETE-DUPLICATES")
693  (assert (string= (remove-duplicates string :start 0 :end 5) "a"))
694  (assert (string= (remove-duplicates string :start 0 :end nil) "a"))
695  (assert (raises-error? (remove-duplicates string :start 0 :end 6)))
696  (assert (raises-error? (remove-duplicates string :start (opaque-identity -1) :end 5)))
697  (assert (raises-error? (remove-duplicates string :start 4 :end 2)))
698  (assert (raises-error? (remove-duplicates string :start 6 :end 9)))
699  (assert (string= (delete-duplicates string :start 0 :end 5) "a"))
700  (reset)
701  (assert (string= (delete-duplicates string :start 0 :end nil) "a"))
702  (reset)
703  (assert (raises-error? (delete-duplicates string :start 0 :end 6)))
704  (reset)
705  (assert (raises-error? (delete-duplicates string :start (opaque-identity -1) :end 5)))
706  (reset)
707  (assert (raises-error? (delete-duplicates string :start 4 :end 2)))
708  (reset)
709  (assert (raises-error? (delete-duplicates string :start 6 :end 9))))
710
711 ;;; Function REPLACE
712 (sequence-bounding-indices-test
713  (format t "~&/Function REPLACE")
714  (assert (string= (replace string "bbbbb" :start1 0 :end1 5) "bbbbb"))
715  (assert (string= (replace (copy-seq "ccccc")
716                            string
717                            :start2 0 :end2 nil) "bbbbb"))
718  (assert (raises-error? (replace string "ccccc" :start1 0 :end1 6)))
719  (assert (raises-error? (replace string "ccccc" :start2 (opaque-identity -1) :end2 5)))
720  (assert (raises-error? (replace string "ccccc" :start1 4 :end1 2)))
721  (assert (raises-error? (replace string "ccccc" :start1 6 :end1 9))))
722
723 ;;; Function SEARCH
724 (sequence-bounding-indices-test
725  (format t "~&/Function SEARCH")
726  (assert (= (search "aa" string :start2 0 :end2 5) 0))
727  (assert (null (search string "aa" :start1 0 :end2 nil)))
728  (assert (raises-error? (search "aa" string :start2 0 :end2 6)))
729  (assert (raises-error? (search "aa" string :start2 (opaque-identity -1) :end2 5)))
730  (assert (raises-error? (search "aa" string :start2 4 :end2 2)))
731  (assert (raises-error? (search "aa" string :start2 6 :end2 9))))
732
733 ;;; Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE,
734 ;;; NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE
735 (defmacro string-case-frob (fn)
736   `(progn
737     (assert (raises-error? (,fn string :start 0 :end 6)))
738     (assert (raises-error? (,fn string :start (opaque-identity -1) :end 5)))
739     (assert (raises-error? (,fn string :start 4 :end 2)))
740     (assert (raises-error? (,fn string :start 6 :end 9)))))
741
742 (sequence-bounding-indices-test
743  (format t "~&/Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE, ...")
744  (string-case-frob string-upcase)
745  (string-case-frob string-downcase)
746  (string-case-frob string-capitalize)
747  (format t "~&/... NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE")
748  (string-case-frob nstring-upcase)
749  (string-case-frob nstring-downcase)
750  (string-case-frob nstring-capitalize))
751
752 ;;; Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=,
753 ;;; STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, STRING-GREATERP,
754 ;;; STRING-NOT-GREATERP, STRING-NOT-LESSP
755 (defmacro string-predicate-frob (fn)
756   `(progn
757     (,fn string "abcde" :start1 0 :end1 5)
758     (,fn "fghij" string :start2 0 :end2 nil)
759     (assert (raises-error? (,fn string "klmno"
760                                 :start1 0 :end1 6)))
761     (assert (raises-error? (,fn "pqrst" string
762                                 :start2 (opaque-identity -1) :end2 5)))
763     (assert (raises-error? (,fn "uvwxy" string
764                                 :start1 4 :end1 2)))
765     (assert (raises-error? (,fn string "z" :start2 6 :end2 9)))))
766 (sequence-bounding-indices-test
767  (format t "~&/Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=, ...")
768  (string-predicate-frob string=)
769  (string-predicate-frob string/=)
770  (string-predicate-frob string<)
771  (string-predicate-frob string>)
772  (string-predicate-frob string<=)
773  (string-predicate-frob string>=))
774 (sequence-bounding-indices-test
775  (format t "~&/... STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, ...")
776  (string-predicate-frob string-equal)
777  (string-predicate-frob string-not-equal)
778  (string-predicate-frob string-lessp))
779 (sequence-bounding-indices-test
780  (format t "~&/... STRING-GREATERP, STRING-NOT-GREATERP, STRING-NOT-LESSP")
781  (string-predicate-frob string-greaterp)
782  (string-predicate-frob string-not-greaterp)
783  (string-predicate-frob string-not-lessp))
784
785 ;;; Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT,
786 ;;; NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
787 (sequence-bounding-indices-test
788  (format t "~&/Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT, ...")
789  (assert (string= (substitute #\b #\a string :start 0 :end 5) "bbbbb"))
790  (assert (string= (substitute #\c #\a string :start 0 :end nil)
791                   "ccccc"))
792  (assert (raises-error? (substitute #\b #\a string :start 0 :end 6)))
793  (assert (raises-error? (substitute #\b #\a string :start (opaque-identity -1) :end 5)))
794  (assert (raises-error? (substitute #\b #\a string :start 4 :end 2)))
795  (assert (raises-error? (substitute #\b #\a string :start 6 :end 9)))
796  (assert (string= (substitute-if #\b #'alpha-char-p string
797                                  :start 0 :end 5)
798                   "bbbbb"))
799  (assert (string= (substitute-if #\c #'alpha-char-p string
800                                  :start 0 :end nil)
801                   "ccccc"))
802  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
803                                        :start 0 :end 6)))
804  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
805                                        :start (opaque-identity -1) :end 5)))
806  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
807                                        :start 4 :end 2)))
808  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
809                                        :start 6 :end 9)))
810  (assert (string= (substitute-if-not #\b #'alpha-char-p string
811                                      :start 0 :end 5)
812                   "aaaaa"))
813  (assert (string= (substitute-if-not #\c #'alpha-char-p string
814                                      :start 0 :end nil)
815                   "aaaaa"))
816  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
817                                            :start 0 :end 6)))
818  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
819                                            :start (opaque-identity -1) :end 5)))
820  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
821                                            :start 4 :end 2)))
822  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
823                                            :start 6 :end 9))))
824 (sequence-bounding-indices-test
825  (format t "~&/... NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT")
826  (assert (string= (nsubstitute #\b #\a string :start 0 :end 5) "bbbbb"))
827  (reset)
828  (assert (string= (nsubstitute #\c #\a string :start 0 :end nil)
829                   "ccccc"))
830  (reset)
831  (assert (raises-error? (nsubstitute #\b #\a string :start 0 :end 6)))
832  (reset)
833  (assert (raises-error? (nsubstitute #\b #\a string :start (opaque-identity -1) :end 5)))
834  (reset)
835  (assert (raises-error? (nsubstitute #\b #\a string :start 4 :end 2)))
836  (reset)
837  (assert (raises-error? (nsubstitute #\b #\a string :start 6 :end 9)))
838  (reset)
839  (assert (string= (nsubstitute-if #\b #'alpha-char-p string
840                                   :start 0 :end 5)
841                   "bbbbb"))
842  (reset)
843  (assert (string= (nsubstitute-if #\c #'alpha-char-p string
844                                   :start 0 :end nil)
845                   "ccccc"))
846  (reset)
847  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
848                                         :start 0 :end 6)))
849  (reset)
850  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
851                                         :start (opaque-identity -1) :end 5)))
852  (reset)
853  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
854                                         :start 4 :end 2)))
855  (reset)
856  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
857                                         :start 6 :end 9)))
858  (reset)
859  (assert (string= (nsubstitute-if-not #\b #'alpha-char-p string
860                                       :start 0 :end 5)
861                   "aaaaa"))
862  (reset)
863  (assert (string= (nsubstitute-if-not #\c #'alpha-char-p string
864                                       :start 0 :end nil)
865                   "aaaaa"))
866  (reset)
867  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
868                                             :start 0 :end 6)))
869  (reset)
870  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
871                                             :start (opaque-identity -1) :end 5)))
872  (reset)
873  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
874                                             :start 4 :end 2)))
875  (reset)
876  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
877                                             :start 6 :end 9))))
878 ;;; Function WRITE-STRING, WRITE-LINE
879 (sequence-bounding-indices-test
880  (format t "~&/Function WRITE-STRING, WRITE-LINE")
881  (write-string string *standard-output* :start 0 :end 5)
882  (write-string string *standard-output* :start 0 :end nil)
883  (assert (raises-error? (write-string string *standard-output*
884                                       :start 0 :end 6)))
885  (assert (raises-error? (write-string string *standard-output*
886                                       :start (opaque-identity -1) :end 5)))
887  (assert (raises-error? (write-string string *standard-output*
888                                       :start 4 :end 2)))
889  (assert (raises-error? (write-string string *standard-output*
890                                       :start 6 :end 9)))
891  (write-line string *standard-output* :start 0 :end 5)
892  (write-line string *standard-output* :start 0 :end nil)
893  (assert (raises-error? (write-line string *standard-output*
894                                       :start 0 :end 6)))
895  (assert (raises-error? (write-line string *standard-output*
896                                       :start (opaque-identity -1) :end 5)))
897  (assert (raises-error? (write-line string *standard-output*
898                                       :start 4 :end 2)))
899  (assert (raises-error? (write-line string *standard-output*
900                                       :start 6 :end 9))))
901
902 ;;; Macro WITH-INPUT-FROM-STRING
903 (sequence-bounding-indices-test
904  (format t "~&/Macro WITH-INPUT-FROM-STRING")
905  (with-input-from-string (s string :start 0 :end 5)
906    (assert (char= (read-char s) #\a)))
907  (with-input-from-string (s string :start 0 :end nil)
908    (assert (char= (read-char s) #\a)))
909  (assert (raises-error?
910           (with-input-from-string (s string :start 0 :end 6)
911             (read-char s))))
912  (assert (raises-error?
913           (with-input-from-string (s string :start (opaque-identity -1) :end 5)
914             (read-char s))))
915  (assert (raises-error?
916           (with-input-from-string (s string :start 4 :end 2)
917             (read-char s))))
918  (assert (raises-error?
919           (with-input-from-string (s string :start 6 :end 9)
920             (read-char s)))))
921 \f
922 ;;; testing bit-bashing according to _The Practice of Programming_
923 (defun fill-bytes-for-testing (bitsize)
924   "Return a list of 'bytes' of type (MOD BITSIZE)."
925   (remove-duplicates (list 0
926                            (1- (ash 1 (1- bitsize)))
927                            (ash 1 (1- bitsize))
928                            (1- (ash 1 bitsize)))))
929
930 (defun fill-with-known-value (value size &rest vectors)
931   (dolist (vec vectors)
932     (dotimes (i size)
933       (setf (aref vec i) value))))
934
935 (defun collect-fill-amounts (n-power)
936   (remove-duplicates
937    (loop for i from 0 upto n-power
938          collect (1- (expt 2 i))
939          collect (expt 2 i)
940          collect (1+ (expt 2 i)))))
941
942 (defun test-fill-bashing (bitsize padding-amount n-power)
943   (let* ((size (+ (* padding-amount 2) (expt 2 n-power) (* padding-amount 2)))
944          (standard (make-array size :element-type `(unsigned-byte ,bitsize)))
945          (bashed (make-array size :element-type `(unsigned-byte ,bitsize)))
946          (fill-amounts (collect-fill-amounts n-power))
947          (bash-function (intern (format nil "UB~A-BASH-FILL" bitsize)
948                                 (find-package "SB-KERNEL"))))
949     (format t "~&/Function ~A..." bash-function)
950     (loop for offset from padding-amount below (* 2 padding-amount) do
951           (dolist (c (fill-bytes-for-testing bitsize))
952             (dolist (n fill-amounts)
953               (fill-with-known-value (mod (lognot c) (ash 1 bitsize)) n
954                                      standard bashed)
955               ;; fill vectors
956               ;; a) the standard slow way
957               (fill standard c :start offset :end (+ offset n))
958               ;; b) the blazingly fast way
959               (let ((value (loop for i from 0 by bitsize
960                                  until (= i sb-vm:n-word-bits)
961                                  sum (ash c i))))
962                 (funcall bash-function value bashed offset n))
963               ;; check for errors
964               (when (mismatch standard bashed)
965                 (format t "Test with offset ~A, fill ~A and length ~A failed.~%"
966                         offset c n)
967                 (format t "Mismatch: ~A ~A~%"
968                         (subseq standard 0 (+ offset n 1))
969                         (subseq bashed 0 (+ offset n 1)))
970                 (return-from test-fill-bashing nil))))
971           finally (return t))))
972
973 (defun test-copy-bashing (bitsize padding-amount n-power)
974   (let* ((size (+ (* padding-amount 2) (expt 2 n-power) (* padding-amount 2)))
975          (standard-dst (make-array size :element-type `(unsigned-byte ,bitsize)))
976          (bashed-dst (make-array size :element-type `(unsigned-byte ,bitsize)))
977          (source (make-array size :element-type `(unsigned-byte ,bitsize)))
978          (fill-amounts (collect-fill-amounts n-power))
979          (bash-function (intern (format nil "UB~A-BASH-COPY" bitsize)
980                                 (find-package "SB-KERNEL"))))
981     (format t "~&/Function ~A..." bash-function)
982     (do ((source-offset padding-amount (1+ source-offset)))
983         ((>= source-offset (* padding-amount 2))
984          ;; success!
985          t)
986      (do ((target-offset padding-amount (1+ target-offset)))
987          ((>= target-offset (* padding-amount 2)))
988        (dolist (c (fill-bytes-for-testing bitsize))
989          (dolist (n fill-amounts)
990            (fill-with-known-value (mod (lognot c) (ash 1 bitsize)) size
991                                   source standard-dst bashed-dst)
992            ;; fill with test data
993            (fill source c :start source-offset :end (+ source-offset n))
994            ;; copy filled test data to test vectors
995            ;; a) the slow way
996            (replace standard-dst source
997                     :start1 target-offset :end1 (+ target-offset n)
998                     :start2 source-offset :end2 (+ source-offset n))
999            ;; b) the blazingly fast way
1000            (funcall bash-function source source-offset
1001                     bashed-dst target-offset n)
1002            ;; check for errors
1003            (when (mismatch standard-dst bashed-dst)
1004              (format t "Test with target-offset ~A, source-offset ~A, fill ~A, and length ~A failed.~%"
1005                      target-offset source-offset c n)
1006              (format t "Mismatch:~% correct ~A~% actual  ~A~%"
1007                      standard-dst
1008                      bashed-dst)
1009              (return-from test-copy-bashing nil))))))))
1010
1011 ;; Too slow for the interpreter
1012 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
1013 (loop for i = 1 then (* i 2) do
1014      ;; the bare '32' here is fairly arbitrary; '8' provides a good
1015      ;; range of lengths over which to fill and copy, which should tease
1016      ;; out most errors in the code (if any exist).  (It also makes this
1017      ;; part of the test suite finish reasonably quickly.)
1018      (assert (test-fill-bashing i 32 8))
1019      (assert (test-copy-bashing i 32 8))
1020      until (= i sb-vm:n-word-bits))
1021 \f
1022 ;;; success