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