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