0.7.12.17:
[sbcl.git] / tests / seq.impure.lisp
1 ;;;; tests related to sequences
2
3 ;;;; This file is impure because we want to be able to use DEFUN.
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; While most of SBCL is derived from the CMU CL system, the test
9 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; from CMU CL.
11 ;;;; 
12 ;;;; This software is in the public domain and is provided with
13 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
14 ;;;; more information.
15
16 (in-package :cl-user)
17
18 (load "assertoid.lisp")
19 (use-package "ASSERTOID")
20
21 ;;; helper functions for exercising SEQUENCE code on data of many
22 ;;; specialized types, and in many different optimization scenarios
23 (defun for-every-seq-1 (base-seq snippet)
24   (dolist (seq-type '(list
25                       (simple-array t 1)
26                       (vector t)
27                       (simple-array character 1)
28                       (vector character)
29                       (simple-array (signed-byte 4) 1)
30                       (vector (signed-byte 4))))
31     (flet ((entirely (eltype)
32              (every (lambda (el) (typep el eltype)) base-seq)))
33       (dolist (declaredness '(nil t))
34         (dolist (optimization '(((speed 3) (space 0))
35                                 ((speed 2) (space 2))
36                                 ((speed 1) (space 2))
37                                 ((speed 0) (space 1))))
38           (let* ((seq (if (eq seq-type 'list)
39                           (coerce base-seq 'list)
40                           (destructuring-bind (type-first &rest type-rest)
41                               seq-type
42                             (ecase type-first
43                               (simple-array
44                                (destructuring-bind (eltype one) type-rest
45                                  (assert (= one 1))
46                                  (if (entirely eltype)
47                                      (coerce base-seq seq-type)
48                                      (return))))
49                               (vector
50                                (destructuring-bind (eltype) type-rest
51                                  (if (entirely eltype)
52                                      (let ((initial-element
53                                             (cond ((subtypep eltype 'character)
54                                                    #\!)
55                                                   ((subtypep eltype 'number)
56                                                    0)
57                                                   (t #'error))))
58                                        (replace (make-array
59                                                  (+ (length base-seq)
60                                                     (random 3))
61                                                  :element-type eltype
62                                                  :fill-pointer
63                                                  (length base-seq)
64                                                  :initial-element
65                                                  initial-element)
66                                                 base-seq))
67                                      (return))))))))
68                  (lambda-expr `(lambda (seq)
69                                  ,@(when declaredness
70                                      `((declare (type ,seq-type seq))))
71                                  (declare (optimize ,@optimization))
72                                  ,snippet)))
73             (format t "~&~S~%" lambda-expr)
74             (multiple-value-bind (fun warnings-p failure-p)
75                 (compile nil lambda-expr)
76               (when (or warnings-p failure-p)
77                 (error "~@<failed compilation:~2I ~_LAMBDA-EXPR=~S ~_WARNINGS-P=~S ~_FAILURE-P=~S~:@>"
78                        lambda-expr warnings-p failure-p))
79               (format t "~&~S ~S ~S ~S ~S~%"
80                       base-seq snippet seq-type declaredness optimization)
81               (format t "~&(TYPEP SEQ 'SIMPLE-ARRAY)=~S~%"
82                       (typep seq 'simple-array))
83               (unless (funcall fun seq)
84                 (error "~@<failed test:~2I ~_BASE-SEQ=~S ~_SNIPPET=~S ~_SEQ-TYPE=~S ~_DECLAREDNESS=~S ~_OPTIMIZATION=~S~:@>"
85                        base-seq
86                        snippet
87                        seq-type
88                        declaredness
89                        optimization)))))))))
90 (defun for-every-seq (base-seq snippets)
91   (dolist (snippet snippets)
92     (for-every-seq-1 base-seq snippet)))
93
94 ;;; a wrapper to hide declared type information from the compiler, so
95 ;;; we don't get stopped by compiler warnings about e.g. compiling
96 ;;; (POSITION 1 #() :KEY #'ABS) when #() has been coerced to a string.
97 (defun indiscriminate (fun)
98   (lambda (&rest rest) (apply fun rest)))
99   
100 ;;; asymmetric test arg order example from ANSI FIND definition page
101 (assert (eql #\space ; original example, depends on ASCII character ordering
102              (find #\d "here are some letters that can be looked at"
103                    :test #'char>)))
104 (assert (eql #\e ; modified example, depends only on standard a-z ordering
105              (find #\f "herearesomeletters" :test #'char>)))
106 (assert (eql 4 ; modified more, avoids charset technicalities completely
107              (find 5 '(6 4) :test '>)))
108
109 ;;; tests of FIND, POSITION, FIND-IF, and POSITION-IF (and a few for
110 ;;; deprecated FIND-IF-NOT and POSITION-IF-NOT too)
111 (for-every-seq #()
112   '((null (find 1 seq))
113     (null (find 1 seq :from-end t))
114     (null (position 1 seq :key (indiscriminate #'abs)))
115     (null (position nil seq :test (constantly t)))
116     (null (position nil seq :test nil))
117     (null (position nil seq :test-not nil))
118     (null (find-if #'1+ seq :key (indiscriminate #'log)))
119     (null (position-if #'identity seq :from-end t))
120     (null (find-if-not #'packagep seq))
121     (null (position-if-not #'packagep seq :key nil))))
122 (for-every-seq #(1)
123   '((null (find 2 seq))
124     ;; Get the argument ordering for asymmetric tests like #'> right.
125     ;; (bug reported and fixed by Alexey Dejneka sbcl-devel 2001-10-17)
126     (eql 1 (find 2 seq :test #'>))
127     (find 2 seq :key #'1+)
128     (find 1 seq :from-end t)
129     (null (find 1 seq :from-end t :start 1))
130     (null (find 0 seq :from-end t))
131     (eql 0 (position 1 seq :key #'abs))
132     (null (position nil seq :test 'equal))
133     (eql 1 (find-if #'1- seq :key #'log))
134     (eql 0 (position-if #'identity seq :from-end t))
135     (null (find-if-not #'sin seq))
136     (eql 0 (position-if-not #'packagep seq :key 'identity))))
137 (for-every-seq #(1 2 3 2 1)
138   '((find 3 seq)
139     (find 3 seq :from-end 'yes)
140     (eql 1 (position 1.5 seq :test #'<))
141     (eql 0 (position 0 seq :key '1-))
142     (eql 4 (position 0 seq :key '1- :from-end t))
143     (eql 2 (position 4 seq :key '1+))
144     (eql 2 (position 4 seq :key '1+ :from-end t))
145     (eql 1 (position 2 seq))
146     (eql 1 (position 2 seq :start 1))
147     (null (find 2 seq :start 1 :end 1))
148     (eql 3 (position 2 seq :start 2))
149     (eql 3 (position 2 seq :key nil :from-end t))
150     (eql 2 (position 3 seq :test '=))
151     (eql 0 (position 3 seq :test-not 'equalp))
152     (eql 2 (position 3 seq :test 'equal :from-end t))
153     (null (position 4 seq :test #'eql))
154     (null (find-if #'packagep seq))
155     (eql 1 (find-if #'plusp seq))
156     (eql 3 (position-if #'plusp seq :key #'1- :from-end t))
157     (eql 1 (position-if #'evenp seq))
158     (eql 3 (position-if #'evenp seq :from-end t))
159     (eql 2 (position-if #'plusp seq :from-end nil :key '1- :start 2))
160     (eql 3 (position-if #'plusp seq :from-end t :key '1- :start 2))
161     (null (position-if #'plusp seq :from-end t :key '1- :start 2 :end 2))
162     (null (find-if-not #'plusp seq))
163     (eql 0 (position-if-not #'evenp seq))))
164 (for-every-seq "string test"
165   '((null (find 0 seq))
166     (null (find #\D seq :key #'char-upcase))
167     (find #\E seq :key #'char-upcase)
168     (null (find #\e seq :key #'char-upcase))
169     (eql 3 (position #\i seq))
170     (eql 0 (position #\s seq :key #'char-downcase))
171     (eql 1 (position #\s seq :key #'char-downcase :test #'char/=))
172     (eql 9 (position #\s seq :from-end t :test #'char=))
173     (eql 10 (position #\s seq :from-end t :test #'char/=))
174     (eql 4 (position #\N seq :from-end t :key 'char-upcase :test #'char-equal))
175     (eql 5 (position-if (lambda (c) (equal #\g c)) seq))
176     (eql 5 (position-if (lambda (c) (equal #\g c)) seq :from-end t))
177     (find-if #'characterp seq)
178     (find-if (lambda (c) (typep c 'base-char)) seq :from-end t)
179     (null (find-if 'upper-case-p seq))))
180          
181 ;;; SUBSEQ
182 (let ((avec (make-array 10
183                         :fill-pointer 4
184                         :initial-contents '(0 1 2 3 iv v vi vii iix ix))))
185   ;; These first five always worked AFAIK.
186   (assert (equalp (subseq avec 0 3) #(0 1 2)))
187   (assert (equalp (subseq avec 3 3) #()))
188   (assert (equalp (subseq avec 1 3) #(1 2)))
189   (assert (equalp (subseq avec 1) #(1 2 3)))
190   (assert (equalp (subseq avec 1 4) #(1 2 3)))
191   ;; SBCL bug found ca. 2002-05-01 by OpenMCL's correct handling of
192   ;; SUBSEQ, CSR's driving portable cross-compilation far enough to
193   ;; reach the SUBSEQ calls in assem.lisp, and WHN's sleazy
194   ;; translation of old CMU CL new-assem.lisp into sufficiently grotty
195   ;; portable Lisp that it passed suitable illegal values to SUBSEQ to
196   ;; exercise the bug:-|
197   ;;
198   ;; SUBSEQ should check its END value against logical LENGTH, not
199   ;; physical ARRAY-DIMENSION 0.
200   ;;
201   ;; fixed in sbcl-0.7.4.22 by WHN
202   (assert (null (ignore-errors (aref (subseq avec 1 5) 0)))))
203
204 ;;; FILL
205 (defun test-fill-typecheck (x)
206   (declare (optimize (safety 3) (space 2) (speed 1)))
207   (fill (make-string 10) x))
208
209 (assert (string= (test-fill-typecheck #\@) "@@@@@@@@@@"))
210 ;;; BUG 186, fixed in sbcl-0.7.5.5
211 (assert (null (ignore-errors (test-fill-typecheck 4097))))
212
213 ;;; MAKE-SEQUENCE, COERCE, CONCATENATE, MERGE, MAP and requested
214 ;;; result type (BUGs 46a, 46b, 66)
215 (macrolet ((assert-type-error (form)
216              `(assert (typep (nth-value 1 (ignore-errors ,form)) 
217                              'type-error))))
218   (dolist (type-stub '((simple-vector) 
219                        (vector *) 
220                        (vector (signed-byte 8))
221                        (vector (unsigned-byte 16))
222                        (vector (signed-byte 32))
223                        (simple-bit-vector)))
224     (declare (optimize safety))
225     (format t "~&~S~%" type-stub)
226     ;; MAKE-SEQUENCE
227     (assert (= (length (make-sequence `(,@type-stub) 10)) 10))
228     (assert (= (length (make-sequence `(,@type-stub 10) 10)) 10))
229     (assert-type-error (make-sequence `(,@type-stub 10) 11))
230     ;; COERCE
231     (assert (= (length (coerce '(0 0 0) `(,@type-stub))) 3))
232     (assert (= (length (coerce #(0 0 0) `(,@type-stub 3))) 3))
233     (assert-type-error (coerce #*111 `(,@type-stub 4)))
234     ;; CONCATENATE
235     (assert (= (length (concatenate `(,@type-stub) #(0 0 0) #*111)) 6))
236     (assert (equalp (concatenate `(,@type-stub) #(0 0 0) #*111)
237                    (coerce #(0 0 0 1 1 1) `(,@type-stub))))
238     (assert (= (length (concatenate `(,@type-stub 6) #(0 0 0) #*111)) 6))
239     (assert (equalp (concatenate `(,@type-stub 6) #(0 0 0) #*111)
240                    (coerce #(0 0 0 1 1 1) `(,@type-stub 6))))
241     (assert-type-error (concatenate `(,@type-stub 5) #(0 0 0) #*111))
242     ;; MERGE
243     (assert (= (length (merge `(,@type-stub) #(0 1 0) #*111 #'>)) 6))
244     (assert (equalp (merge `(,@type-stub) #(0 1 0) #*111 #'>)
245                    (coerce #(1 1 1 0 1 0) `(,@type-stub))))
246     (assert (= (length (merge `(,@type-stub 6) #(0 1 0) #*111 #'>)) 6))
247     (assert (equalp (merge `(,@type-stub 6) #(0 1 0) #*111 #'>)
248                    (coerce #(1 1 1 0 1 0) `(,@type-stub 6))))
249     (assert-type-error (merge `(,@type-stub 4) #(0 1 0) #*111 #'>))
250     ;; MAP
251     (assert (= (length (map `(,@type-stub) #'logxor #(0 0 1 1) '(0 1 0 1))) 4))
252     (assert (equalp (map `(,@type-stub) #'logxor #(0 0 1 1) '(0 1 0 1))
253                    (coerce #(0 1 1 0) `(,@type-stub))))
254     (assert (= (length (map `(,@type-stub 4) #'logxor #(0 0 1 1) '(0 1 0 1))) 
255                4))
256     (assert (equalp (map `(,@type-stub 4) #'logxor #(0 0 1 1) '(0 1 0 1))
257                    (coerce #(0 1 1 0) `(,@type-stub 4))))
258     (assert-type-error (map `(,@type-stub 5) #'logxor #(0 0 1 1) '(0 1 0 1))))
259   ;; some more CONCATENATE tests for strings
260   (locally 
261       (declare (optimize safety))
262     (assert (string= (concatenate 'string "foo" " " "bar") "foo bar"))
263     (assert (string= (concatenate '(string 7) "foo" " " "bar") "foo bar"))
264     (assert-type-error (concatenate '(string 6) "foo" " " "bar"))
265     (assert (string= (concatenate '(string 6) "foo" #(#\b #\a #\r)) "foobar"))
266     (assert-type-error (concatenate '(string 7) "foo" #(#\b #\a #\r))))
267   ;; Non-VECTOR ARRAY types aren't allowed as vector type specifiers.
268   (locally
269     (declare (optimize safety))
270     (assert-type-error (concatenate 'simple-array "foo" "bar"))
271     (assert-type-error (map 'simple-array #'identity '(1 2 3)))
272     (assert (equalp #(11 13)
273                     (map '(simple-array fixnum (*)) #'+ '(1 2 3) '(10 11))))
274     (assert-type-error (coerce '(1 2 3) 'simple-array))
275     (assert-type-error (merge 'simple-array '(1 3) '(2 4) '<))
276     (assert (equalp #(3 2 1) (coerce '(3 2 1) '(vector fixnum))))
277     (assert-type-error (map 'array #'identity '(1 2 3)))
278     (assert-type-error (map '(array fixnum) #'identity '(1 2 3)))
279     (assert (equalp #(1 2 3) (coerce '(1 2 3) '(vector fixnum))))
280     ;; but COERCE has an exemption clause:
281     (assert (string= "foo" (coerce "foo" 'simple-array)))
282     ;; ... though not in all cases.
283     (assert-type-error (coerce '(#\f #\o #\o) 'simple-array))))
284
285 ;;; As pointed out by Raymond Toy on #lisp IRC, MERGE had some issues
286 ;;; with user-defined types until sbcl-0.7.8.11
287 (deftype list-typeoid () 'list)
288 (assert (equal '(1 2 3 4) (merge 'list-typeoid '(1 3) '(2 4) '<)))
289 ;;; and also with types that weren't precicely LIST
290 (assert (equal '(1 2 3 4) (merge 'cons '(1 3) '(2 4) '<)))
291
292 ;;; but wait, there's more! The NULL and CONS types also have implicit
293 ;;; length requirements:
294 (macrolet ((assert-type-error (form)
295              `(assert (typep (nth-value 1 (ignore-errors ,form)) 
296                              'type-error))))
297   (locally
298       (declare (optimize safety))
299     ;; MAKE-SEQUENCE
300     (assert-type-error (make-sequence 'cons 0))
301     (assert-type-error (make-sequence 'null 1))
302     ;; KLUDGE: I'm not certain that this test actually tests for what
303     ;; it should test, in that the type deriver and optimizers might
304     ;; be too smart for the good of an exhaustive test system.
305     ;; However, it makes me feel good.  -- CSR, 2002-10-18
306     (assert (null (make-sequence 'null 0)))
307     (assert (= (length (make-sequence 'cons 3)) 3))
308     ;; and NIL is not a valid type for MAKE-SEQUENCE
309     (assert-type-error (make-sequence 'nil 0))
310     ;; COERCE
311     (assert-type-error (coerce #(1) 'null))
312     (assert-type-error (coerce #() 'cons))
313     (assert (null (coerce #() 'null)))
314     (assert (= (length (coerce #(1) 'cons)) 1))
315     (assert-type-error (coerce #() 'nil))
316     ;; MERGE
317     (assert-type-error (merge 'null '(1 3) '(2 4) '<))
318     (assert-type-error (merge 'cons () () '<))
319     (assert (null (merge 'null () () '<)))
320     (assert (= (length (merge 'cons '(1 3) '(2 4) '<)) 4))
321     (assert-type-error (merge 'nil () () '<))
322     ;; CONCATENATE
323     (assert-type-error (concatenate 'null '(1) "2"))
324     (assert-type-error (concatenate 'cons #() ()))
325     (assert (null (concatenate 'null () #())))
326     (assert (= (length (concatenate 'cons #() '(1) "2 3")) 4))
327     (assert-type-error (concatenate 'nil '(3)))
328     ;; FIXME: tests for MAP to come when some brave soul implements
329     ;; the analogous type checking for MAP/%MAP.
330     ))
331 \f
332 ;;; ELT should signal an error of type TYPE-ERROR if its index
333 ;;; argument isn't a valid sequence index for sequence:
334 (defun test-elt-signal (x)
335   (elt x 3))
336 (assert (raises-error? (test-elt-signal "foo") type-error))
337 (assert (eql (test-elt-signal "foob") #\b))
338 (locally
339   (declare (optimize (safety 3)))
340   (assert (raises-error? (elt (list 1 2 3) 3) type-error)))
341 \f
342 ;;; checks for uniform bounding index handling under SAFETY 3 code.
343 ;;;
344 ;;; KLUDGE: not all in one big form because that causes SBCL to spend
345 ;;; an absolute age trying to compile it.
346 (defmacro sequence-bounding-indices-test (&body body)
347   `(locally
348     ;; See Issues 332 [and 333(!)] in the CLHS
349     (declare (optimize (safety 3)))
350     (let ((string (make-array 10
351                               :fill-pointer 5
352                               :initial-element #\a
353                               :element-type 'base-char)))
354       (flet ((reset ()
355                (setf (fill-pointer string) 10)
356                (fill string #\a)
357                (setf (fill-pointer string) 5)))
358         (declare (ignorable #'reset))
359         ,@body))))
360
361 ;;; Accessor SUBSEQ
362 (sequence-bounding-indices-test
363  (format t "~&/Accessor SUBSEQ~%")
364  (assert (string= (subseq string 0 5) "aaaaa"))
365  (assert (raises-error? (subseq string 0 6)))
366  (assert (raises-error? (subseq string -1 5)))
367  (assert (raises-error? (subseq string 4 2)))
368  (assert (raises-error? (subseq string 6)))
369  (assert (string= (setf (subseq string 0 5) "abcde") "abcde"))
370  (assert (string= (subseq string 0 5) "abcde"))
371  (reset)
372  (assert (raises-error? (setf (subseq string 0 6) "abcdef")))
373  (assert (raises-error? (setf (subseq string -1 5) "abcdef")))
374  (assert (raises-error? (setf (subseq string 4 2) "")))
375  (assert (raises-error? (setf (subseq string 6) "ghij"))))
376
377 ;;; Function COUNT, COUNT-IF, COUNT-IF-NOT
378 (sequence-bounding-indices-test
379  (format t "~&/Function COUNT, COUNT-IF, COUNT-IF-NOT")
380  (assert (= (count #\a string :start 0 :end nil) 5))
381  (assert (= (count #\a string :start 0 :end 5) 5))
382  (assert (raises-error? (count #\a string :start 0 :end 6)))
383  (assert (raises-error? (count #\a string :start -1 :end 5)))
384  (assert (raises-error? (count #\a string :start 4 :end 2)))
385  (assert (raises-error? (count #\a string :start 6 :end 9)))
386  (assert (= (count-if #'alpha-char-p string :start 0 :end nil) 5))
387  (assert (= (count-if #'alpha-char-p string :start 0 :end 5) 5))
388  (assert (raises-error?
389           (count-if #'alpha-char-p string :start 0 :end 6)))
390  (assert (raises-error?
391           (count-if #'alpha-char-p string :start -1 :end 5)))
392  (assert (raises-error?
393           (count-if #'alpha-char-p string :start 4 :end 2)))
394  (assert (raises-error?
395           (count-if #'alpha-char-p string :start 6 :end 9)))
396  (assert (= (count-if-not #'alpha-char-p string :start 0 :end nil) 0))
397  (assert (= (count-if-not #'alpha-char-p string :start 0 :end 5) 0))
398  (assert (raises-error?
399           (count-if-not #'alpha-char-p string :start 0 :end 6)))
400  (assert (raises-error?
401           (count-if-not #'alpha-char-p string :start -1 :end 5)))
402  (assert (raises-error?
403           (count-if-not #'alpha-char-p string :start 4 :end 2)))
404  (assert (raises-error?
405           (count-if-not #'alpha-char-p string :start 6 :end 9))))
406
407 ;;; Function FILL
408 (sequence-bounding-indices-test
409  (format t "~&/Function FILL~%")
410  (assert (string= (fill string #\b :start 0 :end 5) "bbbbb"))
411  (assert (string= (fill string #\c :start 0 :end nil) "ccccc"))
412  (assert (raises-error? (fill string #\d :start 0 :end 6)))
413  (assert (raises-error? (fill string #\d :start -1 :end 5)))
414  (assert (raises-error? (fill string #\d :start 4 :end 2)))
415  (assert (raises-error? (fill string #\d :start 6 :end 9))))
416
417 ;;; Function FIND, FIND-IF, FIND-IF-NOT
418 (sequence-bounding-indices-test
419  (format t "~&/Function FIND, FIND-IF, FIND-IF-NOT~%")
420  (assert (char= (find #\a string :start 0 :end nil) #\a))
421  (assert (char= (find #\a string :start 0 :end 5) #\a))
422  (assert (raises-error? (find #\a string :start 0 :end 6)))
423  (assert (raises-error? (find #\a string :start -1 :end 5)))
424  (assert (raises-error? (find #\a string :start 4 :end 2)))
425  (assert (raises-error? (find #\a string :start 6 :end 9)))
426  (assert (char= (find-if #'alpha-char-p string :start 0 :end nil) #\a))
427  (assert (char= (find-if #'alpha-char-p string :start 0 :end 5) #\a))
428  (assert (raises-error?
429           (find-if #'alpha-char-p string :start 0 :end 6)))
430  (assert (raises-error?
431           (find-if #'alpha-char-p string :start -1 :end 5)))
432  (assert (raises-error?
433           (find-if #'alpha-char-p string :start 4 :end 2)))
434  (assert (raises-error?
435           (find-if #'alpha-char-p string :start 6 :end 9)))
436  (assert (eq (find-if-not #'alpha-char-p string :start 0 :end nil) nil))
437  (assert (eq (find-if-not #'alpha-char-p string :start 0 :end 5) nil))
438  (assert (raises-error?
439           (find-if-not #'alpha-char-p string :start 0 :end 6)))
440  (assert (raises-error?
441           (find-if-not #'alpha-char-p string :start -1 :end 5)))
442  (assert (raises-error?
443           (find-if-not #'alpha-char-p string :start 4 :end 2)))
444  (assert (raises-error?
445           (find-if-not #'alpha-char-p string :start 6 :end 9))))
446
447 ;;; Function MISMATCH
448 (sequence-bounding-indices-test
449  (format t "~&/Function MISMATCH~%")
450  (assert (null (mismatch string "aaaaa" :start1 0 :end1 nil)))
451  (assert (= (mismatch "aaab" string :start2 0 :end2 4) 3))
452  (assert (raises-error? (mismatch "aaaaaa" string :start2 0 :end2 6)))
453  (assert (raises-error? (mismatch string "aaaaaa" :start1 -1 :end1 5)))
454  (assert (raises-error? (mismatch string "" :start1 4 :end1 2)))
455  (assert (raises-error? (mismatch "aaaa" string :start2 6 :end2 9))))
456
457 ;;; Function PARSE-INTEGER
458 (sequence-bounding-indices-test
459  (format t "~&/Function PARSE-INTEGER~%")
460  (setf (fill-pointer string) 10)
461  (setf (subseq string 0 10) "1234567890")
462  (setf (fill-pointer string) 5)
463  (assert (= (parse-integer string :start 0 :end 5) 12345))
464  (assert (= (parse-integer string :start 0 :end nil) 12345))
465  (assert (raises-error? (parse-integer string :start 0 :end 6)))
466  (assert (raises-error? (parse-integer string :start -1 :end 5)))
467  (assert (raises-error? (parse-integer string :start 4 :end 2)))
468  (assert (raises-error? (parse-integer string :start 6 :end 9))))
469
470 ;;; Function PARSE-NAMESTRING
471 (sequence-bounding-indices-test
472  (format t "~&/Function PARSE-NAMESTRING~%")
473  (setf (fill-pointer string) 10)
474  (setf (subseq string 0 10) "/dev/ /tmp")
475  (setf (fill-pointer string) 5)
476  (assert (truename (parse-namestring string nil *default-pathname-defaults*
477                                      :start 0 :end 5)))
478  (assert (truename (parse-namestring string nil *default-pathname-defaults*
479                                      :start 0 :end nil)))
480  (assert (raises-error? (parse-namestring string nil
481                                           *default-pathname-defaults*
482                                           :start 0 :end 6)))
483  (assert (raises-error? (parse-namestring string nil
484                                           *default-pathname-defaults*
485                                           :start -1 :end 5)))
486  (assert (raises-error? (parse-namestring string nil
487                                           *default-pathname-defaults*
488                                           :start 4 :end 2)))
489  (assert (raises-error? (parse-namestring string nil
490                                           *default-pathname-defaults*
491                                           :start 6 :end 9))))
492
493 ;;; Function POSITION, POSITION-IF, POSITION-IF-NOT
494 (sequence-bounding-indices-test
495  (format t "~&/Function POSITION, POSITION-IF, POSITION-IF-NOT~%")
496  (assert (= (position #\a string :start 0 :end nil) 0))
497  (assert (= (position #\a string :start 0 :end 5) 0))
498  (assert (raises-error? (position #\a string :start 0 :end 6)))
499  (assert (raises-error? (position #\a string :start -1 :end 5)))
500  (assert (raises-error? (position #\a string :start 4 :end 2)))
501  (assert (raises-error? (position #\a string :start 6 :end 9)))
502  (assert (= (position-if #'alpha-char-p string :start 0 :end nil) 0))
503  (assert (= (position-if #'alpha-char-p string :start 0 :end 5) 0))
504  (assert (raises-error?
505           (position-if #'alpha-char-p string :start 0 :end 6)))
506  (assert (raises-error?
507           (position-if #'alpha-char-p string :start -1 :end 5)))
508  (assert (raises-error?
509           (position-if #'alpha-char-p string :start 4 :end 2)))
510  (assert (raises-error?
511           (position-if #'alpha-char-p string :start 6 :end 9)))
512  (assert (eq (position-if-not #'alpha-char-p string :start 0 :end nil) nil))
513  (assert (eq (position-if-not #'alpha-char-p string :start 0 :end 5) nil))
514  (assert (raises-error?
515           (position-if-not #'alpha-char-p string :start 0 :end 6)))
516  (assert (raises-error?
517           (position-if-not #'alpha-char-p string :start -1 :end 5)))
518  (assert (raises-error?
519           (position-if-not #'alpha-char-p string :start 4 :end 2)))
520  (assert (raises-error?
521           (position-if-not #'alpha-char-p string :start 6 :end 9))))
522
523 ;;; Function READ-FROM-STRING
524 (sequence-bounding-indices-test
525  (format t "~&/Function READ-FROM-STRING~%")
526  (setf (subseq string 0 5) "(a b)")
527  (assert (equal (read-from-string string nil nil :start 0 :end 5) '(a b)))
528  (assert (equal (read-from-string string nil nil :start 0 :end nil) '(a b)))
529  (assert (raises-error? (read-from-string string nil nil :start 0 :end 6)))
530  (assert (raises-error? (read-from-string string nil nil :start -1 :end 5)))
531  (assert (raises-error? (read-from-string string nil nil :start 4 :end 2)))
532  (assert (raises-error? (read-from-string string nil nil :start 6 :end 9))))
533
534 ;;; Function REDUCE
535 (sequence-bounding-indices-test
536  (format t "~&/Function REDUCE~%")
537  (setf (subseq string 0 5) "abcde")
538  (assert (equal (reduce #'list* string :from-end t :start 0 :end nil)
539                 '(#\a #\b #\c #\d . #\e)))
540  (assert (equal (reduce #'list* string :from-end t :start 0 :end 5)
541                 '(#\a #\b #\c #\d . #\e)))
542  (assert (raises-error? (reduce #'list* string :start 0 :end 6)))
543  (assert (raises-error? (reduce #'list* string :start -1 :end 5)))
544  (assert (raises-error? (reduce #'list* string :start 4 :end 2)))
545  (assert (raises-error? (reduce #'list* string :start 6 :end 9))))
546
547 ;;; Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, DELETE, DELETE-IF,
548 ;;; DELETE-IF-NOT
549 (sequence-bounding-indices-test
550  (format t "~&/Function REMOVE, REMOVE-IF, REMOVE-IF-NOT, ...~%")
551  (assert (equal (remove #\a string :start 0 :end nil) ""))
552  (assert (equal (remove #\a string :start 0 :end 5) ""))
553  (assert (raises-error? (remove #\a string :start 0 :end 6)))
554  (assert (raises-error? (remove #\a string :start -1 :end 5)))
555  (assert (raises-error? (remove #\a string :start 4 :end 2)))
556  (assert (raises-error? (remove #\a string :start 6 :end 9)))
557  (assert (equal (remove-if #'alpha-char-p string :start 0 :end nil) ""))
558  (assert (equal (remove-if #'alpha-char-p string :start 0 :end 5) ""))
559  (assert (raises-error?
560           (remove-if #'alpha-char-p string :start 0 :end 6)))
561  (assert (raises-error?
562           (remove-if #'alpha-char-p string :start -1 :end 5)))
563  (assert (raises-error?
564           (remove-if #'alpha-char-p string :start 4 :end 2)))
565  (assert (raises-error?
566           (remove-if #'alpha-char-p string :start 6 :end 9)))
567  (assert (equal (remove-if-not #'alpha-char-p string :start 0 :end nil)
568                 "aaaaa"))
569  (assert (equal (remove-if-not #'alpha-char-p string :start 0 :end 5)
570                 "aaaaa"))
571  (assert (raises-error?
572           (remove-if-not #'alpha-char-p string :start 0 :end 6)))
573  (assert (raises-error?
574           (remove-if-not #'alpha-char-p string :start -1 :end 5)))
575  (assert (raises-error?
576           (remove-if-not #'alpha-char-p string :start 4 :end 2)))
577  (assert (raises-error?
578           (remove-if-not #'alpha-char-p string :start 6 :end 9)))
579  (format t "~&/... DELETE, DELETE-IF, DELETE-IF-NOT")
580  (assert (equal (delete #\a string :start 0 :end nil) ""))
581  (reset)
582  (assert (equal (delete #\a string :start 0 :end 5) ""))
583  (reset)
584  (assert (raises-error? (delete #\a string :start 0 :end 6)))
585  (reset)
586  (assert (raises-error? (delete #\a string :start -1 :end 5)))
587  (reset)
588  (assert (raises-error? (delete #\a string :start 4 :end 2)))
589  (reset)
590  (assert (raises-error? (delete #\a string :start 6 :end 9)))
591  (reset)
592  (assert (equal (delete-if #'alpha-char-p string :start 0 :end nil) ""))
593  (reset)
594  (assert (equal (delete-if #'alpha-char-p string :start 0 :end 5) ""))
595  (reset)
596  (assert (raises-error?
597           (delete-if #'alpha-char-p string :start 0 :end 6)))
598  (reset)
599  (assert (raises-error?
600           (delete-if #'alpha-char-p string :start -1 :end 5)))
601  (reset)
602  (assert (raises-error?
603           (delete-if #'alpha-char-p string :start 4 :end 2)))
604  (reset)
605  (assert (raises-error?
606           (delete-if #'alpha-char-p string :start 6 :end 9)))
607  (reset)
608  (assert (equal (delete-if-not #'alpha-char-p string :start 0 :end nil)
609                 "aaaaa"))
610  (reset)
611  (assert (equal (delete-if-not #'alpha-char-p string :start 0 :end 5)
612                 "aaaaa"))
613  (reset)
614  (assert (raises-error?
615           (delete-if-not #'alpha-char-p string :start 0 :end 6)))
616  (reset)
617  (assert (raises-error?
618           (delete-if-not #'alpha-char-p string :start -1 :end 5)))
619  (reset)
620  (assert (raises-error?
621           (delete-if-not #'alpha-char-p string :start 4 :end 2)))
622  (reset)
623  (assert (raises-error?
624           (delete-if-not #'alpha-char-p string :start 6 :end 9))))
625
626 ;;; Function REMOVE-DUPLICATES, DELETE-DUPLICATES
627 (sequence-bounding-indices-test
628  (format t "~&/Function REMOVE-DUPLICATES, DELETE-DUPLICATES~%")
629  (assert (string= (remove-duplicates string :start 0 :end 5) "a"))
630  (assert (string= (remove-duplicates string :start 0 :end nil) "a"))
631  (assert (raises-error? (remove-duplicates string :start 0 :end 6)))
632  (assert (raises-error? (remove-duplicates string :start -1 :end 5)))
633  (assert (raises-error? (remove-duplicates string :start 4 :end 2)))
634  (assert (raises-error? (remove-duplicates string :start 6 :end 9)))
635  (assert (string= (delete-duplicates string :start 0 :end 5) "a"))
636  (reset)
637  (assert (string= (delete-duplicates string :start 0 :end nil) "a"))
638  (reset)
639  (assert (raises-error? (delete-duplicates string :start 0 :end 6)))
640  (reset)
641  (assert (raises-error? (delete-duplicates string :start -1 :end 5)))
642  (reset)
643  (assert (raises-error? (delete-duplicates string :start 4 :end 2)))
644  (reset)
645  (assert (raises-error? (delete-duplicates string :start 6 :end 9))))
646
647 ;;; Function REPLACE
648 (sequence-bounding-indices-test
649  (format t "~&/Function REPLACE~%")
650  (assert (string= (replace string "bbbbb" :start1 0 :end1 5) "bbbbb"))
651  (assert (string= (replace (copy-seq "ccccc")
652                            string
653                            :start2 0 :end2 nil) "bbbbb"))
654  (assert (raises-error? (replace string "ccccc" :start1 0 :end1 6)))
655  (assert (raises-error? (replace string "ccccc" :start2 -1 :end2 5)))
656  (assert (raises-error? (replace string "ccccc" :start1 4 :end1 2)))
657  (assert (raises-error? (replace string "ccccc" :start1 6 :end1 9))))
658
659 ;;; Function SEARCH
660 (sequence-bounding-indices-test
661  (format t "~&/Function SEARCH~%")
662  (assert (= (search "aa" string :start2 0 :end2 5) 0))
663  (assert (null (search string "aa" :start1 0 :end2 nil)))
664  (assert (raises-error? (search "aa" string :start2 0 :end2 6)))
665  (assert (raises-error? (search "aa" string :start2 -1 :end2 5)))
666  (assert (raises-error? (search "aa" string :start2 4 :end2 2)))
667  (assert (raises-error? (search "aa" string :start2 6 :end2 9))))
668
669 ;;; Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE,
670 ;;; NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE
671 (sequence-bounding-indices-test
672  (macrolet ((frob (fn)
673               `(progn
674                 (assert (raises-error? (,fn string :start 0 :end 6)))
675                 (assert (raises-error? (,fn string :start -1 :end 5)))
676                 (assert (raises-error? (,fn string :start 4 :end 2)))
677                 (assert (raises-error? (,fn string :start 6 :end 9))))))
678    (format t "~&/Function STRING-UPCASE, STRING-DOWNCASE, STRING-CAPITALIZE, ...~%")
679    (frob string-upcase)
680    (frob string-downcase)
681    (frob string-capitalize)
682    (format t "~&/... NSTRING-UPCASE, NSTRING-DOWNCASE, NSTRING-CAPITALIZE~%")
683    (frob nstring-upcase)
684    (frob nstring-downcase)
685    (frob nstring-capitalize)))
686  
687 ;;; Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=,
688 ;;; STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, STRING-GREATERP,
689 ;;; STRING-NOT-GREATERP, STRING-NOT-LESSP
690 (sequence-bounding-indices-test
691  (macrolet ((frob (fn)
692               `(progn
693                 (,fn string "abcde" :start1 0 :end1 5)
694                 (,fn "fghij" string :start2 0 :end2 nil)
695                 (assert (raises-error? (,fn string "klmno"
696                                             :start1 0 :end1 6)))
697                 (assert (raises-error? (,fn "pqrst" string
698                                             :start2 -1 :end2 5)))
699                 (assert (raises-error? (,fn "uvwxy" string
700                                             :start1 4 :end1 2)))
701                 (assert (raises-error? (,fn string "z" :start2 6 :end2 9))))))
702    (format t "~&/Function STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=, ...")
703    (frob string=)
704    (frob string/=)
705    (frob string<)
706    (frob string>)
707    (frob string<=)
708    (frob string>=)
709    (format t "~&/... STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, ...~%")
710    (frob string-equal)
711    (frob string-not-equal)
712    (frob string-lessp)
713    (format t "~&/... STRING-GREATERP, STRING-NOT-GREATERP, STRING-NOT-LESSP~%")
714    (frob string-greaterp)
715    (frob string-not-greaterp)
716    (frob string-not-lessp)))
717
718 ;;; Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT,
719 ;;; NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT
720 (sequence-bounding-indices-test
721  (format t "~&/Function SUBSTITUTE, SUBSTITUTE-IF, SUBSTITUTE-IF-NOT, ...~%")
722  (assert (string= (substitute #\b #\a string :start 0 :end 5) "bbbbb"))
723  (assert (string= (substitute #\c #\a string :start 0 :end nil)
724                   "ccccc"))
725  (assert (raises-error? (substitute #\b #\a string :start 0 :end 6)))
726  (assert (raises-error? (substitute #\b #\a string :start -1 :end 5)))
727  (assert (raises-error? (substitute #\b #\a string :start 4 :end 2)))
728  (assert (raises-error? (substitute #\b #\a string :start 6 :end 9)))
729  (assert (string= (substitute-if #\b #'alpha-char-p string
730                                  :start 0 :end 5)
731                   "bbbbb"))
732  (assert (string= (substitute-if #\c #'alpha-char-p string
733                                  :start 0 :end nil)
734                   "ccccc"))
735  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
736                                        :start 0 :end 6)))
737  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
738                                        :start -1 :end 5)))
739  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
740                                        :start 4 :end 2)))
741  (assert (raises-error? (substitute-if #\b #'alpha-char-p string
742                                        :start 6 :end 9)))
743  (assert (string= (substitute-if-not #\b #'alpha-char-p string
744                                      :start 0 :end 5)
745                   "aaaaa"))
746  (assert (string= (substitute-if-not #\c #'alpha-char-p string
747                                      :start 0 :end nil)
748                   "aaaaa"))
749  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
750                                            :start 0 :end 6)))
751  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
752                                            :start -1 :end 5)))
753  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
754                                            :start 4 :end 2)))
755  (assert (raises-error? (substitute-if-not #\b #'alpha-char-p string
756                                            :start 6 :end 9)))
757  (format t "~&/... NSUBSTITUTE, NSUBSTITUTE-IF, NSUBSTITUTE-IF-NOT~%")
758  (assert (string= (nsubstitute #\b #\a string :start 0 :end 5) "bbbbb"))
759  (reset)
760  (assert (string= (nsubstitute #\c #\a string :start 0 :end nil)
761                   "ccccc"))
762  (reset)
763  (assert (raises-error? (nsubstitute #\b #\a string :start 0 :end 6)))
764  (reset)
765  (assert (raises-error? (nsubstitute #\b #\a string :start -1 :end 5)))
766  (reset)
767  (assert (raises-error? (nsubstitute #\b #\a string :start 4 :end 2)))
768  (reset)
769  (assert (raises-error? (nsubstitute #\b #\a string :start 6 :end 9)))
770  (reset)
771  (assert (string= (nsubstitute-if #\b #'alpha-char-p string
772                                   :start 0 :end 5)
773                   "bbbbb"))
774  (reset)
775  (assert (string= (nsubstitute-if #\c #'alpha-char-p string
776                                   :start 0 :end nil)
777                   "ccccc"))
778  (reset)
779  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
780                                         :start 0 :end 6)))
781  (reset)
782  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
783                                         :start -1 :end 5)))
784  (reset)
785  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
786                                         :start 4 :end 2)))
787  (reset)
788  (assert (raises-error? (nsubstitute-if #\b #'alpha-char-p string
789                                         :start 6 :end 9)))
790  (reset)
791  (assert (string= (nsubstitute-if-not #\b #'alpha-char-p string
792                                       :start 0 :end 5)
793                   "aaaaa"))
794  (reset)
795  (assert (string= (nsubstitute-if-not #\c #'alpha-char-p string
796                                       :start 0 :end nil)
797                   "aaaaa"))
798  (reset)
799  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
800                                             :start 0 :end 6)))
801  (reset)
802  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
803                                             :start -1 :end 5)))
804  (reset)
805  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
806                                             :start 4 :end 2)))
807  (reset)
808  (assert (raises-error? (nsubstitute-if-not #\b #'alpha-char-p string
809                                             :start 6 :end 9))))
810 ;;; Function WRITE-STRING, WRITE-LINE
811 (sequence-bounding-indices-test
812  (format t "~&/Function WRITE-STRING, WRITE-LINE~%")
813  (write-string string *standard-output* :start 0 :end 5)
814  (write-string string *standard-output* :start 0 :end nil)
815  (assert (raises-error? (write-string string *standard-output*
816                                       :start 0 :end 6)))
817  (assert (raises-error? (write-string string *standard-output*
818                                       :start -1 :end 5)))
819  (assert (raises-error? (write-string string *standard-output*
820                                       :start 4 :end 2)))
821  (assert (raises-error? (write-string string *standard-output*
822                                       :start 6 :end 9)))
823  (write-line string *standard-output* :start 0 :end 5)
824  (write-line string *standard-output* :start 0 :end nil)
825  (assert (raises-error? (write-line string *standard-output*
826                                       :start 0 :end 6)))
827  (assert (raises-error? (write-line string *standard-output*
828                                       :start -1 :end 5)))
829  (assert (raises-error? (write-line string *standard-output*
830                                       :start 4 :end 2)))
831  (assert (raises-error? (write-line string *standard-output*
832                                       :start 6 :end 9))))
833
834 ;;; Macro WITH-INPUT-FROM-STRING
835 (sequence-bounding-indices-test
836  (format t "~&/Macro WITH-INPUT-FROM-STRING~%")
837  (with-input-from-string (s string :start 0 :end 5)
838    (assert (char= (read-char s) #\a)))
839  (with-input-from-string (s string :start 0 :end nil)
840    (assert (char= (read-char s) #\a)))
841  (assert (raises-error?
842           (with-input-from-string (s string :start 0 :end 6)
843             (read-char s))))
844  (assert (raises-error?
845           (with-input-from-string (s string :start -1 :end 5)
846             (read-char s))))
847  (assert (raises-error?
848           (with-input-from-string (s string :start 4 :end 2)
849             (read-char s))))
850  (assert (raises-error?
851           (with-input-from-string (s string :start 6 :end 9)
852             (read-char s)))))
853 \f
854 ;;; success
855 (quit :unix-status 104)