count.lsp 13.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Mon Aug 19 07:31:55 2002
;;;; Contains: Tests for COUNT

(in-package :cl-test)

(deftest count-list.1
  (count 'a '(a b c d e a e f))
  2)

(deftest count-list.2
13
  (count 'a '(a b c d e a e f) :test #'eql)
14
15
16
  2)

(deftest count-list.3
17
  (count 'a '(a b c d e a e f) :test 'eql)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  2)

(deftest count-list.4
  (count 1 '(1 2 2 3 2 1 2 2 5 4) :key #'1-)
  5)

(deftest count-list.5
  (count 1 '(1 2 2 3 2 1 2 2 5 4) :key '1-)
  5)

(deftest count-list.6
  (count 1 '(1 2 2 3 2 1 2 2 5 4) :key #'1- :test #'equal)
  5)

(deftest count-list.7
  (count 1 '(2 1 1 2 3 1 4 1 7 6 1 8) :from-end t)
  5)

(deftest count-list.8
  (let ((c 0))
    (count 1 '(1 2 3 1 4 1 7 6 1 8)
	   :key #'(lambda (x)
		    ;; (format t "~%~A ~A" x c)
		    (prog1 (- x c) (incf c)))))
  4)

(deftest count-list.9
  (let ((c 0))
    (count 1 '(1 2 3 7 4 5 7 6 2 8)
	   :from-end t
	   :key #'(lambda (x)
		    ;; (format t "~%~A ~A" x c)
		    (prog1 (- x c) (incf c)))))
  3)

(deftest count-list.10
  (count 1 '(1 1 1 1 1 2 1 1) :start 3)
  4)

(deftest count-list.11
  (count 1 '(1 1 1 1 1 2 1 1) :end 6)
  5)

(deftest count-list.12
  (count 1 '(1 1 1 1 1 2 1 1) :start 2 :end 7)
  4)

(deftest count-list.13
  (count 1 '(1 1 1 1 1 2 1 1) :start 3 :end nil)
  4)

(deftest count-list.14
  (count 1 '(1 1 1 1 1 2 1 1)  :end nil)
  7)

(deftest count-list.15
74
  (count 1 '(1 1 1 1 1 2 1 1)  :test-not #'eql)
75
76
77
78
  1)

(deftest count-list.16
  (count 1 '(1 1 1 3 1 2 1 1) :start 2 :end 7
79
	 :test #'(lambda (x y) (declare (ignore x y))  t))
80
81
  5)

82
83
84
85
86
87
88
89
(deftest count-list.17
  (count 10 '(1 11 2 4 14 5 18 6 7) :test #'<)
  3)

(deftest count-list.18
  (count 10 '(1 11 2 4 14 5 18 6 7) :test-not #'>=)
  3)

90
91
92
93
94
95
96
;;; On vectors

(deftest count-vector.1
  (count 'a #(a b c d e a e f))
  2)

(deftest count-vector.2
97
  (count 'a #(a b c d e a e f) :test #'eql)
98
99
100
  2)

(deftest count-vector.3
101
  (count 'a #(a b c d e a e f) :test 'eql)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  2)

(deftest count-vector.4
  (count 1 #(1 2 2 3 2 1 2 2 5 4) :key #'1-)
  5)

(deftest count-vector.5
  (count 1 #(1 2 2 3 2 1 2 2 5 4) :key '1-)
  5)

(deftest count-vector.6
  (count 1 #(1 2 2 3 2 1 2 2 5 4) :key #'1- :test #'equal)
  5)

(deftest count-vector.7
  (count 1 #(2 1 1 2 3 1 4 1 7 6 1 8) :from-end t)
  5)

(deftest count-vector.8
  (let ((c 0))
    (count 1 #(1 2 3 1 4 1 7 6 1 8)
	   :key #'(lambda (x)
		    ;; (format t "~%~A ~A" x c)
		    (prog1 (- x c) (incf c)))))
  4)

(deftest count-vector.9
  (let ((c 0))
    (count 1 #(1 2 3 7 4 5 7 6 2 8)
	   :from-end t
	   :key #'(lambda (x)
		    ;; (format t "~%~A ~A" x c)
		    (prog1 (- x c) (incf c)))))
  3)

(deftest count-vector.10
  (count 1 #(1 1 1 1 1 2 1 1) :start 3)
  4)

(deftest count-vector.11
  (count 1 #(1 1 1 1 1 2 1 1) :end 6)
  5)

(deftest count-vector.12
  (count 1 #(1 1 1 1 1 2 1 1) :start 2 :end 7)
  4)

(deftest count-vector.13
  (count 1 #(1 1 1 1 1 2 1 1) :start 3 :end nil)
  4)

(deftest count-vector.14
  (count 1 #(1 1 1 1 1 2 1 1)  :end nil)
  7)

(deftest count-vector.15
158
  (count 1 #(1 1 1 1 1 2 1 1)  :test-not #'eql)
159
160
  1)

161
(deftest count-vector.16
162
  (count 1 #(1 1 1 3 1 2 1 1) :start 2 :end 7
163
	 :test #'(lambda (x y) (declare (ignore x y)) t))
164
165
  5)

166
167
168
169
170
171
172
173
(deftest count-vector.17
  (count 10 #(1 11 2 4 14 5 18 6 7) :test #'<)
  3)

(deftest count-vector.18
  (count 10 #(1 11 2 4 14 5 18 6 7) :test-not #'>=)
  3)

174
175
176
177
178
179
180
181
182
183
;;; Non-simple vectors

(deftest count-filled-vector.1
  (count 'a (make-array 8 :initial-contents '(a b c d e a e f)
			:fill-pointer t))
  2)

(deftest count-filled-vector.2
  (count 'a (make-array 8 :initial-contents '(a b c d e a e f)
			:fill-pointer t)
184
	 :test #'eql)
185
186
187
188
189
  2)

(deftest count-filled-vector.3
  (count 'a (make-array 8 :initial-contents '(a b c d e a e f)
			:fill-pointer t)
190
	 :test 'eql)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  2)

(deftest count-filled-vector.4
  (count 1 (make-array 10 :initial-contents '(1 2 2 3 2 1 2 2 5 4)
		       :fill-pointer t)
	 :key #'1-)
  5)

(deftest count-filled-vector.5
  (count 1 (make-array 10 :initial-contents '(1 2 2 3 2 1 2 2 5 4)
		       :fill-pointer t)
	 :key '1-)
  5)

(deftest count-filled-vector.6
  (count 1 (make-array 10 :initial-contents '(1 2 2 3 2 1 2 2 5 4)
		       :fill-pointer t)
	 :key #'1- :test #'equal)
  5)

(deftest count-filled-vector.7
  (count 1 (make-array 12 :initial-contents '(2 1 1 2 3 1 4 1 7 6 1 8)
		       :fill-pointer t)
	 :from-end t)
  5)

(deftest count-filled-vector.8
  (let ((c 0))
    (count 1 (make-array 10 :initial-contents '(1 2 3 1 4 1 7 6 1 8)
			 :fill-pointer t)
	   :key #'(lambda (x)
		    ;; (format t "~%~A ~A" x c)
		    (prog1 (- x c) (incf c)))))
  4)

(deftest count-filled-vector.9
  (let ((c 0))
    (count 1 (make-array 10 :initial-contents '(1 2 3 7 4 5 7 6 2 8)
			 :fill-pointer t)
	   :from-end t
	   :key #'(lambda (x)
		    ;; (format t "~%~A ~A" x c)
		    (prog1 (- x c) (incf c)))))
  3)

(deftest count-filled-vector.10
  (count 1 (make-array 8 :initial-contents '(1 1 1 1 1 2 1 1)
		       :fill-pointer t)
	 :start 3)
  4)

(deftest count-filled-vector.11
  (count 1 (make-array 8 :initial-contents '(1 1 1 1 1 2 1 1)
		       :fill-pointer t)
	 :end 6)
  5)

(deftest count-filled-vector.12
  (count 1 (make-array 8 :initial-contents '(1 1 1 1 1 2 1 1)
		       :fill-pointer t)
	 :start 2 :end 7)
  4)

(deftest count-filled-vector.13
  (count 1 (make-array 8 :initial-contents '(1 1 1 1 1 2 1 1)
		       :fill-pointer t)
	 :start 3 :end nil)
  4)

(deftest count-filled-vector.14
  (count 1 (make-array 8 :initial-contents '(1 1 1 1 1 2 1 1)
		       :fill-pointer t)
	 :end nil)
  7)

(deftest count-filled-vector.15
  (count 1 (make-array 8 :initial-contents '(1 1 1 1 1 2 1 1)
		       :fill-pointer t)
269
	 :test-not #'eql)
270
271
272
273
274
275
  1)

(deftest count-filled-vector.16
  (count 1 (make-array 8 :initial-contents '(1 1 1 3 1 2 1 1)
		       :fill-pointer t)
	 :start 2 :end 7
276
	 :test #'(lambda (x y) (declare (ignore x y)) t))
277
278
  5)

279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
(deftest count-filled-vector.17
  (count 1 (make-array 8 :initial-contents '(1 1 1 1 1 1 1 1)
		       :fill-pointer 6))
  6)

(deftest count-filled-vector.18
  (count 1 (make-array 8 :initial-contents '(1 1 1 1 1 1 1 1)
		       :fill-pointer 6)
	 :start 2)
  4)
(deftest count-filled-vector.19
  (count 1 (make-array 8 :initial-contents '(1 1 1 1 1 1 1 1)
		       :fill-pointer 6)
	 :from-end 'foo)
  6)

(deftest count-filled-vector.20
  (count 1 (make-array 8 :initial-contents '(1 1 1 1 1 1 1 1)
		       :fill-pointer 6)
	 :start 2 :from-end 'yes)
  4)



303
304
305
306
307
308
309
;;; Tests on bit vectors

(deftest count-bit-vector.1
  (count 1 #*00101100011011000)
  7)

(deftest count-bit-vector.2
310
  (count 1 #*00101100011011000 :test #'eql)
311
312
313
  7)

(deftest count-bit-vector.3
314
  (count 1 #*00101100011011000 :test 'eql)
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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
365
366
  7)

(deftest count-bit-vector.4
  (count 1 #*00101100011011000 :key #'1+)
  10)

(deftest count-bit-vector.5
  (count 0 #*00101100011011000 :key '1-)
  7)

(deftest count-bit-vector.6
  (count 0 #*00101100011011000 :key #'1- :test #'equal)
  7)

(deftest count-bit-vector.7
  (count 1 #*00101100011011000 :from-end t)
  7)

(deftest count-bit-vector.8
  (let ((c 1))
    (count 0 #*0000110101001
	   :key #'(lambda (x) (setf c (- c)) (+ c x))))
  2)

(deftest count-bit-vector.9
  (let ((c 1))
    (count 0 #*0000011010101
	   :from-end t
	   :key #'(lambda (x) (setf c (- c)) (+ c x))))
  4)

(deftest count-bit-vector.10
  (count 1 #*11000110110 :start 3)
  4)

(deftest count-bit-vector.11
  (count 1 '#*110111110111 :end 6)
  5)

(deftest count-bit-vector.12
  (count 1 #*11111011 :start 2 :end 7)
  4)

(deftest count-bit-vector.13
  (count 1 #*11111011 :start 3 :end nil)
  4)

(deftest count-bit-vector.14
  (count 1 #*11111011 :end nil)
  7)

(deftest count-bit-vector.15
367
  (count 1 #*11111011  :test-not #'eql)
368
369
370
371
  1)

(deftest count-bit-vector.16
  (count 1 #*11101101 :start 2 :end 7
372
	 :test #'(lambda (x y) (declare (ignore x y)) t))
373
374
  5)

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
(deftest count-bit-vector.17
  (count 1 (make-array 8 :initial-contents '(1 0 1 1 1 1 1 1)
		       :element-type 'bit
		       :fill-pointer 5))
  4)

(deftest count-bit-vector.18
  (count 1 (make-array 8 :initial-contents '(1 0 1 1 1 1 1 1)
		       :element-type 'bit
		       :fill-pointer 5)
	 :start 1)
  3)

(deftest count-bit-vector.19
  (count 1 (make-array 8 :initial-contents '(1 0 1 1 1 1 1 1)
		       :element-type 'bit
		       :fill-pointer 5)
	 :end nil)
  4)


(deftest count-bit-vector.20
  (count 1 (make-array 8 :initial-contents '(1 0 1 1 1 1 1 1)
		       :element-type 'bit
		       :fill-pointer 6)
	 :end 4)
  3)

403
404
405
406
407
408
409
(deftest count-bit-vector.21
  (count 1 #*00001100100 :test #'<=)
  3)

(deftest count-bit-vector.22
  (count 1 #*00001100100 :test-not #'>)
  3)
410

411
412
413
414
415
416
417
;;; Tests on strings

(deftest count-string.1
  (count #\1 "00101100011011000")
  7)

(deftest count-string.2
418
  (count #\1 "00101100011011000" :test #'eql)
419
420
421
  7)

(deftest count-string.3
422
  (count #\1 "00101100011011000" :test 'eql)
423
424
425
  7)

(deftest count-string.4
426
  (count #\1 "00101100011011000" :key #'(lambda (x) (if (eql x #\0) #\1 #\2)))
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
  10)

(deftest count-string.5
  (count #\1 "00101100011011000" :key 'identity)
  7)

(deftest count-string.6
  (count #\1 "00101100011011000" :key #'identity :test #'equal)
  7)

(deftest count-string.7
  (count #\1 "00101100011011000" :from-end t)
  7)

(deftest count-string.8
  (let ((c nil))
    (count #\0 "0000110101001"
	   :key #'(lambda (x) (setf c (not c))
		    (and c x))))
  5)

(deftest count-string.9
  (let ((c nil))
    (count #\0 "0000011010101"
	   :from-end t
	   :key #'(lambda (x) (setf c (not c))
		    (and c x))))
  3)

(deftest count-string.10
  (count #\1 "11000110110" :start 3)
  4)

(deftest count-string.11
  (count #\1 '"110111110111" :end 6)
  5)

(deftest count-string.12
  (count #\1 "11111011" :start 2 :end 7)
  4)

(deftest count-string.13
  (count #\1 "11111011" :start 3 :end nil)
  4)

(deftest count-string.14
  (count #\1 "11111011" :end nil)
  7)

(deftest count-string.15
477
  (count #\1 "11111011"  :test-not #'eql)
478
479
480
481
  1)

(deftest count-string.16
  (count #\1 "11101101" :start 2 :end 7
482
	 :test #'(lambda (x y) (declare (ignore x y)) t))
483
484
  5)

485
(deftest count-string.17
486
  (count #\a (make-array 10 :initial-contents "abaaacaaaa"
487
488
489
490
491
			 :fill-pointer 7
			 :element-type 'character))
  5)

(deftest count-string.18
492
  (count #\a (make-array 10 :initial-contents "abaaacaaaa"
493
494
495
496
497
498
			 :fill-pointer 7
			 :element-type 'character)
	 :start 1)
  4)

(deftest count-string.19
499
  (count #\a (make-array 10 :initial-contents "abaaacaaaa"
500
501
502
503
504
505
			 :fill-pointer 7
			 :element-type 'character)
	 :end nil)
  5)

(deftest count-string.20
506
  (count #\a (make-array 10 :initial-contents "abaaacaaaa"
507
508
509
510
511
			 :fill-pointer 7
			 :element-type 'character)
	 :start 2 :end 5)
  3)

512
513
514
515
516
517
518
519
(deftest count-string.21
  (count #\1 "00001100100" :test #'char<=)
  3)

(deftest count-string.22
  (count #\1 "00001100100" :test-not #'char>)
  3)

520
521
;;; Argument order tests

522
(deftest count.order.1
523
524
525
526
527
528
529
530
531
532
533
534
535
  (let ((i 0) c1 c2 c3 c4 c5 c6 c7)
    (values
     (count (progn (setf c1 (incf i)) nil)
	    (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)
	    :test (progn (setf c7 (incf i)) #'eql)
	    )
     i c1 c2 c3 c4 c5 c6 c7))
  1 7 1 2 3 4 5 6 7)

536
(deftest count.order.2
537
538
539
540
541
542
543
544
545
546
547
548
549
550
  (let ((i 0) c1 c2 c3 c4 c5 c6 c7)
    (values
     (count (progn (setf c1 (incf i)) nil)
	    (progn (setf c2 (incf i)) '(a nil b c nil d e))
	    :test (progn (setf c3 (incf i)) #'eql)
	    :from-end (progn (setf c4 (incf i)) nil)
	    :key (progn (setf c5 (incf i)) #'identity)
	    :end (progn (setf c6 (incf i)) 3)
	    :start (progn (setf c7 (incf i)) 0)
	    )
     i c1 c2 c3 c4 c5 c6 c7))
  1 7 1 2 3 4 5 6 7)


551
;;; Keyword tests
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568

(deftest count.allow-other-keys.1
  (count 'a '(b a d a c) :bad t :allow-other-keys t)
  2)

(deftest count.allow-other-keys.2
  (count 'a '(b a d a c) :allow-other-keys #p"*" :also-bad t)
  2)

;;; The leftmost of two :allow-other-keys arguments is the one that  matters.
(deftest count.allow-other-keys.3
  (count 'a '(b a d a c)
	 :allow-other-keys t
	 :allow-other-keys nil
	 :bad t)
  2)

569
(deftest count.keywords.4
570
571
572
573
574
575
576
  (count 2 '(1 2 3 2 5) :key #'identity :key #'1+)
  2)

(deftest count.allow-other-keys.5
  (count 'a '(a b c a) :allow-other-keys nil)
  2)

577
578
;;; Error tests

579
(deftest count.error.1
580
  (classify-error (count 'a 1))
581
582
  type-error)

583
(deftest count.error.2
584
  (classify-error (count 'a 'a))
585
586
  type-error)

587
(deftest count.error.3
588
  (classify-error (count 'a #\a))
589
590
  type-error)

591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
(deftest count.error.4
  (classify-error (count))
  program-error)

(deftest count.error.5
  (classify-error (count nil))
  program-error)

(deftest count.error.6
  (classify-error (count nil nil :bad t))
  program-error)

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

(deftest count.error.8
  (classify-error (count nil nil :key))
  program-error)

(deftest count.error.9
  (classify-error (count nil nil 3 3))
  program-error)

615
616
617
618
619
620
;;; Only leftmost :allow-other-keys argument matters
(deftest count.error.10
  (classify-error (count 'a nil :bad t
			 :allow-other-keys nil
			 :allow-other-keys t))
  program-error)
621
622
623
624
625

(deftest count.error.11
  (classify-error (locally (count 'a 1) t))
  type-error)

626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
(deftest count.error.12
  (classify-error (count 'b '(a b c) :test #'identity))
  program-error)

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

(deftest count.error.14
  (classify-error (count 'b '(a b c) :test-not #'identity))
  program-error)

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