2
;;;; Author: Paul Dietz
3
;;;; Created: Tue Aug 20 08:01:30 2002
4
;;;; Contains: Tests for COUNT-IF
8
(deftest count-if-list.1
9
(count-if #'identity '(a b nil c d nil e))
12
(deftest count-if-list.2
13
(count-if #'not '(a b nil c d nil e))
16
(deftest count-if-list.3
17
(count-if #'(lambda (x) (break)) nil)
20
(deftest count-if-list.4
21
(count-if #'identity '(a b nil c d nil e) :key #'identity)
24
(deftest count-if-list.5
25
(count-if 'identity '(a b nil c d nil e) :key #'identity)
28
(deftest count-if-list.6
29
(count-if #'identity '(a b nil c d nil e) :key 'identity)
32
(deftest count-if-list.8
33
(count-if #'identity '(a b nil c d nil e) :key 'not)
36
(deftest count-if-list.9
37
(count-if #'evenp '(1 2 3 4 4 1 8 10 1))
40
(deftest count-if-list.10
41
(count-if #'evenp '(1 2 3 4 4 1 8 10 1) :key #'1+)
44
(deftest count-if-list.11
46
(count-if #'evenp '(1 2 3 4 4 1 8 10 1)
47
:key #'(lambda (x) (+ x (incf c)))))
50
(deftest count-if-list.12
52
(count-if #'evenp '(0 1 2 3 4 4 1 7 10 1)
54
:key #'(lambda (x) (+ x (incf c)))))
57
(deftest count-if-list.13
58
(count-if #'(lambda (x) (eqt x 'a))
59
'(a b c d a e f a e f f a a) :start 2)
62
(deftest count-if-list.14
63
(count-if #'(lambda (x) (eqt x 'a))
64
'(a b c d a e f a e f f a a) :end 7)
67
(deftest count-if-list.15
68
(count-if #'(lambda (x) (eqt x 'a))
69
'(a b c d a e f a e f f a a) :end 7
73
(deftest count-if-list.16
74
(count-if #'(lambda (x) (eqt x 'a))
75
'(a b c d a e f a e f f a a) :end 7
82
(deftest count-if-vector.1
83
(count-if #'identity #(a b nil c d nil e))
86
(deftest count-if-vector.2
87
(count-if #'not #(a b nil c d nil e))
90
(deftest count-if-vector.3
91
(count-if #'(lambda (x) (break)) #())
94
(deftest count-if-vector.4
95
(count-if #'identity #(a b nil c d nil e) :key #'identity)
98
(deftest count-if-vector.5
99
(count-if 'identity #(a b nil c d nil e) :key #'identity)
102
(deftest count-if-vector.6
103
(count-if #'identity #(a b nil c d nil e) :key 'identity)
106
(deftest count-if-vector.8
107
(count-if #'identity #(a b nil c d nil e) :key 'not)
110
(deftest count-if-vector.9
111
(count-if #'evenp #(1 2 3 4 4 1 8 10 1))
114
(deftest count-if-vector.10
115
(count-if #'evenp #(1 2 3 4 4 1 8 10 1) :key #'1+)
118
(deftest count-if-vector.11
120
(count-if #'evenp #(1 2 3 4 4 1 8 10 1)
121
:key #'(lambda (x) (+ x (incf c)))))
124
(deftest count-if-vector.12
126
(count-if #'evenp #(0 1 2 3 4 4 1 7 10 1)
128
:key #'(lambda (x) (+ x (incf c)))))
131
(deftest count-if-vector.13
132
(count-if #'(lambda (x) (eqt x 'a))
133
#(a b c d a e f a e f f a a) :start 2)
136
(deftest count-if-vector.14
137
(count-if #'(lambda (x) (eqt x 'a))
138
#(a b c d a e f a e f f a a) :end 7)
141
(deftest count-if-vector.15
142
(count-if #'(lambda (x) (eqt x 'a))
143
#(a b c d a e f a e f f a a) :end 7
147
(deftest count-if-vector.16
148
(count-if #'(lambda (x) (eqt x 'a))
149
#(a b c d a e f a e f f a a) :end 7
150
:start 2 :from-end t)
153
;;; Non-simple vectors
155
(deftest count-if-nonsimple-vector.1
156
(count-if #'identity (make-array 7 :initial-contents '(a b nil c d nil e)
161
(deftest count-if-nonsimple-vector.2
162
(count-if #'not (make-array 7 :initial-contents '(a b nil c d nil e)
167
(deftest count-if-nonsimple-vector.3
168
(count-if #'(lambda (x) (break)) (make-array 0
173
(deftest count-if-nonsimple-vector.4
175
(make-array 7 :initial-contents '(a b nil c d nil e)
181
(deftest count-if-nonsimple-vector.5
183
(make-array 7 :initial-contents '(a b nil c d nil e)
189
(deftest count-if-nonsimple-vector.6
191
(make-array 7 :initial-contents '(a b nil c d nil e)
197
(deftest count-if-nonsimple-vector.8
199
(make-array 7 :initial-contents '(a b nil c d nil e)
205
(deftest count-if-nonsimple-vector.9
206
(count-if #'evenp (make-array 9 :initial-contents '(1 2 3 4 4 1 8 10 1)
207
:fill-pointer t :adjustable t))
210
(deftest count-if-nonsimple-vector.10
212
(make-array 9 :initial-contents '(1 2 3 4 4 1 8 10 1)
213
:fill-pointer t :adjustable t)
217
(deftest count-if-nonsimple-vector.11
220
(make-array 9 :initial-contents '(1 2 3 4 4 1 8 10 1)
221
:fill-pointer t :adjustable t)
222
:key #'(lambda (x) (+ x (incf c)))))
225
(deftest count-if-nonsimple-vector.12
228
(make-array 10 :initial-contents '(0 1 2 3 4 4 1 7 10 1)
229
:fill-pointer t :adjustable t)
231
:key #'(lambda (x) (+ x (incf c)))))
234
(deftest count-if-nonsimple-vector.13
235
(count-if #'(lambda (x) (eqt x 'a))
236
(make-array 13 :initial-contents '(a b c d a e f a e f f a a)
237
:fill-pointer t :adjustable t)
241
(deftest count-if-nonsimple-vector.14
242
(count-if #'(lambda (x) (eqt x 'a))
243
(make-array 13 :initial-contents '(a b c d a e f a e f f a a)
244
:fill-pointer t :adjustable t)
248
(deftest count-if-nonsimple-vector.15
249
(count-if #'(lambda (x) (eqt x 'a))
250
(make-array 13 :initial-contents '(a b c d a e f a e f f a a)
251
:fill-pointer t :adjustable t)
255
(deftest count-if-nonsimple-vector.16
256
(count-if #'(lambda (x) (eqt x 'a))
257
(make-array 13 :initial-contents '(a b c d a e f a e f f a a)
258
:fill-pointer t :adjustable t)
259
:end 7 :start 2 :from-end t)
262
(deftest count-if-nonsimple-vector.17
263
(flet ((%f (x) (eqt x 'a)))
264
(let ((s (make-array 13 :initial-contents '(a b c d a e f a e f f a a)
266
(values (count-if #'%f s)
267
(count-if #'%f s :end nil)
268
(count-if #'%f s :end 4)
269
(count-if #'%f s :start 1)
270
(count-if #'%f s :start 1 :end 4)
271
(count-if #'%f s :start 1 :end 4 :from-end t))))
274
;;; tests on bit-vectors
276
(deftest count-if-bit-vector.1
277
(count-if #'evenp #*001011101101)
280
(deftest count-if-bit-vector.2
281
(count-if #'identity #*001011101101)
284
(deftest count-if-bit-vector.3
285
(count-if #'(lambda (x) (break)) #*)
288
(deftest count-if-bit-vector.4
289
(count-if #'identity #*001011101101 :key #'zerop)
292
(deftest count-if-bit-vector.5
293
(count-if 'identity #*001011101101 :key #'zerop)
296
(deftest count-if-bit-vector.6
297
(count-if #'identity #*001011101101 :key 'zerop)
300
(deftest count-if-bit-vector.8
301
(count-if #'identity #*001011101101 :key 'oddp)
304
(deftest count-if-bit-vector.10
305
(count-if #'evenp #*001011101101 :key #'1+)
308
(deftest count-if-bit-vector.11
310
(count-if #'evenp #*001011101101
311
:key #'(lambda (x) (+ x (incf c)))))
314
(deftest count-if-bit-vector.12
316
(count-if #'evenp #*001011101101
318
:key #'(lambda (x) (+ x (incf c)))))
321
(deftest count-if-bit-vector.13
322
(count-if #'zerop #*0111011011100 :start 2)
325
(deftest count-if-bit-vector.14
326
(count-if #'zerop #*0111011011100 :end 7)
329
(deftest count-if-bit-vector.15
330
(count-if #'zerop #*0111011011100 :end 7 :start 2)
333
(deftest count-if-bit-vector.16
334
(count-if #'zerop #*0111011011100 :end 7 :start 2 :from-end t)
337
(deftest count-if-bit-vector.17
338
(let ((s (make-array '(10) :initial-contents '(0 0 1 0 1 0 0 1 1 0)
341
(values (count-if #'zerop s)
342
(count-if #'zerop s :end nil)
343
(count-if #'zerop s :end 4)
344
(count-if #'zerop s :start 5)
345
(count-if #'zerop s :start 1 :end 4)))
350
(deftest count-if-string.1
351
(count-if #'(lambda (x) (eql x #\0)) "001011101101")
354
(deftest count-if-string.2
355
(count-if #'identity "001011101101")
358
(deftest count-if-string.3
359
(count-if #'(lambda (x) (break)) "")
362
(deftest count-if-string.4
363
(count-if #'identity "001011101101" :key #'(lambda (x) (eql x #\0)))
366
(deftest count-if-string.5
367
(count-if 'identity "001011101101" :key #'(lambda (x) (eql x #\0)))
370
(deftest count-if-string.6
371
(count-if #'(lambda (x) (eql x #\0)) "001011101101" :key 'identity)
374
(deftest count-if-string.8
375
(count-if #'identity "001011101101" :key #'(lambda (x) (eql x #\1)))
378
(deftest count-if-string.11
380
(count-if #'evenp "001011101101"
381
:key #'(lambda (x) (+ (if (eql x #\0) 0 1) (incf c)))))
384
(deftest count-if-string.12
386
(count-if #'evenp "001011101101"
388
:key #'(lambda (x) (+ (if (eql x #\0) 0 1) (incf c)))))
391
(deftest count-if-string.13
392
(count-if #'(lambda (x) (eql x #\0)) "0111011011100" :start 2)
395
(deftest count-if-string.14
396
(count-if #'(lambda (x) (eql x #\0)) "0111011011100" :end 7)
399
(deftest count-if-string.15
400
(count-if #'(lambda (x) (eql x #\0)) "0111011011100" :end 7 :start 2)
403
(deftest count-if-string.16
404
(count-if #'(lambda (x) (eql x #\0))
405
"0111011011100" :end 7 :start 2 :from-end t)
408
(deftest count-if-string.17
409
(let ((s (make-array '(10)
410
:initial-contents "00a0aa0a0a"
411
:element-type 'character
413
(values (count-if #'digit-char-p s)
414
(count-if #'digit-char-p s :end nil)
415
(count-if #'digit-char-p s :start 1)
416
(count-if #'digit-char-p s :end 2)
417
(count-if #'digit-char-p s :start 1 :end 2)))
420
;;; Argument order tests
422
(deftest count-if.order.1
423
(let ((i 0) c1 c2 c3 c4 c5 c6)
425
(count-if (progn (setf c1 (incf i)) #'null)
426
(progn (setf c2 (incf i)) '(a nil b c nil d e))
427
:start (progn (setf c3 (incf i)) 0)
428
:end (progn (setf c4 (incf i)) 3)
429
:key (progn (setf c5 (incf i)) #'identity)
430
:from-end (progn (setf c6 (incf i)) nil)
432
i c1 c2 c3 c4 c5 c6))
435
(deftest count-if.order.2
436
(let ((i 0) c1 c2 c3 c4 c5 c6)
438
(count-if (progn (setf c1 (incf i)) #'null)
439
(progn (setf c2 (incf i)) '(a nil b c nil d e))
440
:from-end (progn (setf c3 (incf i)) nil)
441
:key (progn (setf c4 (incf i)) #'identity)
442
:end (progn (setf c5 (incf i)) 3)
443
:start (progn (setf c6 (incf i)) 0)
445
i c1 c2 c3 c4 c5 c6))
451
(deftest count-if.allow-other-keys.1
452
(count-if #'evenp '(1 2 3 4 5) :bad t :allow-other-keys t)
455
(deftest count-if.allow-other-keys.2
456
(count-if #'evenp '(1 2 3 4 5) :allow-other-keys #p"*" :also-bad t)
459
;;; The leftmost of two :allow-other-keys arguments is the one that matters.
460
(deftest count-if.allow-other-keys.3
461
(count-if #'evenp '(1 2 3 4 5)
463
:allow-other-keys nil
467
(deftest count-if.keywords.4
468
(count-if #'evenp '(1 2 3 4 5) :key #'identity :key #'1+)
471
(deftest count-if.allow-other-keys.5
472
(count-if #'evenp '(1 2 3 4 5) :allow-other-keys nil)
478
(deftest count-if.error.1
479
(signals-error (count-if #'identity 1) type-error)
482
(deftest count-if.error.2
483
(signals-error (count-if #'identity 'a) type-error)
486
(deftest count-if.error.3
487
(signals-error (count-if #'identity #\a) type-error)
490
(deftest count-if.error.4
491
(signals-error (count-if) program-error)
494
(deftest count-if.error.5
495
(signals-error (count-if #'null) program-error)
498
(deftest count-if.error.6
499
(signals-error (count-if #'null nil :bad t) program-error)
502
(deftest count-if.error.7
503
(signals-error (count-if #'null nil :bad t :allow-other-keys nil)
507
(deftest count-if.error.8
508
(signals-error (count-if #'null nil :key) program-error)
511
(deftest count-if.error.9
512
(signals-error (count-if #'null nil 3 3) program-error)
515
;;; Only leftmost :allow-other-keys argument matters
516
(deftest count-if.error.10
517
(signals-error (count-if #'null nil :bad t
518
:allow-other-keys nil
523
(deftest count-if.error.11
524
(signals-error (locally (count-if #'identity 1) t) type-error)
527
(deftest count-if.error.12
528
(signals-error (count-if #'cons '(a b c)) program-error)
531
(deftest count-if.error.13
532
(signals-error (count-if #'car '(a b c)) type-error)
535
(deftest count-if.error.14
536
(signals-error (count-if #'identity '(a b c) :key #'cdr)
540
(deftest count-if.error.15
541
(signals-error (count-if #'identity '(a b c) :key #'cons)