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