substitute-if-not.lsp 26.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Aug 31 18:17:09 2002
;;;; Contains: Tests for SUBSTITUTE-IF-NOT

(in-package :cl-test)

(deftest substitute-if-not-list.1
  (let ((x '())) (values (substitute-if-not 'b #'null x) x))
  nil nil)

(deftest substitute-if-not-list.2
13
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x) x))
14 15 16 17
  (b b b c)
  (a b a c))

(deftest substitute-if-not-list.3
18
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count nil) x))
19 20 21 22
  (b b b c)
  (a b a c))

(deftest substitute-if-not-list.4
23
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 2) x))
24 25 26 27
  (b b b c)
  (a b a c))

(deftest substitute-if-not-list.5
28
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 1) x))
29 30 31 32
  (b b a c)
  (a b a c))

(deftest substitute-if-not-list.6
33
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 0) x))
34 35 36 37
  (a b a c)
  (a b a c))

(deftest substitute-if-not-list.7
38
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count -1) x))
39 40 41 42
  (a b a c)
  (a b a c))

(deftest substitute-if-not-list.8
43
  (let ((x '())) (values (substitute-if-not 'b (is-not-eql-p 'a) x :from-end t) x))
44 45 46
  nil nil)

(deftest substitute-if-not-list.9
47
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :from-end t) x))
48 49 50 51
  (b b b c)
  (a b a c))

(deftest substitute-if-not-list.10
52
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :from-end t :count nil) x))
53 54 55 56
  (b b b c)
  (a b a c))

(deftest substitute-if-not-list.11
57
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 2 :from-end t) x))
58 59 60 61
  (b b b c)
  (a b a c))

(deftest substitute-if-not-list.12
62
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 1 :from-end t) x))
63 64 65 66
  (a b b c)
  (a b a c))

(deftest substitute-if-not-list.13
67
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 0 :from-end t) x))
68 69 70 71
  (a b a c)
  (a b a c))

