1.0.12.5: WITH-ARRAY-DATA touchups
[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.
381 ;;;
382 ;;; This used to be SAFETY 3 only, but bypassing these checks with
383 ;;; above-zero speed when SPEED > SAFETY is not The SBCL Way.
384 ;;;
385 ;;; KLUDGE: not all in one big form because that causes SBCL to spend
386 ;;; an absolute age trying to compile it.
387 (defmacro sequence-bounding-indices-test (&body body)
388   `(progn
389      (locally
390     ;; See Issues 332 [and 333(!)] in the CLHS
391     (declare (optimize (speed 3) (safety 1)))
392     (let ((string (make-array 10
393                               :fill-pointer 5
394                               :initial-element #\a
395                               :element-type 'base-char)))
396         ,(car body)
397         (format t "... BASE-CHAR")
398         (finish-output)
399         (flet ((reset ()
400                  (setf (fill-pointer string) 10)
401                  (fill string #\a)
402                  (setf (fill-pointer string) 5)))
403           (declare (ignorable #'reset))
404           ,@(cdr body))))
405     (locally
406       ;; See Issues 332 [and 333(!)] in the CLHS
407       (declare (optimize (speed 3) (safety 1)))
408       (let ((string (make-array 10
409                                 :fill-pointer 5
410                                 :initial-element #\a
411                                 :element-type 'character)))
412         ,(car body)
413         (format t "... CHARACTER")
414         (finish-output)
415       (flet ((reset ()
416                (setf (fill-pointer string) 10)
417                (fill string #\a)
418                (setf (fill-pointer string) 5)))
419         (declare (ignorable #'reset))
420           ,@(cdr body))))))
421
422 (declaim (notinline opaque-identity))
423 (defun opaque-identity (x) x)
424 ;;; Accessor SUBSEQ
425 (sequence-bounding-indices-test
426  (format t "~&/Accessor SUBSEQ")
427  (assert (string= (subseq string 0 5) "aaaaa"))
428  (assert (raises-error? (subseq string 0 6)))
429  (assert (raises-error? (subseq string (opaque-identity -1) 5)))
430  (assert (raises-error? (subseq string 4 2)))
431  (assert (raises-error? (subseq string 6)))
432  (assert (string= (setf (subseq string 0 5) "abcde") "abcde"))
433  (assert (string= (subseq string 0 5) "abcde"))
434  (reset)
435  (assert (raises-error? (setf (subseq string 0 6) "abcdef")))
436  (assert (raises-error? (setf (subseq string (opaque-identity -1) 5) "abcdef")))
437  (assert (raises-error? (setf (subseq string 4 2) "")))
438  (assert (raises-error? (setf (subseq string 6) "ghij"))))
439
440 ;;; Function COUNT, COUNT-IF, COUNT-IF-NOT
441 (sequence-bounding-indices-test
442  (format t "~&/Function COUNT, COUNT-IF, COUNT-IF-NOT")
443  (assert (= (count #\a string :start 0 :end nil) 5))
444  (assert (= (count #\a string :start 0 :end 5) 5))
445  (assert (raises-error? (count #\a string :start 0 :end 6)))
446  (assert (raises-error? (count #\a string :start (opaque-identity -1) :end 5)))
447  (assert (raises-error? (count #\a string :start 4 :end 2)))
448  (assert (raises-error? (count #\a string :start 6 :end 9)))
449  (assert (= (count-if #'alpha-char-p string :start 0 :end nil) 5))
450  (assert (= (count-if #'alpha-char-p string :start 0 :end 5) 5))
451  (assert (raises-error?
452           (count-if #'alpha-char-p string :start 0 :end 6)))
453  (assert (raises-error?
454           (count-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
455  (assert (raises-error?
456           (count-if #'alpha-char-p string :start 4 :end 2)))
457  (assert (raises-error?
458           (count-if #'alpha-char-p string :start 6 :end 9)))
459  (assert (= (count-if-not #'alpha-char-p string :start 0 :end nil) 0))
460  (assert (= (count-if-not #'alpha-char-p string :start 0 :end 5) 0))
461  (assert (raises-error?
462           (count-if-not #'alpha-char-p string :start 0 :end 6)))
463  (assert (raises-error?
464           (count-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
465  (assert (raises-error?
466           (count-if-not #'alpha-char-p string :start 4 :end 2)))
467  (assert (raises-error?
468           (count-if-not #'alpha-char-p string :start 6 :end 9))))
469
470 ;;; Function FILL
471 (sequence-bounding-indices-test
472  (format t "~&/Function FILL")
473  (assert (string= (fill string #\b :start 0 :end 5) "bbbbb"))
474  (assert (string= (fill string #\c :start 0 :end nil) "ccccc"))
475  (assert (raises-error? (fill string #\d :start 0 :end 6)))
476  (assert (raises-error? (fill string #\d :start (opaque-identity -1) :end 5)))
477  (assert (raises-error? (fill string #\d :start 4 :end 2)))
478  (assert (raises-error? (fill string #\d :start 6 :end 9))))
479
480 ;;; Function FIND, FIND-IF, FIND-IF-NOT
481 (sequence-bounding-indices-test
482  (format t "~&/Function FIND, FIND-IF, FIND-IF-NOT")
483  (assert (char= (find #\a string :start 0 :end nil) #\a))
484  (assert (char= (find #\a string :start 0 :end 5) #\a))
485  (assert (raises-error? (find #\a string :start 0 :end 6)))
486  (assert (raises-error? (find #\a string :start (opaque-identity -1) :end 5)))
487  (assert (raises-error? (find #\a string :start 4 :end 2)))
488  (assert (raises-error? (find #\a string :start 6 :end 9)))
489  (assert (char= (find-if #'alpha-char-p string :start 0 :end nil) #\a))
490  (assert (char= (find-if #'alpha-char-p string :start 0 :end 5) #\a))
491  (assert (raises-error?
492           (find-if #'alpha-char-p string :start 0 :end 6)))
493  (assert (raises-error?
494           (find-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
495  (assert (raises-error?
496           (find-if #'alpha-char-p string :start 4 :end 2)))
497  (assert (raises-error?
498           (find-if #'alpha-char-p string :start 6 :end 9)))
499  (assert (eq (find-if-not #'alpha-char-p string :start 0 :end nil) nil))
500  (assert (eq (find-if-not #'alpha-char-p string :start 0 :end 5) nil))
501  (assert (raises-error?
502           (find-if-not #'alpha-char-p string :start 0 :end 6)))
503  (assert (raises-error?
504           (find-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
505  (assert (raises-error?
506           (find-if-not #'alpha-char-p string :start 4 :end 2)))
507  (assert (raises-error?
508           (find-if-not #'alpha-char-p string :start 6 :end 9))))
509
510 ;;; Function MISMATCH
511 (sequence-bounding-indices-test
512  (format t "~&/Function MISMATCH")
513  (assert (null (mismatch string "aaaaa" :start1 0 :end1 nil)))
514  (assert (= (mismatch "aaab" string :start2 0 :end2 4) 3))
515  (assert (raises-error? (mismatch "aaaaaa" string :start2 0 :end2 6)))
516  (assert (raises-error? (mismatch string "aaaaaa" :start1 (opaque-identity -1) :end1 5)))
517  (assert (raises-error? (mismatch string "" :start1 4 :end1 2)))
518  (assert (raises-error? (mismatch "aaaa" string :start2 6 :end2 9))))
519
520 ;;; Function PARSE-INTEGER
521 (sequence-bounding-indices-test
522  (format t "~&/Function PARSE-INTEGER")
523  (setf (fill-pointer string) 10)
524  (setf (subseq string 0 10) "1234567890")
525  (setf (fill-pointer string) 5)
526  (assert (= (parse-integer string :start 0 :end 5) 12345))
527  (assert (= (parse-integer string :start 0 :end nil) 12345))
528  (assert (raises-error? (parse-integer string :start 0 :end 6)))
529  (assert (raises-error? (parse-integer string :start (opaque-identity -1) :end 5)))
530  (assert (raises-error? (parse-integer string :start 4 :end 2)))
531  (assert (raises-error? (parse-integer string :start 6 :end 9))))
532
533 ;;; Function PARSE-NAMESTRING
534 (sequence-bounding-indices-test
535  (format t "~&/Function PARSE-NAMESTRING")
536  (setf (fill-pointer string) 10)
537  (setf (subseq string 0 10)
538        #-win32 "/dev/ /tmp"
539        #+win32 "C:/   NUL")
540  (setf (fill-pointer string) 5)
541  (assert (truename (parse-namestring string nil *default-pathname-defaults*
542                                      :start 0 :end 5)))
543  (assert (truename (parse-namestring string nil *default-pathname-defaults*
544                                      :start 0 :end nil)))
545  (assert (raises-error? (parse-namestring string nil
546                                           *default-pathname-defaults*
547                                           :start 0 :end 6)))
548  (assert (raises-error? (parse-namestring string nil
549                                           *default-pathname-defaults*
550                                           :start (opaque-identity -1) :end 5)))
551  (assert (raises-error? (parse-namestring string nil
552                                           *default-pathname-defaults*
553                                           :start 4 :end 2)))
554  (assert (raises-error? (parse-namestring string nil
555                                           *default-pathname-defaults*
556                                           :start 6 :end 9))))
557
558 ;;; Function POSITION, POSITION-IF, POSITION-IF-NOT
559 (sequence-bounding-indices-test
560  (format t "~&/Function POSITION, POSITION-IF, POSITION-IF-NOT")
561
562  (assert (= (position #\a string :start 0 :end nil) 0))
563  (assert (= (position #\a string :start 0 :end 5) 0))
564  (assert (raises-error? (position #\a string :start 0 :end 6)))
565  (assert (raises-error? (position #\a string :start (opaque-identity -1) :end 5)))
566  (assert (raises-error? (position #\a string :start 4 :end 2)))
567  (assert (raises-error? (position #\a string :start 6 :end 9)))
568  (assert (= (position-if #'alpha-char-p string :start 0 :end nil) 0))
569  (assert (= (position-if #'alpha-char-p string :start 0 :end 5) 0))
570  (assert (raises-error?
571           (position-if #'alpha-char-p string :start 0 :end 6)))
572  (assert (raises-error?
573           (position-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
574  (assert (raises-error?
575           (position-if #'alpha-char-p string :start 4 :end 2)))
576  (assert (raises-error?
577           (position-if #'alpha-char-p string :start 6 :end 9)))
578  (assert (eq (position-if-not #'alpha-char-p string :start 0 :end nil) nil))
579  (assert (eq (position-if-not #'alpha-char-p string :start 0 :end 5) nil))
580  (assert (raises-error?
581           (position-if-not #'alpha-char-p string :start 0 :end 6)))
582  (assert (raises-error?
583           (position-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
584  (assert (raises-error?
585           (position-if-not #'alpha-char-p string :start 4 :end 2)))
586  (assert (raises-error?
587           (position-if-not #'alpha-char-p string :start 6 :end 9))))
588
589 ;;; Function READ-FROM-STRING
590 (sequence-bounding-indices-test
591  (format t "~&/Function READ-FROM-STRING")
592  (setf (subseq string 0 5) "(a b)")
593  (assert (equal (read-from-string string nil nil :start 0 :end 5) '(a b)))
594  (assert (equal (read-from-string string nil nil :start 0 :end nil) '(a b)))
595  (assert (raises-error? (read-from-string string nil nil :start 0 :end 6)))
596  (assert (raises-error? (read-from-string string nil nil :start (opaque-identity -1) :end 5)))
597  (assert (raises-error? (read-from-string string nil nil :start 4 :end 2)))
598  (assert (raises-error? (read-from-string string nil nil :start 6 :end 9))))
599
600 ;;; Function REDUCE
601 (sequence-bounding-indices-test
602  (format t "~&/Function REDUCE")
603  (setf (subseq string 0 5) "abcde")
604  (assert (equal (reduce #'list* string :from-end t :start 0 :end nil)
605                 '(#\a #\b #\c #\d . #\e)))
606  (assert (equal (reduce #'list* string :from-end t :start 0 :end 5)
607                 '(#\a #\b #\c #\d . #\e)))
608  (assert (raises-error? (reduce #'list* string :start 0 :end 6)))
609  (assert (raises-error? (reduce #'list* string :start (opaque-identity -1) :end 5)))
610  (assert (raises-error? (reduce #'list* string :start 4 :end 2)))
611  (assert (raises-error? (reduce #'list* string :start 6 :end 9))))
612
613 ;;; Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, DELETE, DELETE-IF,
614 ;;; DELETE-IF-NOT
615 (sequence-bounding-indices-test
616  (format t "~&/Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, ...")
617  (assert (equal (remove #\a string :start 0 :end nil) ""))
618  (assert (equal (remove #\a string :start 0 :end 5) ""))
619  (assert (raises-error? (remove #\a string :start 0 :end 6)))
620  (assert (raises-error? (remove #\a string :start (opaque-identity -1) :end 5)))
621  (assert (raises-error? (remove #\a string :start 4 :end 2)))
622  (assert (raises-error? (remove #\a string :start 6 :end 9)))
623  (assert (equal (remove-if #'alpha-char-p string :start 0 :end nil) ""))
624  (assert (equal (remove-if #'alpha-char-p string :start 0 :end 5) ""))
625  (assert (raises-error?
626           (remove-if #'alpha-char-p string :start 0 :end 6)))
627  (assert (raises-error?
628           (remove-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
629  (assert (raises-error?
630           (remove-if #'alpha-char-p string :start 4 :end 2)))
631  (assert (raises-error?
632           (remove-if #'alpha-char-p string :start 6 :end 9)))
633  (assert (equal (remove-if-not #'alpha-char-p string :start 0 :end nil)
634                 "aaaaa"))
635  (assert (equal (remove-if-not #'alpha-char-p string :start 0 :end 5)
636                 "aaaaa"))
637  (assert (raises-error?
638           (remove-if-not #'alpha-char-p string :start 0 :end 6)))
639  (assert (raises-error?
640           (remove-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
641  (assert (raises-error?
642           (remove-if-not #'alpha-char-p string :start 4 :end 2)))
643  (assert (raises-error?
644           (remove-if-not #'alpha-char-p string :start 6 :end 9))))
645 (sequence-bounding-indices-test
646  (format t "~&/... DELETE, DELETE-IF, DELETE-IF-NOT")
647  (assert (equal (delete #\a string :start 0 :end nil) ""))
648  (reset)
649  (assert (equal (delete #\a string :start 0 :end 5) ""))
650  (reset)
651  (assert (raises-error? (delete #\a string :start 0 :end 6)))
652  (reset)
653  (assert (raises-error? (delete #\a string :start (opaque-identity -1) :end 5)))
654  (reset)
655  (assert (raises-error? (delete #\a string :start 4 :end 2)))
656  (reset)
657  (assert (raises-error? (delete #\a string :start 6 :end 9)))
658  (reset)
659  (assert (equal (delete-if #'alpha-char-p string :start 0 :end nil) ""))
660  (reset)
661  (assert (equal (delete-if #'alpha-char-p string :start 0 :end 5) ""))
662  (reset)
663  (assert (raises-error?
664           (delete-if #'alpha-char-p string :start 0 :end 6)))
665  (reset)
666  (assert (raises-error?
667           (delete-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
668  (reset)
669  (assert (raises-error?
670           (delete-if #'alpha-char-p string :start 4 :end 2)))
671  (reset)
672  (assert (raises-error?
673           (delete-if #'alpha-char-p string :start 6 :end 9)))
674  (reset)
675  (assert (equal (delete-if-not #'alpha-char-p string :start 0 :end nil)
676                 "aaaaa"))
677  (reset)
678  (assert (equal (delete-if-not #'alpha-char-p string :start 0 :end 5)
679                 "aaaaa"))
680  (reset)
681  (assert (raises-error?
682           (delete-if-not #'alpha-char-p string :start 0 :end 6)))
683  (reset)
684  (assert (raises-error?
685           (delete-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
686  (reset)
687  (assert (raises-error?
688           (delete-if-not #'alpha-char-p string :start 4 :end 2)))
689  (reset)
690  (assert (raises-error?
691           (delete-if-not #'alpha-char-p string :start 6 :end 9))))
692
693 ;;; Function REMOVE-DUPLICATES, DELETE-DUPLICATES
694 (sequence-bounding-indices-test
695  (format t "~&/Function REMOVE-DUPLICATES, DELETE-DUPLICATES")
696  (assert (string= (remove-duplicates string :start 0 :end 5) "a"))
697  (assert (string= (remove-duplicates string :start 0 :end nil) "a"))
698  (assert (raises-error? (remove-duplicates string :start 0 :end 6)))
699  (assert (raises-error? (remove-duplicates string :start (opaque-identity -1) :end 5)))
700  (assert (raises-error? (remove-duplicates string :start 4 :end 2)))
701  (assert (raises-error? (remove-duplicates string :start 6 :end 9)))
702  (assert (string= (delete-duplicates string :start 0 :end 5) "a"))
703  (reset)
704  (assert (string= (delete-duplicates string :start 0 :end nil) "a"))
705  (reset)
706  (assert (raises-error? (delete-duplicates string :start 0 :end 6)))
707  (reset)
708  (assert (raises-error? (delete-duplicates string :start (opaque-identity -1) :end 5)))
709  (reset)
710  (assert (raises-error? (delete-duplicates string :start 4 :end 2)))
711  (reset)
712  (assert (raises-error? (delete-duplicates string :start 6 :end 9))))
713
714 ;;; Function REPLACE
715 (sequence-bounding-indices-test
716  (format t "~&/Function REPLACE")
717  (assert (string= (replace string "bbbbb" :start1 0 :end1 5) "bbbbb"))
718  (assert (string= (replace (copy-seq "ccccc")
719                            string
720                            :start2 0 :end2 nil) "bbbbb"))
721  (assert (raises-error? (replace string "ccccc" :start1 0 :end1 6)))
722  (assert (raises-error? (replace string "ccccc" :start2 (opaque-identity -1) :end2 5)))
723  (assert (raises-error? (replace string "ccccc" :start1 4 :end1 2)))
724  (assert (raises-error? (replace string "ccccc" :start1 6 :end1 9))))
725
726 ;;; Function SEARCH
727 (sequence-bounding-indices-test
728  (format t "~&/Function SEARCH")
729  (assert (= (search "aa" string :start2 0 :end2 5) 0))
730  (assert (null (search string "aa" :start1 0 :end2 nil)))
731  (assert (raises-error? (search "aa" string :start2 0 :end2 6)))
732  (assert (raises-error? (search "aa" string :start2 (opaque-identity -1) :end2 5)))
733  (assert (raises-error? (search "aa" string :start2 4 :end2 2)))
734  (assert (raises-error? (search "aa" string :start2 6 :end2 9))))
735
736 ;;; Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE,
737 ;;; NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE
738 (defmacro string-case-frob (fn)
739   `(progn
740     (assert (raises-error? (,fn string :start 0 :end 6)))
741     (assert (raises-error? (,fn string :start (opaque-identity -1) :end 5)))
742     (assert (raises-error? (,fn string :start 4 :end 2)))
743     (assert (raises-error? (,fn string :start 6 :end 9)))))
744
745 (sequence-bounding-indices-test
746  (format t "~&/Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE, ...")
747  (string-case-frob string-upcase)
748  (string-case-frob string-downcase)
749  (string-case-frob string-capitalize)
750  (format t "~&/... NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE")
751  (string-case-frob nstring-upcase)
752  (string-case-frob nstring-downcase)
753  (string-case-frob nstring-capitalize))
754
755 ;;; Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=,
756 ;;; STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, STRING-GREATERP,
757 ;;; STRING-NOT-GREATERP, STRING-NOT-LESSP
758 (defmacro string-predicate-frob (fn)
759   `(progn
760     (,fn string "abcde" :start1 0 :end1 5)
761     (,fn "fghij" string :start2 0 :end2 nil)
762     (assert (raises-error? (,fn string "klmno"
763                                 :start1 0 :end1 6)))
764     (assert (raises-error? (,fn "pqrst" string
765                                 :start2 (opaque-identity -1) :end2 5)))
766     (assert (raises-error? (,fn "uvwxy" string
767                                 :start1 4 :end1 2)))
768     (assert (raises-error? (,fn string "z" :start2 6 :end2 9)))))
769 (sequence-bounding-indices-test
770  (format t "~&/Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=, ...")
771  (string-predicate-frob string=)
772  (string-predicate-frob string/=)
773  (string-predicate-frob string<)
774  (string-predicate-frob string>)
775  (string-predicate-frob string<=)
776  (string-predicate-frob string>=))
777 (sequence-bounding-indices-test
778  (format t "~&/... STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, ...")
779  (string-predicate-frob string-equal)
780  (string-predicate-frob string-not-equal)
781  (string-predicate-frob string-lessp))
782 (sequence-bounding-indices-test
783  (format t "~&/... STRING-GREATERP, STRING-NOT-GREATERP, STRING-NOT-LESSP")
784  (string-predicate-frob string-greaterp)
785  (string-predicate-frob string-not-greaterp)
786  (string-predicate-frob string-not-lessp))
787
788 ;;; Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT,
789 ;;; NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
790 (sequence-bounding-indices-test
791  (format t "~&/Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT, ...")
792  (assert (string= (substitute #\b #\a string :start 0 :end 5) "bbbbb"))
793  (assert (string= (substitute #\c #\a string :start 0 :end nil)
794                   "ccccc"))
795  (assert (raises-error? (substitute #\b #\a string :start 0 :end 6)))
796  (assert (raises-error? (substitute #\b #\a string :start (opaque-identity -1) :end 5)))
797  (assert (raises-error? (substitute #\b #\a string :start 4 :end 2)))
798  (assert (raises-error? (substitute #\b #\a string :start 6 :end 9)))
799  (assert (string= (substitute-if #\b #'alpha-char-p string
800                                  :start 0 :end 5)
801                   "bbbbb"))
802  (assert (string= (substitute-if #\c #'alpha-char-p string
803                                  :start 0 :end nil)
804                   "ccccc"))
805  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
806                                        :start 0 :end 6)))
807  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
808                                        :start (opaque-identity -1) :end 5)))
809  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
810                                        :start 4 :end 2)))
811  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
812                                        :start 6 :end 9)))
813  (assert (string= (substitute-if-not #\b #'alpha-char-p string
814                                      :start 0 :end 5)
815                   "aaaaa"))
816  (assert (string= (substitute-if-not #\c #'alpha-char-p string
817                                      :start 0 :end nil)
818                   "aaaaa"))
819  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
820                                            :start 0 :end 6)))
821  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
822                                            :start (opaque-identity -1) :end 5)))
823  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
824                                            :start 4 :end 2)))
825  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
826                                            :start 6 :end 9))))
827 (sequence-bounding-indices-test
828  (format t "~&/... NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT")
829  (assert (string= (nsubstitute #\b #\a string :start 0 :end 5) "bbbbb"))
830  (reset)
831  (assert (string= (nsubstitute #\c #\a string :start 0 :end nil)
832                   "ccccc"))
833  (reset)
834  (assert (raises-error? (nsubstitute #\b #\a string :start 0 :end 6)))
835  (reset)
836  (assert (raises-error? (nsubstitute #\b #\a string :start (opaque-identity -1) :end 5)))
837  (reset)
838  (assert (raises-error? (nsubstitute #\b #\a string :start 4 :end 2)))
839  (reset)
840  (assert (raises-error? (nsubstitute #\b #\a string :start 6 :end 9)))
841  (reset)
842  (assert (string= (nsubstitute-if #\b #'alpha-char-p string
843                                   :start 0 :end 5)
844                   "bbbbb"))
845  (reset)
846  (assert (string= (nsubstitute-if #\c #'alpha-char-p string
847                                   :start 0 :end nil)
848                   "ccccc"))
849  (reset)
850  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
851                                         :start 0 :end 6)))
852  (reset)
853  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
854                                         :start (opaque-identity -1) :end 5)))
855  (reset)
856  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
857                                         :start 4 :end 2)))
858  (reset)
859  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
860                                         :start 6 :end 9)))
861  (reset)
862  (assert (string= (nsubstitute-if-not #\b #'alpha-char-p string
863                                       :start 0 :end 5)
864                   "aaaaa"))
865  (reset)
866  (assert (string= (nsubstitute-if-not #\c #'alpha-char-p string
867                                       :start 0 :end nil)
868                   "aaaaa"))
869  (reset)
870  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
871                                             :start 0 :end 6)))
872  (reset)
873  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
874                                             :start (opaque-identity -1) :end 5)))
875  (reset)
876  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
877                                             :start 4 :end 2)))
878  (reset)
879  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
880                                             :start 6 :end 9))))
881 ;;; Function WRITE-STRING, WRITE-LINE
882 (sequence-bounding-indices-test
883  (format t "~&/Function WRITE-STRING, WRITE-LINE")
884  (write-string string *standard-output* :start 0 :end 5)
885  (write-string string *standard-output* :start 0 :end nil)
886  (assert (raises-error? (write-string string *standard-output*
887                                       :start 0 :end 6)))
888  (assert (raises-error? (write-string string *standard-output*
889                                       :start (opaque-identity -1) :end 5)))
890  (assert (raises-error? (write-string string *standard-output*
891                                       :start 4 :end 2)))
892  (assert (raises-error? (write-string string *standard-output*
893                                       :start 6 :end 9)))
894  (write-line string *standard-output* :start 0 :end 5)
895  (write-line string *standard-output* :start 0 :end nil)
896  (assert (raises-error? (write-line string *standard-output*
897                                       :start 0 :end 6)))
898  (assert (raises-error? (write-line string *standard-output*
899                                       :start (opaque-identity -1) :end 5)))
900  (assert (raises-error? (write-line string *standard-output*
901                                       :start 4 :end 2)))
902  (assert (raises-error? (write-line string *standard-output*
903                                       :start 6 :end 9))))
904
905 ;;; Macro WITH-INPUT-FROM-STRING
906 (sequence-bounding-indices-test
907  (format t "~&/Macro WITH-INPUT-FROM-STRING")
908  (with-input-from-string (s string :start 0 :end 5)
909    (assert (char= (read-char s) #\a)))
910  (with-input-from-string (s string :start 0 :end nil)
911    (assert (char= (read-char s) #\a)))
912  (assert (raises-error?
913           (with-input-from-string (s string :start 0 :end 6)
914             (read-char s))))
915  (assert (raises-error?
916           (with-input-from-string (s string :start (opaque-identity -1) :end 5)
917             (read-char s))))
918  (assert (raises-error?
919           (with-input-from-string (s string :start 4 :end 2)
920             (read-char s))))
921  (assert (raises-error?
922           (with-input-from-string (s string :start 6 :end 9)
923             (read-char s)))))
924 \f
925 ;;; testing bit-bashing according to _The Practice of Programming_
926 (defun fill-bytes-for-testing (bitsize)
927   "Return a list of 'bytes' of type (MOD BITSIZE)."
928   (remove-duplicates (list 0
929                            (1- (ash 1 (1- bitsize)))
930                            (ash 1 (1- bitsize))
931                            (1- (ash 1 bitsize)))))
932
933 (defun fill-with-known-value (value size &rest vectors)
934   (dolist (vec vectors)
935     (dotimes (i size)
936       (setf (aref vec i) value))))
937
938 (defun collect-fill-amounts (n-power)
939   (remove-duplicates
940    (loop for i from 0 upto n-power
941          collect (1- (expt 2 i))
942          collect (expt 2 i)
943          collect (1+ (expt 2 i)))))
944
945 (defun test-fill-bashing (bitsize padding-amount n-power)
946   (let* ((size (+ (* padding-amount 2) (expt 2 n-power) (* padding-amount 2)))
947          (standard (make-array size :element-type `(unsigned-byte ,bitsize)))
948          (bashed (make-array size :element-type `(unsigned-byte ,bitsize)))
949          (fill-amounts (collect-fill-amounts n-power))
950          (bash-function (intern (format nil "UB~A-BASH-FILL" bitsize)
951                                 (find-package "SB-KERNEL"))))
952     (format t "~&/Function ~A..." bash-function)
953     (loop for offset from padding-amount below (* 2 padding-amount) do
954           (dolist (c (fill-bytes-for-testing bitsize))
955             (dolist (n fill-amounts)
956               (fill-with-known-value (mod (lognot c) (ash 1 bitsize)) n
957                                      standard bashed)
958               ;; fill vectors
959               ;; a) the standard slow way
960               (fill standard c :start offset :end (+ offset n))
961               ;; b) the blazingly fast way
962               (let ((value (loop for i from 0 by bitsize
963                                  until (= i sb-vm:n-word-bits)
964                                  sum (ash c i))))
965                 (funcall bash-function value bashed offset n))
966               ;; check for errors
967               (when (mismatch standard bashed)
968                 (format t "Test with offset ~A, fill ~A and length ~A failed.~%"
969                         offset c n)
970                 (format t "Mismatch: ~A ~A~%"
971                         (subseq standard 0 (+ offset n 1))
972                         (subseq bashed 0 (+ offset n 1)))
973                 (return-from test-fill-bashing nil))))
974           finally (return t))))
975
976 (defun test-copy-bashing (bitsize padding-amount n-power)
977   (let* ((size (+ (* padding-amount 2) (expt 2 n-power) (* padding-amount 2)))
978          (standard-dst (make-array size :element-type `(unsigned-byte ,bitsize)))
979          (bashed-dst (make-array size :element-type `(unsigned-byte ,bitsize)))
980          (source (make-array size :element-type `(unsigned-byte ,bitsize)))
981          (fill-amounts (collect-fill-amounts n-power))
982          (bash-function (intern (format nil "UB~A-BASH-COPY" bitsize)
983                                 (find-package "SB-KERNEL"))))
984     (format t "~&/Function ~A..." bash-function)
985     (do ((source-offset padding-amount (1+ source-offset)))
986         ((>= source-offset (* padding-amount 2))
987          ;; success!
988          t)
989      (do ((target-offset padding-amount (1+ target-offset)))
990          ((>= target-offset (* padding-amount 2)))
991        (dolist (c (fill-bytes-for-testing bitsize))
992          (dolist (n fill-amounts)
993            (fill-with-known-value (mod (lognot c) (ash 1 bitsize)) size
994                                   source standard-dst bashed-dst)
995            ;; fill with test data
996            (fill source c :start source-offset :end (+ source-offset n))
997            ;; copy filled test data to test vectors
998            ;; a) the slow way
999            (replace standard-dst source
1000                     :start1 target-offset :end1 (+ target-offset n)
1001                     :start2 source-offset :end2 (+ source-offset n))
1002            ;; b) the blazingly fast way
1003            (funcall bash-function source source-offset
1004                     bashed-dst target-offset n)
1005            ;; check for errors
1006            (when (mismatch standard-dst bashed-dst)
1007              (format t "Test with target-offset ~A, source-offset ~A, fill ~A, and length ~A failed.~%"
1008                      target-offset source-offset c n)
1009              (format t "Mismatch:~% correct ~A~% actual  ~A~%"
1010                      standard-dst
1011                      bashed-dst)
1012              (return-from test-copy-bashing nil))))))))
1013
1014 ;; Too slow for the interpreter
1015 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
1016 (loop for i = 1 then (* i 2) do
1017      ;; the bare '13' here is fairly arbitrary, except that it's been
1018      ;; reduced from '32', which made the tests take aeons; '8' provides
1019      ;; a good range of lengths over which to fill and copy, which
1020      ;; should tease out most errors in the code (if any exist).  (It
1021      ;; also makes this part of the test suite finish reasonably
1022      ;; quickly.)
1023      (assert (time (test-fill-bashing i 13 8)))
1024      (assert (time (test-copy-bashing i 13 8)))
1025      until (= i sb-vm:n-word-bits))
1026
1027 (defun test-inlined-bashing (bitsize)
1028   ;; We have to compile things separately for each bitsize so the
1029   ;; compiler will work out the array type and trigger the REPLACE
1030   ;; transform.
1031   (let ((lambda-form
1032          `(lambda ()
1033             (let* ((n-elements-per-word ,(truncate sb-vm:n-word-bits bitsize))
1034                    (size (* 3 n-elements-per-word))
1035                    (standard-dst (make-array size :element-type '(unsigned-byte ,bitsize)))
1036                    (bashed-dst (make-array size :element-type '(unsigned-byte ,bitsize)))
1037                    (source (make-array size :element-type '(unsigned-byte ,bitsize))))
1038               (declare (type (simple-array (unsigned-byte ,bitsize) (*))
1039                              source standard-dst bashed-dst))
1040               (do ((i 0 (1+ i))
1041                    (offset n-elements-per-word (1+ offset)))
1042                   ((>= offset (* 2 n-elements-per-word)) t)
1043                 (dolist (c (fill-bytes-for-testing ,bitsize))
1044                   (fill-with-known-value (mod (lognot c) (ash 1 ,bitsize)) size
1045                                          source standard-dst bashed-dst)
1046                   ;; fill with test-data
1047                   (fill source c :start offset :end (+ offset n-elements-per-word))
1048                   ;; copy filled data to test vectors
1049                   ;;
1050                   ;; a) the slow way (which is actually fast, since this
1051                   ;; should be transformed into UB*-BASH-COPY)
1052                   (replace standard-dst source
1053                            :start1 (- offset n-elements-per-word i)
1054                            :start2 (- offset n-elements-per-word i)
1055                            :end1 offset :end2 offset)
1056                   ;; b) the fast way--we fold the
1057                   ;; :START{1,2} arguments above ourselves
1058                   ;; to trigger the REPLACE transform
1059                   (replace bashed-dst source
1060                            :start1 0 :start2 0 :end1 offset :end2 offset)
1061                   ;; check for errors
1062                   (when (or (mismatch standard-dst bashed-dst)
1063                             ;; trigger COPY-SEQ transform
1064                             (mismatch (copy-seq standard-dst) bashed-dst)
1065                             ;; trigger SUBSEQ transform
1066                             (mismatch (subseq standard-dst (- offset n-elements-per-word i))
1067                                       bashed-dst))
1068                     (format t "Test with target-offset ~A, source-offset ~A, fill ~A, and length ~A failed.~%"
1069                             0 0 c offset)
1070                     (format t "Mismatch:~% correct ~A~% actual  ~A~%"
1071                             standard-dst
1072                             bashed-dst)
1073                     (return-from nil nil))))))))
1074     (funcall (compile nil lambda-form))))
1075
1076 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
1077 (loop for i = 1 then (* i 2) do
1078       (assert (test-inlined-bashing i))
1079       until (= i sb-vm:n-word-bits))
1080 \f
1081 ;;; tests from the Sacla test suite via Eric Marsden, 2007-05-07
1082 (remove-duplicates (vector 1 2 2 1) :test-not (lambda (a b) (not (= a b))))
1083
1084 (delete-duplicates (vector #\a #\b #\c #\a)
1085                    :test-not (lambda (a b) (not (char-equal a b))))
1086 \f
1087 ;;; success