| 1 |
(in-package :cl-user) |
| 2 |
|
| 3 |
(defpackage :alexandria/tests |
| 4 |
(:use :cl :alexandria #+sbcl :sb-rt #-sbcl :rtest) |
| 5 |
(:import-from #+sbcl :sb-rt #-sbcl :rtest |
| 6 |
#:*compile-tests* #:*expected-failures*)) |
| 7 |
|
| 8 |
(in-package :alexandria/tests) |
| 9 |
|
| 10 |
(defun run-tests (&key ((:compiled *compile-tests*))) |
| 11 |
(do-tests)) |
| 12 |
|
| 13 |
(defun hash-table-test-name (name) |
| 14 |
;; Workaround for Clisp calling EQL in a hash-table FASTHASH-EQL. |
| 15 |
(hash-table-test (make-hash-table :test name))) |
| 16 |
|
| 17 |
;;;; Arrays |
| 18 |
|
| 19 |
(deftest copy-array.1 |
| 20 |
(let* ((orig (vector 1 2 3)) |
| 21 |
(copy (copy-array orig))) |
| 22 |
(values (eq orig copy) (equalp orig copy))) |
| 23 |
nil t) |
| 24 |
|
| 25 |
(deftest copy-array.2 |
| 26 |
(let ((orig (make-array 1024 :fill-pointer 0 :initial-element nil))) |
| 27 |
(vector-push-extend 1 orig) |
| 28 |
(vector-push-extend 2 orig) |
| 29 |
(vector-push-extend 3 orig) |
| 30 |
(let ((copy (copy-array orig))) |
| 31 |
(values (eq orig copy) (equalp orig copy) |
| 32 |
(array-has-fill-pointer-p copy) |
| 33 |
(eql (fill-pointer orig) (fill-pointer copy))))) |
| 34 |
nil t t t) |
| 35 |
|
| 36 |
(deftest copy-array.3 |
| 37 |
(let* ((orig (vector 1 2 3)) |
| 38 |
(copy (copy-array orig))) |
| 39 |
(typep copy 'simple-array)) |
| 40 |
t) |
| 41 |
|
| 42 |
(deftest copy-array.4 |
| 43 |
(let ((orig (make-array 21 |
| 44 |
:adjustable t |
| 45 |
:fill-pointer 0))) |
| 46 |
(dotimes (n 42) |
| 47 |
(vector-push-extend n orig)) |
| 48 |
(let ((copy (copy-array orig |
| 49 |
:adjustable nil |
| 50 |
:fill-pointer nil))) |
| 51 |
(typep copy 'simple-array))) |
| 52 |
t) |
| 53 |
|
| 54 |
(deftest array-index.1 |
| 55 |
(typep 0 'array-index) |
| 56 |
t) |
| 57 |
|
| 58 |
;;;; Conditions |
| 59 |
|
| 60 |
(deftest unwind-protect-case.1 |
| 61 |
(let (result) |
| 62 |
(unwind-protect-case () |
| 63 |
(random 10) |
| 64 |
(:normal (push :normal result)) |
| 65 |
(:abort (push :abort result)) |
| 66 |
(:always (push :always result))) |
| 67 |
result) |
| 68 |
(:always :normal)) |
| 69 |
|
| 70 |
(deftest unwind-protect-case.2 |
| 71 |
(let (result) |
| 72 |
(unwind-protect-case () |
| 73 |
(random 10) |
| 74 |
(:always (push :always result)) |
| 75 |
(:normal (push :normal result)) |
| 76 |
(:abort (push :abort result))) |
| 77 |
result) |
| 78 |
(:normal :always)) |
| 79 |
|
| 80 |
(deftest unwind-protect-case.3 |
| 81 |
(let (result1 result2 result3) |
| 82 |
(ignore-errors |
| 83 |
(unwind-protect-case () |
| 84 |
(error "FOOF!") |
| 85 |
(:normal (push :normal result1)) |
| 86 |
(:abort (push :abort result1)) |
| 87 |
(:always (push :always result1)))) |
| 88 |
(catch 'foof |
| 89 |
(unwind-protect-case () |
| 90 |
(throw 'foof 42) |
| 91 |
(:normal (push :normal result2)) |
| 92 |
(:abort (push :abort result2)) |
| 93 |
(:always (push :always result2)))) |
| 94 |
(block foof |
| 95 |
(unwind-protect-case () |
| 96 |
(return-from foof 42) |
| 97 |
(:normal (push :normal result3)) |
| 98 |
(:abort (push :abort result3)) |
| 99 |
(:always (push :always result3)))) |
| 100 |
(values result1 result2 result3)) |
| 101 |
(:always :abort) |
| 102 |
(:always :abort) |
| 103 |
(:always :abort)) |
| 104 |
|
| 105 |
(deftest unwind-protect-case.4 |
| 106 |
(let (result) |
| 107 |
(unwind-protect-case (aborted-p) |
| 108 |
(random 42) |
| 109 |
(:always (setq result aborted-p))) |
| 110 |
result) |
| 111 |
nil) |
| 112 |
|
| 113 |
(deftest unwind-protect-case.5 |
| 114 |
(let (result) |
| 115 |
(block foof |
| 116 |
(unwind-protect-case (aborted-p) |
| 117 |
(return-from foof) |
| 118 |
(:always (setq result aborted-p)))) |
| 119 |
result) |
| 120 |
t) |
| 121 |
|
| 122 |
;;;; Control flow |
| 123 |
|
| 124 |
(deftest switch.1 |
| 125 |
(switch (13 :test =) |
| 126 |
(12 :oops) |
| 127 |
(13.0 :yay)) |
| 128 |
:yay) |
| 129 |
|
| 130 |
(deftest switch.2 |
| 131 |
(switch (13) |
| 132 |
((+ 12 2) :oops) |
| 133 |
((- 13 1) :oops2) |
| 134 |
(t :yay)) |
| 135 |
:yay) |
| 136 |
|
| 137 |
(deftest eswitch.1 |
| 138 |
(let ((x 13)) |
| 139 |
(eswitch (x :test =) |
| 140 |
(12 :oops) |
| 141 |
(13.0 :yay))) |
| 142 |
:yay) |
| 143 |
|
| 144 |
(deftest eswitch.2 |
| 145 |
(let ((x 13)) |
| 146 |
(eswitch (x :key 1+) |
| 147 |
(11 :oops) |
| 148 |
(14 :yay))) |
| 149 |
:yay) |
| 150 |
|
| 151 |
(deftest cswitch.1 |
| 152 |
(cswitch (13 :test =) |
| 153 |
(12 :oops) |
| 154 |
(13.0 :yay)) |
| 155 |
:yay) |
| 156 |
|
| 157 |
(deftest cswitch.2 |
| 158 |
(cswitch (13 :key 1-) |
| 159 |
(12 :yay) |
| 160 |
(13.0 :oops)) |
| 161 |
:yay) |
| 162 |
|
| 163 |
(deftest multiple-value-prog2.1 |
| 164 |
(multiple-value-prog2 |
| 165 |
(values 1 1 1) |
| 166 |
(values 2 20 200) |
| 167 |
(values 3 3 3)) |
| 168 |
2 20 200) |
| 169 |
|
| 170 |
(deftest nth-value-or.1 |
| 171 |
(multiple-value-bind (a b c) |
| 172 |
(nth-value-or 1 |
| 173 |
(values 1 nil 1) |
| 174 |
(values 2 2 2)) |
| 175 |
(= a b c 2)) |
| 176 |
t) |
| 177 |
|
| 178 |
(deftest whichever.1 |
| 179 |
(let ((x (whichever 1 2 3))) |
| 180 |
(and (member x '(1 2 3)) t)) |
| 181 |
t) |
| 182 |
|
| 183 |
(deftest whichever.2 |
| 184 |
(let* ((a 1) |
| 185 |
(b 2) |
| 186 |
(c 3) |
| 187 |
(x (whichever a b c))) |
| 188 |
(and (member x '(1 2 3)) t)) |
| 189 |
t) |
| 190 |
|
| 191 |
;; https://gitlab.common-lisp.net/alexandria/alexandria/issues/13 |
| 192 |
(deftest whichever.3 |
| 193 |
(multiple-value-bind (code warnings?) |
| 194 |
(compile nil `(lambda (x) |
| 195 |
(whichever (1+ x)))) |
| 196 |
(and (not warnings?) |
| 197 |
(= 6 (funcall code 5)))) |
| 198 |
t) |
| 199 |
|
| 200 |
(deftest xor.1 |
| 201 |
(xor nil nil 1 nil) |
| 202 |
1 |
| 203 |
t) |
| 204 |
|
| 205 |
(deftest xor.2 |
| 206 |
(xor nil nil 1 2) |
| 207 |
nil |
| 208 |
nil) |
| 209 |
|
| 210 |
(deftest xor.3 |
| 211 |
(xor nil nil nil) |
| 212 |
nil |
| 213 |
t) |
| 214 |
|
| 215 |
;;;; Definitions |
| 216 |
|
| 217 |
(deftest define-constant.1 |
| 218 |
(let ((name (gensym))) |
| 219 |
(eval `(define-constant ,name "FOO" :test 'equal)) |
| 220 |
(eval `(define-constant ,name "FOO" :test 'equal)) |
| 221 |
(values (equal "FOO" (symbol-value name)) |
| 222 |
(constantp name))) |
| 223 |
t |
| 224 |
t) |
| 225 |
|
| 226 |
(deftest define-constant.2 |
| 227 |
(let ((name (gensym))) |
| 228 |
(eval `(define-constant ,name 13)) |
| 229 |
(eval `(define-constant ,name 13)) |
| 230 |
(values (eql 13 (symbol-value name)) |
| 231 |
(constantp name))) |
| 232 |
t |
| 233 |
t) |
| 234 |
|
| 235 |
;;;; Errors |
| 236 |
|
| 237 |
;;; TYPEP is specified to return a generalized boolean and, for |
| 238 |
;;; example, ECL exploits this by returning the superclasses of ERROR |
| 239 |
;;; in this case. |
| 240 |
(defun errorp (x) |
| 241 |
(not (null (typep x 'error)))) |
| 242 |
|
| 243 |
(deftest required-argument.1 |
| 244 |
(multiple-value-bind (res err) |
| 245 |
(ignore-errors (required-argument)) |
| 246 |
(errorp err)) |
| 247 |
t) |
| 248 |
|
| 249 |
;;;; Hash tables |
| 250 |
|
| 251 |
(deftest ensure-gethash.1 |
| 252 |
(let ((table (make-hash-table)) |
| 253 |
(x (list 1))) |
| 254 |
(multiple-value-bind (value already-there) |
| 255 |
(ensure-gethash x table 42) |
| 256 |
(and (= value 42) |
| 257 |
(not already-there) |
| 258 |
(= 42 (gethash x table)) |
| 259 |
(multiple-value-bind (value2 already-there2) |
| 260 |
(ensure-gethash x table 13) |
| 261 |
(and (= value2 42) |
| 262 |
already-there2 |
| 263 |
(= 42 (gethash x table))))))) |
| 264 |
t) |
| 265 |
|
| 266 |
(deftest ensure-gethash.2 |
| 267 |
(let ((table (make-hash-table)) |
| 268 |
(count 0)) |
| 269 |
(multiple-value-call #'values |
| 270 |
(ensure-gethash (progn (incf count) :foo) |
| 271 |
(progn (incf count) table) |
| 272 |
(progn (incf count) :bar)) |
| 273 |
(gethash :foo table) |
| 274 |
count)) |
| 275 |
:bar nil :bar t 3) |
| 276 |
|
| 277 |
(deftest copy-hash-table.1 |
| 278 |
(let ((orig (make-hash-table :test 'eq :size 123)) |
| 279 |
(foo "foo")) |
| 280 |
(setf (gethash orig orig) t |
| 281 |
(gethash foo orig) t) |
| 282 |
(let ((eq-copy (copy-hash-table orig)) |
| 283 |
(eql-copy (copy-hash-table orig :test 'eql)) |
| 284 |
(equal-copy (copy-hash-table orig :test 'equal)) |
| 285 |
(equalp-copy (copy-hash-table orig :test 'equalp))) |
| 286 |
(list |
| 287 |
(eql (hash-table-rehash-size eq-copy) |
| 288 |
(hash-table-rehash-size orig)) |
| 289 |
(hash-table-count eql-copy) |
| 290 |
(gethash orig eq-copy) |
| 291 |
(gethash (copy-seq foo) eql-copy) |
| 292 |
(gethash foo eql-copy) |
| 293 |
(gethash (copy-seq foo) equal-copy) |
| 294 |
(gethash "FOO" equal-copy) |
| 295 |
(gethash "FOO" equalp-copy)))) |
| 296 |
(t 2 t nil t t nil t)) |
| 297 |
|
| 298 |
(deftest copy-hash-table.2 |
| 299 |
(let ((ht (make-hash-table)) |
| 300 |
(list (list :list (vector :A :B :C)))) |
| 301 |
(setf (gethash 'list ht) list) |
| 302 |
(let* ((shallow-copy (copy-hash-table ht)) |
| 303 |
(deep1-copy (copy-hash-table ht :key 'copy-list)) |
| 304 |
(list (gethash 'list ht)) |
| 305 |
(shallow-list (gethash 'list shallow-copy)) |
| 306 |
(deep1-list (gethash 'list deep1-copy))) |
| 307 |
(list (eq ht shallow-copy) |
| 308 |
(eq ht deep1-copy) |
| 309 |
(eq list shallow-list) |
| 310 |
(eq list deep1-list) ; outer list was copied. |
| 311 |
(eq (second list) (second shallow-list)) |
| 312 |
(eq (second list) (second deep1-list)) ; inner vector wasn't copied. |
| 313 |
))) |
| 314 |
(nil nil t nil t t)) |
| 315 |
|
| 316 |
(deftest maphash-keys.1 |
| 317 |
(let ((keys nil) |
| 318 |
(table (make-hash-table))) |
| 319 |
(declare (notinline maphash-keys)) |
| 320 |
(dotimes (i 10) |
| 321 |
(setf (gethash i table) t)) |
| 322 |
(maphash-keys (lambda (k) (push k keys)) table) |
| 323 |
(set-equal keys '(0 1 2 3 4 5 6 7 8 9))) |
| 324 |
t) |
| 325 |
|
| 326 |
(deftest maphash-values.1 |
| 327 |
(let ((vals nil) |
| 328 |
(table (make-hash-table))) |
| 329 |
(declare (notinline maphash-values)) |
| 330 |
(dotimes (i 10) |
| 331 |
(setf (gethash i table) (- i))) |
| 332 |
(maphash-values (lambda (v) (push v vals)) table) |
| 333 |
(set-equal vals '(0 -1 -2 -3 -4 -5 -6 -7 -8 -9))) |
| 334 |
t) |
| 335 |
|
| 336 |
(deftest hash-table-keys.1 |
| 337 |
(let ((table (make-hash-table))) |
| 338 |
(dotimes (i 10) |
| 339 |
(setf (gethash i table) t)) |
| 340 |
(set-equal (hash-table-keys table) '(0 1 2 3 4 5 6 7 8 9))) |
| 341 |
t) |
| 342 |
|
| 343 |
(deftest hash-table-values.1 |
| 344 |
(let ((table (make-hash-table))) |
| 345 |
(dotimes (i 10) |
| 346 |
(setf (gethash (gensym) table) i)) |
| 347 |
(set-equal (hash-table-values table) '(0 1 2 3 4 5 6 7 8 9))) |
| 348 |
t) |
| 349 |
|
| 350 |
(deftest hash-table-alist.1 |
| 351 |
(let ((table (make-hash-table))) |
| 352 |
(dotimes (i 10) |
| 353 |
(setf (gethash i table) (- i))) |
| 354 |
(let ((alist (hash-table-alist table))) |
| 355 |
(list (length alist) |
| 356 |
(assoc 0 alist) |
| 357 |
(assoc 3 alist) |
| 358 |
(assoc 9 alist) |
| 359 |
(assoc nil alist)))) |
| 360 |
(10 (0 . 0) (3 . -3) (9 . -9) nil)) |
| 361 |
|
| 362 |
(deftest hash-table-plist.1 |
| 363 |
(let ((table (make-hash-table))) |
| 364 |
(dotimes (i 10) |
| 365 |
(setf (gethash i table) (- i))) |
| 366 |
(let ((plist (hash-table-plist table))) |
| 367 |
(list (length plist) |
| 368 |
(getf plist 0) |
| 369 |
(getf plist 2) |
| 370 |
(getf plist 7) |
| 371 |
(getf plist nil)))) |
| 372 |
(20 0 -2 -7 nil)) |
| 373 |
|
| 374 |
(deftest alist-hash-table.1 |
| 375 |
(let* ((alist '((0 a) (1 b) (2 c))) |
| 376 |
(table (alist-hash-table alist))) |
| 377 |
(list (hash-table-count table) |
| 378 |
(gethash 0 table) |
| 379 |
(gethash 1 table) |
| 380 |
(gethash 2 table) |
| 381 |
(eq (hash-table-test-name 'eql) |
| 382 |
(hash-table-test table)))) |
| 383 |
(3 (a) (b) (c) t)) |
| 384 |
|
| 385 |
(deftest alist-hash-table.duplicate-keys |
| 386 |
(let* ((alist '((0 a) (1 b) (0 c) (1 d) (2 e))) |
| 387 |
(table (alist-hash-table alist))) |
| 388 |
(list (hash-table-count table) |
| 389 |
(gethash 0 table) |
| 390 |
(gethash 1 table) |
| 391 |
(gethash 2 table))) |
| 392 |
(3 (a) (b) (e))) |
| 393 |
|
| 394 |
(deftest plist-hash-table.1 |
| 395 |
(let* ((plist '(:a 1 :b 2 :c 3)) |
| 396 |
(table (plist-hash-table plist :test 'eq))) |
| 397 |
(list (hash-table-count table) |
| 398 |
(gethash :a table) |
| 399 |
(gethash :b table) |
| 400 |
(gethash :c table) |
| 401 |
(gethash 2 table) |
| 402 |
(gethash nil table) |
| 403 |
(eq (hash-table-test-name 'eq) |
| 404 |
(hash-table-test table)))) |
| 405 |
(3 1 2 3 nil nil t)) |
| 406 |
|
| 407 |
(deftest plist-hash-table.duplicate-keys |
| 408 |
(let* ((plist '(:a 1 :b 2 :a 3 :b 4 :c 5)) |
| 409 |
(table (plist-hash-table plist))) |
| 410 |
(list (hash-table-count table) |
| 411 |
(gethash :a table) |
| 412 |
(gethash :b table) |
| 413 |
(gethash :c table))) |
| 414 |
(3 1 2 5)) |
| 415 |
|
| 416 |
;;;; Functions |
| 417 |
|
| 418 |
(deftest disjoin.1 |
| 419 |
(let ((disjunction (disjoin (lambda (x) |
| 420 |
(and (consp x) :cons)) |
| 421 |
(lambda (x) |
| 422 |
(and (stringp x) :string))))) |
| 423 |
(list (funcall disjunction 'zot) |
| 424 |
(funcall disjunction '(foo bar)) |
| 425 |
(funcall disjunction "test"))) |
| 426 |
(nil :cons :string)) |
| 427 |
|
| 428 |
(deftest disjoin.2 |
| 429 |
(let ((disjunction (disjoin #'zerop))) |
| 430 |
(list (funcall disjunction 0) |
| 431 |
(funcall disjunction 1))) |
| 432 |
(t nil)) |
| 433 |
|
| 434 |
(deftest conjoin.1 |
| 435 |
(let ((conjunction (conjoin #'consp |
| 436 |
(lambda (x) |
| 437 |
(stringp (car x))) |
| 438 |
(lambda (x) |
| 439 |
(char (car x) 0))))) |
| 440 |
(list (funcall conjunction 'zot) |
| 441 |
(funcall conjunction '(foo)) |
| 442 |
(funcall conjunction '("foo")))) |
| 443 |
(nil nil #\f)) |
| 444 |
|
| 445 |
(deftest conjoin.2 |
| 446 |
(let ((conjunction (conjoin #'zerop))) |
| 447 |
(list (funcall conjunction 0) |
| 448 |
(funcall conjunction 1))) |
| 449 |
(t nil)) |
| 450 |
|
| 451 |
(deftest compose.1 |
| 452 |
(let ((composite (compose '1+ |
| 453 |
(lambda (x) |
| 454 |
(* x 2)) |
| 455 |
#'read-from-string))) |
| 456 |
(funcall composite "1")) |
| 457 |
3) |
| 458 |
|
| 459 |
(deftest compose.2 |
| 460 |
(let ((composite |
| 461 |
(locally (declare (notinline compose)) |
| 462 |
(compose '1+ |
| 463 |
(lambda (x) |
| 464 |
(* x 2)) |
| 465 |
#'read-from-string)))) |
| 466 |
(funcall composite "2")) |
| 467 |
5) |
| 468 |
|
| 469 |
(deftest compose.3 |
| 470 |
(let ((compose-form (funcall (compiler-macro-function 'compose) |
| 471 |
'(compose '1+ |
| 472 |
(lambda (x) |
| 473 |
(* x 2)) |
| 474 |
#'read-from-string) |
| 475 |
nil))) |
| 476 |
(let ((fun (funcall (compile nil `(lambda () ,compose-form))))) |
| 477 |
(funcall fun "3"))) |
| 478 |
7) |
| 479 |
|
| 480 |
(deftest compose.4 |
| 481 |
(let ((composite (compose #'zerop))) |
| 482 |
(list (funcall composite 0) |
| 483 |
(funcall composite 1))) |
| 484 |
(t nil)) |
| 485 |
|
| 486 |
(deftest multiple-value-compose.1 |
| 487 |
(let ((composite (multiple-value-compose |
| 488 |
#'truncate |
| 489 |
(lambda (x y) |
| 490 |
(values y x)) |
| 491 |
(lambda (x) |
| 492 |
(with-input-from-string (s x) |
| 493 |
(values (read s) (read s))))))) |
| 494 |
(multiple-value-list (funcall composite "2 7"))) |
| 495 |
(3 1)) |
| 496 |
|
| 497 |
(deftest multiple-value-compose.2 |
| 498 |
(let ((composite (locally (declare (notinline multiple-value-compose)) |
| 499 |
(multiple-value-compose |
| 500 |
#'truncate |
| 501 |
(lambda (x y) |
| 502 |
(values y x)) |
| 503 |
(lambda (x) |
| 504 |
(with-input-from-string (s x) |
| 505 |
(values (read s) (read s)))))))) |
| 506 |
(multiple-value-list (funcall composite "2 11"))) |
| 507 |
(5 1)) |
| 508 |
|
| 509 |
(deftest multiple-value-compose.3 |
| 510 |
(let ((compose-form (funcall (compiler-macro-function 'multiple-value-compose) |
| 511 |
'(multiple-value-compose |
| 512 |
#'truncate |
| 513 |
(lambda (x y) |
| 514 |
(values y x)) |
| 515 |
(lambda (x) |
| 516 |
(with-input-from-string (s x) |
| 517 |
(values (read s) (read s))))) |
| 518 |
nil))) |
| 519 |
(let ((fun (funcall (compile nil `(lambda () ,compose-form))))) |
| 520 |
(multiple-value-list (funcall fun "2 9")))) |
| 521 |
(4 1)) |
| 522 |
|
| 523 |
(deftest multiple-value-compose.4 |
| 524 |
(let ((composite (multiple-value-compose #'truncate))) |
| 525 |
(multiple-value-list (funcall composite 9 2))) |
| 526 |
(4 1)) |
| 527 |
|
| 528 |
(deftest curry.1 |
| 529 |
(let ((curried (curry '+ 3))) |
| 530 |
(funcall curried 1 5)) |
| 531 |
9) |
| 532 |
|
| 533 |
(deftest curry.2 |
| 534 |
(let ((curried (locally (declare (notinline curry)) |
| 535 |
(curry '* 2 3)))) |
| 536 |
(funcall curried 7)) |
| 537 |
42) |
| 538 |
|
| 539 |
(deftest curry.3 |
| 540 |
(let ((curried-form (funcall (compiler-macro-function 'curry) |
| 541 |
'(curry '/ 8) |
| 542 |
nil))) |
| 543 |
(let ((fun (funcall (compile nil `(lambda () ,curried-form))))) |
| 544 |
(funcall fun 2))) |
| 545 |
4) |
| 546 |
(eval-when (:compile-toplevel :load-toplevel :execute) |
| 547 |
(pushnew 'curry.3 *expected-failures*)) |
| 548 |
|
| 549 |
(deftest curry.4 |
| 550 |
(let* ((x 1) |
| 551 |
(curried (curry (progn |
| 552 |
(incf x) |
| 553 |
(lambda (y z) (* x y z))) |
| 554 |
3))) |
| 555 |
(list (funcall curried 7) |
| 556 |
(funcall curried 7) |
| 557 |
x)) |
| 558 |
(42 42 2)) |
| 559 |
|
| 560 |
(deftest rcurry.1 |
| 561 |
(let ((r (rcurry '/ 2))) |
| 562 |
(funcall r 8)) |
| 563 |
4) |
| 564 |
|
| 565 |
(deftest rcurry.2 |
| 566 |
(let* ((x 1) |
| 567 |
(curried (rcurry (progn |
| 568 |
(incf x) |
| 569 |
(lambda (y z) (* x y z))) |
| 570 |
3))) |
| 571 |
(list (funcall curried 7) |
| 572 |
(funcall curried 7) |
| 573 |
x)) |
| 574 |
(42 42 2)) |
| 575 |
|
| 576 |
(deftest named-lambda.1 |
| 577 |
(let ((fac (named-lambda fac (x) |
| 578 |
(if (> x 1) |
| 579 |
(* x (fac (- x 1))) |
| 580 |
x)))) |
| 581 |
(funcall fac 5)) |
| 582 |
120) |
| 583 |
|
| 584 |
(deftest named-lambda.2 |
| 585 |
(let ((fac (named-lambda fac (&key x) |
| 586 |
(if (> x 1) |
| 587 |
(* x (fac :x (- x 1))) |
| 588 |
x)))) |
| 589 |
(funcall fac :x 5)) |
| 590 |
120) |
| 591 |
|
| 592 |
;;;; Lists |
| 593 |
|
| 594 |
(deftest alist-plist.1 |
| 595 |
(alist-plist '((a . 1) (b . 2) (c . 3))) |
| 596 |
(a 1 b 2 c 3)) |
| 597 |
|
| 598 |
(deftest plist-alist.1 |
| 599 |
(plist-alist '(a 1 b 2 c 3)) |
| 600 |
((a . 1) (b . 2) (c . 3))) |
| 601 |
|
| 602 |
(deftest unionf.1 |
| 603 |
(let* ((list (list 1 2 3)) |
| 604 |
(orig list)) |
| 605 |
(unionf list (list 1 2 4)) |
| 606 |
(values (equal orig (list 1 2 3)) |
| 607 |
(eql (length list) 4) |
| 608 |
(set-difference list (list 1 2 3 4)) |
| 609 |
(set-difference (list 1 2 3 4) list))) |
| 610 |
t |
| 611 |
t |
| 612 |
nil |
| 613 |
nil) |
| 614 |
|
| 615 |
(deftest nunionf.1 |
| 616 |
(let ((list (list 1 2 3))) |
| 617 |
(nunionf list (list 1 2 4)) |
| 618 |
(values (eql (length list) 4) |
| 619 |
(set-difference (list 1 2 3 4) list) |
| 620 |
(set-difference list (list 1 2 3 4)))) |
| 621 |
t |
| 622 |
nil |
| 623 |
nil) |
| 624 |
|
| 625 |
(deftest appendf.1 |
| 626 |
(let* ((list (list 1 2 3)) |
| 627 |
(orig list)) |
| 628 |
(appendf list '(4 5 6) '(7 8)) |
| 629 |
(list list (eq list orig))) |
| 630 |
((1 2 3 4 5 6 7 8) nil)) |
| 631 |
|
| 632 |
(deftest nconcf.1 |
| 633 |
(let ((list1 (list 1 2 3)) |
| 634 |
(list2 (list 4 5 6))) |
| 635 |
(nconcf list1 list2 (list 7 8 9)) |
| 636 |
list1) |
| 637 |
(1 2 3 4 5 6 7 8 9)) |
| 638 |
|
| 639 |
(deftest circular-list.1 |
| 640 |
(let ((circle (circular-list 1 2 3))) |
| 641 |
(list (first circle) |
| 642 |
(second circle) |
| 643 |
(third circle) |
| 644 |
(fourth circle) |
| 645 |
(eq circle (nthcdr 3 circle)))) |
| 646 |
(1 2 3 1 t)) |
| 647 |
|
| 648 |
(deftest circular-list-p.1 |
| 649 |
(let* ((circle (circular-list 1 2 3 4)) |
| 650 |
(tree (list circle circle)) |
| 651 |
(dotted (cons circle t)) |
| 652 |
(proper (list 1 2 3 circle)) |
| 653 |
(tailcirc (list* 1 2 3 circle))) |
| 654 |
(list (circular-list-p circle) |
| 655 |
(circular-list-p tree) |
| 656 |
(circular-list-p dotted) |
| 657 |
(circular-list-p proper) |
| 658 |
(circular-list-p tailcirc))) |
| 659 |
(t nil nil nil t)) |
| 660 |
|
| 661 |
(deftest circular-list-p.2 |
| 662 |
(circular-list-p 'foo) |
| 663 |
nil) |
| 664 |
|
| 665 |
(deftest circular-tree-p.1 |
| 666 |
(let* ((circle (circular-list 1 2 3 4)) |
| 667 |
(tree1 (list circle circle)) |
| 668 |
(tree2 (let* ((level2 (list 1 nil 2)) |
| 669 |
(level1 (list level2))) |
| 670 |
(setf (second level2) level1) |
| 671 |
level1)) |
| 672 |
(dotted (cons circle t)) |
| 673 |
(proper (list 1 2 3 circle)) |
| 674 |
(tailcirc (list* 1 2 3 circle)) |
| 675 |
(quite-proper (list 1 2 3)) |
| 676 |
(quite-dotted (list 1 (cons 2 3)))) |
| 677 |
(list (circular-tree-p circle) |
| 678 |
(circular-tree-p tree1) |
| 679 |
(circular-tree-p tree2) |
| 680 |
(circular-tree-p dotted) |
| 681 |
(circular-tree-p proper) |
| 682 |
(circular-tree-p tailcirc) |
| 683 |
(circular-tree-p quite-proper) |
| 684 |
(circular-tree-p quite-dotted))) |
| 685 |
(t t t t t t nil nil)) |
| 686 |
|
| 687 |
(deftest circular-tree-p.2 |
| 688 |
(alexandria:circular-tree-p '#1=(#1#)) |
| 689 |
t) |
| 690 |
|
| 691 |
(deftest proper-list-p.1 |
| 692 |
(let ((l1 (list 1)) |
| 693 |
(l2 (list 1 2)) |
| 694 |
(l3 (cons 1 2)) |
| 695 |
(l4 (list (cons 1 2) 3)) |
| 696 |
(l5 (circular-list 1 2))) |
| 697 |
(list (proper-list-p l1) |
| 698 |
(proper-list-p l2) |
| 699 |
(proper-list-p l3) |
| 700 |
(proper-list-p l4) |
| 701 |
(proper-list-p l5))) |
| 702 |
(t t nil t nil)) |
| 703 |
|
| 704 |
(deftest proper-list-p.2 |
| 705 |
(proper-list-p '(1 2 . 3)) |
| 706 |
nil) |
| 707 |
|
| 708 |
(deftest proper-list.type.1 |
| 709 |
(let ((l1 (list 1)) |
| 710 |
(l2 (list 1 2)) |
| 711 |
(l3 (cons 1 2)) |
| 712 |
(l4 (list (cons 1 2) 3)) |
| 713 |
(l5 (circular-list 1 2))) |
| 714 |
(list (typep l1 'proper-list) |
| 715 |
(typep l2 'proper-list) |
| 716 |
(typep l3 'proper-list) |
| 717 |
(typep l4 'proper-list) |
| 718 |
(typep l5 'proper-list))) |
| 719 |
(t t nil t nil)) |
| 720 |
|
| 721 |
(deftest proper-list-length.1 |
| 722 |
(values |
| 723 |
(proper-list-length nil) |
| 724 |
(proper-list-length (list 1)) |
| 725 |
(proper-list-length (list 2 2)) |
| 726 |
(proper-list-length (list 3 3 3)) |
| 727 |
(proper-list-length (list 4 4 4 4)) |
| 728 |
(proper-list-length (list 5 5 5 5 5)) |
| 729 |
(proper-list-length (list 6 6 6 6 6 6)) |
| 730 |
(proper-list-length (list 7 7 7 7 7 7 7)) |
| 731 |
(proper-list-length (list 8 8 8 8 8 8 8 8)) |
| 732 |
(proper-list-length (list 9 9 9 9 9 9 9 9 9))) |
| 733 |
0 1 2 3 4 5 6 7 8 9) |
| 734 |
|
| 735 |
(deftest proper-list-length.2 |
| 736 |
(flet ((plength (x) |
| 737 |
(handler-case |
| 738 |
(proper-list-length x) |
| 739 |
(type-error () |
| 740 |
:ok)))) |
| 741 |
(values |
| 742 |
(plength (list* 1)) |
| 743 |
(plength (list* 2 2)) |
| 744 |
(plength (list* 3 3 3)) |
| 745 |
(plength (list* 4 4 4 4)) |
| 746 |
(plength (list* 5 5 5 5 5)) |
| 747 |
(plength (list* 6 6 6 6 6 6)) |
| 748 |
(plength (list* 7 7 7 7 7 7 7)) |
| 749 |
(plength (list* 8 8 8 8 8 8 8 8)) |
| 750 |
(plength (list* 9 9 9 9 9 9 9 9 9)))) |
| 751 |
:ok :ok :ok |
| 752 |
:ok :ok :ok |
| 753 |
:ok :ok :ok) |
| 754 |
|
| 755 |
(deftest lastcar.1 |
| 756 |
(let ((l1 (list 1)) |
| 757 |
(l2 (list 1 2))) |
| 758 |
(list (lastcar l1) |
| 759 |
(lastcar l2))) |
| 760 |
(1 2)) |
| 761 |
|
| 762 |
(deftest lastcar.error.2 |
| 763 |
(handler-case |
| 764 |
(progn |
| 765 |
(lastcar (circular-list 1 2 3)) |
| 766 |
nil) |
| 767 |
(error () |
| 768 |
t)) |
| 769 |
t) |
| 770 |
|
| 771 |
(deftest setf-lastcar.1 |
| 772 |
(let ((l (list 1 2 3 4))) |
| 773 |
(values (lastcar l) |
| 774 |
(progn |
| 775 |
(setf (lastcar l) 42) |
| 776 |
(lastcar l)))) |
| 777 |
4 |
| 778 |
42) |
| 779 |
|
| 780 |
(deftest setf-lastcar.2 |
| 781 |
(let ((l (circular-list 1 2 3))) |
| 782 |
(multiple-value-bind (res err) |
| 783 |
(ignore-errors (setf (lastcar l) 4)) |
| 784 |
(typep err 'type-error))) |
| 785 |
t) |
| 786 |
|
| 787 |
(deftest make-circular-list.1 |
| 788 |
(let ((l (make-circular-list 3 :initial-element :x))) |
| 789 |
(setf (car l) :y) |
| 790 |
(list (eq l (nthcdr 3 l)) |
| 791 |
(first l) |
| 792 |
(second l) |
| 793 |
(third l) |
| 794 |
(fourth l))) |
| 795 |
(t :y :x :x :y)) |
| 796 |
|
| 797 |
(deftest circular-list.type.1 |
| 798 |
(let* ((l1 (list 1 2 3)) |
| 799 |
(l2 (circular-list 1 2 3)) |
| 800 |
(l3 (list* 1 2 3 l2))) |
| 801 |
(list (typep l1 'circular-list) |
| 802 |
(typep l2 'circular-list) |
| 803 |
(typep l3 'circular-list))) |
| 804 |
(nil t t)) |
| 805 |
|
| 806 |
(deftest ensure-list.1 |
| 807 |
(let ((x (list 1)) |
| 808 |
(y 2)) |
| 809 |
(list (ensure-list x) |
| 810 |
(ensure-list y))) |
| 811 |
((1) (2))) |
| 812 |
|
| 813 |
(deftest ensure-cons.1 |
| 814 |
(let ((x (cons 1 2)) |
| 815 |
(y nil) |
| 816 |
(z "foo")) |
| 817 |
(values (ensure-cons x) |
| 818 |
(ensure-cons y) |
| 819 |
(ensure-cons z))) |
| 820 |
(1 . 2) |
| 821 |
(nil) |
| 822 |
("foo")) |
| 823 |
|
| 824 |
(deftest setp.1 |
| 825 |
(setp '(1)) |
| 826 |
t) |
| 827 |
|
| 828 |
(deftest setp.2 |
| 829 |
(setp nil) |
| 830 |
t) |
| 831 |
|
| 832 |
(deftest setp.3 |
| 833 |
(setp "foo") |
| 834 |
nil) |
| 835 |
|
| 836 |
(deftest setp.4 |
| 837 |
(setp '(1 2 3 1)) |
| 838 |
nil) |
| 839 |
|
| 840 |
(deftest setp.5 |
| 841 |
(setp '(1 2 3)) |
| 842 |
t) |
| 843 |
|
| 844 |
(deftest setp.6 |
| 845 |
(setp '(a :a)) |
| 846 |
t) |
| 847 |
|
| 848 |
(deftest setp.7 |
| 849 |
(setp '(a :a) :key 'character) |
| 850 |
nil) |
| 851 |
|
| 852 |
(deftest setp.8 |
| 853 |
(setp '(a :a) :key 'character :test (constantly nil)) |
| 854 |
t) |
| 855 |
|
| 856 |
(deftest set-equal.1 |
| 857 |
(set-equal '(1 2 3) '(3 1 2)) |
| 858 |
t) |
| 859 |
|
| 860 |
(deftest set-equal.2 |
| 861 |
(set-equal '("Xa") '("Xb") |
| 862 |
:test (lambda (a b) (eql (char a 0) (char b 0)))) |
| 863 |
t) |
| 864 |
|
| 865 |
(deftest set-equal.3 |
| 866 |
(set-equal '(1 2) '(4 2)) |
| 867 |
nil) |
| 868 |
|
| 869 |
(deftest set-equal.4 |
| 870 |
(set-equal '(a b c) '(:a :b :c) :key 'string :test 'equal) |
| 871 |
t) |
| 872 |
|
| 873 |
(deftest set-equal.5 |
| 874 |
(set-equal '(a d c) '(:a :b :c) :key 'string :test 'equal) |
| 875 |
nil) |
| 876 |
|
| 877 |
(deftest set-equal.6 |
| 878 |
(set-equal '(a b c) '(a b c d)) |
| 879 |
nil) |
| 880 |
|
| 881 |
(deftest map-product.1 |
| 882 |
(map-product 'cons '(2 3) '(1 4)) |
| 883 |
((2 . 1) (2 . 4) (3 . 1) (3 . 4))) |
| 884 |
|
| 885 |
(deftest map-product.2 |
| 886 |
(map-product #'cons '(2 3) '(1 4)) |
| 887 |
((2 . 1) (2 . 4) (3 . 1) (3 . 4))) |
| 888 |
|
| 889 |
(deftest flatten.1 |
| 890 |
(flatten '((1) 2 (((3 4))) ((((5)) 6)) 7)) |
| 891 |
(1 2 3 4 5 6 7)) |
| 892 |
|
| 893 |
(deftest remove-from-plist.1 |
| 894 |
(let ((orig '(a 1 b 2 c 3 d 4))) |
| 895 |
(list (remove-from-plist orig 'a 'c) |
| 896 |
(remove-from-plist orig 'b 'd) |
| 897 |
(remove-from-plist orig 'b) |
| 898 |
(remove-from-plist orig 'a) |
| 899 |
(remove-from-plist orig 'd 42 "zot") |
| 900 |
(remove-from-plist orig 'a 'b 'c 'd) |
| 901 |
(remove-from-plist orig 'a 'b 'c 'd 'x) |
| 902 |
(equal orig '(a 1 b 2 c 3 d 4)))) |
| 903 |
((b 2 d 4) |
| 904 |
(a 1 c 3) |
| 905 |
(a 1 c 3 d 4) |
| 906 |
(b 2 c 3 d 4) |
| 907 |
(a 1 b 2 c 3) |
| 908 |
nil |
| 909 |
nil |
| 910 |
t)) |
| 911 |
|
| 912 |
(deftest delete-from-plist.1 |
| 913 |
(let ((orig '(a 1 b 2 c 3 d 4 d 5))) |
| 914 |
(list (delete-from-plist (copy-list orig) 'a 'c) |
| 915 |
(delete-from-plist (copy-list orig) 'b 'd) |
| 916 |
(delete-from-plist (copy-list orig) 'b) |
| 917 |
(delete-from-plist (copy-list orig) 'a) |
| 918 |
(delete-from-plist (copy-list orig) 'd 42 "zot") |
| 919 |
(delete-from-plist (copy-list orig) 'a 'b 'c 'd) |
| 920 |
(delete-from-plist (copy-list orig) 'a 'b 'c 'd 'x) |
| 921 |
(equal orig (delete-from-plist orig)) |
| 922 |
(eq orig (delete-from-plist orig)))) |
| 923 |
((b 2 d 4 d 5) |
| 924 |
(a 1 c 3) |
| 925 |
(a 1 c 3 d 4 d 5) |
| 926 |
(b 2 c 3 d 4 d 5) |
| 927 |
(a 1 b 2 c 3) |
| 928 |
nil |
| 929 |
nil |
| 930 |
t |
| 931 |
t)) |
| 932 |
|
| 933 |
(deftest mappend.1 |
| 934 |
(mappend (compose 'list '*) '(1 2 3) '(1 2 3)) |
| 935 |
(1 4 9)) |
| 936 |
|
| 937 |
(deftest assoc-value.1 |
| 938 |
(let ((key1 '(complex key)) |
| 939 |
(key2 'simple-key) |
| 940 |
(alist '()) |
| 941 |
(result '())) |
| 942 |
(push 1 (assoc-value alist key1 :test #'equal)) |
| 943 |
(push 2 (assoc-value alist key1 :test 'equal)) |
| 944 |
(push 42 (assoc-value alist key2)) |
| 945 |
(push 43 (assoc-value alist key2 :test 'eq)) |
| 946 |
(push (assoc-value alist key1 :test #'equal) result) |
| 947 |
(push (assoc-value alist key2) result) |
| 948 |
|
| 949 |
(push 'very (rassoc-value alist (list 2 1) :test #'equal)) |
| 950 |
(push (cdr (assoc '(very complex key) alist :test #'equal)) result) |
| 951 |
result) |
| 952 |
((2 1) (43 42) (2 1))) |
| 953 |
|
| 954 |
;;;; Numbers |
| 955 |
|
| 956 |
(deftest clamp.1 |
| 957 |
(list (clamp 1.5 1 2) |
| 958 |
(clamp 2.0 1 2) |
| 959 |
(clamp 1.0 1 2) |
| 960 |
(clamp 3 1 2) |
| 961 |
(clamp 0 1 2)) |
| 962 |
(1.5 2.0 1.0 2 1)) |
| 963 |
|
| 964 |
(deftest gaussian-random.1 |
| 965 |
(let ((min -0.2) |
| 966 |
(max +0.2)) |
| 967 |
(multiple-value-bind (g1 g2) |
| 968 |
(gaussian-random min max) |
| 969 |
(values (<= min g1 max) |
| 970 |
(<= min g2 max) |
| 971 |
(/= g1 g2) ;uh |
| 972 |
))) |
| 973 |
t |
| 974 |
t |
| 975 |
t) |
| 976 |
|
| 977 |
#+sbcl |
| 978 |
(deftest gaussian-random.2 |
| 979 |
(handler-case |
| 980 |
(sb-ext:with-timeout 2 |
| 981 |
(progn |
| 982 |
(loop |
| 983 |
:repeat 10000 |
| 984 |
:do (gaussian-random 0 nil)) |
| 985 |
'done)) |
| 986 |
(sb-ext:timeout () |
| 987 |
'timed-out)) |
| 988 |
done) |
| 989 |
|
| 990 |
(deftest iota.1 |
| 991 |
(iota 3) |
| 992 |
(0 1 2)) |
| 993 |
|
| 994 |
(deftest iota.2 |
| 995 |
(iota 3 :start 0.0d0) |
| 996 |
(0.0d0 1.0d0 2.0d0)) |
| 997 |
|
| 998 |
(deftest iota.3 |
| 999 |
(iota 3 :start 2 :step 3.0) |
| 1000 |
(2.0 5.0 8.0)) |
| 1001 |
|
| 1002 |
(deftest map-iota.1 |
| 1003 |
(let (all) |
| 1004 |
(declare (notinline map-iota)) |
| 1005 |
(values (map-iota (lambda (x) (push x all)) |
| 1006 |
3 |
| 1007 |
:start 2 |
| 1008 |
:step 1.1d0) |
| 1009 |
all)) |
| 1010 |
3 |
| 1011 |
(4.2d0 3.1d0 2.0d0)) |
| 1012 |
|
| 1013 |
(deftest lerp.1 |
| 1014 |
(lerp 0.5 1 2) |
| 1015 |
1.5) |
| 1016 |
|
| 1017 |
(deftest lerp.2 |
| 1018 |
(lerp 0.1 1 2) |
| 1019 |
1.1) |
| 1020 |
|
| 1021 |
(deftest lerp.3 |
| 1022 |
(lerp 0.1 4 25) |
| 1023 |
6.1) |
| 1024 |
|
| 1025 |
(deftest mean.1 |
| 1026 |
(mean '(1 2 3)) |
| 1027 |
2) |
| 1028 |
|
| 1029 |
(deftest mean.2 |
| 1030 |
(mean '(1 2 3 4)) |
| 1031 |
5/2) |
| 1032 |
|
| 1033 |
(deftest mean.3 |
| 1034 |
(mean '(1 2 10)) |
| 1035 |
13/3) |
| 1036 |
|
| 1037 |
(deftest median.1 |
| 1038 |
(median '(100 0 99 1 98 2 97)) |
| 1039 |
97) |
| 1040 |
|
| 1041 |
(deftest median.2 |
| 1042 |
(median '(100 0 99 1 98 2 97 96)) |
| 1043 |
193/2) |
| 1044 |
|
| 1045 |
(deftest variance.1 |
| 1046 |
(variance (list 1 2 3)) |
| 1047 |
2/3) |
| 1048 |
|
| 1049 |
(deftest standard-deviation.1 |
| 1050 |
(< 0 (standard-deviation (list 1 2 3)) 1) |
| 1051 |
t) |
| 1052 |
|
| 1053 |
(deftest maxf.1 |
| 1054 |
(let ((x 1)) |
| 1055 |
(maxf x 2) |
| 1056 |
x) |
| 1057 |
2) |
| 1058 |
|
| 1059 |
(deftest maxf.2 |
| 1060 |
(let ((x 1)) |
| 1061 |
(maxf x 0) |
| 1062 |
x) |
| 1063 |
1) |
| 1064 |
|
| 1065 |
(deftest maxf.3 |
| 1066 |
(let ((x 1) |
| 1067 |
(c 0)) |
| 1068 |
(maxf x (incf c)) |
| 1069 |
(list x c)) |
| 1070 |
(1 1)) |
| 1071 |
|
| 1072 |
(deftest maxf.4 |
| 1073 |
(let ((xv (vector 0 0 0)) |
| 1074 |
(p 0)) |
| 1075 |
(maxf (svref xv (incf p)) (incf p)) |
| 1076 |
(list p xv)) |
| 1077 |
(2 #(0 2 0))) |
| 1078 |
|
| 1079 |
(deftest minf.1 |
| 1080 |
(let ((y 1)) |
| 1081 |
(minf y 0) |
| 1082 |
y) |
| 1083 |
0) |
| 1084 |
|
| 1085 |
(deftest minf.2 |
| 1086 |
(let ((xv (vector 10 10 10)) |
| 1087 |
(p 0)) |
| 1088 |
(minf (svref xv (incf p)) (incf p)) |
| 1089 |
(list p xv)) |
| 1090 |
(2 #(10 2 10))) |
| 1091 |
|
| 1092 |
(deftest subfactorial.1 |
| 1093 |
(mapcar #'subfactorial (iota 22)) |
| 1094 |
(1 |
| 1095 |
0 |
| 1096 |
1 |
| 1097 |
2 |
| 1098 |
9 |
| 1099 |
44 |
| 1100 |
265 |
| 1101 |
1854 |
| 1102 |
14833 |
| 1103 |
133496 |
| 1104 |
1334961 |
| 1105 |
14684570 |
| 1106 |
176214841 |
| 1107 |
2290792932 |
| 1108 |
32071101049 |
| 1109 |
481066515734 |
| 1110 |
7697064251745 |
| 1111 |
130850092279664 |
| 1112 |
2355301661033953 |
| 1113 |
44750731559645106 |
| 1114 |
895014631192902121 |
| 1115 |
18795307255050944540)) |
| 1116 |
|
| 1117 |
;;;; Arrays |
| 1118 |
|
| 1119 |
#+nil |
| 1120 |
(deftest array-index.type) |
| 1121 |
|
| 1122 |
#+nil |
| 1123 |
(deftest copy-array) |
| 1124 |
|
| 1125 |
;;;; Sequences |
| 1126 |
|
| 1127 |
(deftest rotate.1 |
| 1128 |
(list (rotate (list 1 2 3) 0) |
| 1129 |
(rotate (list 1 2 3) 1) |
| 1130 |
(rotate (list 1 2 3) 2) |
| 1131 |
(rotate (list 1 2 3) 3) |
| 1132 |
(rotate (list 1 2 3) 4)) |
| 1133 |
((1 2 3) |
| 1134 |
(3 1 2) |
| 1135 |
(2 3 1) |
| 1136 |
(1 2 3) |
| 1137 |
(3 1 2))) |
| 1138 |
|
| 1139 |
(deftest rotate.2 |
| 1140 |
(list (rotate (vector 1 2 3 4) 0) |
| 1141 |
(rotate (vector 1 2 3 4)) |
| 1142 |
(rotate (vector 1 2 3 4) 2) |
| 1143 |
(rotate (vector 1 2 3 4) 3) |
| 1144 |
(rotate (vector 1 2 3 4) 4) |
| 1145 |
(rotate (vector 1 2 3 4) 5)) |
| 1146 |
(#(1 2 3 4) |
| 1147 |
#(4 1 2 3) |
| 1148 |
#(3 4 1 2) |
| 1149 |
#(2 3 4 1) |
| 1150 |
#(1 2 3 4) |
| 1151 |
#(4 1 2 3))) |
| 1152 |
|
| 1153 |
(deftest rotate.3 |
| 1154 |
(list (rotate (list 1 2 3) 0) |
| 1155 |
(rotate (list 1 2 3) -1) |
| 1156 |
(rotate (list 1 2 3) -2) |
| 1157 |
(rotate (list 1 2 3) -3) |
| 1158 |
(rotate (list 1 2 3) -4)) |
| 1159 |
((1 2 3) |
| 1160 |
(2 3 1) |
| 1161 |
(3 1 2) |
| 1162 |
(1 2 3) |
| 1163 |
(2 3 1))) |
| 1164 |
|
| 1165 |
(deftest rotate.4 |
| 1166 |
(list (rotate (vector 1 2 3 4) 0) |
| 1167 |
(rotate (vector 1 2 3 4) -1) |
| 1168 |
(rotate (vector 1 2 3 4) -2) |
| 1169 |
(rotate (vector 1 2 3 4) -3) |
| 1170 |
(rotate (vector 1 2 3 4) -4) |
| 1171 |
(rotate (vector 1 2 3 4) -5)) |
| 1172 |
(#(1 2 3 4) |
| 1173 |
#(2 3 4 1) |
| 1174 |
#(3 4 1 2) |
| 1175 |
#(4 1 2 3) |
| 1176 |
#(1 2 3 4) |
| 1177 |
#(2 3 4 1))) |
| 1178 |
|
| 1179 |
(deftest rotate.5 |
| 1180 |
(values (rotate (list 1) 17) |
| 1181 |
(rotate (list 1) -5)) |
| 1182 |
(1) |
| 1183 |
(1)) |
| 1184 |
|
| 1185 |
(deftest shuffle.1 |
| 1186 |
(let ((s (shuffle (iota 100)))) |
| 1187 |
(list (equal s (iota 100)) |
| 1188 |
(every (lambda (x) |
| 1189 |
(member x s)) |
| 1190 |
(iota 100)) |
| 1191 |
(every (lambda (x) |
| 1192 |
(typep x '(integer 0 99))) |
| 1193 |
s))) |
| 1194 |
(nil t t)) |
| 1195 |
|
| 1196 |
(deftest shuffle.2 |
| 1197 |
(let ((s (shuffle (coerce (iota 100) 'vector)))) |
| 1198 |
(list (equal s (coerce (iota 100) 'vector)) |
| 1199 |
(every (lambda (x) |
| 1200 |
(find x s)) |
| 1201 |
(iota 100)) |
| 1202 |
(every (lambda (x) |
| 1203 |
(typep x '(integer 0 99))) |
| 1204 |
s))) |
| 1205 |
(nil t t)) |
| 1206 |
|
| 1207 |
(deftest shuffle.3 |
| 1208 |
(let* ((orig (coerce (iota 21) 'vector)) |
| 1209 |
(copy (copy-seq orig))) |
| 1210 |
(shuffle copy :start 10 :end 15) |
| 1211 |
(list (every #'eql (subseq copy 0 10) (subseq orig 0 10)) |
| 1212 |
(every #'eql (subseq copy 15) (subseq orig 15)))) |
| 1213 |
(t t)) |
| 1214 |
|
| 1215 |
(deftest random-elt.1 |
| 1216 |
(let ((s1 #(1 2 3 4)) |
| 1217 |
(s2 '(1 2 3 4))) |
| 1218 |
(list (dotimes (i 1000 nil) |
| 1219 |
(unless (member (random-elt s1) s2) |
| 1220 |
(return nil)) |
| 1221 |
(when (/= (random-elt s1) (random-elt s1)) |
| 1222 |
(return t))) |
| 1223 |
(dotimes (i 1000 nil) |
| 1224 |
(unless (member (random-elt s2) s2) |
| 1225 |
(return nil)) |
| 1226 |
(when (/= (random-elt s2) (random-elt s2)) |
| 1227 |
(return t))))) |
| 1228 |
(t t)) |
| 1229 |
|
| 1230 |
(deftest removef.1 |
| 1231 |
(let* ((x '(1 2 3)) |
| 1232 |
(x* x) |
| 1233 |
(y #(1 2 3)) |
| 1234 |
(y* y)) |
| 1235 |
(removef x 1) |
| 1236 |
(removef y 3) |
| 1237 |
(list x x* y y*)) |
| 1238 |
((2 3) |
| 1239 |
(1 2 3) |
| 1240 |
#(1 2) |
| 1241 |
#(1 2 3))) |
| 1242 |
|
| 1243 |
(deftest deletef.1 |
| 1244 |
(let* ((x (list 1 2 3)) |
| 1245 |
(x* x) |
| 1246 |
(y (vector 1 2 3))) |
| 1247 |
(deletef x 2) |
| 1248 |
(deletef y 1) |
| 1249 |
(list x x* y)) |
| 1250 |
((1 3) |
| 1251 |
(1 3) |
| 1252 |
#(2 3))) |
| 1253 |
|
| 1254 |
(deftest map-permutations.1 |
| 1255 |
(let ((seq (list 1 2 3)) |
| 1256 |
(seen nil) |
| 1257 |
(ok t)) |
| 1258 |
(map-permutations (lambda (s) |
| 1259 |
(unless (set-equal s seq) |
| 1260 |
(setf ok nil)) |
| 1261 |
(when (member s seen :test 'equal) |
| 1262 |
(setf ok nil)) |
| 1263 |
(push s seen)) |
| 1264 |
seq |
| 1265 |
:copy t) |
| 1266 |
(values ok (length seen))) |
| 1267 |
t |
| 1268 |
6) |
| 1269 |
|
| 1270 |
(deftest proper-sequence.type.1 |
| 1271 |
(mapcar (lambda (x) |
| 1272 |
(typep x 'proper-sequence)) |
| 1273 |
(list (list 1 2 3) |
| 1274 |
(vector 1 2 3) |
| 1275 |
#2a((1 2) (3 4)) |
| 1276 |
(circular-list 1 2 3 4))) |
| 1277 |
(t t nil nil)) |
| 1278 |
|
| 1279 |
(deftest emptyp.1 |
| 1280 |
(mapcar #'emptyp |
| 1281 |
(list (list 1) |
| 1282 |
(circular-list 1) |
| 1283 |
nil |
| 1284 |
(vector) |
| 1285 |
(vector 1))) |
| 1286 |
(nil nil t t nil)) |
| 1287 |
|
| 1288 |
(deftest sequence-of-length-p.1 |
| 1289 |
(mapcar #'sequence-of-length-p |
| 1290 |
(list nil |
| 1291 |
#() |
| 1292 |
(list 1) |
| 1293 |
(vector 1) |
| 1294 |
(list 1 2) |
| 1295 |
(vector 1 2) |
| 1296 |
(list 1 2) |
| 1297 |
(vector 1 2) |
| 1298 |
(list 1 2) |
| 1299 |
(vector 1 2)) |
| 1300 |
(list 0 |
| 1301 |
0 |
| 1302 |
1 |
| 1303 |
1 |
| 1304 |
2 |
| 1305 |
2 |
| 1306 |
1 |
| 1307 |
1 |
| 1308 |
4 |
| 1309 |
4)) |
| 1310 |
(t t t t t t nil nil nil nil)) |
| 1311 |
|
| 1312 |
(deftest length=.1 |
| 1313 |
(mapcar #'length= |
| 1314 |
(list nil |
| 1315 |
#() |
| 1316 |
(list 1) |
| 1317 |
(vector 1) |
| 1318 |
(list 1 2) |
| 1319 |
(vector 1 2) |
| 1320 |
(list 1 2) |
| 1321 |
(vector 1 2) |
| 1322 |
(list 1 2) |
| 1323 |
(vector 1 2)) |
| 1324 |
(list 0 |
| 1325 |
0 |
| 1326 |
1 |
| 1327 |
1 |
| 1328 |
2 |
| 1329 |
2 |
| 1330 |
1 |
| 1331 |
1 |
| 1332 |
4 |
| 1333 |
4)) |
| 1334 |
(t t t t t t nil nil nil nil)) |
| 1335 |
|
| 1336 |
(deftest length=.2 |
| 1337 |
;; test the compiler macro |
| 1338 |
(macrolet ((x (&rest args) |
| 1339 |
(funcall |
| 1340 |
(compile nil |
| 1341 |
`(lambda () |
| 1342 |
(length= ,@args)))))) |
| 1343 |
(list (x 2 '(1 2)) |
| 1344 |
(x '(1 2) '(3 4)) |
| 1345 |
(x '(1 2) 2) |
| 1346 |
(x '(1 2) 2 '(3 4)) |
| 1347 |
(x 1 2 3))) |
| 1348 |
(t t t t nil)) |
| 1349 |
|
| 1350 |
(deftest copy-sequence.1 |
| 1351 |
(let ((l (list 1 2 3)) |
| 1352 |
(v (vector #\a #\b #\c))) |
| 1353 |
(declare (notinline copy-sequence)) |
| 1354 |
(let ((l.list (copy-sequence 'list l)) |
| 1355 |
(l.vector (copy-sequence 'vector l)) |
| 1356 |
(l.spec-v (copy-sequence '(vector fixnum) l)) |
| 1357 |
(v.vector (copy-sequence 'vector v)) |
| 1358 |
(v.list (copy-sequence 'list v)) |
| 1359 |
(v.string (copy-sequence 'string v))) |
| 1360 |
(list (member l (list l.list l.vector l.spec-v)) |
| 1361 |
(member v (list v.vector v.list v.string)) |
| 1362 |
(equal l.list l) |
| 1363 |
(equalp l.vector #(1 2 3)) |
| 1364 |
(type= (upgraded-array-element-type 'fixnum) |
| 1365 |
(array-element-type l.spec-v)) |
| 1366 |
(equalp v.vector v) |
| 1367 |
(equal v.list '(#\a #\b #\c)) |
| 1368 |
(equal "abc" v.string)))) |
| 1369 |
(nil nil t t t t t t)) |
| 1370 |
|
| 1371 |
(deftest first-elt.1 |
| 1372 |
(mapcar #'first-elt |
| 1373 |
(list (list 1 2 3) |
| 1374 |
"abc" |
| 1375 |
(vector :a :b :c))) |
| 1376 |
(1 #\a :a)) |
| 1377 |
|
| 1378 |
(deftest first-elt.error.1 |
| 1379 |
(mapcar (lambda (x) |
| 1380 |
(handler-case |
| 1381 |
(first-elt x) |
| 1382 |
(type-error () |
| 1383 |
:type-error))) |
| 1384 |
(list nil |
| 1385 |
#() |
| 1386 |
12 |
| 1387 |
:zot)) |
| 1388 |
(:type-error |
| 1389 |
:type-error |
| 1390 |
:type-error |
| 1391 |
:type-error)) |
| 1392 |
|
| 1393 |
(deftest setf-first-elt.1 |
| 1394 |
(let ((l (list 1 2 3)) |
| 1395 |
(s (copy-seq "foobar")) |
| 1396 |
(v (vector :a :b :c))) |
| 1397 |
(setf (first-elt l) -1 |
| 1398 |
(first-elt s) #\x |
| 1399 |
(first-elt v) 'zot) |
| 1400 |
(values l s v)) |
| 1401 |
(-1 2 3) |
| 1402 |
"xoobar" |
| 1403 |
#(zot :b :c)) |
| 1404 |
|
| 1405 |
(deftest setf-first-elt.error.1 |
| 1406 |
(let ((l 'foo)) |
| 1407 |
(multiple-value-bind (res err) |
| 1408 |
(ignore-errors (setf (first-elt l) 4)) |
| 1409 |
(typep err 'type-error))) |
| 1410 |
t) |
| 1411 |
|
| 1412 |
(deftest last-elt.1 |
| 1413 |
(mapcar #'last-elt |
| 1414 |
(list (list 1 2 3) |
| 1415 |
(vector :a :b :c) |
| 1416 |
"FOOBAR" |
| 1417 |
#*001 |
| 1418 |
#*010)) |
| 1419 |
(3 :c #\R 1 0)) |
| 1420 |
|
| 1421 |
(deftest last-elt.error.1 |
| 1422 |
(mapcar (lambda (x) |
| 1423 |
(handler-case |
| 1424 |
(last-elt x) |
| 1425 |
(type-error () |
| 1426 |
:type-error))) |
| 1427 |
(list nil |
| 1428 |
#() |
| 1429 |
12 |
| 1430 |
:zot |
| 1431 |
(circular-list 1 2 3) |
| 1432 |
(list* 1 2 3 (circular-list 4 5)))) |
| 1433 |
(:type-error |
| 1434 |
:type-error |
| 1435 |
:type-error |
| 1436 |
:type-error |
| 1437 |
:type-error |
| 1438 |
:type-error)) |
| 1439 |
|
| 1440 |
(deftest setf-last-elt.1 |
| 1441 |
(let ((l (list 1 2 3)) |
| 1442 |
(s (copy-seq "foobar")) |
| 1443 |
(b (copy-seq #*010101001))) |
| 1444 |
(setf (last-elt l) '??? |
| 1445 |
(last-elt s) #\? |
| 1446 |
(last-elt b) 0) |
| 1447 |
(values l s b)) |
| 1448 |
(1 2 ???) |
| 1449 |
"fooba?" |
| 1450 |
#*010101000) |
| 1451 |
|
| 1452 |
(deftest setf-last-elt.error.1 |
| 1453 |
(handler-case |
| 1454 |
(setf (last-elt 'foo) 13) |
| 1455 |
(type-error () |
| 1456 |
:type-error)) |
| 1457 |
:type-error) |
| 1458 |
|
| 1459 |
(deftest starts-with.1 |
| 1460 |
(list (starts-with 1 '(1 2 3)) |
| 1461 |
(starts-with 1 #(1 2 3)) |
| 1462 |
(starts-with #\x "xyz") |
| 1463 |
(starts-with 2 '(1 2 3)) |
| 1464 |
(starts-with 3 #(1 2 3)) |
| 1465 |
(starts-with 1 1) |
| 1466 |
(starts-with nil nil)) |
| 1467 |
(t t t nil nil nil nil)) |
| 1468 |
|
| 1469 |
(deftest starts-with.2 |
| 1470 |
(values (starts-with 1 '(-1 2 3) :key '-) |
| 1471 |
(starts-with "foo" '("foo" "bar") :test 'equal) |
| 1472 |
(starts-with "f" '(#\f) :key 'string :test 'equal) |
| 1473 |
(starts-with -1 '(0 1 2) :key #'1+) |
| 1474 |
(starts-with "zot" '("ZOT") :test 'equal)) |
| 1475 |
t |
| 1476 |
t |
| 1477 |
t |
| 1478 |
nil |
| 1479 |
nil) |
| 1480 |
|
| 1481 |
(deftest ends-with.1 |
| 1482 |
(list (ends-with 3 '(1 2 3)) |
| 1483 |
(ends-with 3 #(1 2 3)) |
| 1484 |
(ends-with #\z "xyz") |
| 1485 |
(ends-with 2 '(1 2 3)) |
| 1486 |
(ends-with 1 #(1 2 3)) |
| 1487 |
(ends-with 1 1) |
| 1488 |
(ends-with nil nil)) |
| 1489 |
(t t t nil nil nil nil)) |
| 1490 |
|
| 1491 |
(deftest ends-with.2 |
| 1492 |
(values (ends-with 2 '(0 13 1) :key '1+) |
| 1493 |
(ends-with "foo" (vector "bar" "foo") :test 'equal) |
| 1494 |
(ends-with "X" (vector 1 2 #\X) :key 'string :test 'equal) |
| 1495 |
(ends-with "foo" "foo" :test 'equal)) |
| 1496 |
t |
| 1497 |
t |
| 1498 |
t |
| 1499 |
nil) |
| 1500 |
|
| 1501 |
(deftest ends-with.error.1 |
| 1502 |
(handler-case |
| 1503 |
(ends-with 3 (circular-list 3 3 3 1 3 3)) |
| 1504 |
(type-error () |
| 1505 |
:type-error)) |
| 1506 |
:type-error) |
| 1507 |
|
| 1508 |
(deftest sequences.passing-improper-lists |
| 1509 |
(macrolet ((signals-error-p (form) |
| 1510 |
`(handler-case |
| 1511 |
(progn ,form nil) |
| 1512 |
(type-error (e) |
| 1513 |
t))) |
| 1514 |
(cut (fn &rest args) |
| 1515 |
(with-gensyms (arg) |
| 1516 |
(print`(lambda (,arg) |
| 1517 |
(apply ,fn (list ,@(substitute arg '_ args)))))))) |
| 1518 |
(let ((circular-list (make-circular-list 5 :initial-element :foo)) |
| 1519 |
(dotted-list (list* 'a 'b 'c 'd))) |
| 1520 |
(loop for nth from 0 |
| 1521 |
for fn in (list |
| 1522 |
(cut #'lastcar _) |
| 1523 |
(cut #'rotate _ 3) |
| 1524 |
(cut #'rotate _ -3) |
| 1525 |
(cut #'shuffle _) |
| 1526 |
(cut #'random-elt _) |
| 1527 |
(cut #'last-elt _) |
| 1528 |
(cut #'ends-with :foo _)) |
| 1529 |
nconcing |
| 1530 |
(let ((on-circular-p (signals-error-p (funcall fn circular-list))) |
| 1531 |
(on-dotted-p (signals-error-p (funcall fn dotted-list)))) |
| 1532 |
(when (or (not on-circular-p) (not on-dotted-p)) |
| 1533 |
(append |
| 1534 |
(unless on-circular-p |
| 1535 |
(let ((*print-circle* t)) |
| 1536 |
(list |
| 1537 |
(format nil |
| 1538 |
"No appropriate error signalled when passing ~S to ~Ath entry." |
| 1539 |
circular-list nth)))) |
| 1540 |
(unless on-dotted-p |
| 1541 |
(list |
| 1542 |
(format nil |
| 1543 |
"No appropriate error signalled when passing ~S to ~Ath entry." |
| 1544 |
dotted-list nth))))))))) |
| 1545 |
nil) |
| 1546 |
|
| 1547 |
;;;; IO |
| 1548 |
|
| 1549 |
(deftest read-stream-content-into-string.1 |
| 1550 |
(values (with-input-from-string (stream "foo bar") |
| 1551 |
(read-stream-content-into-string stream)) |
| 1552 |
(with-input-from-string (stream "foo bar") |
| 1553 |
(read-stream-content-into-string stream :buffer-size 1)) |
| 1554 |
(with-input-from-string (stream "foo bar") |
| 1555 |
(read-stream-content-into-string stream :buffer-size 6)) |
| 1556 |
(with-input-from-string (stream "foo bar") |
| 1557 |
(read-stream-content-into-string stream :buffer-size 7))) |
| 1558 |
"foo bar" |
| 1559 |
"foo bar" |
| 1560 |
"foo bar" |
| 1561 |
"foo bar") |
| 1562 |
|
| 1563 |
(deftest read-stream-content-into-string.1-umlauts |
| 1564 |
(values (with-input-from-string (stream "föö βαρ") |
| 1565 |
(read-stream-content-into-string stream)) |
| 1566 |
(with-input-from-string (stream "föö βαρ") |
| 1567 |
(read-stream-content-into-string stream :buffer-size 1)) |
| 1568 |
(with-input-from-string (stream "föö βαρ") |
| 1569 |
(read-stream-content-into-string stream :buffer-size 6)) |
| 1570 |
(with-input-from-string (stream "föö βαρ") |
| 1571 |
(read-stream-content-into-string stream :buffer-size 7))) |
| 1572 |
"föö βαρ" |
| 1573 |
"föö βαρ" |
| 1574 |
"föö βαρ" |
| 1575 |
"föö βαρ") |
| 1576 |
|
| 1577 |
|
| 1578 |
(deftest read-stream-content-into-string.2 |
| 1579 |
(handler-case |
| 1580 |
(let ((stream (make-broadcast-stream))) |
| 1581 |
(read-stream-content-into-string stream :buffer-size 0)) |
| 1582 |
(type-error () |
| 1583 |
:type-error)) |
| 1584 |
:type-error) |
| 1585 |
|
| 1586 |
#+(or) |
| 1587 |
(defvar *octets* |
| 1588 |
(map '(simple-array (unsigned-byte 8) (7)) #'char-code "foo bar")) |
| 1589 |
|
| 1590 |
#+(or) |
| 1591 |
(deftest read-stream-content-into-byte-vector.1 |
| 1592 |
(values (with-input-from-byte-vector (stream *octets*) |
| 1593 |
(read-stream-content-into-byte-vector stream)) |
| 1594 |
(with-input-from-byte-vector (stream *octets*) |
| 1595 |
(read-stream-content-into-byte-vector stream :initial-size 1)) |
| 1596 |
(with-input-from-byte-vector (stream *octets*) |
| 1597 |
(read-stream-content-into-byte-vector stream 'alexandria::%length 6)) |
| 1598 |
(with-input-from-byte-vector (stream *octets*) |
| 1599 |
(read-stream-content-into-byte-vector stream 'alexandria::%length 3))) |
| 1600 |
*octets* |
| 1601 |
*octets* |
| 1602 |
*octets* |
| 1603 |
(subseq *octets* 0 3)) |
| 1604 |
|
| 1605 |
(deftest read-stream-content-into-byte-vector.2 |
| 1606 |
(handler-case |
| 1607 |
(let ((stream (make-broadcast-stream))) |
| 1608 |
(read-stream-content-into-byte-vector stream :initial-size 0)) |
| 1609 |
(error () |
| 1610 |
:error)) |
| 1611 |
:error) |
| 1612 |
|
| 1613 |
;;;; Macros |
| 1614 |
|
| 1615 |
(deftest with-unique-names.1 |
| 1616 |
(let ((*gensym-counter* 0)) |
| 1617 |
(let ((syms (with-unique-names (foo bar quux) |
| 1618 |
(list foo bar quux)))) |
| 1619 |
(list (find-if #'symbol-package syms) |
| 1620 |
(equal '("FOO0" "BAR1" "QUUX2") |
| 1621 |
(mapcar #'symbol-name syms))))) |
| 1622 |
(nil t)) |
| 1623 |
|
| 1624 |
(deftest with-unique-names.2 |
| 1625 |
(let ((*gensym-counter* 0)) |
| 1626 |
(let ((syms (with-unique-names ((foo "_foo_") (bar -bar-) (quux #\q)) |
| 1627 |
(list foo bar quux)))) |
| 1628 |
(list (find-if #'symbol-package syms) |
| 1629 |
(equal '("_foo_0" "-BAR-1" "q2") |
| 1630 |
(mapcar #'symbol-name syms))))) |
| 1631 |
(nil t)) |
| 1632 |
|
| 1633 |
(deftest with-unique-names.3 |
| 1634 |
(let ((*gensym-counter* 0) |
| 1635 |
(*error-output* (make-broadcast-stream))) |
| 1636 |
(multiple-value-bind (res err) |
| 1637 |
(ignore-errors |
| 1638 |
(eval |
| 1639 |
'(let ((syms |
| 1640 |
(with-unique-names ((foo "_foo_") (bar -bar-) (quux 42)) |
| 1641 |
(list foo bar quux)))) |
| 1642 |
(list (find-if #'symbol-package syms) |
| 1643 |
(equal '("_foo_0" "-BAR-1" "q2") |
| 1644 |
(mapcar #'symbol-name syms)))))) |
| 1645 |
(errorp err))) |
| 1646 |
t) |
| 1647 |
|
| 1648 |
(deftest once-only.1 |
| 1649 |
(macrolet ((cons1.good (x) |
| 1650 |
(once-only (x) |
| 1651 |
`(cons ,x ,x))) |
| 1652 |
(cons1.bad (x) |
| 1653 |
`(cons ,x ,x))) |
| 1654 |
(let ((y 0)) |
| 1655 |
(list (cons1.good (incf y)) |
| 1656 |
y |
| 1657 |
(cons1.bad (incf y)) |
| 1658 |
y))) |
| 1659 |
((1 . 1) 1 (2 . 3) 3)) |
| 1660 |
|
| 1661 |
(deftest once-only.2 |
| 1662 |
(macrolet ((cons1 (x) |
| 1663 |
(once-only ((y x)) |
| 1664 |
`(cons ,y ,y)))) |
| 1665 |
(let ((z 0)) |
| 1666 |
(list (cons1 (incf z)) |
| 1667 |
z |
| 1668 |
(cons1 (incf z))))) |
| 1669 |
((1 . 1) 1 (2 . 2))) |
| 1670 |
|
| 1671 |
(deftest parse-body.1 |
| 1672 |
(parse-body '("doc" "body") :documentation t) |
| 1673 |
("body") |
| 1674 |
nil |
| 1675 |
"doc") |
| 1676 |
|
| 1677 |
(deftest parse-body.2 |
| 1678 |
(parse-body '("body") :documentation t) |
| 1679 |
("body") |
| 1680 |
nil |
| 1681 |
nil) |
| 1682 |
|
| 1683 |
(deftest parse-body.3 |
| 1684 |
(parse-body '("doc" "body")) |
| 1685 |
("doc" "body") |
| 1686 |
nil |
| 1687 |
nil) |
| 1688 |
|
| 1689 |
(deftest parse-body.4 |
| 1690 |
(parse-body '((declare (foo)) "doc" (declare (bar)) body) :documentation t) |
| 1691 |
(body) |
| 1692 |
((declare (foo)) (declare (bar))) |
| 1693 |
"doc") |
| 1694 |
|
| 1695 |
(deftest parse-body.5 |
| 1696 |
(parse-body '((declare (foo)) "doc" (declare (bar)) body)) |
| 1697 |
("doc" (declare (bar)) body) |
| 1698 |
((declare (foo))) |
| 1699 |
nil) |
| 1700 |
|
| 1701 |
(deftest parse-body.6 |
| 1702 |
(multiple-value-bind (res err) |
| 1703 |
(ignore-errors |
| 1704 |
(parse-body '("foo" "bar" "quux") |
| 1705 |
:documentation t)) |
| 1706 |
(errorp err)) |
| 1707 |
t) |
| 1708 |
|
| 1709 |
;;;; Symbols |
| 1710 |
|
| 1711 |
(deftest ensure-symbol.1 |
| 1712 |
(ensure-symbol :cons :cl) |
| 1713 |
cons |
| 1714 |
:external) |
| 1715 |
|
| 1716 |
(deftest ensure-symbol.2 |
| 1717 |
(ensure-symbol "CONS" :alexandria) |
| 1718 |
cons |
| 1719 |
:inherited) |
| 1720 |
|
| 1721 |
(deftest ensure-symbol.3 |
| 1722 |
(ensure-symbol 'foo :keyword) |
| 1723 |
:foo |
| 1724 |
:external) |
| 1725 |
|
| 1726 |
(deftest ensure-symbol.4 |
| 1727 |
(ensure-symbol #\* :alexandria) |
| 1728 |
* |
| 1729 |
:inherited) |
| 1730 |
|
| 1731 |
(deftest format-symbol.1 |
| 1732 |
(let ((s (format-symbol nil '#:x-~d 13))) |
| 1733 |
(list (symbol-package s) |
| 1734 |
(string= (string '#:x-13) (symbol-name s)))) |
| 1735 |
(nil t)) |
| 1736 |
|
| 1737 |
(deftest format-symbol.2 |
| 1738 |
(format-symbol :keyword '#:sym-~a (string :bolic)) |
| 1739 |
:sym-bolic) |
| 1740 |
|
| 1741 |
(deftest format-symbol.3 |
| 1742 |
(let ((*package* (find-package :cl))) |
| 1743 |
(format-symbol t '#:find-~a (string 'package))) |
| 1744 |
find-package) |
| 1745 |
|
| 1746 |
(deftest make-keyword.1 |
| 1747 |
(list (make-keyword 'zot) |
| 1748 |
(make-keyword "FOO") |
| 1749 |
(make-keyword #\Q)) |
| 1750 |
(:zot :foo :q)) |
| 1751 |
|
| 1752 |
(deftest make-gensym-list.1 |
| 1753 |
(let ((*gensym-counter* 0)) |
| 1754 |
(let ((syms (make-gensym-list 3 "FOO"))) |
| 1755 |
(list (find-if 'symbol-package syms) |
| 1756 |
(equal '("FOO0" "FOO1" "FOO2") |
| 1757 |
(mapcar 'symbol-name syms))))) |
| 1758 |
(nil t)) |
| 1759 |
|
| 1760 |
(deftest make-gensym-list.2 |
| 1761 |
(let ((*gensym-counter* 0)) |
| 1762 |
(let ((syms (make-gensym-list 3))) |
| 1763 |
(list (find-if 'symbol-package syms) |
| 1764 |
(equal '("G0" "G1" "G2") |
| 1765 |
(mapcar 'symbol-name syms))))) |
| 1766 |
(nil t)) |
| 1767 |
|
| 1768 |
;;;; Type-system |
| 1769 |
|
| 1770 |
(deftest of-type.1 |
| 1771 |
(locally |
| 1772 |
(declare (notinline of-type)) |
| 1773 |
(let ((f (of-type 'string))) |
| 1774 |
(list (funcall f "foo") |
| 1775 |
(funcall f 'bar)))) |
| 1776 |
(t nil)) |
| 1777 |
|
| 1778 |
(deftest type=.1 |
| 1779 |
(type= 'string 'string) |
| 1780 |
t |
| 1781 |
t) |
| 1782 |
|
| 1783 |
(deftest type=.2 |
| 1784 |
(multiple-value-bind (= ok) |
| 1785 |
(type= 'list '(or null cons)) |
| 1786 |
(or (and = ok) |
| 1787 |
(and (not =) (not ok)))) |
| 1788 |
t) |
| 1789 |
|
| 1790 |
#-allegro |
| 1791 |
(deftest type=.3 |
| 1792 |
(multiple-value-bind (= ok) |
| 1793 |
(type= 'null '(and symbol list)) |
| 1794 |
(or (and = ok) |
| 1795 |
(and (not =) (not ok)))) |
| 1796 |
t) |
| 1797 |
|
| 1798 |
(deftest type=.4 |
| 1799 |
(nth-value 0 (type= 'string '(satisfies emptyp))) |
| 1800 |
nil) |
| 1801 |
|
| 1802 |
(deftest type=.5 |
| 1803 |
(type= 'string 'list) |
| 1804 |
nil |
| 1805 |
t) |
| 1806 |
|
| 1807 |
(defun type=.6.stringp (thing) |
| 1808 |
(not (not (stringp thing)))) |
| 1809 |
|
| 1810 |
(deftest type=.6 |
| 1811 |
(multiple-value-bind (= ok) |
| 1812 |
(type= '(or string (satisfies type=.6.stringp)) 'string) |
| 1813 |
(or (and = ok) |
| 1814 |
(and (not =) (not ok)))) |
| 1815 |
t) |
| 1816 |
|
| 1817 |
(macrolet |
| 1818 |
((test (type numbers) |
| 1819 |
`(deftest ,(format-symbol t '#:cdr5.~a (string type)) |
| 1820 |
(let ((numbers ,numbers)) |
| 1821 |
(values (mapcar (of-type ',(format-symbol t '#:negative-~a (string type))) numbers) |
| 1822 |
(mapcar (of-type ',(format-symbol t '#:non-positive-~a (string type))) numbers) |
| 1823 |
(mapcar (of-type ',(format-symbol t '#:non-negative-~a (string type))) numbers) |
| 1824 |
(mapcar (of-type ',(format-symbol t '#:positive-~a (string type))) numbers))) |
| 1825 |
(t t t nil nil nil nil) |
| 1826 |
(t t t t nil nil nil) |
| 1827 |
(nil nil nil t t t t) |
| 1828 |
(nil nil nil nil t t t)))) |
| 1829 |
(test fixnum (list most-negative-fixnum -42 -1 0 1 42 most-positive-fixnum)) |
| 1830 |
(test integer (list (1- most-negative-fixnum) -42 -1 0 1 42 (1+ most-positive-fixnum))) |
| 1831 |
(test rational (list (1- most-negative-fixnum) -42/13 -1 0 1 42/13 (1+ most-positive-fixnum))) |
| 1832 |
(test real (list most-negative-long-float -42/13 -1 0 1 42/13 most-positive-long-float)) |
| 1833 |
(test float (list most-negative-short-float -42.02 -1.0 0.0 1.0 42.02 most-positive-short-float)) |
| 1834 |
(test short-float (list most-negative-short-float -42.02s0 -1.0s0 0.0s0 1.0s0 42.02s0 most-positive-short-float)) |
| 1835 |
(test single-float (list most-negative-single-float -42.02f0 -1.0f0 0.0f0 1.0f0 42.02f0 most-positive-single-float)) |
| 1836 |
(test double-float (list most-negative-double-float -42.02d0 -1.0d0 0.0d0 1.0d0 42.02d0 most-positive-double-float)) |
| 1837 |
(test long-float (list most-negative-long-float -42.02l0 -1.0l0 0.0l0 1.0l0 42.02l0 most-positive-long-float))) |
| 1838 |
|
| 1839 |
;;;; Bindings |
| 1840 |
|
| 1841 |
(declaim (notinline opaque)) |
| 1842 |
(defun opaque (x) |
| 1843 |
x) |
| 1844 |
|
| 1845 |
(deftest if-let.1 |
| 1846 |
(if-let (x (opaque :ok)) |
| 1847 |
x |
| 1848 |
:bad) |
| 1849 |
:ok) |
| 1850 |
|
| 1851 |
(deftest if-let.2 |
| 1852 |
(if-let (x (opaque nil)) |
| 1853 |
:bad |
| 1854 |
(and (not x) :ok)) |
| 1855 |
:ok) |
| 1856 |
|
| 1857 |
(deftest if-let.3 |
| 1858 |
(let ((x 1)) |
| 1859 |
(if-let ((x 2) |
| 1860 |
(y x)) |
| 1861 |
(+ x y) |
| 1862 |
:oops)) |
| 1863 |
3) |
| 1864 |
|
| 1865 |
(deftest if-let.4 |
| 1866 |
(if-let ((x 1) |
| 1867 |
(y nil)) |
| 1868 |
:oops |
| 1869 |
(and (not y) x)) |
| 1870 |
1) |
| 1871 |
|
| 1872 |
(deftest if-let.5 |
| 1873 |
(if-let (x) |
| 1874 |
:oops |
| 1875 |
(not x)) |
| 1876 |
t) |
| 1877 |
|
| 1878 |
(deftest if-let.error.1 |
| 1879 |
(handler-case |
| 1880 |
(eval '(if-let x |
| 1881 |
:oops |
| 1882 |
:oops)) |
| 1883 |
(type-error () |
| 1884 |
:type-error)) |
| 1885 |
:type-error) |
| 1886 |
|
| 1887 |
(deftest when-let.1 |
| 1888 |
(when-let (x (opaque :ok)) |
| 1889 |
(setf x (cons x x)) |
| 1890 |
x) |
| 1891 |
(:ok . :ok)) |
| 1892 |
|
| 1893 |
(deftest when-let.2 |
| 1894 |
(when-let ((x 1) |
| 1895 |
(y nil) |
| 1896 |
(z 3)) |
| 1897 |
:oops) |
| 1898 |
nil) |
| 1899 |
|
| 1900 |
(deftest when-let.3 |
| 1901 |
(let ((x 1)) |
| 1902 |
(when-let ((x 2) |
| 1903 |
(y x)) |
| 1904 |
(+ x y))) |
| 1905 |
3) |
| 1906 |
|
| 1907 |
(deftest when-let.error.1 |
| 1908 |
(handler-case |
| 1909 |
(eval '(when-let x :oops)) |
| 1910 |
(type-error () |
| 1911 |
:type-error)) |
| 1912 |
:type-error) |
| 1913 |
|
| 1914 |
(deftest when-let*.1 |
| 1915 |
(let ((x 1)) |
| 1916 |
(when-let* ((x 2) |
| 1917 |
(y x)) |
| 1918 |
(+ x y))) |
| 1919 |
4) |
| 1920 |
|
| 1921 |
(deftest when-let*.2 |
| 1922 |
(let ((y 1)) |
| 1923 |
(when-let* (x y) |
| 1924 |
(1+ x))) |
| 1925 |
2) |
| 1926 |
|
| 1927 |
(deftest when-let*.3 |
| 1928 |
(when-let* ((x t) |
| 1929 |
(y (consp x)) |
| 1930 |
(z (error "OOPS"))) |
| 1931 |
t) |
| 1932 |
nil) |
| 1933 |
|
| 1934 |
(deftest when-let*.error.1 |
| 1935 |
(handler-case |
| 1936 |
(eval '(when-let* x :oops)) |
| 1937 |
(type-error () |
| 1938 |
:type-error)) |
| 1939 |
:type-error) |
| 1940 |
|
| 1941 |
(deftest doplist.1 |
| 1942 |
(let (keys values) |
| 1943 |
(doplist (k v '(a 1 b 2 c 3) (values t (reverse keys) (reverse values) k v)) |
| 1944 |
(push k keys) |
| 1945 |
(push v values))) |
| 1946 |
t |
| 1947 |
(a b c) |
| 1948 |
(1 2 3) |
| 1949 |
nil |
| 1950 |
nil) |
| 1951 |
|
| 1952 |
(deftest count-permutations.1 |
| 1953 |
(values (count-permutations 31 7) |
| 1954 |
(count-permutations 1 1) |
| 1955 |
(count-permutations 2 1) |
| 1956 |
(count-permutations 2 2) |
| 1957 |
(count-permutations 3 2) |
| 1958 |
(count-permutations 3 1)) |
| 1959 |
13253058000 |
| 1960 |
1 |
| 1961 |
2 |
| 1962 |
2 |
| 1963 |
6 |
| 1964 |
3) |
| 1965 |
|
| 1966 |
(deftest binomial-coefficient.1 |
| 1967 |
(alexandria:binomial-coefficient 1239 139) |
| 1968 |
28794902202288970200771694600561826718847179309929858835480006683522184441358211423695124921058123706380656375919763349913245306834194782172712255592710204598527867804110129489943080460154) |
| 1969 |
|
| 1970 |
;; Exercise bignum case (at least on x86). |
| 1971 |
(deftest binomial-coefficient.2 |
| 1972 |
(alexandria:binomial-coefficient 2000000000000 20) |
| 1973 |
430998041177272843950422879590338454856322722740402365741730748431530623813012487773080486408378680853987520854296499536311275320016878730999689934464711239072435565454954447356845336730100919970769793030177499999999900000000000) |
| 1974 |
|
| 1975 |
(deftest copy-stream.1 |
| 1976 |
(let ((data "sdkfjhsakfh weior763495ewofhsdfk sdfadlkfjhsadf woif sdlkjfhslkdfh sdklfjh")) |
| 1977 |
(values (equal data |
| 1978 |
(with-input-from-string (in data) |
| 1979 |
(with-output-to-string (out) |
| 1980 |
(alexandria:copy-stream in out)))) |
| 1981 |
(equal (subseq data 10 20) |
| 1982 |
(with-input-from-string (in data) |
| 1983 |
(with-output-to-string (out) |
| 1984 |
(alexandria:copy-stream in out :start 10 :end 20)))) |
| 1985 |
(equal (subseq data 10) |
| 1986 |
(with-input-from-string (in data) |
| 1987 |
(with-output-to-string (out) |
| 1988 |
(alexandria:copy-stream in out :start 10)))) |
| 1989 |
(equal (subseq data 0 20) |
| 1990 |
(with-input-from-string (in data) |
| 1991 |
(with-output-to-string (out) |
| 1992 |
(alexandria:copy-stream in out :end 20)))))) |
| 1993 |
t |
| 1994 |
t |
| 1995 |
t |
| 1996 |
t) |
| 1997 |
|
| 1998 |
(deftest extremum.1 |
| 1999 |
(let ((n 0)) |
| 2000 |
(dotimes (i 10) |
| 2001 |
(let ((data (shuffle (coerce (iota 10000 :start i) 'vector))) |
| 2002 |
(ok t)) |
| 2003 |
(unless (eql i (extremum data #'<)) |
| 2004 |
(setf ok nil)) |
| 2005 |
(unless (eql i (extremum (coerce data 'list) #'<)) |
| 2006 |
(setf ok nil)) |
| 2007 |
(unless (eql (+ 9999 i) (extremum data #'>)) |
| 2008 |
(setf ok nil)) |
| 2009 |
(unless (eql (+ 9999 i) (extremum (coerce data 'list) #'>)) |
| 2010 |
(setf ok nil)) |
| 2011 |
(when ok |
| 2012 |
(incf n)))) |
| 2013 |
(when (eql 10 (extremum #(100 1 10 1000) #'> :start 1 :end 3)) |
| 2014 |
(incf n)) |
| 2015 |
(when (eql -1000 (extremum #(100 1 10 -1000) #'> :key 'abs)) |
| 2016 |
(incf n)) |
| 2017 |
(when (eq nil (extremum "" (lambda (a b) (error "wtf? ~S, ~S" a b)))) |
| 2018 |
(incf n)) |
| 2019 |
n) |
| 2020 |
13) |
| 2021 |
|
| 2022 |
(deftest starts-with-subseq.string |
| 2023 |
(starts-with-subseq "f" "foo" :return-suffix t) |
| 2024 |
t |
| 2025 |
"oo") |
| 2026 |
|
| 2027 |
(deftest starts-with-subseq.vector |
| 2028 |
(starts-with-subseq #(1) #(1 2 3) :return-suffix t) |
| 2029 |
t |
| 2030 |
#(2 3)) |
| 2031 |
|
| 2032 |
(deftest starts-with-subseq.list |
| 2033 |
(starts-with-subseq '(1) '(1 2 3) :return-suffix t) |
| 2034 |
t |
| 2035 |
(2 3)) |
| 2036 |
|
| 2037 |
(deftest starts-with-subseq.start1 |
| 2038 |
(starts-with-subseq "foo" "oop" :start1 1) |
| 2039 |
t |
| 2040 |
nil) |
| 2041 |
|
| 2042 |
(deftest starts-with-subseq.start2 |
| 2043 |
(starts-with-subseq "foo" "xfoop" :start2 1) |
| 2044 |
t |
| 2045 |
nil) |
| 2046 |
|
| 2047 |
(deftest format-symbol.print-case-bound |
| 2048 |
(let ((upper (intern "FOO-BAR")) |
| 2049 |
(lower (intern "foo-bar")) |
| 2050 |
(*print-escape* nil)) |
| 2051 |
(values |
| 2052 |
(let ((*print-case* :downcase)) |
| 2053 |
(and (eq upper (format-symbol t "~A" upper)) |
| 2054 |
(eq lower (format-symbol t "~A" lower)))) |
| 2055 |
(let ((*print-case* :upcase)) |
| 2056 |
(and (eq upper (format-symbol t "~A" upper)) |
| 2057 |
(eq lower (format-symbol t "~A" lower)))) |
| 2058 |
(let ((*print-case* :capitalize)) |
| 2059 |
(and (eq upper (format-symbol t "~A" upper)) |
| 2060 |
(eq lower (format-symbol t "~A" lower)))))) |
| 2061 |
t |
| 2062 |
t |
| 2063 |
t) |
| 2064 |
|
| 2065 |
;; In CLISP, (eql #C(2 0) 2) => T |
| 2066 |
;; but (eql #C(2 0) 2.0) => NIL |
| 2067 |
;; so we need a complex start point |
| 2068 |
(deftest iota.fp-start-and-complex-integer-step |
| 2069 |
(equal '(#C(0.0 0.0) #C(0.0 2.0) #C(0.0 4.0)) |
| 2070 |
(iota 3 :start #C(0.0 0.0) :step #C(0 2))) |
| 2071 |
t) |
| 2072 |
|
| 2073 |
(deftest parse-ordinary-lambda-list.1 |
| 2074 |
(multiple-value-bind (req opt rest keys allowp aux keyp) |
| 2075 |
(parse-ordinary-lambda-list '(a b c |
| 2076 |
&optional o1 (o2 42) (o3 42 o3-supplied?) |
| 2077 |
&key (k1) ((:key k2)) (k3 42 k3-supplied?)) |
| 2078 |
:normalize t) |
| 2079 |
(and (equal '(a b c) req) |
| 2080 |
(equal '((o1 nil nil) |
| 2081 |
(o2 42 nil) |
| 2082 |
(o3 42 o3-supplied?)) |
| 2083 |
opt) |
| 2084 |
(equal '(((:k1 k1) nil nil) |
| 2085 |
((:key k2) nil nil) |
| 2086 |
((:k3 k3) 42 k3-supplied?)) |
| 2087 |
keys) |
| 2088 |
(not allowp) |
| 2089 |
(not aux) |
| 2090 |
(eq t keyp))) |
| 2091 |
t) |