(deftest substitute-if-not-list.14
72
  (let ((x '(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count -1 :from-end t) x))
73 74 75 76 77 78 79 80
  (a b a c)
  (a b a c))

(deftest substitute-if-not-list.15
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (let* ((orig '(a a a a a a a a a a))
		     (x (copy-seq orig))
81
		     (y (substitute-if-not 'x (is-not-eql-p 'a) x :start i :end j)))
82 83 84 85 86 87 88 89 90 91 92
		(and (equal orig x)
		     (equal y (nconc (make-list i :initial-element 'a)
				     (make-list (- j i) :initial-element 'x)
				     (make-list (- 10 j) :initial-element 'a)))))))
  t)

(deftest substitute-if-not-list.16
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (let* ((orig '(a a a a a a a a a a))
		     (x (copy-seq orig))
93
		     (y (substitute-if-not 'x (is-not-eql-p 'a) x :start i :end j :from-end t)))
94 95 96 97 98 99 100 101 102 103 104 105
		(and (equal orig x)
		     (equal y (nconc (make-list i :initial-element 'a)
				     (make-list (- j i) :initial-element 'x)
				     (make-list (- 10 j) :initial-element 'a)))))))
  t)

(deftest substitute-if-not-list.17
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (loop for c from 0 to (- j i) always
		    (let* ((orig '(a a a a a a a a a a))
			   (x (copy-seq orig))
106
			   (y (substitute-if-not 'x (is-not-eql-p 'a) x :start i :end j :count c)))
107 108 109 110 111 112 113 114 115 116 117 118
		      (and (equal orig x)
			   (equal y (nconc (make-list i :initial-element 'a)
					   (make-list c :initial-element 'x)
					   (make-list (- 10 (+ i c)) :initial-element 'a))))))))
  t)

(deftest substitute-if-not-list.18
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (loop for c from 0 to (- j i) always
		    (let* ((orig '(a a a a a a a a a a))
			   (x (copy-seq orig))
119
			   (y (substitute-if-not 'x (is-not-eql-p 'a) x :start i :end j :count c :from-end t)))
120 121 122 123 124 125 126 127 128
		      (and (equal orig x)
			   (equal y (nconc (make-list (- j c) :initial-element 'a)
					   (make-list c :initial-element 'x)
					   (make-list (- 10 j) :initial-element 'a))))))))
  t)

;;; Tests on vectors

(deftest substitute-if-not-vector.1
129
  (let ((x #())) (values (substitute-if-not 'b (is-not-eql-p 'a) x) x))
130 131 132
  #() #())

(deftest substitute-if-not-vector.2
133
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x) x))
134 135 136 137
  #(b b b c)
  #(a b a c))

(deftest substitute-if-not-vector.3
138
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count nil) x))
139 140 141 142
  #(b b b c)
  #(a b a c))

(deftest substitute-if-not-vector.4
143
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 2) x))
144 145 146 147
  #(b b b c)
  #(a b a c))

(deftest substitute-if-not-vector.5
148
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 1) x))
149 150 151 152
  #(b b a c)
  #(a b a c))

(deftest substitute-if-not-vector.6
153
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 0) x))
154 155 156 157
  #(a b a c)
  #(a b a c))

(deftest substitute-if-not-vector.7
158
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count -1) x))
159 160 161 162
  #(a b a c)
  #(a b a c))

(deftest substitute-if-not-vector.8
163
  (let ((x #())) (values (substitute-if-not 'b (is-not-eql-p 'a) x :from-end t) x))
164 165 166
  #() #())

(deftest substitute-if-not-vector.9
167
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :from-end t) x))
168 169 170 171
  #(b b b c)
  #(a b a c))

(deftest substitute-if-not-vector.10
172
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :from-end t :count nil) x))
173 174 175 176
  #(b b b c)
  #(a b a c))

(deftest substitute-if-not-vector.11
177
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 2 :from-end t) x))
178 179 180 181
  #(b b b c)
  #(a b a c))

(deftest substitute-if-not-vector.12
182
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 1 :from-end t) x))
183 184 185 186
  #(a b b c)
  #(a b a c))

(deftest substitute-if-not-vector.13
187
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count 0 :from-end t) x))
188 189 190 191
  #(a b a c)
  #(a b a c))

(deftest substitute-if-not-vector.14
192
  (let ((x #(a b a c))) (values (substitute-if-not 'b (is-not-eql-p 'a) x :count -1 :from-end t) x))
193 194 195 196 197 198 199 200
  #(a b a c)
  #(a b a c))

(deftest substitute-if-not-vector.15
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (let* ((orig #(a a a a a a a a a a))
		     (x (copy-seq orig))
201
		     (y (substitute-if-not 'x (is-not-eql-p 'a) x :start i :end j)))
202 203 204 205 206 207 208 209 210 211 212 213
		(and (equalp orig x)
		     (equalp y (concatenate 'simple-vector
					   (make-array i :initial-element 'a)
					   (make-array (- j i) :initial-element 'x)
					   (make-array (- 10 j) :initial-element 'a)))))))
  t)

(deftest substitute-if-not-vector.16
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (let* ((orig #(a a a a a a a a a a))
		     (x (copy-seq orig))
214
		     (y (substitute-if-not 'x (is-not-eql-p 'a) x :start i :end j :from-end t)))
215 216 217 218 219 220 221 222 223 224 225 226 227
		(and (equalp orig x)
		     (equalp y (concatenate 'simple-vector
					   (make-array i :initial-element 'a)
					   (make-array (- j i) :initial-element 'x)
					   (make-array (- 10 j) :initial-element 'a)))))))
  t)

(deftest substitute-if-not-vector.17
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (loop for c from 0 to (- j i) always
		    (let* ((orig #(a a a a a a a a a a))
			   (x (copy-seq orig))
228
			   (y (substitute-if-not 'x (is-not-eql-p 'a) x :start i :end j :count c)))
229 230 231 232 233 234 235 236 237 238 239 240 241
		      (and (equalp orig x)
			   (equalp y (concatenate 'simple-vector
						 (make-array i :initial-element 'a)
						 (make-array c :initial-element 'x)
						 (make-array (- 10 (+ i c)) :initial-element 'a))))))))
  t)

(deftest substitute-if-not-vector.18
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (loop for c from 0 to (- j i) always
		    (let* ((orig #(a a a a a a a a a a))
			   (x (copy-seq orig))
242
			   (y (substitute-if-not 'x (is-not-eql-p 'a) x :start i :end j :count c :from-end t)))
243 244 245 246 247 248 249
		      (and (equalp orig x)
			   (equalp y (concatenate 'simple-vector
						 (make-array (- j c) :initial-element 'a)
						 (make-array c :initial-element 'x)
						 (make-array (- 10 j) :initial-element 'a))))))))
  t)

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
(deftest substitute-if-not-vector.28
  (let* ((x (make-array '(10) :initial-contents '(a b a c b a d e a f)
		       :fill-pointer 5))
	 (result (substitute-if-not 'z (is-not-eql-p 'a) x)))
    result)
  #(z b z c b))

(deftest substitute-if-not-vector.29
  (let* ((x (make-array '(10) :initial-contents '(a b a c b a d e a f)
		       :fill-pointer 5))
	 (result (substitute-if-not 'z (is-not-eql-p 'a) x :from-end t)))
    result)
  #(z b z c b))

(deftest substitute-if-not-vector.30
  (let* ((x (make-array '(10) :initial-contents '(a b a c b a d e a f)
		       :fill-pointer 5))
	 (result (substitute-if-not 'z (is-not-eql-p 'a) x :count 1)))
    result)
  #(z b a c b))

(deftest substitute-if-not-vector.31
  (let* ((x (make-array '(10) :initial-contents '(a b a c b a d e a f)
		       :fill-pointer 5))
	 (result (substitute-if-not 'z (is-not-eql-p 'a) x
				    :from-end t :count 1)))
    result)
  #(a b z c b))

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
(deftest substitute-if-not-vector.32
  (let* ((v1 (copy-seq #(a b c d a b c d a b c d a b c d)))
	 (v2 (make-array '(8) :displaced-to v1
			 :displaced-index-offset 3)))
    (values
     (substitute-if-not 'x (is-not-eql-p 'c) v2 :count 1)
     v1))
  #(d a b x d a b c)
  #(a b c d a b c d a b c d a b c d))

(deftest substitute-if-not-vector.33
  (let* ((v1 (copy-seq #(a b c d a b c d a b c d a b c d)))
	 (v2 (make-array '(8) :displaced-to v1
			 :displaced-index-offset 3)))
    (values
     (substitute-if-not 'x (is-not-eql-p 'c) v2 :count 1 :from-end t)
     v1))
  #(d a b c d a b x)
  #(a b c d a b c d a b c d a b c d))


300

301 302 303
;;; Tests on strings

(deftest substitute-if-not-string.1
304
  (let ((x "")) (values (substitute-if-not #\b (is-not-eql-p #\a) x) x))
305 306 307
  "" "")

(deftest substitute-if-not-string.2
308
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x) x))
309 310 311 312
  "bbbc"
  "abac")

(deftest substitute-if-not-string.3
313
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :count nil) x))
314 315 316 317
  "bbbc"
  "abac")

(deftest substitute-if-not-string.4
318
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :count 2) x))
319 320 321 322
  "bbbc"
  "abac")

(deftest substitute-if-not-string.5
323
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :count 1) x))
324 325 326 327
  "bbac"
  "abac")

(deftest substitute-if-not-string.6
328
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :count 0) x))
329 330 331 332
  "abac"
  "abac")

(deftest substitute-if-not-string.7
333
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :count -1) x))
334 335 336 337
  "abac"
  "abac")

(deftest substitute-if-not-string.8
338
  (let ((x "")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :from-end t) x))
339 340 341
  "" "")

(deftest substitute-if-not-string.9
342
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :from-end t) x))
343 344 345 346
  "bbbc"
  "abac")

(deftest substitute-if-not-string.10
347
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :from-end t :count nil) x))
348 349 350 351
  "bbbc"
  "abac")

(deftest substitute-if-not-string.11
352
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :count 2 :from-end t) x))
353 354 355 356
  "bbbc"
  "abac")

(deftest substitute-if-not-string.12
357
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :count 1 :from-end t) x))
358 359 360 361
  "abbc"
  "abac")

(deftest substitute-if-not-string.13
362
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :count 0 :from-end t) x))
363 364 365 366
  "abac"
  "abac")

(deftest substitute-if-not-string.14
367
  (let ((x "abac")) (values (substitute-if-not #\b (is-not-eql-p #\a) x :count -1 :from-end t) x))
368 369 370 371 372 373 374 375
  "abac"
  "abac")

(deftest substitute-if-not-string.15
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (let* ((orig "aaaaaaaaaa")
		     (x (copy-seq orig))
376
		     (y (substitute-if-not #\x (is-not-eql-p #\a) x :start i :end j)))
377 378 379 380 381 382 383 384 385 386 387 388
		(and (equalp orig x)
		     (equalp y (concatenate 'simple-string
					   (make-array i :initial-element #\a)
					   (make-array (- j i) :initial-element #\x)
					   (make-array (- 10 j) :initial-element #\a)))))))
  t)

(deftest substitute-if-not-string.16
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (let* ((orig "aaaaaaaaaa")
		     (x (copy-seq orig))
389
		     (y (substitute-if-not #\x (is-not-eql-p #\a) x :start i :end j :from-end t)))
390 391 392 393 394 395 396 397 398 399 400 401 402
		(and (equalp orig x)
		     (equalp y (concatenate 'simple-string
					   (make-array i :initial-element #\a)
					   (make-array (- j i) :initial-element #\x)
					   (make-array (- 10 j) :initial-element #\a)))))))
  t)

(deftest substitute-if-not-string.17
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (loop for c from 0 to (- j i) always
		    (let* ((orig "aaaaaaaaaa")
			   (x (copy-seq orig))
403
			   (y (substitute-if-not #\x (is-not-eql-p #\a) x :start i :end j :count c)))
404 405 406 407 408 409 410 411 412 413 414 415 416
		      (and (equalp orig x)
			   (equalp y (concatenate 'simple-string
						 (make-array i :initial-element #\a)
						 (make-array c :initial-element #\x)
						 (make-array (- 10 (+ i c)) :initial-element #\a))))))))
  t)

(deftest substitute-if-not-string.18
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (loop for c from 0 to (- j i) always
		    (let* ((orig "aaaaaaaaaa")
			   (x (copy-seq orig))
417
			   (y (substitute-if-not #\x (is-not-eql-p #\a) x :start i :end j :count c :from-end t)))
418 419 420 421 422 423 424
		      (and (equalp orig x)
			   (equalp y (concatenate 'simple-string
						 (make-array (- j c) :initial-element #\a)
						 (make-array c :initial-element #\x)
						 (make-array (- 10 j) :initial-element #\a))))))))
  t)

425
(deftest substitute-if-not-string.28
426
  (let* ((x (make-array '(10) :initial-contents "abacbadeaf"
427 428 429 430 431 432
		       :fill-pointer 5 :element-type 'character))
	 (result (substitute-if-not #\z (is-not-eql-p #\a) x)))
    result)
  "zbzcb")

(deftest substitute-if-not-string.29
433
  (let* ((x (make-array '(10) :initial-contents "abacbadeaf"
434 435 436 437 438 439
		       :fill-pointer 5 :element-type 'character))
	 (result (substitute-if-not #\z (is-not-eql-p #\a) x :from-end t)))
    result)
  "zbzcb")

(deftest substitute-if-not-string.30
440
  (let* ((x (make-array '(10) :initial-contents "abacbadeaf"
441 442 443 444 445 446
		       :fill-pointer 5 :element-type 'character))
	 (result (substitute-if-not #\z (is-not-eql-p #\a) x :count 1)))
    result)
  "zbacb")

(deftest substitute-if-not-string.31
447
  (let* ((x (make-array '(10) :initial-contents "abacbadeaf"
448 449 450 451 452 453 454 455
		       :fill-pointer 5 :element-type 'character))
	 (result (substitute-if-not #\z (is-not-eql-p #\a) x
				    :from-end t :count 1)))
    result)
  "abzcb")



456 457 458 459 460
;;; Tests on bitstrings

(deftest substitute-if-not-bitstring.1
  (let* ((orig #*)
	 (x (copy-seq orig))
461
	 (result (substitute-if-not 0 (is-not-eql-p 1) x)))
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
    (and (equalp orig x)
	 result))
  #*)

(deftest substitute-if-not-bitstring.2
  (let* ((orig #*)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x)))
    (and (equalp orig x)
	 result))
  #*)

(deftest substitute-if-not-bitstring.3
  (let* ((orig #*010101)
	 (x (copy-seq orig))
477
	 (result (substitute-if-not 0 (is-not-eql-p 1) x)))
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
    (and (equalp orig x)
	 result))
  #*000000)

(deftest substitute-if-not-bitstring.4
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x)))
    (and (equalp orig x)
	 result))
  #*111111)

(deftest substitute-if-not-bitstring.5
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x :start 1)))
    (and (equalp orig x)
	 result))
  #*011111)
  
(deftest substitute-if-not-bitstring.6
  (let* ((orig #*010101)
	 (x (copy-seq orig))
501
	 (result (substitute-if-not 0 (is-not-eql-p 1) x :start 2 :end nil)))
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    (and (equalp orig x)
	 result))
  #*010000)

(deftest substitute-if-not-bitstring.7
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x :end 4)))
    (and (equalp orig x)
	 result))
  #*111101)
  
(deftest substitute-if-not-bitstring.8
  (let* ((orig #*010101)
	 (x (copy-seq orig))
517
	 (result (substitute-if-not 0 (is-not-eql-p 1) x :end nil)))
518 519 520 521 522 523 524
    (and (equalp orig x)
	 result))
  #*000000)

(deftest substitute-if-not-bitstring.9
  (let* ((orig #*010101)
	 (x (copy-seq orig))
525
	 (result (substitute-if-not 0 (is-not-eql-p 1) x :end 3)))
526 527 528 529 530 531 532
    (and (equalp orig x)
	 result))
  #*000101)

(deftest substitute-if-not-bitstring.10
  (let* ((orig #*010101)
	 (x (copy-seq orig))
533
	 (result (substitute-if-not 0 (is-not-eql-p 1) x :start 2 :end 4)))
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    (and (equalp orig x)
	 result))
  #*010001)

(deftest substitute-if-not-bitstring.11
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x :start 2 :end 4)))
    (and (equalp orig x)
	 result))
  #*011101)

(deftest substitute-if-not-bitstring.12
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x :count 1)))
    (and (equalp orig x)
	 result))
  #*110101)

(deftest substitute-if-not-bitstring.13
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x :count 0)))
    (and (equalp orig x)
	 result))
  #*010101)

(deftest substitute-if-not-bitstring.14
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x :count -1)))
    (and (equalp orig x)
	 result))
  #*010101)

(deftest substitute-if-not-bitstring.15
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x :count 1 :from-end t)))
    (and (equalp orig x)
	 result))
  #*010111)

(deftest substitute-if-not-bitstring.16
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x :count 0 :from-end t)))
    (and (equalp orig x)
	 result))
  #*010101)

(deftest substitute-if-not-bitstring.17
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x :count -1 :from-end t)))
    (and (equalp orig x)
	 result))
  #*010101)

(deftest substitute-if-not-bitstring.18
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x :count nil)))
    (and (equalp orig x)
	 result))
  #*111111)

(deftest substitute-if-not-bitstring.19
  (let* ((orig #*010101)
	 (x (copy-seq orig))
	 (result (substitute-if-not 1 (complement #'zerop) x :count nil :from-end t)))
    (and (equalp orig x)
	 result))
  #*111111)

(deftest substitute-if-not-bitstring.20
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (loop for c from 0 to (- j i) always
		    (let* ((orig #*0000000000)
			   (x (copy-seq orig))
			   (y (substitute-if-not 1 (complement #'zerop) x :start i :end j :count c)))
		      (and (equalp orig x)
			   (equalp y (concatenate
				      'simple-bit-vector
				      (make-list i :initial-element 0)
				      (make-list c :initial-element 1)
				      (make-list (- 10 (+ i c)) :initial-element 0))))))))
  t)

(deftest substitute-if-not-bitstring.21
  (loop for i from 0 to 9 always
	(loop for j from i to 10 always
	      (loop for c from 0 to (- j i) always
		    (let* ((orig #*1111111111)
			   (x (copy-seq orig))
631
			   (y (substitute-if-not 0 (is-not-eql-p 1) x :start i :end j :count c :from-end t)))
632 633 634 635 636 637 638 639 640 641 642 643 644
		      (and (equalp orig x)
			   (equalp y (concatenate
				      'simple-bit-vector
				      (make-list (- j c) :initial-element 1)
				      (make-list c :initial-element 0)
				      (make-list (- 10 j) :initial-element 1))))))))
  t)

;;; More tests

(deftest substitute-if-not-list.24
  (let* ((orig '((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
	 (x (copy-seq orig))
645
	 (result (substitute-if-not '(a 10) (is-not-eql-p 'a) x :key #'car)))
646 647 648 649 650 651 652
    (and (equal orig x)
	 result))
  ((a 10) (b 2) (a 10) (c 4) (d 5) (a 10) (e 7)))

(deftest substitute-if-not-list.25
  (let* ((orig '((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
	 (x (copy-seq orig))
653
	 (result (substitute-if-not '(a 10) (is-not-eql-p 'a) x
654 655 656 657 658 659 660 661
				:key #'car :start 1 :end 5)))
    (and (equal orig x)
	 result))
  ((a 1) (b 2) (a 10) (c 4) (d 5) (a 6) (e 7)))

(deftest substitute-if-not-vector.24
  (let* ((orig #((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
	 (x (copy-seq orig))
662
	 (result (substitute-if-not '(a 10) (is-not-eql-p 'a) x :key #'car)))
663 664 665 666 667 668 669
    (and (equalp orig x)
	 result))
  #((a 10) (b 2) (a 10) (c 4) (d 5) (a 10) (e 7)))
    
(deftest substitute-if-not-vector.25
  (let* ((orig #((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
	 (x (copy-seq orig))
670
	 (result (substitute-if-not '(a 10) (is-not-eql-p 'a) x :key #'car :start 1 :end 5)))
671 672 673 674 675 676 677
    (and (equalp orig x)
	 result))
  #((a 1) (b 2) (a 10) (c 4) (d 5) (a 6) (e 7)))

(deftest substitute-if-not-string.24
  (let* ((orig "0102342015")
	 (x (copy-seq orig))
678
	 (result (substitute-if-not #\a (is-not-eql-p #\1) x :key #'nextdigit)))
679 680 681 682 683 684 685
    (and (equalp orig x)
	 result))
  "a1a2342a15")
    
(deftest substitute-if-not-string.25
  (let* ((orig "0102342015")
	 (x (copy-seq orig))
686
	 (result (substitute-if-not #\a (is-not-eql-p #\1) x :key #'nextdigit :start 1 :end 6)))
687 688 689 690
    (and (equalp orig x)
	 result))
  "01a2342015")

691 692 693 694 695 696 697 698 699
(deftest substitute-if-not-string.26
  (do-special-strings
   (s "xyzabcxyzabc" nil)
   (assert (string= (substitute-if-not #\! (is-not-eql-p #\a) s) "xyz!bcxyz!bc"))
   (assert (string= (substitute-if-not #\! (is-not-eql-p #\a) s :count 1) "xyz!bcxyzabc"))
   (assert (string= (substitute-if-not #\! (is-not-eql-p #\a) s :count 1 :from-end t) "xyzabcxyz!bc"))
   (assert (string= s "xyzabcxyzabc")))
  nil)

700 701 702
(deftest substitute-if-not-bitstring.26
  (let* ((orig #*00111001011010110)
	 (x (copy-seq orig))
703
	 (result (substitute-if-not 1 (is-not-eql-p 1) x :key #'1+)))
704 705 706 707 708 709 710
    (and (equalp orig x)
	 result))
  #*11111111111111111)
    
(deftest substitute-if-not-bitstring.27
  (let* ((orig #*00111001011010110)
	 (x (copy-seq orig))
711
	 (result (substitute-if-not 1 (is-not-eql-p 1) x :key #'1+ :start 1 :end 10)))
712 713 714
    (and (equalp orig x)
	 result))
  #*01111111111010110)
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

(deftest substitute-if-not-bit-vector.30
  (let* ((x (make-array '(10) :initial-contents '(0 1 0 1 1 0 1 1 0 1)
		       :fill-pointer 5 :element-type 'bit))
	 (result (substitute-if-not 1 #'onep x)))
    result)
  #*11111)

(deftest substitute-if-not-bit-vector.31
  (let* ((x (make-array '(10) :initial-contents '(0 1 0 1 1 0 1 1 0 1)
		       :fill-pointer 5 :element-type 'bit))
	 (result (substitute-if-not 1 #'onep x :from-end t)))
    result)
  #*11111)

(deftest substitute-if-not-bit-vector.32
  (let* ((x (make-array '(10) :initial-contents '(0 1 0 1 1 0 1 1 0 1)
		       :fill-pointer 5 :element-type 'bit))
	 (result (substitute-if-not 1 #'onep x :count 1)))
    result)
  #*11011)

(deftest substitute-if-not-bit-vector.33
  (let* ((x (make-array '(10) :initial-contents '(0 1 0 1 1 0 1 1 0 1)
		       :fill-pointer 5 :element-type 'bit))
	 (result (substitute-if-not 1 #'onep x :from-end t :count 1)))
    result)
  #*01111)
743

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
(deftest substitute-if-not.order.1
  (let ((i 0) a b c d e f g h)
    (values
     (substitute-if-not
      (progn (setf a (incf i)) 'a)
      (progn (setf b (incf i)) #'identity)
      (progn (setf c (incf i)) (list nil 1 2 nil 3 4 nil 5))
      :count (progn (setf d (incf i)) 2)
      :start (progn (setf e (incf i)) 0)
      :end (progn (setf f (incf i)) 7)
      :key (progn (setf g (incf i)) #'identity)
      :from-end (setf h (incf i))
      )
     i a b c d e f g h))
  (nil 1 2 a 3 4 a 5)
  8 1 2 3 4 5 6 7 8)

(deftest substitute-if-not.order.2
  (let ((i 0) a b c d e f g h)
    (values
     (substitute-if-not
      (progn (setf a (incf i)) 'a)
      (progn (setf b (incf i)) #'identity)
      (progn (setf c (incf i)) (list nil 1 2 nil 3 4 nil 5))
      :from-end (setf h (incf i))
      :key (progn (setf g (incf i)) #'identity)
      :end (progn (setf f (incf i)) 7)
      :start (progn (setf e (incf i)) 0)
      :count (progn (setf d (incf i)) 2)
      )
     i a b c d e f g h))
  (nil 1 2 a 3 4 a 5)
  8 1 2 3 8 7 6 5 4)

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
;;; Keyword tests

(deftest substitute-if-not.allow-other-keys.1
  (substitute-if-not 'a #'zerop (list 1 2 0 3 1 0 3)
		      :allow-other-keys t :bad t)
  (a a 0 a a 0 a))

(deftest substitute-if-not.allow-other-keys.2
  (substitute-if-not 'a #'zerop (list 1 2 0 3 1 0 3)
		      :bad t :allow-other-keys t)
  (a a 0 a a 0 a))

(deftest substitute-if-not.allow-other-keys.3
  (substitute-if-not 'a #'zerop (list 1 2 0 3 1 0 3) :allow-other-keys t
		  :allow-other-keys nil :bad t)
  (a a 0 a a 0 a))

(deftest substitute-if-not.allow-other-keys.4
  (substitute-if-not 'a #'zerop (list 1 2 0 3 1 0 3) :bad t
		  :allow-other-keys t :allow-other-keys nil)
  (a a 0 a a 0 a))

(deftest substitute-if-not.allow-other-keys.5
  (substitute-if-not 'a #'zerop (list 1 2 0 3 1 0 3)
		  :allow-other-keys t :key #'1-)
  (1 a a a 1 a a))

(deftest substitute-if-not.keywords.6
  (substitute-if-not 'a #'zerop (list 1 2 0 3 1 0 3)
		      :key #'1- :key #'identity)
  (1 a a a 1 a a))

(deftest substitute-if-not.allow-other-keys.7
  (substitute-if-not 'a #'zerop (list 1 2 0 3 1 0 3) :allow-other-keys t
		  :bad t :allow-other-keys nil)
  (a a 0 a a 0 a))

(deftest substitute-if-not.allow-other-keys.8
  (substitute-if-not 'a #'zerop (list 1 2 0 3 1 0 3) :allow-other-keys nil)
  (a a 0 a a 0 a))

819 820 821
;;; Error cases

(deftest substitute-if-not.error.1
822 823
  (signals-error (substitute-if-not) program-error)
  t)
824 825

(deftest substitute-if-not.error.2
826 827
  (signals-error (substitute-if-not 'a) program-error)
  t)
828 829

(deftest substitute-if-not.error.3
830 831
  (signals-error (substitute-if-not 'a #'null) program-error)
  t)
832 833

(deftest substitute-if-not.error.4
834 835
  (signals-error (substitute-if-not 'a #'null nil 'bad t) program-error)
  t)
836 837

(deftest substitute-if-not.error.5
838 839 840
  (signals-error (substitute-if-not 'a #'null nil
				      'bad t :allow-other-keys nil) program-error)
  t)
841 842

(deftest substitute-if-not.error.6
843 844
  (signals-error (substitute-if-not 'a #'null nil :key) program-error)
  t)
845 846

(deftest substitute-if-not.error.7
847 848
  (signals-error (substitute-if-not 'a #'null nil 1 2) program-error)
  t)
849 850

(deftest substitute-if-not.error.8
851 852
  (signals-error (substitute-if-not 'a #'cons (list 'a 'b 'c)) program-error)
  t)
853 854

(deftest substitute-if-not.error.9
855 856
  (signals-error (substitute-if-not 'a #'car (list 'a 'b 'c)) type-error)
  t)
857 858

(deftest substitute-if-not.error.10
859 860 861 862
  (signals-error (substitute-if-not 'a #'identity (list 'a 'b 'c)
				  :key #'car)
		 type-error)
  t)
863 864

(deftest substitute-if-not.error.11
865 866 867 868
  (signals-error (substitute-if-not 'a #'identity (list 'a 'b 'c)
				  :key #'cons)
		 program-error)
  t)
869

870 871 872
(deftest substitute-if-not.error.12
  (check-type-error #'(lambda (x) (substitute-if-not 'a #'not x)) #'sequencep)
  nil)