count-if-not.lsp 16.2 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
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Tue Aug 20 22:42:35 2002
;;;; Contains: Tests for COUNT-IF-NOT

(in-package :cl-test)

(deftest count-if-not-list.1
  (count-if-not #'identity '(a b nil c d nil e))
  2)

(deftest count-if-not-list.2
  (count-if-not #'not '(a b nil c d nil e))
  5)

(deftest count-if-not-list.3
  (count-if-not #'(lambda (x) (break)) nil)
  0)

(deftest count-if-not-list.4
  (count-if-not #'identity '(a b nil c d nil e) :key #'identity)
  2)

(deftest count-if-not-list.5
  (count-if-not 'identity '(a b nil c d nil e) :key #'identity)
  2)

(deftest count-if-not-list.6
  (count-if-not #'identity '(a b nil c d nil e) :key 'identity)
  2)

(deftest count-if-not-list.8
  (count-if-not #'identity '(a b nil c d nil e) :key 'not)
  5)

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

(deftest count-if-not-list.10
  (count-if-not #'oddp '(1 2 3 4 4 1 8 10 1) :key #'1+)
  4)

(deftest count-if-not-list.11
  (let ((c 0))
    (count-if-not #'oddp '(1 2 3 4 4 1 8 10 1)
sds's avatar
sds committed
47
              :key #'(lambda (x) (+ x (incf c)))))
48 49 50 51 52
  6)

(deftest count-if-not-list.12
  (let ((c 0))
    (count-if-not #'oddp '(0 1 2 3 4 4 1 7 10 1)
sds's avatar
sds committed
53 54
              :from-end t
              :key #'(lambda (x) (+ x (incf c)))))
55 56 57
  8)

(deftest count-if-not-list.13
58
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
59
            '(a b c d a e f a e f f a a) :start 2)
60 61 62
  4)

(deftest count-if-not-list.14
63
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
64
            '(a b c d a e f a e f f a a) :end 7)
65
  2)
sds's avatar
sds committed
66

67
(deftest count-if-not-list.15
68
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
69 70
            '(a b c d a e f a e f f a a) :end 7
            :start 2)
71
  1)
sds's avatar
sds committed
72

73
(deftest count-if-not-list.16
74
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
75 76
            '(a b c d a e f a e f f a a) :end 7
            :start 2 :from-end t)
77
  1)
sds's avatar
sds committed
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

;;; tests on vectors

(deftest count-if-not-vector.1
  (count-if-not #'identity #(a b nil c d nil e))
  2)

(deftest count-if-not-vector.2
  (count-if-not #'not #(a b nil c d nil e))
  5)

(deftest count-if-not-vector.3
  (count-if-not #'(lambda (x) (break)) #())
  0)

(deftest count-if-not-vector.4
  (count-if-not #'not #(a b nil c d nil e) :key #'identity)
  5)

(deftest count-if-not-vector.5
  (count-if-not 'not #(a b nil c d nil e) :key #'identity)
  5)

(deftest count-if-not-vector.6
  (count-if-not #'not #(a b nil c d nil e) :key 'identity)
  5)

(deftest count-if-not-vector.8
  (count-if-not #'not #(a b nil c d nil e) :key 'not)
  2)

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

(deftest count-if-not-vector.10
  (count-if-not #'oddp #(1 2 3 4 4 1 8 10 1) :key #'1+)
  4)

(deftest count-if-not-vector.11
  (let ((c 0))
    (count-if-not #'oddp #(1 2 3 4 4 1 8 10 1)
sds's avatar
sds committed
121
              :key #'(lambda (x) (+ x (incf c)))))
122 123 124 125 126
  6)

(deftest count-if-not-vector.12
  (let ((c 0))
    (count-if-not #'oddp #(0 1 2 3 4 4 1 7 10 1)
sds's avatar
sds committed
127 128
              :from-end t
              :key #'(lambda (x) (+ x (incf c)))))
129 130 131
  8)

(deftest count-if-not-vector.13
132
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
133
            #(a b c d a e f a e f f a a) :start 2)
134 135 136
  4)

(deftest count-if-not-vector.14
137
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
138
            #(a b c d a e f a e f f a a) :end 7)
139
  2)
sds's avatar
sds committed
140

141
(deftest count-if-not-vector.15
142
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
143 144
            #(a b c d a e f a e f f a a) :end 7
            :start 2)
145
  1)
sds's avatar
sds committed
146

147
(deftest count-if-not-vector.16
148
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
149 150
            #(a b c d a e f a e f f a a) :end 7
            :start 2 :from-end t)
151 152 153 154 155 156
  1)

;;; Non-simple vectors

(deftest count-if-not-nonsimple-vector.1
  (count-if-not #'identity (make-array 7 :initial-contents '(a b nil c d nil e)
sds's avatar
sds committed
157 158
                                       :fill-pointer t
                                       :adjustable t))
159 160 161 162
  2)

(deftest count-if-not-nonsimple-vector.2
  (count-if-not #'not (make-array 7 :initial-contents '(a b nil c d nil e)
sds's avatar
sds committed
163 164
                                  :fill-pointer t
                                  :adjustable t))
165 166 167 168
  5)

(deftest count-if-not-nonsimple-vector.3
  (count-if-not #'(lambda (x) (break)) (make-array 0
sds's avatar
sds committed
169 170
                                                   :fill-pointer t
                                                   :adjustable t))
171 172 173 174
  0)

(deftest count-if-not-nonsimple-vector.4
  (count-if-not #'not
sds's avatar
sds committed
175 176 177 178
            (make-array 7 :initial-contents '(a b nil c d nil e)
                        :fill-pointer t
                        :adjustable t)
            :key #'identity)
179 180 181 182
  5)

(deftest count-if-not-nonsimple-vector.5
  (count-if-not 'not
sds's avatar
sds committed
183 184 185 186
            (make-array 7 :initial-contents '(a b nil c d nil e)
                        :fill-pointer t
                        :adjustable t)
            :key #'identity)
187 188 189 190
  5)

(deftest count-if-not-nonsimple-vector.6
  (count-if-not #'not
sds's avatar
sds committed
191 192 193 194
            (make-array 7 :initial-contents '(a b nil c d nil e)
                        :fill-pointer t
                        :adjustable t)
            :key 'identity)
195 196 197 198
  5)

(deftest count-if-not-nonsimple-vector.8
  (count-if-not #'not
sds's avatar
sds committed
199 200 201 202
            (make-array 7 :initial-contents '(a b nil c d nil e)
                        :fill-pointer t
                        :adjustable t)
            :key 'not)
203 204 205 206
  2)

(deftest count-if-not-nonsimple-vector.9
  (count-if-not #'oddp (make-array 9 :initial-contents '(1 2 3 4 4 1 8 10 1)
sds's avatar
sds committed
207
                                :fill-pointer t :adjustable t))
208 209 210 211
  5)

(deftest count-if-not-nonsimple-vector.10
  (count-if-not #'oddp
sds's avatar
sds committed
212 213 214
            (make-array 9 :initial-contents '(1 2 3 4 4 1 8 10 1)
                        :fill-pointer t :adjustable t)
            :key #'1+)
215 216 217 218 219
  4)

(deftest count-if-not-nonsimple-vector.11
  (let ((c 0))
    (count-if-not #'oddp
sds's avatar
sds committed
220 221 222
              (make-array 9 :initial-contents '(1 2 3 4 4 1 8 10 1)
                          :fill-pointer t :adjustable t)
              :key #'(lambda (x) (+ x (incf c)))))
223 224 225 226 227
  6)

(deftest count-if-not-nonsimple-vector.12
  (let ((c 0))
    (count-if-not #'oddp
sds's avatar
sds committed
228 229 230 231
              (make-array 10 :initial-contents '(0 1 2 3 4 4 1 7 10 1)
                          :fill-pointer t :adjustable t)
              :from-end t
              :key #'(lambda (x) (+ x (incf c)))))
232 233 234
  8)

(deftest count-if-not-nonsimple-vector.13
235
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
236 237 238
            (make-array 13 :initial-contents '(a b c d a e f a e f f a a)
                        :fill-pointer t :adjustable t)
            :start 2)
239 240 241
  4)

(deftest count-if-not-nonsimple-vector.14
242
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
243 244 245
            (make-array 13 :initial-contents '(a b c d a e f a e f f a a)
                        :fill-pointer t :adjustable t)
            :end 7)
246
  2)
sds's avatar
sds committed
247

248
(deftest count-if-not-nonsimple-vector.15
249
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
250 251 252
            (make-array 13 :initial-contents '(a b c d a e f a e f f a a)
                        :fill-pointer t :adjustable t)
            :end 7 :start 2)
253
  1)
sds's avatar
sds committed
254

255
(deftest count-if-not-nonsimple-vector.16
256
  (count-if-not #'(lambda (x) (not (eqt x 'a)))
sds's avatar
sds committed
257 258 259
            (make-array 13 :initial-contents '(a b c d a e f a e f f a a)
                        :fill-pointer t :adjustable t)
            :end 7 :start 2 :from-end t)
260 261
  1)

262 263
(deftest count-if-not-nonsimple-vector.17
  (flet ((%a (c) (not (eqt c 'a)))
sds's avatar
sds committed
264
         (%f (c) (not (eqt c 'f))))
265
    (let ((a (make-array 13 :initial-contents '(a b c d a e f a e f f a a)
sds's avatar
sds committed
266
                         :fill-pointer 9)))
267
      (values (count-if-not #'%a a)
sds's avatar
sds committed
268 269 270 271
              (count-if-not #'%a a :from-end t)
              (count-if-not #'%f a)
              (count-if-not #'%f a :from-end t)
              )))
272 273
  3 3 1 1)

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
;;; Other special vectors

`(deftest count-if-not.special-vector.1
  (do-special-integer-vectors
   (v #(1 0 1 1 1 0 1 1 1 0 1) nil)
   (assert (eql (count-if-not #'plusp v) 3))
   (assert (eql (count-if-not #'zerop v) 8))
   (assert (eql (count-if-not #'plusp v :start 2) 2))
   (assert (eql (count-if-not #'zerop v :end 9) 7)))
  nil)

(deftest count-if-not.special-vector.2
  (do-special-integer-vectors
   (v #(1 3 2 4 7 5 6 1 0 2 4) nil)
   (assert (eql (count-if-not #'evenp v) 5))
   (assert (eql (count-if-not #'oddp v) 6))
   (assert (eql (count-if-not #'plusp v :start 2) 1))
   (assert (eql (count-if-not #'zerop v :end 8) 8)))
  nil)

(deftest count-if-not.special-vector.3
  (loop for etype in '(short-float single-float double-float long-float)
sds's avatar
sds committed
296 297 298 299 300 301
        for vals = (loop for e in '(0 1 2 1 3 0 4 5 6 0)
                         collect (coerce e etype))
        for vec = (make-array (length vals) :element-type etype :initial-contents vals)
        for result = (count-if-not #'zerop vec)
        unless (= result 7)
        collect (list etype vals vec result))
302 303 304 305
  nil)

(deftest count-if-not.special-vector.4
  (loop for cetype in '(short-float single-float double-float long-float integer rational)
sds's avatar
sds committed
306 307 308 309 310 311 312
        for etype = `(complex ,cetype)
        for vals = (loop for e in '(6 1 2 1 3 -4 4 5 6 100)
                         collect (complex 0 (coerce e cetype)))
        for vec = (make-array (length vals) :element-type etype :initial-contents vals)
        for result = (count-if-not #'(lambda (x) (< (abs x) 5/2)) vec)
        unless (= result 7)
        collect (list etype vals vec result))
313 314 315
  nil)


316 317 318
;;; tests on bit-vectors

(deftest count-if-not-bit-vector.1
319 320 321
  (count-if-not #'oddp #*001011101101)
  5)

322
(deftest count-if-not-bit-vector.2
323 324 325
  (count-if-not #'identity #*001011101101)
  0)

326
(deftest count-if-not-bit-vector.3
327 328 329
  (count-if-not #'(lambda (x) (break)) #*)
  0)

330
(deftest count-if-not-bit-vector.4
331 332 333
  (count-if-not #'identity #*001011101101 :key #'zerop)
  7)

334
(deftest count-if-not-bit-vector.5
335 336 337
  (count-if-not 'not #*001011101101 :key #'zerop)
  5)

338
(deftest count-if-not-bit-vector.6
339 340 341
  (count-if-not #'not #*001011101101 :key 'zerop)
  5)

342
(deftest count-if-not-bit-vector.8
343 344 345
  (count-if-not #'identity #*001011101101 :key 'oddp)
  5)

346
(deftest count-if-not-bit-vector.10
347 348 349
  (count-if-not #'oddp #*001011101101 :key #'1+)
  7)

350
(deftest count-if-not-bit-vector.11
351 352
  (let ((c 0))
    (count-if-not #'oddp #*001011101101
sds's avatar
sds committed
353
                  :key #'(lambda (x) (+ x (incf c)))))
354 355
  7)

356
(deftest count-if-not-bit-vector.12
357 358
  (let ((c 0))
    (count-if-not #'oddp #*001011101101
sds's avatar
sds committed
359 360
              :from-end t
              :key #'(lambda (x) (+ x (incf c)))))
361 362
  5)

363
(deftest count-if-not-bit-vector.13
364 365 366
  (count-if-not #'zerop #*0111011011100 :start 2)
  7)

367
(deftest count-if-not-bit-vector.14
368 369
  (count-if-not #'zerop #*0111011011100 :end 7)
  5)
sds's avatar
sds committed
370

371
(deftest count-if-not-bit-vector.15
372 373
  (count-if-not #'zerop #*0111011011100 :end 7 :start 2)
  4)
sds's avatar
sds committed
374

375
(deftest count-if-not-bit-vector.16
376 377 378
  (count-if-not #'zerop #*0111011011100 :end 7 :start 2 :from-end t)
  4)

379 380
(deftest count-if-not-bit-vector.17
  (let ((a (make-array '(10) :initial-contents '(0 0 0 1 1 1 0 1 0 0)
sds's avatar
sds committed
381 382
                       :fill-pointer 5
                       :element-type 'bit)))
383
    (and (bit-vector-p a)
sds's avatar
sds committed
384 385 386 387
         (values (count-if-not #'zerop a)
                 (count-if-not #'oddp a)
                 (count-if-not #'zerop a :from-end t)
                 (count-if-not #'oddp a :from-end t))))
388 389
  2 3 2 3)

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
;;; tests on strings

(deftest count-if-not-string.1
  (count-if-not #'(lambda (x) (eql x #\0)) "001011101101")
  7)

(deftest count-if-not-string.2
  (count-if-not #'identity "001011101101")
  0)

(deftest count-if-not-string.3
  (count-if-not #'(lambda (x) (break)) "")
  0)

(deftest count-if-not-string.4
  (count-if-not #'identity "001011101101" :key #'(lambda (x) (eql x #\0)))
  7)

(deftest count-if-not-string.5
  (count-if-not 'identity "001011101101" :key #'(lambda (x) (eql x #\0)))
  7)

(deftest count-if-not-string.6
  (count-if-not #'(lambda (x) (eql x #\0)) "001011101101" :key 'identity)
  7)

(deftest count-if-not-string.8
  (count-if-not #'identity "001011101101" :key #'(lambda (x) (eql x #\1)))
  5)

(deftest count-if-not-string.11
  (let ((c 0))
    (count-if-not #'oddp "001011101101"
sds's avatar
sds committed
423
                  :key #'(lambda (x) (+ (if (eql x #\0) 0 1) (incf c)))))
424 425 426 427 428
  7)

(deftest count-if-not-string.12
  (let ((c 0))
    (count-if-not #'oddp "001011101101"
sds's avatar
sds committed
429 430
                  :from-end t
                  :key #'(lambda (x) (+ (if (eql x #\0) 0 1) (incf c)))))
431 432 433 434 435 436 437 438 439
  5)

(deftest count-if-not-string.13
  (count-if-not #'(lambda (x) (eql x #\0)) "0111011011100" :start 2)
  7)

(deftest count-if-not-string.14
  (count-if-not #'(lambda (x) (eql x #\0)) "0111011011100" :end 7)
  5)
sds's avatar
sds committed
440

441 442 443
(deftest count-if-not-string.15
  (count-if-not #'(lambda (x) (eql x #\0)) "0111011011100" :end 7 :start 2)
  4)
sds's avatar
sds committed
444

445 446
(deftest count-if-not-string.16
  (count-if-not #'(lambda (x) (eql x #\0))
sds's avatar
sds committed
447
                "0111011011100" :end 7 :start 2 :from-end t)
448 449
  4)

450 451
(deftest count-if-not-string.17
  (flet ((%zerop (c) (eql c #\0))
sds's avatar
sds committed
452
         (%onep (c) (eql c #\1)))
453
    (let ((a (make-array '(10) :initial-contents "0001110100"
sds's avatar
sds committed
454 455
                         :fill-pointer 5
                         :element-type 'character)))
456
      (and (stringp a)
sds's avatar
sds committed
457 458 459 460
           (values (count-if-not #'%zerop a)
                   (count-if-not #'%onep a)
                   (count-if-not #'%zerop a :from-end t)
                   (count-if-not #'%onep a :from-end t)))))
461 462
  2 3 2 3)

463 464 465 466 467 468
(deftest count-if-not-string.18
  (do-special-strings
   (s "a1ha^%&%#( 873ff83nfa!" nil)
   (assert (= (count-if-not #'alpha-char-p s) 14)))
  nil)

469 470
;;; Argument order tests

471
(deftest count-if-not.order.1
472 473 474 475 476 477 478 479 480 481 482 483 484
  (let ((i 0) c1 c2 c3 c4 c5 c6)
    (values
     (count-if-not
      (progn (setf c1 (incf i)) #'null)
      (progn (setf c2 (incf i)) '(a nil b c nil d e))
      :start (progn (setf c3 (incf i)) 0)
      :end (progn (setf c4 (incf i)) 3)
      :key (progn (setf c5 (incf i)) #'not)
      :from-end (progn (setf c6 (incf i)) nil)
      )
     i c1 c2 c3 c4 c5 c6))
  1 6 1 2 3 4 5 6)

485
(deftest count-if-not.order.2
486 487 488 489 490 491 492 493 494 495 496 497 498
  (let ((i 0) c1 c2 c3 c4 c5 c6)
    (values
     (count-if-not
      (progn (setf c1 (incf i)) #'null)
      (progn (setf c2 (incf i)) '(a nil b c nil d e))
      :from-end (progn (setf c3 (incf i)) nil)
      :key (progn (setf c4 (incf i)) #'not)
      :end (progn (setf c5 (incf i)) 3)
      :start (progn (setf c6 (incf i)) 0)
      )
     i c1 c2 c3 c4 c5 c6))
  1 6 1 2 3 4 5 6)

499
;;; Keyword tests
500 501 502 503 504 505 506 507 508 509 510 511

(deftest count-if-not.keywords.1
  (count-if-not #'oddp '(1 2 3 4 5) :bad t :allow-other-keys t)
  2)

(deftest count-if-not.keywords.2
  (count-if-not #'oddp '(1 2 3 4 5) :allow-other-keys #p"*" :also-bad t)
  2)

;;; The leftmost of two :allow-other-keys arguments is the one that  matters.
(deftest count-if-not.keywords.3
  (count-if-not #'oddp '(1 2 3 4 5)
sds's avatar
sds committed
512 513 514
            :allow-other-keys t
            :allow-other-keys nil
            :bad t)
515 516 517 518 519 520 521 522 523 524
  2)

(deftest count-if-not.keywords.4
  (count-if-not #'oddp '(1 2 3 4 5) :key #'identity :key #'1+)
  2)

(deftest count-if-not.allow-other-keys.5
  (count-if-not #'null '(nil a b c nil) :allow-other-keys nil)
  3)

525 526
;;; Error tests

527
(deftest count-if-not.error.1
528 529
  (check-type-error #'(lambda (x) (count-if-not #'identity x)) #'sequencep)
  nil)
530 531

(deftest count-if-not.error.4
532 533
  (signals-error (count-if-not) program-error)
  t)
534 535

(deftest count-if-not.error.5
536 537
  (signals-error (count-if-not #'null) program-error)
  t)
538 539

(deftest count-if-not.error.6
540 541
  (signals-error (count-if-not #'null nil :bad t) program-error)
  t)
542 543

(deftest count-if-not.error.7
544
  (signals-error (count-if-not #'null nil :bad t :allow-other-keys nil)
sds's avatar
sds committed
545
                 program-error)
546
  t)
547 548

(deftest count-if-not.error.8
549 550
  (signals-error (count-if-not #'null nil :key) program-error)
  t)
551 552

(deftest count-if-not.error.9
553 554
  (signals-error (count-if-not #'null nil 3 3) program-error)
  t)
555 556 557

;;; Only leftmost :allow-other-keys argument matters
(deftest count-if-not.error.10
558
  (signals-error (count-if-not #'null nil :bad t
sds's avatar
sds committed
559 560 561
                                :allow-other-keys nil
                                :allow-other-keys t)
                 program-error)
562
  t)
563

564
(deftest count-if-not.error.11
565
  (signals-error (locally (count-if-not #'identity 1) t)
sds's avatar
sds committed
566
                 type-error)
567
  t)
568 569

(deftest count-if-not.error.12
570 571
  (signals-error (count-if-not #'cons '(a b c)) program-error)
  t)
572 573

(deftest count-if-not.error.13
574 575
  (signals-error (count-if-not #'car '(a b c)) type-error)
  t)
576 577

(deftest count-if-not.error.14
578
  (signals-error (count-if-not #'identity '(a b c) :key #'cdr)
sds's avatar
sds committed
579
                 type-error)
580
  t)
581 582

(deftest count-if-not.error.15
583
  (signals-error (count-if-not #'identity '(a b c) :key #'cons)
sds's avatar
sds committed
584
                 program-error)
585
  t)