| 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 |
;;;; Arrays |
|---|
| 14 |
|
|---|
| 15 |
(deftest copy-array.1 |
|---|
| 16 |
(let* ((orig (vector 1 2 3)) |
|---|
| 17 |
(copy (copy-array orig))) |
|---|
| 18 |
(values (eq orig copy) (equalp orig copy))) |
|---|
| 19 |
nil t) |
|---|
| 20 |
|
|---|
| 21 |
(deftest copy-array.2 |
|---|
| 22 |
(let ((orig (make-array 1024 :fill-pointer 0))) |
|---|
| 23 |
(vector-push-extend 1 orig) |
|---|
| 24 |
(vector-push-extend 2 orig) |
|---|
| 25 |
(vector-push-extend 3 orig) |
|---|
| 26 |
(let ((copy (copy-array orig))) |
|---|
| 27 |
(values (eq orig copy) (equalp orig copy) |
|---|
| 28 |
(array-has-fill-pointer-p copy) |
|---|
| 29 |
(eql (fill-pointer orig) (fill-pointer copy))))) |
|---|
| 30 |
nil t t t) |
|---|
| 31 |
|
|---|
| 32 |
(deftest array-index.1 |
|---|
| 33 |
(typep 0 'array-index) |
|---|
| 34 |
t) |
|---|
| 35 |
|
|---|
| 36 |
;;;; Conditions |
|---|
| 37 |
|
|---|
| 38 |
(deftest unwind-protect-case.1 |
|---|
| 39 |
(let (result) |
|---|
| 40 |
(unwind-protect-case () |
|---|
| 41 |
(random 10) |
|---|
| 42 |
(:normal (push :normal result)) |
|---|
| 43 |
(:abort (push :abort result)) |
|---|
| 44 |
(:always (push :always result))) |
|---|
| 45 |
result) |
|---|
| 46 |
(:always :normal)) |
|---|
| 47 |
|
|---|
| 48 |
(deftest unwind-protect-case.2 |
|---|
| 49 |
(let (result) |
|---|
| 50 |
(unwind-protect-case () |
|---|
| 51 |
(random 10) |
|---|
| 52 |
(:always (push :always result)) |
|---|
| 53 |
(:normal (push :normal result)) |
|---|
| 54 |
(:abort (push :abort result))) |
|---|
| 55 |
result) |
|---|
| 56 |
(:normal :always)) |
|---|
| 57 |
|
|---|
| 58 |
(deftest unwind-protect-case.3 |
|---|
| 59 |
(let (result1 result2 result3) |
|---|
| 60 |
(ignore-errors |
|---|
| 61 |
(unwind-protect-case () |
|---|
| 62 |
(error "FOOF!") |
|---|
| 63 |
(:normal (push :normal result1)) |
|---|
| 64 |
(:abort (push :abort result1)) |
|---|
| 65 |
(:always (push :always result1)))) |
|---|
| 66 |
(catch 'foof |
|---|
| 67 |
(unwind-protect-case () |
|---|
| 68 |
(throw 'foof 42) |
|---|
| 69 |
(:normal (push :normal result2)) |
|---|
| 70 |
(:abort (push :abort result2)) |
|---|
| 71 |
(:always (push :always result2)))) |
|---|
| 72 |
(block foof |
|---|
| 73 |
(unwind-protect-case () |
|---|
| 74 |
(return-from foof 42) |
|---|
| 75 |
(:normal (push :normal result3)) |
|---|
| 76 |
(:abort (push :abort result3)) |
|---|
| 77 |
(:always (push :always result3)))) |
|---|
| 78 |
(values result1 result2 result3)) |
|---|
| 79 |
(:always :abort) |
|---|
| 80 |
(:always :abort) |
|---|
| 81 |
(:always :abort)) |
|---|
| 82 |
|
|---|
| 83 |
(deftest unwind-protect-case.4 |
|---|
| 84 |
(let (result) |
|---|
| 85 |
(unwind-protect-case (aborted-p) |
|---|
| 86 |
(random 42) |
|---|
| 87 |
(:always (setq result aborted-p))) |
|---|
| 88 |
result) |
|---|
| 89 |
nil) |
|---|
| 90 |
|
|---|
| 91 |
(deftest unwind-protect-case.5 |
|---|
| 92 |
(let (result) |
|---|
| 93 |
(block foof |
|---|
| 94 |
(unwind-protect-case (aborted-p) |
|---|
| 95 |
(return-from foof) |
|---|
| 96 |
(:always (setq result aborted-p)))) |
|---|
| 97 |
result) |
|---|
| 98 |
t) |
|---|
| 99 |
|
|---|
| 100 |
;;;; Control flow |
|---|
| 101 |
|
|---|
| 102 |
(deftest switch.1 |
|---|
| 103 |
(switch (13 :test =) |
|---|
| 104 |
(12 :oops) |
|---|
| 105 |
(13.0 :yay)) |
|---|
| 106 |
:yay) |
|---|
| 107 |
|
|---|
| 108 |
(deftest switch.2 |
|---|
| 109 |
(switch (13) |
|---|
| 110 |
((+ 12 2) :oops) |
|---|
| 111 |
((- 13 1) :oops2) |
|---|
| 112 |
(t :yay)) |
|---|
| 113 |
:yay) |
|---|
| 114 |
|
|---|
| 115 |
(deftest eswitch.1 |
|---|
| 116 |
(let ((x 13)) |
|---|
| 117 |
(eswitch (x :test =) |
|---|
| 118 |
(12 :oops) |
|---|
| 119 |
(13.0 :yay))) |
|---|
| 120 |
:yay) |
|---|
| 121 |
|
|---|
| 122 |
(deftest eswitch.2 |
|---|
| 123 |
(let ((x 13)) |
|---|
| 124 |
(eswitch (x :key 1+) |
|---|
| 125 |
(11 :oops) |
|---|
| 126 |
(14 :yay))) |
|---|
| 127 |
:yay) |
|---|
| 128 |
|
|---|
| 129 |
(deftest cswitch.1 |
|---|
| 130 |
(cswitch (13 :test =) |
|---|
| 131 |
(12 :oops) |
|---|
| 132 |
(13.0 :yay)) |
|---|
| 133 |
:yay) |
|---|
| 134 |
|
|---|
| 135 |
(deftest cswitch.2 |
|---|
| 136 |
(cswitch (13 :key 1-) |
|---|
| 137 |
(12 :yay) |
|---|
| 138 |
(13.0 :oops)) |
|---|
| 139 |
:yay) |
|---|
| 140 |
|
|---|
| 141 |
(deftest whichever.1 |
|---|
| 142 |
(let ((x (whichever 1 2 3))) |
|---|
| 143 |
(and (member x '(1 2 3)) t)) |
|---|
| 144 |
t) |
|---|
| 145 |
|
|---|
| 146 |
(deftest whichever.2 |
|---|
| 147 |
(let* ((a 1) |
|---|
| 148 |
(b 2) |
|---|
| 149 |
(c 3) |
|---|
| 150 |
(x (whichever a b c))) |
|---|
| 151 |
(and (member x '(1 2 3)) t)) |
|---|
| 152 |
t) |
|---|
| 153 |
|
|---|
| 154 |
(deftest xor.1 |
|---|
| 155 |
(xor nil nil 1 nil) |
|---|
| 156 |
1 |
|---|
| 157 |
t) |
|---|
| 158 |
|
|---|
| 159 |
;;;; Definitions |
|---|
| 160 |
|
|---|
| 161 |
(deftest define-constant.1 |
|---|
| 162 |
(let ((name (gensym))) |
|---|
| 163 |
(eval `(define-constant ,name "FOO" :test 'equal)) |
|---|
| 164 |
(eval `(define-constant ,name "FOO" :test 'equal)) |
|---|
| 165 |
(values (equal "FOO" (symbol-value name)) |
|---|
| 166 |
(constantp name))) |
|---|
| 167 |
t |
|---|
| 168 |
t) |
|---|
| 169 |
|
|---|
| 170 |
(deftest define-constant.2 |
|---|
| 171 |
(let ((name (gensym))) |
|---|
| 172 |
(eval `(define-constant ,name 13)) |
|---|
| 173 |
(eval `(define-constant ,name 13)) |
|---|
| 174 |
(values (eql 13 (symbol-value name)) |
|---|
| 175 |
(constantp name))) |
|---|
| 176 |
t |
|---|
| 177 |
t) |
|---|
| 178 |
|
|---|
| 179 |
;;;; Errors |
|---|
| 180 |
|
|---|
| 181 |
;;; TYPEP is specified to return a generalized boolean and, for |
|---|
| 182 |
;;; example, ECL exploits this by returning the superclasses of ERROR |
|---|
| 183 |
;;; in this case. |
|---|
| 184 |
(defun errorp (x) |
|---|
| 185 |
(not (null (typep x 'error)))) |
|---|
| 186 |
|
|---|
| 187 |
(deftest required-argument.1 |
|---|
| 188 |
(multiple-value-bind (res err) |
|---|
| 189 |
(ignore-errors (required-argument)) |
|---|
| 190 |
(errorp err)) |
|---|
| 191 |
t) |
|---|
| 192 |
|
|---|
| 193 |
;;;; Hash tables |
|---|
| 194 |
|
|---|
| 195 |
(deftest ensure-hash-table.1 |
|---|
| 196 |
(let ((table (make-hash-table)) |
|---|
| 197 |
(x (list 1))) |
|---|
| 198 |
(multiple-value-bind (value already-there) |
|---|
| 199 |
(ensure-gethash x table 42) |
|---|
| 200 |
(and (= value 42) |
|---|
| 201 |
(not already-there) |
|---|
| 202 |
(= 42 (gethash x table)) |
|---|
| 203 |
(multiple-value-bind (value2 already-there2) |
|---|
| 204 |
(ensure-gethash x table 13) |
|---|
| 205 |
(and (= value2 42) |
|---|
| 206 |
already-there2 |
|---|
| 207 |
(= 42 (gethash x table))))))) |
|---|
| 208 |
t) |
|---|
| 209 |
|
|---|
| 210 |
#+clisp (pushnew 'copy-hash-table.1 *expected-failures*) |
|---|
| 211 |
|
|---|
| 212 |
(deftest copy-hash-table.1 |
|---|
| 213 |
(let ((orig (make-hash-table :test 'eq :size 123)) |
|---|
| 214 |
(foo "foo")) |
|---|
| 215 |
(setf (gethash orig orig) t |
|---|
| 216 |
(gethash foo orig) t) |
|---|
| 217 |
(let ((eq-copy (copy-hash-table orig)) |
|---|
| 218 |
(eql-copy (copy-hash-table orig :test 'eql)) |
|---|
| 219 |
(equal-copy (copy-hash-table orig :test 'equal)) |
|---|
| 220 |
;; CLISP overflows the stack with this bit. |
|---|
| 221 |
;; See <http://sourceforge.net/tracker/index.php?func=detail&aid=2029069&group_id=1355&atid=101355>. |
|---|
| 222 |
#-clisp (equalp-copy (copy-hash-table orig :test 'equalp))) |
|---|
| 223 |
(list (eql (hash-table-size eq-copy) (hash-table-size orig)) |
|---|
| 224 |
(eql (hash-table-rehash-size eq-copy) |
|---|
| 225 |
(hash-table-rehash-size orig)) |
|---|
| 226 |
(hash-table-count eql-copy) |
|---|
| 227 |
(gethash orig eq-copy) |
|---|
| 228 |
(gethash (copy-seq foo) eql-copy) |
|---|
| 229 |
(gethash foo eql-copy) |
|---|
| 230 |
(gethash (copy-seq foo) equal-copy) |
|---|
| 231 |
(gethash "FOO" equal-copy) |
|---|
| 232 |
#-clisp (gethash "FOO" equalp-copy)))) |
|---|
| 233 |
(t t 2 t nil t t nil t)) |
|---|
| 234 |
|
|---|
| 235 |
(deftest copy-hash-table.2 |
|---|
| 236 |
(let ((ht (make-hash-table)) |
|---|
| 237 |
(list (list :list (vector :A :B :C)))) |
|---|
| 238 |
(setf (gethash 'list ht) list) |
|---|
| 239 |
(let* ((shallow-copy (copy-hash-table ht)) |
|---|
| 240 |
(deep1-copy (copy-hash-table ht :key 'copy-list)) |
|---|
| 241 |
(list (gethash 'list ht)) |
|---|
| 242 |
(shallow-list (gethash 'list shallow-copy)) |
|---|
| 243 |
(deep1-list (gethash 'list deep1-copy))) |
|---|
| 244 |
(list (eq ht shallow-copy) |
|---|
| 245 |
(eq ht deep1-copy) |
|---|
| 246 |
(eq list shallow-list) |
|---|
| 247 |
(eq list deep1-list) ; outer list was copied. |
|---|
| 248 |
(eq (second list) (second shallow-list)) |
|---|
| 249 |
(eq (second list) (second deep1-list)) ; inner vector wasn't copied. |
|---|
| 250 |
))) |
|---|
| 251 |
(nil nil t nil t t)) |
|---|
| 252 |
|
|---|
| 253 |
(deftest maphash-keys.1 |
|---|
| 254 |
(let ((keys nil) |
|---|
| 255 |
(table (make-hash-table))) |
|---|
| 256 |
(declare (notinline maphash-keys)) |
|---|
| 257 |
(dotimes (i 10) |
|---|
| 258 |
(setf (gethash i table) t)) |
|---|
| 259 |
(maphash-keys (lambda (k) (push k keys)) table) |
|---|
| 260 |
(set-equal keys '(0 1 2 3 4 5 6 7 8 9))) |
|---|
| 261 |
t) |
|---|
| 262 |
|
|---|
| 263 |
(deftest maphash-values.1 |
|---|
| 264 |
(let ((vals nil) |
|---|
| 265 |
(table (make-hash-table))) |
|---|
| 266 |
(declare (notinline maphash-values)) |
|---|
| 267 |
(dotimes (i 10) |
|---|
| 268 |
(setf (gethash i table) (- i))) |
|---|
| 269 |
(maphash-values (lambda (v) (push v vals)) table) |
|---|
| 270 |
(set-equal vals '(0 -1 -2 -3 -4 -5 -6 -7 -8 -9))) |
|---|
| 271 |
t) |
|---|
| 272 |
|
|---|
| 273 |
(deftest hash-table-keys.1 |
|---|
| 274 |
(let ((table (make-hash-table))) |
|---|
| 275 |
(dotimes (i 10) |
|---|
| 276 |
(setf (gethash i table) t)) |
|---|
| 277 |
(set-equal (hash-table-keys table) '(0 1 2 3 4 5 6 7 8 9))) |
|---|
| 278 |
t) |
|---|
| 279 |
|
|---|
| 280 |
(deftest hash-table-values.1 |
|---|
| 281 |
(let ((table (make-hash-table))) |
|---|
| 282 |
(dotimes (i 10) |
|---|
| 283 |
(setf (gethash (gensym) table) i)) |
|---|
| 284 |
(set-equal (hash-table-values table) '(0 1 2 3 4 5 6 7 8 9))) |
|---|
| 285 |
t) |
|---|
| 286 |
|
|---|
| 287 |
(deftest hash-table-alist.1 |
|---|
| 288 |
(let ((table (make-hash-table))) |
|---|
| 289 |
(dotimes (i 10) |
|---|
| 290 |
(setf (gethash i table) (- i))) |
|---|
| 291 |
(let ((alist (hash-table-alist table))) |
|---|
| 292 |
(list (length alist) |
|---|
| 293 |
(assoc 0 alist) |
|---|
| 294 |
(assoc 3 alist) |
|---|
| 295 |
(assoc 9 alist) |
|---|
| 296 |
(assoc nil alist)))) |
|---|
| 297 |
(10 (0 . 0) (3 . -3) (9 . -9) nil)) |
|---|
| 298 |
|
|---|
| 299 |
(deftest hash-table-plist.1 |
|---|
| 300 |
(let ((table (make-hash-table))) |
|---|
| 301 |
(dotimes (i 10) |
|---|
| 302 |
(setf (gethash i table) (- i))) |
|---|
| 303 |
(let ((plist (hash-table-plist table))) |
|---|
| 304 |
(list (length plist) |
|---|
| 305 |
(getf plist 0) |
|---|
| 306 |
(getf plist 2) |
|---|
| 307 |
(getf plist 7) |
|---|
| 308 |
(getf plist nil)))) |
|---|
| 309 |
(20 0 -2 -7 nil)) |
|---|
| 310 |
|
|---|
| 311 |
#+clisp (pushnew 'alist-hash-table.1 *expected-failures*) |
|---|
| 312 |
|
|---|
| 313 |
(deftest alist-hash-table.1 |
|---|
| 314 |
(let* ((alist '((0 a) (1 b) (2 c))) |
|---|
| 315 |
(table (alist-hash-table alist))) |
|---|
| 316 |
(list (hash-table-count table) |
|---|
| 317 |
(gethash 0 table) |
|---|
| 318 |
(gethash 1 table) |
|---|
| 319 |
(gethash 2 table) |
|---|
| 320 |
(hash-table-test table))) ; CLISP returns EXT:FASTHASH-EQL. |
|---|
| 321 |
(3 (a) (b) (c) eql)) |
|---|
| 322 |
|
|---|
| 323 |
#+clisp (pushnew 'plist-hash-table.1 *expected-failures*) |
|---|
| 324 |
|
|---|
| 325 |
(deftest plist-hash-table.1 |
|---|
| 326 |
(let* ((plist '(:a 1 :b 2 :c 3)) |
|---|
| 327 |
(table (plist-hash-table plist :test 'eq))) |
|---|
| 328 |
(list (hash-table-count table) |
|---|
| 329 |
(gethash :a table) |
|---|
| 330 |
(gethash :b table) |
|---|
| 331 |
(gethash :c table) |
|---|
| 332 |
(gethash 2 table) |
|---|
| 333 |
(gethash nil table) |
|---|
| 334 |
(hash-table-test table))) ; CLISP returns EXT:FASTHASH-EQ. |
|---|
| 335 |
(3 1 2 3 nil nil eq)) |
|---|
| 336 |
|
|---|
| 337 |
;;;; Functions |
|---|
| 338 |
|
|---|
| 339 |
(deftest disjoin.1 |
|---|
| 340 |
(let ((disjunction (disjoin (lambda (x) |
|---|
| 341 |
(and (consp x) :cons)) |
|---|
| 342 |
(lambda (x) |
|---|
| 343 |
(and (stringp x) :string))))) |
|---|
| 344 |
(list (funcall disjunction 'zot) |
|---|
| 345 |
(funcall disjunction '(foo bar)) |
|---|
| 346 |
(funcall disjunction "test"))) |
|---|
| 347 |
(nil :cons :string)) |
|---|
| 348 |
|
|---|
| 349 |
(deftest conjoin.1 |
|---|
| 350 |
(let ((conjunction (conjoin #'consp |
|---|
| 351 |
(lambda (x) |
|---|
| 352 |
(stringp (car x))) |
|---|
| 353 |
(lambda (x) |
|---|
| 354 |
(char (car x) 0))))) |
|---|
| 355 |
(list (funcall conjunction 'zot) |
|---|
| 356 |
(funcall conjunction '(foo)) |
|---|
| 357 |
(funcall conjunction '("foo")))) |
|---|
| 358 |
(nil nil #\f)) |
|---|
| 359 |
|
|---|
| 360 |
(deftest compose.1 |
|---|
| 361 |
(let ((composite (compose '1+ |
|---|
| 362 |
(lambda (x) |
|---|
| 363 |
(* x 2)) |
|---|
| 364 |
#'read-from-string))) |
|---|
| 365 |
(funcall composite "1")) |
|---|
| 366 |
3) |
|---|
| 367 |
|
|---|
| 368 |
(deftest compose.2 |
|---|
| 369 |
(let ((composite |
|---|
| 370 |
(locally (declare (notinline compose)) |
|---|
| 371 |
(compose '1+ |
|---|
| 372 |
(lambda (x) |
|---|
| 373 |
(* x 2)) |
|---|
| 374 |
#'read-from-string)))) |
|---|
| 375 |
(funcall composite "2")) |
|---|
| 376 |
5) |
|---|
| 377 |
|
|---|
| 378 |
(deftest compose.3 |
|---|
| 379 |
(let ((compose-form (funcall (compiler-macro-function 'compose) |
|---|
| 380 |
'(compose '1+ |
|---|
| 381 |
(lambda (x) |
|---|
| 382 |
(* x 2)) |
|---|
| 383 |
#'read-from-string) |
|---|
| 384 |
nil))) |
|---|
| 385 |
(let ((fun (funcall (compile nil `(lambda () ,compose-form))))) |
|---|
| 386 |
(funcall fun "3"))) |
|---|
| 387 |
7) |
|---|
| 388 |
|
|---|
| 389 |
(deftest multiple-value-compose.1 |
|---|
| 390 |
(let ((composite (multiple-value-compose |
|---|
| 391 |
#'truncate |
|---|
| 392 |
(lambda (x y) |
|---|
| 393 |
(values y x)) |
|---|
| 394 |
(lambda (x) |
|---|
| 395 |
(with-input-from-string (s x) |
|---|
| 396 |
(values (read s) (read s))))))) |
|---|
| 397 |
(multiple-value-list (funcall composite "2 7"))) |
|---|
| 398 |
(3 1)) |
|---|
| 399 |
|
|---|
| 400 |
(deftest multiple-value-compose.2 |
|---|
| 401 |
(let ((composite (locally (declare (notinline multiple-value-compose)) |
|---|
| 402 |
(multiple-value-compose |
|---|
| 403 |
#'truncate |
|---|
| 404 |
(lambda (x y) |
|---|
| 405 |
(values y x)) |
|---|
| 406 |
(lambda (x) |
|---|
| 407 |
(with-input-from-string (s x) |
|---|
| 408 |
(values (read s) (read s)))))))) |
|---|
| 409 |
(multiple-value-list (funcall composite "2 11"))) |
|---|
| 410 |
(5 1)) |
|---|
| 411 |
|
|---|
| 412 |
(deftest multiple-value-compose.3 |
|---|
| 413 |
(let ((compose-form (funcall (compiler-macro-function 'multiple-value-compose) |
|---|
| 414 |
'(multiple-value-compose |
|---|
| 415 |
#'truncate |
|---|
| 416 |
(lambda (x y) |
|---|
| 417 |
(values y x)) |
|---|
| 418 |
(lambda (x) |
|---|
| 419 |
(with-input-from-string (s x) |
|---|
| 420 |
(values (read s) (read s))))) |
|---|
| 421 |
nil))) |
|---|
| 422 |
(let ((fun (funcall (compile nil `(lambda () ,compose-form))))) |
|---|
| 423 |
(multiple-value-list (funcall fun "2 9")))) |
|---|
| 424 |
(4 1)) |
|---|
| 425 |
|
|---|
| 426 |
(deftest curry.1 |
|---|
| 427 |
(let ((curried (curry '+ 3))) |
|---|
| 428 |
(funcall curried 1 5)) |
|---|
| 429 |
9) |
|---|
| 430 |
|
|---|
| 431 |
(deftest curry.2 |
|---|
| 432 |
(let ((curried (locally (declare (notinline curry)) |
|---|
| 433 |
(curry '* 2 3)))) |
|---|
| 434 |
(funcall curried 7)) |
|---|
| 435 |
42) |
|---|
| 436 |
|
|---|
| 437 |
(deftest curry.3 |
|---|
| 438 |
(let ((curried-form (funcall (compiler-macro-function 'curry) |
|---|
| 439 |
'(curry '/ 8) |
|---|
| 440 |
nil))) |
|---|
| 441 |
(let ((fun (funcall (compile nil `(lambda () ,curried-form))))) |
|---|
| 442 |
(funcall fun 2))) |
|---|
| 443 |
4) |
|---|
| 444 |
|
|---|
| 445 |
(deftest rcurry.1 |
|---|
| 446 |
(let ((r (rcurry '/ 2))) |
|---|
| 447 |
(funcall r 8)) |
|---|
| 448 |
4) |
|---|
| 449 |
|
|---|
| 450 |
(deftest named-lambda.1 |
|---|
| 451 |
(let ((fac (named-lambda fac (x) |
|---|
| 452 |
(if (> x 1) |
|---|
| 453 |
(* x (fac (- x 1))) |
|---|
| 454 |
x)))) |
|---|
| 455 |
(funcall fac 5)) |
|---|
| 456 |
120) |
|---|
| 457 |
|
|---|
| 458 |
(deftest named-lambda.2 |
|---|
| 459 |
(let ((fac (named-lambda fac (&key x) |
|---|
| 460 |
(if (> x 1) |
|---|
| 461 |
(* x (fac :x (- x 1))) |
|---|
| 462 |
x)))) |
|---|
| 463 |
(funcall fac :x 5)) |
|---|
| 464 |
120) |
|---|
| 465 |
|
|---|
| 466 |
;;;; Lists |
|---|
| 467 |
|
|---|
| 468 |
(deftest alist-plist.1 |
|---|
| 469 |
(alist-plist '((a . 1) (b . 2) (c . 3))) |
|---|
| 470 |
(a 1 b 2 c 3)) |
|---|
| 471 |
|
|---|
| 472 |
(deftest plist-alist.1 |
|---|
| 473 |
(plist-alist '(a 1 b 2 c 3)) |
|---|
| 474 |
((a . 1) (b . 2) (c . 3))) |
|---|
| 475 |
|
|---|
| 476 |
(deftest unionf.1 |
|---|
| 477 |
(let* ((list (list 1 2 3)) |
|---|
| 478 |
(orig list)) |
|---|
| 479 |
(unionf list (list 1 2 4)) |
|---|
| 480 |
(values (equal orig (list 1 2 3)) |
|---|
| 481 |
(eql (length list) 4) |
|---|
| 482 |
(set-difference list (list 1 2 3 4)) |
|---|
| 483 |
(set-difference (list 1 2 3 4) list))) |
|---|
| 484 |
t |
|---|
| 485 |
t |
|---|
| 486 |
nil |
|---|
| 487 |
nil) |
|---|
| 488 |
|
|---|
| 489 |
(deftest nunionf.1 |
|---|
| 490 |
(let ((list (list 1 2 3))) |
|---|
| 491 |
(nunionf list (list 1 2 4)) |
|---|
| 492 |
(values (eql (length list) 4) |
|---|
| 493 |
(set-difference (list 1 2 3 4) list) |
|---|
| 494 |
(set-difference list (list 1 2 3 4)))) |
|---|
| 495 |
t |
|---|
| 496 |
nil |
|---|
| 497 |
nil) |
|---|
| 498 |
|
|---|
| 499 |
(deftest appendf.1 |
|---|
| 500 |
(let* ((list (list 1 2 3)) |
|---|
| 501 |
(orig list)) |
|---|
| 502 |
(appendf list '(4 5 6) '(7 8)) |
|---|
| 503 |
(list list (eq list orig))) |
|---|
| 504 |
((1 2 3 4 5 6 7 8) nil)) |
|---|
| 505 |
|
|---|
| 506 |
(deftest nconcf.1 |
|---|
| 507 |
(let ((list1 (list 1 2 3)) |
|---|
| 508 |
(list2 (list 4 5 6))) |
|---|
| 509 |
(nconcf list1 list2 (list 7 8 9)) |
|---|
| 510 |
list1) |
|---|
| 511 |
(1 2 3 4 5 6 7 8 9)) |
|---|
| 512 |
|
|---|
| 513 |
(deftest circular-list.1 |
|---|
| 514 |
(let ((circle (circular-list 1 2 3))) |
|---|
| 515 |
(list (first circle) |
|---|
| 516 |
(second circle) |
|---|
| 517 |
(third circle) |
|---|
| 518 |
(fourth circle) |
|---|
| 519 |
(eq circle (nthcdr 3 circle)))) |
|---|
| 520 |
(1 2 3 1 t)) |
|---|
| 521 |
|
|---|
| 522 |
(deftest circular-list-p.1 |
|---|
| 523 |
(let* ((circle (circular-list 1 2 3 4)) |
|---|
| 524 |
(tree (list circle circle)) |
|---|
| 525 |
(dotted (cons circle t)) |
|---|
| 526 |
(proper (list 1 2 3 circle)) |
|---|
| 527 |
(tailcirc (list* 1 2 3 circle))) |
|---|
| 528 |
(list (circular-list-p circle) |
|---|
| 529 |
(circular-list-p tree) |
|---|
| 530 |
(circular-list-p dotted) |
|---|
| 531 |
(circular-list-p proper) |
|---|
| 532 |
(circular-list-p tailcirc))) |
|---|
| 533 |
(t nil nil nil t)) |
|---|
| 534 |
|
|---|
| 535 |
(deftest circular-list-p.2 |
|---|
| 536 |
(circular-list-p 'foo) |
|---|
| 537 |
nil) |
|---|
| 538 |
|
|---|
| 539 |
(deftest circular-tree-p.1 |
|---|
| 540 |
(let* ((circle (circular-list 1 2 3 4)) |
|---|
| 541 |
(tree1 (list circle circle)) |
|---|
| 542 |
(tree2 (let* ((level2 (list 1 nil 2)) |
|---|
| 543 |
(level1 (list level2))) |
|---|
| 544 |
(setf (second level2) level1) |
|---|
| 545 |
level1)) |
|---|
| 546 |
(dotted (cons circle t)) |
|---|
| 547 |
(proper (list 1 2 3 circle)) |
|---|
| 548 |
(tailcirc (list* 1 2 3 circle)) |
|---|
| 549 |
(quite-proper (list 1 2 3)) |
|---|
| 550 |
(quite-dotted (list 1 (cons 2 3)))) |
|---|
| 551 |
(list (circular-tree-p circle) |
|---|
| 552 |
(circular-tree-p tree1) |
|---|
| 553 |
(circular-tree-p tree2) |
|---|
| 554 |
(circular-tree-p dotted) |
|---|
| 555 |
(circular-tree-p proper) |
|---|
| 556 |
(circular-tree-p tailcirc) |
|---|
| 557 |
(circular-tree-p quite-proper) |
|---|
| 558 |
(circular-tree-p quite-dotted))) |
|---|
| 559 |
(t t t t t t nil nil)) |
|---|
| 560 |
|
|---|
| 561 |
(deftest proper-list-p.1 |
|---|
| 562 |
(let ((l1 (list 1)) |
|---|
| 563 |
(l2 (list 1 2)) |
|---|
| 564 |
(l3 (cons 1 2)) |
|---|
| 565 |
(l4 (list (cons 1 2) 3)) |
|---|
| 566 |
(l5 (circular-list 1 2))) |
|---|
| 567 |
(list (proper-list-p l1) |
|---|
| 568 |
(proper-list-p l2) |
|---|
| 569 |
(proper-list-p l3) |
|---|
| 570 |
(proper-list-p l4) |
|---|
| 571 |
(proper-list-p l5))) |
|---|
| 572 |
(t t nil t nil)) |
|---|
| 573 |
|
|---|
| 574 |
(deftest proper-list-p.2 |
|---|
| 575 |
(proper-list-p '(1 2 . 3)) |
|---|
| 576 |
nil) |
|---|
| 577 |
|
|---|
| 578 |
(deftest proper-list.type.1 |
|---|
| 579 |
(let ((l1 (list 1)) |
|---|
| 580 |
(l2 (list 1 2)) |
|---|
| 581 |
(l3 (cons 1 2)) |
|---|
| 582 |
(l4 (list (cons 1 2) 3)) |
|---|
| 583 |
(l5 (circular-list 1 2))) |
|---|
| 584 |
(list (typep l1 'proper-list) |
|---|
| 585 |
(typep l2 'proper-list) |
|---|
| 586 |
(typep l3 'proper-list) |
|---|
| 587 |
(typep l4 'proper-list) |
|---|
| 588 |
(typep l5 'proper-list))) |
|---|
| 589 |
(t t nil t nil)) |
|---|
| 590 |
|
|---|
| 591 |
(deftest proper-list-length.1 |
|---|
| 592 |
(values |
|---|
| 593 |
(proper-list-length nil) |
|---|
| 594 |
(proper-list-length (list 1)) |
|---|
| 595 |
(proper-list-length (list 2 2)) |
|---|
| 596 |
(proper-list-length (list 3 3 3)) |
|---|
| 597 |
(proper-list-length (list 4 4 4 4)) |
|---|
| 598 |
(proper-list-length (list 5 5 5 5 5)) |
|---|
| 599 |
(proper-list-length (list 6 6 6 6 6 6)) |
|---|
| 600 |
(proper-list-length (list 7 7 7 7 7 7 7)) |
|---|
| 601 |
(proper-list-length (list 8 8 8 8 8 8 8 8)) |
|---|
| 602 |
(proper-list-length (list 9 9 9 9 9 9 9 9 9))) |
|---|
| 603 |
0 1 2 3 4 5 6 7 8 9) |
|---|
| 604 |
|
|---|
| 605 |
(deftest proper-list-length.2 |
|---|
| 606 |
(flet ((plength (x) |
|---|
| 607 |
(handler-case |
|---|
| 608 |
(proper-list-length x) |
|---|
| 609 |
(type-error () |
|---|
| 610 |
:ok)))) |
|---|
| 611 |
(values |
|---|
| 612 |
(plength (list* 1)) |
|---|
| 613 |
(plength (list* 2 2)) |
|---|
| 614 |
(plength (list* 3 3 3)) |
|---|
| 615 |
(plength (list* 4 4 4 4)) |
|---|
| 616 |
(plength (list* 5 5 5 5 5)) |
|---|
| 617 |
(plength (list* 6 6 6 6 6 6)) |
|---|
| 618 |
(plength (list* 7 7 7 7 7 7 7)) |
|---|
| 619 |
(plength (list* 8 8 8 8 8 8 8 8)) |
|---|
| 620 |
(plength (list* 9 9 9 9 9 9 9 9 9)))) |
|---|
| 621 |
:ok :ok :ok |
|---|
| 622 |
:ok :ok :ok |
|---|
| 623 |
:ok :ok :ok) |
|---|
| 624 |
|
|---|
| 625 |
(deftest lastcar.1 |
|---|
| 626 |
(let ((l1 (list 1)) |
|---|
| 627 |
(l2 (list 1 2))) |
|---|
| 628 |
(list (lastcar l1) |
|---|
| 629 |
(lastcar l2))) |
|---|
| 630 |
(1 2)) |
|---|
| 631 |
|
|---|
| 632 |
(deftest lastcar.error.2 |
|---|
| 633 |
(handler-case |
|---|
| 634 |
(progn |
|---|
| 635 |
(lastcar (circular-list 1 2 3)) |
|---|
| 636 |
nil) |
|---|
| 637 |
(error () |
|---|
| 638 |
t)) |
|---|
| 639 |
t) |
|---|
| 640 |
|
|---|
| 641 |
(deftest setf-lastcar.1 |
|---|
| 642 |
(let ((l (list 1 2 3 4))) |
|---|
| 643 |
(values (lastcar l) |
|---|
| 644 |
(progn |
|---|
| 645 |
(setf (lastcar l) 42) |
|---|
| 646 |
(lastcar l)))) |
|---|
| 647 |
4 |
|---|
| 648 |
42) |
|---|
| 649 |
|
|---|
| 650 |
(deftest setf-lastcar.2 |
|---|
| 651 |
(let ((l (circular-list 1 2 3))) |
|---|
| 652 |
(multiple-value-bind (res err) |
|---|
| 653 |
(ignore-errors (setf (lastcar l) 4)) |
|---|
| 654 |
(typep err 'type-error))) |
|---|
| 655 |
t) |
|---|
| 656 |
|
|---|
| 657 |
(deftest make-circular-list.1 |
|---|
| 658 |
(let ((l (make-circular-list 3 :initial-element :x))) |
|---|
| 659 |
(setf (car l) :y) |
|---|
| 660 |
(list (eq l (nthcdr 3 l)) |
|---|
| 661 |
(first l) |
|---|
| 662 |
(second l) |
|---|
| 663 |
(third l) |
|---|
| 664 |
(fourth l))) |
|---|
| 665 |
(t :y :x :x :y)) |
|---|
| 666 |
|
|---|
| 667 |
(deftest circular-list.type.1 |
|---|
| 668 |
(let* ((l1 (list 1 2 3)) |
|---|
| 669 |
(l2 (circular-list 1 2 3)) |
|---|
| 670 |
(l3 (list* 1 2 3 l2))) |
|---|
| 671 |
(list (typep l1 'circular-list) |
|---|
| 672 |
(typep l2 'circular-list) |
|---|
| 673 |
(typep l3 'circular-list))) |
|---|
| 674 |
(nil t t)) |
|---|
| 675 |
|
|---|
| 676 |
(deftest ensure-list.1 |
|---|
| 677 |
(let ((x (list 1)) |
|---|
| 678 |
(y 2)) |
|---|
| 679 |
(list (ensure-list x) |
|---|
| 680 |
(ensure-list y))) |
|---|
| 681 |
((1) (2))) |
|---|
| 682 |
|
|---|
| 683 |
(deftest ensure-cons.1 |
|---|
| 684 |
(let ((x (cons 1 2)) |
|---|
| 685 |
(y nil) |
|---|
| 686 |
(z "foo")) |
|---|
| 687 |
(values (ensure-cons x) |
|---|
| 688 |
(ensure-cons y) |
|---|
| 689 |
(ensure-cons z))) |
|---|
| 690 |
(1 . 2) |
|---|
| 691 |
(nil) |
|---|
| 692 |
("foo")) |
|---|
| 693 |
|
|---|
| 694 |
(deftest setp.1 |
|---|
| 695 |
(setp '(1)) |
|---|
| 696 |
t) |
|---|
| 697 |
|
|---|
| 698 |
(deftest setp.2 |
|---|
| 699 |
(setp nil) |
|---|
| 700 |
t) |
|---|
| 701 |
|
|---|
| 702 |
(deftest setp.3 |
|---|
| 703 |
(setp "foo") |
|---|
| 704 |
nil) |
|---|
| 705 |
|
|---|
| 706 |
(deftest setp.4 |
|---|
| 707 |
(setp '(1 2 3 1)) |
|---|
| 708 |
nil) |
|---|
| 709 |
|
|---|
| 710 |
(deftest setp.5 |
|---|
| 711 |
(setp '(1 2 3)) |
|---|
| 712 |
t) |
|---|
| 713 |
|
|---|
| 714 |
(deftest setp.6 |
|---|
| 715 |
(setp '(a :a)) |
|---|
| 716 |
t) |
|---|
| 717 |
|
|---|
| 718 |
(deftest setp.7 |
|---|
| 719 |
(setp '(a :a) :key 'character) |
|---|
| 720 |
nil) |
|---|
| 721 |
|
|---|
| 722 |
(deftest setp.8 |
|---|
| 723 |
(setp '(a :a) :key 'character :test (constantly nil)) |
|---|
| 724 |
t) |
|---|
| 725 |
|
|---|
| 726 |
(deftest set-equal.1 |
|---|
| 727 |
(set-equal '(1 2 3) '(3 1 2)) |
|---|
| 728 |
t) |
|---|
| 729 |
|
|---|
| 730 |
(deftest set-equal.2 |
|---|
| 731 |
(set-equal '("Xa") '("Xb") |
|---|
| 732 |
:test (lambda (a b) (eql (char a 0) (char b 0)))) |
|---|
| 733 |
t) |
|---|
| 734 |
|
|---|
| 735 |
(deftest set-equal.3 |
|---|
| 736 |
(set-equal '(1 2) '(4 2)) |
|---|
| 737 |
nil) |
|---|
| 738 |
|
|---|
| 739 |
(deftest set-equal.4 |
|---|
| 740 |
(set-equal '(a b c) '(:a :b :c) :key 'string :test 'equal) |
|---|
| 741 |
t) |
|---|
| 742 |
|
|---|
| 743 |
(deftest set-equal.5 |
|---|
| 744 |
(set-equal '(a d c) '(:a :b :c) :key 'string :test 'equal) |
|---|
| 745 |
nil) |
|---|
| 746 |
|
|---|
| 747 |
(deftest set-equal.6 |
|---|
| 748 |
(set-equal '(a b c) '(a b c d)) |
|---|
| 749 |
nil) |
|---|
| 750 |
|
|---|
| 751 |
(deftest map-product.1 |
|---|
| 752 |
(map-product 'cons '(2 3) '(1 4)) |
|---|
| 753 |
((2 . 1) (2 . 4) (3 . 1) (3 . 4))) |
|---|
| 754 |
|
|---|
| 755 |
(deftest map-product.2 |
|---|
| 756 |
(map-product #'cons '(2 3) '(1 4)) |
|---|
| 757 |
((2 . 1) (2 . 4) (3 . 1) (3 . 4))) |
|---|
| 758 |
|
|---|
| 759 |
(deftest flatten.1 |
|---|
| 760 |
(flatten '((1) 2 (((3 4))) ((((5)) 6)) 7)) |
|---|
| 761 |
(1 2 3 4 5 6 7)) |
|---|
| 762 |
|
|---|
| 763 |
(deftest remove-from-plist.1 |
|---|
| 764 |
(let ((orig '(a 1 b 2 c 3 d 4))) |
|---|
| 765 |
(list (remove-from-plist orig 'a 'c) |
|---|
| 766 |
(remove-from-plist orig 'b 'd) |
|---|
| 767 |
(remove-from-plist orig 'b) |
|---|
| 768 |
(remove-from-plist orig 'a) |
|---|
| 769 |
(remove-from-plist orig 'd 42 "zot") |
|---|
| 770 |
(remove-from-plist orig 'a 'b 'c 'd) |
|---|
| 771 |
(remove-from-plist orig 'a 'b 'c 'd 'x) |
|---|
| 772 |
(equal orig '(a 1 b 2 c 3 d 4)))) |
|---|
| 773 |
((b 2 d 4) |
|---|
| 774 |
(a 1 c 3) |
|---|
| 775 |
(a 1 c 3 d 4) |
|---|
| 776 |
(b 2 c 3 d 4) |
|---|
| 777 |
(a 1 b 2 c 3) |
|---|
| 778 |
nil |
|---|
| 779 |
nil |
|---|
| 780 |
t)) |
|---|
| 781 |
|
|---|
| 782 |
(deftest mappend.1 |
|---|
| 783 |
(mappend (compose 'list '*) '(1 2 3) '(1 2 3)) |
|---|
| 784 |
(1 4 9)) |
|---|
| 785 |
|
|---|
| 786 |
;;;; Numbers |
|---|
| 787 |
|
|---|
| 788 |
(deftest clamp.1 |
|---|
| 789 |
(list (clamp 1.5 1 2) |
|---|
| 790 |
(clamp 2.0 1 2) |
|---|
| 791 |
(clamp 1.0 1 2) |
|---|
| 792 |
(clamp 3 1 2) |
|---|
| 793 |
(clamp 0 1 2)) |
|---|
| 794 |
(1.5 2.0 1.0 2 1)) |
|---|
| 795 |
|
|---|
| 796 |
(deftest gaussian-random.1 |
|---|
| 797 |
(let ((min -0.2) |
|---|
| 798 |
(max +0.2)) |
|---|
| 799 |
(multiple-value-bind (g1 g2) |
|---|
| 800 |
(gaussian-random min max) |
|---|
| 801 |
(values (<= min g1 max) |
|---|
| 802 |
(<= min g2 max) |
|---|
| 803 |
(/= g1 g2) ;uh |
|---|
| 804 |
))) |
|---|
| 805 |
t |
|---|
| 806 |
t |
|---|
| 807 |
t) |
|---|
| 808 |
|
|---|
| 809 |
(deftest iota.1 |
|---|
| 810 |
(iota 3) |
|---|
| 811 |
(0 1 2)) |
|---|
| 812 |
|
|---|
| 813 |
(deftest iota.2 |
|---|
| 814 |
(iota 3 :start 0.0d0) |
|---|
| 815 |
(0.0d0 1.0d0 2.0d0)) |
|---|
| 816 |
|
|---|
| 817 |
(deftest iota.3 |
|---|
| 818 |
(iota 3 :start 2 :step 3.0) |
|---|
| 819 |
(2.0 5.0 8.0)) |
|---|
| 820 |
|
|---|
| 821 |
(deftest map-iota.1 |
|---|
| 822 |
(let (all) |
|---|
| 823 |
(declare (notinline map-iota)) |
|---|
| 824 |
(values (map-iota (lambda (x) (push x all)) |
|---|
| 825 |
3 |
|---|
| 826 |
:start 2 |
|---|
| 827 |
:step 1.1d0) |
|---|
| 828 |
all)) |
|---|
| 829 |
3 |
|---|
| 830 |
(4.2d0 3.1d0 2.0d0)) |
|---|
| 831 |
|
|---|
| 832 |
(deftest lerp.1 |
|---|
| 833 |
(lerp 0.5 1 2) |
|---|
| 834 |
1.5) |
|---|
| 835 |
|
|---|
| 836 |
(deftest lerp.2 |
|---|
| 837 |
(lerp 0.1 1 2) |
|---|
| 838 |
1.1) |
|---|
| 839 |
|
|---|
| 840 |
(deftest mean.1 |
|---|
| 841 |
(mean '(1 2 3)) |
|---|
| 842 |
2) |
|---|
| 843 |
|
|---|
| 844 |
(deftest mean.2 |
|---|
| 845 |
(mean '(1 2 3 4)) |
|---|
| 846 |
5/2) |
|---|
| 847 |
|
|---|
| 848 |
(deftest mean.3 |
|---|
| 849 |
(mean '(1 2 10)) |
|---|
| 850 |
13/3) |
|---|
| 851 |
|
|---|
| 852 |
(deftest median.1 |
|---|
| 853 |
(median '(100 0 99 1 98 2 97)) |
|---|
| 854 |
97) |
|---|
| 855 |
|
|---|
| 856 |
(deftest median.2 |
|---|
| 857 |
(median '(100 0 99 1 98 2 97 96)) |
|---|
| 858 |
195/2) |
|---|
| 859 |
|
|---|
| 860 |
(deftest variance.1 |
|---|
| 861 |
(variance (list 1 2 3)) |
|---|
| 862 |
2/3) |
|---|
| 863 |
|
|---|
| 864 |
(deftest standard-deviation.1 |
|---|
| 865 |
(< 0 (standard-deviation (list 1 2 3)) 1) |
|---|
| 866 |
t) |
|---|
| 867 |
|
|---|
| 868 |
(deftest maxf.1 |
|---|
| 869 |
(let ((x 1)) |
|---|
| 870 |
(maxf x 2) |
|---|
| 871 |
x) |
|---|
| 872 |
2) |
|---|
| 873 |
|
|---|
| 874 |
(deftest maxf.2 |
|---|
| 875 |
(let ((x 1)) |
|---|
| 876 |
(maxf x 0) |
|---|
| 877 |
x) |
|---|
| 878 |
1) |
|---|
| 879 |
|
|---|
| 880 |
(deftest maxf.3 |
|---|
| 881 |
(let ((x 1) |
|---|
| 882 |
(c 0)) |
|---|
| 883 |
(maxf x (incf c)) |
|---|
| 884 |
(list x c)) |
|---|
| 885 |
(1 1)) |
|---|
| 886 |
|
|---|
| 887 |
(deftest maxf.4 |
|---|
| 888 |
(let ((xv (vector 0 0 0)) |
|---|
| 889 |
(p 0)) |
|---|
| 890 |
(maxf (svref xv (incf p)) (incf p)) |
|---|
| 891 |
(list p xv)) |
|---|
| 892 |
(2 #(0 2 0))) |
|---|
| 893 |
|
|---|
| 894 |
(deftest minf.1 |
|---|
| 895 |
(let ((y 1)) |
|---|
| 896 |
(minf y 0) |
|---|
| 897 |
y) |
|---|
| 898 |
0) |
|---|
| 899 |
|
|---|
| 900 |
(deftest minf.2 |
|---|
| 901 |
(let ((xv (vector 10 10 10)) |
|---|
| 902 |
(p 0)) |
|---|
| 903 |
(minf (svref xv (incf p)) (incf p)) |
|---|
| 904 |
(list p xv)) |
|---|
| 905 |
(2 #(10 2 10))) |
|---|
| 906 |
|
|---|
| 907 |
;;;; Arrays |
|---|
| 908 |
|
|---|
| 909 |
#+nil |
|---|
| 910 |
(deftest array-index.type) |
|---|
| 911 |
|
|---|
| 912 |
#+nil |
|---|
| 913 |
(deftest copy-array) |
|---|
| 914 |
|
|---|
| 915 |
;;;; Sequences |
|---|
| 916 |
|
|---|
| 917 |
(deftest rotate.1 |
|---|
| 918 |
(list (rotate (list 1 2 3) 0) |
|---|
| 919 |
(rotate (list 1 2 3) 1) |
|---|
| 920 |
(rotate (list 1 2 3) 2) |
|---|
| 921 |
(rotate (list 1 2 3) 3) |
|---|
| 922 |
(rotate (list 1 2 3) 4)) |
|---|
| 923 |
((1 2 3) |
|---|
| 924 |
(3 1 2) |
|---|
| 925 |
(2 3 1) |
|---|
| 926 |
(1 2 3) |
|---|
| 927 |
(3 1 2))) |
|---|
| 928 |
|
|---|
| 929 |
(deftest rotate.2 |
|---|
| 930 |
(list (rotate (vector 1 2 3 4) 0) |
|---|
| 931 |
(rotate (vector 1 2 3 4)) |
|---|
| 932 |
(rotate (vector 1 2 3 4) 2) |
|---|
| 933 |
(rotate (vector 1 2 3 4) 3) |
|---|
| 934 |
(rotate (vector 1 2 3 4) 4) |
|---|
| 935 |
(rotate (vector 1 2 3 4) 5)) |
|---|
| 936 |
(#(1 2 3 4) |
|---|
| 937 |
#(4 1 2 3) |
|---|
| 938 |
#(3 4 1 2) |
|---|
| 939 |
#(2 3 4 1) |
|---|
| 940 |
#(1 2 3 4) |
|---|
| 941 |
#(4 1 2 3))) |
|---|
| 942 |
|
|---|
| 943 |
(deftest rotate.3 |
|---|
| 944 |
(list (rotate (list 1 2 3) 0) |
|---|
| 945 |
(rotate (list 1 2 3) -1) |
|---|
| 946 |
(rotate (list 1 2 3) -2) |
|---|
| 947 |
(rotate (list 1 2 3) -3) |
|---|
| 948 |
(rotate (list 1 2 3) -4)) |
|---|
| 949 |
((1 2 3) |
|---|
| 950 |
(2 3 1) |
|---|
| 951 |
(3 1 2) |
|---|
| 952 |
(1 2 3) |
|---|
| 953 |
(2 3 1))) |
|---|
| 954 |
|
|---|
| 955 |
(deftest rotate.4 |
|---|
| 956 |
(list (rotate (vector 1 2 3 4) 0) |
|---|
| 957 |
(rotate (vector 1 2 3 4) -1) |
|---|
| 958 |
(rotate (vector 1 2 3 4) -2) |
|---|
| 959 |
(rotate (vector 1 2 3 4) -3) |
|---|
| 960 |
(rotate (vector 1 2 3 4) -4) |
|---|
| 961 |
(rotate (vector 1 2 3 4) -5)) |
|---|
| 962 |
(#(1 2 3 4) |
|---|
| 963 |
#(2 3 4 1) |
|---|
| 964 |
#(3 4 1 2) |
|---|
| 965 |
#(4 1 2 3) |
|---|
| 966 |
#(1 2 3 4) |
|---|
| 967 |
#(2 3 4 1))) |
|---|
| 968 |
|
|---|
| 969 |
(deftest rotate.5 |
|---|
| 970 |
(values (rotate (list 1) 17) |
|---|
| 971 |
(rotate (list 1) -5)) |
|---|
| 972 |
(1) |
|---|
| 973 |
(1)) |
|---|
| 974 |
|
|---|
| 975 |
(deftest shuffle.1 |
|---|
| 976 |
(let ((s (shuffle (iota 100)))) |
|---|
| 977 |
(list (equal s (iota 100)) |
|---|
| 978 |
(every (lambda (x) |
|---|
| 979 |
(member x s)) |
|---|
| 980 |
(iota 100)) |
|---|
| 981 |
(every (lambda (x) |
|---|
| 982 |
(typep x '(integer 0 99))) |
|---|
| 983 |
s))) |
|---|
| 984 |
(nil t t)) |
|---|
| 985 |
|
|---|
| 986 |
(deftest shuffle.2 |
|---|
| 987 |
(let ((s (shuffle (coerce (iota 100) 'vector)))) |
|---|
| 988 |
(list (equal s (coerce (iota 100) 'vector)) |
|---|
| 989 |
(every (lambda (x) |
|---|
| 990 |
(find x s)) |
|---|
| 991 |
(iota 100)) |
|---|
| 992 |
(every (lambda (x) |
|---|
| 993 |
(typep x '(integer 0 99))) |
|---|
| 994 |
s))) |
|---|
| 995 |
(nil t t)) |
|---|
| 996 |
|
|---|
| 997 |
(deftest random-elt.1 |
|---|
| 998 |
(let ((s1 #(1 2 3 4)) |
|---|
| 999 |
(s2 '(1 2 3 4))) |
|---|
| 1000 |
(list (dotimes (i 1000 nil) |
|---|
| 1001 |
(unless (member (random-elt s1) s2) |
|---|
| 1002 |
(return nil)) |
|---|
| 1003 |
(when (/= (random-elt s1) (random-elt s1)) |
|---|
| 1004 |
(return t))) |
|---|
| 1005 |
(dotimes (i 1000 nil) |
|---|
| 1006 |
(unless (member (random-elt s2) s2) |
|---|
| 1007 |
(return nil)) |
|---|
| 1008 |
(when (/= (random-elt s2) (random-elt s2)) |
|---|
| 1009 |
(return t))))) |
|---|
| 1010 |
(t t)) |
|---|
| 1011 |
|
|---|
| 1012 |
(deftest removef.1 |
|---|
| 1013 |
(let* ((x '(1 2 3)) |
|---|
| 1014 |
(x* x) |
|---|
| 1015 |
(y #(1 2 3)) |
|---|
| 1016 |
(y* y)) |
|---|
| 1017 |
(removef x 1) |
|---|
| 1018 |
(removef y 3) |
|---|
| 1019 |
(list x x* y y*)) |
|---|
| 1020 |
((2 3) |
|---|
| 1021 |
(1 2 3) |
|---|
| 1022 |
#(1 2) |
|---|
| 1023 |
#(1 2 3))) |
|---|
| 1024 |
|
|---|
| 1025 |
(deftest deletef.1 |
|---|
| 1026 |
(let* ((x (list 1 2 3)) |
|---|
| 1027 |
(x* x) |
|---|
| 1028 |
(y (vector 1 2 3))) |
|---|
| 1029 |
(deletef x 2) |
|---|
| 1030 |
(deletef y 1) |
|---|
| 1031 |
(list x x* y)) |
|---|
| 1032 |
((1 3) |
|---|
| 1033 |
(1 3) |
|---|
| 1034 |
#(2 3))) |
|---|
| 1035 |
|
|---|
| 1036 |
(deftest map-permutations.1 |
|---|
| 1037 |
(let ((seq (list 1 2 3)) |
|---|
| 1038 |
(seen nil) |
|---|
| 1039 |
(ok t)) |
|---|
| 1040 |
(map-permutations (lambda (s) |
|---|
| 1041 |
(unless (set-equal s seq) |
|---|
| 1042 |
(setf ok nil)) |
|---|
| 1043 |
(when (member s seen :test 'equal) |
|---|
| 1044 |
(setf ok nil)) |
|---|
| 1045 |
(push s seen)) |
|---|
| 1046 |
seq |
|---|
| 1047 |
:copy t) |
|---|
| 1048 |
(values ok (length seen))) |
|---|
| 1049 |
t |
|---|
| 1050 |
6) |
|---|
| 1051 |
|
|---|
| 1052 |
(deftest proper-sequence.type.1 |
|---|
| 1053 |
(mapcar (lambda (x) |
|---|
| 1054 |
(typep x 'proper-sequence)) |
|---|
| 1055 |
(list (list 1 2 3) |
|---|
| 1056 |
(vector 1 2 3) |
|---|
| 1057 |
#2a((1 2) (3 4)) |
|---|
| 1058 |
(circular-list 1 2 3 4))) |
|---|
| 1059 |
(t t nil nil)) |
|---|
| 1060 |
|
|---|
| 1061 |
(deftest emptyp.1 |
|---|
| 1062 |
(mapcar #'emptyp |
|---|
| 1063 |
(list (list 1) |
|---|
| 1064 |
(circular-list 1) |
|---|
| 1065 |
nil |
|---|
| 1066 |
(vector) |
|---|
| 1067 |
(vector 1))) |
|---|
| 1068 |
(nil nil t t nil)) |
|---|
| 1069 |
|
|---|
| 1070 |
(deftest sequence-of-length-p.1 |
|---|
| 1071 |
(mapcar #'sequence-of-length-p |
|---|
| 1072 |
(list nil |
|---|
| 1073 |
#() |
|---|
| 1074 |
(list 1) |
|---|
| 1075 |
(vector 1) |
|---|
| 1076 |
(list 1 2) |
|---|
| 1077 |
(vector 1 2) |
|---|
| 1078 |
(list 1 2) |
|---|
| 1079 |
(vector 1 2) |
|---|
| 1080 |
(list 1 2) |
|---|
| 1081 |
(vector 1 2)) |
|---|
| 1082 |
(list 0 |
|---|
| 1083 |
0 |
|---|
| 1084 |
1 |
|---|
| 1085 |
1 |
|---|
| 1086 |
2 |
|---|
| 1087 |
2 |
|---|
| 1088 |
1 |
|---|
| 1089 |
1 |
|---|
| 1090 |
4 |
|---|
| 1091 |
4)) |
|---|
| 1092 |
(t t t t t t nil nil nil nil)) |
|---|
| 1093 |
|
|---|
| 1094 |
(deftest length=.1 |
|---|
| 1095 |
(mapcar #'length= |
|---|
| 1096 |
(list nil |
|---|
| 1097 |
#() |
|---|
| 1098 |
(list 1) |
|---|
| 1099 |
(vector 1) |
|---|
| 1100 |
(list 1 2) |
|---|
| 1101 |
(vector 1 2) |
|---|
| 1102 |
(list 1 2) |
|---|
| 1103 |
(vector 1 2) |
|---|
| 1104 |
(list 1 2) |
|---|
| 1105 |
(vector 1 2)) |
|---|
| 1106 |
(list 0 |
|---|
| 1107 |
0 |
|---|
| 1108 |
1 |
|---|
| 1109 |
1 |
|---|
| 1110 |
2 |
|---|
| 1111 |
2 |
|---|
| 1112 |
1 |
|---|
| 1113 |
1 |
|---|
| 1114 |
4 |
|---|
| 1115 |
4)) |
|---|
| 1116 |
(t t t t t t nil nil nil nil)) |
|---|
| 1117 |
|
|---|
| 1118 |
(deftest length=.2 |
|---|
| 1119 |
;; test the compiler macro |
|---|
| 1120 |
(macrolet ((x (&rest args) |
|---|
| 1121 |
(funcall |
|---|
| 1122 |
(compile nil |
|---|
| 1123 |
`(lambda () |
|---|
| 1124 |
(length= ,@args)))))) |
|---|
| 1125 |
(list (x 2 '(1 2)) |
|---|
| 1126 |
(x '(1 2) '(3 4)) |
|---|
| 1127 |
(x '(1 2) 2) |
|---|
| 1128 |
(x '(1 2) 2 '(3 4)) |
|---|
| 1129 |
(x 1 2 3))) |
|---|
| 1130 |
(t t t t nil)) |
|---|
| 1131 |
|
|---|
| 1132 |
(deftest copy-sequence.1 |
|---|
| 1133 |
(let ((l (list 1 2 3)) |
|---|
| 1134 |
(v (vector #\a #\b #\c))) |
|---|
| 1135 |
(declare (notinline copy-sequence)) |
|---|
| 1136 |
(let ((l.list (copy-sequence 'list l)) |
|---|
| 1137 |
(l.vector (copy-sequence 'vector l)) |
|---|
| 1138 |
(l.spec-v (copy-sequence '(vector fixnum) l)) |
|---|
| 1139 |
(v.vector (copy-sequence 'vector v)) |
|---|
| 1140 |
(v.list (copy-sequence 'list v)) |
|---|
| 1141 |
(v.string (copy-sequence 'string v))) |
|---|
| 1142 |
(list (member l (list l.list l.vector l.spec-v)) |
|---|
| 1143 |
(member v (list v.vector v.list v.string)) |
|---|
| 1144 |
(equal l.list l) |
|---|
| 1145 |
(equalp l.vector #(1 2 3)) |
|---|
| 1146 |
(eql (upgraded-array-element-type 'fixnum) |
|---|
| 1147 |
(array-element-type l.spec-v)) |
|---|
| 1148 |
(equalp v.vector v) |
|---|
| 1149 |
(equal v.list '(#\a #\b #\c)) |
|---|
| 1150 |
(equal "abc" v.string)))) |
|---|
| 1151 |
(nil nil t t t t t t)) |
|---|
| 1152 |
|
|---|
| 1153 |
(deftest first-elt.1 |
|---|
| 1154 |
(mapcar #'first-elt |
|---|
| 1155 |
(list (list 1 2 3) |
|---|
| 1156 |
"abc" |
|---|
| 1157 |
(vector :a :b :c))) |
|---|
| 1158 |
(1 #\a :a)) |
|---|
| 1159 |
|
|---|
| 1160 |
(deftest first-elt.error.1 |
|---|
| 1161 |
(mapcar (lambda (x) |
|---|
| 1162 |
(handler-case |
|---|
| 1163 |
(first-elt x) |
|---|
| 1164 |
(type-error () |
|---|
| 1165 |
:type-error))) |
|---|
| 1166 |
(list nil |
|---|
| 1167 |
#() |
|---|
| 1168 |
12 |
|---|
| 1169 |
:zot)) |
|---|
| 1170 |
(:type-error |
|---|
| 1171 |
:type-error |
|---|
| 1172 |
:type-error |
|---|
| 1173 |
:type-error)) |
|---|
| 1174 |
|
|---|
| 1175 |
(deftest setf-first-elt.1 |
|---|
| 1176 |
(let ((l (list 1 2 3)) |
|---|
| 1177 |
(s (copy-seq "foobar")) |
|---|
| 1178 |
(v (vector :a :b :c))) |
|---|
| 1179 |
(setf (first-elt l) -1 |
|---|
| 1180 |
(first-elt s) #\x |
|---|
| 1181 |
(first-elt v) 'zot) |
|---|
| 1182 |
(values l s v)) |
|---|
| 1183 |
(-1 2 3) |
|---|
| 1184 |
"xoobar" |
|---|
| 1185 |
#(zot :b :c)) |
|---|
| 1186 |
|
|---|
| 1187 |
(deftest setf-first-elt.error.1 |
|---|
| 1188 |
(let ((l 'foo)) |
|---|
| 1189 |
(multiple-value-bind (res err) |
|---|
| 1190 |
(ignore-errors (setf (first-elt l) 4)) |
|---|
| 1191 |
(typep err 'type-error))) |
|---|
| 1192 |
t) |
|---|
| 1193 |
|
|---|
| 1194 |
(deftest last-elt.1 |
|---|
| 1195 |
(mapcar #'last-elt |
|---|
| 1196 |
(list (list 1 2 3) |
|---|
| 1197 |
(vector :a :b :c) |
|---|
| 1198 |
"FOOBAR" |
|---|
| 1199 |
#*001 |
|---|
| 1200 |
#*010)) |
|---|
| 1201 |
(3 :c #\R 1 0)) |
|---|
| 1202 |
|
|---|
| 1203 |
(deftest last-elt.error.1 |
|---|
| 1204 |
(mapcar (lambda (x) |
|---|
| 1205 |
(handler-case |
|---|
| 1206 |
(last-elt x) |
|---|
| 1207 |
(type-error () |
|---|
| 1208 |
:type-error))) |
|---|
| 1209 |
(list nil |
|---|
| 1210 |
#() |
|---|
| 1211 |
&n |
|---|