Don't pass thread context to another thread when handling SIGNIT.
[sbcl.git] / tests / print.impure.lisp
blobbf9f43c7143563c534f45305255cd7373cc1945f
1 ;;;; miscellaneous tests of printing stuff
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (load "assertoid.lisp")
15 (use-package "ASSERTOID")
17 ;;; We should be able to output X readably (at least when *READ-EVAL*).
18 (defun assert-readable-output (x)
19 (assert (eql x
20 (let ((*read-eval* t))
21 (read-from-string (with-output-to-string (s)
22 (write x :stream s :readably t)))))))
24 ;;; Even when *READ-EVAL* is NIL, we should be able to output some
25 ;;; (not necessarily readable) representation without signalling an
26 ;;; error.
27 (defun assert-unreadable-output (x)
28 (let ((*read-eval* nil))
29 (with-output-to-string (s) (write x :stream s :readably nil))))
31 (defun assert-output (x)
32 (assert-readable-output x)
33 (assert-unreadable-output x))
35 ;;; Ensure that we don't print a value cell as #S(RANDOM-CLASS ...)
36 (defun f (x) (lambda (y) (+ (incf x) y)))
37 (compile 'f)
38 (with-test (:name :output-value-cell)
39 (assert (search "#<value cell"
40 (write-to-string (sb-kernel:%closure-index-ref (f 3) 0)))))
42 ;;; Nathan Froyd reported that sbcl-0.6.11.34 screwed up output of
43 ;;; floating point infinities.
44 (with-test (:name (write float :infinities))
45 (dolist (x (list short-float-positive-infinity short-float-negative-infinity
46 single-float-positive-infinity single-float-negative-infinity
47 double-float-positive-infinity double-float-negative-infinity
48 long-float-positive-infinity long-float-negative-infinity))
49 (assert-output x)))
51 ;;; Eric Marsden reported that this would blow up in CMU CL (even
52 ;;; though ANSI says that the mismatch between ~F expected type and
53 ;;; provided string type is supposed to be handled without signalling
54 ;;; an error) and provided a fix which was ported to sbcl-0.6.12.35.
55 (with-test (:name (format :fixed-format-floating-point-directive :type-mismatch))
56 (let ((*standard-output* (make-broadcast-stream)))
57 (assert (null (format t "~F" "foo")))))
59 ;;; This was a bug in SBCL until 0.6.12.40 (originally reported as a
60 ;;; CMU CL bug by Erik Naggum on comp.lang.lisp).
61 (with-test (:name (format bit-vector *print-base* *print-radix*))
62 (loop for base from 2 to 36
63 with *print-radix* = t
64 do (let ((*print-base* base))
65 (assert (string= "#*101" (format nil "~S" #*101))))))
67 ;;; bug in sbcl-0.7.1.25, reported by DB sbcl-devel 2002-02-25
68 (with-test (:name (:format :decimal-directive :type-mismatch))
69 (assert (string= "0.5" (format nil "~2D" 0.5))))
71 ;;; we want malformed format strings to cause errors rather than have
72 ;;; some DWIM "functionality".
73 (with-test (:name (format :tabulate-directive :malformed))
74 (multiple-value-bind (fun failure-p warnings)
75 (checked-compile `(lambda () (format nil "~:2T"))
76 :allow-failure t :allow-warnings t)
77 (assert failure-p)
78 (assert (= 1 (length warnings)))
79 (assert-error (funcall fun))))
81 ;;; bug reported, with fix, by Robert Strandh, sbcl-devel 2002-03-09,
82 ;;; fixed in sbcl-0.7.1.36:
83 (with-test (:name (format :monetary-floating-point-directive 1))
84 (assert (string= (format nil "~2,3,8,'0$" 1234567.3d0) "1234567.30")))
86 ;;; checks that other FORMAT-DOLLAR output remains sane after the
87 ;;; 0.7.1.36 change
88 (with-test (:name (format :monetary-floating-point-directive 2))
89 (assert (string= (format nil "~$" 0) "0.00"))
90 (assert (string= (format nil "~$" 4) "4.00"))
91 (assert (string= (format nil "~$" -4.0) "-4.00"))
92 (assert (string= (format nil "~2,7,11$" -4.0) "-0000004.00"))
93 (assert (string= (format nil "~2,7,11,' $" 1.1) " 0000001.10"))
94 (assert (string= (format nil "~1,7,11,' $" 1.1) " 0000001.1"))
95 (assert (string= (format nil "~1,3,8,' $" 7.3) " 007.3"))
96 (assert (string= (format nil "~2,3,8,'0$" 7.3) "00007.30")))
98 ;;; Check for symbol lookup in ~/ / directive -- double-colon was
99 ;;; broken in 0.7.1.36 and earlier
100 (defun print-foo (stream arg colonp atsignp &rest params)
101 (declare (ignore colonp atsignp params))
102 (format stream "~d" arg))
104 (with-test (:name (format :call-function-directive :smoke))
105 (assert (string= (format nil "~/print-foo/" 2) "2"))
106 (assert (string= (format nil "~/cl-user:print-foo/" 2) "2"))
107 (assert (string= (format nil "~/cl-user::print-foo/" 2) "2")))
109 (with-test (:name (format :call-function-directive :syntax-errors))
110 (flet ((test (args)
111 (multiple-value-bind (fun failure-p)
112 (checked-compile `(lambda (format nil ,@args))
113 :allow-failure t)
114 (assert failure-p)
115 (assert-error (funcall fun)))))
116 (test '("~/cl-user:::print-foo/" 2))
117 (test '("~/cl-user:a:print-foo/" 2))
118 (test '("~/a:cl-user:print-foo/" 2))
119 (test '("~/cl-user:print-foo:print-foo/" 2))))
121 ;;; better make sure that we get this one right, too
122 (defun print-foo\:print-foo (stream arg colonp atsignp &rest params)
123 (declare (ignore colonp atsignp params))
124 (format stream "~d" arg))
126 (with-test (:name (format :call-function-directive :colon-in-function-name))
127 (assert (string= (format nil "~/cl-user:print-foo:print-foo/" 2) "2"))
128 (assert (string= (format nil "~/cl-user::print-foo:print-foo/" 2) "2")))
130 ;;; Check for error detection of illegal directives in a~<..~> justify
131 ;;; block (see ANSI section 22.3.5.2)
132 (with-test (:name (format :justification-directive :illegal-directives))
133 (flet ((test (args)
134 (multiple-value-bind (fun failure-p)
135 (checked-compile `(lambda () (format nil ,@args))
136 :allow-failure t)
137 (assert failure-p)
138 (assert-error (funcall fun)))))
139 (test '("~<~W~>" 'foo))
140 (test '("~<~<~A~:>~>" '(foo))))
141 (assert (string= (format nil "~<~<~A~>~>" 'foo) "FOO")))
143 (with-test (:name (format :justification-directive :atsign-check))
144 (multiple-value-bind (fun failure-p)
145 (checked-compile `(lambda () (format nil "~<~@>"))
146 :allow-failure t)
147 (assert failure-p)
148 (assert-error (funcall fun)))
149 (assert-error (eval '(format nil "~<~@>"))))
151 ;;; Check that arrays that we print while *PRINT-READABLY* is true are
152 ;;; in fact generating similar objects.
153 (with-test (:name (print array *print-readably* :dimensions))
154 (assert (equal (array-dimensions
155 (read-from-string
156 (with-output-to-string (s)
157 (let ((*print-readably* t))
158 (print (make-array '(1 2 0)) s)))))
159 '(1 2 0))))
161 (with-test (:name (print array *print-readably* :element-type))
162 (dolist (array (list (make-array '(1 0 1))
163 (make-array 0 :element-type nil)
164 (make-array 1 :element-type 'base-char)
165 (make-array 1 :element-type 'character)))
166 (assert (multiple-value-bind (result error)
167 (ignore-errors (read-from-string
168 (with-output-to-string (s)
169 (let ((*print-readably* t))
170 (print array s)))))
171 ;; it might not be readably-printable
172 (or (typep error 'print-not-readable)
173 (and
174 ;; or else it had better have the same dimensions
175 (equal (array-dimensions result) (array-dimensions array))
176 ;; and the same element-type
177 (equal (array-element-type result) (array-element-type array))))))))
179 ;;; before 0.8.0.66 it signalled UNBOUND-VARIABLE
180 (with-test (:name (write vector :smoke))
181 (let ((*standard-output* (make-broadcast-stream)))
182 (write #(1 2 3) :pretty nil :readably t)))
184 ;;; another UNBOUND-VARIABLE, this time due to a bug in FORMATTER
185 ;;; expanders.
186 (with-test (:name (formatter :smoke))
187 (funcall (formatter "~@<~A~:*~A~:>") (make-broadcast-stream) 3))
189 ;;; the PPC floating point backend was at one point sufficiently
190 ;;; broken that this looped infinitely or caused segmentation
191 ;;; violations through stack corruption.
192 (with-test (:name (print float :smoke))
193 (let ((*standard-output* (make-broadcast-stream)))
194 (print 0.0001)))
196 ;;; In sbcl-0.8.7, the ~W format directive interpreter implemented the
197 ;;; sense of the colon and at-sign modifiers exactly backwards.
199 ;;; (Yes, the test for this *is* substantially hairier than the fix;
200 ;;; wanna make something of it?)
201 (cl:in-package :cl-user)
202 (defstruct wexerciser-0-8-7)
203 (defun wexercise-0-8-7-interpreted (wformat)
204 (format t wformat (make-wexerciser-0-8-7)))
205 (defmacro define-compiled-wexercise-0-8-7 (wexercise wformat)
206 `(defun ,wexercise ()
207 (declare (optimize (speed 3) (space 1)))
208 (format t ,wformat (make-wexerciser-0-8-7))
209 (values)))
210 (define-compiled-wexercise-0-8-7 wexercise-0-8-7-compiled-without-atsign "~W")
211 (define-compiled-wexercise-0-8-7 wexercise-0-8-7-compiled-with-atsign "~@W")
212 (defmethod print-object :before ((wexerciser-0-8-7 wexerciser-0-8-7) stream)
213 (unless (and *print-level* *print-length*)
214 (error "gotcha coming")))
215 (with-test (:name (format :write-directive :colon :at-sign 1))
216 (let ((*print-level* 11)
217 (*print-length* 12)
218 (*standard-output* (make-broadcast-stream)))
219 (wexercise-0-8-7-interpreted "~W")
220 (wexercise-0-8-7-compiled-without-atsign)))
221 (remove-method #'print-object
222 (find-method #'print-object
223 '(:before)
224 (mapcar #'find-class '(wexerciser-0-8-7 t))))
225 (defmethod print-object :before ((wexerciser-0-8-7 wexerciser-0-8-7) stream)
226 (when (or *print-level* *print-length*)
227 (error "gotcha going")))
228 (with-test (:name (format :write-directive :colon :at-sign 2))
229 (let ((*print-level* 11)
230 (*print-length* 12)
231 (*standard-output* (make-broadcast-stream)))
232 (wexercise-0-8-7-interpreted "~@W")
233 (wexercise-0-8-7-compiled-with-atsign)))
235 ;;; WRITE-TO-STRING was erroneously DEFKNOWNed as FOLDABLE
237 ;;; This bug from PFD
238 (defpackage "SCRATCH-WRITE-TO-STRING" (:use))
239 (with-test (:name (write symbol *package*))
240 (with-standard-io-syntax
241 (let* ((*package* (find-package "SCRATCH-WRITE-TO-STRING"))
242 (answer (write-to-string 'scratch-write-to-string::x :readably nil)))
243 (assert (string= answer "X")))))
245 ;;; and a couple from Bruno Haible
246 (defun my-pprint-reverse (out list)
247 (write-char #\( out)
248 (when (setq list (reverse list))
249 (loop
250 (write (pop list) :stream out)
251 (when (endp list) (return))
252 (write-char #\Space out)))
253 (write-char #\) out))
254 (with-test (:name (write *print-pprint-dispatch* 1))
255 (with-standard-io-syntax
256 (let ((*print-pprint-dispatch* (copy-pprint-dispatch)))
257 (set-pprint-dispatch '(cons (member foo)) 'my-pprint-reverse 0)
258 (let ((answer (write-to-string '(foo bar :boo 1) :pretty t :escape t)))
259 (assert (string= answer "(1 :BOO BAR FOO)"))))))
261 (defun my-pprint-logical (out list)
262 (pprint-logical-block (out list :prefix "(" :suffix ")")
263 (when list
264 (loop
265 (write-char #\? out)
266 (write (pprint-pop) :stream out)
267 (write-char #\? out)
268 (pprint-exit-if-list-exhausted)
269 (write-char #\Space out)))))
270 (with-test (:name (write *print-pprint-dispatch* 2))
271 (with-standard-io-syntax
272 (let ((*print-pprint-dispatch* (copy-pprint-dispatch)))
273 (set-pprint-dispatch '(cons (member bar)) 'my-pprint-logical 0)
274 (let ((answer (write-to-string '(bar foo :boo 1) :pretty t :escape t)))
275 (assert (string= answer "(?BAR? ?FOO? ?:BOO? ?1?)"))))))
277 ;;; FORMAT string compile-time checker failure, reported by Thomas
278 ;;; F. Burdick
279 (with-test (:name (format :compile-time-check))
280 (multiple-value-bind (fun failure-p warnings)
281 (checked-compile '(lambda () (format nil "~{"))
282 :allow-failure t :allow-warnings t)
283 (assert failure-p)
284 (assert (= (length warnings) 1))
285 (assert-error (funcall fun))))
287 ;;; floating point print/read consistency
288 (with-test (:name (read print float :consistency))
289 (let* ((x (/ -9.349640046247849d-21 -9.381494249123696d-11))
290 (y (read-from-string (write-to-string x :readably t))))
291 (assert (eql x y)))
293 (let ((x1 (float -5496527/100000000000000000))
294 (x2 (float -54965272/1000000000000000000)))
295 (assert (or (equal (multiple-value-list (integer-decode-float x1))
296 (multiple-value-list (integer-decode-float x2)))
297 (string/= (prin1-to-string x1) (prin1-to-string x2))))))
299 ;;; readable printing of arrays with *print-radix* t
300 (with-test (:name (write read array *print-radix*))
301 (let ((*print-radix* t)
302 (*print-readably* t)
303 (*print-pretty* nil))
304 (let ((output (with-output-to-string (s)
305 (write #2a((t t) (nil nil)) :stream s))))
306 (assert (equalp (read-from-string output) #2a((t t) (nil nil)))))))
308 ;;; NIL parameters to "interpreted" FORMAT directives
309 (with-test (:name (format :v-directive-arg nil))
310 (assert (string= (format nil "~v%" nil) (string #\Newline))))
312 ;;; PRINC-TO-STRING should bind print-readably
313 (with-test (:name (princ-to-string *print-readably*))
314 (let ((*print-readably* t))
315 (assert (string= (princ-to-string #\7)
316 (write-to-string #\7 :escape nil :readably nil)))))
318 ;;; in FORMAT, ~^ inside ~:{ should go to the next case, not break
319 ;;; iteration, even if one argument is just a one-element list.
320 (with-test (:name (format :escape-upward-directive :in :iteration-directive))
321 (assert (string= (format nil "~:{~A~^~}" '((A) (C D))) "AC")))
323 ;;; errors should be signaled if pprint and justification are mixed
324 ;;; injudiciously...
325 (with-test (:name (format :mixing :justification-directive :pprint-directives :illegal))
326 (dolist (x (list "~<~:;~>~_" "~<~:;~>~I" "~<~:;~>~W"
327 "~<~:;~>~:T" "~<~:;~>~<~:>" "~_~<~:;~>"
328 "~I~<~:;~>" "~W~<~:;~>" "~:T~<~:;~>" "~<~:>~<~:;~>"))
329 (assert-error (format nil x nil))
330 (assert-error (format nil (eval `(formatter ,x)) nil))))
331 ;;; ...but not in judicious cases.
332 (with-test (:name (format :mixing :justification-directive :pprint-directives :legal))
333 (dolist (x (list "~<~;~>~_" "~<~;~>~I" "~<~;~>~W"
334 "~<~;~>~:T" "~<~;~>~<~>" "~_~<~;~>"
335 "~I~<~;~>" "~W~<~;~>" "~:T~<~;~>" "~<~>~<~;~>"
336 "~<~:;~>~T" "~T~<~:;~>"))
337 (assert (format nil x nil))
338 (assert (format nil (eval `(formatter ,x)) nil))))
340 ;;; bug 350: bignum printing so memory-hungry that heap runs out
341 ;;; -- just don't stall here forever on a slow box
342 (with-test (:name :bug-350)
343 (handler-case
344 (with-timeout 10
345 (print (ash 1 1000000) (make-broadcast-stream)))
346 (timeout ()
347 (print 'timeout!))))
349 ;;; bug 371: bignum print/read inconsistency
350 (defvar *bug-371* -7043009959286724629649270926654940933664689003233793014518979272497911394287216967075767325693021717277238746020477538876750544587281879084559996466844417586093291189295867052594478662802691926547232838591510540917276694295393715934079679531035912244103731582711556740654671309980075069010778644542022/670550434139267031632063192770201289106737062379324644110801846820471752716238484923370056920388400273070254958650831435834503195629325418985020030706879602898158806736813101434594805676212779217311897830937606064579213895527844045511878668289820732425014254579493444623868748969110751636786165152601)
351 (with-test (:name (read print bignum :consistency :bug-371))
352 (let ((*print-base* 5)
353 (*read-base* 5)
354 (*print-radix* nil))
355 (assert (= *bug-371* (read-from-string (prin1-to-string *bug-371*))))))
357 ;;; a spot of random-testing for rational printing
358 (defvar *seed-state* (make-random-state))
359 (with-open-file (f "last-random-state.lisp-expr"
360 :direction :output :if-exists :supersede)
361 ;; I don't want to see this every time
362 (write *seed-state* :pretty nil :stream f)) ; so that we can reproduce errors
363 (with-test (:name (read print rational :consistency ))
364 (let ((seed (make-random-state *seed-state*)))
365 (loop repeat 42
366 do (let ((n (random (ash 1 1000) seed))
367 (d (random (ash 1 1000) seed)))
368 (when (zerop (random 2 seed))
369 (setf n (- n)))
370 (let ((r (/ n d)))
371 (loop for base from 2 to 36
372 do (let ((*print-base* base)
373 (*read-base* base)
374 (*print-radix* nil))
375 (assert (= r (read-from-string (prin1-to-string r))))
376 (if (= 36 base)
377 (decf *read-base*)
378 (incf *read-base*))
379 (assert (not (eql r (read-from-string (prin1-to-string r)))))
380 (let ((*print-radix* t))
381 (assert (= r (read-from-string
382 (princ-to-string r))))))))))))
384 ;;;; Bugs, found by PFD
385 ;;; NIL parameter for ~^ means `not supplied'
386 (with-test (:name (format :escape-upward-directive :v-directive-arg nil))
387 (loop for (format arg result) in
388 '(("~:{~D~v^~D~}" ((3 1 4) (1 0 2) (7 nil) (5 nil 6)) "341756")
389 ("~:{~1,2,v^~A~}" ((nil 0) (3 1) (0 2)) "02"))
390 do (assert (string= (funcall #'format nil format arg) result))
391 do (assert (string= (with-output-to-string (s)
392 (funcall (eval `(formatter ,format)) s arg))
393 result))))
395 ;;; NIL first parameter for ~R is equivalent to no parameter.
396 (with-test (:name (format :radix-directive nil :argument))
397 (assert (string= (format nil "~VR" nil 5) "five"))
398 (assert (string= (format nil (formatter "~VR") nil 6) "six")))
400 ;;; CSR inserted a bug into Burger & Dybvig's float printer. Caught
401 ;;; by Raymond Toy
402 (with-test (:name (format :exponential-floating-point-directive :smoke))
403 (assert (string= (format nil "~E" 1d23) "1.d+23")))
405 ;;; Fixed-format bugs from CLISP's test suite (reported by Bruno
406 ;;; Haible, bug 317)
407 (with-test (:name (format :fixed-format-floating-point-directive :bug-317))
408 (assert (string= (format nil "~1F" 10) "10."))
409 (assert (string= (format nil "~0F" 10) "10."))
410 (assert (string= (format nil "~2F" 1234567.1) "1234567.")))
412 ;;; here's one that seems to fail most places. I think this is right,
413 ;;; and most of the other answers I've seen are definitely wrong.
414 (with-test (:name (format :general-floating-point-directive :smoke))
415 (assert (string= (format nil "~G" 1d23) "100000000000000000000000. ")))
417 ;;; Adam Warner's test case
418 (with-test (:name (format :fixed-format-floating-point-directive :at-sign))
419 (assert (string= (format nil "~@F" 1.23) "+1.23")))
422 ;;; New (2005-11-08, also known as CSR House day) float format test
423 ;;; cases. Simon Alexander, Raymond Toy, and others
424 (with-test (:name (format :floating-point-directives :misc))
425 (assert (string= (format nil "~9,4,,-7E" pi) ".00000003d+8"))
426 (assert (string= (format nil "~9,4,,-5E" pi) ".000003d+6"))
427 (assert (string= (format nil "~5,4,,7E" pi) "3141600.d-6"))
428 (assert (string= (format nil "~11,4,,3E" pi) " 314.16d-2"))
429 (assert (string= (format nil "~11,4,,5E" pi) " 31416.d-4"))
430 (assert (string= (format nil "~11,4,,0E" pi) " 0.3142d+1"))
431 (assert (string= (format nil "~9,,,-1E" pi) ".03142d+2"))
432 (assert (string= (format nil "~,,,-2E" pi) "0.003141592653589793d+3"))
433 (assert (string= (format nil "~,,,2E" pi) "31.41592653589793d-1"))
434 (assert (string= (format nil "~E" pi) "3.141592653589793d+0"))
435 (assert (string= (format nil "~9,5,,-1E" pi) ".03142d+2"))
436 (assert (string= (format nil "~11,5,,-1E" pi) " 0.03142d+2"))
437 (assert (string= (format nil "~G" pi) "3.141592653589793 "))
438 (assert (string= (format nil "~9,5G" pi) "3.1416 "))
439 (assert (string= (format nil "|~13,6,2,7E|" pi) "| 3141593.d-06|"))
440 (assert (string= (format nil "~9,3,2,0,'%E" pi) "0.314d+01"))
441 (assert (string= (format nil "~9,0,6f" pi) " 3141593."))
442 (assert (string= (format nil "~6,2,1,'*F" pi) " 31.42"))
443 (assert (string= (format nil "~6,2,1,'*F" (* 100 pi)) "******"))
444 (assert (string= (format nil "~9,3,2,-2,'%@E" pi) "+.003d+03"))
445 (assert (string= (format nil "~10,3,2,-2,'%@E" pi) "+0.003d+03"))
446 (assert (string= (format nil "~15,3,2,-2,'%,'=@E" pi) "=====+0.003d+03"))
447 (assert (string= (format nil "~9,3,2,-2,'%E" pi) "0.003d+03"))
448 (assert (string= (format nil "~8,3,2,-2,'%@E" pi) "%%%%%%%%"))
450 (assert (string= (format nil "~g" 1e0) "1. "))
451 (assert (string= (format nil "~g" 1.2d40) "12000000000000000000000000000000000000000. "))
453 (assert (string= (format nil "~e" 0) "0.0e+0"))
454 (assert (string= (format nil "~e" 0d0) "0.0d+0"))
455 (assert (string= (format nil "~9,,4e" 0d0) "0.0d+0000")))
457 (with-test (:name (print hash-table print-not-readable))
458 (let ((table (make-hash-table)))
459 (setf (gethash 1 table) t)
460 (assert-error (with-standard-io-syntax
461 (let ((*read-eval* nil)
462 (*print-readably* t))
463 (with-output-to-string (*standard-output*)
464 (prin1 table))))
465 print-not-readable)))
467 ;; Test that we can print characters readably regardless of the external format
468 ;; of the stream.
470 (defun test-readable-character (character external-format)
471 (let ((file "print.impure.tmp"))
472 (unwind-protect
473 (progn
474 (with-open-file (stream file
475 :direction :output
476 :external-format external-format
477 :if-exists :supersede)
478 (write character :stream stream :readably t))
479 (with-open-file (stream file
480 :direction :input
481 :external-format external-format
482 :if-does-not-exist :error)
483 (assert (char= (read stream) character))))
484 (ignore-errors
485 (delete-file file)))))
487 (with-test (:name (:print-readable :character :utf-8) :skipped-on '(not :sb-unicode))
488 (test-readable-character (code-char #xfffe) :utf-8))
490 (with-test (:name (:print-readable :character :iso-8859-1) :skipped-on '(not :sb-unicode))
491 (test-readable-character (code-char #xfffe) :iso-8859-1))
493 (with-test (:name (format :character-directive :colon))
494 (assert (string= (eval '(format nil "~:C" #\a)) "a"))
495 (assert (string= (format nil (formatter "~:C") #\a) "a")))
497 ;;; This used to trigger an AVER instead.
498 (with-test (:name (format :end-of-justification-directive :mismatch))
499 (assert-error (eval '(formatter "~>")) sb-format:format-error)
500 (assert-error (eval '(format t "~>")) sb-format:format-error))
502 ;;; readably printing hash-tables, check for circularity
503 (with-test (:name (print read hash-table *print-circle*))
504 (let ((x (cons 1 2))
505 (h (make-hash-table))
506 (*print-readably* t)
507 (*print-circle* t)
508 (*read-eval* t))
509 (setf (gethash x h) h)
510 (destructuring-bind (x2 . h2) (read-from-string (write-to-string (cons x h)))
511 (assert (equal x x2))
512 (assert (eq h2 (gethash x2 h2))))))
514 ;;; an off-by-one error in the ~R format directive until 1.0.15.20
515 ;;; prevented printing cardinals and ordinals between (expt 10 63) and
516 ;;; (1- (expt 10 66))
517 (with-test (:name (format :radix-directive :large-values))
518 (assert (string= (format nil "~R" (expt 10 63)) "one vigintillion"))
519 (assert (string= (format nil "~:R" (expt 10 63)) "one vigintillionth")))
521 ;;; too-clever cacheing for PRINT-OBJECT resulted in a bogus method
522 ;;; for printing RESTART objects. Check also CONTROL-STACK-EXHAUSTED
523 ;;; and HEAP-EXHAUSTED-ERROR.
524 (with-test (:name (print-object restart condition))
525 (let ((result (with-output-to-string (*standard-output*)
526 (princ (find-restart 'abort)))))
527 (assert (string/= result "#<" :end1 2)))
528 (let ((result (with-output-to-string (*standard-output*)
529 (princ (make-condition 'sb-kernel::control-stack-exhausted)))))
530 (assert (string/= result "#<" :end1 2)))
531 (let ((result (with-output-to-string (*standard-output*)
532 (princ (make-condition 'sb-kernel::heap-exhausted-error)))))
533 (assert (string/= result "#<" :end1 2))))
535 (with-test (:name (:with-standard-io-syntax :bind-print-pprint-dispatch))
536 (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil)))
537 (set-pprint-dispatch 'symbol #'(lambda (stream obj)
538 (declare (ignore obj))
539 (write-string "FOO" stream)))
540 (with-standard-io-syntax
541 (let ((*print-pretty* t))
542 (assert (string= (princ-to-string 'bar) "BAR"))))))
544 ;;; lp#1398290 (which obsoletes lp#488979)
546 (defclass a-class-name () ())
548 (with-test (:name :print-unreadable-no-conditional-newline)
549 (assert (not (find #\Newline
550 (let ((*print-pretty* t)
551 (*print-right-margin* 10))
552 (format nil "~A" (make-instance 'a-class-name)))
553 :test #'char=)))
555 (assert (not (find #\Newline
556 (let ((*print-pretty* nil)
557 (*print-right-margin* 10))
558 (format nil "~A" (make-instance 'a-class-name)))
559 :test #'char=))))
561 ;;; The PRINT-OBJECT method for RANDOM-STATE used to have a bogus
562 ;;; dimension argument for MAKE-ARRAY.
563 (with-test (:name :print-random-state)
564 (assert (equalp *random-state*
565 (read-from-string
566 (write-to-string *random-state*)))))
568 (with-test (:name :write-return-value)
569 ;; COMPILE is called explicitly because there was a bug in the
570 ;; compiler-macro for WRITE, which isn't expanded by the evaluator.
571 (assert (= 123 (funcall (checked-compile '(lambda (s) (write 123 :stream s)))
572 (make-broadcast-stream)))))
574 (with-test (:name :write/write-to-string-compiler-macro-lp/598374+581564)
575 (let ((test (checked-compile
576 `(lambda (object &optional output-stream)
577 (write object :stream output-stream)))))
578 (assert (equal "(HELLO WORLD)"
579 (with-output-to-string (*standard-output*)
580 (let ((list '(hello world)))
581 (assert (eq list (funcall test list)))))))
582 (assert (equal "12"
583 (with-output-to-string (*standard-output*)
584 (assert (eql 12 (funcall test 12)))))))
585 (let ((test (checked-compile
586 `(lambda ()
587 (let ((*print-length* 42))
588 (write-to-string *print-length* :length nil))))))
589 (assert (equal "42" (funcall test)))))
591 (with-test (:name (format :compile-literal-dest-string))
592 (multiple-value-bind (fun failure-p warnings)
593 (checked-compile `(lambda (x) (format "~A" x))
594 :allow-warnings t)
595 (declare (ignore fun))
596 (assert failure-p)
597 (assert (= (length warnings) 1))))
599 (with-test (:name :bug-308961)
600 (assert (string= (format nil "~4,1F" 0.001) " 0.0"))
601 (assert (string= (format nil "~4,1@F" 0.001) "+0.0"))
602 (assert (string= (format nil "~E" 0.01) "1.e-2"))
603 (assert (string= (format nil "~G" 0.01) "1.00e-2")))
605 (with-test (:name (:fp-print-read-consistency single-float))
606 (let ((*random-state* (make-random-state t))
607 (oops))
608 (loop for f = most-positive-single-float then (/ f 2.0)
609 while (> f 0.0)
610 do (loop repeat 10
611 for fr = (random f)
612 do (unless (eql fr (read-from-string (prin1-to-string fr)))
613 (push fr oops)
614 (return))))
615 (loop for f = most-negative-single-float then (/ f 2.0)
616 while (< f -0.0)
617 do (loop repeat 10
618 for fr = (- (random (- f)))
619 do (unless (eql fr (read-from-string (prin1-to-string fr)))
620 (push fr oops)
621 (return))))
622 (when oops
623 (error "FP print-read inconsistencies:~%~:{ ~S => ~S~%~}"
624 (mapcar (lambda (f)
625 (list f (read-from-string (prin1-to-string f))))
626 oops)))))
628 (with-test (:name (:fp-print-read-consistency double-float))
629 (let ((*random-state* (make-random-state t))
630 (oops))
631 ;; FIXME skipping denormalized floats due to bug 793774.
632 (loop for f = most-positive-double-float then (/ f 2d0)
633 while (> f 0d0)
634 do (loop repeat 10
635 for fr = (random f)
636 do (unless (float-denormalized-p fr)
637 (unless (eql fr (read-from-string (prin1-to-string fr)))
638 (push fr oops)
639 (return)))))
640 (loop for f = most-negative-double-float then (/ f 2d0)
641 while (< f -0d0)
642 do (loop repeat 10
643 for fr = (- (random (- f)))
644 do (unless (float-denormalized-p fr)
645 (unless (eql fr (read-from-string (prin1-to-string fr)))
646 (push fr oops)
647 (return)))))
648 (when oops
649 (error "FP print-read inconsistencies:~%~:{ ~S => ~S~%~}"
650 (mapcar (lambda (f)
651 (list f (read-from-string (prin1-to-string f))))
652 oops)))))
654 (with-test (:name :bug-811386)
655 (assert (equal " 0.00" (format nil "~7,2,-2f" 0)))
656 (assert (equal " 0.00" (format nil "~7,2,2f" 0)))
657 (assert (equal " 0.01" (format nil "~7,2,-2f" 1)))
658 (assert (equal " 100.00" (format nil "~7,2,2f" 1)))
659 (assert (equal " 0.00" (format nil "~7,2,-2f" 0.1)))
660 (assert (equal " 10.00" (format nil "~7,2,2f" 0.1)))
661 (assert (equal " 0.01" (format nil "~7,2,-2f" 0.5))))
663 (with-test (:name :bug-867684)
664 (assert (equal "ab" (format nil "a~0&b"))))
666 (with-test (:name :print-unreadably-function)
667 (assert (equal "\"foo\""
668 (handler-bind ((print-not-readable #'sb-ext:print-unreadably))
669 (let ((*read-eval* nil))
670 (write-to-string (coerce "foo" 'base-string) :readably t))))))
672 (with-test (:name :printing-specialized-arrays-readably)
673 (let ((*read-eval* t)
674 (dimss (loop repeat 10
675 collect (loop repeat (1+ (random 3))
676 collect (1+ (random 10)))))
677 (props sb-vm::*specialized-array-element-type-properties*))
678 (labels ((random-elt (type)
679 (case type
680 (base-char
681 (code-char (random 128)))
682 (character
683 (code-char (random char-code-limit)))
684 (single-float
685 (+ least-positive-normalized-single-float
686 (random most-positive-single-float)))
687 (double-float
688 (+ least-positive-normalized-double-float
689 (random most-positive-double-float)))
690 (bit
691 (random 2))
692 (fixnum
693 (random most-positive-fixnum))
694 ((t)
696 (otherwise
697 (destructuring-bind (type x) type
698 (ecase type
699 (unsigned-byte
700 (random (1- (expt 2 x))))
701 (signed-byte
702 (- (random (expt 2 (1- x)))))
703 (complex
704 (complex (random-elt x) (random-elt x)))))))))
705 (dotimes (i (length props))
706 (let ((et (sb-vm::saetp-specifier (aref props i))))
707 (when et
708 (when (eq 'base-char et)
709 ;; base-strings not included in the #. printing.
710 (go :next))
711 (dolist (dims dimss)
712 (let ((a (make-array dims :element-type et)))
713 (assert (equal et (array-element-type a)))
714 (dotimes (i (array-total-size a))
715 (setf (row-major-aref a i) (random-elt et)))
716 (let ((copy (read-from-string (write-to-string a :readably t))))
717 (assert (equal dims (array-dimensions copy)))
718 (assert (equal et (array-element-type copy)))
719 (assert (equal (array-total-size a) (array-total-size copy)))
720 (dotimes (i (array-total-size a))
721 (assert (equal (row-major-aref a i) (row-major-aref copy i)))))))))
722 :next))))
724 (with-test (:name (format :negative-colinc-and-mincol))
725 (assert-error (format nil "~-2a" 1))
726 (assert-error (format nil "~,0a" 1)))
728 (with-test (:name (format :bug-905817))
729 ;; The bug manifests itself in an endless loop in FORMAT.
730 ;; Correct behaviour is to signal an error.
731 (handler-case
732 (with-timeout 5
733 (assert-error (format nil "e~8,0s" 12395)))
734 (timeout ()
735 (error "Endless loop in FORMAT"))))
737 (with-test (:name :format-type-check)
738 (assert (equal "1/10" (format nil "~2r" 1/2)))
739 (assert-error (format nil "~r" 1.32) sb-format:format-error)
740 (assert-error (format nil "~c" 1.32) sb-format:format-error)
741 (assert (equal "1/10" (eval '(format nil "~2r" 1/2))))
742 (assert-error (eval '(format nil "~r" 1.32)) sb-format:format-error)
743 (assert-error (eval '(format nil "~c" 1.32)) sb-format:format-error))
745 ;; Setup for test of print-object on a class with no proper name.
746 ;; There are various PRINT-OBJECT tests strewn throughout, all of which
747 ;; are not in the obvious place to me, except for perhaps 'clos.impure'
748 ;; which is also not the right place because that file concerns CLOS
749 ;; behavior in general, not to mention being far too noisy in its output.
750 (defclass fruit () (a))
751 (defvar *fruit1* (find-class 'fruit))
752 (setf (find-class 'fruit) nil)
753 (defclass fruit () (n o))
754 (defvar *fruit2* (find-class 'fruit))
755 (setf (find-class 'fruit) nil)
756 (defclass fruit () (x))
757 (with-test (:name (print-object :improper-class-name))
758 (assert (string/= (write-to-string *fruit1*) (write-to-string *fruit2*)))
759 (assert (string/= (write-to-string (find-class 'fruit))
760 (write-to-string *fruit1*))))
762 (defclass f-s-o-for-print-object (sb-mop:funcallable-standard-object)
764 (:metaclass sb-mop:funcallable-standard-class))
765 (with-test (:name (print-object sb-mop:funcallable-standard-object))
766 (let ((instance (make-instance 'f-s-o-for-print-object)))
767 (assert (eql 0 (search "#<F-S-O-FOR-PRINT-OBJECT"
768 (with-output-to-string (stream)
769 (print-object instance stream)))))))
771 (with-test (:name :format-readably)
772 (let ((*print-readably* t))
773 (assert (format nil "~$" #'format))
774 (assert (format nil "~d" #'format))
775 (assert (format nil "~x" #'format))
776 (assert (format nil "~b" #'format))
777 (assert (format nil "~3r" #'format))
778 (locally
779 (declare (notinline format))
780 (assert (format nil "~$" #'format))
781 (assert (format nil "~d" #'format))
782 (assert (format nil "~x" #'format))
783 (assert (format nil "~b" #'format))
784 (assert (format nil "~3r" #'format)))))
786 (with-test (:name (format *print-base*))
787 (let ((*print-base* 3))
788 (assert (equal (format nil "~g" '(123)) "(123)"))
789 (assert (equal (format nil "~f" '(123)) "(123)"))
790 (assert (equal (format nil "~e" '(123)) "(123)"))
791 (assert (equal (format nil "~$" '(123)) "(123)"))
792 (locally
793 (declare (notinline format))
794 (assert (equal (format nil "~g" '(123)) "(123)"))
795 (assert (equal (format nil "~f" '(123)) "(123)"))
796 (assert (equal (format nil "~e" '(123)) "(123)"))
797 (assert (equal (format nil "~$" '(123)) "(123)")))))
799 (with-test (:name (format :concatenate))
800 (assert (equal
801 (funcall (checked-compile `(lambda (x) (format nil "~s" (the string x))))
802 "\\")
803 (prin1-to-string "\\"))))
805 (with-test (:name (write :stream nil))
806 (assert
807 (equal
808 (with-output-to-string (*standard-output*)
809 (funcall (checked-compile `(lambda () (write "xx" :stream nil)))))
810 "\"xx\"")))
812 (define-condition foo () (a))
813 (defvar *ccc* (make-condition 'foo))
814 (handler-bind ((warning #'muffle-warning)) (define-condition foo (warning) (a)))
815 (with-test (:name :write-obsolete-condition)
816 (assert (search "UNPRINTABLE" (write-to-string *ccc*))))
818 (with-test (:name (format :no-overeager-compile-time-processing))
819 (checked-compile '(lambda (x) (format t "~/nopackage:nofun/" x))))
821 (with-test (:name :print-case-capitalize)
822 (assert (string= (write-to-string 'fluffy-bunny-count :case :capitalize)
823 "Fluffy-Bunny-Count")))
825 (defclass foo2 () ())
826 (with-test (:name :print-random-standard-object) ; lp# 1654550
827 (assert (search "#<FOO2 {" (write-to-string (make-instance 'foo2) :pretty nil)))
828 (assert (search "#<FOO2 {" (write-to-string (make-instance 'foo2) :pretty t))))