Fix printing of value-cells
[sbcl.git] / tests / print.impure.lisp
blob8477687b4b94781801ea82bdef0b749fb3c63b43
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)))))))))
383 (write-char #\.)
384 (finish-output))))
386 ;;;; Bugs, found by PFD
387 ;;; NIL parameter for ~^ means `not supplied'
388 (with-test (:name (format :escape-upward-directive :v-directive-arg nil))
389 (loop for (format arg result) in
390 '(("~:{~D~v^~D~}" ((3 1 4) (1 0 2) (7 nil) (5 nil 6)) "341756")
391 ("~:{~1,2,v^~A~}" ((nil 0) (3 1) (0 2)) "02"))
392 do (assert (string= (funcall #'format nil format arg) result))
393 do (assert (string= (with-output-to-string (s)
394 (funcall (eval `(formatter ,format)) s arg))
395 result))))
397 ;;; NIL first parameter for ~R is equivalent to no parameter.
398 (with-test (:name (format :radix-directive nil :argument))
399 (assert (string= (format nil "~VR" nil 5) "five"))
400 (assert (string= (format nil (formatter "~VR") nil 6) "six")))
402 ;;; CSR inserted a bug into Burger & Dybvig's float printer. Caught
403 ;;; by Raymond Toy
404 (with-test (:name (format :exponential-floating-point-directive :smoke))
405 (assert (string= (format nil "~E" 1d23) "1.d+23")))
407 ;;; Fixed-format bugs from CLISP's test suite (reported by Bruno
408 ;;; Haible, bug 317)
409 (with-test (:name (format :fixed-format-floating-point-directive :bug-317))
410 (assert (string= (format nil "~1F" 10) "10."))
411 (assert (string= (format nil "~0F" 10) "10."))
412 (assert (string= (format nil "~2F" 1234567.1) "1234567.")))
414 ;;; here's one that seems to fail most places. I think this is right,
415 ;;; and most of the other answers I've seen are definitely wrong.
416 (with-test (:name (format :general-floating-point-directive :smoke))
417 (assert (string= (format nil "~G" 1d23) "100000000000000000000000. ")))
419 ;;; Adam Warner's test case
420 (with-test (:name (format :fixed-format-floating-point-directive :at-sign))
421 (assert (string= (format nil "~@F" 1.23) "+1.23")))
424 ;;; New (2005-11-08, also known as CSR House day) float format test
425 ;;; cases. Simon Alexander, Raymond Toy, and others
426 (with-test (:name (format :floating-point-directives :misc))
427 (assert (string= (format nil "~9,4,,-7E" pi) ".00000003d+8"))
428 (assert (string= (format nil "~9,4,,-5E" pi) ".000003d+6"))
429 (assert (string= (format nil "~5,4,,7E" pi) "3141600.d-6"))
430 (assert (string= (format nil "~11,4,,3E" pi) " 314.16d-2"))
431 (assert (string= (format nil "~11,4,,5E" pi) " 31416.d-4"))
432 (assert (string= (format nil "~11,4,,0E" pi) " 0.3142d+1"))
433 (assert (string= (format nil "~9,,,-1E" pi) ".03142d+2"))
434 (assert (string= (format nil "~,,,-2E" pi) "0.003141592653589793d+3"))
435 (assert (string= (format nil "~,,,2E" pi) "31.41592653589793d-1"))
436 (assert (string= (format nil "~E" pi) "3.141592653589793d+0"))
437 (assert (string= (format nil "~9,5,,-1E" pi) ".03142d+2"))
438 (assert (string= (format nil "~11,5,,-1E" pi) " 0.03142d+2"))
439 (assert (string= (format nil "~G" pi) "3.141592653589793 "))
440 (assert (string= (format nil "~9,5G" pi) "3.1416 "))
441 (assert (string= (format nil "|~13,6,2,7E|" pi) "| 3141593.d-06|"))
442 (assert (string= (format nil "~9,3,2,0,'%E" pi) "0.314d+01"))
443 (assert (string= (format nil "~9,0,6f" pi) " 3141593."))
444 (assert (string= (format nil "~6,2,1,'*F" pi) " 31.42"))
445 (assert (string= (format nil "~6,2,1,'*F" (* 100 pi)) "******"))
446 (assert (string= (format nil "~9,3,2,-2,'%@E" pi) "+.003d+03"))
447 (assert (string= (format nil "~10,3,2,-2,'%@E" pi) "+0.003d+03"))
448 (assert (string= (format nil "~15,3,2,-2,'%,'=@E" pi) "=====+0.003d+03"))
449 (assert (string= (format nil "~9,3,2,-2,'%E" pi) "0.003d+03"))
450 (assert (string= (format nil "~8,3,2,-2,'%@E" pi) "%%%%%%%%"))
452 (assert (string= (format nil "~g" 1e0) "1. "))
453 (assert (string= (format nil "~g" 1.2d40) "12000000000000000000000000000000000000000. "))
455 (assert (string= (format nil "~e" 0) "0.0e+0"))
456 (assert (string= (format nil "~e" 0d0) "0.0d+0"))
457 (assert (string= (format nil "~9,,4e" 0d0) "0.0d+0000")))
459 (with-test (:name (print hash-table print-not-readable))
460 (let ((table (make-hash-table)))
461 (setf (gethash 1 table) t)
462 (assert-error (with-standard-io-syntax
463 (let ((*read-eval* nil)
464 (*print-readably* t))
465 (with-output-to-string (*standard-output*)
466 (prin1 table))))
467 print-not-readable)))
469 ;; Test that we can print characters readably regardless of the external format
470 ;; of the stream.
472 (defun test-readable-character (character external-format)
473 (let ((file "print.impure.tmp"))
474 (unwind-protect
475 (progn
476 (with-open-file (stream file
477 :direction :output
478 :external-format external-format
479 :if-exists :supersede)
480 (write character :stream stream :readably t))
481 (with-open-file (stream file
482 :direction :input
483 :external-format external-format
484 :if-does-not-exist :error)
485 (assert (char= (read stream) character))))
486 (ignore-errors
487 (delete-file file)))))
489 (with-test (:name (:print-readable :character :utf-8) :skipped-on '(not :sb-unicode))
490 (test-readable-character (code-char #xfffe) :utf-8))
492 (with-test (:name (:print-readable :character :iso-8859-1) :skipped-on '(not :sb-unicode))
493 (test-readable-character (code-char #xfffe) :iso-8859-1))
495 (with-test (:name (format :character-directive :colon))
496 (assert (string= (eval '(format nil "~:C" #\a)) "a"))
497 (assert (string= (format nil (formatter "~:C") #\a) "a")))
499 ;;; This used to trigger an AVER instead.
500 (with-test (:name (format :end-of-justification-directive :mismatch))
501 (assert-error (eval '(formatter "~>")) sb-format:format-error)
502 (assert-error (eval '(format t "~>")) sb-format:format-error))
504 ;;; readably printing hash-tables, check for circularity
505 (with-test (:name (print read hash-table *print-circle*))
506 (let ((x (cons 1 2))
507 (h (make-hash-table))
508 (*print-readably* t)
509 (*print-circle* t)
510 (*read-eval* t))
511 (setf (gethash x h) h)
512 (destructuring-bind (x2 . h2) (read-from-string (write-to-string (cons x h)))
513 (assert (equal x x2))
514 (assert (eq h2 (gethash x2 h2))))))
516 ;;; an off-by-one error in the ~R format directive until 1.0.15.20
517 ;;; prevented printing cardinals and ordinals between (expt 10 63) and
518 ;;; (1- (expt 10 66))
519 (with-test (:name (format :radix-directive :large-values))
520 (assert (string= (format nil "~R" (expt 10 63)) "one vigintillion"))
521 (assert (string= (format nil "~:R" (expt 10 63)) "one vigintillionth")))
523 ;;; too-clever cacheing for PRINT-OBJECT resulted in a bogus method
524 ;;; for printing RESTART objects. Check also CONTROL-STACK-EXHAUSTED
525 ;;; and HEAP-EXHAUSTED-ERROR.
526 (with-test (:name (print-object restart condition))
527 (let ((result (with-output-to-string (*standard-output*)
528 (princ (find-restart 'abort)))))
529 (assert (string/= result "#<" :end1 2)))
530 (let ((result (with-output-to-string (*standard-output*)
531 (princ (make-condition 'sb-kernel::control-stack-exhausted)))))
532 (assert (string/= result "#<" :end1 2)))
533 (let ((result (with-output-to-string (*standard-output*)
534 (princ (make-condition 'sb-kernel::heap-exhausted-error)))))
535 (assert (string/= result "#<" :end1 2))))
537 (with-test (:name (:with-standard-io-syntax :bind-print-pprint-dispatch))
538 (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil)))
539 (set-pprint-dispatch 'symbol #'(lambda (stream obj)
540 (declare (ignore obj))
541 (write-string "FOO" stream)))
542 (with-standard-io-syntax
543 (let ((*print-pretty* t))
544 (assert (string= (princ-to-string 'bar) "BAR"))))))
546 ;;; lp#1398290 (which obsoletes lp#488979)
548 (defclass a-class-name () ())
550 (with-test (:name :print-unreadable-no-conditional-newline)
551 (assert (not (find #\Newline
552 (let ((*print-pretty* t)
553 (*print-right-margin* 10))
554 (format nil "~A" (make-instance 'a-class-name)))
555 :test #'char=)))
557 (assert (not (find #\Newline
558 (let ((*print-pretty* nil)
559 (*print-right-margin* 10))
560 (format nil "~A" (make-instance 'a-class-name)))
561 :test #'char=))))
563 ;;; The PRINT-OBJECT method for RANDOM-STATE used to have a bogus
564 ;;; dimension argument for MAKE-ARRAY.
565 (with-test (:name :print-random-state)
566 (assert (equalp *random-state*
567 (read-from-string
568 (write-to-string *random-state*)))))
570 (with-test (:name :write-return-value)
571 ;; COMPILE is called explicitly because there was a bug in the
572 ;; compiler-macro for WRITE, which isn't expanded by the evaluator.
573 (assert (= 123 (funcall (checked-compile '(lambda (s) (write 123 :stream s)))
574 (make-broadcast-stream)))))
576 (with-test (:name :write/write-to-string-compiler-macro-lp/598374+581564)
577 (let ((test (checked-compile
578 `(lambda (object &optional output-stream)
579 (write object :stream output-stream)))))
580 (assert (equal "(HELLO WORLD)"
581 (with-output-to-string (*standard-output*)
582 (let ((list '(hello world)))
583 (assert (eq list (funcall test list)))))))
584 (assert (equal "12"
585 (with-output-to-string (*standard-output*)
586 (assert (eql 12 (funcall test 12)))))))
587 (let ((test (checked-compile
588 `(lambda ()
589 (let ((*print-length* 42))
590 (write-to-string *print-length* :length nil))))))
591 (assert (equal "42" (funcall test)))))
593 (with-test (:name (format :compile-literal-dest-string))
594 (multiple-value-bind (fun failure-p warnings)
595 (checked-compile `(lambda (x) (format "~A" x))
596 :allow-warnings t)
597 (declare (ignore fun))
598 (assert failure-p)
599 (assert (= (length warnings) 1))))
601 (with-test (:name :bug-308961)
602 (assert (string= (format nil "~4,1F" 0.001) " 0.0"))
603 (assert (string= (format nil "~4,1@F" 0.001) "+0.0"))
604 (assert (string= (format nil "~E" 0.01) "1.e-2"))
605 (assert (string= (format nil "~G" 0.01) "1.00e-2")))
607 (with-test (:name (:fp-print-read-consistency single-float))
608 (let ((*random-state* (make-random-state t))
609 (oops))
610 (loop for f = most-positive-single-float then (/ f 2.0)
611 while (> f 0.0)
612 do (loop repeat 10
613 for fr = (random f)
614 do (unless (eql fr (read-from-string (prin1-to-string fr)))
615 (push fr oops)
616 (return))))
617 (loop for f = most-negative-single-float then (/ f 2.0)
618 while (< f -0.0)
619 do (loop repeat 10
620 for fr = (- (random (- f)))
621 do (unless (eql fr (read-from-string (prin1-to-string fr)))
622 (push fr oops)
623 (return))))
624 (when oops
625 (error "FP print-read inconsistencies:~%~:{ ~S => ~S~%~}"
626 (mapcar (lambda (f)
627 (list f (read-from-string (prin1-to-string f))))
628 oops)))))
630 (with-test (:name (:fp-print-read-consistency double-float))
631 (let ((*random-state* (make-random-state t))
632 (oops))
633 ;; FIXME skipping denormalized floats due to bug 793774.
634 (loop for f = most-positive-double-float then (/ f 2d0)
635 while (> f 0d0)
636 do (loop repeat 10
637 for fr = (random f)
638 do (unless (float-denormalized-p fr)
639 (unless (eql fr (read-from-string (prin1-to-string fr)))
640 (push fr oops)
641 (return)))))
642 (loop for f = most-negative-double-float then (/ f 2d0)
643 while (< f -0d0)
644 do (loop repeat 10
645 for fr = (- (random (- f)))
646 do (unless (float-denormalized-p fr)
647 (unless (eql fr (read-from-string (prin1-to-string fr)))
648 (push fr oops)
649 (return)))))
650 (when oops
651 (error "FP print-read inconsistencies:~%~:{ ~S => ~S~%~}"
652 (mapcar (lambda (f)
653 (list f (read-from-string (prin1-to-string f))))
654 oops)))))
656 (with-test (:name :bug-811386)
657 (assert (equal " 0.00" (format nil "~7,2,-2f" 0)))
658 (assert (equal " 0.00" (format nil "~7,2,2f" 0)))
659 (assert (equal " 0.01" (format nil "~7,2,-2f" 1)))
660 (assert (equal " 100.00" (format nil "~7,2,2f" 1)))
661 (assert (equal " 0.00" (format nil "~7,2,-2f" 0.1)))
662 (assert (equal " 10.00" (format nil "~7,2,2f" 0.1)))
663 (assert (equal " 0.01" (format nil "~7,2,-2f" 0.5))))
665 (with-test (:name :bug-867684)
666 (assert (equal "ab" (format nil "a~0&b"))))
668 (with-test (:name :print-unreadably-function)
669 (assert (equal "\"foo\""
670 (handler-bind ((print-not-readable #'sb-ext:print-unreadably))
671 (let ((*read-eval* nil))
672 (write-to-string (coerce "foo" 'base-string) :readably t))))))
674 (with-test (:name :printing-specialized-arrays-readably)
675 (let ((*read-eval* t)
676 (dimss (loop repeat 10
677 collect (loop repeat (1+ (random 3))
678 collect (1+ (random 10)))))
679 (props sb-vm::*specialized-array-element-type-properties*))
680 (labels ((random-elt (type)
681 (case type
682 (base-char
683 (code-char (random 128)))
684 (character
685 (code-char (random char-code-limit)))
686 (single-float
687 (+ least-positive-normalized-single-float
688 (random most-positive-single-float)))
689 (double-float
690 (+ least-positive-normalized-double-float
691 (random most-positive-double-float)))
692 (bit
693 (random 2))
694 (fixnum
695 (random most-positive-fixnum))
696 ((t)
698 (otherwise
699 (destructuring-bind (type x) type
700 (ecase type
701 (unsigned-byte
702 (random (1- (expt 2 x))))
703 (signed-byte
704 (- (random (expt 2 (1- x)))))
705 (complex
706 (complex (random-elt x) (random-elt x)))))))))
707 (dotimes (i (length props))
708 (let ((et (sb-vm::saetp-specifier (aref props i))))
709 (when et
710 (when (eq 'base-char et)
711 ;; base-strings not included in the #. printing.
712 (go :next))
713 (dolist (dims dimss)
714 (let ((a (make-array dims :element-type et)))
715 (assert (equal et (array-element-type a)))
716 (dotimes (i (array-total-size a))
717 (setf (row-major-aref a i) (random-elt et)))
718 (let ((copy (read-from-string (write-to-string a :readably t))))
719 (assert (equal dims (array-dimensions copy)))
720 (assert (equal et (array-element-type copy)))
721 (assert (equal (array-total-size a) (array-total-size copy)))
722 (dotimes (i (array-total-size a))
723 (assert (equal (row-major-aref a i) (row-major-aref copy i)))))))))
724 :next))))
726 (with-test (:name (format :negative-colinc-and-mincol))
727 (assert-error (format nil "~-2a" 1))
728 (assert-error (format nil "~,0a" 1)))
730 (with-test (:name (format :bug-905817))
731 ;; The bug manifests itself in an endless loop in FORMAT.
732 ;; Correct behaviour is to signal an error.
733 (handler-case
734 (with-timeout 5
735 (assert-error (format nil "e~8,0s" 12395)))
736 (timeout ()
737 (error "Endless loop in FORMAT"))))
739 (with-test (:name :format-type-check)
740 (assert (equal "1/10" (format nil "~2r" 1/2)))
741 (assert-error (format nil "~r" 1.32) sb-format:format-error)
742 (assert-error (format nil "~c" 1.32) sb-format:format-error)
743 (assert (equal "1/10" (eval '(format nil "~2r" 1/2))))
744 (assert-error (eval '(format nil "~r" 1.32)) sb-format:format-error)
745 (assert-error (eval '(format nil "~c" 1.32)) sb-format:format-error))
747 ;; Setup for test of print-object on a class with no proper name.
748 ;; There are various PRINT-OBJECT tests strewn throughout, all of which
749 ;; are not in the obvious place to me, except for perhaps 'clos.impure'
750 ;; which is also not the right place because that file concerns CLOS
751 ;; behavior in general, not to mention being far too noisy in its output.
752 (defclass fruit () (a))
753 (defvar *fruit1* (find-class 'fruit))
754 (setf (find-class 'fruit) nil)
755 (defclass fruit () (n o))
756 (defvar *fruit2* (find-class 'fruit))
757 (setf (find-class 'fruit) nil)
758 (defclass fruit () (x))
759 (with-test (:name (print-object :improper-class-name))
760 (assert (string/= (write-to-string *fruit1*) (write-to-string *fruit2*)))
761 (assert (string/= (write-to-string (find-class 'fruit))
762 (write-to-string *fruit1*))))
764 (with-test (:name :format-readably)
765 (let ((*print-readably* t))
766 (assert (format nil "~$" #'format))
767 (assert (format nil "~d" #'format))
768 (assert (format nil "~x" #'format))
769 (assert (format nil "~b" #'format))
770 (assert (format nil "~3r" #'format))
771 (locally
772 (declare (notinline format))
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)))))
779 (with-test (:name (format *print-base*))
780 (let ((*print-base* 3))
781 (assert (equal (format nil "~g" '(123)) "(123)"))
782 (assert (equal (format nil "~f" '(123)) "(123)"))
783 (assert (equal (format nil "~e" '(123)) "(123)"))
784 (assert (equal (format nil "~$" '(123)) "(123)"))
785 (locally
786 (declare (notinline format))
787 (assert (equal (format nil "~g" '(123)) "(123)"))
788 (assert (equal (format nil "~f" '(123)) "(123)"))
789 (assert (equal (format nil "~e" '(123)) "(123)"))
790 (assert (equal (format nil "~$" '(123)) "(123)")))))
792 (with-test (:name (format :concatenate))
793 (assert (equal
794 (funcall (checked-compile `(lambda (x) (format nil "~s" (the string x))))
795 "\\")
796 (prin1-to-string "\\"))))
798 (with-test (:name (write :stream nil))
799 (assert
800 (equal
801 (with-output-to-string (*standard-output*)
802 (funcall (checked-compile `(lambda () (write "xx" :stream nil)))))
803 "\"xx\"")))
805 (define-condition foo () (a))
806 (defvar *ccc* (make-condition 'foo))
807 (define-condition foo (warning) (a))
808 (with-test (:name :write-obsolete-condition)
809 (assert (search "UNPRINTABLE" (write-to-string *ccc*))))
811 (with-test (:name (format :no-overeager-compile-time-processing))
812 (checked-compile '(lambda (x) (format t "~/nopackage:nofun/" x))))
814 (with-test (:name :print-case-capitalize)
815 (assert (string= (write-to-string 'fluffy-bunny-count :case :capitalize)
816 "Fluffy-Bunny-Count")))