position-if.lsp 14 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
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Fri Aug 23 22:08:57 2002
;;;; Contains: Tests for POSITION-IF

(in-package :cl-test)

(deftest position-if-list.1
  (position-if #'evenp '(1 3 1 4 3 2 1 8 9))
  3)

(deftest position-if-list.2
  (position-if 'evenp '(1 3 1 4 3 2 1 8 9))
  3)

(deftest position-if-list.3
  (position-if #'evenp '(1 3 1 4 3 2 1 8 9) :start 4)
  5)

(deftest position-if-list.4
  (position-if #'evenp '(1 3 1 4 3 2 1 8 9) :from-end t)
  7)

(deftest position-if-list.5
  (position-if #'evenp '(1 3 1 4 3 2 1 8 9) :from-end nil)
  3)

(deftest position-if-list.6
  (position-if #'evenp '(1 3 1 4 3 2 1 8 9) :start 4
sds's avatar
sds committed
30
               :from-end t)
31 32 33 34 35 36 37 38 39 40 41 42
  7)

(deftest position-if-list.7
  (position-if #'evenp '(1 3 1 4 3 2 1 8 9) :end nil)
  3)

(deftest position-if-list.8
  (position-if #'evenp '(1 3 1 4 3 2 1 8 9) :end 3)
  nil)

(deftest position-if-list.9
  (loop for i from 0 to 8
sds's avatar
sds committed
43 44 45 46
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'evenp '(1 3 1 4 3 2 1 8 9) :start i :end j)))
47 48 49 50 51 52 53 54 55 56 57 58
  ((nil nil nil 3 3 3 3 3 3)
   (nil nil 3 3 3 3 3 3)
   (nil 3 3 3 3 3 3)
   (3 3 3 3 3 3)
   (nil 5 5 5 5)
   (5 5 5 5)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-list.10
  (loop for i from 0 to 8
sds's avatar
sds committed
59 60 61 62 63
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'evenp '(1 3 1 4 3 2 1 8 9) :start i :end j
                           :from-end t)))
64 65 66 67 68 69 70 71 72 73 74 75
  ((nil nil nil 3 3 5 5 7 7)
   (nil nil 3 3 5 5 7 7)
   (nil 3 3 5 5 7 7)
   (3 3 5 5 7 7)
   (nil 5 5 7 7)
   (5 5 7 7)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-list.11
  (loop for i from 0 to 8
sds's avatar
sds committed
76 77 78 79 80
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'oddp '(1 3 1 4 3 2 1 8 9) :start i :end j
                           :key '1+)))
81 82 83 84 85 86 87 88 89 90 91 92
  ((nil nil nil 3 3 3 3 3 3)
   (nil nil 3 3 3 3 3 3)
   (nil 3 3 3 3 3 3)
   (3 3 3 3 3 3)
   (nil 5 5 5 5)
   (5 5 5 5)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-list.12
  (loop for i from 0 to 8
sds's avatar
sds committed
93 94 95 96 97
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'oddp '(1 3 1 4 3 2 1 8 9) :start i :end j
                           :key #'1+ :from-end t)))
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  ((nil nil nil 3 3 5 5 7 7)
   (nil nil 3 3 5 5 7 7)
   (nil 3 3 5 5 7 7)
   (3 3 5 5 7 7)
   (nil 5 5 7 7)
   (5 5 7 7)
   (nil 7 7)
   (7 7)
   (nil)))

;;; Vector tests

(deftest position-if-vector.1
  (position-if #'evenp #(1 3 1 4 3 2 1 8 9))
  3)

(deftest position-if-vector.2
  (position-if 'evenp #(1 3 1 4 3 2 1 8 9))
  3)

(deftest position-if-vector.3
  (position-if #'evenp #(1 3 1 4 3 2 1 8 9) :start 4)
  5)

(deftest position-if-vector.4
  (position-if #'evenp #(1 3 1 4 3 2 1 8 9) :from-end t)
  7)

(deftest position-if-vector.5
  (position-if #'evenp #(1 3 1 4 3 2 1 8 9) :from-end nil)
  3)

(deftest position-if-vector.6
  (position-if #'evenp #(1 3 1 4 3 2 1 8 9) :start 4
sds's avatar
sds committed
132
               :from-end t)
133 134 135 136 137 138 139 140 141 142 143 144
  7)

(deftest position-if-vector.7
  (position-if #'evenp #(1 3 1 4 3 2 1 8 9) :end nil)
  3)

(deftest position-if-vector.8
  (position-if #'evenp #(1 3 1 4 3 2 1 8 9) :end 3)
  nil)

(deftest position-if-vector.9
  (loop for i from 0 to 8
sds's avatar
sds committed
145 146 147 148
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'evenp #(1 3 1 4 3 2 1 8 9) :start i :end j)))
149 150 151 152 153 154 155 156 157 158 159 160
  ((nil nil nil 3 3 3 3 3 3)
   (nil nil 3 3 3 3 3 3)
   (nil 3 3 3 3 3 3)
   (3 3 3 3 3 3)
   (nil 5 5 5 5)
   (5 5 5 5)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-vector.10
  (loop for i from 0 to 8
sds's avatar
sds committed
161 162 163 164 165
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'evenp #(1 3 1 4 3 2 1 8 9) :start i :end j
                           :from-end t)))
166 167 168 169 170 171 172 173 174 175 176 177
  ((nil nil nil 3 3 5 5 7 7)
   (nil nil 3 3 5 5 7 7)
   (nil 3 3 5 5 7 7)
   (3 3 5 5 7 7)
   (nil 5 5 7 7)
   (5 5 7 7)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-vector.11
  (loop for i from 0 to 8
sds's avatar
sds committed
178 179 180 181 182
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'oddp #(1 3 1 4 3 2 1 8 9) :start i :end j
                           :key '1+)))
183 184 185 186 187 188 189 190 191 192 193 194
  ((nil nil nil 3 3 3 3 3 3)
   (nil nil 3 3 3 3 3 3)
   (nil 3 3 3 3 3 3)
   (3 3 3 3 3 3)
   (nil 5 5 5 5)
   (5 5 5 5)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-vector.12
  (loop for i from 0 to 8
sds's avatar
sds committed
195 196 197 198 199
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'oddp #(1 3 1 4 3 2 1 8 9) :start i :end j
                           :key #'1+ :from-end t)))
200 201 202 203 204 205 206 207 208 209
  ((nil nil nil 3 3 5 5 7 7)
   (nil nil 3 3 5 5 7 7)
   (nil 3 3 5 5 7 7)
   (3 3 5 5 7 7)
   (nil 5 5 7 7)
   (5 5 7 7)
   (nil 7 7)
   (7 7)
   (nil)))

210
(deftest position-if-vector.13
211
  (let ((a (make-array '(10) :initial-contents '(1 3 1 4 3 1 2 1 8 9)
sds's avatar
sds committed
212
                       :fill-pointer 5)))
213 214
    (flet ((%f (x) (eql x 1)))
      (values (position-if #'%f a)
sds's avatar
sds committed
215
              (position-if #'%f a :from-end t))))
216 217 218 219
  0 2)

(deftest position-if-vector.14
  (let* ((v1 #(x x x a b 1 d a b 2 d y y y y y))
sds's avatar
sds committed
220 221
         (v2 (make-array '(8) :displaced-to v1
                        :displaced-index-offset 3)))
222
    (values (position-if #'integerp v2)
sds's avatar
sds committed
223
            (position-if #'integerp v2 :from-end t)))
224
  2 6)
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
;;; Bit vector tests

(deftest position-if-bit-vector.1
  (position-if #'evenp #*111010101)
  3)

(deftest position-if-bit-vector.2
  (position-if 'evenp #*111010101)
  3)

(deftest position-if-bit-vector.3
  (position-if #'evenp #*111010101 :start 4)
  5)

(deftest position-if-bit-vector.4
  (position-if #'evenp #*111010101 :from-end t)
  7)

(deftest position-if-bit-vector.5
  (position-if #'evenp #*111010101 :from-end nil)
  3)

(deftest position-if-bit-vector.6
  (position-if #'evenp #*111010101 :start 4
sds's avatar
sds committed
250
               :from-end t)
251 252 253 254 255 256 257 258 259 260 261 262
  7)

(deftest position-if-bit-vector.7
  (position-if #'evenp #*111010101 :end nil)
  3)

(deftest position-if-bit-vector.8
  (position-if #'evenp #*111010101 :end 3)
  nil)

(deftest position-if-bit-vector.9
  (loop for i from 0 to 8
sds's avatar
sds committed
263 264 265 266
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'evenp #*111010101 :start i :end j)))
267 268 269 270 271 272 273 274 275 276 277 278
  ((nil nil nil 3 3 3 3 3 3)
   (nil nil 3 3 3 3 3 3)
   (nil 3 3 3 3 3 3)
   (3 3 3 3 3 3)
   (nil 5 5 5 5)
   (5 5 5 5)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-bit-vector.10
  (loop for i from 0 to 8
sds's avatar
sds committed
279 280 281 282 283
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'evenp #*111010101 :start i :end j
                           :from-end t)))
284 285 286 287 288 289 290 291 292 293 294 295
  ((nil nil nil 3 3 5 5 7 7)
   (nil nil 3 3 5 5 7 7)
   (nil 3 3 5 5 7 7)
   (3 3 5 5 7 7)
   (nil 5 5 7 7)
   (5 5 7 7)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-bit-vector.11
  (loop for i from 0 to 8
sds's avatar
sds committed
296 297 298 299 300
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'oddp #*111010101 :start i :end j
                           :key #'1+)))
301 302 303 304 305 306 307 308 309 310 311 312
  ((nil nil nil 3 3 3 3 3 3)
   (nil nil 3 3 3 3 3 3)
   (nil 3 3 3 3 3 3)
   (3 3 3 3 3 3)
   (nil 5 5 5 5)
   (5 5 5 5)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-bit-vector.12
  (loop for i from 0 to 8
sds's avatar
sds committed
313 314 315 316 317
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'oddp #*111010101 :start i :end j
                           :key '1+ :from-end t)))
318 319 320 321 322 323 324 325 326 327
  ((nil nil nil 3 3 5 5 7 7)
   (nil nil 3 3 5 5 7 7)
   (nil 3 3 5 5 7 7)
   (3 3 5 5 7 7)
   (nil 5 5 7 7)
   (5 5 7 7)
   (nil 7 7)
   (7 7)
   (nil)))

328 329
(deftest position-if-bit-vector.13
  (let ((a (make-array '(10) :initial-contents '(1 1 1 1 1 0 0 0 0 0)
sds's avatar
sds committed
330 331
                       :fill-pointer 5
                       :element-type 'bit)))
332
    (values (position-if #'evenp a)
sds's avatar
sds committed
333 334 335
            (position-if #'evenp a :from-end 'foo)
            (position-if #'oddp a)
            (position-if #'oddp a :from-end 'foo)))
336 337
  nil nil 0 4)

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
;;; string tests

(deftest position-if-string.1
  (position-if #'evendigitp "131432189")
  3)

(deftest position-if-string.2
  (position-if 'evendigitp "131432189")
  3)

(deftest position-if-string.3
  (position-if #'evendigitp "131432189" :start 4)
  5)

(deftest position-if-string.4
  (position-if #'evendigitp "131432189" :from-end t)
  7)

(deftest position-if-string.5
  (position-if #'evendigitp "131432189" :from-end nil)
  3)

(deftest position-if-string.6
  (position-if #'evendigitp "131432189" :start 4
sds's avatar
sds committed
362
               :from-end t)
363 364 365 366 367 368 369 370 371 372 373 374
  7)

(deftest position-if-string.7
  (position-if #'evendigitp "131432189" :end nil)
  3)

(deftest position-if-string.8
  (position-if #'evendigitp "131432189" :end 3)
  nil)

(deftest position-if-string.9
  (loop for i from 0 to 8
sds's avatar
sds committed
375 376 377 378
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'evendigitp "131432189" :start i :end j)))
379 380 381 382 383 384 385 386 387 388 389 390
  ((nil nil nil 3 3 3 3 3 3)
   (nil nil 3 3 3 3 3 3)
   (nil 3 3 3 3 3 3)
   (3 3 3 3 3 3)
   (nil 5 5 5 5)
   (5 5 5 5)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-string.10
  (loop for i from 0 to 8
sds's avatar
sds committed
391 392 393 394 395
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'evendigitp "131432189" :start i :end j
                           :from-end t)))
396 397 398 399 400 401 402 403 404 405 406 407
  ((nil nil nil 3 3 5 5 7 7)
   (nil nil 3 3 5 5 7 7)
   (nil 3 3 5 5 7 7)
   (3 3 5 5 7 7)
   (nil 5 5 7 7)
   (5 5 7 7)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-string.11
  (loop for i from 0 to 8
sds's avatar
sds committed
408 409 410 411 412
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'odddigitp "131432189" :start i :end j
                           :key #'nextdigit)))
413 414 415 416 417 418 419 420 421 422 423 424
  ((nil nil nil 3 3 3 3 3 3)
   (nil nil 3 3 3 3 3 3)
   (nil 3 3 3 3 3 3)
   (3 3 3 3 3 3)
   (nil 5 5 5 5)
   (5 5 5 5)
   (nil 7 7)
   (7 7)
   (nil)))

(deftest position-if-string.12
  (loop for i from 0 to 8
sds's avatar
sds committed
425 426 427 428 429
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if #'odddigitp "131432189" :start i :end j
                           :key 'nextdigit :from-end t)))
430 431 432 433 434 435 436 437 438 439
  ((nil nil nil 3 3 5 5 7 7)
   (nil nil 3 3 5 5 7 7)
   (nil 3 3 5 5 7 7)
   (3 3 5 5 7 7)
   (nil 5 5 7 7)
   (5 5 7 7)
   (nil 7 7)
   (7 7)
   (nil)))

440 441
(deftest position-if-string.13
  (flet ((%f (c) (eql c #\0))
sds's avatar
sds committed
442
         (%g (c) (eql c #\1)))
443
    (let ((a (make-array '(10) :initial-contents "1111100000"
sds's avatar
sds committed
444 445
                       :fill-pointer 5
                       :element-type 'character)))
446
    (values (position-if #'%f a)
sds's avatar
sds committed
447 448 449
            (position-if #'%f a :from-end 'foo)
            (position-if #'%g a)
            (position-if #'%g a :from-end 'foo))))
450 451
  nil nil 0 4)

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
(deftest position-if-string.14
  (do-special-strings
   (s "12345a6  78b90" nil)
   (let ((pos (position-if #'alpha-char-p s)))
     (assert (eql pos 5) () "First alpha char in ~A is at position ~A" s pos)))
  nil)

(deftest position-if-string.15
  (do-special-strings
   (s "12345a6  78b90" nil)
   (let ((pos (position-if #'alpha-char-p s :from-end t)))
     (assert (eql pos 11) () "Last alpha char in ~A is at position ~A" s pos)))
  nil)


467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
(deftest position-if.order.1
  (let ((i 0) a b c d e f)
    (values
     (position-if
      (progn (setf a (incf i)) #'zerop)
      (progn (setf b (incf i)) '(3 1 8 2 1 2 3 4))
      :from-end (setf c (incf i))
      :start (progn (setf d (incf i)) 1)
      :end (progn (setf e (incf i)) 6)
      :key (progn (setf f (incf i)) #'1-))
     i a b c d e f))
  4 6 1 2 3 4 5 6)

(deftest position-if.order.2
  (let ((i 0) a b c d e f)
    (values
     (position-if
      (progn (setf a (incf i)) #'zerop)
      (progn (setf b (incf i)) '(3 1 8 2 1 2 3 4))
      :key (progn (setf c (incf i)) #'1-)
      :end (progn (setf d (incf i)) 6)
      :start (progn (setf e (incf i)) 1)
      :from-end (setf f (incf i)))
     i a b c d e f))
  4 6 1 2 3 4 5 6)

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
;;; Keyword tests

(deftest position-if.allow-other-keys.1
  (position-if #'zerop '(1 2 0 3 2 1) :allow-other-keys t)
  2)

(deftest position-if.allow-other-keys.2
  (position-if #'zerop '(1 2 0 3 2 1) :allow-other-keys nil)
  2)

(deftest position-if.allow-other-keys.3
  (position-if #'zerop '(1 2 0 3 2 1) :allow-other-keys t :bad t)
  2)

(deftest position-if.allow-other-keys.4
  (position-if #'zerop '(1 2 0 3 2 1) :bad t :allow-other-keys t)
  2)

(deftest position-if.allow-other-keys.5
  (position-if #'zerop '(1 2 0 3 2 1) :bad t :allow-other-keys t :key #'1-)
  0)

(deftest position-if.keywords.6
  (position-if #'zerop '(1 2 0 3 2 1) :key #'1- :key #'identity)
  0)

(deftest position-if.allow-other-keys.7
  (position-if #'zerop '(1 2 0 3 2 1) :bad t :allow-other-keys t
sds's avatar
sds committed
521
               :allow-other-keys nil)
522 523 524 525
  2)

(deftest position-if.allow-other-keys.8
  (position-if #'zerop '(1 2 0 3 2 1) :allow-other-keys t :bad t
sds's avatar
sds committed
526
               :allow-other-keys nil)
527 528 529 530
  2)

(deftest position-if.allow-other-keys.9
  (position-if #'zerop '(1 2 0 3 2 1) :allow-other-keys t
sds's avatar
sds committed
531
               :allow-other-keys nil :bad t)
532 533 534
  2)


535 536
;;; Error tests

537
(deftest position-if.error.1
pfdietz's avatar
pfdietz committed
538 539
  (check-type-error #'(lambda (x) (position-if #'identity x)) #'sequencep)
  nil)
540

541
(deftest position-if.error.4
542 543
  (signals-error (position-if 'null '(a b c . d)) type-error)
  t)
544 545

(deftest position-if.error.5
546 547
  (signals-error (position-if) program-error)
  t)
548 549

(deftest position-if.error.6
550 551
  (signals-error (position-if #'null) program-error)
  t)
552 553

(deftest position-if.error.7
554 555
  (signals-error (position-if #'null nil :key) program-error)
  t)
556 557

(deftest position-if.error.8
558 559
  (signals-error (position-if #'null nil 'bad t) program-error)
  t)
560 561

(deftest position-if.error.9
562 563
  (signals-error (position-if #'null nil 'bad t :allow-other-keys nil) program-error)
  t)
564 565

(deftest position-if.error.10
566 567
  (signals-error (position-if #'null nil 1 2) program-error)
  t)
568

569
(deftest position-if.error.11
570 571
  (signals-error (locally (position-if #'identity 'b) t) type-error)
  t)
572 573

(deftest position-if.error.12
574 575
  (signals-error (position-if #'cons '(a b c d)) program-error)
  t)
576 577

(deftest position-if.error.13
578 579
  (signals-error (position-if #'car '(a b c d)) type-error)
  t)
580 581

(deftest position-if.error.14
582 583
  (signals-error (position-if #'identity '(a b c d) :key #'cdr) type-error)
  t)
584 585

(deftest position-if.error.15
586 587
  (signals-error (position-if #'identity '(a b c d) :key #'cons) program-error)
  t)