Fix make-array transforms.
[sbcl.git] / tests / reader.impure.lisp
1 ;;;; tests related to the Lisp reader
2
3 ;;;; This file is impure because we want to modify the readtable and stuff.
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; While most of SBCL is derived from the CMU CL system, the test
9 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; from CMU CL.
11 ;;;;
12 ;;;; This software is in the public domain and is provided with
13 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
14 ;;;; more information.
15
16 (load "assertoid.lisp")
17 (use-package "ASSERTOID")
18
19 ;;; Bug 30, involving mistakes in binding the read table, made this
20 ;;; code fail.
21 (defun read-vector (stream char)
22   (declare (ignorable char))
23   (coerce (read-delimited-list #\] stream t) 'vector))
24 (set-macro-character #\[ #'read-vector nil)
25 (set-macro-character #\] (get-macro-character #\)) nil)
26 (multiple-value-bind (res pos)
27     (read-from-string "[1 2 3]") ; ==> #(1 2 3), 7
28   (assert (equalp res #(1 2 3)))
29   (assert (= pos 7)))
30 (multiple-value-bind (res pos)
31     (read-from-string "#\\x") ; ==> #\x, 3
32   (assert (equalp res #\x))
33   (assert (= pos 3)))
34 (multiple-value-bind (res pos)
35     (read-from-string "[#\\x]")
36   (assert (equalp res #(#\x)))
37   (assert (= pos 5)))
38
39 ;;; Bug 51b. (try to throw READER-ERRORs when the reader encounters
40 ;;; dubious input)
41 (assert (raises-error? (read-from-string "1e1000") reader-error))
42 (assert (raises-error? (read-from-string "1/0") reader-error))
43
44 ;;; Bug reported by Antonio Martinez on comp.lang.lisp 2003-02-03 in
45 ;;; message <b32da960.0302030640.7d6fc610@posting.google.com>: reading
46 ;;; circular instances of CLOS classes didn't work:
47 (defclass box ()
48   ((value :initarg :value :reader value)))
49 (defun read-box (stream char)
50   (declare (ignore char))
51   (let ((objects (read-delimited-list #\] stream t)))
52     (unless (= 1 (length objects))
53       (error "Unknown box reader syntax"))
54     (make-instance 'box :value (first objects))))
55 (set-macro-character #\[ 'read-box)
56 (set-syntax-from-char #\] #\))
57 (multiple-value-bind (res pos)
58     (read-from-string "#1=[#1#]")
59   (assert (eq (value res) res))
60   (assert (= pos 8)))
61 ;;; much, much, later (in Feb 2007), CSR noticed that the problem
62 ;;; still exists for funcallable instances.
63 (defclass funcallable-box (box sb-mop:funcallable-standard-object) ()
64   (:metaclass sb-mop:funcallable-standard-class))
65 (defun read-funcallable-box (stream char)
66   (declare (ignore char))
67   (let ((objects (read-delimited-list #\} stream t)))
68     (unless (= 1 (length objects))
69       (error "Unknown box reader syntax"))
70     (make-instance 'funcallable-box :value (first objects))))
71 (set-macro-character #\{ 'read-funcallable-box)
72 (set-syntax-from-char #\} #\))
73 (multiple-value-bind (res pos)
74     (read-from-string "#1={#1#}")
75   (assert (eq (value res) res))
76   (assert (= pos 8)))
77
78 ;;; CSR managed to break the #S reader macro in the process of merging
79 ;;; SB-PCL:CLASS and CL:CLASS -- make sure it works
80 (defstruct readable-struct a)
81 (macrolet
82     ((frob (string)
83        `(assert (eq (readable-struct-a (read-from-string ,string)) t))))
84   (frob "#S(READABLE-STRUCT :A T)")
85   (frob "#S(READABLE-STRUCT A T)")
86   (frob "#S(READABLE-STRUCT \"A\" T)")
87   (frob "#S(READABLE-STRUCT #\\A T)")
88   (frob "#S(READABLE-STRUCT #\\A T :A NIL)"))
89 (macrolet
90     ((frob (string)
91        `(assert (raises-error? (read-from-string ,string) reader-error))))
92   (frob "#S(READABLE-STRUCT . :A)")
93   (frob "#S(READABLE-STRUCT :A . T)")
94   (frob "#S(READABLE-STRUCT :A T . :A)")
95   (frob "#S(READABLE-STRUCT :A T :A . T)"))
96
97 ;;; reported by Henrik Motakef
98 (defpackage "")
99 (assert (eq (symbol-package (read-from-string "||::FOO"))
100             (find-package "")))
101
102 ;;; test nested reads, test case by Helmut Eller for cmucl
103 (defclass my-in-stream (sb-gray:fundamental-character-input-stream)
104   ((last-char :initarg :last-char)))
105
106 (let ((string " a ")
107       (i 0))
108   (defmethod sb-gray:stream-read-char ((s my-in-stream))
109     (with-input-from-string (s "b") (read s))
110     (with-slots (last-char) s
111       (cond (last-char (prog1 last-char (setf last-char nil)))
112              (t (prog1 (aref string i)
113                   (setq i (mod (1+ i) (length string)))))))))
114
115 (defmethod sb-gray:stream-unread-char ((s my-in-stream) char)
116   (setf (slot-value s 'last-char) char)
117   nil)
118
119 (assert (eq 'a (read (make-instance 'my-in-stream :last-char nil))))
120
121 ;;; NIL as the last argument to SET-SYNTAX-FROM-CHAR in compiled code,
122 ;;; reported by Levente Mészáros
123 (let ((fun (compile nil '(lambda ()
124                           (set-syntax-from-char #\{ #\( *readtable* nil)))))
125   (funcall fun)
126   (assert (equal '(:ok) (read-from-string "{:ok)"))))
127
128 (with-test (:name :bad-recursive-read)
129   ;; This use to signal an unbound-variable error instead.
130   (assert (eq :error
131               (handler-case
132                   (with-input-from-string (s "42")
133                     (read s t nil t))
134                 (reader-error (e)
135                   :error)))))
136
137 (with-test (:name :standard-readtable-modified)
138   (macrolet ((test (form &optional op)
139                `(assert
140                  (eq :error
141                      (handler-case
142                          (progn ,form t)
143                        (sb-int:standard-readtable-modified-error (e)
144                          ,@(when op
145                             `((assert
146                                (equal ,op (sb-kernel::standard-readtable-modified-operation e)))))
147                          :error))))))
148     (let ((rt *readtable*))
149      (with-standard-io-syntax
150        (let ((srt *readtable*))
151          (test (setf (readtable-case srt) :preserve) '(setf readtable-case))
152          (test (copy-readtable rt srt) 'copy-readtable)
153          (test (set-syntax-from-char #\a #\a srt rt) 'set-syntax-from-char)
154          (test (set-macro-character #\a (constantly t) t srt) 'set-macro-character)
155          (test (make-dispatch-macro-character #\! t srt))
156          (test (set-dispatch-macro-character #\# #\a (constantly t) srt) 'set-dispatch-macro-character))))))
157
158 (with-test (:name :reader-package-errors)
159   (flet ((test (string)
160            (handler-case
161                (progn (read-from-string string) :feh)
162              (error (e)
163                (when (and (typep e 'reader-error) (typep e 'package-error))
164                  (package-error-package e))))))
165     (assert (equal "NO-SUCH-PKG" (test "no-such-pkg::foo")))
166     (assert (eq (find-package :cl) (test "cl:no-such-sym")))))
167
168 ;;; THIS SHOULD BE LAST as it frobs the standard readtable
169 (with-test (:name :set-macro-character-nil)
170   (handler-bind ((sb-int:standard-readtable-modified-error #'continue))
171     (let ((fun (lambda (&rest args) 'ok)))
172       ;; NIL means the standard readtable.
173       (assert (eq t (set-macro-character #\~ fun nil nil)))
174       (assert (eq fun (get-macro-character #\~ nil)))
175       (assert (eq t (set-dispatch-macro-character #\# #\~ fun nil)))
176       (assert (eq fun (get-dispatch-macro-character #\# #\~ nil))))))
177
178 ;;; success