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
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
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 "assertoid.lisp")
19 (:use :cl :assertoid))
21 (in-package :seq-test)
23 ;;; helper functions for exercising SEQUENCE code on data of many
24 ;;; specialized types, and in many different optimization scenarios
25 (defun for-every-seq-1 (base-seq snippet)
26 (dolist (seq-type '(list
29 (simple-array character 1)
31 (simple-array (signed-byte 4) 1)
32 (vector (signed-byte 4))))
33 (flet ((entirely (eltype)
34 (every (lambda (el) (typep el eltype)) base-seq)))
35 (dolist (declaredness '(nil t))
36 (dolist (optimization '(((speed 3) (space 0))
39 ((speed 0) (space 1))))
40 (let* ((seq (if (eq seq-type 'list)
41 (coerce base-seq 'list)
42 (destructuring-bind (type-first &rest type-rest)
46 (destructuring-bind (eltype one) type-rest
49 (coerce base-seq seq-type)
52 (destructuring-bind (eltype) type-rest
54 (let ((initial-element
55 (cond ((subtypep eltype 'character)
57 ((subtypep eltype 'number)
70 (lambda-expr `(lambda (seq)
72 `((declare (type ,seq-type seq))))
73 (declare (optimize ,@optimization))
75 (format t "~&~S~%" lambda-expr)
76 (multiple-value-bind (fun warnings-p failure-p)
77 (compile nil lambda-expr)
78 (when (or warnings-p failure-p)
79 (error "~@<failed compilation:~2I ~_LAMBDA-EXPR=~S ~_WARNINGS-P=~S ~_FAILURE-P=~S~:@>"
80 lambda-expr warnings-p failure-p))
81 (format t "~&~S ~S~%~S~%~S ~S~%"
82 base-seq snippet seq-type declaredness optimization)
83 (format t "~&(TYPEP SEQ 'SIMPLE-ARRAY)=~S~%"
84 (typep seq 'simple-array))
85 (unless (funcall fun seq)
86 (error "~@<failed test:~2I ~_BASE-SEQ=~S ~_SNIPPET=~S ~_SEQ-TYPE=~S ~_DECLAREDNESS=~S ~_OPTIMIZATION=~S~:@>"
92 (defun for-every-seq (base-seq snippets)
93 (dolist (snippet snippets)
94 (for-every-seq-1 base-seq snippet)))
96 ;;; a wrapper to hide declared type information from the compiler, so
97 ;;; we don't get stopped by compiler warnings about e.g. compiling
98 ;;; (POSITION 1 #() :KEY #'ABS) when #() has been coerced to a string.
99 (defun indiscriminate (fun)
100 (lambda (&rest rest) (apply fun rest)))
102 ;;; asymmetric test arg order example from ANSI FIND definition page
103 (assert (eql #\space ; original example, depends on ASCII character ordering
104 (find #\d "here are some letters that can be looked at"
106 (assert (eql #\e ; modified example, depends only on standard a-z ordering
107 (find #\f "herearesomeletters" :test #'char>)))
108 (assert (eql 4 ; modified more, avoids charset technicalities completely
109 (find 5 '(6 4) :test '>)))
111 ;;; tests of FIND, POSITION, FIND-IF, and POSITION-IF (and a few for
112 ;;; deprecated FIND-IF-NOT and POSITION-IF-NOT too)
114 '((null (find 1 seq))
115 (null (find 1 seq :from-end t))
116 (null (position 1 seq :key (indiscriminate #'abs)))
117 (null (position nil seq :test (constantly t)))
118 (null (position nil seq :test nil))
119 (null (position nil seq :test-not nil))
120 (null (find-if #'1+ seq :key (indiscriminate #'log)))
121 (null (position-if #'identity seq :from-end t))
122 (null (find-if-not #'packagep seq))
123 (null (position-if-not #'packagep seq :key nil))))
125 '((null (find 2 seq))
126 ;; Get the argument ordering for asymmetric tests like #'> right.
127 ;; (bug reported and fixed by Alexey Dejneka sbcl-devel 2001-10-17)
128 (eql 1 (find 2 seq :test #'>))
129 (find 2 seq :key #'1+)
130 (find 1 seq :from-end t)
131 (null (find 1 seq :from-end t :start 1))
132 (null (find 0 seq :from-end t))
133 (eql 0 (position 1 seq :key #'abs))
134 (null (position nil seq :test 'equal))
135 (eql 1 (find-if #'1- seq :key #'log))
136 (eql 0 (position-if #'identity seq :from-end t))
137 (null (find-if-not #'sin seq))
138 (eql 0 (position-if-not #'packagep seq :key 'identity))))
139 (for-every-seq #(1 2 3 2 1)
141 (find 3 seq :from-end 'yes)
142 (eql 1 (position 1.5 seq :test #'<))
143 (eql 0 (position 0 seq :key '1-))
144 (eql 4 (position 0 seq :key '1- :from-end t))
145 (eql 2 (position 4 seq :key '1+))
146 (eql 2 (position 4 seq :key '1+ :from-end t))
147 (eql 1 (position 2 seq))
148 (eql 1 (position 2 seq :start 1))
149 (null (find 2 seq :start 1 :end 1))
150 (eql 3 (position 2 seq :start 2))
151 (eql 3 (position 2 seq :key nil :from-end t))
152 (eql 2 (position 3 seq :test '=))
153 (eql 0 (position 3 seq :test-not 'equalp))
154 (eql 2 (position 3 seq :test 'equal :from-end t))
155 (null (position 4 seq :test #'eql))
156 (null (find-if #'packagep seq))
157 (eql 1 (find-if #'plusp seq))
158 (eql 3 (position-if #'plusp seq :key #'1- :from-end t))
159 (eql 1 (position-if #'evenp seq))
160 (eql 3 (position-if #'evenp seq :from-end t))
161 (eql 2 (position-if #'plusp seq :from-end nil :key '1- :start 2))
162 (eql 3 (position-if #'plusp seq :from-end t :key '1- :start 2))
163 (null (position-if #'plusp seq :from-end t :key '1- :start 2 :end 2))
164 (null (find-if-not #'plusp seq))
165 (eql 0 (position-if-not #'evenp seq))))
166 (for-every-seq "string test"
167 '((null (find 0 seq))
168 (null (find #\D seq :key #'char-upcase))
169 (find #\E seq :key #'char-upcase)
170 (null (find #\e seq :key #'char-upcase))
171 (eql 3 (position #\i seq))
172 (eql 0 (position #\s seq :key #'char-downcase))
173 (eql 1 (position #\s seq :key #'char-downcase :test #'char/=))
174 (eql 9 (position #\s seq :from-end t :test #'char=))
175 (eql 10 (position #\s seq :from-end t :test #'char/=))
176 (eql 4 (position #\N seq :from-end t :key 'char-upcase :test #'char-equal))
177 (eql 5 (position-if (lambda (c) (equal #\g c)) seq))
178 (eql 5 (position-if (lambda (c) (equal #\g c)) seq :from-end t))
179 (find-if #'characterp seq)
180 (find-if (lambda (c) (typep c 'base-char)) seq :from-end t)
181 (null (find-if 'upper-case-p seq))))
184 (let ((avec (make-array 10
186 :initial-contents '(0 1 2 3 iv v vi vii iix ix))))
187 ;; These first five always worked AFAIK.
188 (assert (equalp (subseq avec 0 3) #(0 1 2)))
189 (assert (equalp (subseq avec 3 3) #()))
190 (assert (equalp (subseq avec 1 3) #(1 2)))
191 (assert (equalp (subseq avec 1) #(1 2 3)))
192 (assert (equalp (subseq avec 1 4) #(1 2 3)))
193 ;; SBCL bug found ca. 2002-05-01 by OpenMCL's correct handling of
194 ;; SUBSEQ, CSR's driving portable cross-compilation far enough to
195 ;; reach the SUBSEQ calls in assem.lisp, and WHN's sleazy
196 ;; translation of old CMU CL new-assem.lisp into sufficiently grotty
197 ;; portable Lisp that it passed suitable illegal values to SUBSEQ to
198 ;; exercise the bug:-|
200 ;; SUBSEQ should check its END value against logical LENGTH, not
201 ;; physical ARRAY-DIMENSION 0.
203 ;; fixed in sbcl-0.7.4.22 by WHN
204 (assert (null (ignore-errors (aref (subseq avec 1 5) 0)))))
207 (defun test-fill-typecheck (x)
208 (declare (optimize (safety 3) (space 2) (speed 1)))
209 (fill (make-string 10) x))
211 (assert (string= (test-fill-typecheck #\@) "@@@@@@@@@@"))
212 ;;; BUG 186, fixed in sbcl-0.7.5.5
213 (assert (null (ignore-errors (test-fill-typecheck 4097))))
215 ;;; MAKE-SEQUENCE, COERCE, CONCATENATE, MERGE, MAP and requested
216 ;;; result type (BUGs 46a, 46b, 66)
217 (macrolet ((assert-type-error (form)
218 `(assert (typep (nth-value 1 (ignore-errors ,form))
220 (dolist (type-stub '((simple-vector)
222 (vector (signed-byte 8))
223 (vector (unsigned-byte 16))
224 (vector (signed-byte 32))
225 (simple-bit-vector)))
226 (declare (optimize safety))
227 (format t "~&~S~%" type-stub)
229 (assert (= (length (make-sequence `(,@type-stub) 10)) 10))
230 (assert (= (length (make-sequence `(,@type-stub 10) 10)) 10))
231 (assert-type-error (make-sequence `(,@type-stub 10) 11))
233 (assert (= (length (coerce '(0 0 0) `(,@type-stub))) 3))
234 (assert (= (length (coerce #(0 0 0) `(,@type-stub 3))) 3))
235 (assert-type-error (coerce #*111 `(,@type-stub 4)))
237 (assert (= (length (concatenate `(,@type-stub) #(0 0 0) #*111)) 6))
238 (assert (equalp (concatenate `(,@type-stub) #(0 0 0) #*111)
239 (coerce #(0 0 0 1 1 1) `(,@type-stub))))
240 (assert (= (length (concatenate `(,@type-stub 6) #(0 0 0) #*111)) 6))
241 (assert (equalp (concatenate `(,@type-stub 6) #(0 0 0) #*111)
242 (coerce #(0 0 0 1 1 1) `(,@type-stub 6))))
243 (assert-type-error (concatenate `(,@type-stub 5) #(0 0 0) #*111))
245 (assert (= (length (merge `(,@type-stub) #(0 1 0) #*111 #'>)) 6))
246 (assert (equalp (merge `(,@type-stub) #(0 1 0) #*111 #'>)
247 (coerce #(1 1 1 0 1 0) `(,@type-stub))))
248 (assert (= (length (merge `(,@type-stub 6) #(0 1 0) #*111 #'>)) 6))
249 (assert (equalp (merge `(,@type-stub 6) #(0 1 0) #*111 #'>)
250 (coerce #(1 1 1 0 1 0) `(,@type-stub 6))))
251 (assert-type-error (merge `(,@type-stub 4) #(0 1 0) #*111 #'>))
253 (assert (= (length (map `(,@type-stub) #'logxor #(0 0 1 1) '(0 1 0 1))) 4))
254 (assert (equalp (map `(,@type-stub) #'logxor #(0 0 1 1) '(0 1 0 1))
255 (coerce #(0 1 1 0) `(,@type-stub))))
256 (assert (= (length (map `(,@type-stub 4) #'logxor #(0 0 1 1) '(0 1 0 1)))
258 (assert (equalp (map `(,@type-stub 4) #'logxor #(0 0 1 1) '(0 1 0 1))
259 (coerce #(0 1 1 0) `(,@type-stub 4))))
260 (assert-type-error (map `(,@type-stub 5) #'logxor #(0 0 1 1) '(0 1 0 1))))
261 ;; some more CONCATENATE tests for strings
263 (declare (optimize safety))
264 (assert (string= (concatenate 'string "foo" " " "bar") "foo bar"))
265 (assert (string= (concatenate '(string 7) "foo" " " "bar") "foo bar"))
266 (assert-type-error (concatenate '(string 6) "foo" " " "bar"))
267 (assert (string= (concatenate '(string 6) "foo" #(#\b #\a #\r)) "foobar"))
268 (assert-type-error (concatenate '(string 7) "foo" #(#\b #\a #\r))))
269 ;; Non-VECTOR ARRAY types aren't allowed as vector type specifiers.
271 (declare (optimize safety))
272 (assert-type-error (concatenate 'simple-array "foo" "bar"))
273 (assert-type-error (map 'simple-array #'identity '(1 2 3)))
274 (assert (equalp #(11 13)
275 (map '(simple-array fixnum (*)) #'+ '(1 2 3) '(10 11))))
276 (assert-type-error (coerce '(1 2 3) 'simple-array))
277 (assert-type-error (merge 'simple-array '(1 3) '(2 4) '<))
278 (assert (equalp #(3 2 1) (coerce '(3 2 1) '(vector fixnum))))
279 (assert-type-error (map 'array #'identity '(1 2 3)))
280 (assert-type-error (map '(array fixnum) #'identity '(1 2 3)))
281 (assert (equalp #(1 2 3) (coerce '(1 2 3) '(vector fixnum))))
282 ;; but COERCE has an exemption clause:
283 (assert (string= "foo" (coerce "foo" 'simple-array)))
284 ;; ... though not in all cases.
285 (assert-type-error (coerce '(#\f #\o #\o) 'simple-array))))
287 ;;; As pointed out by Raymond Toy on #lisp IRC, MERGE had some issues
288 ;;; with user-defined types until sbcl-0.7.8.11
289 (deftype list-typeoid () 'list)
290 (assert (equal '(1 2 3 4) (merge 'list-typeoid '(1 3) '(2 4) '<)))
291 ;;; and also with types that weren't precicely LIST
292 (assert (equal '(1 2 3 4) (merge 'cons '(1 3) '(2 4) '<)))
294 ;;; but wait, there's more! The NULL and CONS types also have implicit
295 ;;; length requirements:
296 (macrolet ((assert-type-error (form)
297 `(assert (typep (nth-value 1 (ignore-errors ,form))
300 (declare (optimize safety))
302 (assert-type-error (make-sequence 'cons 0))
303 (assert-type-error (make-sequence 'null 1))
304 (assert-type-error (make-sequence '(cons t null) 0))
305 (assert-type-error (make-sequence '(cons t null) 2))
306 ;; KLUDGE: I'm not certain that this test actually tests for what
307 ;; it should test, in that the type deriver and optimizers might
308 ;; be too smart for the good of an exhaustive test system.
309 ;; However, it makes me feel good. -- CSR, 2002-10-18
310 (assert (null (make-sequence 'null 0)))
311 (assert (= (length (make-sequence 'cons 3)) 3))
312 (assert (= (length (make-sequence '(cons t null) 1)) 1))
313 ;; and NIL is not a valid type for MAKE-SEQUENCE
314 (assert-type-error (make-sequence 'nil 0))
316 (assert-type-error (coerce #(1) 'null))
317 (assert-type-error (coerce #() 'cons))
318 (assert-type-error (coerce #() '(cons t null)))
319 (assert-type-error (coerce #(1 2) '(cons t null)))
320 (assert (null (coerce #() 'null)))
321 (assert (= (length (coerce #(1) 'cons)) 1))
322 (assert (= (length (coerce #(1) '(cons t null))) 1))
323 (assert-type-error (coerce #() 'nil))
325 (assert-type-error (merge 'null '(1 3) '(2 4) '<))
326 (assert-type-error (merge 'cons () () '<))
327 (assert (null (merge 'null () () '<)))
328 (assert (= (length (merge 'cons '(1 3) '(2 4) '<)) 4))
329 (assert (= (length (merge '(cons t (cons t (cons t (cons t null))))
330 '(1 3) '(2 4) '<)) 4))
331 (assert-type-error (merge 'nil () () '<))
333 (assert-type-error (concatenate 'null '(1) "2"))
334 (assert-type-error (concatenate 'cons #() ()))
335 (assert-type-error (concatenate '(cons t null) #(1 2 3) #(4 5 6)))
336 (assert (null (concatenate 'null () #())))
337 (assert (= (length (concatenate 'cons #() '(1) "2 3")) 4))
338 (assert (= (length (concatenate '(cons t cons) '(1) "34")) 3))
339 (assert-type-error (concatenate 'nil '(3)))
340 ;; FIXME: tests for MAP to come when some brave soul implements
341 ;; the analogous type checking for MAP/%MAP.
344 ;;; ELT should signal an error of type TYPE-ERROR if its index
345 ;;; argument isn't a valid sequence index for sequence:
346 (defun test-elt-signal (x)
348 (assert (raises-error? (test-elt-signal "foo") type-error))
349 (assert (eql (test-elt-signal "foob") #\b))
351 (declare (optimize (safety 3)))
352 (assert (raises-error? (elt (list 1 2 3) 3) type-error)))
354 ;;; confusion in the refactoring led to this signalling an unbound
355 ;;; variable, not a type error.
356 (defun svrefalike (x)
358 (assert (raises-error? (svrefalike #*0) type-error))
360 ;;; checks for uniform bounding index handling under SAFETY 3 code.
362 ;;; KLUDGE: not all in one big form because that causes SBCL to spend
363 ;;; an absolute age trying to compile it.
364 (defmacro sequence-bounding-indices-test (&body body)
367 ;; See Issues 332 [and 333(!)] in the CLHS
368 (declare (optimize (safety 3)))
369 (let ((string (make-array 10
372 :element-type 'base-char)))
374 (format t "... BASE-CHAR")
377 (setf (fill-pointer string) 10)
379 (setf (fill-pointer string) 5)))
380 (declare (ignorable #'reset))
383 ;; See Issues 332 [and 333(!)] in the CLHS
384 (declare (optimize (safety 3)))
385 (let ((string (make-array 10
388 :element-type 'character)))
390 (format t "... CHARACTER")
393 (setf (fill-pointer string) 10)
395 (setf (fill-pointer string) 5)))
396 (declare (ignorable #'reset))
399 (declaim (notinline opaque-identity))
400 (defun opaque-identity (x) x)
402 (sequence-bounding-indices-test
403 (format t "~&/Accessor SUBSEQ")
404 (assert (string= (subseq string 0 5) "aaaaa"))
405 (assert (raises-error? (subseq string 0 6)))
406 (assert (raises-error? (subseq string (opaque-identity -1) 5)))
407 (assert (raises-error? (subseq string 4 2)))
408 (assert (raises-error? (subseq string 6)))
409 (assert (string= (setf (subseq string 0 5) "abcde") "abcde"))
410 (assert (string= (subseq string 0 5) "abcde"))
412 (assert (raises-error? (setf (subseq string 0 6) "abcdef")))
413 (assert (raises-error? (setf (subseq string (opaque-identity -1) 5) "abcdef")))
414 (assert (raises-error? (setf (subseq string 4 2) "")))
415 (assert (raises-error? (setf (subseq string 6) "ghij"))))
417 ;;; Function COUNT, COUNT-IF, COUNT-IF-NOT
418 (sequence-bounding-indices-test
419 (format t "~&/Function COUNT, COUNT-IF, COUNT-IF-NOT")
420 (assert (= (count #\a string :start 0 :end nil) 5))
421 (assert (= (count #\a string :start 0 :end 5) 5))
422 (assert (raises-error? (count #\a string :start 0 :end 6)))
423 (assert (raises-error? (count #\a string :start (opaque-identity -1) :end 5)))
424 (assert (raises-error? (count #\a string :start 4 :end 2)))
425 (assert (raises-error? (count #\a string :start 6 :end 9)))
426 (assert (= (count-if #'alpha-char-p string :start 0 :end nil) 5))
427 (assert (= (count-if #'alpha-char-p string :start 0 :end 5) 5))
428 (assert (raises-error?
429 (count-if #'alpha-char-p string :start 0 :end 6)))
430 (assert (raises-error?
431 (count-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
432 (assert (raises-error?
433 (count-if #'alpha-char-p string :start 4 :end 2)))
434 (assert (raises-error?
435 (count-if #'alpha-char-p string :start 6 :end 9)))
436 (assert (= (count-if-not #'alpha-char-p string :start 0 :end nil) 0))
437 (assert (= (count-if-not #'alpha-char-p string :start 0 :end 5) 0))
438 (assert (raises-error?
439 (count-if-not #'alpha-char-p string :start 0 :end 6)))
440 (assert (raises-error?
441 (count-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
442 (assert (raises-error?
443 (count-if-not #'alpha-char-p string :start 4 :end 2)))
444 (assert (raises-error?
445 (count-if-not #'alpha-char-p string :start 6 :end 9))))
448 (sequence-bounding-indices-test
449 (format t "~&/Function FILL")
450 (assert (string= (fill string #\b :start 0 :end 5) "bbbbb"))
451 (assert (string= (fill string #\c :start 0 :end nil) "ccccc"))
452 (assert (raises-error? (fill string #\d :start 0 :end 6)))
453 (assert (raises-error? (fill string #\d :start (opaque-identity -1) :end 5)))
454 (assert (raises-error? (fill string #\d :start 4 :end 2)))
455 (assert (raises-error? (fill string #\d :start 6 :end 9))))
457 ;;; Function FIND, FIND-IF, FIND-IF-NOT
458 (sequence-bounding-indices-test
459 (format t "~&/Function FIND, FIND-IF, FIND-IF-NOT")
460 (assert (char= (find #\a string :start 0 :end nil) #\a))
461 (assert (char= (find #\a string :start 0 :end 5) #\a))
462 (assert (raises-error? (find #\a string :start 0 :end 6)))
463 (assert (raises-error? (find #\a string :start (opaque-identity -1) :end 5)))
464 (assert (raises-error? (find #\a string :start 4 :end 2)))
465 (assert (raises-error? (find #\a string :start 6 :end 9)))
466 (assert (char= (find-if #'alpha-char-p string :start 0 :end nil) #\a))
467 (assert (char= (find-if #'alpha-char-p string :start 0 :end 5) #\a))
468 (assert (raises-error?
469 (find-if #'alpha-char-p string :start 0 :end 6)))
470 (assert (raises-error?
471 (find-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
472 (assert (raises-error?
473 (find-if #'alpha-char-p string :start 4 :end 2)))
474 (assert (raises-error?
475 (find-if #'alpha-char-p string :start 6 :end 9)))
476 (assert (eq (find-if-not #'alpha-char-p string :start 0 :end nil) nil))
477 (assert (eq (find-if-not #'alpha-char-p string :start 0 :end 5) nil))
478 (assert (raises-error?
479 (find-if-not #'alpha-char-p string :start 0 :end 6)))
480 (assert (raises-error?
481 (find-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
482 (assert (raises-error?
483 (find-if-not #'alpha-char-p string :start 4 :end 2)))
484 (assert (raises-error?
485 (find-if-not #'alpha-char-p string :start 6 :end 9))))
487 ;;; Function MISMATCH
488 (sequence-bounding-indices-test
489 (format t "~&/Function MISMATCH")
490 (assert (null (mismatch string "aaaaa" :start1 0 :end1 nil)))
491 (assert (= (mismatch "aaab" string :start2 0 :end2 4) 3))
492 (assert (raises-error? (mismatch "aaaaaa" string :start2 0 :end2 6)))
493 (assert (raises-error? (mismatch string "aaaaaa" :start1 (opaque-identity -1) :end1 5)))
494 (assert (raises-error? (mismatch string "" :start1 4 :end1 2)))
495 (assert (raises-error? (mismatch "aaaa" string :start2 6 :end2 9))))
497 ;;; Function PARSE-INTEGER
498 (sequence-bounding-indices-test
499 (format t "~&/Function PARSE-INTEGER")
500 (setf (fill-pointer string) 10)
501 (setf (subseq string 0 10) "1234567890")
502 (setf (fill-pointer string) 5)
503 (assert (= (parse-integer string :start 0 :end 5) 12345))
504 (assert (= (parse-integer string :start 0 :end nil) 12345))
505 (assert (raises-error? (parse-integer string :start 0 :end 6)))
506 (assert (raises-error? (parse-integer string :start (opaque-identity -1) :end 5)))
507 (assert (raises-error? (parse-integer string :start 4 :end 2)))
508 (assert (raises-error? (parse-integer string :start 6 :end 9))))
510 ;;; Function PARSE-NAMESTRING
511 (sequence-bounding-indices-test
512 (format t "~&/Function PARSE-NAMESTRING")
513 (setf (fill-pointer string) 10)
514 (setf (subseq string 0 10) "/dev/ /tmp")
515 (setf (fill-pointer string) 5)
516 (assert (truename (parse-namestring string nil *default-pathname-defaults*
518 (assert (truename (parse-namestring string nil *default-pathname-defaults*
520 (assert (raises-error? (parse-namestring string nil
521 *default-pathname-defaults*
523 (assert (raises-error? (parse-namestring string nil
524 *default-pathname-defaults*
525 :start (opaque-identity -1) :end 5)))
526 (assert (raises-error? (parse-namestring string nil
527 *default-pathname-defaults*
529 (assert (raises-error? (parse-namestring string nil
530 *default-pathname-defaults*
533 ;;; Function POSITION, POSITION-IF, POSITION-IF-NOT
534 (sequence-bounding-indices-test
535 (format t "~&/Function POSITION, POSITION-IF, POSITION-IF-NOT")
537 (assert (= (position #\a string :start 0 :end nil) 0))
538 (assert (= (position #\a string :start 0 :end 5) 0))
539 (assert (raises-error? (position #\a string :start 0 :end 6)))
540 (assert (raises-error? (position #\a string :start (opaque-identity -1) :end 5)))
541 (assert (raises-error? (position #\a string :start 4 :end 2)))
542 (assert (raises-error? (position #\a string :start 6 :end 9)))
543 (assert (= (position-if #'alpha-char-p string :start 0 :end nil) 0))
544 (assert (= (position-if #'alpha-char-p string :start 0 :end 5) 0))
545 (assert (raises-error?
546 (position-if #'alpha-char-p string :start 0 :end 6)))
547 (assert (raises-error?
548 (position-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
549 (assert (raises-error?
550 (position-if #'alpha-char-p string :start 4 :end 2)))
551 (assert (raises-error?
552 (position-if #'alpha-char-p string :start 6 :end 9)))
553 (assert (eq (position-if-not #'alpha-char-p string :start 0 :end nil) nil))
554 (assert (eq (position-if-not #'alpha-char-p string :start 0 :end 5) nil))
555 (assert (raises-error?
556 (position-if-not #'alpha-char-p string :start 0 :end 6)))
557 (assert (raises-error?
558 (position-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
559 (assert (raises-error?
560 (position-if-not #'alpha-char-p string :start 4 :end 2)))
561 (assert (raises-error?
562 (position-if-not #'alpha-char-p string :start 6 :end 9))))
564 ;;; Function READ-FROM-STRING
565 (sequence-bounding-indices-test
566 (format t "~&/Function READ-FROM-STRING")
567 (setf (subseq string 0 5) "(a b)")
568 (assert (equal (read-from-string string nil nil :start 0 :end 5) '(a b)))
569 (assert (equal (read-from-string string nil nil :start 0 :end nil) '(a b)))
570 (assert (raises-error? (read-from-string string nil nil :start 0 :end 6)))
571 (assert (raises-error? (read-from-string string nil nil :start (opaque-identity -1) :end 5)))
572 (assert (raises-error? (read-from-string string nil nil :start 4 :end 2)))
573 (assert (raises-error? (read-from-string string nil nil :start 6 :end 9))))
576 (sequence-bounding-indices-test
577 (format t "~&/Function REDUCE")
578 (setf (subseq string 0 5) "abcde")
579 (assert (equal (reduce #'list* string :from-end t :start 0 :end nil)
580 '(#\a #\b #\c #\d . #\e)))
581 (assert (equal (reduce #'list* string :from-end t :start 0 :end 5)
582 '(#\a #\b #\c #\d . #\e)))
583 (assert (raises-error? (reduce #'list* string :start 0 :end 6)))
584 (assert (raises-error? (reduce #'list* string :start (opaque-identity -1) :end 5)))
585 (assert (raises-error? (reduce #'list* string :start 4 :end 2)))
586 (assert (raises-error? (reduce #'list* string :start 6 :end 9))))
588 ;;; Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, DELETE, DELETE-IF,
590 (sequence-bounding-indices-test
591 (format t "~&/Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, ...")
592 (assert (equal (remove #\a string :start 0 :end nil) ""))
593 (assert (equal (remove #\a string :start 0 :end 5) ""))
594 (assert (raises-error? (remove #\a string :start 0 :end 6)))
595 (assert (raises-error? (remove #\a string :start (opaque-identity -1) :end 5)))
596 (assert (raises-error? (remove #\a string :start 4 :end 2)))
597 (assert (raises-error? (remove #\a string :start 6 :end 9)))
598 (assert (equal (remove-if #'alpha-char-p string :start 0 :end nil) ""))
599 (assert (equal (remove-if #'alpha-char-p string :start 0 :end 5) ""))
600 (assert (raises-error?
601 (remove-if #'alpha-char-p string :start 0 :end 6)))
602 (assert (raises-error?
603 (remove-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
604 (assert (raises-error?
605 (remove-if #'alpha-char-p string :start 4 :end 2)))
606 (assert (raises-error?
607 (remove-if #'alpha-char-p string :start 6 :end 9)))
608 (assert (equal (remove-if-not #'alpha-char-p string :start 0 :end nil)
610 (assert (equal (remove-if-not #'alpha-char-p string :start 0 :end 5)
612 (assert (raises-error?
613 (remove-if-not #'alpha-char-p string :start 0 :end 6)))
614 (assert (raises-error?
615 (remove-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
616 (assert (raises-error?
617 (remove-if-not #'alpha-char-p string :start 4 :end 2)))
618 (assert (raises-error?
619 (remove-if-not #'alpha-char-p string :start 6 :end 9))))
620 (sequence-bounding-indices-test
621 (format t "~&/... DELETE, DELETE-IF, DELETE-IF-NOT")
622 (assert (equal (delete #\a string :start 0 :end nil) ""))
624 (assert (equal (delete #\a string :start 0 :end 5) ""))
626 (assert (raises-error? (delete #\a string :start 0 :end 6)))
628 (assert (raises-error? (delete #\a string :start (opaque-identity -1) :end 5)))
630 (assert (raises-error? (delete #\a string :start 4 :end 2)))
632 (assert (raises-error? (delete #\a string :start 6 :end 9)))
634 (assert (equal (delete-if #'alpha-char-p string :start 0 :end nil) ""))
636 (assert (equal (delete-if #'alpha-char-p string :start 0 :end 5) ""))
638 (assert (raises-error?
639 (delete-if #'alpha-char-p string :start 0 :end 6)))
641 (assert (raises-error?
642 (delete-if #'alpha-char-p string :start (opaque-identity -1) :end 5)))
644 (assert (raises-error?
645 (delete-if #'alpha-char-p string :start 4 :end 2)))
647 (assert (raises-error?
648 (delete-if #'alpha-char-p string :start 6 :end 9)))
650 (assert (equal (delete-if-not #'alpha-char-p string :start 0 :end nil)
653 (assert (equal (delete-if-not #'alpha-char-p string :start 0 :end 5)
656 (assert (raises-error?
657 (delete-if-not #'alpha-char-p string :start 0 :end 6)))
659 (assert (raises-error?
660 (delete-if-not #'alpha-char-p string :start (opaque-identity -1) :end 5)))
662 (assert (raises-error?
663 (delete-if-not #'alpha-char-p string :start 4 :end 2)))
665 (assert (raises-error?
666 (delete-if-not #'alpha-char-p string :start 6 :end 9))))
668 ;;; Function REMOVE-DUPLICATES, DELETE-DUPLICATES
669 (sequence-bounding-indices-test
670 (format t "~&/Function REMOVE-DUPLICATES, DELETE-DUPLICATES")
671 (assert (string= (remove-duplicates string :start 0 :end 5) "a"))
672 (assert (string= (remove-duplicates string :start 0 :end nil) "a"))
673 (assert (raises-error? (remove-duplicates string :start 0 :end 6)))
674 (assert (raises-error? (remove-duplicates string :start (opaque-identity -1) :end 5)))
675 (assert (raises-error? (remove-duplicates string :start 4 :end 2)))
676 (assert (raises-error? (remove-duplicates string :start 6 :end 9)))
677 (assert (string= (delete-duplicates string :start 0 :end 5) "a"))
679 (assert (string= (delete-duplicates string :start 0 :end nil) "a"))
681 (assert (raises-error? (delete-duplicates string :start 0 :end 6)))
683 (assert (raises-error? (delete-duplicates string :start (opaque-identity -1) :end 5)))
685 (assert (raises-error? (delete-duplicates string :start 4 :end 2)))
687 (assert (raises-error? (delete-duplicates string :start 6 :end 9))))
690 (sequence-bounding-indices-test
691 (format t "~&/Function REPLACE")
692 (assert (string= (replace string "bbbbb" :start1 0 :end1 5) "bbbbb"))
693 (assert (string= (replace (copy-seq "ccccc")
695 :start2 0 :end2 nil) "bbbbb"))
696 (assert (raises-error? (replace string "ccccc" :start1 0 :end1 6)))
697 (assert (raises-error? (replace string "ccccc" :start2 (opaque-identity -1) :end2 5)))
698 (assert (raises-error? (replace string "ccccc" :start1 4 :end1 2)))
699 (assert (raises-error? (replace string "ccccc" :start1 6 :end1 9))))
702 (sequence-bounding-indices-test
703 (format t "~&/Function SEARCH")
704 (assert (= (search "aa" string :start2 0 :end2 5) 0))
705 (assert (null (search string "aa" :start1 0 :end2 nil)))
706 (assert (raises-error? (search "aa" string :start2 0 :end2 6)))
707 (assert (raises-error? (search "aa" string :start2 (opaque-identity -1) :end2 5)))
708 (assert (raises-error? (search "aa" string :start2 4 :end2 2)))
709 (assert (raises-error? (search "aa" string :start2 6 :end2 9))))
711 ;;; Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE,
712 ;;; NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE
713 (defmacro string-case-frob (fn)
715 (assert (raises-error? (,fn string :start 0 :end 6)))
716 (assert (raises-error? (,fn string :start (opaque-identity -1) :end 5)))
717 (assert (raises-error? (,fn string :start 4 :end 2)))
718 (assert (raises-error? (,fn string :start 6 :end 9)))))
720 (sequence-bounding-indices-test
721 (format t "~&/Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE, ...")
722 (string-case-frob string-upcase)
723 (string-case-frob string-downcase)
724 (string-case-frob string-capitalize)
725 (format t "~&/... NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE")
726 (string-case-frob nstring-upcase)
727 (string-case-frob nstring-downcase)
728 (string-case-frob nstring-capitalize))
730 ;;; Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=,
731 ;;; STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, STRING-GREATERP,
732 ;;; STRING-NOT-GREATERP, STRING-NOT-LESSP
733 (defmacro string-predicate-frob (fn)
735 (,fn string "abcde" :start1 0 :end1 5)
736 (,fn "fghij" string :start2 0 :end2 nil)
737 (assert (raises-error? (,fn string "klmno"
739 (assert (raises-error? (,fn "pqrst" string
740 :start2 (opaque-identity -1) :end2 5)))
741 (assert (raises-error? (,fn "uvwxy" string
743 (assert (raises-error? (,fn string "z" :start2 6 :end2 9)))))
744 (sequence-bounding-indices-test
745 (format t "~&/Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=, ...")
746 (string-predicate-frob string=)
747 (string-predicate-frob string/=)
748 (string-predicate-frob string<)
749 (string-predicate-frob string>)
750 (string-predicate-frob string<=)
751 (string-predicate-frob string>=))
752 (sequence-bounding-indices-test
753 (format t "~&/... STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, ...")
754 (string-predicate-frob string-equal)
755 (string-predicate-frob string-not-equal)
756 (string-predicate-frob string-lessp))
757 (sequence-bounding-indices-test
758 (format t "~&/... STRING-GREATERP, STRING-NOT-GREATERP, STRING-NOT-LESSP")
759 (string-predicate-frob string-greaterp)
760 (string-predicate-frob string-not-greaterp)
761 (string-predicate-frob string-not-lessp))
763 ;;; Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT,
764 ;;; NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
765 (sequence-bounding-indices-test
766 (format t "~&/Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT, ...")
767 (assert (string= (substitute #\b #\a string :start 0 :end 5) "bbbbb"))
768 (assert (string= (substitute #\c #\a string :start 0 :end nil)
770 (assert (raises-error? (substitute #\b #\a string :start 0 :end 6)))
771 (assert (raises-error? (substitute #\b #\a string :start (opaque-identity -1) :end 5)))
772 (assert (raises-error? (substitute #\b #\a string :start 4 :end 2)))
773 (assert (raises-error? (substitute #\b #\a string :start 6 :end 9)))
774 (assert (string= (substitute-if #\b #'alpha-char-p string
777 (assert (string= (substitute-if #\c #'alpha-char-p string
780 (assert (raises-error? (substitute-if #\b #'alpha-char-p string
782 (assert (raises-error? (substitute-if #\b #'alpha-char-p string
783 :start (opaque-identity -1) :end 5)))
784 (assert (raises-error? (substitute-if #\b #'alpha-char-p string
786 (assert (raises-error? (substitute-if #\b #'alpha-char-p string
788 (assert (string= (substitute-if-not #\b #'alpha-char-p string
791 (assert (string= (substitute-if-not #\c #'alpha-char-p string
794 (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
796 (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
797 :start (opaque-identity -1) :end 5)))
798 (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
800 (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
802 (sequence-bounding-indices-test
803 (format t "~&/... NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT")
804 (assert (string= (nsubstitute #\b #\a string :start 0 :end 5) "bbbbb"))
806 (assert (string= (nsubstitute #\c #\a string :start 0 :end nil)
809 (assert (raises-error? (nsubstitute #\b #\a string :start 0 :end 6)))
811 (assert (raises-error? (nsubstitute #\b #\a string :start (opaque-identity -1) :end 5)))
813 (assert (raises-error? (nsubstitute #\b #\a string :start 4 :end 2)))
815 (assert (raises-error? (nsubstitute #\b #\a string :start 6 :end 9)))
817 (assert (string= (nsubstitute-if #\b #'alpha-char-p string
821 (assert (string= (nsubstitute-if #\c #'alpha-char-p string
825 (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
828 (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
829 :start (opaque-identity -1) :end 5)))
831 (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
834 (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
837 (assert (string= (nsubstitute-if-not #\b #'alpha-char-p string
841 (assert (string= (nsubstitute-if-not #\c #'alpha-char-p string
845 (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
848 (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
849 :start (opaque-identity -1) :end 5)))
851 (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
854 (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
856 ;;; Function WRITE-STRING, WRITE-LINE
857 (sequence-bounding-indices-test
858 (format t "~&/Function WRITE-STRING, WRITE-LINE")
859 (write-string string *standard-output* :start 0 :end 5)
860 (write-string string *standard-output* :start 0 :end nil)
861 (assert (raises-error? (write-string string *standard-output*
863 (assert (raises-error? (write-string string *standard-output*
864 :start (opaque-identity -1) :end 5)))
865 (assert (raises-error? (write-string string *standard-output*
867 (assert (raises-error? (write-string string *standard-output*
869 (write-line string *standard-output* :start 0 :end 5)
870 (write-line string *standard-output* :start 0 :end nil)
871 (assert (raises-error? (write-line string *standard-output*
873 (assert (raises-error? (write-line string *standard-output*
874 :start (opaque-identity -1) :end 5)))
875 (assert (raises-error? (write-line string *standard-output*
877 (assert (raises-error? (write-line string *standard-output*
880 ;;; Macro WITH-INPUT-FROM-STRING
881 (sequence-bounding-indices-test
882 (format t "~&/Macro WITH-INPUT-FROM-STRING")
883 (with-input-from-string (s string :start 0 :end 5)
884 (assert (char= (read-char s) #\a)))
885 (with-input-from-string (s string :start 0 :end nil)
886 (assert (char= (read-char s) #\a)))
887 (assert (raises-error?
888 (with-input-from-string (s string :start 0 :end 6)
890 (assert (raises-error?
891 (with-input-from-string (s string :start (opaque-identity -1) :end 5)
893 (assert (raises-error?
894 (with-input-from-string (s string :start 4 :end 2)
896 (assert (raises-error?
897 (with-input-from-string (s string :start 6 :end 9)
900 ;;; testing bit-bashing according to _The Practice of Programming_
901 (defun fill-bytes-for-testing (bitsize)
902 "Return a list of 'bytes' of type (MOD BITSIZE)."
903 (remove-duplicates (list 0
904 (1- (ash 1 (1- bitsize)))
906 (1- (ash 1 bitsize)))))
908 (defun fill-with-known-value (value size &rest vectors)
909 (dolist (vec vectors)
911 (setf (aref vec i) value))))
913 (defun collect-fill-amounts (n-power)
915 (loop for i from 0 upto n-power
916 collect (1- (expt 2 i))
918 collect (1+ (expt 2 i)))))
920 (defun test-fill-bashing (bitsize padding-amount n-power)
921 (let* ((size (+ (* padding-amount 2) (expt 2 n-power) (* padding-amount 2)))
922 (standard (make-array size :element-type `(unsigned-byte ,bitsize)))
923 (bashed (make-array size :element-type `(unsigned-byte ,bitsize)))
924 (fill-amounts (collect-fill-amounts n-power))
925 (bash-function (intern (format nil "UB~A-BASH-FILL" bitsize)
926 (find-package "SB-KERNEL"))))
927 (loop for offset from padding-amount below (* 2 padding-amount) do
928 (dolist (c (fill-bytes-for-testing bitsize))
929 (dolist (n fill-amounts)
930 (fill-with-known-value (mod (lognot c) (ash 1 bitsize)) n
933 ;; a) the standard slow way
934 (fill standard c :start offset :end (+ offset n))
935 ;; b) the blazingly fast way
936 (let ((value (loop for i from 0 by bitsize
937 until (= i sb-vm:n-word-bits)
939 (funcall bash-function value bashed offset n))
941 (when (mismatch standard bashed)
942 (format t "Test with offset ~A, fill ~A and length ~A failed.~%"
944 (format t "Mismatch: ~A ~A~%"
945 (subseq standard 0 (+ offset n 1))
946 (subseq bashed 0 (+ offset n 1)))
947 (return-from test-fill-bashing nil))))
948 finally (return t))))
950 (defun test-copy-bashing (bitsize padding-amount n-power)
951 (let* ((size (+ (* padding-amount 2) (expt 2 n-power) (* padding-amount 2)))
952 (standard-dst (make-array size :element-type `(unsigned-byte ,bitsize)))
953 (bashed-dst (make-array size :element-type `(unsigned-byte ,bitsize)))
954 (source (make-array size :element-type `(unsigned-byte ,bitsize)))
955 (fill-amounts (collect-fill-amounts n-power))
956 (bash-function (intern (format nil "UB~A-BASH-COPY" bitsize)
957 (find-package "SB-KERNEL"))))
958 (do ((source-offset padding-amount (1+ source-offset)))
959 ((>= source-offset (* padding-amount 2))
962 (do ((target-offset padding-amount (1+ target-offset)))
963 ((>= target-offset (* padding-amount 2)))
964 (dolist (c (fill-bytes-for-testing bitsize))
965 (dolist (n fill-amounts)
966 (fill-with-known-value (mod (lognot c) (ash 1 bitsize)) size
967 source standard-dst bashed-dst)
968 ;; fill with test data
969 (fill source c :start source-offset :end (+ source-offset n))
970 ;; copy filled test data to test vectors
972 (replace standard-dst source
973 :start1 target-offset :end1 (+ target-offset n)
974 :start2 source-offset :end2 (+ source-offset n))
975 ;; b) the blazingly fast way
976 (funcall bash-function source source-offset
977 bashed-dst target-offset n)
979 (when (mismatch standard-dst bashed-dst)
980 (format t "Test with target-offset ~A, source-offset ~A, fill ~A, and length ~A failed.~%"
981 target-offset source-offset c n)
982 (format t "Mismatch:~% correct ~A~% actual ~A~%"
985 (return-from test-copy-bashing nil))))))))
987 (loop for i = 1 then (* i 2) do
988 ;; the bare '32' here is fairly arbitrary; '8' provides a good
989 ;; range of lengths over which to fill and copy, which should tease
990 ;; out most errors in the code (if any exist). (It also makes this
991 ;; part of the test suite finish reasonably quickly.)
992 (assert (test-fill-bashing i 32 8))
993 (assert (test-copy-bashing i 32 8))
994 until (= i sb-vm:n-word-bits))
997 (sb-ext:quit :unix-status 104)