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