dyndebug_init: fix gencgc_verbose handling.
[sbcl.git] / tests / seq.impure.lisp
blobd24dfc2d881f387c0c9cd16cdd8ad8fd1ef3df43
1 ;;;; tests related to sequences
3 ;;;; This file is impure because we want to be able to use DEFUN.
5 ;;;; This software is part of the SBCL system. See the README file for
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.
16 (load "test-util.lisp")
17 (load "assertoid.lisp")
19 (defpackage :seq-test
20 (:use :cl :assertoid :test-util))
22 (in-package :seq-test)
24 ;;; user-defined mock sequence class for testing generic versions of
25 ;;; sequence functions.
26 (defclass list-backed-sequence (standard-object
27 sequence)
28 ((elements :initarg :elements :type list :accessor %elements)))
30 (defmethod sequence:make-sequence-like ((sequence list-backed-sequence) length
31 &rest args &key
32 initial-element initial-contents)
33 (declare (ignore initial-element initial-contents))
34 (make-instance 'list-backed-sequence
35 :elements (apply #'sequence:make-sequence-like
36 '() length args)))
38 (defmethod sequence:length ((sequence list-backed-sequence))
39 (length (%elements sequence)))
41 (defmethod sequence:elt
42 ((sequence list-backed-sequence) index)
43 (nth index (%elements sequence)))
45 (defmethod (setf sequence:elt)
46 (new-value (sequence list-backed-sequence) index)
47 (setf (nth index (%elements sequence)) new-value))
49 ;;; helper functions for exercising SEQUENCE code on data of many
50 ;;; specialized types, and in many different optimization scenarios
51 (defun for-every-seq-1 (base-seq snippet)
52 (labels
53 ((entirely (eltype)
54 (every (lambda (el) (typep el eltype)) base-seq))
55 (make-sequence-for-type (type)
56 (etypecase type
57 ((member list list-backed-sequence)
58 (coerce base-seq type))
59 ((cons (eql simple-array) (cons * (cons (eql 1) null)))
60 (destructuring-bind (eltype one) (rest type)
61 (declare (ignore one))
62 (when (entirely eltype)
63 (coerce base-seq type))))
64 ((cons (eql vector))
65 (destructuring-bind (eltype) (rest type)
66 (when (entirely eltype)
67 (let ((initial-element
68 (cond ((subtypep eltype 'character)
69 #\!)
70 ((subtypep eltype 'number)
72 (t #'error))))
73 (replace (make-array
74 (+ (length base-seq)
75 (random 3))
76 :element-type eltype
77 :fill-pointer
78 (length base-seq)
79 :initial-element
80 initial-element)
81 base-seq))))))))
82 (dolist (seq-type '(list
83 (simple-array t 1)
84 (vector t)
85 (simple-array character 1)
86 (vector character)
87 (simple-array (signed-byte 4) 1)
88 (vector (signed-byte 4))
89 list-backed-sequence))
90 (dolist (declaredness '(nil t))
91 (map-optimize-declarations
92 (lambda (optimization)
93 (let ((seq (make-sequence-for-type seq-type))
94 (lambda-expr `(lambda (seq)
95 (declare (sb-ext:muffle-conditions
96 sb-ext:compiler-note))
97 ,@(when declaredness
98 `((declare (type ,seq-type seq))))
99 (declare (optimize ,@optimization))
100 ,snippet)))
101 (when (not seq)
102 (return))
103 ;(format t "~&~S~%" lambda-expr)
104 (let ((fun (checked-compile lambda-expr)))
105 ;(format t "~&~S ~S~%~S~%~S ~S~%"
106 ; base-seq snippet seq-type declaredness optimization)
107 ;(format t "~&(TYPEP SEQ 'SIMPLE-ARRAY)=~S~%"
108 ; (typep seq 'simple-array))
109 (unless (funcall fun seq)
110 (error "~@<failed test:~2I ~_BASE-SEQ=~S ~_SNIPPET=~S ~_SEQ-TYPE=~S ~_DECLAREDNESS=~S ~_OPTIMIZATION=~S~:@>"
111 base-seq
112 snippet
113 seq-type
114 declaredness
115 optimization)))))
116 :safety nil :debug nil :compilation-speed nil)))))
117 (defun for-every-seq (base-seq snippets)
118 (dolist (snippet snippets)
119 (for-every-seq-1 base-seq snippet)))
121 ;;; a wrapper to hide declared type information from the compiler, so
122 ;;; we don't get stopped by compiler warnings about e.g. compiling
123 ;;; (POSITION 1 #() :KEY #'ABS) when #() has been coerced to a string.
124 (defun indiscriminate (fun)
125 (lambda (&rest rest) (apply fun rest)))
127 ;;; asymmetric test arg order example from ANSI FIND definition page
128 (assert (eql #\space ; original example, depends on ASCII character ordering
129 (find #\d "here are some letters that can be looked at"
130 :test #'char>)))
131 (assert (eql #\e ; modified example, depends only on standard a-z ordering
132 (find #\f "herearesomeletters" :test #'char>)))
133 (assert (eql 4 ; modified more, avoids charset technicalities completely
134 (find 5 '(6 4) :test '>)))
136 (with-test (:name sequence:emptyp)
137 (for-every-seq #()
138 '((eq t (sequence:emptyp seq))))
139 (for-every-seq #(1)
140 '((eq nil (sequence:emptyp seq)))))
142 ;;; tests of FIND, POSITION, FIND-IF, and POSITION-IF (and a few for
143 ;;; deprecated FIND-IF-NOT and POSITION-IF-NOT too)
144 (for-every-seq #()
145 '((null (find 1 seq))
146 (null (find 1 seq :from-end t))
147 (null (position 1 seq :key (indiscriminate #'abs)))
148 (null (position nil seq :test (constantly t)))
149 (null (position nil seq :test nil))
150 (null (position nil seq :test-not nil))
151 (null (find-if #'1+ seq :key (indiscriminate #'log)))
152 (null (position-if #'identity seq :from-end t))
153 (null (find-if-not #'packagep seq))
154 (null (position-if-not #'packagep seq :key nil))))
155 (for-every-seq #(1)
156 '((null (find 2 seq))
157 ;; Get the argument ordering for asymmetric tests like #'> right.
158 ;; (bug reported and fixed by Alexey Dejneka sbcl-devel 2001-10-17)
159 (eql 1 (find 2 seq :test #'>))
160 (find 2 seq :key #'1+)
161 (find 1 seq :from-end t)
162 (null (find 1 seq :from-end t :start 1))
163 (null (find 0 seq :from-end t))
164 (eql 0 (position 1 seq :key #'abs))
165 (null (position nil seq :test 'equal))
166 (eql 1 (find-if #'1- seq :key #'log))
167 (eql 0 (position-if #'identity seq :from-end t))
168 (null (find-if-not #'sin seq))
169 (eql 0 (position-if-not #'packagep seq :key 'identity))))
170 (for-every-seq #(1 2 3 2 1)
171 '((find 3 seq)
172 (find 3 seq :from-end 'yes)
173 (eql 1 (position 1.5 seq :test #'<))
174 (eql 0 (position 0 seq :key '1-))
175 (eql 4 (position 0 seq :key '1- :from-end t))
176 (eql 2 (position 4 seq :key '1+))
177 (eql 2 (position 4 seq :key '1+ :from-end t))
178 (eql 1 (position 2 seq))
179 (eql 1 (position 2 seq :start 1))
180 (null (find 2 seq :start 1 :end 1))
181 (eql 3 (position 2 seq :start 2))
182 (eql 3 (position 2 seq :key nil :from-end t))
183 (eql 2 (position 3 seq :test '=))
184 (eql 0 (position 3 seq :test-not 'equalp))
185 (eql 2 (position 3 seq :test 'equal :from-end t))
186 (null (position 4 seq :test #'eql))
187 (null (find-if #'packagep seq))
188 (eql 1 (find-if #'plusp seq))
189 (eql 3 (position-if #'plusp seq :key #'1- :from-end t))
190 (eql 1 (position-if #'evenp seq))
191 (eql 3 (position-if #'evenp seq :from-end t))
192 (eql 2 (position-if #'plusp seq :from-end nil :key '1- :start 2))
193 (eql 3 (position-if #'plusp seq :from-end t :key '1- :start 2))
194 (null (position-if #'plusp seq :from-end t :key '1- :start 2 :end 2))
195 (null (find-if-not #'plusp seq))
196 (eql 0 (position-if-not #'evenp seq))
197 (eql 0 (search #(1) seq))
198 (eql 1 (search #(4 5) seq :key 'oddp))
199 (eql 1 (search #(-2) seq :test (lambda (a b) (= (- a) b))))
200 (eql 4 (search #(1) seq :start2 1))
201 (null (search #(3) seq :start2 3))
202 (eql 2 (search #(3) seq :start2 2))
203 (eql 0 (search #(1 2) seq))
204 (null (search #(2 1 3) seq))
205 (eql 0 (search #(0 1 2 4) seq :start1 1 :end1 3))
206 (eql 3 (search #(0 2 1 4) seq :start1 1 :end1 3))
207 (eql 4 (search #(1) seq :from-end t))
208 (eql 0 (search #(1 2) seq :from-end t))
209 (null (search #(1 2) seq :from-end t :start2 1))
210 (eql 0 (search #(0 1 2 4) seq :from-end t :start1 1 :end1 3))
211 (eql 3 (search #(0 2 1 4) seq :from-end t :start1 1 :end1 3))
212 (null (search #(2 1 3) seq :from-end t))))
213 (for-every-seq "string test"
214 '((null (find 0 seq))
215 (null (find #\D seq :key #'char-upcase))
216 (find #\E seq :key #'char-upcase)
217 (null (find #\e seq :key #'char-upcase))
218 (eql 3 (position #\i seq))
219 (eql 0 (position #\s seq :key #'char-downcase))
220 (eql 1 (position #\s seq :key #'char-downcase :test #'char/=))
221 (eql 9 (position #\s seq :from-end t :test #'char=))
222 (eql 10 (position #\s seq :from-end t :test #'char/=))
223 (eql 4 (position #\N seq :from-end t :key 'char-upcase :test #'char-equal))
224 (eql 5 (position-if (lambda (c) (equal #\g c)) seq))
225 (eql 5 (position-if (lambda (c) (equal #\g c)) seq :from-end t))
226 (find-if #'characterp seq)
227 (find-if (lambda (c) (typep c 'base-char)) seq :from-end t)
228 (null (find-if 'upper-case-p seq))))
230 ;;; SUBSEQ
231 (with-test (:name :subseq)
232 (let ((avec (make-array 10
233 :fill-pointer 4
234 :initial-contents '(0 1 2 3 iv v vi vii iix ix))))
235 ;; These first five always worked AFAIK.
236 (assert (equalp (subseq avec 0 3) #(0 1 2)))
237 (assert (equalp (subseq avec 3 3) #()))
238 (assert (equalp (subseq avec 1 3) #(1 2)))
239 (assert (equalp (subseq avec 1) #(1 2 3)))
240 (assert (equalp (subseq avec 1 4) #(1 2 3)))
241 ;; SBCL bug found ca. 2002-05-01 by OpenMCL's correct handling of
242 ;; SUBSEQ, CSR's driving portable cross-compilation far enough to
243 ;; reach the SUBSEQ calls in assem.lisp, and WHN's sleazy
244 ;; translation of old CMU CL new-assem.lisp into sufficiently grotty
245 ;; portable Lisp that it passed suitable illegal values to SUBSEQ to
246 ;; exercise the bug:-|
248 ;; SUBSEQ should check its END value against logical LENGTH, not
249 ;; physical ARRAY-DIMENSION 0.
251 ;; fixed in sbcl-0.7.4.22 by WHN
252 (assert (null (ignore-errors (aref (subseq avec 1 5) 0))))))
254 ;;; FILL
255 (defun test-fill-typecheck (x)
256 (declare (optimize (safety 3) (space 2) (speed 1)))
257 (fill (make-string 10) x))
259 (assert (string= (test-fill-typecheck #\@) "@@@@@@@@@@"))
260 ;;; BUG 186, fixed in sbcl-0.7.5.5
261 (assert (null (ignore-errors (test-fill-typecheck 4097))))
263 ;;; MAKE-SEQUENCE, COERCE, CONCATENATE, MERGE, MAP and requested
264 ;;; result type (BUGs 46a, 46b, 66)
265 (with-test (:name :sequence-functions)
266 (macrolet ((assert-type-error (form)
267 `(assert-error ,form type-error)))
268 (dolist (type-stub '((simple-vector)
269 (vector *)
270 (vector (signed-byte 8))
271 (vector (unsigned-byte 16))
272 (vector (signed-byte 32))
273 (simple-bit-vector)))
274 (declare (optimize safety))
275 (format t "~&~S~%" type-stub)
276 ;; MAKE-SEQUENCE
277 (assert (= (length (make-sequence `(,@type-stub) 10)) 10))
278 (assert (= (length (make-sequence `(,@type-stub 10) 10)) 10))
279 (assert-type-error (make-sequence `(,@type-stub 10) 11))
280 ;; COERCE
281 (assert (= (length (coerce '(0 0 0) `(,@type-stub))) 3))
282 (assert (= (length (coerce #(0 0 0) `(,@type-stub 3))) 3))
283 (assert-type-error (coerce #*111 `(,@type-stub 4)))
284 ;; CONCATENATE
285 (assert (= (length (concatenate `(,@type-stub) #(0 0 0) #*111)) 6))
286 (assert (equalp (concatenate `(,@type-stub) #(0 0 0) #*111)
287 (coerce #(0 0 0 1 1 1) `(,@type-stub))))
288 (assert (= (length (concatenate `(,@type-stub 6) #(0 0 0) #*111)) 6))
289 (assert (equalp (concatenate `(,@type-stub 6) #(0 0 0) #*111)
290 (coerce #(0 0 0 1 1 1) `(,@type-stub 6))))
291 (assert-type-error (concatenate `(,@type-stub 5) #(0 0 0) #*111))
292 ;; MERGE
293 (macrolet ((test (type)
294 `(merge ,type (copy-seq #(0 1 0)) (copy-seq #*111) #'>)))
295 (assert (= (length (test `(,@type-stub))) 6))
296 (assert (equalp (test `(,@type-stub))
297 (coerce #(1 1 1 0 1 0) `(,@type-stub))))
298 (assert (= (length (test `(,@type-stub 6))) 6))
299 (assert (equalp (test `(,@type-stub 6))
300 (coerce #(1 1 1 0 1 0) `(,@type-stub 6))))
301 (assert-type-error (test `(,@type-stub 4))))
302 ;; MAP
303 (assert (= (length (map `(,@type-stub) #'logxor #(0 0 1 1) '(0 1 0 1))) 4))
304 (assert (equalp (map `(,@type-stub) #'logxor #(0 0 1 1) '(0 1 0 1))
305 (coerce #(0 1 1 0) `(,@type-stub))))
306 (assert (= (length (map `(,@type-stub 4) #'logxor #(0 0 1 1) '(0 1 0 1)))
308 (assert (equalp (map `(,@type-stub 4) #'logxor #(0 0 1 1) '(0 1 0 1))
309 (coerce #(0 1 1 0) `(,@type-stub 4))))
310 (assert-type-error (map `(,@type-stub 5) #'logxor #(0 0 1 1) '(0 1 0 1))))
311 ;; some more CONCATENATE tests for strings
312 (locally
313 (declare (optimize safety))
314 (assert (string= (concatenate 'string "foo" " " "bar") "foo bar"))
315 (assert (string= (concatenate '(string 7) "foo" " " "bar") "foo bar"))
316 (assert-type-error (concatenate '(string 6) "foo" " " "bar"))
317 (assert (string= (concatenate '(string 6) "foo" #(#\b #\a #\r)) "foobar"))
318 (assert (string= (concatenate '(string 6) #(#\b #\a #\r) "foo") "barfoo"))
319 (assert-type-error (concatenate '(string 7) "foo" #(#\b #\a #\r))))
320 ;; Non-VECTOR ARRAY types aren't allowed as vector type specifiers.
321 (locally
322 (declare (optimize safety))
323 (assert-type-error (concatenate 'simple-array "foo" "bar"))
324 (assert-type-error (map 'simple-array #'identity '(1 2 3)))
325 (assert (equalp #(11 13)
326 (map '(simple-array fixnum (*)) #'+ '(1 2 3) '(10 11))))
327 (assert-type-error (coerce '(1 2 3) 'simple-array))
328 (assert-type-error (merge 'simple-array (list 1 3) (list 2 4) '<))
329 (assert (equalp #(3 2 1) (coerce '(3 2 1) '(vector fixnum))))
330 (assert-type-error (map 'array #'identity '(1 2 3)))
331 (assert-type-error (map '(array fixnum) #'identity '(1 2 3)))
332 (assert (equalp #(1 2 3) (coerce '(1 2 3) '(vector fixnum))))
333 ;; but COERCE has an exemption clause:
334 (assert (string= "foo" (coerce "foo" 'simple-array)))
335 ;; ... though not in all cases.
336 (assert-type-error (coerce '(#\f #\o #\o) 'simple-array)))))
338 ;; CONCATENATE used to fail for generic sequences for result-type NULL.
339 (with-test (:name (concatenate :result-type-null :bug-1162301))
340 (assert (sequence:emptyp (concatenate 'null)))
342 (for-every-seq #()
343 '((sequence:emptyp (concatenate 'null seq))
344 (sequence:emptyp (concatenate 'null seq seq))
345 (sequence:emptyp (concatenate 'null seq #()))
346 (sequence:emptyp (concatenate 'null seq ""))))
348 (for-every-seq #(1)
349 (mapcar (lambda (form)
350 `(typep (nth-value 1 (ignore-errors ,form)) 'type-error))
351 '((concatenate 'null seq)
352 (concatenate 'null seq seq)
353 (concatenate 'null seq #())
354 (concatenate 'null seq "2")))))
356 ;;; As pointed out by Raymond Toy on #lisp IRC, MERGE had some issues
357 ;;; with user-defined types until sbcl-0.7.8.11
358 (deftype list-typeoid () 'list)
359 (with-test (:name :merge-user-types)
360 (assert (equal '(1 2 3 4) (merge 'list-typeoid (list 1 3) (list 2 4) '<)))
361 ;;; and also with types that weren't precicely LIST
362 (assert (equal '(1 2 3 4) (merge 'cons (list 1 3) (list 2 4) '<))))
364 ;;; but wait, there's more! The NULL and CONS types also have implicit
365 ;;; length requirements:
366 (with-test (:name :sequence-functions-list-types)
367 (macrolet ((assert-type-error (form)
368 `(assert (typep (nth-value 1 (ignore-errors ,form))
369 'type-error))))
370 (locally
371 (declare (optimize safety))
372 ;; MAKE-SEQUENCE
373 (assert-type-error (make-sequence 'cons 0))
374 (assert-type-error (make-sequence 'null 1))
375 (assert-type-error (make-sequence '(cons t null) 0))
376 (assert-type-error (make-sequence '(cons t null) 2))
377 ;; KLUDGE: I'm not certain that this test actually tests for what
378 ;; it should test, in that the type deriver and optimizers might
379 ;; be too smart for the good of an exhaustive test system.
380 ;; However, it makes me feel good. -- CSR, 2002-10-18
381 (assert (null (make-sequence 'null 0)))
382 (assert (= (length (make-sequence 'cons 3)) 3))
383 (assert (= (length (make-sequence '(cons t null) 1)) 1))
384 ;; and NIL is not a valid type for MAKE-SEQUENCE
385 (assert-type-error (make-sequence 'nil 0))
386 ;; COERCE
387 (assert-type-error (coerce #(1) 'null))
388 (assert-type-error (coerce #() 'cons))
389 (assert-type-error (coerce #() '(cons t null)))
390 (assert-type-error (coerce #(1 2) '(cons t null)))
391 (assert (null (coerce #() 'null)))
392 (assert (= (length (coerce #(1) 'cons)) 1))
393 (assert (= (length (coerce #(1) '(cons t null))) 1))
394 (assert-type-error (coerce #() 'nil))
395 ;; MERGE
396 (assert-type-error (merge 'null (list 1 3) (list 2 4) '<))
397 (assert-type-error (merge 'cons () () '<))
398 (assert (null (merge 'null () () '<)))
399 (assert (= (length (merge 'cons (list 1 3) (list 2 4) '<)) 4))
400 (assert (= (length (merge '(cons t (cons t (cons t (cons t null))))
401 (list 1 3) (list 2 4)
402 '<))
404 (assert-type-error (merge 'nil () () '<))
405 ;; CONCATENATE
406 (assert-type-error (concatenate 'cons #() ()))
407 (assert-type-error (concatenate '(cons t null) #(1 2 3) #(4 5 6)))
408 (assert (= (length (concatenate 'cons #() '(1) "2 3")) 4))
409 (assert (= (length (concatenate '(cons t cons) '(1) "34")) 3))
410 (assert-type-error (concatenate 'nil '(3)))
411 ;; FIXME: tests for MAP to come when some brave soul implements
412 ;; the analogous type checking for MAP/%MAP.
415 ;;; ELT should signal an error of type TYPE-ERROR if its index
416 ;;; argument isn't a valid sequence index for sequence:
417 (defun test-elt-signal (x)
418 (elt x 3))
419 (assert-error (test-elt-signal "foo") type-error)
420 (assert (eql (test-elt-signal "foob") #\b))
421 (locally
422 (declare (optimize (safety 3)))
423 (assert-error (elt (list 1 2 3) 3) type-error))
425 ;;; confusion in the refactoring led to this signalling an unbound
426 ;;; variable, not a type error.
427 (defun svrefalike (x)
428 (svref x 0))
429 (assert-error (svrefalike #*0) type-error)
431 ;;; checks for uniform bounding index handling.
433 ;;; This used to be SAFETY 3 only, but bypassing these checks with
434 ;;; above-zero speed when SPEED > SAFETY is not The SBCL Way.
436 ;;; KLUDGE: not all in one big form because that causes SBCL to spend
437 ;;; an absolute age trying to compile it.
438 (defmacro sequence-bounding-indices-test (&body body)
439 `(progn
440 (locally
441 ;; See Issues 332 [and 333(!)] in the CLHS
442 (declare (optimize (speed 3) (safety 1)))
443 (let ((string (make-array 10
444 :fill-pointer 5
445 :initial-element #\a
446 :element-type 'base-char)))
447 ,(car body)
448 (format t "... BASE-CHAR")
449 (finish-output)
450 (flet ((reset ()
451 (setf (fill-pointer string) 10)
452 (fill string #\a)
453 (setf (fill-pointer string) 5)))
454 (declare (ignorable #'reset))
455 ,@(cdr body))))
456 (locally
457 ;; See Issues 332 [and 333(!)] in the CLHS
458 (declare (optimize (speed 3) (safety 1)))
459 (let ((string (make-array 10
460 :fill-pointer 5
461 :initial-element #\a
462 :element-type 'character)))
463 ,(car body)
464 (format t "... CHARACTER")
465 (finish-output)
466 (flet ((reset ()
467 (setf (fill-pointer string) 10)
468 (fill string #\a)
469 (setf (fill-pointer string) 5)))
470 (declare (ignorable #'reset))
471 ,@(cdr body))))))
473 (declaim (notinline opaque-identity))
474 (defun opaque-identity (x) x)
475 ;;; Accessor SUBSEQ
476 (sequence-bounding-indices-test
477 (format t "~&/Accessor SUBSEQ")
478 (assert (string= (subseq string 0 5) "aaaaa"))
479 (assert-error (subseq string 0 6))
480 (assert-error (subseq string (opaque-identity -1) 5))
481 (assert-error (subseq string 4 2))
482 (assert-error (subseq string 6))
483 (assert (string= (setf (subseq string 0 5) "abcde") "abcde"))
484 (assert (string= (subseq string 0 5) "abcde"))
485 (reset)
486 (assert-error (setf (subseq string 0 6) "abcdef"))
487 (assert-error (setf (subseq string (opaque-identity -1) 5) "abcdef"))
488 (assert-error (setf (subseq string 4 2) ""))
489 (assert-error (setf (subseq string 6) "ghij")))
491 ;;; Function COUNT, COUNT-IF, COUNT-IF-NOT
492 (sequence-bounding-indices-test
493 (format t "~&/Function COUNT, COUNT-IF, COUNT-IF-NOT")
494 (assert (= (count #\a string :start 0 :end nil) 5))
495 (assert (= (count #\a string :start 0 :end 5) 5))
496 (assert-error (count #\a string :start 0 :end 6))
497 (assert-error (count #\a string :start (opaque-identity -1) :end 5))
498 (assert-error (count #\a string :start 4 :end 2))
499 (assert-error (count #\a string :start 6 :end 9))
500 (assert (= (count-if #'alpha-char-p string :start 0 :end nil) 5))
501 (assert (= (count-if #'alpha-char-p string :start 0 :end 5) 5))
502 (assert-error
503 (count-if #'alpha-char-p string :start 0 :end 6))
504 (assert-error
505 (count-if #'alpha-char-p string :start (opaque-identity -1) :end 5))
506 (assert-error
507 (count-if #'alpha-char-p string :start 4 :end 2))
508 (assert-error
509 (count-if #'alpha-char-p string :start 6 :end 9))
510 (assert (= (count-if-not #'alpha-char-p string :start 0 :end nil) 0))
511 (assert (= (count-if-not #'alpha-char-p string :start 0 :end 5) 0))
512 (assert-error
513 (count-if-not #'alpha-char-p string :start 0 :end 6))
514 (assert-error
515 (count-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5))
516 (assert-error
517 (count-if-not #'alpha-char-p string :start 4 :end 2))
518 (assert-error
519 (count-if-not #'alpha-char-p string :start 6 :end 9)))
521 ;;; Function FILL
522 (sequence-bounding-indices-test
523 (format t "~&/Function FILL")
524 (assert (string= (fill string #\b :start 0 :end 5) "bbbbb"))
525 (assert (string= (fill string #\c :start 0 :end nil) "ccccc"))
526 (assert-error (fill string #\d :start 0 :end 6))
527 (assert-error (fill string #\d :start (opaque-identity -1) :end 5))
528 (assert-error (fill string #\d :start 4 :end 2))
529 (assert-error (fill string #\d :start 6 :end 9)))
531 ;;; Function FIND, FIND-IF, FIND-IF-NOT
532 (sequence-bounding-indices-test
533 (format t "~&/Function FIND, FIND-IF, FIND-IF-NOT")
534 (assert (char= (find #\a string :start 0 :end nil) #\a))
535 (assert (char= (find #\a string :start 0 :end 5) #\a))
536 (assert-error (find #\a string :start 0 :end 6))
537 (assert-error (find #\a string :start (opaque-identity -1) :end 5))
538 (assert-error (find #\a string :start 4 :end 2))
539 (assert-error (find #\a string :start 6 :end 9))
540 (assert (char= (find-if #'alpha-char-p string :start 0 :end nil) #\a))
541 (assert (char= (find-if #'alpha-char-p string :start 0 :end 5) #\a))
542 (assert-error
543 (find-if #'alpha-char-p string :start 0 :end 6))
544 (assert-error
545 (find-if #'alpha-char-p string :start (opaque-identity -1) :end 5))
546 (assert-error
547 (find-if #'alpha-char-p string :start 4 :end 2))
548 (assert-error
549 (find-if #'alpha-char-p string :start 6 :end 9))
550 (assert (eq (find-if-not #'alpha-char-p string :start 0 :end nil) nil))
551 (assert (eq (find-if-not #'alpha-char-p string :start 0 :end 5) nil))
552 (assert-error
553 (find-if-not #'alpha-char-p string :start 0 :end 6))
554 (assert-error
555 (find-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5))
556 (assert-error
557 (find-if-not #'alpha-char-p string :start 4 :end 2))
558 (assert-error
559 (find-if-not #'alpha-char-p string :start 6 :end 9)))
561 ;;; Function MISMATCH
562 (sequence-bounding-indices-test
563 (format t "~&/Function MISMATCH")
564 (assert (null (mismatch string "aaaaa" :start1 0 :end1 nil)))
565 (assert (= (mismatch "aaab" string :start2 0 :end2 4) 3))
566 (assert-error (mismatch "aaaaaa" string :start2 0 :end2 6))
567 (assert-error (mismatch string "aaaaaa" :start1 (opaque-identity -1) :end1 5))
568 (assert-error (mismatch string "" :start1 4 :end1 2))
569 (assert-error (mismatch "aaaa" string :start2 6 :end2 9)))
571 ;;; Function PARSE-INTEGER
572 (sequence-bounding-indices-test
573 (format t "~&/Function PARSE-INTEGER")
574 (setf (fill-pointer string) 10)
575 (setf (subseq string 0 10) "1234567890")
576 (setf (fill-pointer string) 5)
577 (assert (= (parse-integer string :start 0 :end 5) 12345))
578 (assert (= (parse-integer string :start 0 :end nil) 12345))
579 (assert-error (parse-integer string :start 0 :end 6))
580 (assert-error (parse-integer string :start (opaque-identity -1) :end 5))
581 (assert-error (parse-integer string :start 4 :end 2))
582 (assert-error (parse-integer string :start 6 :end 9)))
584 ;;; Function PARSE-NAMESTRING
585 (sequence-bounding-indices-test
586 (format t "~&/Function PARSE-NAMESTRING")
587 (setf (fill-pointer string) 10)
588 (setf (subseq string 0 10)
589 #-win32 "/dev/ /tmp"
590 #+win32 "C:/ NUL")
591 (setf (fill-pointer string) 5)
592 (assert (truename (parse-namestring string nil *default-pathname-defaults*
593 :start 0 :end 5)))
594 (assert (truename (parse-namestring string nil *default-pathname-defaults*
595 :start 0 :end nil)))
596 (assert-error (parse-namestring string nil
597 *default-pathname-defaults*
598 :start 0 :end 6))
599 (assert-error (parse-namestring string nil
600 *default-pathname-defaults*
601 :start (opaque-identity -1) :end 5))
602 (assert-error (parse-namestring string nil
603 *default-pathname-defaults*
604 :start 4 :end 2))
605 (assert-error (parse-namestring string nil
606 *default-pathname-defaults*
607 :start 6 :end 9)))
609 ;;; Function POSITION, POSITION-IF, POSITION-IF-NOT
610 (sequence-bounding-indices-test
611 (format t "~&/Function POSITION, POSITION-IF, POSITION-IF-NOT")
613 (assert (= (position #\a string :start 0 :end nil) 0))
614 (assert (= (position #\a string :start 0 :end 5) 0))
615 (assert-error (position #\a string :start 0 :end 6))
616 (assert-error (position #\a string :start (opaque-identity -1) :end 5))
617 (assert-error (position #\a string :start 4 :end 2))
618 (assert-error (position #\a string :start 6 :end 9))
619 (assert (= (position-if #'alpha-char-p string :start 0 :end nil) 0))
620 (assert (= (position-if #'alpha-char-p string :start 0 :end 5) 0))
621 (assert-error
622 (position-if #'alpha-char-p string :start 0 :end 6))
623 (assert-error
624 (position-if #'alpha-char-p string :start (opaque-identity -1) :end 5))
625 (assert-error
626 (position-if #'alpha-char-p string :start 4 :end 2))
627 (assert-error
628 (position-if #'alpha-char-p string :start 6 :end 9))
629 (assert (eq (position-if-not #'alpha-char-p string :start 0 :end nil) nil))
630 (assert (eq (position-if-not #'alpha-char-p string :start 0 :end 5) nil))
631 (assert-error
632 (position-if-not #'alpha-char-p string :start 0 :end 6))
633 (assert-error
634 (position-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5))
635 (assert-error
636 (position-if-not #'alpha-char-p string :start 4 :end 2))
637 (assert-error
638 (position-if-not #'alpha-char-p string :start 6 :end 9)))
640 ;;; Function READ-FROM-STRING
641 (sequence-bounding-indices-test
642 (format t "~&/Function READ-FROM-STRING")
643 (setf (subseq string 0 5) "(a b)")
644 (assert (equal (read-from-string string nil nil :start 0 :end 5) '(a b)))
645 (assert (equal (read-from-string string nil nil :start 0 :end nil) '(a b)))
646 (assert-error (read-from-string string nil nil :start 0 :end 6))
647 (assert-error (read-from-string string nil nil :start (opaque-identity -1) :end 5))
648 (assert-error (read-from-string string nil nil :start 4 :end 2))
649 (assert-error (read-from-string string nil nil :start 6 :end 9)))
651 ;;; Function REDUCE
652 (sequence-bounding-indices-test
653 (format t "~&/Function REDUCE")
654 (setf (subseq string 0 5) "abcde")
655 (assert (equal (reduce #'list* string :from-end t :start 0 :end nil)
656 '(#\a #\b #\c #\d . #\e)))
657 (assert (equal (reduce #'list* string :from-end t :start 0 :end 5)
658 '(#\a #\b #\c #\d . #\e)))
659 (assert-error (reduce #'list* string :start 0 :end 6))
660 (assert-error (reduce #'list* string :start (opaque-identity -1) :end 5))
661 (assert-error (reduce #'list* string :start 4 :end 2))
662 (assert-error (reduce #'list* string :start 6 :end 9)))
664 ;;; Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, DELETE, DELETE-IF,
665 ;;; DELETE-IF-NOT
666 (sequence-bounding-indices-test
667 (format t "~&/Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, ...")
668 (assert (equal (remove #\a string :start 0 :end nil) ""))
669 (assert (equal (remove #\a string :start 0 :end 5) ""))
670 (assert-error (remove #\a string :start 0 :end 6))
671 (assert-error (remove #\a string :start (opaque-identity -1) :end 5))
672 (assert-error (remove #\a string :start 4 :end 2))
673 (assert-error (remove #\a string :start 6 :end 9))
674 (assert (equal (remove-if #'alpha-char-p string :start 0 :end nil) ""))
675 (assert (equal (remove-if #'alpha-char-p string :start 0 :end 5) ""))
676 (assert-error
677 (remove-if #'alpha-char-p string :start 0 :end 6))
678 (assert-error
679 (remove-if #'alpha-char-p string :start (opaque-identity -1) :end 5))
680 (assert-error
681 (remove-if #'alpha-char-p string :start 4 :end 2))
682 (assert-error
683 (remove-if #'alpha-char-p string :start 6 :end 9))
684 (assert (equal (remove-if-not #'alpha-char-p string :start 0 :end nil)
685 "aaaaa"))
686 (assert (equal (remove-if-not #'alpha-char-p string :start 0 :end 5)
687 "aaaaa"))
688 (assert-error
689 (remove-if-not #'alpha-char-p string :start 0 :end 6))
690 (assert-error
691 (remove-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5))
692 (assert-error
693 (remove-if-not #'alpha-char-p string :start 4 :end 2))
694 (assert-error
695 (remove-if-not #'alpha-char-p string :start 6 :end 9)))
696 (sequence-bounding-indices-test
697 (format t "~&/... DELETE, DELETE-IF, DELETE-IF-NOT")
698 (assert (equal (delete #\a string :start 0 :end nil) ""))
699 (reset)
700 (assert (equal (delete #\a string :start 0 :end 5) ""))
701 (reset)
702 (assert-error (delete #\a string :start 0 :end 6))
703 (reset)
704 (assert-error (delete #\a string :start (opaque-identity -1) :end 5))
705 (reset)
706 (assert-error (delete #\a string :start 4 :end 2))
707 (reset)
708 (assert-error (delete #\a string :start 6 :end 9))
709 (reset)
710 (assert (equal (delete-if #'alpha-char-p string :start 0 :end nil) ""))
711 (reset)
712 (assert (equal (delete-if #'alpha-char-p string :start 0 :end 5) ""))
713 (reset)
714 (assert-error
715 (delete-if #'alpha-char-p string :start 0 :end 6))
716 (reset)
717 (assert-error
718 (delete-if #'alpha-char-p string :start (opaque-identity -1) :end 5))
719 (reset)
720 (assert-error
721 (delete-if #'alpha-char-p string :start 4 :end 2))
722 (reset)
723 (assert-error
724 (delete-if #'alpha-char-p string :start 6 :end 9))
725 (reset)
726 (assert (equal (delete-if-not #'alpha-char-p string :start 0 :end nil)
727 "aaaaa"))
728 (reset)
729 (assert (equal (delete-if-not #'alpha-char-p string :start 0 :end 5)
730 "aaaaa"))
731 (reset)
732 (assert-error
733 (delete-if-not #'alpha-char-p string :start 0 :end 6))
734 (reset)
735 (assert-error
736 (delete-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5))
737 (reset)
738 (assert-error
739 (delete-if-not #'alpha-char-p string :start 4 :end 2))
740 (reset)
741 (assert-error
742 (delete-if-not #'alpha-char-p string :start 6 :end 9)))
744 ;;; Function REMOVE-DUPLICATES, DELETE-DUPLICATES
745 (sequence-bounding-indices-test
746 (format t "~&/Function REMOVE-DUPLICATES, DELETE-DUPLICATES")
747 (assert (string= (remove-duplicates string :start 0 :end 5) "a"))
748 (assert (string= (remove-duplicates string :start 0 :end nil) "a"))
749 (assert-error (remove-duplicates string :start 0 :end 6))
750 (assert-error (remove-duplicates string :start (opaque-identity -1) :end 5))
751 (assert-error (remove-duplicates string :start 4 :end 2))
752 (assert-error (remove-duplicates string :start 6 :end 9))
753 (assert (string= (delete-duplicates string :start 0 :end 5) "a"))
754 (reset)
755 (assert (string= (delete-duplicates string :start 0 :end nil) "a"))
756 (reset)
757 (assert-error (delete-duplicates string :start 0 :end 6))
758 (reset)
759 (assert-error (delete-duplicates string :start (opaque-identity -1) :end 5))
760 (reset)
761 (assert-error (delete-duplicates string :start 4 :end 2))
762 (reset)
763 (assert-error (delete-duplicates string :start 6 :end 9)))
765 ;;; Function REPLACE
766 (sequence-bounding-indices-test
767 (format t "~&/Function REPLACE")
768 (assert (string= (replace string "bbbbb" :start1 0 :end1 5) "bbbbb"))
769 (assert (string= (replace (copy-seq "ccccc")
770 string
771 :start2 0 :end2 nil) "bbbbb"))
772 (assert-error (replace string "ccccc" :start1 0 :end1 6))
773 (assert-error (replace string "ccccc" :start2 (opaque-identity -1) :end2 5))
774 (assert-error (replace string "ccccc" :start1 4 :end1 2))
775 (assert-error (replace string "ccccc" :start1 6 :end1 9)))
777 ;;; Function SEARCH
778 (sequence-bounding-indices-test
779 (format t "~&/Function SEARCH")
780 (assert (= (search "aa" string :start2 0 :end2 5) 0))
781 (assert (null (search string "aa" :start1 0 :end2 nil)))
782 (assert-error (search "aa" string :start2 0 :end2 6))
783 (assert-error (search "aa" string :start2 (opaque-identity -1) :end2 5))
784 (assert-error (search "aa" string :start2 4 :end2 2))
785 (assert-error (search "aa" string :start2 6 :end2 9)))
787 ;;; Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE,
788 ;;; NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE
789 (defmacro string-case-frob (fn)
790 `(progn
791 (assert-error (,fn string :start 0 :end 6))
792 (assert-error (,fn string :start (opaque-identity -1) :end 5))
793 (assert-error (,fn string :start 4 :end 2))
794 (assert-error (,fn string :start 6 :end 9))))
796 (sequence-bounding-indices-test
797 (format t "~&/Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE, ...")
798 (string-case-frob string-upcase)
799 (string-case-frob string-downcase)
800 (string-case-frob string-capitalize)
801 (format t "~&/... NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE")
802 (string-case-frob nstring-upcase)
803 (string-case-frob nstring-downcase)
804 (string-case-frob nstring-capitalize))
806 ;;; Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=,
807 ;;; STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, STRING-GREATERP,
808 ;;; STRING-NOT-GREATERP, STRING-NOT-LESSP
809 (defmacro string-predicate-frob (fn)
810 `(progn
811 (,fn string "abcde" :start1 0 :end1 5)
812 (,fn "fghij" string :start2 0 :end2 nil)
813 (assert-error (,fn string "klmno"
814 :start1 0 :end1 6))
815 (assert-error (,fn "pqrst" string
816 :start2 (opaque-identity -1) :end2 5))
817 (assert-error (,fn "uvwxy" string
818 :start1 4 :end1 2))
819 (assert-error (,fn string "z" :start2 6 :end2 9))))
820 (sequence-bounding-indices-test
821 (format t "~&/Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=, ...")
822 (string-predicate-frob string=)
823 (string-predicate-frob string/=)
824 (string-predicate-frob string<)
825 (string-predicate-frob string>)
826 (string-predicate-frob string<=)
827 (string-predicate-frob string>=))
828 (sequence-bounding-indices-test
829 (format t "~&/... STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, ...")
830 (string-predicate-frob string-equal)
831 (string-predicate-frob string-not-equal)
832 (string-predicate-frob string-lessp))
833 (sequence-bounding-indices-test
834 (format t "~&/... STRING-GREATERP, STRING-NOT-GREATERP, STRING-NOT-LESSP")
835 (string-predicate-frob string-greaterp)
836 (string-predicate-frob string-not-greaterp)
837 (string-predicate-frob string-not-lessp))
839 ;;; Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT,
840 ;;; NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
841 (sequence-bounding-indices-test
842 (format t "~&/Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT, ...")
843 (assert (string= (substitute #\b #\a string :start 0 :end 5) "bbbbb"))
844 (assert (string= (substitute #\c #\a string :start 0 :end nil)
845 "ccccc"))
846 (assert-error (substitute #\b #\a string :start 0 :end 6))
847 (assert-error (substitute #\b #\a string :start (opaque-identity -1) :end 5))
848 (assert-error (substitute #\b #\a string :start 4 :end 2))
849 (assert-error (substitute #\b #\a string :start 6 :end 9))
850 (assert (string= (substitute-if #\b #'alpha-char-p string
851 :start 0 :end 5)
852 "bbbbb"))
853 (assert (string= (substitute-if #\c #'alpha-char-p string
854 :start 0 :end nil)
855 "ccccc"))
856 (assert-error (substitute-if #\b #'alpha-char-p string
857 :start 0 :end 6))
858 (assert-error (substitute-if #\b #'alpha-char-p string
859 :start (opaque-identity -1) :end 5))
860 (assert-error (substitute-if #\b #'alpha-char-p string
861 :start 4 :end 2))
862 (assert-error (substitute-if #\b #'alpha-char-p string
863 :start 6 :end 9))
864 (assert (string= (substitute-if-not #\b #'alpha-char-p string
865 :start 0 :end 5)
866 "aaaaa"))
867 (assert (string= (substitute-if-not #\c #'alpha-char-p string
868 :start 0 :end nil)
869 "aaaaa"))
870 (assert-error (substitute-if-not #\b #'alpha-char-p string
871 :start 0 :end 6))
872 (assert-error (substitute-if-not #\b #'alpha-char-p string
873 :start (opaque-identity -1) :end 5))
874 (assert-error (substitute-if-not #\b #'alpha-char-p string
875 :start 4 :end 2))
876 (assert-error (substitute-if-not #\b #'alpha-char-p string
877 :start 6 :end 9)))
878 (sequence-bounding-indices-test
879 (format t "~&/... NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT")
880 (assert (string= (nsubstitute #\b #\a string :start 0 :end 5) "bbbbb"))
881 (reset)
882 (assert (string= (nsubstitute #\c #\a string :start 0 :end nil)
883 "ccccc"))
884 (reset)
885 (assert-error (nsubstitute #\b #\a string :start 0 :end 6))
886 (reset)
887 (assert-error (nsubstitute #\b #\a string :start (opaque-identity -1) :end 5))
888 (reset)
889 (assert-error (nsubstitute #\b #\a string :start 4 :end 2))
890 (reset)
891 (assert-error (nsubstitute #\b #\a string :start 6 :end 9))
892 (reset)
893 (assert (string= (nsubstitute-if #\b #'alpha-char-p string
894 :start 0 :end 5)
895 "bbbbb"))
896 (reset)
897 (assert (string= (nsubstitute-if #\c #'alpha-char-p string
898 :start 0 :end nil)
899 "ccccc"))
900 (reset)
901 (assert-error (nsubstitute-if #\b #'alpha-char-p string
902 :start 0 :end 6))
903 (reset)
904 (assert-error (nsubstitute-if #\b #'alpha-char-p string
905 :start (opaque-identity -1) :end 5))
906 (reset)
907 (assert-error (nsubstitute-if #\b #'alpha-char-p string
908 :start 4 :end 2))
909 (reset)
910 (assert-error (nsubstitute-if #\b #'alpha-char-p string
911 :start 6 :end 9))
912 (reset)
913 (assert (string= (nsubstitute-if-not #\b #'alpha-char-p string
914 :start 0 :end 5)
915 "aaaaa"))
916 (reset)
917 (assert (string= (nsubstitute-if-not #\c #'alpha-char-p string
918 :start 0 :end nil)
919 "aaaaa"))
920 (reset)
921 (assert-error (nsubstitute-if-not #\b #'alpha-char-p string
922 :start 0 :end 6))
923 (reset)
924 (assert-error (nsubstitute-if-not #\b #'alpha-char-p string
925 :start (opaque-identity -1) :end 5))
926 (reset)
927 (assert-error (nsubstitute-if-not #\b #'alpha-char-p string
928 :start 4 :end 2))
929 (reset)
930 (assert-error (nsubstitute-if-not #\b #'alpha-char-p string
931 :start 6 :end 9)))
932 ;;; Function WRITE-STRING, WRITE-LINE
933 (sequence-bounding-indices-test
934 (format t "~&/Function WRITE-STRING, WRITE-LINE")
935 (write-string string *standard-output* :start 0 :end 5)
936 (write-string string *standard-output* :start 0 :end nil)
937 (assert-error (write-string string *standard-output*
938 :start 0 :end 6))
939 (assert-error (write-string string *standard-output*
940 :start (opaque-identity -1) :end 5))
941 (assert-error (write-string string *standard-output*
942 :start 4 :end 2))
943 (assert-error (write-string string *standard-output*
944 :start 6 :end 9))
945 (write-line string *standard-output* :start 0 :end 5)
946 (write-line string *standard-output* :start 0 :end nil)
947 (assert-error (write-line string *standard-output*
948 :start 0 :end 6))
949 (assert-error (write-line string *standard-output*
950 :start (opaque-identity -1) :end 5))
951 (assert-error (write-line string *standard-output*
952 :start 4 :end 2))
953 (assert-error (write-line string *standard-output*
954 :start 6 :end 9)))
956 ;;; Macro WITH-INPUT-FROM-STRING
957 (sequence-bounding-indices-test
958 (format t "~&/Macro WITH-INPUT-FROM-STRING")
959 (with-input-from-string (s string :start 0 :end 5)
960 (assert (char= (read-char s) #\a)))
961 (with-input-from-string (s string :start 0 :end nil)
962 (assert (char= (read-char s) #\a)))
963 (assert-error
964 (with-input-from-string (s string :start 0 :end 6)
965 (read-char s)))
966 (assert-error
967 (with-input-from-string (s string :start (opaque-identity -1) :end 5)
968 (read-char s)))
969 (assert-error
970 (with-input-from-string (s string :start 4 :end 2)
971 (read-char s)))
972 (assert-error
973 (with-input-from-string (s string :start 6 :end 9)
974 (read-char s))))
976 ;;; testing bit-bashing according to _The Practice of Programming_
977 (defun fill-bytes-for-testing (bitsize)
978 "Return a list of 'bytes' of type (MOD BITSIZE)."
979 (remove-duplicates (list 0
980 (1- (ash 1 (1- bitsize)))
981 (ash 1 (1- bitsize))
982 (1- (ash 1 bitsize)))))
984 (defun fill-with-known-value (value size &rest vectors)
985 (dolist (vec vectors)
986 (dotimes (i size)
987 (setf (aref vec i) value))))
989 (defun collect-fill-amounts (n-power)
990 (remove-duplicates
991 (loop for i from 0 upto n-power
992 collect (1- (expt 2 i))
993 collect (expt 2 i)
994 collect (1+ (expt 2 i)))))
996 (defun test-fill-bashing (bitsize padding-amount n-power)
997 (let* ((size (+ (* padding-amount 2) (expt 2 n-power) (* padding-amount 2)))
998 (standard (make-array size :element-type `(unsigned-byte ,bitsize)))
999 (bashed (make-array size :element-type `(unsigned-byte ,bitsize)))
1000 (fill-amounts (collect-fill-amounts n-power))
1001 (bash-function (intern (format nil "UB~A-BASH-FILL" bitsize)
1002 (find-package "SB-KERNEL"))))
1003 (format t "~&/Function ~A..." bash-function)
1004 (loop for offset from padding-amount below (* 2 padding-amount) do
1005 (dolist (c (fill-bytes-for-testing bitsize))
1006 (dolist (n fill-amounts)
1007 (fill-with-known-value (mod (lognot c) (ash 1 bitsize)) n
1008 standard bashed)
1009 ;; fill vectors
1010 ;; a) the standard slow way
1011 (locally (declare (notinline fill))
1012 (fill standard c :start offset :end (+ offset n)))
1013 ;; b) the blazingly fast way
1014 (let ((value (loop for i from 0 by bitsize
1015 until (= i sb-vm:n-word-bits)
1016 sum (ash c i))))
1017 (funcall bash-function value bashed offset n))
1018 ;; check for errors
1019 (when (mismatch standard bashed)
1020 (format t "Test with offset ~A, fill ~A and length ~A failed.~%"
1021 offset c n)
1022 (format t "Mismatch: ~A ~A~%"
1023 (subseq standard 0 (+ offset n 1))
1024 (subseq bashed 0 (+ offset n 1)))
1025 (return-from test-fill-bashing nil))))
1026 finally (return t))))
1028 (defun test-copy-bashing (bitsize padding-amount n-power)
1029 (let* ((size (+ (* padding-amount 2) (expt 2 n-power) (* padding-amount 2)))
1030 (standard-dst (make-array size :element-type `(unsigned-byte ,bitsize)))
1031 (bashed-dst (make-array size :element-type `(unsigned-byte ,bitsize)))
1032 (source (make-array size :element-type `(unsigned-byte ,bitsize)))
1033 (fill-amounts (collect-fill-amounts n-power))
1034 (bash-function (intern (format nil "UB~A-BASH-COPY" bitsize)
1035 (find-package "SB-KERNEL"))))
1036 (format t "~&/Function ~A..." bash-function)
1037 (do ((source-offset padding-amount (1+ source-offset)))
1038 ((>= source-offset (* padding-amount 2))
1039 ;; success!
1041 (do ((target-offset padding-amount (1+ target-offset)))
1042 ((>= target-offset (* padding-amount 2)))
1043 (dolist (c (fill-bytes-for-testing bitsize))
1044 (dolist (n fill-amounts)
1045 (fill-with-known-value (mod (lognot c) (ash 1 bitsize)) size
1046 source standard-dst bashed-dst)
1047 ;; fill with test data
1048 (fill source c :start source-offset :end (+ source-offset n))
1049 ;; copy filled test data to test vectors
1050 ;; a) the slow way
1051 (replace standard-dst source
1052 :start1 target-offset :end1 (+ target-offset n)
1053 :start2 source-offset :end2 (+ source-offset n))
1054 ;; b) the blazingly fast way
1055 (funcall bash-function source source-offset
1056 bashed-dst target-offset n)
1057 ;; check for errors
1058 (when (mismatch standard-dst bashed-dst)
1059 (format t "Test with target-offset ~A, source-offset ~A, fill ~A, and length ~A failed.~%"
1060 target-offset source-offset c n)
1061 (format t "Mismatch:~% correct ~A~% actual ~A~%"
1062 standard-dst
1063 bashed-dst)
1064 (return-from test-copy-bashing nil))))))))
1066 ;; Too slow for the interpreter
1067 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
1068 (loop for i = 1 then (* i 2) do
1069 ;; the bare '13' here is fairly arbitrary, except that it's been
1070 ;; reduced from '32', which made the tests take aeons; '8' provides
1071 ;; a good range of lengths over which to fill and copy, which
1072 ;; should tease out most errors in the code (if any exist). (It
1073 ;; also makes this part of the test suite finish reasonably
1074 ;; quickly.)
1075 (assert (time (test-fill-bashing i 13 8)))
1076 (assert (time (test-copy-bashing i 13 8)))
1077 until (= i sb-vm:n-word-bits))
1079 (defun test-inlined-bashing (bitsize)
1080 ;; We have to compile things separately for each bitsize so the
1081 ;; compiler will work out the array type and trigger the REPLACE
1082 ;; transform.
1083 (let ((lambda-form
1084 `(lambda ()
1085 (let* ((n-elements-per-word ,(truncate sb-vm:n-word-bits bitsize))
1086 (size (* 3 n-elements-per-word))
1087 (standard-dst (make-array size :element-type '(unsigned-byte ,bitsize)))
1088 (bashed-dst (make-array size :element-type '(unsigned-byte ,bitsize)))
1089 (source (make-array size :element-type '(unsigned-byte ,bitsize))))
1090 (declare (type (simple-array (unsigned-byte ,bitsize) (*))
1091 source standard-dst bashed-dst))
1092 (do ((i 0 (1+ i))
1093 (offset n-elements-per-word (1+ offset)))
1094 ((>= offset (* 2 n-elements-per-word)) t)
1095 (dolist (c (fill-bytes-for-testing ,bitsize))
1096 (fill-with-known-value (mod (lognot c) (ash 1 ,bitsize)) size
1097 source standard-dst bashed-dst)
1098 ;; fill with test-data
1099 (fill source c :start offset :end (+ offset n-elements-per-word))
1100 ;; copy filled data to test vectors
1102 ;; a) the slow way (which is actually fast, since this
1103 ;; should be transformed into UB*-BASH-COPY)
1104 (replace standard-dst source
1105 :start1 (- offset n-elements-per-word i)
1106 :start2 (- offset n-elements-per-word i)
1107 :end1 offset :end2 offset)
1108 ;; b) the fast way--we fold the
1109 ;; :START{1,2} arguments above ourselves
1110 ;; to trigger the REPLACE transform
1111 (replace bashed-dst source
1112 :start1 0 :start2 0 :end1 offset :end2 offset)
1113 ;; check for errors
1114 (when (or (mismatch standard-dst bashed-dst)
1115 ;; trigger COPY-SEQ transform
1116 (mismatch (copy-seq standard-dst) bashed-dst)
1117 ;; trigger SUBSEQ transform
1118 (mismatch (subseq standard-dst (- offset n-elements-per-word i))
1119 bashed-dst))
1120 (format t "Test with target-offset ~A, source-offset ~A, fill ~A, and length ~A failed.~%"
1121 0 0 c offset)
1122 (format t "Mismatch:~% correct ~A~% actual ~A~%"
1123 standard-dst
1124 bashed-dst)
1125 (return-from nil nil))))))))
1126 (funcall (checked-compile lambda-form))))
1128 #+#.(cl:if (cl:eq sb-ext:*evaluator-mode* :compile) '(and) '(or))
1129 (loop for i = 1 then (* i 2) do
1130 (assert (test-inlined-bashing i))
1131 until (= i sb-vm:n-word-bits))
1133 ;;; tests from the Sacla test suite via Eric Marsden, 2007-05-07
1134 (remove-duplicates (vector 1 2 2 1) :test-not (lambda (a b) (not (= a b))))
1136 (delete-duplicates (vector #\a #\b #\c #\a)
1137 :test-not (lambda (a b) (not (char-equal a b))))
1139 ;;; FILL on lists
1140 (let ((l (list 1 2 3)))
1141 (assert (eq l (fill l 0 :start 1 :end 2)))
1142 (assert (equal l '(1 0 3)))
1143 (assert (eq l (fill l 'x :start 2 :end 3)))
1144 (assert (equal l '(1 0 x)))
1145 (assert (eq l (fill l 'y :start 1)))
1146 (assert (equal l '(1 y y)))
1147 (assert (eq l (fill l 'z :end 2)))
1148 (assert (equal l '(z z y)))
1149 (assert (eq l (fill l 1)))
1150 (assert (equal l '(1 1 1)))
1151 (assert-error (fill l 0 :start 4))
1152 (assert-error (fill l 0 :end 4))
1153 (assert-error (fill l 0 :start 2 :end 1)))
1155 ;;; Both :TEST and :TEST-NOT provided
1156 (with-test (:name :test-and-test-not-to-adjoin)
1157 (multiple-value-bind (fun failure-p warnings)
1158 (checked-compile `(lambda (item test test-not)
1159 (adjoin item '(1 2 3 :foo)
1160 :test test
1161 :test-not test-not))
1162 :allow-warnings t)
1163 (declare (ignore failure-p))
1164 (assert (= 1 (length warnings)))
1165 (assert-error (funcall fun 1 #'eql (complement #'eql)))))
1167 ;;; tests of deftype types equivalent to STRING or SIMPLE-STRING
1168 (deftype %string () 'string)
1169 (deftype %simple-string () 'simple-string)
1170 (deftype string-3 () '(string 3))
1171 (deftype simple-string-3 () '(simple-string 3))
1173 (with-test (:name :user-defined-string-types-map-etc)
1174 (dolist (type '(%string %simple-string string-3 simple-string-3))
1175 (assert (string= "foo" (coerce '(#\f #\o #\o) type)))
1176 (assert (string= "foo" (map type 'identity #(#\f #\o #\o))))
1177 (assert (string= "foo" (merge type (copy-seq '(#\o)) (copy-seq '(#\f #\o))
1178 'char<)))
1179 (assert (string= "foo" (concatenate type '(#\f) "oo")))
1180 (assert (string= "ooo" (make-sequence type 3 :initial-element #\o)))))
1181 (with-test (:name :user-defined-string-types-map-etc-error)
1182 (dolist (type '(string-3 simple-string-3))
1183 (assert-error (coerce '(#\q #\u #\u #\x) type))
1184 (assert-error (map type 'identity #(#\q #\u #\u #\x)))
1185 (assert-error (merge type (copy-seq '(#\q #\x)) (copy-seq "uu") 'char<))
1186 (assert-error (concatenate type "qu" '(#\u #\x)))
1187 (assert-error (make-sequence type 4 :initial-element #\u))))
1189 (defun test-bit-position (size set start end from-end res)
1190 (let ((v (make-array size :element-type 'bit :initial-element 0)))
1191 (dolist (i set)
1192 (setf (bit v i) 1))
1193 (dolist (f (list (checked-compile
1194 `(lambda (b v s e fe)
1195 (position b (the bit-vector v) :start s :end e :from-end fe)))
1196 (checked-compile
1197 `(lambda (b v s e fe)
1198 (assert (eql b 1))
1199 (position 1 (the bit-vector v) :start s :end e :from-end fe)))
1200 (checked-compile
1201 `(lambda (b v s e fe)
1202 (position b (the vector v) :start s :end e :from-end fe)))))
1203 (let ((got (funcall f 1 v start end from-end)))
1204 (unless (eql res got)
1205 (cerror "Continue" "POSITION 1, Wanted ~S, got ~S.~% size = ~S, set = ~S, from-end = ~S"
1206 res got
1207 size set from-end)))))
1208 (let ((v (make-array size :element-type 'bit :initial-element 1)))
1209 (dolist (i set)
1210 (setf (bit v i) 0))
1211 (dolist (f (list (checked-compile
1212 `(lambda (b v s e fe)
1213 (position b (the bit-vector v) :start s :end e :from-end fe)))
1214 (checked-compile
1215 `(lambda (b v s e fe)
1216 (assert (eql b 0))
1217 (position 0 (the bit-vector v) :start s :end e :from-end fe)))
1218 (checked-compile
1219 `(lambda (b v s e fe)
1220 (position b (the vector v) :start s :end e :from-end fe)))))
1221 (let ((got (funcall f 0 v start end from-end)))
1222 (unless (eql res got)
1223 (cerror "Continue" "POSITION 0, Wanted ~S, got ~S.~% size = ~S, set = ~S, from-end = ~S"
1224 res got
1225 size set from-end))))))
1227 (defun random-test-bit-position (n)
1228 (loop repeat n
1229 do (let* ((vector (make-array (+ 2 (random 5000)) :element-type 'bit))
1230 (offset (random (1- (length vector))))
1231 (size (1+ (random (- (length vector) offset))))
1232 (disp (make-array size :element-type 'bit :displaced-to vector
1233 :displaced-index-offset offset)))
1234 (assert (plusp size))
1235 (loop repeat 10
1236 do (setf (bit vector (random (length vector))) 1))
1237 (flet ((test (orig)
1238 (declare (bit-vector orig))
1239 (let ((copy (coerce orig 'simple-vector))
1240 (p0 (random (length orig)))
1241 (p1 (1+ (random (length orig)))))
1242 (multiple-value-bind (s e)
1243 (if (> p1 p0)
1244 (values p0 p1)
1245 (values p1 p0))
1246 (assert (eql (position 1 copy :start s :end e)
1247 (position 1 orig :start s :end e)))
1248 (assert (eql (position 1 copy :start s :end e :from-end t)
1249 (position 1 orig :start s :end e :from-end t)))))))
1250 (test vector)
1251 (test disp)))))
1253 (with-test (:name :bit-position)
1254 (test-bit-position 0 (list) 0 0 nil nil)
1255 (test-bit-position 0 (list) 0 0 t nil)
1256 (test-bit-position 1 (list 0) 0 0 nil nil)
1257 (test-bit-position 1 (list 0) 0 0 t nil)
1258 (test-bit-position 1 (list 0) 0 1 nil 0)
1259 (test-bit-position 1 (list 0) 0 1 t 0)
1260 (test-bit-position 10 (list 0 1) 0 1 nil 0)
1261 (test-bit-position 10 (list 0 1) 1 1 nil nil)
1262 (test-bit-position 10 (list 0 1) 0 1 t 0)
1263 (test-bit-position 10 (list 0 1) 1 1 t nil)
1264 (test-bit-position 10 (list 0 3) 1 4 nil 3)
1265 (test-bit-position 10 (list 0 3) 1 4 t 3)
1266 (test-bit-position 10 (list 0 3 6) 1 10 nil 3)
1267 (test-bit-position 10 (list 0 3 6) 1 10 t 6)
1268 (test-bit-position 1000 (list 128 700) 20 500 nil 128)
1269 (test-bit-position 1000 (list 128 700) 20 500 t 128)
1270 (test-bit-position 1000 (list 423 762) 200 800 nil 423)
1271 (test-bit-position 1000 (list 423 762) 200 800 t 762)
1272 (test-bit-position 1000 (list 298 299) 100 400 nil 298)
1273 (test-bit-position 1000 (list 298 299) 100 400 t 299))
1275 (with-test (:name (:bit-position :random-test))
1276 (random-test-bit-position 10000))
1278 ;; REVERSE and NREVERSE should assert that the returned value
1279 ;; from a generic sequence operation is of type SEQUENCE.
1280 (defclass bogus-reversal-seq (sequence standard-object) ())
1281 (defmethod sequence:reverse ((self bogus-reversal-seq))
1282 #2a((x y) (1 2)))
1283 (with-test (:name :generic-sequence-reverse)
1284 (assert-error (reverse (make-instance 'bogus-reversal-seq))))
1286 (with-test (:name :abstract-base-sequence-satisfies-sequencep)
1287 (assert (typep (sb-pcl::class-prototype (find-class 'sequence)) 'sequence)))
1289 (defvar *macro-invocations* 0)
1290 ;; in case someone adds more tests after this, don't mess up OPAQUE-IDENTITY
1291 (defun opaque-id-again (x) x)
1292 (define-compiler-macro opaque-id-again (x) (incf *macro-invocations*) x)
1293 (with-test (:name :mapfoo-admits-compiler-macros)
1294 (checked-compile '(lambda (l) (mapcar #'opaque-id-again l)))
1295 (assert (= *macro-invocations* 1))
1296 (checked-compile '(lambda (l) (some #'opaque-id-again l)))
1297 (assert (= *macro-invocations* 2)))
1299 ;;; success