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