nsubstitute.lsp 32.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Aug 31 16:56:48 2002
;;;; Contains: Tests for NSUBSTITUTE

(in-package :cl-test)

(deftest nsubstitute-list.1
  (nsubstitute 'b 'a nil)
  nil)

(deftest nsubstitute-list.2
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x) x)
  (b b b c))

(deftest nsubstitute-list.3
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x :count nil))
  (b b b c))

(deftest nsubstitute-list.4
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x :count 2))
  (b b b c))

(deftest nsubstitute-list.5
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x :count 1))
  (b b a c))

(deftest nsubstitute-list.6
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x :count 0))
  (a b a c))

(deftest nsubstitute-list.7
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x :count -1))
  (a b a c))

(deftest nsubstitute-list.8
  (nsubstitute 'b 'a nil :from-end t)
  nil)

(deftest nsubstitute-list.9
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x :from-end t))
  (b b b c))

(deftest nsubstitute-list.10
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x :from-end t :count nil))
  (b b b c))

(deftest nsubstitute-list.11
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x :count 2 :from-end t))
  (b b b c))

(deftest nsubstitute-list.12
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x :count 1 :from-end t))
  (a b b c))

(deftest nsubstitute-list.13
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x :count 0 :from-end t))
  (a b a c))

(deftest nsubstitute-list.14
  (let ((x (copy-seq '(a b a c)))) (nsubstitute 'b 'a x :count -1 :from-end t))
  (a b a c))

(deftest nsubstitute-list.15
  (loop for i from 0 to 9 always
sds's avatar
sds committed
66 67 68 69 70 71 72
        (loop for j from i to 10 always
              (let* ((orig '(a a a a a a a a a a))
                     (x (copy-seq orig))
                     (y (nsubstitute 'x 'a x :start i :end j)))
                (equal y (nconc (make-list i :initial-element 'a)
                                (make-list (- j i) :initial-element 'x)
                                (make-list (- 10 j) :initial-element 'a))))))
73 74 75 76
  t)

(deftest nsubstitute-list.16
  (loop for i from 0 to 9 always
sds's avatar
sds committed
77 78 79 80 81 82 83
        (loop for j from i to 10 always
              (let* ((orig '(a a a a a a a a a a))
                     (x (copy-seq orig))
                     (y (nsubstitute 'x 'a x :start i :end j :from-end t)))
                (equal y (nconc (make-list i :initial-element 'a)
                                (make-list (- j i) :initial-element 'x)
                                (make-list (- 10 j) :initial-element 'a))))))
84 85 86 87
  t)

(deftest nsubstitute-list.17
  (loop for i from 0 to 9 always
sds's avatar
sds committed
88 89 90 91 92 93 94 95
        (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))
                           (y (nsubstitute 'x 'a x :start i :end j :count c)))
                      (equal y (nconc (make-list i :initial-element 'a)
                                      (make-list c :initial-element 'x)
                                      (make-list (- 10 (+ i c)) :initial-element 'a)))))))
96 97 98 99
  t)

(deftest nsubstitute-list.18
  (loop for i from 0 to 9 always
sds's avatar
sds committed
100 101 102 103 104 105 106 107
        (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))
                           (y (nsubstitute 'x 'a x :start i :end j :count c :from-end t)))
                      (equal y (nconc (make-list (- j c) :initial-element 'a)
                                      (make-list c :initial-element 'x)
                                      (make-list (- 10 j) :initial-element 'a)))))))
108 109 110 111
  t)

(deftest nsubstitute-list.19
  (let* ((orig '(1 2 3 4 5 6 7 8 9))
sds's avatar
sds committed
112 113
         (x (copy-seq orig))
         (result (nsubstitute 'x 5 x :test #'(lambda (a b) (<= (abs (- a b)) 2)))))
114 115 116 117 118
    result)
  (1 2 x x x x x 8 9))

(deftest nsubstitute-list.20
  (let* ((orig '(1 2 3 4 5 6 7 8 9))
sds's avatar
sds committed
119 120 121
         (x (copy-seq orig))
         (c -4)
         (result (nsubstitute 'x 5 x :test #'(lambda (a b) (incf c 2) (= (+ b c) a)))))
122 123 124 125 126 127
    result)
  (1 2 x 4 5 6 7 8 9))


(deftest nsubstitute-list.21
  (let* ((orig '(1 2 3 4 5 6 7 8 9))
sds's avatar
sds committed
128 129 130 131
         (x (copy-seq orig))
         (c 5)
         (result (nsubstitute 'x 9 x :test #'(lambda (a b) (incf c -2) (= (+ b c) a))
                             :from-end t)))
132 133 134 135 136
    result)
  (1 2 3 4 5 6 7 x 9))

(deftest nsubstitute-list.22
  (let* ((orig '(1 2 3 4 5 6 7 8 9))
sds's avatar
sds committed
137 138 139
         (x (copy-seq orig))
         (c -4)
         (result (nsubstitute 'x 5 x :test-not #'(lambda (a b) (incf c 2) (/= (+ b c) a)))))
140 141 142 143 144 145
    result)
  (1 2 x 4 5 6 7 8 9))


(deftest nsubstitute-list.23
  (let* ((orig '(1 2 3 4 5 6 7 8 9))
sds's avatar
sds committed
146 147 148 149
         (x (copy-seq orig))
         (c 5)
         (result (nsubstitute 'x 9 x :test-not #'(lambda (a b) (incf c -2) (/= (+ b c) a))
                             :from-end t)))
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    result)
  (1 2 3 4 5 6 7 x 9))

;;; Tests on vectors

(deftest nsubstitute-vector.1
  (let ((x #())) (values (nsubstitute 'b 'a x) x))
  #() #())

(deftest nsubstitute-vector.2
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x))
  #(b b b c))

(deftest nsubstitute-vector.3
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x :count nil) x)
  #(b b b c))

(deftest nsubstitute-vector.4
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x :count 2))
  #(b b b c))

(deftest nsubstitute-vector.5
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x :count 1))
  #(b b a c))

(deftest nsubstitute-vector.6
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x :count 0))
  #(a b a c))

(deftest nsubstitute-vector.7
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x :count -1))
  #(a b a c))

(deftest nsubstitute-vector.8
  (let ((x #())) (nsubstitute 'b 'a x :from-end t))
  #())

(deftest nsubstitute-vector.9
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x :from-end t))
  #(b b b c))

(deftest nsubstitute-vector.10
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x :from-end t :count nil))
  #(b b b c))

(deftest nsubstitute-vector.11
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x :count 2 :from-end t))
  #(b b b c))

(deftest nsubstitute-vector.12
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x :count 1 :from-end t))
  #(a b b c))

(deftest nsubstitute-vector.13
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x :count 0 :from-end t))
  #(a b a c))

(deftest nsubstitute-vector.14
  (let ((x (copy-seq #(a b a c)))) (nsubstitute 'b 'a x :count -1 :from-end t))
  #(a b a c))

(deftest nsubstitute-vector.15
  (loop for i from 0 to 9 always
sds's avatar
sds committed
213 214 215 216 217 218 219 220
        (loop for j from i to 10 always
              (let* ((orig #(a a a a a a a a a a))
                     (x (copy-seq orig))
                     (y (nsubstitute 'x 'a x :start i :end j)))
                (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))))))
221 222 223 224
  t)

(deftest nsubstitute-vector.16
  (loop for i from 0 to 9 always
sds's avatar
sds committed
225 226 227 228 229 230 231 232
        (loop for j from i to 10 always
              (let* ((orig #(a a a a a a a a a a))
                     (x (copy-seq orig))
                     (y (nsubstitute 'x 'a x :start i :end j :from-end t)))
                (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))))))
233 234 235 236
  t)

(deftest nsubstitute-vector.17
  (loop for i from 0 to 9 always
sds's avatar
sds committed
237 238 239 240 241 242 243 244 245
        (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))
                           (y (nsubstitute 'x 'a x :start i :end j :count c)))
                      (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)))))))
246 247 248 249
  t)

(deftest nsubstitute-vector.18
  (loop for i from 0 to 9 always
sds's avatar
sds committed
250 251 252 253 254 255 256 257 258
        (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))
                           (y (nsubstitute 'x 'a x :start i :end j :count c :from-end t)))
                      (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)))))))
259 260 261 262
  t)

(deftest nsubstitute-vector.19
  (let* ((orig #(1 2 3 4 5 6 7 8 9))
sds's avatar
sds committed
263 264
         (x (copy-seq orig))
         (result (nsubstitute 'x 5 x :test #'(lambda (a b) (<= (abs (- a b)) 2)))))
265 266 267 268 269
    result)
  #(1 2 x x x x x 8 9))

(deftest nsubstitute-vector.20
  (let* ((orig #(1 2 3 4 5 6 7 8 9))
sds's avatar
sds committed
270 271 272
         (x (copy-seq orig))
         (c -4)
         (result (nsubstitute 'x 5 x :test #'(lambda (a b) (incf c 2) (= (+ b c) a)))))
273 274 275 276 277 278
    result)
  #(1 2 x 4 5 6 7 8 9))


(deftest nsubstitute-vector.21
  (let* ((orig #(1 2 3 4 5 6 7 8 9))
sds's avatar
sds committed
279 280 281 282
         (x (copy-seq orig))
         (c 5)
         (result (nsubstitute 'x 9 x :test #'(lambda (a b) (incf c -2) (= (+ b c) a))
                             :from-end t)))
283 284 285 286 287
    result)
  #(1 2 3 4 5 6 7 x 9))

(deftest nsubstitute-vector.22
  (let* ((orig #(1 2 3 4 5 6 7 8 9))
sds's avatar
sds committed
288 289 290
         (x (copy-seq orig))
         (c -4)
         (result (nsubstitute 'x 5 x :test-not #'(lambda (a b) (incf c 2) (/= (+ b c) a)))))
291 292 293 294 295
    result)
  #(1 2 x 4 5 6 7 8 9))

(deftest nsubstitute-vector.23
  (let* ((orig #(1 2 3 4 5 6 7 8 9))
sds's avatar
sds committed
296 297 298 299
         (x (copy-seq orig))
         (c 5)
         (result (nsubstitute 'x 9 x :test-not #'(lambda (a b) (incf c -2) (/= (+ b c) a))
                             :from-end t)))
300 301 302
    result)
  #(1 2 3 4 5 6 7 x 9))

303 304
(deftest nsubstitute-vector.28
  (let* ((x (make-array '(10) :initial-contents '(a b a c b a d e a f)
sds's avatar
sds committed
305 306
                       :fill-pointer 5))
         (result (nsubstitute 'z 'a x)))
307 308 309 310 311
    result)
  #(z b z c b))

(deftest nsubstitute-vector.29
  (let* ((x (make-array '(10) :initial-contents '(a b a c b a d e a f)
sds's avatar
sds committed
312 313
                       :fill-pointer 5))
         (result (nsubstitute 'z 'a x :from-end t)))
314 315 316 317 318
    result)
  #(z b z c b))

(deftest nsubstitute-vector.30
  (let* ((x (make-array '(10) :initial-contents '(a b a c b a d e a f)
sds's avatar
sds committed
319 320
                       :fill-pointer 5))
         (result (nsubstitute 'z 'a x :count 1)))
321 322 323 324 325
    result)
  #(z b a c b))

(deftest nsubstitute-vector.31
  (let* ((x (make-array '(10) :initial-contents '(a b a c b a d e a f)
sds's avatar
sds committed
326 327
                       :fill-pointer 5))
         (result (nsubstitute 'z 'a x :from-end t :count 1)))
328 329 330
    result)
  #(a b z c b))

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
;;; Tests on strings

(deftest nsubstitute-string.1
  (let ((x "")) (nsubstitute #\b #\a x))
  "")

(deftest nsubstitute-string.2
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x))
  "bbbc")

(deftest nsubstitute-string.3
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x :count nil))
  "bbbc")

(deftest nsubstitute-string.4
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x :count 2))
  "bbbc")

(deftest nsubstitute-string.5
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x :count 1))
  "bbac")

(deftest nsubstitute-string.6
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x :count 0))
  "abac")

(deftest nsubstitute-string.7
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x :count -1))
  "abac")

(deftest nsubstitute-string.8
  (let ((x "")) (nsubstitute #\b #\a x :from-end t))
  "")

(deftest nsubstitute-string.9
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x :from-end t))
  "bbbc")

(deftest nsubstitute-string.10
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x :from-end t :count nil))
  "bbbc")

(deftest nsubstitute-string.11
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x :count 2 :from-end t))
  "bbbc")

(deftest nsubstitute-string.12
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x :count 1 :from-end t))
  "abbc")

(deftest nsubstitute-string.13
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x :count 0 :from-end t))
  "abac")

(deftest nsubstitute-string.14
  (let ((x (copy-seq "abac"))) (nsubstitute #\b #\a x :count -1 :from-end t))
  "abac")

(deftest nsubstitute-string.15
  (loop for i from 0 to 9 always
sds's avatar
sds committed
391 392 393 394 395 396 397 398
        (loop for j from i to 10 always
              (let* ((orig "aaaaaaaaaa")
                     (x (copy-seq orig))
                     (y (nsubstitute #\x #\a x :start i :end j)))
                (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))))))
399 400 401 402
  t)

(deftest nsubstitute-string.16
  (loop for i from 0 to 9 always
sds's avatar
sds committed
403 404 405 406 407 408 409 410
        (loop for j from i to 10 always
              (let* ((orig "aaaaaaaaaa")
                     (x (copy-seq orig))
                     (y (nsubstitute #\x #\a x :start i :end j :from-end t)))
                (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))))))
411 412 413 414
  t)

(deftest nsubstitute-string.17
  (loop for i from 0 to 9 always
sds's avatar
sds committed
415 416 417 418 419 420 421 422 423
        (loop for j from i to 10 always
              (loop for c from 0 to (- j i) always
                    (let* ((orig "aaaaaaaaaa")
                           (x (copy-seq orig))
                           (y (nsubstitute #\x #\a x :start i :end j :count c)))
                      (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)))))))
424 425 426 427
  t)

(deftest nsubstitute-string.18
  (loop for i from 0 to 9 always
sds's avatar
sds committed
428 429 430 431 432 433 434 435 436
        (loop for j from i to 10 always
              (loop for c from 0 to (- j i) always
                    (let* ((orig "aaaaaaaaaa")
                           (x (copy-seq orig))
                           (y (nsubstitute #\x #\a x :start i :end j :count c :from-end t)))
                      (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)))))))
437 438 439 440
  t)

(deftest nsubstitute-string.19
  (let* ((orig "123456789")
sds's avatar
sds committed
441 442 443 444 445
         (x (copy-seq orig))
         (result (nsubstitute #\x #\5 x :test #'(lambda (a b)
                                                 (setq a (read-from-string (string a)))
                                                 (setq b (read-from-string (string b)))
                                                 (<= (abs (- a b)) 2)))))
446 447 448 449 450
    result)
  "12xxxxx89")

(deftest nsubstitute-string.20
  (let* ((orig "123456789")
sds's avatar
sds committed
451 452 453 454 455 456
         (x (copy-seq orig))
         (c -4)
         (result (nsubstitute #\x #\5 x :test #'(lambda (a b)
                                                 (setq a (read-from-string (string a)))
                                                 (setq b (read-from-string (string b)))
                                                 (incf c 2) (= (+ b c) a)))))
457 458 459 460 461 462
    result)
  "12x456789")


(deftest nsubstitute-string.21
  (let* ((orig "123456789")
sds's avatar
sds committed
463 464 465 466 467 468 469
         (x (copy-seq orig))
         (c 5)
         (result (nsubstitute #\x #\9 x :test #'(lambda (a b)
                                                 (setq a (read-from-string (string a)))
                                                 (setq b (read-from-string (string b)))
                                                 (incf c -2) (= (+ b c) a))
                             :from-end t)))
470 471 472 473 474
    result)
  "1234567x9")

(deftest nsubstitute-string.22
  (let* ((orig "123456789")
sds's avatar
sds committed
475 476 477 478 479 480
         (x (copy-seq orig))
         (c -4)
         (result (nsubstitute #\x #\5 x :test-not #'(lambda (a b)
                                                     (setq a (read-from-string (string a)))
                                                     (setq b (read-from-string (string b)))
                                                     (incf c 2) (/= (+ b c) a)))))
481 482 483 484 485 486
    result)
  "12x456789")


(deftest nsubstitute-string.23
  (let* ((orig "123456789")
sds's avatar
sds committed
487 488 489 490 491 492 493
         (x (copy-seq orig))
         (c 5)
         (result (nsubstitute #\x #\9 x :test-not #'(lambda (a b)
                                                     (setq a (read-from-string (string a)))
                                                     (setq b (read-from-string (string b)))
                                                     (incf c -2) (/= (+ b c) a))
                             :from-end t)))
494 495 496
    result)
  "1234567x9")

497
(deftest nsubstitute-string.28
498
  (let* ((x (make-array '(10) :initial-contents "abacbadeaf"
sds's avatar
sds committed
499 500
                       :fill-pointer 5 :element-type 'character))
         (result (nsubstitute #\z #\a x)))
501 502 503 504
    result)
  "zbzcb")

(deftest nsubstitute-string.29
505
  (let* ((x (make-array '(10) :initial-contents "abacbadeaf"
sds's avatar
sds committed
506 507
                       :fill-pointer 5 :element-type 'character))
         (result (nsubstitute #\z #\a x :from-end t)))
508 509
    result)
  "zbzcb")
510

511
(deftest nsubstitute-string.30
512
  (let* ((x (make-array '(10) :initial-contents "abacbadeaf"
sds's avatar
sds committed
513 514
                       :fill-pointer 5 :element-type 'character))
         (result (nsubstitute #\z #\a x :count 1)))
515 516 517 518
    result)
  "zbacb")

(deftest nsubstitute-string.31
519
  (let* ((x (make-array '(10) :initial-contents "abacbadeaf"
sds's avatar
sds committed
520 521
                       :fill-pointer 5 :element-type 'character))
         (result (nsubstitute #\z #\a x :from-end t :count 1)))
522 523 524 525 526 527 528
    result)
  "abzcb")


;;; Tests on bit-vectors

(deftest nsubstitute-bit-vector.1
529
  (let* ((orig #*)
sds's avatar
sds committed
530 531
         (x (copy-seq orig))
         (result (nsubstitute 0 1 x)))
532 533 534
    result)
  #*)

535
(deftest nsubstitute-bit-vector.2
536
  (let* ((orig #*)
sds's avatar
sds committed
537 538
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x)))
539 540 541
    result)
  #*)

542
(deftest nsubstitute-bit-vector.3
543
  (let* ((orig #*010101)
sds's avatar
sds committed
544 545
         (x (copy-seq orig))
         (result (nsubstitute 0 1 x)))
546 547 548
    result)
  #*000000)

549
(deftest nsubstitute-bit-vector.4
550
  (let* ((orig #*010101)
sds's avatar
sds committed
551 552
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x)))
553 554 555
    result)
  #*111111)

556
(deftest nsubstitute-bit-vector.5
557
  (let* ((orig #*010101)
sds's avatar
sds committed
558 559
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x :start 1)))
560 561
    result)
  #*011111)
sds's avatar
sds committed
562

563
(deftest nsubstitute-bit-vector.6
564
  (let* ((orig #*010101)
sds's avatar
sds committed
565 566
         (x (copy-seq orig))
         (result (nsubstitute 0 1 x :start 2 :end nil)))
567 568 569
    result)
  #*010000)

570
(deftest nsubstitute-bit-vector.7
571
  (let* ((orig #*010101)
sds's avatar
sds committed
572 573
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x :end 4)))
574 575
    result)
  #*111101)
sds's avatar
sds committed
576

577
(deftest nsubstitute-bit-vector.8
578
  (let* ((orig #*010101)
sds's avatar
sds committed
579 580
         (x (copy-seq orig))
         (result (nsubstitute 0 1 x :end nil)))
581 582 583
    result)
  #*000000)

584
(deftest nsubstitute-bit-vector.9
585
  (let* ((orig #*010101)
sds's avatar
sds committed
586 587
         (x (copy-seq orig))
         (result (nsubstitute 0 1 x :end 3)))
588 589 590
    result)
  #*000101)

591
(deftest nsubstitute-bit-vector.10
592
  (let* ((orig #*010101)
sds's avatar
sds committed
593 594
         (x (copy-seq orig))
         (result (nsubstitute 0 1 x :start 2 :end 4)))
595 596 597
    result)
  #*010001)

598
(deftest nsubstitute-bit-vector.11
599
  (let* ((orig #*010101)
sds's avatar
sds committed
600 601
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x :start 2 :end 4)))
602 603 604
    result)
  #*011101)

605
(deftest nsubstitute-bit-vector.12
606
  (let* ((orig #*010101)
sds's avatar
sds committed
607 608
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x :count 1)))
609 610 611
    result)
  #*110101)

612
(deftest nsubstitute-bit-vector.13
613
  (let* ((orig #*010101)
sds's avatar
sds committed
614 615
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x :count 0)))
616 617 618
    result)
  #*010101)

619
(deftest nsubstitute-bit-vector.14
620
  (let* ((orig #*010101)
sds's avatar
sds committed
621 622
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x :count -1)))
623 624 625
    result)
  #*010101)

626
(deftest nsubstitute-bit-vector.15
627
  (let* ((orig #*010101)
sds's avatar
sds committed
628 629
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x :count 1 :from-end t)))
630 631 632
    result)
  #*010111)

633
(deftest nsubstitute-bit-vector.16
634
  (let* ((orig #*010101)
sds's avatar
sds committed
635 636
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x :count 0 :from-end t)))
637 638 639
    result)
  #*010101)

640
(deftest nsubstitute-bit-vector.17
641
  (let* ((orig #*010101)
sds's avatar
sds committed
642 643
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x :count -1 :from-end t)))
644 645 646
    result)
  #*010101)

647
(deftest nsubstitute-bit-vector.18
648
  (let* ((orig #*010101)
sds's avatar
sds committed
649 650
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x :count nil)))
651 652 653
    result)
  #*111111)

654
(deftest nsubstitute-bit-vector.19
655
  (let* ((orig #*010101)
sds's avatar
sds committed
656 657
         (x (copy-seq orig))
         (result (nsubstitute 1 0 x :count nil :from-end t)))
658 659 660
    result)
  #*111111)

661
(deftest nsubstitute-bit-vector.20
662
  (loop for i from 0 to 9 always
sds's avatar
sds committed
663 664 665 666 667 668 669 670 671 672
        (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 (nsubstitute 1 0 x :start i :end j :count c)))
                      (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)))))))
673 674
  t)

675
(deftest nsubstitute-bit-vector.21
676
  (loop for i from 0 to 9 always
sds's avatar
sds committed
677 678 679 680 681 682 683 684 685 686
        (loop for j from i to 10 always
              (loop for c from 0 to (- j i) always
                    (let* ((orig #*1111111111)
                           (x (copy-seq orig))
                           (y (nsubstitute 0 1 x :start i :end j :count c :from-end t)))
                      (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)))))))
687 688
  t)

689
(deftest nsubstitute-bit-vector.22
690
  (let* ((orig #*0101010101)
sds's avatar
sds committed
691 692 693
         (x (copy-seq orig))
         (c 0)
         (result (nsubstitute 1 0 x :test #'(lambda (a b) (incf c) (and (<= 2 c 5) (= a b))))))
694 695 696
    result)
  #*0111110101)

697
(deftest nsubstitute-bit-vector.23
698
  (let* ((orig #*0101010101)
sds's avatar
sds committed
699 700 701 702
         (x (copy-seq orig))
         (c 0)
         (result (nsubstitute 1 0 x :test-not #'(lambda (a b) (incf c)
                                                 (not (and (<= 2 c 5) (= a b)))))))
703 704 705
    result)
  #*0111110101)

706
(deftest nsubstitute-bit-vector.24
707
  (let* ((orig #*0101010101)
sds's avatar
sds committed
708 709 710 711
         (x (copy-seq orig))
         (c 0)
         (result (nsubstitute 1 0 x :test #'(lambda (a b) (incf c) (and (<= 2 c 5) (= a b)))
                             :from-end t)))
712 713 714
    result)
  #*0101011111)

715
(deftest nsubstitute-bit-vector.25
716
  (let* ((orig #*0101010101)
sds's avatar
sds committed
717 718 719 720 721
         (x (copy-seq orig))
         (c 0)
         (result (nsubstitute 1 0 x :test-not #'(lambda (a b) (incf c)
                                                 (not (and (<= 2 c 5) (= a b))))
                             :from-end t)))
722 723 724
    result)
  #*0101011111)

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
(defharmless nsubstitute.test-and-test-not.1
  (nsubstitute 'b 'a (list 'a 'b 'c 'd 'a 'b) :test #'eql :test-not #'eql))

(defharmless nsubstitute.test-and-test-not.2
  (nsubstitute 'b 'a (list 'a 'b 'c 'd 'a 'b) :test-not #'eql :test #'eql))

(defharmless nsubstitute.test-and-test-not.3
  (nsubstitute 'b 'a (vector 'a 'b 'c 'd 'a 'b) :test #'eql :test-not #'eql))

(defharmless nsubstitute.test-and-test-not.4
  (nsubstitute 'b 'a (vector 'a 'b 'c 'd 'a 'b) :test-not #'eql :test #'eql))

(defharmless nsubstitute.test-and-test-not.5
  (nsubstitute #\b #\a (copy-seq "abcdab") :test #'eql :test-not #'eql))

(defharmless nsubstitute.test-and-test-not.6
  (nsubstitute #\b #\a (copy-seq "abcdab") :test-not #'eql :test #'eql))

(defharmless nsubstitute.test-and-test-not.7
  (nsubstitute 1 0 (copy-seq #*001101001) :test #'eql :test-not #'eql))

(defharmless nsubstitute.test-and-test-not.8
  (nsubstitute 0 1 (copy-seq #*1100110101) :test-not #'eql :test #'eql))


750 751 752 753
;;;; additional tests

(deftest nsubstitute-list.24
  (let* ((orig '((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
sds's avatar
sds committed
754 755
         (x (copy-seq orig))
         (result (nsubstitute '(a 10) 'a x :key #'car)))
756 757 758 759 760
    result)
  ((a 10) (b 2) (a 10) (c 4) (d 5) (a 10) (e 7)))

(deftest nsubstitute-list.25
  (let* ((orig '((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
sds's avatar
sds committed
761 762
         (x (copy-seq orig))
         (result (nsubstitute '(a 10) 'a x :key #'car :start 1 :end 5)))
763 764 765 766 767
    result)
  ((a 1) (b 2) (a 10) (c 4) (d 5) (a 6) (e 7)))

(deftest nsubstitute-list.26
  (let* ((orig '((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
sds's avatar
sds committed
768 769
         (x (copy-seq orig))
         (result (nsubstitute '(a 10) 'a x :key #'car :test (complement #'eql))))
770 771 772 773 774
    result)
  ((a 1) (a 10) (a 3) (a 10) (a 10) (a 6) (a 10)))

(deftest nsubstitute-list.27
  (let* ((orig '((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
sds's avatar
sds committed
775 776
         (x (copy-seq orig))
         (result (nsubstitute '(a 10) 'a x :key #'car :test-not #'eql)))
777
    result)
sds's avatar
sds committed
778
  ((a 1) (a 10) (a 3) (a 10) (a 10) (a 6) (a 10)))
779 780 781

(deftest nsubstitute-vector.24
  (let* ((orig #((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
sds's avatar
sds committed
782 783
         (x (copy-seq orig))
         (result (nsubstitute '(a 10) 'a x :key #'car)))
784 785
    result)
  #((a 10) (b 2) (a 10) (c 4) (d 5) (a 10) (e 7)))
sds's avatar
sds committed
786

787 788
(deftest nsubstitute-vector.25
  (let* ((orig #((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
sds's avatar
sds committed
789 790
         (x (copy-seq orig))
         (result (nsubstitute '(a 10) 'a x :key #'car :start 1 :end 5)))
791 792 793 794 795
    result)
  #((a 1) (b 2) (a 10) (c 4) (d 5) (a 6) (e 7)))

(deftest nsubstitute-vector.26
  (let* ((orig #((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
sds's avatar
sds committed
796 797
         (x (copy-seq orig))
         (result (nsubstitute '(a 10) 'a x :key #'car :test (complement #'eql))))
798 799 800 801 802
    result)
  #((a 1) (a 10) (a 3) (a 10) (a 10) (a 6) (a 10)))

(deftest nsubstitute-vector.27
  (let* ((orig #((a 1) (b 2) (a 3) (c 4) (d 5) (a 6) (e 7)))
sds's avatar
sds committed
803 804
         (x (copy-seq orig))
         (result (nsubstitute '(a 10) 'a x :key #'car :test-not #'eql)))
805 806 807
    result)
  #((a 1) (a 10) (a 3) (a 10) (a 10) (a 6) (a 10)))

808 809
(deftest nsubstitute-vector.32
  (let* ((v1 (copy-seq #(a b c d a b c d a b c d a b c d)))
sds's avatar
sds committed
810 811
         (v2 (make-array '(8) :displaced-to v1
                         :displaced-index-offset 3)))
812 813 814 815 816 817 818 819
    (values
     (nsubstitute 'x 'c v2 :count 1)
     v1))
  #(d a b x d a b c)
  #(a b c d a b x d a b c d a b c d))

(deftest nsubstitute-vector.33
  (let* ((v1 (copy-seq #(a b c d a b c d a b c d a b c d)))
sds's avatar
sds committed
820 821
         (v2 (make-array '(8) :displaced-to v1
                         :displaced-index-offset 3)))
822 823 824 825 826 827 828
    (values
     (nsubstitute 'x '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 x d a b c d))


829 830
(deftest nsubstitute-string.24
  (let* ((orig "0102342015")
sds's avatar
sds committed
831 832
         (x (copy-seq orig))
         (result (nsubstitute #\a #\1 x :key #'nextdigit)))
833 834
    result)
  "a1a2342a15")
sds's avatar
sds committed
835

836 837
(deftest nsubstitute-string.25
  (let* ((orig "0102342015")
sds's avatar
sds committed
838 839
         (x (copy-seq orig))
         (result (nsubstitute #\a #\1 x :key #'nextdigit :start 1 :end 6)))
840 841 842 843 844
    result)
  "01a2342015")

(deftest nsubstitute-string.26
  (let* ((orig "0102342015")
sds's avatar
sds committed
845 846
         (x (copy-seq orig))
         (result (nsubstitute #\a #\1 x :key #'nextdigit :test (complement #'eql))))
847 848 849 850 851
    result)
  "0a0aaaa0aa")

(deftest nsubstitute-string.27
  (let* ((orig "0102342015")
sds's avatar
sds committed
852 853
         (x (copy-seq orig))
         (result (nsubstitute #\a #\1 x :key #'nextdigit :test-not #'eql)))
854 855
    result)
  "0a0aaaa0aa")
856

857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
(deftest nsubstitute-string.32
  (do-special-strings
   (s "xyzabcxyzabc" nil)
   (assert (string= (nsubstitute #\! #\a s) "xyz!bcxyz!bc"))
   (assert (string= s "xyz!bcxyz!bc")))
  nil)

(deftest nsubstitute-string.33
  (do-special-strings
   (s "xyzabcxyzabc" nil)
   (assert (string= (nsubstitute #\! #\a s :count 1) "xyz!bcxyzabc"))
   (assert (string= s "xyz!bcxyzabc")))
  nil)

(deftest nsubstitute-string.34
  (do-special-strings
   (s "xyzabcxyzabc" nil)
   (assert (string= (nsubstitute #\! #\a s :count 1 :from-end t) "xyzabcxyz!bc"))
   (assert (string= s "xyzabcxyz!bc")))
  nil)

;;; More bit vector tests

880 881
(deftest nsubstitute-bit-vector.30
  (let* ((x (make-array '(10) :initial-contents '(0 1 0 1 1 0 1 1 0 1)
sds's avatar
sds committed
882 883
                       :fill-pointer 5 :element-type 'bit))
         (result (nsubstitute 1 0 x)))
884 885 886 887 888
    result)
  #*11111)

(deftest nsubstitute-bit-vector.31
  (let* ((x (make-array '(10) :initial-contents '(0 1 0 1 1 0 1 1 0 1)
sds's avatar
sds committed
889 890
                       :fill-pointer 5 :element-type 'bit))
         (result (nsubstitute 1 0 x :from-end t)))
891 892 893 894 895
    result)
  #*11111)

(deftest nsubstitute-bit-vector.32
  (let* ((x (make-array '(10) :initial-contents '(0 1 0 1 1 0 1 1 0 1)
sds's avatar
sds committed
896 897
                       :fill-pointer 5 :element-type 'bit))
         (result (nsubstitute 1 0 x :count 1)))
898 899 900 901 902
    result)
  #*11011)

(deftest nsubstitute-bit-vector.33
  (let* ((x (make-array '(10) :initial-contents '(0 1 0 1 1 0 1 1 0 1)
sds's avatar
sds committed
903 904
                       :fill-pointer 5 :element-type 'bit))
         (result (nsubstitute 1 0 x :from-end t :count 1)))
905 906
    result)
  #*01111)
907

908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
(deftest nsubstitute.order.1
  (let ((i 0) a b c d e f g h)
    (values
     (nsubstitute
      (progn (setf a (incf i)) 'a)
      (progn (setf b (incf i)) nil)
      (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 nsubstitute.order.2
  (let ((i 0) a b c d e f g h)
    (values
     (nsubstitute
      (progn (setf a (incf i)) 'a)
      (progn (setf b (incf i)) nil)
      (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)

942 943 944 945 946 947 948 949 950 951 952 953
;;; Keyword tests

(deftest nsubstitute.allow-other-keys.1
  (nsubstitute 'a 0 (list 1 2 0 3 1 0 3) :allow-other-keys t :bad t)
  (1 2 a 3 1 a 3))

(deftest nsubstitute.allow-other-keys.2
  (nsubstitute 'a 0 (list 1 2 0 3 1 0 3) :bad t :allow-other-keys t)
  (1 2 a 3 1 a 3))

(deftest nsubstitute.allow-other-keys.3
  (nsubstitute 'a 0 (list 1 2 0 3 1 0 3) :allow-other-keys t
sds's avatar
sds committed
954
                  :allow-other-keys nil :bad t)
955 956 957 958
  (1 2 a 3 1 a 3))

(deftest nsubstitute.allow-other-keys.4
  (nsubstitute 'a 0 (list 1 2 0 3 1 0 3) :bad t
sds's avatar
sds committed
959
                  :allow-other-keys t :allow-other-keys nil)
960 961 962 963
  (1 2 a 3 1 a 3))

(deftest nsubstitute.allow-other-keys.5
  (nsubstitute 'a 0 (list 1 2 0 3 1 0 3)
sds's avatar
sds committed
964
                  :allow-other-keys t :key #'1-)
965 966 967 968 969 970 971 972
  (a 2 0 3 a 0 3))

(deftest nsubstitute.keywords.6
  (nsubstitute 'a 0 (list 1 2 0 3 1 0 3) :key #'1- :key #'identity)
  (a 2 0 3 a 0 3))

(deftest nsubstitute.allow-other-keys.7
  (nsubstitute 'a 0 (list 1 2 0 3 1 0 3) :allow-other-keys t
sds's avatar
sds committed
973
                  :bad t :allow-other-keys nil)
974 975 976 977 978 979 980
  (1 2 a 3 1 a 3))

(deftest nsubstitute.allow-other-keys.8
  (nsubstitute 'a 0 (list 1 2 0 3 1 0 3) :allow-other-keys nil)
  (1 2 a 3 1 a 3))


981 982 983
;;; Error cases

(deftest nsubstitute.error.1
984 985
  (signals-error (nsubstitute) program-error)
  t)
986 987

(deftest nsubstitute.error.2
988 989
  (signals-error (nsubstitute 'a) program-error)
  t)
990 991

(deftest nsubstitute.error.3
992 993
  (signals-error (nsubstitute 'a 'b) program-error)
  t)
994 995

(deftest nsubstitute.error.4
996 997
  (signals-error (nsubstitute 'a 'b nil 'bad t) program-error)
  t)
998 999

(deftest nsubstitute.error.5
1000 1001
  (signals-error (nsubstitute 'a 'b nil 'bad t :allow-other-keys nil) program-error)
  t)
1002 1003

(deftest nsubstitute.error.6
1004 1005
  (signals-error (nsubstitute 'a 'b nil :key) program-error)
  t)
1006 1007

(deftest nsubstitute.error.7
1008 1009
  (signals-error (nsubstitute 'a 'b nil 1 2) program-error)
  t)
1010 1011

(deftest nsubstitute.error.8
1012 1013
  (signals-error (nsubstitute 'a 'b (list 'a 'b 'c) :test #'identity) program-error)
  t)
1014 1015

(deftest nsubstitute.error.9
1016 1017
  (signals-error (nsubstitute 'a 'b (list 'a 'b 'c) :test-not #'identity) program-error)
  t)
1018 1019

(deftest nsubstitute.error.10
1020 1021
  (signals-error (nsubstitute 'a 'b (list 'a 'b 'c) :key #'cons) program-error)
  t)
1022 1023

(deftest nsubstitute.error.11
1024 1025
  (signals-error (nsubstitute 'a 'b (list 'a 'b 'c) :key #'car) type-error)
  t)
1026

pfdietz's avatar
pfdietz committed
1027 1028 1029
(deftest nsubstitute.error.12
  (check-type-error #'(lambda (x) (nsubstitute 1 0 x)) #'sequencep)
  nil)