fill.lsp 15.7 KB
Newer Older
1 2 3 4 5 6 7
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Oct 12 19:44:45 2002
;;;; Contains: Tests on FILL

(in-package :cl-test)

8
(deftest fill.error.1
9 10
  (signals-error (fill 'a 'b) type-error)
  t)
pfdietz's avatar
pfdietz committed
11

12
(deftest fill.error.2
13 14
  (signals-error (fill) program-error)
  t)
15 16

(deftest fill.error.3
17 18
  (signals-error (fill (list 'a 'b)) program-error)
  t)
19 20

(deftest fill.error.4
21 22
  (signals-error (fill (list 'a 'b) 'c :bad t) program-error)
  t)
23 24

(deftest fill.error.5
25
  (signals-error (fill (list 'a 'b) 'c :bad t :allow-other-keys nil)
sds's avatar
sds committed
26
                 program-error)
27
  t)
28 29

(deftest fill.error.6
30 31
  (signals-error (fill (list 'a 'b) 'c :start) program-error)
  t)
32 33

(deftest fill.error.7
34 35
  (signals-error (fill (list 'a 'b) 'c :end) program-error)
  t)
36 37

(deftest fill.error.8
38 39
  (signals-error (fill (list 'a 'b) 'c 1 2) program-error)
  t)
40

41
(deftest fill.error.10
42
  (signals-error (fill (list 'a 'b) 'c :bad t :allow-other-keys nil
sds's avatar
sds committed
43 44
                        :allow-other-keys t)
                 program-error)
45
  t)
46

47
(deftest fill.error.11
48 49
  (signals-error (locally (fill 'a 'b) t) type-error)
  t)
50

51 52 53 54
;;; Fill on arrays

(deftest array-fill-1
  (let* ((a (make-array '(5) :initial-contents '(a b c d e)))
sds's avatar
sds committed
55
         (b (fill a 'x)))
56
    (values (eqt a b)
sds's avatar
sds committed
57
            (map 'list #'identity a)))
58 59 60 61
  t (x x x x x))

(deftest array-fill-2
  (let* ((a (make-array '(5) :initial-contents '(a b c d e)))
sds's avatar
sds committed
62
         (b (fill a 'x :start 2)))
63
    (values (eqt a b)
sds's avatar
sds committed
64
            (map 'list #'identity a)))
65 66 67 68
  t (a b x x x))

(deftest array-fill-3
  (let* ((a (make-array '(5) :initial-contents '(a b c d e)))
sds's avatar
sds committed
69
         (b (fill a 'x :end 2)))
70
    (values (eqt a b)
sds's avatar
sds committed
71
            (map 'list #'identity a)))
72 73 74 75
  t (x x c d e))

(deftest array-fill-4
  (let* ((a (make-array '(5) :initial-contents '(a b c d e)))
sds's avatar
sds committed
76
         (b (fill a 'x :start 1 :end 3)))
77
    (values (eqt a b)
sds's avatar
sds committed
78
            (map 'list #'identity a)))
79 80 81 82
  t (a x x d e))

(deftest array-fill-5
  (let* ((a (make-array '(5) :initial-contents '(a b c d e)))
sds's avatar
sds committed
83
         (b (fill a 'x :start 1 :end nil)))
84
    (values (eqt a b)
sds's avatar
sds committed
85
            (map 'list #'identity a)))
86 87 88 89
  t (a x x x x))

(deftest array-fill-6
  (let* ((a (make-array '(5) :initial-contents '(a b c d e)))
sds's avatar
sds committed
90
         (b (fill a 'x :end nil)))
91
    (values (eqt a b)
sds's avatar
sds committed
92
            (map 'list #'identity a)))
93 94 95
  t (x x x x x))

(deftest array-fill-7
96
  (signals-error
97
   (let* ((a (make-array '(5))))
98 99 100
     (fill a 'x :start -1))
   type-error)
  t)
101 102

(deftest array-fill-8
103
  (signals-error
104
   (let* ((a (make-array '(5))))
105 106 107
     (fill a 'x :start 'a))
   type-error)
  t)
108 109

(deftest array-fill-9
110
  (signals-error
111
   (let* ((a (make-array '(5))))
112 113 114
     (fill a 'x :end -1))
   type-error)
  t)
115 116

(deftest array-fill-10
117
  (signals-error
118
   (let* ((a (make-array '(5))))
119 120 121
     (fill a 'x :end 'a))
   type-error)
  t)
122 123 124 125 126

;;; fill on arrays of fixnums

(deftest array-fixnum-fill-1
  (let* ((a (make-array '(5) :element-type 'fixnum :initial-contents '(1 2 3 4 5)))
sds's avatar
sds committed
127
         (b (fill a 6)))
128
    (values (eqt a b)
sds's avatar
sds committed
129
            (map 'list #'identity a)))
130 131 132 133
  t (6 6 6 6 6))

(deftest array-fixnum-fill-2
  (let* ((a (make-array '(5) :element-type 'fixnum :initial-contents '(1 2 3 4 5)))
sds's avatar
sds committed
134
         (b (fill a 6 :start 2)))
135
    (values (eqt a b)
sds's avatar
sds committed
136
            (map 'list #'identity a)))
137 138 139 140
  t (1 2 6 6 6))

(deftest array-fixnum-fill-3
  (let* ((a (make-array '(5) :element-type 'fixnum :initial-contents '(1 2 3 4 5)))
sds's avatar
sds committed
141
         (b (fill a 7 :end 2)))
142
    (values (eqt a b)
sds's avatar
sds committed
143
            (map 'list #'identity a)))
144 145 146 147
  t (7 7 3 4 5))

(deftest array-fixnum-fill-4
  (let* ((a (make-array '(5) :element-type 'fixnum :initial-contents '(1 2 3 4 5)))
sds's avatar
sds committed
148
         (b (fill a 8 :start 1 :end 3)))
149
    (values (eqt a b)
sds's avatar
sds committed
150
            (map 'list #'identity a)))
151 152 153 154
  t (1 8 8 4 5))

(deftest array-fixnum-fill-5
  (let* ((a (make-array '(5) :element-type 'fixnum :initial-contents '(1 2 3 4 5)))
sds's avatar
sds committed
155
         (b (fill a 0 :start 1 :end nil)))
156
    (values (eqt a b)
sds's avatar
sds committed
157
            (map 'list #'identity a)))
158 159 160 161
  t (1 0 0 0 0))

(deftest array-fixnum-fill-6
  (let* ((a (make-array '(5) :element-type 'fixnum :initial-contents '(1 2 3 4 5)))
sds's avatar
sds committed
162
         (b (fill a -1 :end nil)))
163
    (values (eqt a b)
sds's avatar
sds committed
164
            (map 'list #'identity a)))
165 166 167
  t (-1 -1 -1 -1 -1))

(deftest array-fixnum-fill-7
168
  (signals-error
169
   (let* ((a (make-array '(5) :element-type 'fixnum)))
170 171 172
     (fill a 10 :start -1))
   type-error)
  t)
173 174

(deftest array-fixnum-fill-8
175
  (signals-error
176
   (let* ((a (make-array '(5) :element-type 'fixnum)))
177 178 179
     (fill a 100 :start 'a))
   type-error)
  t)
180 181

(deftest array-fixnum-fill-9
182
  (signals-error
183
   (let* ((a (make-array '(5) :element-type 'fixnum)))
184 185 186
     (fill a -5 :end -1))
   type-error)
  t)
187 188

(deftest array-fixnum-fill-10
189
  (signals-error
190
   (let* ((a (make-array '(5) :element-type 'fixnum)))
191 192 193
     (fill a 17 :end 'a))
   type-error)
  t)
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

;;; fill on arrays of unsigned eight bit bytes

(deftest array-unsigned-byte8-fill-1
  (array-unsigned-byte-fill-test-fn 8 6)
  t (6 6 6 6 6))

(deftest array-unsigned-byte8-fill-2
  (array-unsigned-byte-fill-test-fn 8 6 :start 2)
  t (1 2 6 6 6))

(deftest array-unsigned-byte8-fill-3
  (array-unsigned-byte-fill-test-fn 8 7 :end 2)
  t (7 7 3 4 5))

(deftest array-unsigned-byte8-fill-4
  (array-unsigned-byte-fill-test-fn 8 8 :start 1 :end 3)
  t (1 8 8 4 5))

(deftest array-unsigned-byte8-fill-5
  (array-unsigned-byte-fill-test-fn 8 9 :start 1 :end nil)
  t (1 9 9 9 9))

(deftest array-unsigned-byte8-fill-6
  (array-unsigned-byte-fill-test-fn 8 0 :end nil)
  t (0 0 0 0 0))

(deftest array-unsigned-byte8-fill-7
222
  (signals-error (array-unsigned-byte-fill-test-fn 8 0 :start -1)
sds's avatar
sds committed
223
                 type-error)
224
  t)
225 226

(deftest array-unsigned-byte8-fill-8
227
  (signals-error (array-unsigned-byte-fill-test-fn 8 100 :start 'a)
sds's avatar
sds committed
228
                 type-error)
229
  t)
230 231

(deftest array-unsigned-byte8-fill-9
232
  (signals-error (array-unsigned-byte-fill-test-fn 8 19 :end -1)
sds's avatar
sds committed
233
                 type-error)
234
  t)
235 236

(deftest array-unsigned-byte8-fill-10
237
  (signals-error (array-unsigned-byte-fill-test-fn 8 17 :end 'a)
sds's avatar
sds committed
238
                 type-error)
239
  t)
240

241
;;; Tests on arrays with fill pointers
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
(deftest array-fill-pointer-fill.1
  (let ((s1 (make-array '(10) :fill-pointer 5 :initial-element nil)))
    (fill s1 'a)
    (loop for i from 0 to 9 collect (aref s1 i)))
  (a a a a a nil nil nil nil nil))

(deftest array-fill-pointer-fill.2
  (let ((s1 (make-array '(10) :fill-pointer 5 :initial-element nil)))
    (fill s1 'a :end nil)
    (loop for i from 0 to 9 collect (aref s1 i)))
  (a a a a a nil nil nil nil nil))

;;; Tests on strings

(deftest fill.string.1
  (let* ((s1 (copy-seq "abcde"))
sds's avatar
sds committed
259
         (s2 (fill s1 #\z)))
260 261 262 263 264 265
    (values (eqt s1 s2) s2))
  t
  "zzzzz")

(deftest fill.string.2
  (let* ((s1 (copy-seq "abcde"))
sds's avatar
sds committed
266
         (s2 (fill s1 #\z :start 0 :end 1)))
267 268 269 270 271 272
    (values (eqt s1 s2) s2))
  t
  "zbcde")

(deftest fill.string.3
  (let* ((s1 (copy-seq "abcde"))
sds's avatar
sds committed
273
         (s2 (fill s1 #\z :end 2)))
274 275 276 277 278 279
    (values (eqt s1 s2) s2))
  t
  "zzcde")

(deftest fill.string.4
  (let* ((s1 (copy-seq "abcde"))
sds's avatar
sds committed
280
         (s2 (fill s1 #\z :end nil)))
281 282 283 284
    (values (eqt s1 s2) s2))
  t
  "zzzzz")

285 286
(deftest fill.string.5
  (let* ((s1 "aaaaaaaa")
sds's avatar
sds committed
287
         (len (length s1)))
288
    (loop for start from 0 to (1- len)
sds's avatar
sds committed
289 290 291 292 293 294 295 296 297 298
          always
          (loop for end from (1+ start) to len
                always
                (let* ((s2 (copy-seq s1))
                       (s3 (fill s2 #\z :start start :end end)))
                  (and (eqt s2 s3)
                       (string= s3
                                (substitute-if #\z (constantly t) s1
                                               :start start :end end))
                       t)))))
299 300 301 302
  t)

(deftest fill.string.6
  (let* ((s1 "aaaaaaaa")
sds's avatar
sds committed
303
         (len (length s1)))
304
    (loop for start from 0 to (1- len)
sds's avatar
sds committed
305 306 307 308 309 310 311 312
          always
          (let* ((s2 (copy-seq s1))
                 (s3 (fill s2 #\z :start start)))
            (and (eqt s2 s3)
                 (string= s3
                          (substitute-if #\z (constantly t) s1
                                         :start start))
                 t))))
313 314 315 316
  t)

(deftest fill.string.7
  (let* ((s1 "aaaaaaaa")
sds's avatar
sds committed
317
         (len (length s1)))
318
    (loop for start from 0 to (1- len)
sds's avatar
sds committed
319 320 321 322 323 324 325 326
          always
          (let* ((s2 (copy-seq s1))
                 (s3 (fill s2 #\z :end nil :start start)))
            (and (eqt s2 s3)
                 (string= s3
                          (substitute-if #\z (constantly t) s1
                                         :end nil :start start))
                 t))))
327 328 329 330
  t)

(deftest fill.string.8
  (let* ((s1 "aaaaaaaa")
sds's avatar
sds committed
331
         (len (length s1)))
332
    (loop for end from 1 to len
sds's avatar
sds committed
333 334 335 336 337 338 339 340
          always
          (let* ((s2 (copy-seq s1))
                 (s3 (fill s2 #\z :end end)))
            (and (eqt s2 s3)
                 (string= s3
                          (substitute-if #\z (constantly t) s1
                                         :end end))
                 t))))
341 342 343 344
  t)

(deftest fill.string.9
  (let* ((s1 (make-array '(8) :element-type 'character
sds's avatar
sds committed
345 346 347
                         :initial-element #\z
                         :fill-pointer 4))
         (s2 (fill s1 #\a)))
348
    (and (eqt s1 s2)
sds's avatar
sds committed
349 350
         (coerce (loop for i from 0 to 7 collect (aref s2 i))
                 'string)))
351 352 353 354
  "aaaazzzz")

(deftest fill.string.10
  (let* ((s1 (make-array '(8) :element-type 'base-char
sds's avatar
sds committed
355 356 357
                         :initial-element #\z
                         :fill-pointer 4))
         (s2 (fill s1 #\a)))
358
    (and (eqt s1 s2)
sds's avatar
sds committed
359 360
         (coerce (loop for i from 0 to 7 collect (aref s2 i))
                 'base-string)))
361 362 363 364 365 366
  "aaaazzzz")

;;; Tests for bit vectors

(deftest fill.bit-vector.1
  (let* ((s1 (copy-seq #*01100))
sds's avatar
sds committed
367
         (s2 (fill s1 0)))
368 369 370
    (values (eqt s1 s2) s2))
  t
  #*00000)
371

372 373
(deftest fill.bit-vector.2
  (let* ((s1 (copy-seq #*00100))
sds's avatar
sds committed
374
         (s2 (fill s1 1 :start 0 :end 1)))
375 376 377
    (values (eqt s1 s2) s2))
  t
  #*10100)
378

379 380
(deftest fill.bit-vector.3
  (let* ((s1 (copy-seq #*00010))
sds's avatar
sds committed
381
         (s2 (fill s1 1 :end 2)))
382 383 384
    (values (eqt s1 s2) s2))
  t
  #*11010)
385

386 387
(deftest fill.bit-vector.4
  (let* ((s1 (copy-seq #*00111))
sds's avatar
sds committed
388
         (s2 (fill s1 0 :end nil)))
389 390 391 392 393 394
    (values (eqt s1 s2) s2))
  t
  #*00000)

(deftest fill.bit-vector.5
  (let* ((s1 #*00000000)
sds's avatar
sds committed
395
         (len (length s1)))
396
    (loop for start from 0 to (1- len)
sds's avatar
sds committed
397 398 399 400 401 402 403 404 405 406
          always
          (loop for end from (1+ start) to len
                always
                (let* ((s2 (copy-seq s1))
                       (s3 (fill s2 1 :start start :end end)))
                  (and (eqt s2 s3)
                       (equalp s3
                               (substitute-if 1 (constantly t) s1
                                              :start start :end end))
                       t)))))
407 408 409 410
  t)

(deftest fill.bit-vector.6
  (let* ((s1 #*11111111)
sds's avatar
sds committed
411
         (len (length s1)))
412
    (loop for start from 0 to (1- len)
sds's avatar
sds committed
413 414 415 416 417 418 419 420
          always
          (let* ((s2 (copy-seq s1))
                 (s3 (fill s2 0 :start start)))
            (and (eqt s2 s3)
                 (equalp s3
                         (substitute-if 0 (constantly t) s1
                                        :start start))
                 t))))
421 422 423 424
  t)

(deftest fill.bit-vector.7
  (let* ((s1 #*00000000)
sds's avatar
sds committed
425
         (len (length s1)))
426
    (loop for start from 0 to (1- len)
sds's avatar
sds committed
427 428 429 430 431 432 433 434
          always
          (let* ((s2 (copy-seq s1))
                 (s3 (fill s2 1 :end nil :start start)))
            (and (eqt s2 s3)
                 (equalp s3
                         (substitute-if 1 (constantly t) s1
                                        :end nil :start start))
                 t))))
435 436 437 438
  t)

(deftest fill.bit-vector.8
  (let* ((s1 #*11111111)
sds's avatar
sds committed
439
         (len (length s1)))
440
    (loop for end from 1 to len
sds's avatar
sds committed
441 442 443 444 445 446 447 448
          always
          (let* ((s2 (copy-seq s1))
                 (s3 (fill s2 0 :end end)))
            (and (eqt s2 s3)
                 (equalp s3
                         (substitute-if 0 (constantly t) s1
                                        :end end))
                 t))))
449 450 451 452
  t)

(deftest fill.bit-vector.9
  (let* ((s1 (make-array '(8) :element-type 'bit
sds's avatar
sds committed
453 454 455
                         :initial-element 0
                         :fill-pointer 4))
         (s2 (fill s1 1)))
456
    (and (eqt s1 s2)
sds's avatar
sds committed
457 458
         (coerce (loop for i from 0 to 7 collect (aref s2 i))
                 'bit-vector)))
459
  #*11110000)
460

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
;;; Test of :allow-other-keys

(deftest fill.allow-other-keys.1
  (fill (list 'a 'b 'c 'd 'e) 'a :allow-other-keys t)
  (a a a a a))

(deftest fill.allow-other-keys.2
  (fill (list 'a 'b 'c 'd 'e) 'a :allow-other-keys nil)
  (a a a a a))

(deftest fill.allow-other-keys.3
  (fill (list 'a 'b 'c 'd 'e) 'a :allow-other-keys t :bad t)
  (a a a a a))

(deftest fill.allow-other-keys.4
  (fill (list 'a 'b 'c 'd 'e) 'a :bad t :allow-other-keys t)
  (a a a a a))

(deftest fill.allow-other-keys.5
  (fill (list 'a 'b 'c 'd 'e) 'a 'bad t :allow-other-keys t)
  (a a a a a))

(deftest fill.allow-other-keys.6
  (fill (list 'a 'b 'c 'd 'e) 'a  :bad t :allow-other-keys t
sds's avatar
sds committed
485
        :allow-other-keys nil)
486 487 488 489
  (a a a a a))

(deftest fill.allow-other-keys.7
  (fill (list 'a 'b 'c 'd 'e) 'a  :allow-other-keys t :allow-other-keys nil
sds's avatar
sds committed
490
        :bad t)
491 492 493
  (a a a a a))


494 495 496 497 498 499
;;; Tests of evaluation order

(deftest fill.order.1
  (let ((i 0) x y (a (copy-seq #(a a a a))))
    (values
     (fill (progn (setf x (incf i)) a)
sds's avatar
sds committed
500
           (progn (setf y (incf i)) 'z))
501 502 503 504 505 506 507
     i x y))
  #(z z z z) 2 1 2)

(deftest fill.order.2
  (let ((i 0) x y z w (a (copy-seq #(a a a a))))
    (values
     (fill (progn (setf x (incf i)) a)
sds's avatar
sds committed
508 509 510
           (progn (setf y (incf i)) 'z)
           :start (progn (setf z (incf i)) 1)
           :end   (progn (setf w (incf i)) 3))
511 512 513 514 515 516 517
     i x y z w))
  #(a z z a) 4 1 2 3 4)

(deftest fill.order.3
  (let ((i 0) x y z w (a (copy-seq #(a a a a))))
    (values
     (fill (progn (setf x (incf i)) a)
sds's avatar
sds committed
518 519 520
           (progn (setf y (incf i)) 'z)
           :end   (progn (setf z (incf i)) 3)
           :start (progn (setf w (incf i)) 1))
521 522 523 524 525 526 527
     i x y z w))
  #(a z z a) 4 1 2 3 4)

(deftest fill.order.4
  (let ((i 0) x y z p q r s w (a (copy-seq #(a a a a))))
    (values
     (fill (progn (setf x (incf i)) a)
sds's avatar
sds committed
528 529 530 531 532 533 534
           (progn (setf y (incf i)) 'z)
           :end   (progn (setf z (incf i)) 3)
           :end   (progn (setf p (incf i)) 1)
           :end   (progn (setf q (incf i)) 1)
           :end   (progn (setf r (incf i)) 1)
           :start (progn (setf s (incf i)) 1)
           :start (progn (setf w (incf i)) 0))
535 536
     i x y z p q r s w))
  #(a z z a) 8 1 2 3 4 5 6 7 8)
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

;;; Specialized strings

(deftest fill.specialized-strings.1
  (do-special-strings
   (s (copy-seq "abcde") nil)
   (assert (string= s "abcde"))
   (assert (eq s (fill s #\x)))
   (assert (string= s "xxxxx")))
  nil)

(deftest fill.specialized-strings.2
  (do-special-strings
   (s (copy-seq "abcde") nil)
   (assert (string= s "abcde"))
   (assert (eq s (fill s #\x  :start 2)))
   (assert (string= s "abxxx")))
  nil)

(deftest fill.specialized-strings.3
  (do-special-strings
   (s (copy-seq "abcde") nil)
   (assert (string= s "abcde"))
   (assert (eq s (fill s #\x  :end 3)))
   (assert (string= s "xxxde")))
  nil)

(deftest fill.specialized-strings.4
  (do-special-strings
   (s (copy-seq "abcde") nil)
   (assert (string= s "abcde"))
   (assert (eq s (fill s #\x  :start 1 :end 4)))
   (assert (string= s "axxxe")))
  nil)
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

;;; Specialized vector tests

(deftest fill.specialized-vectors.1
  (do-special-integer-vectors
   (v #(0 1 1 0 1) nil)
   (let ((etype (array-element-type v)))
     (assert (eq v (fill v 0)))
     (assert (equal (array-element-type v) etype)))
   (assert (equalp v #(0 0 0 0 0))))
  nil)

(deftest fill.specialized-vectors.2
  (do-special-integer-vectors
   (v #(0 -1 1 0 -1) nil)
   (let ((etype (array-element-type v)))
     (assert (eq v (fill v 1)))
     (assert (equal (array-element-type v) etype)))
   (assert (equalp v #(1 1 1 1 1))))
  nil)

(deftest fill.specialized-vectors.3
  (do-special-integer-vectors
   (v #(1 1 1 1 0) nil)
   (let ((etype (array-element-type v)))
     (assert (eq v (fill v 0 :start 1 :end 3)))
     (assert (equal (array-element-type v) etype)))
   (assert (equalp v #(1 0 0 1 0))))
sds's avatar
sds committed
599
  nil)