count-if.lsp 12.7 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
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Tue Aug 20 08:01:30 2002
;;;; Contains: Tests for COUNT-IF

(in-package :cl-test)

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

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

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

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

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

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

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

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

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

(deftest count-if-list.11
  (let ((c 0))
    (count-if #'evenp '(1 2 3 4 4 1 8 10 1)
	      :key #'(lambda (x) (+ x (incf c)))))
  6)

(deftest count-if-list.12
  (let ((c 0))
    (count-if #'evenp '(0 1 2 3 4 4 1 7 10 1)
	      :from-end t
	      :key #'(lambda (x) (+ x (incf c)))))
  8)

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

(deftest count-if-list.14
63
  (count-if #'(lambda (x) (eqt x 'a))
64 65 66 67
	    '(a b c d a e f a e f f a a) :end 7)
  2)
  
(deftest count-if-list.15
68
  (count-if #'(lambda (x) (eqt x 'a))
69 70 71 72 73
	    '(a b c d a e f a e f f a a) :end 7
	    :start 2)
  1)
  
(deftest count-if-list.16
74
  (count-if #'(lambda (x) (eqt x 'a))
75 76 77 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 121 122 123 124 125 126 127 128 129 130 131
	    '(a b c d a e f a e f f a a) :end 7
	    :start 2 :from-end t)
  1)
  

;;; tests on vectors

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

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

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

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

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

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

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

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

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

(deftest count-if-vector.11
  (let ((c 0))
    (count-if #'evenp #(1 2 3 4 4 1 8 10 1)
	      :key #'(lambda (x) (+ x (incf c)))))
  6)

(deftest count-if-vector.12
  (let ((c 0))
    (count-if #'evenp #(0 1 2 3 4 4 1 7 10 1)
	      :from-end t
	      :key #'(lambda (x) (+ x (incf c)))))
  8)

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

(deftest count-if-vector.14
137
  (count-if #'(lambda (x) (eqt x 'a))
138 139 140 141
	    #(a b c d a e f a e f f a a) :end 7)
  2)
  
(deftest count-if-vector.15
142
  (count-if #'(lambda (x) (eqt x 'a))
143 144 145 146 147
	    #(a b c d a e f a e f f a a) :end 7
	    :start 2)
  1)
  
(deftest count-if-vector.16
148
  (count-if #'(lambda (x) (eqt x 'a))
149 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	    #(a b c d a e f a e f f a a) :end 7
	    :start 2 :from-end t)
  1)

;;; Non-simple vectors

(deftest count-if-nonsimple-vector.1
  (count-if #'identity (make-array 7 :initial-contents '(a b nil c d nil e)
				   :fill-pointer t
				   :adjustable t))
  5)

(deftest count-if-nonsimple-vector.2
  (count-if #'not (make-array 7 :initial-contents '(a b nil c d nil e)
				   :fill-pointer t
				   :adjustable t))
  2)

(deftest count-if-nonsimple-vector.3
  (count-if #'(lambda (x) (break)) (make-array 0
				   :fill-pointer t
				   :adjustable t))
  0)

(deftest count-if-nonsimple-vector.4
  (count-if #'identity
	    (make-array 7 :initial-contents '(a b nil c d nil e)
			:fill-pointer t
			:adjustable t)
	    :key #'identity)
  5)

(deftest count-if-nonsimple-vector.5
  (count-if 'identity
	    (make-array 7 :initial-contents '(a b nil c d nil e)
			:fill-pointer t
			:adjustable t)
	    :key #'identity)
  5)

(deftest count-if-nonsimple-vector.6
  (count-if #'identity
	    (make-array 7 :initial-contents '(a b nil c d nil e)
			:fill-pointer t
			:adjustable t)
	    :key 'identity)
  5)

(deftest count-if-nonsimple-vector.8
  (count-if #'identity
	    (make-array 7 :initial-contents '(a b nil c d nil e)
			:fill-pointer t
			:adjustable t)
	    :key 'not)
  2)

(deftest count-if-nonsimple-vector.9
  (count-if #'evenp (make-array 9 :initial-contents '(1 2 3 4 4 1 8 10 1)
				:fill-pointer t :adjustable t))
  5)

(deftest count-if-nonsimple-vector.10
  (count-if #'evenp
	    (make-array 9 :initial-contents '(1 2 3 4 4 1 8 10 1)
			:fill-pointer t :adjustable t)
	    :key #'1+)
  4)

(deftest count-if-nonsimple-vector.11
  (let ((c 0))
    (count-if #'evenp
	      (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)))))
  6)

(deftest count-if-nonsimple-vector.12
  (let ((c 0))
    (count-if #'evenp
	      (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)))))
  8)

(deftest count-if-nonsimple-vector.13
235
  (count-if #'(lambda (x) (eqt x 'a))
236 237 238 239 240 241
	    (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)
  4)

(deftest count-if-nonsimple-vector.14
242
  (count-if #'(lambda (x) (eqt x 'a))
243 244 245 246 247 248
	    (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)
  2)
  
(deftest count-if-nonsimple-vector.15
249
  (count-if #'(lambda (x) (eqt x 'a))
250 251 252 253 254 255
	    (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)
  1)
  
(deftest count-if-nonsimple-vector.16
256
  (count-if #'(lambda (x) (eqt x 'a))
257 258 259 260 261
	    (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)
  1)

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
(deftest count-if-nonsimple-vector.17
  (flet ((%f (x) (eqt x 'a)))
    (let ((s (make-array 13 :initial-contents '(a b c d a e f a e f f a a)
			 :fill-pointer 6)))
      (values (count-if #'%f s)
	      (count-if #'%f s :end nil)
	      (count-if #'%f s :end 4)
	      (count-if #'%f s :start 1)
	      (count-if #'%f s :start 1 :end 4)
	      (count-if #'%f s :start 1 :end 4 :from-end t))))
  2 2 1 1 0 0)

;;; tests on bit-vectors

(deftest count-if-bit-vector.1
277 278 279
  (count-if #'evenp #*001011101101)
  5)

280
(deftest count-if-bit-vector.2
281 282 283
  (count-if #'identity #*001011101101)
  12)

284
(deftest count-if-bit-vector.3
285 286 287
  (count-if #'(lambda (x) (break)) #*)
  0)

288
(deftest count-if-bit-vector.4
289 290 291
  (count-if #'identity #*001011101101 :key #'zerop)
  5)

292
(deftest count-if-bit-vector.5
293 294 295
  (count-if 'identity #*001011101101 :key #'zerop)
  5)

296
(deftest count-if-bit-vector.6
297 298 299
  (count-if #'identity #*001011101101 :key 'zerop)
  5)

300
(deftest count-if-bit-vector.8
301 302 303
  (count-if #'identity #*001011101101 :key 'oddp)
  7)

304
(deftest count-if-bit-vector.10
305 306 307
  (count-if #'evenp #*001011101101 :key #'1+)
  7)

308
(deftest count-if-bit-vector.11
309 310 311 312 313
  (let ((c 0))
    (count-if #'evenp #*001011101101
	      :key #'(lambda (x) (+ x (incf c)))))
  7)

314
(deftest count-if-bit-vector.12
315 316 317 318 319 320
  (let ((c 0))
    (count-if #'evenp #*001011101101
	      :from-end t
	      :key #'(lambda (x) (+ x (incf c)))))
  5)

321
(deftest count-if-bit-vector.13
322 323 324
  (count-if #'zerop #*0111011011100 :start 2)
  4)

325
(deftest count-if-bit-vector.14
326 327 328
  (count-if #'zerop #*0111011011100 :end 7)
  2)
  
329
(deftest count-if-bit-vector.15
330 331 332
  (count-if #'zerop #*0111011011100 :end 7 :start 2)
  1)
  
333
(deftest count-if-bit-vector.16
334 335 336
  (count-if #'zerop #*0111011011100 :end 7 :start 2 :from-end t)
  1)

337 338 339 340 341 342 343 344 345 346 347
(deftest count-if-bit-vector.17
  (let ((s (make-array '(10) :initial-contents '(0 0 1 0 1 0 0 1 1 0)
		       :element-type 'bit
		       :fill-pointer 6)))
    (values (count-if #'zerop s)
	    (count-if #'zerop s :end nil)
	    (count-if #'zerop s :end 4)
	    (count-if #'zerop s :start 5)
	    (count-if #'zerop s :start 1 :end 4)))
  4 4 3 1 2)

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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
;;; tests on strings

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

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

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

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

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

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

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

(deftest count-if-string.11
  (let ((c 0))
    (count-if #'evenp "001011101101"
	      :key #'(lambda (x) (+ (if (eql x #\0) 0 1) (incf c)))))
  7)

(deftest count-if-string.12
  (let ((c 0))
    (count-if #'evenp "001011101101"
	      :from-end t
	      :key #'(lambda (x) (+ (if (eql x #\0) 0 1) (incf c)))))
  5)

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

(deftest count-if-string.14
  (count-if #'(lambda (x) (eql x #\0)) "0111011011100" :end 7)
  2)
  
(deftest count-if-string.15
  (count-if #'(lambda (x) (eql x #\0)) "0111011011100" :end 7 :start 2)
  1)
  
(deftest count-if-string.16
  (count-if #'(lambda (x) (eql x #\0))
	    "0111011011100" :end 7 :start 2 :from-end t)
  1)

408 409
(deftest count-if-string.17
  (let ((s (make-array '(10)
410
		       :initial-contents "00a0aa0a0a"
411 412 413 414 415 416 417 418
		       :element-type 'character
		       :fill-pointer 6)))
    (values (count-if #'digit-char-p s)
	    (count-if #'digit-char-p s :end nil)
	    (count-if #'digit-char-p s :start 1)
	    (count-if #'digit-char-p s :end 2)
	    (count-if #'digit-char-p s :start 1 :end 2)))
  3 3 2 2 1)
419

420 421
;;; Argument order tests

422
(deftest count-if.order.1
423 424 425 426 427 428 429 430 431 432 433 434
  (let ((i 0) c1 c2 c3 c4 c5 c6)
    (values
     (count-if (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)) #'identity)
	       :from-end (progn (setf c6 (incf i)) nil)
	       )
     i c1 c2 c3 c4 c5 c6))
  1 6 1 2 3 4 5 6)

435
(deftest count-if.order.2
436 437 438 439 440 441 442 443 444 445 446 447 448
  (let ((i 0) c1 c2 c3 c4 c5 c6)
    (values
     (count-if (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)) #'identity)
	       :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)


449
;;; Keyword tests
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

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

(deftest count-if.allow-other-keys.2
  (count-if #'evenp '(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.allow-other-keys.3
  (count-if #'evenp '(1 2 3 4 5)
	    :allow-other-keys t
	    :allow-other-keys nil
	    :bad t)
  2)

467
(deftest count-if.keywords.4
468 469 470
  (count-if #'evenp '(1 2 3 4 5) :key #'identity :key #'1+)
  2)

471 472 473
(deftest count-if.allow-other-keys.5
  (count-if #'evenp '(1 2 3 4 5) :allow-other-keys nil)
  2)
474

475
	    
476 477
;;; Error tests

478
(deftest count-if.error.1
479
  (classify-error (count-if #'identity 1))
480 481
  type-error)

482
(deftest count-if.error.2
483
  (classify-error (count-if #'identity 'a))
484 485
  type-error)

486
(deftest count-if.error.3
487
  (classify-error (count-if #'identity #\a))
488
  type-error)
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

(deftest count-if.error.4
  (classify-error (count-if))
  program-error)

(deftest count-if.error.5
  (classify-error (count-if #'null))
  program-error)

(deftest count-if.error.6
  (classify-error (count-if #'null nil :bad t))
  program-error)

(deftest count-if.error.7
  (classify-error (count-if #'null nil :bad t :allow-other-keys nil))
  program-error)

(deftest count-if.error.8
  (classify-error (count-if #'null nil :key))
  program-error)

(deftest count-if.error.9
  (classify-error (count-if #'null nil 3 3))
  program-error)
513 514 515 516 517 518 519

;;; Only leftmost :allow-other-keys argument matters
(deftest count-if.error.10
  (classify-error (count-if #'null nil :bad t
			    :allow-other-keys nil
			    :allow-other-keys t))
  program-error)
520 521 522 523

(deftest count-if.error.11
  (classify-error (locally (count-if #'identity 1) t))
  type-error)
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

(deftest count-if.error.12
  (classify-error (count-if #'cons '(a b c)))
  program-error)

(deftest count-if.error.13
  (classify-error (count-if #'car '(a b c)))
  type-error)

(deftest count-if.error.14
  (classify-error (count-if #'identity '(a b c) :key #'cdr))
  type-error)

(deftest count-if.error.15
  (classify-error (count-if #'identity '(a b c) :key #'cons))
  program-error)