reduce.lsp 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Aug 18 14:08:57 2002
;;;; Contains: Tests for function REDUCE

(in-package :cl-test)

(deftest reduce-list.1
  (reduce #'cons '(a b c d e f))
  (((((a . b) . c) . d) . e) . f))

(deftest reduce-list.2
  (reduce #'cons '(a b c d e f) :from-end t)
  (a b c d e . f))

(deftest reduce-list.3
  (reduce #'cons '(a b c d e f) :initial-value 'z)
  ((((((z . a) . b) . c) . d) . e) . f))
sds's avatar
sds committed
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
(deftest reduce-list.4
  (reduce #'cons '(a b c d e f) :from-end t :initial-value 'g)
  (a b c d e f . g))

(deftest reduce-list.5
  (reduce #'cons '(a b c d e f) :from-end nil)
  (((((a . b) . c) . d) . e) . f))

(deftest reduce-list.6
  (reduce #'cons '(a b c d e f) :from-end 17)
  (a b c d e . f))

(deftest reduce-list.7
  (reduce #'cons '(a b c d e f) :end nil)
  (((((a . b) . c) . d) . e) . f))

(deftest reduce-list.8
  (reduce #'cons '(a b c d e f) :end 3)
  ((a . b) . c))

(deftest reduce-list.9
  (reduce #'cons '(a b c d e f) :start 1 :end 4)
  ((b . c) . d))

(deftest reduce-list.10
  (reduce #'cons '(a b c d e f) :start 1 :end 4 :from-end t)
  (b c . d))

(deftest reduce-list.11
  (reduce #'cons '(a b c d e f) :start 1 :end 4 :from-end t
sds's avatar
sds committed
50
          :initial-value nil)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  (b c d))

(deftest reduce-list.12
  (reduce 'cons '(a b c d e f))
  (((((a . b) . c) . d) . e) . f))

(deftest reduce-list.13
  (reduce #'+ nil)
  0)

(deftest reduce-list.14
  (reduce #'+ '(1 2 3) :start 0 :end 0)
  0)

(deftest reduce-list.15
  (reduce #'+ '(1 2 3) :key '1+)
  9)

(deftest reduce-list.16
  (reduce #'cons '(1 2 3) :key '1+ :from-end t :initial-value nil)
  (2 3 4))

(deftest reduce-list.17
  (reduce #'+ '(1 2 3 4 5 6 7) :key '1+ :start 2 :end 6)
  22)

;;;;;;;

(deftest reduce-array.1
  (reduce #'cons #(a b c d e f))
  (((((a . b) . c) . d) . e) . f))

(deftest reduce-array.2
  (reduce #'cons #(a b c d e f) :from-end t)
  (a b c d e . f))

(deftest reduce-array.3
  (reduce #'cons #(a b c d e f) :initial-value 'z)
  ((((((z . a) . b) . c) . d) . e) . f))
sds's avatar
sds committed
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
(deftest reduce-array.4
  (reduce #'cons #(a b c d e f) :from-end t :initial-value 'g)
  (a b c d e f . g))

(deftest reduce-array.5
  (reduce #'cons #(a b c d e f) :from-end nil)
  (((((a . b) . c) . d) . e) . f))

(deftest reduce-array.6
  (reduce #'cons #(a b c d e f) :from-end 17)
  (a b c d e . f))

(deftest reduce-array.7
  (reduce #'cons #(a b c d e f) :end nil)
  (((((a . b) . c) . d) . e) . f))

(deftest reduce-array.8
  (reduce #'cons #(a b c d e f) :end 3)
  ((a . b) . c))

(deftest reduce-array.9
  (reduce #'cons #(a b c d e f) :start 1 :end 4)
  ((b . c) . d))

(deftest reduce-array.10
  (reduce #'cons #(a b c d e f) :start 1 :end 4 :from-end t)
  (b c . d))

(deftest reduce-array.11
  (reduce #'cons #(a b c d e f) :start 1 :end 4 :from-end t
sds's avatar
sds committed
121
          :initial-value nil)
122 123 124 125 126 127 128 129 130 131
  (b c d))

(deftest reduce-array.12
  (reduce 'cons #(a b c d e f))
  (((((a . b) . c) . d) . e) . f))

(deftest reduce-array.13
  (reduce #'+ #(1 2 3) :start 0 :end 0)
  0)

132 133
(deftest reduce-array.14
  (let ((a (make-array '(8) :initial-contents '(1 2 3 4 5 6 7 8)
sds's avatar
sds committed
134
                       :fill-pointer 4)))
135 136 137 138 139
    (reduce #'+ a))
  10)

(deftest reduce-array.15
  (let ((a (make-array '(8) :initial-contents '(1 2 3 4 5 6 7 8)
sds's avatar
sds committed
140
                       :fill-pointer 4)))
141 142 143 144 145
    (reduce #'+ a :end nil))
  10)

(deftest reduce-array.16
  (let ((a (make-array '(8) :initial-contents '(1 2 3 4 5 6 7 8)
sds's avatar
sds committed
146
                       :fill-pointer 4)))
147 148 149 150 151
    (reduce #'+ a :from-end t))
  10)

(deftest reduce-array.17
  (let ((a (make-array '(8) :initial-contents '(1 2 3 4 5 6 7 8)
sds's avatar
sds committed
152
                       :fill-pointer 4)))
153 154 155 156 157
    (reduce #'+ a :initial-value 1))
  11)

(deftest reduce-array.18
  (let ((a (make-array '(8) :initial-contents '(1 2 3 4 5 6 7 8)
sds's avatar
sds committed
158
                       :fill-pointer 4)))
159 160 161 162 163
    (reduce #'+ a :initial-value 1 :start 2))
  8)

(deftest reduce-array.19
  (let ((a (make-array '(8) :initial-contents '(1 2 3 4 5 6 7 8)
sds's avatar
sds committed
164
                       :fill-pointer 4)))
165 166 167
    (reduce #'+ a :end 3))
  6)

168 169 170 171 172 173 174 175 176 177 178 179
;;; Specialized vectors

(deftest reduce-array.20
  (do-special-integer-vectors
   (v #(1 0 0 1 1 0) nil)
   (assert (eql (reduce #'+ v) 3)))
  nil)

(deftest reduce-array.21
  (do-special-integer-vectors
   (v #(1 0 0 1 1 0) nil)
   (assert (equal (reduce #'cons v :from-end t :initial-value nil)
sds's avatar
sds committed
180
                  '(1 0 0 1 1 0))))
181 182 183 184 185 186 187 188 189 190 191 192 193 194
  nil)

(deftest reduce-array.22
  (do-special-integer-vectors
   (v #(1 2 3 4 5 6 7) nil)
   (assert (eql (reduce #'+ v) 28))
   (assert (eql (reduce #'+ v :from-end t) 28))
   (assert (eql (reduce #'+ v :start 1) 27))
   (assert (eql (reduce #'+ v :initial-value 10) 38))
   (assert (eql (reduce #'+ v :end 6) 21)))
  nil)

(deftest reduce-array.23
  (let* ((len 10)
sds's avatar
sds committed
195
         (expected (* 1/2 (1+ len) len)))
196
    (loop for etype in '(short-float single-float double-float long-float)
sds's avatar
sds committed
197 198 199 200 201
          for vals = (loop for i from 1 to len collect (coerce i etype))
          for vec = (make-array len :initial-contents vals :element-type etype)
          for result = (reduce #'+ vec)
          unless (= result (coerce expected etype))
          collect (list etype vals vec result)))
202 203 204 205
  nil)

(deftest reduce-array.24
  (let* ((len 10)
sds's avatar
sds committed
206
         (expected (* 1/2 (1+ len) len)))
207
    (loop for cetype in '(short-float single-float double-float long-float)
sds's avatar
sds committed
208 209 210 211 212 213 214
          for etype = `(complex ,cetype)
          for vals = (loop for i from 1 to len collect (complex (coerce i cetype)
                                                                (coerce (- i) cetype)))
          for vec = (make-array len :initial-contents vals :element-type etype)
          for result = (reduce #'+ vec)
          unless (= result (complex (coerce expected cetype) (coerce (- expected) cetype)))
          collect (list etype vals vec result)))
215 216 217 218 219 220 221
  nil)

(deftest reduce-array.25
  (do-special-integer-vectors
   (v (vector 0 most-positive-fixnum 0 most-positive-fixnum 0) nil)
   (assert (eql (reduce #'+ v) (* 2 most-positive-fixnum))))
  nil)
222

223 224
;;;;;;;;

225
(deftest reduce.error.1
226 227
  (check-type-error #'(lambda (x) (reduce 'cons x)) #'sequencep)
  nil)
228

229
(deftest reduce.error.2
230 231
  (signals-error (reduce) program-error)
  t)
232 233

(deftest reduce.error.3
234 235
  (signals-error (reduce #'list nil :start) program-error)
  t)
236 237

(deftest reduce.error.4
238 239
  (signals-error (reduce #'list nil 'bad t) program-error)
  t)
240 241

(deftest reduce.error.5
242 243
  (signals-error (reduce #'list nil 'bad t :allow-other-keys nil) program-error)
  t)
244 245

(deftest reduce.error.6
246 247
  (signals-error (reduce #'list nil 1 2) program-error)
  t)
248

249
(deftest reduce.error.7
250 251
  (signals-error (locally (reduce 'cons 'a) t) type-error)
  t)
252

253
(deftest reduce.error.8
254 255
  (signals-error (reduce #'identity '(a b c)) program-error)
  t)
256 257

(deftest reduce.error.9
258 259
  (signals-error (reduce #'cons '(a b c) :key #'cons) program-error)
  t)
260 261

(deftest reduce.error.10
262 263
  (signals-error (reduce #'cons '(a b c) :key #'car) type-error)
  t)
264 265


266 267 268 269 270 271 272 273 274 275 276 277 278
;;;;;;;;

(deftest reduce-string.1
  (reduce #'cons "abcdef")
  (((((#\a . #\b) . #\c) . #\d) . #\e) . #\f))

(deftest reduce-string.2
  (reduce #'cons "abcdef" :from-end t)
  (#\a #\b #\c #\d #\e . #\f))

(deftest reduce-string.3
  (reduce #'cons "abcdef" :initial-value 'z)
  ((((((z . #\a) . #\b) . #\c) . #\d) . #\e) . #\f))
sds's avatar
sds committed
279

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
(deftest reduce-string.4
  (reduce #'cons "abcdef" :from-end t :initial-value 'g)
  (#\a #\b #\c #\d #\e #\f . g))

(deftest reduce-string.5
  (reduce #'cons "abcdef" :from-end nil)
   (((((#\a . #\b) . #\c) . #\d) . #\e) . #\f))

(deftest reduce-string.6
  (reduce #'cons "abcdef" :from-end 17)
   (#\a #\b #\c #\d #\e . #\f))

(deftest reduce-string.7
  (reduce #'cons "abcdef" :end nil)
  (((((#\a . #\b) . #\c) . #\d) . #\e) . #\f))

(deftest reduce-string.8
  (reduce #'cons "abcdef" :end 3)
  ((#\a . #\b) . #\c))

(deftest reduce-string.9
  (reduce #'cons "abcdef" :start 1 :end 4)
  ((#\b . #\c) . #\d))

(deftest reduce-string.10
  (reduce #'cons "abcdef" :start 1 :end 4 :from-end t)
  (#\b #\c . #\d))

(deftest reduce-string.11
  (reduce #'cons "abcdef" :start 1 :end 4 :from-end t
sds's avatar
sds committed
310
          :initial-value nil)
311 312 313 314 315 316 317 318 319 320
  (#\b #\c #\d))

(deftest reduce-string.12
  (reduce 'cons "abcdef")
  (((((#\a . #\b) . #\c) . #\d) . #\e) . #\f))

(deftest reduce-string.13
  (reduce #'+ "abc" :start 0 :end 0)
  0)

321
(deftest reduce-string.14
322
  (let ((s (make-array '(8) :initial-contents "abcdefgh"
sds's avatar
sds committed
323 324
                       :fill-pointer 6
                       :element-type 'character)))
325
    (coerce (reduce #'(lambda (x y) (cons y x)) s :initial-value nil)
sds's avatar
sds committed
326
            'string))
327 328 329
  "fedcba")

(deftest reduce-string.15
330
  (let ((s (make-array '(8) :initial-contents "abcdefgh"
sds's avatar
sds committed
331 332
                       :fill-pointer 6
                       :element-type 'character)))
333
    (coerce (reduce #'(lambda (x y) (cons y x)) s :initial-value nil
sds's avatar
sds committed
334 335
                    :start 1)
            'string))
336 337 338
  "fedcb")

(deftest reduce-string.16
339
  (let ((s (make-array '(8) :initial-contents "abcdefgh"
sds's avatar
sds committed
340 341 342 343 344
                       :fill-pointer 6
                       :element-type 'character)))
    (coerce (reduce #'(lambda (x y) (cons y x)) s :end nil
                    :initial-value nil)
            'string))
345 346 347
  "fedcba")

(deftest reduce-string.17
348
  (let ((s (make-array '(8) :initial-contents "abcdefgh"
sds's avatar
sds committed
349 350 351 352 353
                       :fill-pointer 6
                       :element-type 'character)))
    (coerce (reduce #'(lambda (x y) (cons y x)) s :end 4
                    :initial-value nil)
            'string))
354 355
  "dcba")

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
(deftest reduce-string.18
  (do-special-strings
   (s "12345" nil)
   (let ((x (reduce #'(lambda (x y) (cons y x)) s)))
     (assert (equal x '(#\5 #\4 #\3 #\2 . #\1)))))
  nil)

(deftest reduce-string.19
  (do-special-strings
   (s "54321" nil)
   (let ((x (reduce #'cons s :from-end t)))
     (assert (equal x '(#\5 #\4 #\3 #\2 . #\1)))))
  nil)

(deftest reduce-string.20
  (do-special-strings
   (s "12345" nil)
   (let ((x (reduce #'(lambda (x y) (cons y x)) s :initial-value nil)))
     (assert (equal x '(#\5 #\4 #\3 #\2 #\1)))))
  nil)

377 378 379 380 381 382 383 384 385 386 387 388 389
;;;;;;;;

(deftest reduce-bitstring.1
  (reduce #'cons #*001101)
  (((((0 . 0) . 1) . 1) . 0) . 1))

(deftest reduce-bitstring.2
  (reduce #'cons #*001101 :from-end t)
  (0 0 1 1 0 . 1))

(deftest reduce-bitstring.3
  (reduce #'cons #*001101 :initial-value 'z)
  ((((((z . 0) . 0) . 1) . 1) . 0) . 1))
sds's avatar
sds committed
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
(deftest reduce-bitstring.4
  (reduce #'cons #*001101 :from-end t :initial-value 'g)
  (0 0 1 1 0 1 . g))

(deftest reduce-bitstring.5
  (reduce #'cons #*001101 :from-end nil)
  (((((0 . 0) . 1) . 1) . 0) . 1))

(deftest reduce-bitstring.6
  (reduce #'cons #*001101 :from-end 17)
  (0 0 1 1 0 . 1))

(deftest reduce-bitstring.7
  (reduce #'cons #*001101 :end nil)
  (((((0 . 0) . 1) . 1) . 0) . 1))

(deftest reduce-bitstring.8
  (reduce #'cons #*001101 :end 3)
  ((0 . 0) . 1))

(deftest reduce-bitstring.9
  (reduce #'cons #*001101 :start 1 :end 4)
  ((0 . 1) . 1))

(deftest reduce-bitstring.10
  (reduce #'cons #*001101 :start 1 :end 4 :from-end t)
  (0 1 . 1))

(deftest reduce-bitstring.11
  (reduce #'cons #*001101 :start 1 :end 4 :from-end t
sds's avatar
sds committed
421
          :initial-value nil)
422 423 424 425 426 427 428 429 430
  (0 1 1))

(deftest reduce-bitstring.12
  (reduce 'cons #*001101)
  (((((0 . 0) . 1) . 1) . 0) . 1))

(deftest reduce-bitstring.13
  (reduce #'+ #(1 1 1) :start 0 :end 0)
  0)
431 432 433

(deftest reduce-bitstring.14
  (let ((s (make-array '(8) :initial-contents '(0 0 1 0 1 1 0 1)
sds's avatar
sds committed
434 435
                       :fill-pointer 6
                       :element-type 'bit)))
436 437 438 439 440
    (reduce #'+ s))
  3)

(deftest reduce-bitstring.15
  (let ((s (make-array '(8) :initial-contents '(0 0 1 0 1 1 0 1)
sds's avatar
sds committed
441 442
                       :fill-pointer 6
                       :element-type 'bit)))
443 444 445 446 447
    (reduce #'+ s :start 3))
  2)

(deftest reduce-bitstring.16
  (let ((s (make-array '(8) :initial-contents '(0 0 1 0 1 1 0 1)
sds's avatar
sds committed
448 449
                       :fill-pointer 6
                       :element-type 'bit)))
450 451 452 453 454
    (reduce #'+ s :start 3 :initial-value 10))
  12)

(deftest reduce-bitstring.17
  (let ((s (make-array '(8) :initial-contents '(0 0 1 0 1 1 0 1)
sds's avatar
sds committed
455 456
                       :fill-pointer 6
                       :element-type 'bit)))
457 458 459 460 461
    (reduce #'+ s :end nil))
  3)

(deftest reduce-bitstring.18
  (let ((s (make-array '(8) :initial-contents '(1 1 1 1 1 1 1 1)
sds's avatar
sds committed
462 463
                       :fill-pointer 6
                       :element-type 'bit)))
464 465
    (reduce #'+ s :start 2 :end 4))
  2)
466

467 468 469 470 471 472
;;; Order of evaluation tests

(deftest reduce.order.1
  (let ((i 0) x y)
    (values
     (reduce (progn (setf x (incf i)) #'cons)
sds's avatar
sds committed
473
             (progn (setf y (incf i)) '(a b c)))
474 475 476 477 478 479 480
     i x y))
   ((a . b) . c) 2 1 2)

(deftest reduce.order.2
  (let ((i 0) a b c d e f g)
    (values
     (reduce (progn (setf a (incf i)) #'cons)
sds's avatar
sds committed
481 482 483 484 485 486 487
             (progn (setf b (incf i)) '(a b c d e f))
             :from-end (progn (setf c (incf i)) t)
             :initial-value (progn (setf d (incf i)) 'nil)
             :start (progn (setf e (incf i)) 1)
             :end (progn (setf f (incf i)) 4)
             :key (progn (setf g (incf i)) #'identity)
             )
488 489 490 491 492 493 494
     i a b c d e f g))
  (b c d) 7 1 2 3 4 5 6 7)

(deftest reduce.order.3
  (let ((i 0) a b c d e f g)
    (values
     (reduce (progn (setf a (incf i)) #'cons)
sds's avatar
sds committed
495 496 497 498 499 500 501
             (progn (setf b (incf i)) '(a b c d e f))
             :key (progn (setf c (incf i)) #'identity)
             :end (progn (setf d (incf i)) 4)
             :start (progn (setf e (incf i)) 1)
             :initial-value (progn (setf f (incf i)) 'nil)
             :from-end (progn (setf g (incf i)) t)
             )
502 503 504 505
     i a b c d e f g))
  (b c d) 7 1 2 3 4 5 6 7)


506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
;;; Keyword tests

(deftest reduce.allow-other-keys.1
  (reduce #'+ '(1 2 3) :allow-other-keys t)
  6)

(deftest reduce.allow-other-keys.2
  (reduce #'+ '(1 2 3) :allow-other-keys nil)
  6)

(deftest reduce.allow-other-keys.3
  (reduce #'+ '(1 2 3) :bad t :allow-other-keys t)
  6)

(deftest reduce.allow-other-keys.4
  (reduce #'+ '(1 2 3) :allow-other-keys t :bad t)
  6)

(deftest reduce.allow-other-keys.5
  (reduce #'+ '(1 2 3) :allow-other-keys t :allow-other-keys nil :bad t)
  6)

(deftest reduce.allow-other-keys.6
  (reduce #'+ '(1 2 3) :allow-other-keys t :bad t :allow-other-keys nil)
  6)

(deftest reduce.allow-other-keys.7
  (reduce #'+ '(1 2 3) :bad t :allow-other-keys t :allow-other-keys nil)
  6)

(deftest reduce.allow-other-keys.8
  (reduce #'cons '(1 2 3) :allow-other-keys t :from-end t :bad t
sds's avatar
sds committed
538
          :initial-value nil)
539 540 541 542
  (1 2 3))

(deftest reduce.keywords.9
  (reduce #'cons '(1 2 3) :from-end t :from-end nil
sds's avatar
sds committed
543
          :initial-value nil :initial-value 'a)
544
  (1 2 3))
545