| 1 |
;;;; elisp.test --- tests guile's elisp support -*- scheme -*- |
| 2 |
;;;; Copyright (C) 2002, 2003, 2006, 2009, 2010, 2020 Free Software Foundation, Inc. |
| 3 |
;;;; |
| 4 |
;;;; This library is free software; you can redistribute it and/or |
| 5 |
;;;; modify it under the terms of the GNU Lesser General Public |
| 6 |
;;;; License as published by the Free Software Foundation; either |
| 7 |
;;;; version 3 of the License, or (at your option) any later version. |
| 8 |
;;;; |
| 9 |
;;;; This library is distributed in the hope that it will be useful, |
| 10 |
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
;;;; Lesser General Public License for more details. |
| 13 |
;;;; |
| 14 |
;;;; You should have received a copy of the GNU Lesser General Public |
| 15 |
;;;; License along with this library; if not, write to the Free Software |
| 16 |
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
|
| 18 |
(define-module (test-suite test-elisp) |
| 19 |
#:use-module (ice-9 copy-tree) |
| 20 |
#:use-module (test-suite lib) |
| 21 |
#:use-module (system base compile) |
| 22 |
#:use-module (ice-9 weak-vector)) |
| 23 |
|
| 24 |
(with-test-prefix "scheme" |
| 25 |
|
| 26 |
(with-test-prefix "nil value is a boolean" |
| 27 |
|
| 28 |
(pass-if "boolean?" |
| 29 |
(boolean? #nil))) |
| 30 |
|
| 31 |
|
| 32 |
(with-test-prefix "nil value is false" |
| 33 |
|
| 34 |
(pass-if "not" |
| 35 |
(eq? (not #nil) #t)) |
| 36 |
|
| 37 |
(pass-if "if" |
| 38 |
(if #nil #f #t)) |
| 39 |
|
| 40 |
(pass-if "and" |
| 41 |
(eq? (and #nil #t) #f)) |
| 42 |
|
| 43 |
(pass-if "or" |
| 44 |
(eq? (or #nil #f) #f)) |
| 45 |
|
| 46 |
(pass-if "cond" |
| 47 |
(cond (#nil #f) (else #t))) |
| 48 |
|
| 49 |
(pass-if "do" |
| 50 |
(call-with-current-continuation |
| 51 |
(lambda (exit) |
| 52 |
(do ((i 0 (+ i 1))) |
| 53 |
(#nil (exit #f)) |
| 54 |
(if (> i 10) |
| 55 |
(exit #t))))))) |
| 56 |
|
| 57 |
|
| 58 |
(with-test-prefix "nil value as an empty list" |
| 59 |
|
| 60 |
(pass-if "list?" |
| 61 |
(list? #nil)) |
| 62 |
|
| 63 |
(pass-if "null?" |
| 64 |
(null? #nil)) |
| 65 |
|
| 66 |
(pass-if "sort" |
| 67 |
(eq? (sort #nil <) #nil))) |
| 68 |
|
| 69 |
|
| 70 |
(with-test-prefix "lists formed using nil value" |
| 71 |
|
| 72 |
(pass-if "list?" |
| 73 |
(list? (cons 'a #nil))) |
| 74 |
|
| 75 |
(pass-if "length of #nil" |
| 76 |
(= (length #nil) 0)) |
| 77 |
|
| 78 |
(pass-if "length" |
| 79 |
(= (length (cons 'a (cons 'b (cons 'c #nil)))) 3)) |
| 80 |
|
| 81 |
(pass-if "length (with backquoted list)" |
| 82 |
(= (length '(a b c . #nil)) 3)) |
| 83 |
|
| 84 |
(pass-if "write (#nil)" |
| 85 |
(string=? (with-output-to-string |
| 86 |
(lambda () (write #nil))) |
| 87 |
"#nil")) ; Hmmm... should be "()" ? |
| 88 |
|
| 89 |
(pass-if "display (#nil)" |
| 90 |
(string=? (with-output-to-string |
| 91 |
(lambda () (display #nil))) |
| 92 |
"#nil")) ; Ditto. |
| 93 |
|
| 94 |
(pass-if "write (list)" |
| 95 |
(string=? (with-output-to-string |
| 96 |
(lambda () (write (cons 'a #nil)))) |
| 97 |
"(a)")) |
| 98 |
|
| 99 |
(pass-if "display (list)" |
| 100 |
(string=? (with-output-to-string |
| 101 |
(lambda () (display (cons 'a #nil)))) |
| 102 |
"(a)")) |
| 103 |
|
| 104 |
(pass-if "assq" |
| 105 |
(and (equal? (assq 1 '((1 one) (2 two) . #nil)) |
| 106 |
'(1 one)) |
| 107 |
(equal? (assq 3 '((1 one) (2 two) . #nil)) |
| 108 |
#f))) |
| 109 |
|
| 110 |
(pass-if "assv" |
| 111 |
(and (equal? (assv 1 '((1 one) (2 two) . #nil)) |
| 112 |
'(1 one)) |
| 113 |
(equal? (assv 3 '((1 one) (2 two) . #nil)) |
| 114 |
#f))) |
| 115 |
|
| 116 |
(pass-if "assoc" |
| 117 |
(and (equal? (assoc 1 '((1 one) (2 two) . #nil)) |
| 118 |
'(1 one)) |
| 119 |
(equal? (assoc 3 '((1 one) (2 two) . #nil)) |
| 120 |
#f))) |
| 121 |
|
| 122 |
(pass-if "with-fluids*" |
| 123 |
(let ((f (make-fluid)) |
| 124 |
(g (make-fluid))) |
| 125 |
(with-fluids* (cons f (cons g #nil)) |
| 126 |
'(3 4) |
| 127 |
(lambda () |
| 128 |
(and (eqv? (fluid-ref f) 3) |
| 129 |
(eqv? (fluid-ref g) 4)))))) |
| 130 |
|
| 131 |
(pass-if "append!" |
| 132 |
(let ((a (copy-tree '(1 2 3))) |
| 133 |
(b (copy-tree '(4 5 6 . #nil))) |
| 134 |
(c (copy-tree '(7 8 9))) |
| 135 |
(d (copy-tree '(a b c . #nil)))) |
| 136 |
(equal? (append! a b c d) |
| 137 |
'(1 2 3 4 5 6 7 8 9 a b c . #nil)))) |
| 138 |
|
| 139 |
(pass-if "last-pair" |
| 140 |
(equal? (last-pair '(1 2 3 4 5 . #nil)) |
| 141 |
(cons 5 #nil))) |
| 142 |
|
| 143 |
(pass-if "reverse" |
| 144 |
(equal? (reverse '(1 2 3 4 5 . #nil)) |
| 145 |
'(5 4 3 2 1))) ; Hmmm... is this OK, or |
| 146 |
; should it be |
| 147 |
; '(5 4 3 2 1 . #nil) ? |
| 148 |
|
| 149 |
(pass-if "reverse!" |
| 150 |
(equal? (reverse! (copy-tree '(1 2 3 4 5 . #nil))) |
| 151 |
'(5 4 3 2 1))) ; Ditto. |
| 152 |
|
| 153 |
(pass-if "list-ref" |
| 154 |
(eqv? (list-ref '(0 1 2 3 4 . #nil) 4) 4)) |
| 155 |
|
| 156 |
(pass-if-exception "list-ref" |
| 157 |
exception:out-of-range |
| 158 |
(eqv? (list-ref '(0 1 2 3 4 . #nil) 6) 6)) |
| 159 |
|
| 160 |
(pass-if "list-set!" |
| 161 |
(let ((l (copy-tree '(0 1 2 3 4 . #nil)))) |
| 162 |
(list-set! l 4 44) |
| 163 |
(= (list-ref l 4) 44))) |
| 164 |
|
| 165 |
(pass-if-exception "list-set!" |
| 166 |
exception:out-of-range |
| 167 |
(let ((l (copy-tree '(0 1 2 3 4 . #nil)))) |
| 168 |
(list-set! l 6 44) |
| 169 |
(= (list-ref l 6) 44))) |
| 170 |
|
| 171 |
(pass-if "list-cdr-set!" |
| 172 |
(let ((l (copy-tree '(0 1 2 3 4 . #nil)))) |
| 173 |
(and (begin |
| 174 |
(list-cdr-set! l 4 44) |
| 175 |
(equal? l '(0 1 2 3 4 . 44))) |
| 176 |
(begin |
| 177 |
(list-cdr-set! l 3 '(new . #nil)) |
| 178 |
(equal? l '(0 1 2 3 new . #nil)))))) |
| 179 |
|
| 180 |
(pass-if-exception "list-cdr-set!" |
| 181 |
exception:out-of-range |
| 182 |
(let ((l (copy-tree '(0 1 2 3 4 . #nil)))) |
| 183 |
(list-cdr-set! l 6 44))) |
| 184 |
|
| 185 |
(pass-if "memq" |
| 186 |
(equal? (memq 'c '(a b c d . #nil)) '(c d . #nil))) |
| 187 |
|
| 188 |
(pass-if "memv" |
| 189 |
(equal? (memv 'c '(a b c d . #nil)) '(c d . #nil))) |
| 190 |
|
| 191 |
(pass-if "member" |
| 192 |
(equal? (member "c" '("a" "b" "c" "d" . #nil)) '("c" "d" . #nil))) |
| 193 |
|
| 194 |
(pass-if "list->vector" |
| 195 |
(equal? '#(1 2 3) (list->vector '(1 2 3 . #nil)))) |
| 196 |
|
| 197 |
(pass-if "list->vector" |
| 198 |
(equal? '#(1 2 3) (list->vector '(1 2 3 . #nil)))) |
| 199 |
|
| 200 |
(pass-if "list->weak-vector" |
| 201 |
(equal? (weak-vector 1 2 3) (list->weak-vector '(1 2 3 . #nil)))) |
| 202 |
|
| 203 |
(pass-if "sorted?" |
| 204 |
(and (sorted? '(1 2 3 . #nil) <) |
| 205 |
(not (sorted? '(1 6 3 . #nil) <)))) |
| 206 |
|
| 207 |
(pass-if "merge" |
| 208 |
(equal? (merge '(1 4 7 10) |
| 209 |
(merge '(2 5 8 11 . #nil) |
| 210 |
'(3 6 9 12 . #nil) |
| 211 |
<) |
| 212 |
<) |
| 213 |
'(1 2 3 4 5 6 7 8 9 10 11 12 . #nil))) |
| 214 |
|
| 215 |
(pass-if "merge!" |
| 216 |
(equal? (merge! (copy-tree '(1 4 7 10)) |
| 217 |
(merge! (copy-tree '(2 5 8 11 . #nil)) |
| 218 |
(copy-tree '(3 6 9 12 . #nil)) |
| 219 |
<) |
| 220 |
<) |
| 221 |
'(1 2 3 4 5 6 7 8 9 10 11 12 . #nil))) |
| 222 |
|
| 223 |
(pass-if "sort" |
| 224 |
(equal? (sort '(1 5 3 8 4 . #nil) <) '(1 3 4 5 8))) |
| 225 |
|
| 226 |
(pass-if "stable-sort" |
| 227 |
(equal? (stable-sort '(1 5 3 8 4 . #nil) <) '(1 3 4 5 8))) |
| 228 |
|
| 229 |
(pass-if "sort!" |
| 230 |
(equal? (sort! (copy-tree '(1 5 3 8 4 . #nil)) <) |
| 231 |
'(1 3 4 5 8))) |
| 232 |
|
| 233 |
(pass-if "stable-sort!" |
| 234 |
(equal? (stable-sort! (copy-tree '(1 5 3 8 4 . #nil)) <) |
| 235 |
'(1 3 4 5 8)))) |
| 236 |
|
| 237 |
|
| 238 |
(with-test-prefix "value preservation" |
| 239 |
|
| 240 |
(pass-if "car" |
| 241 |
(eq? (car (cons #nil 'a)) #nil)) |
| 242 |
|
| 243 |
(pass-if "cdr" |
| 244 |
(eq? (cdr (cons 'a #nil)) #nil)) |
| 245 |
|
| 246 |
(pass-if "vector-ref" |
| 247 |
(eq? (vector-ref (vector #nil) 0) #nil)))) |
| 248 |
|
| 249 |
|
| 250 |
;;; |
| 251 |
;;; elisp |
| 252 |
;;; |
| 253 |
|
| 254 |
(with-test-prefix "elisp" |
| 255 |
|
| 256 |
(define (elisp-pass-if expr expected) |
| 257 |
(pass-if (with-output-to-string |
| 258 |
(lambda () |
| 259 |
(write expr))) |
| 260 |
(let ((calc (with-output-to-string |
| 261 |
(lambda () |
| 262 |
(write (compile expr #:from 'elisp #:to 'value)))))) |
| 263 |
(string=? calc expected)))) |
| 264 |
|
| 265 |
(define (elisp-pass-if/maybe-error key expr expected) |
| 266 |
(pass-if (with-output-to-string (lambda () (write expr))) |
| 267 |
(string=? |
| 268 |
(catch key |
| 269 |
(lambda () |
| 270 |
(with-output-to-string |
| 271 |
(lambda () (write (eval-elisp expr))))) |
| 272 |
(lambda (k . args) |
| 273 |
(format (current-error-port) |
| 274 |
"warning: caught ~a: ~a\n" k args) |
| 275 |
(throw 'unresolved))) |
| 276 |
expected))) |
| 277 |
|
| 278 |
(elisp-pass-if '(and #f) "#f") |
| 279 |
(elisp-pass-if '(and #t) "#t") |
| 280 |
(elisp-pass-if '(and nil) "#nil") |
| 281 |
(elisp-pass-if '(and t) "#t") |
| 282 |
(elisp-pass-if '(and) "#t") |
| 283 |
(elisp-pass-if '(cond (nil t) (t 3)) "3") |
| 284 |
(elisp-pass-if '(cond (nil t) (t)) "#t") |
| 285 |
(elisp-pass-if '(cond (nil)) "#nil") |
| 286 |
(elisp-pass-if '(cond) "#nil") |
| 287 |
(elisp-pass-if '(if #f 'a 'b) "b") |
| 288 |
(elisp-pass-if '(if #t 'a 'b) "a") |
| 289 |
(elisp-pass-if '(if nil 'a 'b) "b") |
| 290 |
(elisp-pass-if '(if nil 1 2 3 4) "4") |
| 291 |
(elisp-pass-if '(if nil 1 2) "2") |
| 292 |
(elisp-pass-if '(if nil 1) "#nil") |
| 293 |
(elisp-pass-if '(if t 1 2) "1") |
| 294 |
(elisp-pass-if '(if t 1) "1") |
| 295 |
(elisp-pass-if '(let (a) a) "#nil") |
| 296 |
(elisp-pass-if '(let* (a) a) "#nil") |
| 297 |
(elisp-pass-if '(let* ((a 1) (b (* a 2))) b) "2") |
| 298 |
(elisp-pass-if '(null nil) "#t") |
| 299 |
(elisp-pass-if '(or 1 2 3) "1") |
| 300 |
(elisp-pass-if '(or nil t nil) "#t") |
| 301 |
(elisp-pass-if '(or nil) "#nil") |
| 302 |
(elisp-pass-if '(or t nil t) "#t") |
| 303 |
(elisp-pass-if '(or t) "#t") |
| 304 |
(elisp-pass-if '(or) "#nil") |
| 305 |
(elisp-pass-if '(prog1 1 2 3) "1") |
| 306 |
(elisp-pass-if '(prog2 1 2 3) "2") |
| 307 |
(elisp-pass-if '(progn 1 2 3) "3") |
| 308 |
(elisp-pass-if '(while nil 1) "#nil") |
| 309 |
|
| 310 |
(elisp-pass-if '(defun testf (x y &optional o &rest r) (list x y o r)) "testf") |
| 311 |
(elisp-pass-if '(testf 1 2) "(1 2 #nil #nil)") |
| 312 |
(elisp-pass-if '(testf 1 2 3 4 5 56) "(1 2 3 (4 5 56))") |
| 313 |
;; NB `lambda' in Emacs is self-quoting, but that's only after |
| 314 |
;; loading the macro definition of lambda in subr.el. |
| 315 |
(elisp-pass-if '(funcall (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 4) "(1 2 3 (4))") |
| 316 |
|
| 317 |
(elisp-pass-if '(apply (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 nil) |
| 318 |
"(1 2 3 #nil)") |
| 319 |
|
| 320 |
(elisp-pass-if '(setq x 3) "3") |
| 321 |
(elisp-pass-if '(defvar x 4) "x") |
| 322 |
(elisp-pass-if 'x "3") |
| 323 |
|
| 324 |
;; wingo 9 april 2010: the following 10 tests are currently failing. the if & |
| 325 |
;; null tests are good, but I think some of the memq tests are bogus, given |
| 326 |
;; our current thoughts on equalty and nil; though they should succeed with |
| 327 |
;; memv and member in the elisp case. Also I think the function test is bogus. |
| 328 |
#; |
| 329 |
(elisp-pass-if '(if '() 'a 'b) "b") |
| 330 |
#; |
| 331 |
(elisp-pass-if '(null '#f) "#t") |
| 332 |
#; |
| 333 |
(elisp-pass-if '(null '()) "#t") |
| 334 |
#; |
| 335 |
(elisp-pass-if '(null 'nil) "#t") |
| 336 |
#; |
| 337 |
(elisp-pass-if '(memq '() '(())) "(())") |
| 338 |
#; |
| 339 |
(elisp-pass-if '(memq '() '(nil)) "(#nil)") |
| 340 |
#; |
| 341 |
(elisp-pass-if '(memq '() '(t)) "#nil") |
| 342 |
#; |
| 343 |
(elisp-pass-if '(memq nil '(())) "(())") |
| 344 |
#; |
| 345 |
(elisp-pass-if '(memq nil '(nil)) "(#nil)") |
| 346 |
#; |
| 347 |
(elisp-pass-if '(memq nil (list nil)) "(#nil)") |
| 348 |
#; |
| 349 |
(elisp-pass-if '(function (lambda (x y &optional o &rest r) (list x y o r))) "(lambda (x y &optional o &rest r) (list x y o r))") |
| 350 |
) |
| 351 |
|
| 352 |
|
| 353 |
;;; elisp.test ends here |