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