position-if-not.lsp 14.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
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Aug 24 07:10:05 2002
;;;; Contains: Tests for POSITION-IF-NOT-NOT

(in-package :cl-test)

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

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

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

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

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

(deftest position-if-not-list.6
  (position-if-not #'oddp '(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-not-list.7
  (position-if-not #'oddp '(1 3 1 4 3 2 1 8 9) :end nil)
  3)

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

(deftest position-if-not-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-not #'oddp '(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-not-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-not #'oddp '(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-not-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-not #'evenp '(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-not-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-not #'evenp '(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-not-vector.1
  (position-if-not #'oddp #(1 3 1 4 3 2 1 8 9))
  3)

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

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

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

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

(deftest position-if-not-vector.6
  (position-if-not #'oddp #(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-not-vector.7
  (position-if-not #'oddp #(1 3 1 4 3 2 1 8 9) :end nil)
  3)

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

(deftest position-if-not-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-not #'oddp #(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-not-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-not #'oddp #(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-not-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-not #'evenp #(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-not-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-not #'evenp #(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 211
(deftest position-if-not-vector.13
  (let ((a (make-array '(10) :initial-contents '(1 2 3 4 5 a b c d e)
sds's avatar
sds committed
212
                       :fill-pointer 5)))
213 214 215 216 217 218 219
    (values
     (position-if-not #'numberp a)
     (position-if-not #'symbolp a)
     (position-if-not #'numberp a :from-end t)
     (position-if-not #'symbolp a :from-end t)))
  nil 0 nil 4)

220 221
(deftest position-if-not-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
222 223
         (v2 (make-array '(8) :displaced-to v1
                        :displaced-index-offset 3)))
224
    (values (position-if-not #'symbolp v2)
sds's avatar
sds committed
225
            (position-if-not #'symbolp v2 :from-end t)))
226 227
  2 6)

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

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

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

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

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

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

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

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

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

(deftest position-if-not-bit-vector.9
  (loop for i from 0 to 8
sds's avatar
sds committed
265 266 267 268
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if-not #'oddp #*111010101 :start i :end j)))
269 270 271 272 273 274 275 276 277 278 279 280
  ((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-not-bit-vector.10
  (loop for i from 0 to 8
sds's avatar
sds committed
281 282 283 284 285
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if-not #'oddp #*111010101 :start i :end j
                           :from-end t)))
286 287 288 289 290 291 292 293 294 295 296 297
  ((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-not-bit-vector.11
  (loop for i from 0 to 8
sds's avatar
sds committed
298 299 300 301 302
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if-not #'evenp #*111010101 :start i :end j
                           :key #'1+)))
303 304 305 306 307 308 309 310 311 312 313 314
  ((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-not-bit-vector.12
  (loop for i from 0 to 8
sds's avatar
sds committed
315 316 317 318 319
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if-not #'evenp #*111010101 :start i :end j
                           :key '1+ :from-end t)))
320 321 322 323 324 325 326 327 328 329
  ((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)))

330 331
(deftest position-if-not-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
332 333
                       :fill-pointer 5
                       :element-type 'bit)))
334 335 336 337 338
    (values
     (position-if-not #'zerop a)
     (position-if-not (complement #'zerop) a)
     (position-if-not #'zerop a :from-end t)
     (position-if-not (complement #'zerop) a :from-end t)))
sds's avatar
sds committed
339
  0 nil 4 nil)
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
;;; string tests

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

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

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

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

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

(deftest position-if-not-string.6
  (position-if-not #'odddigitp "131432189" :start 4
sds's avatar
sds committed
365
               :from-end t)
366 367 368 369 370 371 372 373 374 375 376 377
  7)

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

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

(deftest position-if-not-string.9
  (loop for i from 0 to 8
sds's avatar
sds committed
378 379 380 381
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if-not #'odddigitp "131432189" :start i :end j)))
382 383 384 385 386 387 388 389 390 391 392 393
  ((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-not-string.10
  (loop for i from 0 to 8
sds's avatar
sds committed
394 395 396 397 398
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if-not #'odddigitp "131432189" :start i :end j
                           :from-end t)))
399 400 401 402 403 404 405 406 407 408 409 410
  ((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-not-string.11
  (loop for i from 0 to 8
sds's avatar
sds committed
411 412 413 414 415
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if-not #'evendigitp "131432183" :start i :end j
                           :key #'nextdigit)))
416 417 418 419 420 421 422 423 424 425 426 427
  ((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-not-string.12
  (loop for i from 0 to 8
sds's avatar
sds committed
428 429 430 431 432
        collect
        (loop for j from (1+ i) to 9
              collect
              (position-if-not #'evendigitp "131432183" :start i :end j
                           :key 'nextdigit :from-end t)))
433 434 435 436 437 438 439 440 441 442
  ((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)))

443
(deftest position-if-not-string.13
444
  (let ((a (make-array '(10) :initial-contents "55555aaaaa"
sds's avatar
sds committed
445 446
                       :fill-pointer 5
                       :element-type 'character)))
447
    (and (stringp a)
sds's avatar
sds committed
448 449 450 451 452
         (values
          (position-if-not #'digit-char-p a)
          (position-if-not (complement #'digit-char-p) a)
          (position-if-not #'digit-char-p a :from-end t)
          (position-if-not (complement #'digit-char-p) a :from-end t))))
453 454
  nil 0 nil 4)

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

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

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
(deftest position-if-not.order.1
  (let ((i 0) a b c d e f)
    (values
     (position-if-not
      (progn (setf a (incf i)) (complement #'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-not.order.2
  (let ((i 0) a b c d e f)
    (values
     (position-if-not
      (progn (setf a (incf i)) (complement #'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)

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 521 522
;;; Keyword tests

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

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

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

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

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

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

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

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

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


537 538
;;; Error tests

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

543
(deftest position-if-not.error.4
544 545
  (signals-error (position-if-not 'identity '(a b c . d)) type-error)
  t)
546

547
(deftest position-if-not.error.5
548 549
  (signals-error (position-if-not) program-error)
  t)
550 551

(deftest position-if-not.error.6
552 553
  (signals-error (position-if-not #'null) program-error)
  t)
554 555

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

(deftest position-if-not.error.8
560 561
  (signals-error (position-if-not #'null nil 'bad t) program-error)
  t)
562 563

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

(deftest position-if-not.error.10
568 569
  (signals-error (position-if-not #'null nil 1 2) program-error)
  t)
570

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

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

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

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

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