tests.lisp 53 KB
Newer Older
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1 2
(in-package :cl-user)

3 4 5 6
(defpackage :alexandria-tests
  (:use :cl :alexandria #+sbcl :sb-rt #-sbcl :rtest)
  (:import-from #+sbcl :sb-rt #-sbcl :rtest
                #:*compile-tests* #:*expected-failures*))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
7

8
(in-package :alexandria-tests)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
9

10
(defun run-tests (&key ((:compiled *compile-tests*)))
11
  (do-tests))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
12

Nikodemus Siivola's avatar
Nikodemus Siivola committed
13 14 15 16
(defun hash-table-test-name (name)
  ;; Workaround for Clisp calling EQL in a hash-table FASTHASH-EQL.
  (hash-table-test (make-hash-table :test name)))

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
;;;; Arrays

(deftest copy-array.1
    (let* ((orig (vector 1 2 3))
           (copy (copy-array orig)))
      (values (eq orig copy) (equalp orig copy)))
  nil t)

(deftest copy-array.2
    (let ((orig (make-array 1024 :fill-pointer 0)))
      (vector-push-extend 1 orig)
      (vector-push-extend 2 orig)
      (vector-push-extend 3 orig)
      (let ((copy (copy-array orig)))
        (values (eq orig copy) (equalp orig copy)
                (array-has-fill-pointer-p copy)
                (eql (fill-pointer orig) (fill-pointer copy)))))
  nil t t t)

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
(deftest copy-array.3
    (let* ((orig (vector 1 2 3))
           (copy (copy-array orig)))
      (typep copy 'simple-array))
  t)

(deftest copy-array.4
   (let ((orig (make-array 21
                           :adjustable t
                           :fill-pointer 0)))
     (dotimes (n 42)
       (vector-push-extend n orig))
     (let ((copy (copy-array orig
                             :adjustable nil
                             :fill-pointer nil)))
       (typep copy 'simple-array)))
 t)

54 55 56 57
(deftest array-index.1
    (typep 0 'array-index)
  t)

58 59 60 61 62
;;;; Conditions

(deftest unwind-protect-case.1
    (let (result)
      (unwind-protect-case ()
Nikodemus Siivola's avatar
Nikodemus Siivola committed
63 64 65 66
          (random 10)
        (:normal (push :normal result))
        (:abort  (push :abort result))
        (:always (push :always result)))
67 68 69 70 71 72
      result)
  (:always :normal))

(deftest unwind-protect-case.2
    (let (result)
      (unwind-protect-case ()
Nikodemus Siivola's avatar
Nikodemus Siivola committed
73 74 75 76
          (random 10)
        (:always (push :always result))
        (:normal (push :normal result))
        (:abort  (push :abort result)))
77 78 79 80 81 82
      result)
  (:normal :always))

(deftest unwind-protect-case.3
    (let (result1 result2 result3)
      (ignore-errors
Nikodemus Siivola's avatar
Nikodemus Siivola committed
83 84 85 86 87
        (unwind-protect-case ()
            (error "FOOF!")
          (:normal (push :normal result1))
          (:abort  (push :abort result1))
          (:always (push :always result1))))
88
      (catch 'foof
Nikodemus Siivola's avatar
Nikodemus Siivola committed
89 90 91 92 93
        (unwind-protect-case ()
            (throw 'foof 42)
          (:normal (push :normal result2))
          (:abort  (push :abort result2))
          (:always (push :always result2))))
94
      (block foof
Nikodemus Siivola's avatar
Nikodemus Siivola committed
95 96 97 98 99
        (unwind-protect-case ()
            (return-from foof 42)
          (:normal (push :normal result3))
          (:abort  (push :abort result3))
          (:always (push :always result3))))
100 101 102 103 104 105 106 107
      (values result1 result2 result3))
  (:always :abort)
  (:always :abort)
  (:always :abort))

(deftest unwind-protect-case.4
    (let (result)
      (unwind-protect-case (aborted-p)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
108 109
          (random 42)
        (:always (setq result aborted-p)))
110 111 112 113 114 115
      result)
  nil)

(deftest unwind-protect-case.5
    (let (result)
      (block foof
Nikodemus Siivola's avatar
Nikodemus Siivola committed
116 117 118
        (unwind-protect-case (aborted-p)
            (return-from foof)
          (:always (setq result aborted-p))))
119 120 121
      result)
  t)

122 123 124 125 126 127 128 129 130
;;;; Control flow

(deftest switch.1
    (switch (13 :test =)
      (12 :oops)
      (13.0 :yay))
  :yay)

(deftest switch.2
131
    (switch (13)
132
      ((+ 12 2) :oops)
133 134
      ((- 13 1) :oops2)
      (t :yay))
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  :yay)

(deftest eswitch.1
    (let ((x 13))
      (eswitch (x :test =)
        (12 :oops)
        (13.0 :yay)))
  :yay)

(deftest eswitch.2
    (let ((x 13))
      (eswitch (x :key 1+)
        (11 :oops)
        (14 :yay)))
  :yay)

(deftest cswitch.1
    (cswitch (13 :test =)
      (12 :oops)
      (13.0 :yay))
  :yay)

(deftest cswitch.2
    (cswitch (13 :key 1-)
      (12 :yay)
      (13.0 :oops))
  :yay)

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
(deftest multiple-value-prog2.1
    (multiple-value-prog2
        (values 1 1 1)
        (values 2 20 200)
      (values 3 3 3))
  2 20 200)

(deftest nth-value-or.1
    (multiple-value-bind (a b c)
        (nth-value-or 1
                      (values 1 nil 1)
                      (values 2 2 2))
      (= a b c 2))
  t)

178 179 180 181 182
(deftest whichever.1
    (let ((x (whichever 1 2 3)))
      (and (member x '(1 2 3)) t))
  t)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
183 184 185 186 187 188 189 190
(deftest whichever.2
    (let* ((a 1)
           (b 2)
           (c 3)
           (x (whichever a b c)))
      (and (member x '(1 2 3)) t))
  t)

191 192 193 194 195
(deftest xor.1
    (xor nil nil 1 nil)
  1
  t)

196 197 198 199 200 201 202 203 204 205
(deftest xor.2
    (xor nil nil 1 2)
  nil
  nil)

(deftest xor.3
    (xor nil nil nil)
  nil
  t)

206 207 208 209
;;;; Definitions

(deftest define-constant.1
    (let ((name (gensym)))
210 211
      (eval `(define-constant ,name "FOO" :test 'equal))
      (eval `(define-constant ,name "FOO" :test 'equal))
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
      (values (equal "FOO" (symbol-value name))
              (constantp name)))
  t
  t)

(deftest define-constant.2
    (let ((name (gensym)))
      (eval `(define-constant ,name 13))
      (eval `(define-constant ,name 13))
      (values (eql 13 (symbol-value name))
              (constantp name)))
  t
  t)

;;;; Errors

228 229 230 231 232 233
;;; TYPEP is specified to return a generalized boolean and, for
;;; example, ECL exploits this by returning the superclasses of ERROR
;;; in this case.
(defun errorp (x)
  (not (null (typep x 'error))))

234 235 236
(deftest required-argument.1
    (multiple-value-bind (res err)
        (ignore-errors (required-argument))
237
      (errorp err))
238 239
  t)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
240 241
;;;; Hash tables

242
(deftest ensure-gethash.1
Nikodemus Siivola's avatar
Nikodemus Siivola committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256
    (let ((table (make-hash-table))
          (x (list 1)))
      (multiple-value-bind (value already-there)
          (ensure-gethash x table 42)
        (and (= value 42)
             (not already-there)
             (= 42 (gethash x table))
             (multiple-value-bind (value2 already-there2)
                 (ensure-gethash x table 13)
               (and (= value2 42)
                    already-there2
                    (= 42 (gethash x table)))))))
  t)

257 258 259 260 261 262 263 264 265 266 267
(deftest ensure-gethash.2
    (let ((table (make-hash-table))
          (count 0))
      (multiple-value-call #'values
        (ensure-gethash (progn (incf count) :foo)
                        (progn (incf count) table)
                        (progn (incf count) :bar))
        (gethash :foo table)
        count))
  :bar nil :bar t 3)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
268 269 270 271 272 273 274 275
(deftest copy-hash-table.1
    (let ((orig (make-hash-table :test 'eq :size 123))
          (foo "foo"))
      (setf (gethash orig orig) t
            (gethash foo orig) t)
      (let ((eq-copy (copy-hash-table orig))
            (eql-copy (copy-hash-table orig :test 'eql))
            (equal-copy (copy-hash-table orig :test 'equal))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
276
            (equalp-copy (copy-hash-table orig :test 'equalp)))
277 278 279
        (list (eql (hash-table-size eq-copy) (hash-table-size orig))
              (eql (hash-table-rehash-size eq-copy)
                   (hash-table-rehash-size orig))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
280 281 282 283 284 285
              (hash-table-count eql-copy)
              (gethash orig eq-copy)
              (gethash (copy-seq foo) eql-copy)
              (gethash foo eql-copy)
              (gethash (copy-seq foo) equal-copy)
              (gethash "FOO" equal-copy)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
286
              (gethash "FOO" equalp-copy))))
287
  (t t 2 t nil t t nil t))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
288

289 290 291 292 293
(deftest copy-hash-table.2
    (let ((ht (make-hash-table))
          (list (list :list (vector :A :B :C))))
      (setf (gethash 'list ht) list)
      (let* ((shallow-copy (copy-hash-table ht))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
294 295 296 297
             (deep1-copy (copy-hash-table ht :key 'copy-list))
             (list         (gethash 'list ht))
             (shallow-list (gethash 'list shallow-copy))
             (deep1-list   (gethash 'list deep1-copy)))
298
        (list (eq ht shallow-copy)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
299 300 301 302 303 304
              (eq ht deep1-copy)
              (eq list shallow-list)
              (eq list deep1-list)                   ; outer list was copied.
              (eq (second list) (second shallow-list))
              (eq (second list) (second deep1-list)) ; inner vector wasn't copied.
              )))
305 306
  (nil nil t nil t t))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
307 308 309
(deftest maphash-keys.1
    (let ((keys nil)
          (table (make-hash-table)))
310
      (declare (notinline maphash-keys))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
311 312 313 314 315 316 317 318 319
      (dotimes (i 10)
        (setf (gethash i table) t))
      (maphash-keys (lambda (k) (push k keys)) table)
      (set-equal keys '(0 1 2 3 4 5 6 7 8 9)))
  t)

(deftest maphash-values.1
    (let ((vals nil)
          (table (make-hash-table)))
320
      (declare (notinline maphash-values))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
321 322 323
      (dotimes (i 10)
        (setf (gethash i table) (- i)))
      (maphash-values (lambda (v) (push v vals)) table)
324
      (set-equal vals '(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
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 367 368 369 370 371
  t)

(deftest hash-table-keys.1
    (let ((table (make-hash-table)))
      (dotimes (i 10)
        (setf (gethash i table) t))
      (set-equal (hash-table-keys table) '(0 1 2 3 4 5 6 7 8 9)))
  t)

(deftest hash-table-values.1
    (let ((table (make-hash-table)))
      (dotimes (i 10)
        (setf (gethash (gensym) table) i))
      (set-equal (hash-table-values table) '(0 1 2 3 4 5 6 7 8 9)))
  t)

(deftest hash-table-alist.1
    (let ((table (make-hash-table)))
      (dotimes (i 10)
        (setf (gethash i table) (- i)))
      (let ((alist (hash-table-alist table)))
        (list (length alist)
              (assoc 0 alist)
              (assoc 3 alist)
              (assoc 9 alist)
              (assoc nil alist))))
  (10 (0 . 0) (3 . -3) (9 . -9) nil))

(deftest hash-table-plist.1
    (let ((table (make-hash-table)))
      (dotimes (i 10)
        (setf (gethash i table) (- i)))
      (let ((plist (hash-table-plist table)))
        (list (length plist)
              (getf plist 0)
              (getf plist 2)
              (getf plist 7)
              (getf plist nil))))
  (20 0 -2 -7 nil))

(deftest alist-hash-table.1
    (let* ((alist '((0 a) (1 b) (2 c)))
           (table (alist-hash-table alist)))
      (list (hash-table-count table)
            (gethash 0 table)
            (gethash 1 table)
            (gethash 2 table)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
372 373 374
            (eq (hash-table-test-name 'eql)
                (hash-table-test table))))
  (3 (a) (b) (c) t))
375

376 377 378 379 380 381 382 383 384
(deftest alist-hash-table.duplicate-keys
    (let* ((alist '((0 a) (1 b) (0 c) (1 d) (2 e)))
           (table (alist-hash-table alist)))
      (list (hash-table-count table)
            (gethash 0 table)
            (gethash 1 table)
            (gethash 2 table)))
  (3 (a) (b) (e)))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
385 386 387 388 389 390 391 392 393
(deftest plist-hash-table.1
    (let* ((plist '(:a 1 :b 2 :c 3))
           (table (plist-hash-table plist :test 'eq)))
      (list (hash-table-count table)
            (gethash :a table)
            (gethash :b table)
            (gethash :c table)
            (gethash 2 table)
            (gethash nil table)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
394 395 396
            (eq (hash-table-test-name 'eq)
                (hash-table-test table))))
  (3 1 2 3 nil nil t))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
397

398 399 400 401 402 403 404 405 406
(deftest plist-hash-table.duplicate-keys
    (let* ((plist '(:a 1 :b 2 :a 3 :b 4 :c 5))
           (table (plist-hash-table plist)))
      (list (hash-table-count table)
            (gethash :a table)
            (gethash :b table)
            (gethash :c table)))
  (3 1 2 5))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
407 408 409 410 411 412 413 414 415 416 417 418
;;;; Functions

(deftest disjoin.1
    (let ((disjunction (disjoin (lambda (x)
                                  (and (consp x) :cons))
                                (lambda (x)
                                  (and (stringp x) :string)))))
      (list (funcall disjunction 'zot)
            (funcall disjunction '(foo bar))
            (funcall disjunction "test")))
  (nil :cons :string))

419 420 421 422 423 424
(deftest disjoin.2
    (let ((disjunction (disjoin #'zerop)))
      (list (funcall disjunction 0)
            (funcall disjunction 1)))
  (t nil))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
425
(deftest conjoin.1
426
    (let ((conjunction (conjoin #'consp
Nikodemus Siivola's avatar
Nikodemus Siivola committed
427 428 429 430 431 432 433 434 435
                                (lambda (x)
                                  (stringp (car x)))
                                (lambda (x)
                                  (char (car x) 0)))))
      (list (funcall conjunction 'zot)
            (funcall conjunction '(foo))
            (funcall conjunction '("foo"))))
  (nil nil #\f))

436 437 438 439 440 441
(deftest conjoin.2
    (let ((conjunction (conjoin #'zerop)))
      (list (funcall conjunction 0)
            (funcall conjunction 1)))
  (t nil))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
442
(deftest compose.1
443
    (let ((composite (compose '1+
Nikodemus Siivola's avatar
Nikodemus Siivola committed
444 445 446 447 448 449 450
                              (lambda (x)
                                (* x 2))
                              #'read-from-string)))
      (funcall composite "1"))
  3)

(deftest compose.2
451
    (let ((composite
Nikodemus Siivola's avatar
Nikodemus Siivola committed
452
           (locally (declare (notinline compose))
453
             (compose '1+
Nikodemus Siivola's avatar
Nikodemus Siivola committed
454 455 456 457 458 459 460 461
                      (lambda (x)
                        (* x 2))
                      #'read-from-string))))
      (funcall composite "2"))
  5)

(deftest compose.3
    (let ((compose-form (funcall (compiler-macro-function 'compose)
462
                                 '(compose '1+
Nikodemus Siivola's avatar
Nikodemus Siivola committed
463 464 465 466 467 468 469 470
                                   (lambda (x)
                                     (* x 2))
                                   #'read-from-string)
                                 nil)))
      (let ((fun (funcall (compile nil `(lambda () ,compose-form)))))
        (funcall fun "3")))
  7)

471 472 473 474 475 476
(deftest compose.4
    (let ((composite (compose #'zerop)))
      (list (funcall composite 0)
            (funcall composite 1)))
  (t nil))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
(deftest multiple-value-compose.1
    (let ((composite (multiple-value-compose
                      #'truncate
                      (lambda (x y)
                        (values y x))
                      (lambda (x)
                        (with-input-from-string (s x)
                          (values (read s) (read s)))))))
      (multiple-value-list (funcall composite "2 7")))
  (3 1))

(deftest multiple-value-compose.2
    (let ((composite (locally (declare (notinline multiple-value-compose))
                       (multiple-value-compose
                        #'truncate
                        (lambda (x y)
                          (values y x))
                       (lambda (x)
                         (with-input-from-string (s x)
                           (values (read s) (read s))))))))
      (multiple-value-list (funcall composite "2 11")))
  (5 1))

(deftest multiple-value-compose.3
    (let ((compose-form (funcall (compiler-macro-function 'multiple-value-compose)
                                 '(multiple-value-compose
                                   #'truncate
                                   (lambda (x y)
                                     (values y x))
                                   (lambda (x)
                                     (with-input-from-string (s x)
                                       (values (read s) (read s)))))
                                 nil)))
      (let ((fun (funcall (compile nil `(lambda () ,compose-form)))))
        (multiple-value-list (funcall fun "2 9"))))
  (4 1))

514 515 516 517 518
(deftest multiple-value-compose.4
    (let ((composite (multiple-value-compose #'truncate)))
      (multiple-value-list (funcall composite 9 2)))
  (4 1))

519
(deftest curry.1
Nikodemus Siivola's avatar
Nikodemus Siivola committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    (let ((curried (curry '+ 3)))
      (funcall curried 1 5))
  9)

(deftest curry.2
    (let ((curried (locally (declare (notinline curry))
                     (curry '* 2 3))))
      (funcall curried 7))
  42)

(deftest curry.3
    (let ((curried-form (funcall (compiler-macro-function 'curry)
                                 '(curry '/ 8)
                                 nil)))
      (let ((fun (funcall (compile nil `(lambda () ,curried-form)))))
        (funcall fun 2)))
  4)

538 539 540 541 542 543 544 545 546 547 548
(deftest curry.4
    (let* ((x 1)
           (curried (curry (progn
                             (incf x)
                             (lambda (y z) (* x y z)))
                           3)))
      (list (funcall curried 7)
            (funcall curried 7)
            x))
  (42 42 2))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
549 550 551 552 553
(deftest rcurry.1
    (let ((r (rcurry '/ 2)))
      (funcall r 8))
  4)

554 555 556 557 558 559 560 561 562 563 564
(deftest rcurry.2
    (let* ((x 1)
           (curried (rcurry (progn
                              (incf x)
                              (lambda (y z) (* x y z)))
                            3)))
      (list (funcall curried 7)
            (funcall curried 7)
            x))
  (42 42 2))

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
(deftest named-lambda.1
    (let ((fac (named-lambda fac (x)
                 (if (> x 1)
                     (* x (fac (- x 1)))
                     x))))
      (funcall fac 5))
  120)

(deftest named-lambda.2
    (let ((fac (named-lambda fac (&key x)
                 (if (> x 1)
                     (* x (fac :x (- x 1)))
                     x))))
      (funcall fac :x 5))
  120)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
581 582
;;;; Lists

583 584 585 586 587 588 589 590 591
(deftest alist-plist.1
    (alist-plist '((a . 1) (b . 2) (c . 3)))
  (a 1 b 2 c 3))

(deftest plist-alist.1
    (plist-alist '(a 1 b 2 c 3))
  ((a . 1) (b . 2) (c . 3)))

(deftest unionf.1
592
    (let* ((list (list 1 2 3))
593
           (orig list))
594
      (unionf list (list 1 2 4))
595 596 597 598 599 600 601 602 603 604
      (values (equal orig (list 1 2 3))
              (eql (length list) 4)
              (set-difference list (list 1 2 3 4))
              (set-difference (list 1 2 3 4) list)))
  t
  t
  nil
  nil)

(deftest nunionf.1
605 606
    (let ((list (list 1 2 3)))
      (nunionf list (list 1 2 4))
607 608 609 610 611 612 613
      (values (eql (length list) 4)
              (set-difference (list 1 2 3 4) list)
              (set-difference list (list 1 2 3 4))))
  t
  nil
  nil)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
614
(deftest appendf.1
615
    (let* ((list (list 1 2 3))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
616 617 618 619 620
           (orig list))
      (appendf list '(4 5 6) '(7 8))
      (list list (eq list orig)))
  ((1 2 3 4 5 6 7 8) nil))

Luis Oliveira's avatar
Luis Oliveira committed
621 622 623 624 625 626 627
(deftest nconcf.1
    (let ((list1 (list 1 2 3))
          (list2 (list 4 5 6)))
      (nconcf list1 list2 (list 7 8 9))
      list1)
  (1 2 3 4 5 6 7 8 9))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
(deftest circular-list.1
    (let ((circle (circular-list 1 2 3)))
      (list (first circle)
            (second circle)
            (third circle)
            (fourth circle)
            (eq circle (nthcdr 3 circle))))
  (1 2 3 1 t))

(deftest circular-list-p.1
    (let* ((circle (circular-list 1 2 3 4))
           (tree (list circle circle))
           (dotted (cons circle t))
           (proper (list 1 2 3 circle))
           (tailcirc (list* 1 2 3 circle)))
      (list (circular-list-p circle)
            (circular-list-p tree)
            (circular-list-p dotted)
            (circular-list-p proper)
            (circular-list-p tailcirc)))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
648
  (t nil nil nil t))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
649

650 651 652 653
(deftest circular-list-p.2
    (circular-list-p 'foo)
  nil)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
654 655 656 657 658 659 660 661 662 663 664 665 666
(deftest circular-tree-p.1
    (let* ((circle (circular-list 1 2 3 4))
           (tree1 (list circle circle))
           (tree2 (let* ((level2 (list 1 nil 2))
                         (level1 (list level2)))
                    (setf (second level2) level1)
                    level1))
           (dotted (cons circle t))
           (proper (list 1 2 3 circle))
           (tailcirc (list* 1 2 3 circle))
           (quite-proper (list 1 2 3))
           (quite-dotted (list 1 (cons 2 3))))
      (list (circular-tree-p circle)
667
            (circular-tree-p tree1)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
668 669 670 671 672 673 674 675
            (circular-tree-p tree2)
            (circular-tree-p dotted)
            (circular-tree-p proper)
            (circular-tree-p tailcirc)
            (circular-tree-p quite-proper)
            (circular-tree-p quite-dotted)))
  (t t t t t t nil nil))

676 677 678 679
(deftest circular-tree-p.2
    (alexandria:circular-tree-p '#1=(#1#))
  t)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
680 681 682 683 684 685 686 687 688 689 690 691 692
(deftest proper-list-p.1
    (let ((l1 (list 1))
          (l2 (list 1 2))
          (l3 (cons 1 2))
          (l4 (list (cons 1 2) 3))
          (l5 (circular-list 1 2)))
      (list (proper-list-p l1)
            (proper-list-p l2)
            (proper-list-p l3)
            (proper-list-p l4)
            (proper-list-p l5)))
  (t t nil t nil))

693 694 695 696
(deftest proper-list-p.2
    (proper-list-p '(1 2 . 3))
  nil)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
697 698 699 700 701 702 703 704 705 706 707 708 709
(deftest proper-list.type.1
    (let ((l1 (list 1))
          (l2 (list 1 2))
          (l3 (cons 1 2))
          (l4 (list (cons 1 2) 3))
          (l5 (circular-list 1 2)))
      (list (typep l1 'proper-list)
            (typep l2 'proper-list)
            (typep l3 'proper-list)
            (typep l4 'proper-list)
            (typep l5 'proper-list)))
  (t t nil t nil))

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
(deftest proper-list-length.1
    (values
     (proper-list-length nil)
     (proper-list-length (list 1))
     (proper-list-length (list 2 2))
     (proper-list-length (list 3 3 3))
     (proper-list-length (list 4 4 4 4))
     (proper-list-length (list 5 5 5 5 5))
     (proper-list-length (list 6 6 6 6 6 6))
     (proper-list-length (list 7 7 7 7 7 7 7))
     (proper-list-length (list 8 8 8 8 8 8 8 8))
     (proper-list-length (list 9 9 9 9 9 9 9 9 9)))
  0 1 2 3 4 5 6 7 8 9)

(deftest proper-list-length.2
    (flet ((plength (x)
             (handler-case
                 (proper-list-length x)
               (type-error ()
                 :ok))))
      (values
       (plength (list* 1))
       (plength (list* 2 2))
       (plength (list* 3 3 3))
       (plength (list* 4 4 4 4))
       (plength (list* 5 5 5 5 5))
       (plength (list* 6 6 6 6 6 6))
       (plength (list* 7 7 7 7 7 7 7))
       (plength (list* 8 8 8 8 8 8 8 8))
       (plength (list* 9 9 9 9 9 9 9 9 9))))
  :ok :ok :ok
  :ok :ok :ok
  :ok :ok :ok)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
744 745 746 747 748 749 750 751 752
(deftest lastcar.1
    (let ((l1 (list 1))
          (l2 (list 1 2)))
      (list (lastcar l1)
            (lastcar l2)))
  (1 2))

(deftest lastcar.error.2
    (handler-case
753
        (progn
Nikodemus Siivola's avatar
Nikodemus Siivola committed
754 755 756 757 758 759
          (lastcar (circular-list 1 2 3))
          nil)
      (error ()
        t))
  t)

760 761 762 763 764 765 766 767 768
(deftest setf-lastcar.1
    (let ((l (list 1 2 3 4)))
      (values (lastcar l)
              (progn
                (setf (lastcar l) 42)
                (lastcar l))))
  4
  42)

769 770 771 772 773 774 775
(deftest setf-lastcar.2
    (let ((l (circular-list 1 2 3)))
      (multiple-value-bind (res err)
          (ignore-errors (setf (lastcar l) 4))
        (typep err 'type-error)))
  t)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
(deftest make-circular-list.1
    (let ((l (make-circular-list 3 :initial-element :x)))
      (setf (car l) :y)
      (list (eq l (nthcdr 3 l))
            (first l)
            (second l)
            (third l)
            (fourth l)))
  (t :y :x :x :y))

(deftest circular-list.type.1
    (let* ((l1 (list 1 2 3))
           (l2 (circular-list 1 2 3))
           (l3 (list* 1 2 3 l2)))
      (list (typep l1 'circular-list)
            (typep l2 'circular-list)
            (typep l3 'circular-list)))
  (nil t t))

(deftest ensure-list.1
    (let ((x (list 1))
          (y 2))
      (list (ensure-list x)
            (ensure-list y)))
  ((1) (2)))

802 803 804 805 806 807 808 809 810 811 812
(deftest ensure-cons.1
    (let ((x (cons 1 2))
          (y nil)
          (z "foo"))
      (values (ensure-cons x)
              (ensure-cons y)
              (ensure-cons z)))
  (1 . 2)
  (nil)
  ("foo"))

813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
(deftest setp.1
    (setp '(1))
  t)

(deftest setp.2
    (setp nil)
  t)

(deftest setp.3
    (setp "foo")
  nil)

(deftest setp.4
    (setp '(1 2 3 1))
  nil)

(deftest setp.5
    (setp '(1 2 3))
  t)

(deftest setp.6
    (setp '(a :a))
  t)

(deftest setp.7
    (setp '(a :a) :key 'character)
  nil)

(deftest setp.8
    (setp '(a :a) :key 'character :test (constantly nil))
  t)

(deftest set-equal.1
    (set-equal '(1 2 3) '(3 1 2))
  t)

(deftest set-equal.2
    (set-equal '("Xa") '("Xb")
               :test (lambda (a b) (eql (char a 0) (char b 0))))
  t)

(deftest set-equal.3
    (set-equal '(1 2) '(4 2))
  nil)

(deftest set-equal.4
    (set-equal '(a b c) '(:a :b :c) :key 'string :test 'equal)
  t)

(deftest set-equal.5
    (set-equal '(a d c) '(:a :b :c) :key 'string :test 'equal)
  nil)

866 867 868 869
(deftest set-equal.6
    (set-equal '(a b c) '(a b c d))
  nil)

870 871 872 873
(deftest map-product.1
    (map-product 'cons '(2 3) '(1 4))
  ((2 . 1) (2 . 4) (3 . 1) (3 . 4)))

874 875 876 877
(deftest map-product.2
    (map-product #'cons '(2 3) '(1 4))
  ((2 . 1) (2 . 4) (3 . 1) (3 . 4)))

878 879 880 881
(deftest flatten.1
    (flatten '((1) 2 (((3 4))) ((((5)) 6)) 7))
  (1 2 3 4 5 6 7))

882
(deftest remove-from-plist.1
883
    (let ((orig '(a 1 b 2 c 3 d 4)))
884 885 886 887 888 889 890
      (list (remove-from-plist orig 'a 'c)
            (remove-from-plist orig 'b 'd)
            (remove-from-plist orig 'b)
            (remove-from-plist orig 'a)
            (remove-from-plist orig 'd 42 "zot")
            (remove-from-plist orig 'a 'b 'c 'd)
            (remove-from-plist orig 'a 'b 'c 'd 'x)
891
            (equal orig '(a 1 b 2 c 3 d 4))))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
892 893 894 895 896 897 898 899 900
  ((b 2 d 4)
   (a 1 c 3)
   (a 1 c 3 d 4)
   (b 2 c 3 d 4)
   (a 1 b 2 c 3)
   nil
   nil
   t))

Attila Lendvai's avatar
Attila Lendvai committed
901
(deftest delete-from-plist.1
902
    (let ((orig '(a 1 b 2 c 3 d 4 d 5)))
Attila Lendvai's avatar
Attila Lendvai committed
903 904 905 906 907 908 909 910 911
      (list (delete-from-plist (copy-list orig) 'a 'c)
            (delete-from-plist (copy-list orig) 'b 'd)
            (delete-from-plist (copy-list orig) 'b)
            (delete-from-plist (copy-list orig) 'a)
            (delete-from-plist (copy-list orig) 'd 42 "zot")
            (delete-from-plist (copy-list orig) 'a 'b 'c 'd)
            (delete-from-plist (copy-list orig) 'a 'b 'c 'd 'x)
            (equal orig (delete-from-plist orig))
            (eq orig (delete-from-plist orig))))
912
  ((b 2 d 4 d 5)
Attila Lendvai's avatar
Attila Lendvai committed
913
   (a 1 c 3)
914 915
   (a 1 c 3 d 4 d 5)
   (b 2 c 3 d 4 d 5)
Attila Lendvai's avatar
Attila Lendvai committed
916 917 918 919 920 921
   (a 1 b 2 c 3)
   nil
   nil
   t
   t))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
922 923 924 925
(deftest mappend.1
    (mappend (compose 'list '*) '(1 2 3) '(1 2 3))
  (1 4 9))

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
(deftest assoc-value.1
    (let ((key1 '(complex key))
          (key2 'simple-key)
          (alist '())
          (result '()))
      (push 1 (assoc-value alist key1 :test #'equal))
      (push 2 (assoc-value alist key1 :test 'equal))
      (push 42 (assoc-value alist key2))
      (push 43 (assoc-value alist key2 :test 'eq))
      (push (assoc-value alist key1 :test #'equal) result)
      (push (assoc-value alist key2) result)

      (push 'very (rassoc-value alist (list 2 1) :test #'equal))
      (push (cdr (assoc '(very complex key) alist :test #'equal)) result)
      result)
  ((2 1) (43 42) (2 1)))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
943 944 945 946 947 948 949 950 951 952 953
;;;; Numbers

(deftest clamp.1
    (list (clamp 1.5 1 2)
          (clamp 2.0 1 2)
          (clamp 1.0 1 2)
          (clamp 3 1 2)
          (clamp 0 1 2))
  (1.5 2.0 1.0 2 1))

(deftest gaussian-random.1
954 955 956 957 958 959 960 961 962 963 964
    (let ((min -0.2)
          (max +0.2))
      (multiple-value-bind (g1 g2)
          (gaussian-random min max)
        (values (<= min g1 max)
                (<= min g2 max)
                (/= g1 g2) ;uh
                )))
  t
  t
  t)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
965

966 967 968 969 970 971 972 973 974 975 976
#+sbcl
(deftest gaussian-random.2
    (handler-case
        (sb-ext:with-timeout 2
          (progn
            (loop
              :repeat 10000
              :do (gaussian-random 0 nil))
            'done))
      (sb-ext:timeout ()
        'timed-out))
Attila Lendvai's avatar
Attila Lendvai committed
977
  done)
978

Nikodemus Siivola's avatar
Nikodemus Siivola committed
979 980 981 982 983 984 985 986 987 988
(deftest iota.1
    (iota 3)
  (0 1 2))

(deftest iota.2
    (iota 3 :start 0.0d0)
  (0.0d0 1.0d0 2.0d0))

(deftest iota.3
    (iota 3 :start 2 :step 3.0)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
989
  (2.0 5.0 8.0))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
990

991 992 993 994 995 996 997 998 999 1000 1001
(deftest map-iota.1
    (let (all)
      (declare (notinline map-iota))
      (values (map-iota (lambda (x) (push x all))
                        3
                        :start 2
                        :step 1.1d0)
              all))
  3
  (4.2d0 3.1d0 2.0d0))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1002 1003 1004 1005 1006 1007 1008 1009
(deftest lerp.1
    (lerp 0.5 1 2)
  1.5)

(deftest lerp.2
    (lerp 0.1 1 2)
  1.1)

1010 1011 1012 1013
(deftest lerp.3
    (lerp 0.1 4 25)
  6.1)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1014 1015 1016 1017 1018 1019 1020 1021
(deftest mean.1
    (mean '(1 2 3))
  2)

(deftest mean.2
    (mean '(1 2 3 4))
  5/2)

1022
(deftest mean.3
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1023
    (mean '(1 2 10))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1024
  13/3)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1025 1026 1027 1028 1029 1030 1031

(deftest median.1
    (median '(100 0 99 1 98 2 97))
  97)

(deftest median.2
    (median '(100 0 99 1 98 2 97 96))
Johan Ur Riise's avatar
Johan Ur Riise committed
1032
  193/2)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1033

1034 1035 1036
(deftest variance.1
    (variance (list 1 2 3))
  2/3)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1037

1038 1039 1040
(deftest standard-deviation.1
    (< 0 (standard-deviation (list 1 2 3)) 1)
  t)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

(deftest maxf.1
    (let ((x 1))
      (maxf x 2)
      x)
  2)

(deftest maxf.2
    (let ((x 1))
      (maxf x 0)
      x)
  1)

(deftest maxf.3
    (let ((x 1)
          (c 0))
      (maxf x (incf c))
      (list x c))
  (1 1))

(deftest maxf.4
    (let ((xv (vector 0 0 0))
          (p 0))
      (maxf (svref xv (incf p)) (incf p))
      (list p xv))
  (2 #(0 2 0)))

(deftest minf.1
    (let ((y 1))
      (minf y 0)
      y)
  0)

(deftest minf.2
    (let ((xv (vector 10 10 10))
          (p 0))
      (minf (svref xv (incf p)) (incf p))
      (list p xv))
  (2 #(10 2 10)))

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
(deftest subfactorial.1
    (mapcar #'subfactorial (iota 22))
  (1
   0
   1
   2
   9
   44
   265
   1854
   14833
   133496
   1334961
   14684570
   176214841
   2290792932
   32071101049
   481066515734
   7697064251745
   130850092279664
   2355301661033953
   44750731559645106
   895014631192902121
   18795307255050944540))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
;;;; Arrays

#+nil
(deftest array-index.type)

#+nil
(deftest copy-array)

;;;; Sequences

1116 1117 1118 1119 1120 1121
(deftest rotate.1
    (list (rotate (list 1 2 3) 0)
          (rotate (list 1 2 3) 1)
          (rotate (list 1 2 3) 2)
          (rotate (list 1 2 3) 3)
          (rotate (list 1 2 3) 4))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1122 1123 1124 1125 1126 1127
  ((1 2 3)
   (3 1 2)
   (2 3 1)
   (1 2 3)
   (3 1 2)))

1128 1129 1130 1131 1132 1133 1134
(deftest rotate.2
    (list (rotate (vector 1 2 3 4) 0)
          (rotate (vector 1 2 3 4))
          (rotate (vector 1 2 3 4) 2)
          (rotate (vector 1 2 3 4) 3)
          (rotate (vector 1 2 3 4) 4)
          (rotate (vector 1 2 3 4) 5))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1135 1136 1137 1138 1139 1140 1141
  (#(1 2 3 4)
    #(4 1 2 3)
    #(3 4 1 2)
    #(2 3 4 1)
    #(1 2 3 4)
    #(4 1 2 3)))

1142 1143 1144 1145 1146 1147
(deftest rotate.3
    (list (rotate (list 1 2 3) 0)
          (rotate (list 1 2 3) -1)
          (rotate (list 1 2 3) -2)
          (rotate (list 1 2 3) -3)
          (rotate (list 1 2 3) -4))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1148 1149 1150 1151 1152 1153
  ((1 2 3)
   (2 3 1)
   (3 1 2)
   (1 2 3)
   (2 3 1)))

1154 1155 1156 1157 1158 1159 1160
(deftest rotate.4
    (list (rotate (vector 1 2 3 4) 0)
          (rotate (vector 1 2 3 4) -1)
          (rotate (vector 1 2 3 4) -2)
          (rotate (vector 1 2 3 4) -3)
          (rotate (vector 1 2 3 4) -4)
          (rotate (vector 1 2 3 4) -5))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1161 1162 1163 1164 1165 1166 1167
  (#(1 2 3 4)
   #(2 3 4 1)
   #(3 4 1 2)
   #(4 1 2 3)
   #(1 2 3 4)
   #(2 3 4 1)))

1168 1169 1170 1171 1172 1173 1174 1175
(deftest rotate.5
    (values (rotate (list 1) 17)
            (rotate (list 1) -5))
  (1)
  (1))

(deftest shuffle.1
    (let ((s (shuffle (iota 100))))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1176 1177 1178 1179 1180 1181 1182 1183 1184
      (list (equal s (iota 100))
            (every (lambda (x)
                     (member x s))
                   (iota 100))
            (every (lambda (x)
                     (typep x '(integer 0 99)))
                   s)))
  (nil t t))

1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
(deftest shuffle.2
    (let ((s (shuffle (coerce (iota 100) 'vector))))
      (list (equal s (coerce (iota 100) 'vector))
            (every (lambda (x)
                     (find x s))
                   (iota 100))
            (every (lambda (x)
                     (typep x '(integer 0 99)))
                   s)))
  (nil t t))

1196 1197 1198 1199 1200 1201 1202 1203
(deftest shuffle.3
    (let* ((orig (coerce (iota 21) 'vector))
           (copy (copy-seq orig)))
      (shuffle copy :start 10 :end 15)
      (list (every #'eql (subseq copy 0 10) (subseq orig 0 10))
            (every #'eql (subseq copy 15) (subseq orig 15))))
  (t t))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
(deftest random-elt.1
    (let ((s1 #(1 2 3 4))
          (s2 '(1 2 3 4)))
      (list (dotimes (i 1000 nil)
              (unless (member (random-elt s1) s2)
                (return nil))
              (when (/= (random-elt s1) (random-elt s1))
                (return t)))
            (dotimes (i 1000 nil)
              (unless (member (random-elt s2) s2)
                (return nil))
              (when (/= (random-elt s2) (random-elt s2))
                (return t)))))
  (t t))

(deftest removef.1
    (let* ((x '(1 2 3))
           (x* x)
           (y #(1 2 3))
           (y* y))
      (removef x 1)
      (removef y 3)
      (list x x* y y*))
  ((2 3)
   (1 2 3)
   #(1 2)
   #(1 2 3)))

(deftest deletef.1
1233
    (let* ((x (list 1 2 3))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1234
           (x* x)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1235
           (y (vector 1 2 3)))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1236 1237 1238 1239 1240 1241 1242
      (deletef x 2)
      (deletef y 1)
      (list x x* y))
  ((1 3)
   (1 3)
   #(2 3)))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
(deftest map-permutations.1
    (let ((seq (list 1 2 3))
          (seen nil)
          (ok t))
      (map-permutations (lambda (s)
                          (unless (set-equal s seq)
                            (setf ok nil))
                          (when (member s seen :test 'equal)
                            (setf ok nil))
                          (push s seen))
                        seq
                        :copy t)
      (values ok (length seen)))
  t
  6)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
(deftest proper-sequence.type.1
    (mapcar (lambda (x)
              (typep x 'proper-sequence))
            (list (list 1 2 3)
                  (vector 1 2 3)
                  #2a((1 2) (3 4))
                  (circular-list 1 2 3 4)))
  (t t nil nil))

(deftest emptyp.1
1269
    (mapcar #'emptyp
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
            (list (list 1)
                  (circular-list 1)
                  nil
                  (vector)
                  (vector 1)))
  (nil nil t t nil))

(deftest sequence-of-length-p.1
    (mapcar #'sequence-of-length-p
            (list nil
                  #()
                  (list 1)
                  (vector 1)
                  (list 1 2)
                  (vector 1 2)
                  (list 1 2)
                  (vector 1 2)
                  (list 1 2)
                  (vector 1 2))
            (list 0
                  0
                  1
                  1
                  2
                  2
                  1
                  1
                  4
                  4))
  (t t t t t t nil nil nil nil))

Attila Lendvai's avatar
Attila Lendvai committed
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
(deftest length=.1
    (mapcar #'length=
            (list nil
                  #()
                  (list 1)
                  (vector 1)
                  (list 1 2)
                  (vector 1 2)
                  (list 1 2)
                  (vector 1 2)
                  (list 1 2)
                  (vector 1 2))
            (list 0
                  0
                  1
                  1
                  2
                  2
                  1
                  1
                  4
                  4))
  (t t t t t t nil nil nil nil))

(deftest length=.2
    ;; test the compiler macro
    (macrolet ((x (&rest args)
                 (funcall
                  (compile nil
                           `(lambda ()
                              (length= ,@args))))))
      (list (x 2 '(1 2))
            (x '(1 2) '(3 4))
            (x '(1 2) 2)
            (x '(1 2) 2 '(3 4))
            (x 1 2 3)))
  (t t t t nil))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1339 1340 1341
(deftest copy-sequence.1
    (let ((l (list 1 2 3))
          (v (vector #\a #\b #\c)))
1342
      (declare (notinline copy-sequence))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
      (let ((l.list (copy-sequence 'list l))
            (l.vector (copy-sequence 'vector l))
            (l.spec-v (copy-sequence '(vector fixnum) l))
            (v.vector (copy-sequence 'vector v))
            (v.list (copy-sequence 'list v))
            (v.string (copy-sequence 'string v)))
        (list (member l (list l.list l.vector l.spec-v))
              (member v (list v.vector v.list v.string))
              (equal l.list l)
              (equalp l.vector #(1 2 3))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1353 1354
              (type= (upgraded-array-element-type 'fixnum)
                     (array-element-type l.spec-v))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
              (equalp v.vector v)
              (equal v.list '(#\a #\b #\c))
              (equal "abc" v.string))))
  (nil nil t t t t t t))

(deftest first-elt.1
    (mapcar #'first-elt
            (list (list 1 2 3)
                  "abc"
                  (vector :a :b :c)))
  (1 #\a :a))

(deftest first-elt.error.1
    (mapcar (lambda (x)
              (handler-case
                  (first-elt x)
                (type-error ()
                  :type-error)))
            (list nil
                  #()
                  12
                  :zot))
  (:type-error
   :type-error
   :type-error
   :type-error))

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
(deftest setf-first-elt.1
    (let ((l (list 1 2 3))
          (s (copy-seq "foobar"))
          (v (vector :a :b :c)))
      (setf (first-elt l) -1
            (first-elt s) #\x
            (first-elt v) 'zot)
      (values l s v))
  (-1 2 3)
  "xoobar"
  #(zot :b :c))

1394 1395 1396 1397 1398 1399 1400
(deftest setf-first-elt.error.1
    (let ((l 'foo))
      (multiple-value-bind (res err)
          (ignore-errors (setf (first-elt l) 4))
        (typep err 'type-error)))
  t)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
(deftest last-elt.1
    (mapcar #'last-elt
            (list (list 1 2 3)
                  (vector :a :b :c)
                  "FOOBAR"
                  #*001
                  #*010))
  (3 :c #\R 1 0))

(deftest last-elt.error.1
    (mapcar (lambda (x)
              (handler-case
                  (last-elt x)
                (type-error ()
                  :type-error)))
            (list nil
                  #()
                  12
                  :zot
                  (circular-list 1 2 3)
                  (list* 1 2 3 (circular-list 4 5))))
1422 1423 1424 1425
  (:type-error
   :type-error
   :type-error
   :type-error
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1426 1427 1428
   :type-error
   :type-error))

1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
(deftest setf-last-elt.1
    (let ((l (list 1 2 3))
          (s (copy-seq "foobar"))
          (b (copy-seq #*010101001)))
      (setf (last-elt l) '???
            (last-elt s) #\?
            (last-elt b) 0)
      (values l s b))
  (1 2 ???)
  "fooba?"
  #*010101000)

1441 1442 1443 1444 1445 1446 1447
(deftest setf-last-elt.error.1
    (handler-case
        (setf (last-elt 'foo) 13)
      (type-error ()
        :type-error))
  :type-error)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
(deftest starts-with.1
    (list (starts-with 1 '(1 2 3))
          (starts-with 1 #(1 2 3))
          (starts-with #\x "xyz")
          (starts-with 2 '(1 2 3))
          (starts-with 3 #(1 2 3))
          (starts-with 1 1)
          (starts-with nil nil))
  (t t t nil nil nil nil))

1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
(deftest starts-with.2
    (values (starts-with 1 '(-1 2 3) :key '-)
            (starts-with "foo" '("foo" "bar") :test 'equal)
            (starts-with "f" '(#\f) :key 'string :test 'equal)
            (starts-with -1 '(0 1 2) :key #'1+)
            (starts-with "zot" '("ZOT") :test 'equal))
  t
  t
  t
  nil
  nil)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
(deftest ends-with.1
    (list (ends-with 3 '(1 2 3))
          (ends-with 3 #(1 2 3))
          (ends-with #\z "xyz")
          (ends-with 2 '(1 2 3))
          (ends-with 1 #(1 2 3))
          (ends-with 1 1)
          (ends-with nil nil))
  (t t t nil nil nil nil))

1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
(deftest ends-with.2
    (values (ends-with 2 '(0 13 1) :key '1+)
            (ends-with "foo" (vector "bar" "foo") :test 'equal)
            (ends-with "X" (vector 1 2 #\X) :key 'string :test 'equal)
            (ends-with "foo" "foo" :test 'equal))
  t
  t
  t
  nil)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1490 1491 1492 1493 1494 1495 1496
(deftest ends-with.error.1
    (handler-case
        (ends-with 3 (circular-list 3 3 3 1 3 3))
      (type-error ()
        :type-error))
  :type-error)

1497 1498
(deftest sequences.passing-improper-lists
    (macrolet ((signals-error-p (form)
1499
                 `(handler-case
1500
                      (progn ,form nil)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1501
                    (type-error (e)
1502 1503 1504 1505 1506 1507
                      t)))
               (cut (fn &rest args)
                 (with-gensyms (arg)
                   (print`(lambda (,arg)
                       (apply ,fn (list ,@(substitute arg '_ args))))))))
      (let ((circular-list (make-circular-list 5 :initial-element :foo))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
            (dotted-list (list* 'a 'b 'c 'd)))
        (loop for nth from 0
              for fn in (list
                         (cut #'lastcar _)
                         (cut #'rotate _ 3)
                         (cut #'rotate _ -3)
                         (cut #'shuffle _)
                         (cut #'random-elt _)
                         (cut #'last-elt _)
                         (cut #'ends-with :foo _))
              nconcing
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
                 (let ((on-circular-p (signals-error-p (funcall fn circular-list)))
                       (on-dotted-p (signals-error-p (funcall fn dotted-list))))
                   (when (or (not on-circular-p) (not on-dotted-p))
                     (append
                      (unless on-circular-p
                        (let ((*print-circle* t))
                          (list
                           (format nil
                                   "No appropriate error signalled when passing ~S to ~Ath entry."
                                   circular-list nth))))
                      (unless on-dotted-p
                        (list
                         (format nil
                                 "No appropriate error signalled when passing ~S to ~Ath entry."
                                 dotted-list nth)))))))))
  nil)

1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
;;;; IO

(deftest read-stream-content-into-string.1
    (values (with-input-from-string (stream "foo bar")
              (read-stream-content-into-string stream))
            (with-input-from-string (stream "foo bar")
              (read-stream-content-into-string stream :buffer-size 1))
            (with-input-from-string (stream "foo bar")
              (read-stream-content-into-string stream :buffer-size 6))
            (with-input-from-string (stream "foo bar")
              (read-stream-content-into-string stream :buffer-size 7)))
  "foo bar"
  "foo bar"
  "foo bar"
  "foo bar")

(deftest read-stream-content-into-string.2
    (handler-case
        (let ((stream (make-broadcast-stream)))
          (read-stream-content-into-string stream :buffer-size 0))
      (type-error ()
        :type-error))
  :type-error)

#+(or)
(defvar *octets*
  (map '(simple-array (unsigned-byte 8) (7)) #'char-code "foo bar"))

#+(or)
(deftest read-stream-content-into-byte-vector.1
    (values (with-input-from-byte-vector (stream *octets*)
              (read-stream-content-into-byte-vector stream))
            (with-input-from-byte-vector (stream *octets*)
              (read-stream-content-into-byte-vector stream :initial-size 1))
            (with-input-from-byte-vector (stream *octets*)
              (read-stream-content-into-byte-vector stream 'alexandria::%length 6))
            (with-input-from-byte-vector (stream *octets*)
              (read-stream-content-into-byte-vector stream 'alexandria::%length 3)))
  *octets*
  *octets*
  *octets*
  (subseq *octets* 0 3))

(deftest read-stream-content-into-byte-vector.2
    (handler-case
        (let ((stream (make-broadcast-stream)))
          (read-stream-content-into-byte-vector stream :initial-size 0))
      (type-error ()
        :type-error))
  :type-error)

;;;; Macros

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1589 1590 1591 1592 1593 1594 1595 1596 1597
(deftest with-unique-names.1
    (let ((*gensym-counter* 0))
      (let ((syms (with-unique-names (foo bar quux)
                    (list foo bar quux))))
        (list (find-if #'symbol-package syms)
              (equal '("FOO0" "BAR1" "QUUX2")
                     (mapcar #'symbol-name syms)))))
  (nil t))

1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
(deftest with-unique-names.2
    (let ((*gensym-counter* 0))
      (let ((syms (with-unique-names ((foo "_foo_") (bar -bar-) (quux #\q))
                    (list foo bar quux))))
        (list (find-if #'symbol-package syms)
              (equal '("_foo_0" "-BAR-1" "q2")
                     (mapcar #'symbol-name syms)))))
  (nil t))

(deftest with-unique-names.3
    (let ((*gensym-counter* 0))
      (multiple-value-bind (res err)
          (ignore-errors
            (eval
             '(let ((syms
                     (with-unique-names ((foo "_foo_") (bar -bar-) (quux 42))
                       (list foo bar quux))))
               (list (find-if #'symbol-package syms)
                (equal '("_foo_0" "-BAR-1" "q2")
                 (mapcar #'symbol-name syms))))))
1618
        (errorp err)))
1619 1620
  t)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
(deftest once-only.1
    (macrolet ((cons1.good (x)
                 (once-only (x)
                   `(cons ,x ,x)))
               (cons1.bad (x)
                 `(cons ,x ,x)))
      (let ((y 0))
        (list (cons1.good (incf y))
              y
              (cons1.bad (incf y))
              y)))
  ((1 . 1) 1 (2 . 3) 3))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
(deftest once-only.2
    (macrolet ((cons1 (x)
                 (once-only ((y x))
                   `(cons ,y ,y))))
      (let ((z 0))
        (list (cons1 (incf z))
              z
              (cons1 (incf z)))))
  ((1 . 1) 1 (2 . 2)))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1644 1645
(deftest parse-body.1
    (parse-body '("doc" "body") :documentation t)
1646 1647
  ("body")
  nil
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1648 1649 1650 1651
  "doc")

(deftest parse-body.2
    (parse-body '("body") :documentation t)
1652 1653
  ("body")
  nil
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1654 1655 1656 1657
  nil)

(deftest parse-body.3
    (parse-body '("doc" "body"))
1658 1659
  ("doc" "body")
  nil
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
  nil)

(deftest parse-body.4
    (parse-body '((declare (foo)) "doc" (declare (bar)) body) :documentation t)
  (body)
  ((declare (foo)) (declare (bar)))
  "doc")

(deftest parse-body.5
    (parse-body '((declare (foo)) "doc" (declare (bar)) body))
  ("doc" (declare (bar)) body)
  ((declare (foo)))
  nil)

1674 1675 1676 1677 1678
(deftest parse-body.6
    (multiple-value-bind (res err)
        (ignore-errors
          (parse-body '("foo" "bar" "quux")
                      :documentation t))
1679
      (errorp err))
1680 1681
  t)

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1682 1683 1684 1685
;;;; Symbols

(deftest ensure-symbol.1
    (ensure-symbol :cons :cl)
1686 1687
  cons
  :external)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1688 1689

(deftest ensure-symbol.2
1690 1691 1692
    (ensure-symbol "CONS" :alexandria)
  cons
  :inherited)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1693 1694 1695

(deftest ensure-symbol.3
    (ensure-symbol 'foo :keyword)
1696 1697
  :foo
  :external)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1698 1699

(deftest ensure-symbol.4
1700 1701 1702
    (ensure-symbol #\* :alexandria)
  *
  :inherited)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1703 1704

(deftest format-symbol.1
1705
    (let ((s (format-symbol nil '#:x-~d 13)))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1706
      (list (symbol-package s)
1707 1708
            (string= (string '#:x-13) (symbol-name s))))
  (nil t))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1709 1710

(deftest format-symbol.2
1711
    (format-symbol :keyword '#:sym-~a (string :bolic))
1712
  :sym-bolic)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1713 1714 1715

(deftest format-symbol.3
    (let ((*package* (find-package :cl)))
1716
      (format-symbol t '#:find-~a (string 'package)))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
  find-package)

(deftest make-keyword.1
    (list (make-keyword 'zot)
          (make-keyword "FOO")
          (make-keyword #\Q))
  (:zot :foo :q))

(deftest make-gensym-list.1
    (let ((*gensym-counter* 0))
      (let ((syms (make-gensym-list 3 "FOO")))
        (list (find-if 'symbol-package syms)
              (equal '("FOO0" "FOO1" "FOO2")
                     (mapcar 'symbol-name syms)))))
  (nil t))

1733 1734 1735 1736 1737 1738 1739 1740
(deftest make-gensym-list.2
    (let ((*gensym-counter* 0))
      (let ((syms (make-gensym-list 3)))
        (list (find-if 'symbol-package syms)
              (equal '("G0" "G1" "G2")
                     (mapcar 'symbol-name syms)))))
  (nil t))

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1741 1742 1743
;;;; Type-system

(deftest of-type.1
1744 1745
    (locally
        (declare (notinline of-type))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1746 1747
    (let ((f (of-type 'string)))
      (list (funcall f "foo")
1748
            (funcall f 'bar))))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
  (t nil))

(deftest type=.1
    (type= 'string 'string)
  t
  t)

(deftest type=.2
    (type= 'list '(or null cons))
  t
  t)

(deftest type=.3
    (type= 'null '(and symbol list))
  t
  t)

(deftest type=.4
    (type= 'string '(satisfies emptyp))
  nil
  nil)

(deftest type=.5
    (type= 'string 'list)
  nil
  t)
1775

1776 1777
(macrolet
    ((test (type numbers)
1778
       `(deftest ,(format-symbol t '#:cdr5.~a (string type))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1779 1780 1781 1782 1783 1784 1785 1786 1787
            (let ((numbers ,numbers))
              (values (mapcar (of-type ',(format-symbol t '#:negative-~a (string type))) numbers)
                      (mapcar (of-type ',(format-symbol t '#:non-positive-~a (string type))) numbers)
                      (mapcar (of-type ',(format-symbol t '#:non-negative-~a (string type))) numbers)
                      (mapcar (of-type ',(format-symbol t '#:positive-~a (string type))) numbers)))
          (t t t nil nil nil nil)
          (t t t t nil nil nil)
          (nil nil nil t t t t)
          (nil nil nil nil t t t))))
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
  (test fixnum       (list most-negative-fixnum       -42      -1     0     1     42      most-positive-fixnum))
  (test integer      (list (1- most-negative-fixnum)  -42      -1     0     1     42      (1+ most-positive-fixnum)))
  (test rational     (list (1- most-negative-fixnum)  -42/13   -1     0     1     42/13   (1+ most-positive-fixnum)))
  (test real         (list most-negative-long-float   -42/13   -1     0     1     42/13   most-positive-long-float))
  (test float        (list most-negative-short-float  -42.02   -1.0   0.0   1.0   42.02   most-positive-short-float))
  (test short-float  (list most-negative-short-float  -42.02s0 -1.0s0 0.0s0 1.0s0 42.02s0 most-positive-short-float))
  (test single-float (list most-negative-single-float -42.02f0 -1.0f0 0.0f0 1.0f0 42.02f0 most-positive-single-float))
  (test double-float (list most-negative-double-float -42.02d0 -1.0d0 0.0d0 1.0d0 42.02d0 most-positive-double-float))
  (test long-float   (list most-negative-long-float   -42.02l0 -1.0l0 0.0l0 1.0l0 42.02l0 most-positive-long-float)))

1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
;;;; Bindings

(declaim (notinline opaque))
(defun opaque (x)
  x)

(deftest if-let.1
    (if-let (x (opaque :ok))
            x
            :bad)
  :ok)

(deftest if-let.2
    (if-let (x (opaque nil))
            :bad
            (and (not x) :ok))
  :ok)

(deftest if-let.3
    (let ((x 1))
      (if-let ((x 2)
               (y x))
              (+ x y)
              :oops))
  3)

(deftest if-let.4
    (if-let ((x 1)
             (y nil))
            :oops
            (and (not y) x))
  1)

1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
(deftest if-let.5
    (if-let (x)
            :oops
            (not x))
  t)

(deftest if-let.error.1
    (handler-case
        (eval '(if-let x
                :oops
                :oops))
      (type-error ()
        :type-error))
  :type-error)

1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
(deftest when-let.1
    (when-let (x (opaque :ok))
      (setf x (cons x x))
      x)
  (:ok . :ok))

(deftest when-let.2
    (when-let ((x 1)
               (y nil)
               (z 3))
      :oops)
  nil)

(deftest when-let.3
    (let ((x 1))
      (when-let ((x 2)
                 (y x))
        (+ x y)))
  3)

1866 1867 1868 1869 1870 1871 1872
(deftest when-let.error.1
    (handler-case
        (eval '(when-let x :oops))
      (type-error ()
        :type-error))
  :type-error)

1873 1874 1875 1876 1877 1878
(deftest when-let*.1
    (let ((x 1))
      (when-let* ((x 2)
                  (y x))
        (+ x y)))
  4)
1879 1880 1881 1882 1883 1884 1885

(deftest when-let*.2
    (let ((y 1))
      (when-let* (x y)
        (1+ x)))
  2)

1886 1887 1888 1889 1890 1891 1892
(deftest when-let*.3
    (when-let* ((x t)
                (y (consp x))
                (z (error "OOPS")))
      t)
  nil)

1893 1894 1895 1896 1897 1898
(deftest when-let*.error.1
    (handler-case
        (eval '(when-let* x :oops))
      (type-error ()
        :type-error))
  :type-error)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1899

Nikodemus Siivola's avatar
Nikodemus Siivola committed
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
(deftest doplist.1
    (let (keys values)
      (doplist (k v '(a 1 b 2 c 3) (values t (reverse keys) (reverse values) k v))
        (push k keys)
        (push v values)))
  t
  (a b c)
  (1 2 3)
  nil
  nil)
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927

(deftest count-permutations.1
    (values (count-permutations 31 7)
            (count-permutations 1 1)
            (count-permutations 2 1)
            (count-permutations 2 2)
            (count-permutations 3 2)
            (count-permutations 3 1))
  13253058000
  1
  2
  2
  6
  3)

(deftest binomial-coefficient.1
    (alexandria:binomial-coefficient 1239 139)
  28794902202288970200771694600561826718847179309929858835480006683522184441358211423695124921058123706380656375919763349913245306834194782172712255592710204598527867804110129489943080460154)
1928

1929 1930 1931 1932 1933
;; Exercise bignum case (at least on x86).
(deftest binomial-coefficient.2
    (alexandria:binomial-coefficient 2000000000000 20)
  430998041177272843950422879590338454856322722740402365741730748431530623813012487773080486408378680853987520854296499536311275320016878730999689934464711239072435565454954447356845336730100919970769793030177499999999900000000000)

1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
(deftest copy-stream.1
    (let ((data "sdkfjhsakfh weior763495ewofhsdfk sdfadlkfjhsadf woif sdlkjfhslkdfh sdklfjh"))
      (values (equal data
                     (with-input-from-string (in data)
                       (with-output-to-string (out)
                         (alexandria:copy-stream in out))))
              (equal (subseq data 10 20)
                     (with-input-from-string (in data)
                       (with-output-to-string (out)
                         (alexandria:copy-stream in out :start 10 :end 20))))
              (equal (subseq data 10)
                     (with-input-from-string (in data)
                       (with-output-to-string (out)
                         (alexandria:copy-stream in out :start 10))))
              (equal (subseq data 0 20)
                     (with-input-from-string (in data)
                       (with-output-to-string (out)
                         (alexandria:copy-stream in out :end 20))))))
  t
  t
  t
  t)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975

(deftest extremum.1
    (let ((n 0))
      (dotimes (i 10)
       (let ((data (shuffle (coerce (iota 10000 :start i) 'vector)))
             (ok t))
         (unless (eql i (extremum data #'<))
           (setf ok nil))
         (unless (eql i (extremum (coerce data 'list) #'<))
           (setf ok nil))
         (unless (eql (+ 9999 i) (extremum data #'>))
           (setf ok nil))
         (unless (eql (+ 9999 i) (extremum (coerce  data 'list) #'>))
           (setf ok nil))
         (when ok
           (incf n))))
      (when (eql 10 (extremum #(100 1 10 1000) #'> :start 1 :end 3))
        (incf n))
      (when (eql -1000 (extremum #(100 1 10 -1000) #'> :key 'abs))
        (incf n))
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1976 1977 1978
      (when (eq nil (extremum "" (lambda (a b) (error "wtf? ~S, ~S" a b))))
        (incf n))
      n)
Nikodemus Siivola's avatar
Nikodemus Siivola committed
1979
  13)
1980

1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
(deftest starts-with-subseq.string
    (starts-with-subseq "f" "foo" :return-suffix t)
  t
  "oo")

(deftest starts-with-subseq.vector
    (starts-with-subseq #(1) #(1 2 3) :return-suffix t)
  t
  #(2 3))

(deftest starts-with-subseq.list
    (starts-with-subseq '(1) '(1 2 3) :return-suffix t)
  t
  (2 3))

1996 1997 1998 1999 2000 2001