syntax.lsp 32 KB
Newer Older
1 2 3 4 5 6 7
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Jan  2 08:12:51 2005
;;;; Contains: Tests of standard syntax

(in-package :cl-test)

8 9
(compile-and-load "reader-aux.lsp")

10 11 12 13
(def-syntax-test syntax.whitespace.1
  ;; Check that various standard or semistandard characters are whitespace[2]
  (let ((names '("Tab" "Newline" "Linefeed" "Space" "Return" "Page")))
    (loop for name in names
sds's avatar
sds committed
14 15 16 17 18 19 20
          for c = (name-char name)
          nconc
          (when c
            (let* ((s (concatenate 'string (string c) "123"))
                   (val (read-from-string s)))
              (unless (eql val 123)
                (list (list name c s val)))))))
21 22 23 24 25 26
  nil)

(def-syntax-test syntax.constituent.1
  ;; Tests of various characters that they are constituent characters,
  ;; and parse to symbols
  (let ((chars (concatenate
sds's avatar
sds committed
27 28 29 30
                'string
                "!$%&*<=>?@[]^_-{}+/"
                "abcdefghijklmnopqrstuvwxyz"
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ")))
31
    (loop for c across chars
sds's avatar
sds committed
32 33 34 35
          for s = (string c)
          for sym = (read-from-string s)
          unless (string= (symbol-name sym) (string-upcase s))
          collect (list c sym)))
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  nil)

;;; Backspace is an invalid constituent character

(def-syntax-test syntax.backspace.invalid
  (let ((c (name-char "Backspace")))
    (if (not c) t
      (eval `(signals-error (read-from-string (string ,c)) reader-error))))
  t)

;;; Rubout is an invalid constituent character

(def-syntax-test syntax.rubout.invalid
  (let ((c (name-char "Rubout")))
    (if (not c) t
      (eval `(signals-error (read-from-string (string ,c)) reader-error))))
  t)
pfdietz's avatar
pfdietz committed
53 54 55 56 57

;;; Digits are alphabetic if >= the read base

(def-syntax-test syntax.digits.alphabetic.1
  (loop for base from 2 to 9
sds's avatar
sds committed
58 59 60 61 62 63 64 65 66
        nconc
        (let ((*read-base* base))
          (loop for digit-val from base to 9
                for c = (elt "0123456789" digit-val)
                for s = (string c)
                for val = (read-from-string s)
                unless (and (symbolp val)
                            (string= s (symbol-name val)))
                collect (list base digit-val c s val))))
pfdietz's avatar
pfdietz committed
67 68 69 70 71 72
  nil)

;;; Reading escaped characters

(def-syntax-test syntax.escaped.1
  (loop for c across +standard-chars+
sds's avatar
sds committed
73 74 75 76 77 78
        for s0 = (string c)
        for s = (concatenate 'string "\\" s0)
        for sym = (read-from-string s)
        unless (and (symbolp sym)
                    (string= (symbol-name sym) s0))
        collect (list c s0 s sym))
pfdietz's avatar
pfdietz committed
79 80 81 82 83
  nil)

(def-syntax-test syntax.escaped.2
  (let ((count 0))
    (loop for i from 0 below (min 65536 char-code-limit)
sds's avatar
sds committed
84 85 86 87 88 89 90 91 92 93
          for c = (code-char i)
          for s0 = (and c (string c))
          for s = (and c (concatenate 'string "\\" s0))
          for sym = (and c (read-from-string s))
          unless (or (not c)
                     (and (symbolp sym)
                          (string= (symbol-name sym) s0)))
          collect (progn
                    (when (> (incf count) 100) (loop-finish))
                    (list i c s0 s sym))))
pfdietz's avatar
pfdietz committed
94 95 96 97
  nil)

(def-syntax-test syntax.escaped.3
  (loop for i = (random (min char-code-limit (ash 1 24)))
sds's avatar
sds committed
98 99 100 101 102 103 104 105 106
        for c = (code-char i)
        for s0 = (and c (string c))
        for s = (and c (concatenate 'string "\\" s0))
        for sym = (and c (read-from-string s))
        repeat 1000
        unless (or (not c)
                   (and (symbolp sym)
                        (string= (symbol-name sym) s0)))
        collect (list i c s0 s sym))
pfdietz's avatar
pfdietz committed
107 108 109 110
  nil)

(def-syntax-test syntax.escaped.4
  (loop for c across +standard-chars+
sds's avatar
sds committed
111 112 113 114 115 116 117 118
        for bad = (find c "\\|")
        for s0 = (string c)
        for s = (concatenate 'string "|" s0 "|")
        for sym = (and (not bad) (read-from-string s))
        unless (or bad
                   (and (symbolp sym)
                        (string= (symbol-name sym) s0)))
        collect (list c s0 s sym))
pfdietz's avatar
pfdietz committed
119 120 121 122 123
  nil)

(def-syntax-test syntax.escaped.5
  (let ((count 0))
    (loop for i from 0 below (min 65536 char-code-limit)
sds's avatar
sds committed
124 125 126 127 128 129 130 131 132 133 134
          for c = (code-char i)
          for bad = (or (not c) (find c "\\|"))
          for s0 = (and c (string c))
          for s = (and c (concatenate 'string "|" s0 "|"))
          for sym = (and c (not bad) (read-from-string s))
          unless (or bad
                     (and (symbolp sym)
                          (string= (symbol-name sym) s0)))
          collect (progn
                    (when (> (incf count) 100) (loop-finish))
                    (list c s0 s sym))))
pfdietz's avatar
pfdietz committed
135 136 137 138
  nil)

(def-syntax-test syntax.escaped.6
  (loop for i = (random (min char-code-limit (ash 1 24)))
sds's avatar
sds committed
139 140 141 142 143 144 145 146 147 148
        for c = (code-char i)
        for bad = (or (not c) (find c "\\|"))
        for s0 = (and c (string c))
        for s = (and c (concatenate 'string "|" s0 "|"))
        for sym = (and (not bad) (read-from-string s))
        repeat 1000
        unless (or bad
                   (and (symbolp sym)
                        (string= (symbol-name sym) s0)))
        collect (list c s0 s sym))
pfdietz's avatar
pfdietz committed
149 150 151 152
  nil)

(def-syntax-test syntax.escape.whitespace.1
  (let ((names '("Tab" "Newline" "Linefeed" "Space" "Return" "Page"
sds's avatar
sds committed
153
                 "Rubout" "Backspace")))
pfdietz's avatar
pfdietz committed
154
    (loop for name in names
sds's avatar
sds committed
155 156 157 158 159 160 161
          for c = (name-char name)
          nconc
          (when c
            (let* ((s (concatenate 'string "\\" (string c)))
                   (val (read-from-string s)))
              (unless (eql val (intern (string c)))
                (list (list name c s val)))))))
pfdietz's avatar
pfdietz committed
162 163
  nil)

pfdietz's avatar
pfdietz committed
164 165 166 167 168
;;;
;;; CLtS appears to be inconsistent on the next test.
;;; Compare the definition of 'invalid' with the specification
;;; of the token reading algorithm.
;;;
pfdietz's avatar
pfdietz committed
169 170 171
(def-syntax-test syntax.escape.whitespace.2
  (let ((names '("Tab" "Newline" "Linefeed" "Space" "Return" "Page")))
    (loop for name in names
sds's avatar
sds committed
172 173 174 175 176 177 178
          for c = (name-char name)
          nconc
          (when c
            (let* ((s (concatenate 'string "|" (string c) "|"))
                   (val (read-from-string s)))
              (unless (eql val (intern (string c)))
                (list (list name c s val)))))))
pfdietz's avatar
pfdietz committed
179 180
  nil)

pfdietz's avatar
pfdietz committed
181
#|
pfdietz's avatar
pfdietz committed
182 183 184
(def-syntax-test syntax.multiple-escape.invalid.backspace
  (let ((c (name-char "Backspace")))
    (or (not c)
sds's avatar
sds committed
185 186
        (let ((s (concatenate 'string "|" (string c) "|")))
          (eval `(signals-error (read-from-string ',s) reader-error)))))
pfdietz's avatar
pfdietz committed
187 188 189 190 191
  t)

(def-syntax-test syntax.multiple-escape.invalid.rubout
  (let ((c (name-char "Rubout")))
    (or (not c)
sds's avatar
sds committed
192 193
        (let ((s (concatenate 'string "|" (string c) "|")))
          (eval `(signals-error (read-from-string ',s) reader-error)))))
pfdietz's avatar
pfdietz committed
194
  t)
pfdietz's avatar
pfdietz committed
195 196 197 198 199 200 201
|#


;;; Tests of #\

(def-syntax-test syntax.sharp-backslash.1
  (loop for c across +standard-chars+
sds's avatar
sds committed
202 203 204 205
        for s = (concatenate 'string "#\\" (string c))
        for c2 = (read-from-string s)
        unless (eql c c2)
        collect (list c s c2))
pfdietz's avatar
pfdietz committed
206 207 208 209 210
  nil)

(def-syntax-test syntax.sharp-backslash.2
  (let ((count 0))
    (loop for i below (min 65536 char-code-limit)
sds's avatar
sds committed
211 212 213 214 215 216
          for c = (code-char i)
          for s = (and c (concatenate 'string "#\\" (string c)))
          for c2 = (and c (read-from-string s))
          unless (eql c c2)
          collect (progn (when (> (incf count) 100) (loop-finish))
                         (list c s c2))))
pfdietz's avatar
pfdietz committed
217 218 219 220
  nil)

(def-syntax-test syntax.sharp-backslash.3
  (loop for i = (random (min (ash 1 24) char-code-limit))
sds's avatar
sds committed
221 222 223 224 225 226
        for c = (code-char i)
        for s = (and c (concatenate 'string "#\\" (string c)))
        for c2 = (and c (read-from-string s))
        repeat 1000
        unless (eql c c2)
        collect (list i c s c2))
pfdietz's avatar
pfdietz committed
227 228 229 230 231
  nil)

(def-syntax-test syntax.sharp-backslash.4
  (flet ((%f (s) (read-from-string (concatenate 'string "#\\" s))))
    (loop for s in '("SPACE" "NEWLINE" "TAB" "RUBOUT" "BACKSPACE" "PAGE" "LINEFEED" "RETURN")
sds's avatar
sds committed
232 233 234 235 236 237
          for c = (name-char s)
          unless (or (null c)
                     (and (eql (%f s) c)
                          (eql (%f (string-downcase s)) c)
                          (eql (%f (string-capitalize s)) c)))
          collect (list s c)))
pfdietz's avatar
pfdietz committed
238 239 240 241
  nil)

(def-syntax-test syntax.sharp-backslash.5
  (flet ((%f (s) (read-from-string (concatenate 'string "#\\" s))))
242
    (let ((good-chars (concatenate 'string +alphanumeric-chars+
sds's avatar
sds committed
243
                                   "<,.>\"':/?[]{}~`!@#$%^&*_-+=")))
244
      (loop for c across +standard-chars+
sds's avatar
sds committed
245 246 247 248 249 250 251 252
            for name = (char-name c)
            unless (or (null name)
                       (string/= "" (string-trim good-chars name))
                       (and (eql (%f name) c)
                            (eql (%f (string-downcase name)) c)
                            (eql (%f (string-upcase name)) c)
                            (eql (%f (string-capitalize name)) c)))
            collect (list c name))))
pfdietz's avatar
pfdietz committed
253 254 255 256
  nil)

(def-syntax-test syntax.sharp-backslash.6
  (flet ((%f (s) (read-from-string (concatenate 'string "#\\" s))))
257
    (let ((good-chars (concatenate 'string +alphanumeric-chars+
sds's avatar
sds committed
258
                                   "<,.>\"':/?[]{}~`!@#$%^&*_-+=")))
259
      (loop for i below (min 65536 char-code-limit)
sds's avatar
sds committed
260 261 262 263 264 265 266 267 268
            for c = (code-char i)
            for name = (and c (char-name c))
            unless (or (null name)
                       (string/= "" (string-trim good-chars name))
                       (and (eql (%f name) c)
                            (eql (%f (string-downcase name)) c)
                            (eql (%f (string-upcase name)) c)
                            (eql (%f (string-capitalize name)) c)))
            collect (list i c name))))
pfdietz's avatar
pfdietz committed
269 270 271 272
  nil)

(def-syntax-test syntax.sharp-backslash.7
  (flet ((%f (s) (read-from-string (concatenate 'string "#\\" s))))
273
    (let ((good-chars (concatenate 'string +alphanumeric-chars+
sds's avatar
sds committed
274
                                   "<,.>\"':/?[]{}~`!@#$%^&*_-+=")))
275
      (loop for i = (random (min (ash 1 24) char-code-limit))
sds's avatar
sds committed
276 277 278 279 280 281 282 283 284 285
            for c = (code-char i)
            for name = (and c (char-name c))
            repeat 1000
            unless (or (null name)
                       (string/= "" (string-trim good-chars name))
                       (and (eql (%f name) c)
                            (eql (%f (string-downcase name)) c)
                            (eql (%f (string-upcase name)) c)
                            (eql (%f (string-capitalize name)) c)))
            collect (list i c name))))
pfdietz's avatar
pfdietz committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  nil)


;;; Tests of #'

(def-syntax-test syntax.sharp-quote.1
  (read-from-string "#'X")
  (function |X|) 3)

(def-syntax-test syntax.sharp-quote.2
  (read-from-string "#':X")
  (function :|X|) 4)

(def-syntax-test syntax.sharp-quote.3
  (read-from-string "#'17")
  (function 17) 4)

(def-syntax-test syntax.sharp-quote.error.1
  (signals-error (read-from-string "#'") end-of-file)
  t)

(def-syntax-test syntax.sharp-quote.error.2
  (signals-error (read-from-string "(#'" nil nil) end-of-file)
  t)

;;; Tess of #(...)

(def-syntax-vector-test syntax.sharp-left-paren.1
  "#()")

(def-syntax-vector-test syntax.sharp-left-paren.2
  "#0()")

(def-syntax-vector-test syntax.sharp-left-paren.3
  "#(a)" a)

(def-syntax-vector-test syntax.sharp-left-paren.4
  "#(a b c)" a b c)

(def-syntax-vector-test syntax.sharp-left-paren.5
  "#2(a)" a a)

(def-syntax-vector-test syntax.sharp-left-paren.6
  "#5(a b)" a b b b b)

(def-syntax-vector-test syntax.sharp-left-paren.7
  "#5(a b c d e)" a b c d e)

(def-syntax-vector-test syntax.sharp-left-paren.8
  "#9(a b c d e)" a b c d e e e e e)

(def-syntax-test syntax.sharp-left-paren.9
  (let ((*read-base* 2))
    (read-from-string "#10(a)"))
  #(a a a a a a a a a a)
  6)

(def-syntax-test syntax.sharp-left-paren.error.1
  (signals-error (read-from-string "#(") end-of-file)
  t)

(def-syntax-test syntax.sharp-left-paren.error.2
  (signals-error (read-from-string "(#(" nil nil) end-of-file)
  t)

;;; Tests of #*

(def-syntax-bit-vector-test syntax.sharp-asterisk.1
  "#*")

(def-syntax-bit-vector-test syntax.sharp-asterisk.2
  "#0*")

(def-syntax-bit-vector-test syntax.sharp-asterisk.3
  "#1*0" 0)

(def-syntax-bit-vector-test syntax.sharp-asterisk.4
  "#1*1" 1)

(def-syntax-bit-vector-test syntax.sharp-asterisk.5
  "#2*1" 1 1)

pfdietz's avatar
pfdietz committed
368
(def-syntax-bit-vector-test syntax.sharp-asterisk.6
pfdietz's avatar
pfdietz committed
369 370
  "#2*0" 0 0)

pfdietz's avatar
pfdietz committed
371
(def-syntax-bit-vector-test syntax.sharp-asterisk.7
pfdietz's avatar
pfdietz committed
372 373
  "#5*010" 0 1 0 0 0)

pfdietz's avatar
pfdietz committed
374
(def-syntax-bit-vector-test syntax.sharp-asterisk.8
pfdietz's avatar
pfdietz committed
375 376
  "#7*0101" 0 1 0 1 1 1 1)

pfdietz's avatar
pfdietz committed
377
(def-syntax-bit-vector-test syntax.sharp-asterisk.9
pfdietz's avatar
pfdietz committed
378 379
  "#10*01010" 0 1 0 1 0 0 0 0 0 0)

pfdietz's avatar
pfdietz committed
380
(def-syntax-test syntax.sharp-asterisk.10
pfdietz's avatar
pfdietz committed
381 382 383 384 385
  (let ((*read-base* 3))
    (read-from-string "#10*01"))
  #*0111111111
  6)

pfdietz's avatar
pfdietz committed
386
(def-syntax-test syntax.sharp-asterisk.11
pfdietz's avatar
pfdietz committed
387 388 389 390
  (let ((*read-suppress* t))
    (values (read-from-string "#1* ")))
  nil)

pfdietz's avatar
pfdietz committed
391
(def-syntax-test syntax.sharp-asterisk.12
pfdietz's avatar
pfdietz committed
392 393 394 395
  (let ((*read-suppress* t))
    (values (read-from-string "#1*00")))
  nil)

pfdietz's avatar
pfdietz committed
396
(def-syntax-test syntax.sharp-asterisk.13
pfdietz's avatar
pfdietz committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  (let ((*read-suppress* t))
    (values (read-from-string "#*012")))
  nil)

(def-syntax-test syntax.sharp-asterisk.error.1
  (signals-error (read-from-string "#1* X") reader-error)
  t)

(def-syntax-test syntax.sharp-asterisk.error.2
  (signals-error (read-from-string "#2*011") reader-error)
  t)

(def-syntax-test syntax.sharp-asterisk.error.3
  (signals-error (read-from-string "#*012") reader-error)
  t)

;;; Tests of #: ...

pfdietz's avatar
pfdietz committed
415
; (def-syntax-unintern-test syntax.sharp-colon.1 "")
pfdietz's avatar
pfdietz committed
416
; (def-syntax-unintern-test syntax.sharp-colon.2 "#")
pfdietz's avatar
pfdietz committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
(def-syntax-unintern-test syntax.sharp-colon.3 "a")
(def-syntax-unintern-test syntax.sharp-colon.4 "A")
(def-syntax-unintern-test syntax.sharp-colon.5 "NIL")
(def-syntax-unintern-test syntax.sharp-colon.6 "T")
(def-syntax-unintern-test syntax.sharp-colon.7 ".")


;;; Tests of #.

(def-syntax-test syntax.sharp-dot.1
  (read-from-string "#.(+ 1 2)")
  3 9)

(def-syntax-test syntax.sharp-dot.2
  (read-from-string "#.'X")
  X 4)

(def-syntax-test syntax.sharp-dot.error.1
  (signals-error (read-from-string "#.") end-of-file)
  t)

(def-syntax-test syntax.sharp-dot.error.2
  (signals-error (read-from-string "(#." nil nil) end-of-file)
  t)

(def-syntax-test syntax.sharp-dot.error.3
  (signals-error (let ((*read-eval* nil)) (read-from-string "#.1")) reader-error)
  t)

;;; Tests of #B

(def-syntax-test syntax.sharp-b.1
  (read-from-string "#b0")
  0 3)

(def-syntax-test syntax.sharp-b.2
  (read-from-string "#B1")
  1 3)

(def-syntax-test syntax.sharp-b.3
  (read-from-string "#b101101")
  45 8)

(def-syntax-test syntax.sharp-b.4
  (read-from-string "#B101101")
  45 8)

(def-syntax-test syntax.sharp-b.5
  (read-from-string "#b010001/100")
  17/4 12)

(def-syntax-test syntax.sharp-b.6
  (read-from-string "#b-10011")
  -19 8)

(def-syntax-test syntax.sharp-b.7
  (read-from-string "#B-1/10")
  -1/2 7)

(def-syntax-test syntax.sharp-b.8
  (read-from-string "#B-0/10")
  0 7)

pfdietz's avatar
pfdietz committed
480
(def-syntax-test syntax.sharp-b.9
pfdietz's avatar
pfdietz committed
481 482 483
  (read-from-string "#b0/111")
  0 7)

pfdietz's avatar
pfdietz committed
484
(def-syntax-test syntax.sharp-b.10
pfdietz's avatar
pfdietz committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
  (let ((*read-eval* nil))
    (read-from-string "#b-10/11"))
  -2/3 8)

;;; Tests of #O

(def-syntax-test syntax.sharp-o.1
  (read-from-string "#o0")
  0 3)

(def-syntax-test syntax.sharp-o.2
  (read-from-string "#O7")
  7 3)

(def-syntax-test syntax.sharp-o.3
  (read-from-string "#o10")
  8 4)

(def-syntax-test syntax.sharp-o.4
  (read-from-string "#O011")
  9 5)

(def-syntax-test syntax.sharp-o.5
  (read-from-string "#o-0")
  0 4)

(def-syntax-test syntax.sharp-o.6
  (read-from-string "#O-1")
  -1 4)

(def-syntax-test syntax.sharp-o.7
  (read-from-string "#O11/10")
  9/8 7)

(def-syntax-test syntax.sharp-o.8
  (read-from-string "#o-1/10")
  -1/8 7)

(def-syntax-test syntax.sharp-o.9
  (read-from-string "#O0/10")
  0 6)

(def-syntax-test syntax.sharp-o.10
  (let ((*read-eval* nil))
    (read-from-string "#o-10/11"))
  -8/9 8)

;;; Tests of #X

(def-syntax-test syntax.sharp-x.1
  (read-from-string "#x0")
  0 3)

(def-syntax-test syntax.sharp-x.2
  (read-from-string "#X1")
  1 3)

(def-syntax-test syntax.sharp-x.3
  (read-from-string "#xa")
  10 3)

(def-syntax-test syntax.sharp-x.4
  (read-from-string "#Xb")
  11 3)

(def-syntax-test syntax.sharp-x.5
  (read-from-string "#XC")
  12 3)

(def-syntax-test syntax.sharp-x.6
  (read-from-string "#xD")
  13 3)

(def-syntax-test syntax.sharp-x.7
  (read-from-string "#xe")
  14 3)

(def-syntax-test syntax.sharp-x.8
  (read-from-string "#Xf")
  15 3)

(def-syntax-test syntax.sharp-x.9
  (read-from-string "#x10")
  16 4)

(def-syntax-test syntax.sharp-x.10
  (read-from-string "#X1ab")
  427 5)

(def-syntax-test syntax.sharp-x.11
  (read-from-string "#x-1")
  -1 4)

(def-syntax-test syntax.sharp-x.12
  (read-from-string "#X-0")
  0 4)

(def-syntax-test syntax.sharp-x.13
  (read-from-string "#xa/B")
  10/11 5)

(def-syntax-test syntax.sharp-x.14
  (read-from-string "#X-1/1c")
  -1/28 7)

(def-syntax-test syntax.sharp-x.15
  (let ((*read-eval* nil))
    (read-from-string "#x-10/11"))
  -16/17 8)

;;; Tests of #nR

(def-syntax-test syntax.sharp-r.1
  (loop for i = (random (ash 1 (+ 2 (random 32))))
sds's avatar
sds committed
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
        for base = (+ 2 (random 35))
        for s = (write-to-string i :radix nil :base base :readably nil)
        for c = (random-from-seq "rR")
        for s2 = (format nil "#~d~c~a" base c s)
        for s3 = (rcase (1 (string-upcase s2))
                        (1 (string-downcase s2))
                        (1 (string-capitalize s2))
                        (1 s2))
        for base2 = (+ 2 (random 35))
        for vals = (let ((*read-base* base2))
                     (multiple-value-list
                      (read-from-string s3)))
        repeat 1000
        unless (equal vals (list i (length s3) ))
        collect (list i base s c s2 s3 base2 vals))
pfdietz's avatar
pfdietz committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
  nil)

(def-syntax-test syntax.sharp-r.2
  (read-from-string "#2r0")
  0 4)

(def-syntax-test syntax.sharp-r.3
  (read-from-string "#36r0")
  0 5)

(def-syntax-test syntax.sharp-r.4
  (read-from-string "#29R-0")
  0 6)

(def-syntax-test syntax.sharp-r.5
  (read-from-string "#23r-1")
  -1 6)

(def-syntax-test syntax.sharp-r.6
  (read-from-string "#17r11")
  18 6)

(def-syntax-test syntax.sharp-t.7
  (read-from-string "#3r10/11")
  3/4 8)

(def-syntax-test syntax.sharp-t.8
  (read-from-string "#5R-10/11")
  -5/6 9)

;;; Tests of #c

(def-syntax-test syntax.sharp-c.1
  (read-from-string "#c(1 1)")
  #.(complex 1 1) 7)

(def-syntax-test syntax.sharp-c.2
  (read-from-string "#C(1 0)")
  1 7)

(def-syntax-test syntax.sharp-c.3
  (read-from-string "#c(0 1)")
  #.(complex 0 1) 7)

(def-syntax-test syntax.sharp-c.4
  (read-from-string "#c(-1/2 1)")
  #.(complex -1/2 1) 10)

(def-syntax-test syntax.sharp-c.5
  (read-from-string "#c (1 1)")
  #.(complex 1 1) 8)
pfdietz's avatar
pfdietz committed
665

pfdietz's avatar
pfdietz committed
666 667
(def-syntax-test syntax.sharp-c.6
  (loop for format in '(short-float single-float double-float long-float)
sds's avatar
sds committed
668 669 670 671 672
        for c = (let ((*read-default-float-format* format))
                  (read-from-string "#c(1.0 0.0)"))
        unless (eql c (complex (coerce 1 format)
                               (coerce 0 format)))
        collect (list format c))
pfdietz's avatar
pfdietz committed
673 674 675 676
  nil)

(def-syntax-test syntax.sharp-c.7
  (loop for format in '(short-float single-float double-float long-float)
sds's avatar
sds committed
677 678 679 680 681
        for c = (let ((*read-default-float-format* format))
                  (read-from-string "#C(0.0 1.0)"))
        unless (eql c (complex (coerce 0 format)
                               (coerce 1 format)))
        collect (list format c))
pfdietz's avatar
pfdietz committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
  nil)

;;; Tests of #a

(def-syntax-array-test syntax.sharp-a.1
  "#0anil"
  (make-array nil :initial-element nil))

(def-syntax-array-test syntax.sharp-a.2
  "#0a1"
  (make-array nil :initial-element 1))

(def-syntax-array-test syntax.sharp-a.3
  "#1a(1 2 3 5)"
  (make-array '(4) :initial-contents '(1 2 3 5)))

(def-syntax-array-test syntax.sharp-a.4
  "#1a\"abcd\""
  (make-array '(4) :initial-contents '(#\a #\b #\c #\d)))

(def-syntax-array-test syntax.sharp-a.5
  "#1a#1a(:a :b :c)"
  (make-array '(3) :initial-contents '(:a :b :c)))

(def-syntax-array-test syntax.sharp-a.6
  "#1a#.(coerce \"abcd\" 'simple-base-string)"
  (make-array '(4) :initial-contents '(#\a #\b #\c #\d)))

(def-syntax-array-test syntax.sharp-a.7
  "#1a#*000110"
  (make-array '(6) :initial-contents '(0 0 0 1 1 0)))

(def-syntax-array-test syntax.sharp-a.8
  "#1a#.(make-array 4 :element-type '(unsigned-byte 8)
                      :initial-contents '(1 2 3 5))"
  (make-array '(4) :initial-contents '(1 2 3 5)))

(def-syntax-array-test syntax.sharp-a.9
  "#1a#.(make-array 4 :element-type '(unsigned-byte 4)
                      :initial-contents '(1 2 3 5))"
  (make-array '(4) :initial-contents '(1 2 3 5)))

(def-syntax-array-test syntax.sharp-a.10
  "#1a#.(make-array 4 :element-type '(signed-byte 4)
                      :initial-contents '(1 2 3 5))"
  (make-array '(4) :initial-contents '(1 2 3 5)))

(def-syntax-array-test syntax.sharp-a.11
  "#1a#.(make-array 4 :element-type '(signed-byte 8)
                      :initial-contents '(1 2 3 5))"
  (make-array '(4) :initial-contents '(1 2 3 5)))

(def-syntax-array-test syntax.sharp-a.12
  "#1a#.(make-array 4 :element-type '(unsigned-byte 16)
                      :initial-contents '(1 2 3 5))"
  (make-array '(4) :initial-contents '(1 2 3 5)))

(def-syntax-array-test syntax.sharp-a.13
  "#1a#.(make-array 4 :element-type '(signed-byte 16)
                      :initial-contents '(1 2 3 5))"
  (make-array '(4) :initial-contents '(1 2 3 5)))

(def-syntax-array-test syntax.sharp-a.14
  "#1a#.(make-array 4 :element-type '(unsigned-byte 32)
                      :initial-contents '(1 2 3 5))"
  (make-array '(4) :initial-contents '(1 2 3 5)))

(def-syntax-array-test syntax.sharp-a.15
  "#1a#.(make-array 4 :element-type '(signed-byte 32)
                      :initial-contents '(1 2 3 5))"
  (make-array '(4) :initial-contents '(1 2 3 5)))

(def-syntax-array-test syntax.sharp-a.16
  "#1a#.(make-array 4 :element-type 'fixnum
                      :initial-contents '(1 2 3 5))"
  (make-array '(4) :initial-contents '(1 2 3 5)))

(def-syntax-array-test syntax.sharp-a.17
  "#1anil"
  (make-array '(0)))

(def-syntax-array-test syntax.sharp-a.18
  "#2anil"
  (make-array '(0 0)))

(def-syntax-array-test syntax.sharp-a.19
  "#2a((2))"
  (make-array '(1 1) :initial-element 2))

(def-syntax-array-test syntax.sharp-a.20
  "#2a((1 2 3)(4 5 6))"
  (make-array '(2 3) :initial-contents #(#(1 2 3) #(4 5 6))))

(def-syntax-array-test syntax.sharp-a.21
  "#2a#(#(1 2 3)#(4 5 6))"
  (make-array '(2 3) :initial-contents '((1 2 3) (4 5 6))))

(def-syntax-array-test syntax.sharp-a.22
  "#2a\"\""
  (make-array '(0 0)))

(def-syntax-array-test syntax.sharp-a.23
  "#2a#*"
  (make-array '(0 0)))

(def-syntax-array-test syntax.sharp-a.24
  "#1a#.(make-array '(10) :fill-pointer 5 :initial-element 17)"
  (make-array '(5) :initial-contents '(17 17 17 17 17)))

(def-syntax-array-test syntax.sharp-a.25
  "#1a#.(make-array '(5) :adjustable t :initial-element 17)"
  (make-array '(5) :initial-contents '(17 17 17 17 17)))

(def-syntax-array-test syntax.sharp-a.26
  "#1A#.(let ((x (make-array '(10) :adjustable t
                      :initial-contents '(1 2 3 4 5 6 7 8 9 10))))
           (make-array '(5) :displaced-to x :displaced-index-offset 2))"
  (make-array '(5) :initial-contents '(3 4 5 6 7)))

;;; Tests of #S

pfdietz's avatar
pfdietz committed
803
(unless (find-class 'syntax-test-struct-1 nil)
804 805
  (defstruct syntax-test-struct-1
    a b c))
pfdietz's avatar
pfdietz committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918

(def-syntax-test syntax.sharp-s.1
  (let ((v (read-from-string "#s(syntax-test-struct-1)")))
    (values
     (notnot (typep v 'syntax-test-struct-1))
     (syntax-test-struct-1-a v)
     (syntax-test-struct-1-b v)
     (syntax-test-struct-1-c v)))
  t nil nil nil)

(def-syntax-test syntax.sharp-s.2
  (let ((v (read-from-string "#S(syntax-test-struct-1 :a x :c y :b z)")))
    (values
     (notnot (typep v 'syntax-test-struct-1))
     (syntax-test-struct-1-a v)
     (syntax-test-struct-1-b v)
     (syntax-test-struct-1-c v)))
  t x z y)

(def-syntax-test syntax.sharp-s.3
  (let ((v (read-from-string "#s(syntax-test-struct-1 \"A\" x)")))
    (values
     (notnot (typep v 'syntax-test-struct-1))
     (syntax-test-struct-1-a v)
     (syntax-test-struct-1-b v)
     (syntax-test-struct-1-c v)))
  t x nil nil)

(def-syntax-test syntax.sharp-s.4
  (let ((v (read-from-string "#S(syntax-test-struct-1 #\\A x)")))
    (values
     (notnot (typep v 'syntax-test-struct-1))
     (syntax-test-struct-1-a v)
     (syntax-test-struct-1-b v)
     (syntax-test-struct-1-c v)))
  t x nil nil)

(def-syntax-test syntax.sharp-s.5
  (let ((v (read-from-string "#s(syntax-test-struct-1 :a x :a y)")))
    (values
     (notnot (typep v 'syntax-test-struct-1))
     (syntax-test-struct-1-a v)
     (syntax-test-struct-1-b v)
     (syntax-test-struct-1-c v)))
  t x nil nil)

(def-syntax-test syntax.sharp-s.6
  (let ((v (read-from-string "#S(syntax-test-struct-1 :a x :allow-other-keys 1)")))
    (values
     (notnot (typep v 'syntax-test-struct-1))
     (syntax-test-struct-1-a v)
     (syntax-test-struct-1-b v)
     (syntax-test-struct-1-c v)))
  t x nil nil)

(def-syntax-test syntax.sharp-s.7
  (let ((v (read-from-string "#s(syntax-test-struct-1 :b z :allow-other-keys nil)")))
    (values
     (notnot (typep v 'syntax-test-struct-1))
     (syntax-test-struct-1-a v)
     (syntax-test-struct-1-b v)
     (syntax-test-struct-1-c v)))
  t nil z nil)


(def-syntax-test syntax.sharp-s.8
  (let ((v (read-from-string "#S(syntax-test-struct-1 :b z :allow-other-keys t :a x :foo bar)")))
    (values
     (notnot (typep v 'syntax-test-struct-1))
     (syntax-test-struct-1-a v)
     (syntax-test-struct-1-b v)
     (syntax-test-struct-1-c v)))
  t x z nil)

(def-syntax-test syntax.sharp-s.9
  (let ((v (read-from-string "#s(syntax-test-struct-1 a x c y b z :a :bad :b bad2 :c bad3)")))
    (values
     (notnot (typep v 'syntax-test-struct-1))
     (syntax-test-struct-1-a v)
     (syntax-test-struct-1-b v)
     (syntax-test-struct-1-c v)))
  t x z y)

(def-syntax-test syntax.sharp-s.10
  (let ((v (read-from-string "#S(syntax-test-struct-1 #:a x #:c y #:b z)")))
    (values
     (notnot (typep v 'syntax-test-struct-1))
     (syntax-test-struct-1-a v)
     (syntax-test-struct-1-b v)
     (syntax-test-struct-1-c v)))
  t x z y)

;; (Put more tests of this in the structure tests)

;;; Tests of #P

(def-syntax-test syntax.sharp-p.1
  (read-from-string "#p\"\"")
  #.(parse-namestring "") 4)

(def-syntax-test syntax.sharp-p.2
  (read-from-string "#P\"syntax.lsp\"")
  #.(parse-namestring "syntax.lsp") 14)

(def-syntax-test syntax.sharp-p.3
  (read-from-string "#P \"syntax.lsp\"")
  #.(parse-namestring "syntax.lsp") 15)

(def-syntax-test syntax.sharp-p.4
  (let ((*read-eval* nil))
    (read-from-string "#p\"syntax.lsp\""))
  #.(parse-namestring "syntax.lsp") 14)

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
(def-syntax-test syntax.sharp-p.5
  (read-from-string "#P#.(make-array '(10) :initial-contents \"syntax.lsp\" :element-type 'base-char)")
  #.(parse-namestring "syntax.lsp") 78)

;;; #<number># and #<number>=

(def-syntax-test syntax.sharp-circle.1
  (let ((x (read-from-string "(#1=(17) #1#)")))
    (assert (eq (car x) (cadr x)))
    x)
  ((17) (17)))

(def-syntax-test syntax.sharp-circle.2
  (let ((x (read-from-string "(#0=(17) #0#)")))
    (assert (eq (car x) (cadr x)))
    x)
  ((17) (17)))

(def-syntax-test syntax.sharp-circle.3
  (let ((x (read-from-string "(#123456789123456789=(17) #123456789123456789#)")))
    (assert (eq (car x) (cadr x)))
    x)
  ((17) (17)))

(def-syntax-test syntax.sharp-circle.4
  (let ((x (read-from-string "#1=(A B . #1#)")))
    (assert (eq (cddr x) x))
    (values (car x) (cadr x)))
  a b)

(def-syntax-test syntax.sharp-circle.5
  (let ((x (read-from-string "#1=#(A B #1#)")))
    (assert (typep x '(simple-vector 3)))
    (assert (eq (elt x 2) x))
    (values (elt x 0) (elt x 1)))
  a b)

(def-syntax-test syntax.sharp-circle.6
  (let ((x (read-from-string "((#1=(17)) #1#)")))
    (assert (eq (caar x) (cadr x)))
    x)
  (((17)) (17)))

(def-syntax-test syntax.sharp-circle.7
  (let ((x (read-from-string "((#1=#2=(:x)) #1# #2#)")))
    (assert (eq (caar x) (cadr x)))
    (assert (eq (caar x) (caddr x)))
    x)
  (((:x)) (:x) (:x)))

;;; #+

(def-syntax-test syntax.sharp-plus.1
  (let ((*features* nil))
    (read-from-string "#+X :bad :good"))
  :good 14)

(def-syntax-test syntax.sharp-plus.2
  (let ((*features* '(:a :x :b)))
    (read-from-string "#+X :good :bad"))
  :good 10)

(def-syntax-test syntax.sharp-plus.3
  (let ((*features* '(:a :x :b)))
    (read-from-string "#+:x :good :bad"))
  :good 11)

(def-syntax-test syntax.sharp-plus.4
  (let ((*features* '(:a :x :b)))
    (read-from-string "#+(and):good :bad"))
  :good 13)

(def-syntax-test syntax.sharp-plus.5
  (let ((*features* '(:a :x :b)))
    (read-from-string "#+(:and):good :bad"))
  :good 14)

(def-syntax-test syntax.sharp-plus.6
  (let ((*features* '(:a :x :b)))
    (read-from-string "#+(or) :bad :good"))
  :good 17)

(def-syntax-test syntax.sharp-plus.7
  (let ((*features* '(:a :x :b)))
    (read-from-string "#+(:or) :bad :good"))
  :good 18)

(def-syntax-test syntax.sharp-plus.8
  (let ((*features* '(x)))
    (read-from-string "#+X :bad :good"))
  :good 14)

(def-syntax-test syntax.sharp-plus.9
  (let ((*features* '(x)))
    (read-from-string "#+CL-TEST::X :good :bad"))
  :good 19)

(def-syntax-test syntax.sharp-plus.10
  (let ((*features* nil))
    (read-from-string "#+(not x) :good :bad"))
  :good 16)

(def-syntax-test syntax.sharp-plus.11
  (let ((*features* '(:x)))
    (read-from-string "#+(not x) :bad :good"))
  :good 20)

(def-syntax-test syntax.sharp-plus.12
  (let ((*features* nil))
    (read-from-string "#+(:not :x) :good :bad"))
  :good 18)

(def-syntax-test syntax.sharp-plus.13
  (let ((*features* '(:a :x :b)))
    (read-from-string "#+(and a b) :good :bad"))
  :good 18)

(def-syntax-test syntax.sharp-plus.14
  (let ((*features* '(:a :x :b)))
    (read-from-string "#+(and a c) :bad :good"))
  :good 22)

(def-syntax-test syntax.sharp-plus.15
  (let ((*features* '(:a :x :b)))
    (read-from-string "#+(or c b) :good :bad"))
  :good 17)

(def-syntax-test syntax.sharp-plus.16
  (let ((*features* '(:a :x :b)))
    (read-from-string "#+(or c d) :bad :good"))
  :good 21)

;;; Tests of #| |#

(def-syntax-test syntax.sharp-bar.1
  (read-from-string "#||#1")
  1 5)

(def-syntax-test syntax.sharp-bar.2
  (read-from-string "1#||#2")
  |1##2| 6)

(def-syntax-test syntax.sharp-bar.3
  (read-from-string "#| #| |# |#1")
  1 12)

(def-syntax-test syntax.sharp-bar.4
  (read-from-string "#| ; |#1")
  1 8)

(def-syntax-test syntax.sharp-bar.5
  (read-from-string "#| ( |#1")
  1 8)

(def-syntax-test syntax.sharp-bar.6
  (read-from-string "#| # |#1")
  1 8)

(def-syntax-test syntax.sharp-bar.7
  (read-from-string "#| .. |#1")
  1 9)

(def-syntax-test syntax.sharp-bar.8
  (loop for c across +standard-chars+
sds's avatar
sds committed
1083 1084 1085 1086
        for s = (concatenate 'string "\#| " (string c) " |\#1")
        for vals = (multiple-value-list (read-from-string s))
        unless (equal vals '(1 8))
        collect (list c s vals))
1087 1088 1089 1090
  nil)

(def-syntax-test syntax.sharp-bar.9
  (loop for i below (min (ash 1 16) char-code-limit)
sds's avatar
sds committed
1091 1092 1093 1094 1095
        for c = (code-char i)
        for s = (and c (concatenate 'string "\#| " (string c) " |\#1"))
        for vals = (and c (multiple-value-list (read-from-string s)))
        unless (or (not c) (equal vals '(1 8)))
        collect (list i c s vals))
1096 1097 1098 1099
  nil)

(def-syntax-test syntax.sharp-bar.10
  (loop for i = (random (min (ash 1 24) char-code-limit))
sds's avatar
sds committed
1100 1101 1102 1103 1104 1105
        for c = (code-char i)
        for s = (and c (concatenate 'string "\#| " (string c) " |\#1"))
        for vals = (and c (multiple-value-list (read-from-string s)))
        repeat 1000
        unless (or (not c) (equal vals '(1 8)))
        collect (list i c s vals))
1106
  nil)
pfdietz's avatar
pfdietz committed
1107 1108 1109 1110 1111 1112

;;;; Various error cases

(def-syntax-test syntax.sharp-whitespace.1
  (let ((names '("Tab" "Newline" "Linefeed" "Space" "Return" "Page")))
    (loop for name in names
sds's avatar
sds committed
1113 1114 1115 1116 1117 1118 1119 1120 1121
          for c = (name-char name)
          when c
          nconc
          (let* ((form `(signals-error
                         (read-from-string ,(concatenate 'string "#" (string c)))
                         reader-error))
                 (vals (multiple-value-list (eval form))))
            (unless (equal vals '(t))
              (list (list name c form vals))))))
pfdietz's avatar
pfdietz committed
1122 1123
  nil)

pfdietz's avatar
pfdietz committed
1124
(def-syntax-test syntax.sharp-less-than.1
pfdietz's avatar
pfdietz committed
1125 1126 1127 1128 1129 1130 1131
  (signals-error (read-from-string "#<" nil nil) reader-error)
  t)


(def-syntax-test syntax.sharp-close-paren.1
  (signals-error (read-from-string "#)" nil nil) reader-error)
  t)
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148

(def-syntax-test syntax.single-escape-eof.1
  (signals-error (read-from-string "\\") end-of-file)
  t)

(def-syntax-test syntax.single-escape-eof.2
  (signals-error (read-from-string "\\" nil nil) end-of-file)
  t)

(def-syntax-test syntax.multiple-escape-eof.1
  (signals-error (read-from-string "|") end-of-file)
  t)

(def-syntax-test syntax.multiple-escape-eof.2
  (signals-error (read-from-string "|" nil nil) end-of-file)
  t)