emit compiler notes of NLX value-cells when (> SPEED SAFETY)
[sbcl.git] / tests / print.impure.lisp
blob5ab865c41499989ff6c05b4a00626f137f4f4592
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 ;;; Nathan Froyd reported that sbcl-0.6.11.34 screwed up output of
36 ;;; floating point infinities.
37 (dolist (x (list short-float-positive-infinity short-float-negative-infinity
38 single-float-positive-infinity single-float-negative-infinity
39 double-float-positive-infinity double-float-negative-infinity
40 long-float-positive-infinity long-float-negative-infinity))
41 (assert-output x))
43 ;;; Eric Marsden reported that this would blow up in CMU CL (even
44 ;;; though ANSI says that the mismatch between ~F expected type and
45 ;;; provided string type is supposed to be handled without signalling
46 ;;; an error) and provided a fix which was ported to sbcl-0.6.12.35.
47 (assert (null (format t "~F" "foo")))
49 ;;; This was a bug in SBCL until 0.6.12.40 (originally reported as a
50 ;;; CMU CL bug by Erik Naggum on comp.lang.lisp).
51 (loop for base from 2 to 36
52 with *print-radix* = t
53 do (let ((*print-base* base))
54 (assert (string= "#*101" (format nil "~S" #*101)))))
56 ;;; bug in sbcl-0.7.1.25, reported by DB sbcl-devel 2002-02-25
57 (assert (string= "0.5" (format nil "~2D" 0.5)))
59 ;;; we want malformed format strings to cause errors rather than have
60 ;;; some DWIM "functionality".
61 (assert (raises-error? (format nil "~:2T")))
63 ;;; bug reported, with fix, by Robert Strandh, sbcl-devel 2002-03-09,
64 ;;; fixed in sbcl-0.7.1.36:
65 (assert (string= (format nil "~2,3,8,'0$" 1234567.3d0) "1234567.30"))
67 ;;; checks that other FORMAT-DOLLAR output remains sane after the
68 ;;; 0.7.1.36 change
69 (assert (string= (format nil "~$" 0) "0.00"))
70 (assert (string= (format nil "~$" 4) "4.00"))
71 (assert (string= (format nil "~$" -4.0) "-4.00"))
72 (assert (string= (format nil "~2,7,11$" -4.0) "-0000004.00"))
73 (assert (string= (format nil "~2,7,11,' $" 1.1) " 0000001.10"))
74 (assert (string= (format nil "~1,7,11,' $" 1.1) " 0000001.1"))
75 (assert (string= (format nil "~1,3,8,' $" 7.3) " 007.3"))
76 (assert (string= (format nil "~2,3,8,'0$" 7.3) "00007.30"))
78 ;;; Check for symbol lookup in ~/ / directive -- double-colon was
79 ;;; broken in 0.7.1.36 and earlier
80 (defun print-foo (stream arg colonp atsignp &rest params)
81 (declare (ignore colonp atsignp params))
82 (format stream "~d" arg))
84 (assert (string= (format nil "~/print-foo/" 2) "2"))
85 (assert (string= (format nil "~/cl-user:print-foo/" 2) "2"))
86 (assert (string= (format nil "~/cl-user::print-foo/" 2) "2"))
87 (assert (raises-error? (format nil "~/cl-user:::print-foo/" 2)))
88 (assert (raises-error? (format nil "~/cl-user:a:print-foo/" 2)))
89 (assert (raises-error? (format nil "~/a:cl-user:print-foo/" 2)))
90 (assert (raises-error? (format nil "~/cl-user:print-foo:print-foo/" 2)))
92 ;;; better make sure that we get this one right, too
93 (defun print-foo\:print-foo (stream arg colonp atsignp &rest params)
94 (declare (ignore colonp atsignp params))
95 (format stream "~d" arg))
97 (assert (string= (format nil "~/cl-user:print-foo:print-foo/" 2) "2"))
98 (assert (string= (format nil "~/cl-user::print-foo:print-foo/" 2) "2"))
100 ;;; Check for error detection of illegal directives in a~<..~> justify
101 ;;; block (see ANSI section 22.3.5.2)
102 (assert (raises-error? (format nil "~<~W~>" 'foo)))
103 (assert (raises-error? (format nil "~<~<~A~:>~>" '(foo))))
104 (assert (string= (format nil "~<~<~A~>~>" 'foo) "FOO"))
106 ;;; Check that arrays that we print while *PRINT-READABLY* is true are
107 ;;; in fact generating similar objects.
108 (assert (equal (array-dimensions
109 (read-from-string
110 (with-output-to-string (s)
111 (let ((*print-readably* t))
112 (print (make-array '(1 2 0)) s)))))
113 '(1 2 0)))
115 (dolist (array (list (make-array '(1 0 1))
116 (make-array 0 :element-type nil)
117 (make-array 1 :element-type 'base-char)
118 (make-array 1 :element-type 'character)))
119 (assert (multiple-value-bind (result error)
120 (ignore-errors (read-from-string
121 (with-output-to-string (s)
122 (let ((*print-readably* t))
123 (print array s)))))
124 ;; it might not be readably-printable
125 (or (typep error 'print-not-readable)
126 (and
127 ;; or else it had better have the same dimensions
128 (equal (array-dimensions result) (array-dimensions array))
129 ;; and the same element-type
130 (equal (array-element-type result) (array-element-type array)))))))
132 ;;; before 0.8.0.66 it signalled UNBOUND-VARIABLE
133 (write #(1 2 3) :pretty nil :readably t)
135 ;;; another UNBOUND-VARIABLE, this time due to a bug in FORMATTER
136 ;;; expanders.
137 (funcall (formatter "~@<~A~:*~A~:>") nil 3)
139 ;;; the PPC floating point backend was at one point sufficiently
140 ;;; broken that this looped infinitely or caused segmentation
141 ;;; violations through stack corruption.
142 (print 0.0001)
144 ;;; In sbcl-0.8.7, the ~W format directive interpreter implemented the
145 ;;; sense of the colon and at-sign modifiers exactly backwards.
147 ;;; (Yes, the test for this *is* substantially hairier than the fix;
148 ;;; wanna make something of it?)
149 (cl:in-package :cl-user)
150 (defstruct wexerciser-0-8-7)
151 (defun wexercise-0-8-7-interpreted (wformat)
152 (format t wformat (make-wexerciser-0-8-7)))
153 (defmacro define-compiled-wexercise-0-8-7 (wexercise wformat)
154 `(defun ,wexercise ()
155 (declare (optimize (speed 3) (space 1)))
156 (format t ,wformat (make-wexerciser-0-8-7))
157 (values)))
158 (define-compiled-wexercise-0-8-7 wexercise-0-8-7-compiled-without-atsign "~W")
159 (define-compiled-wexercise-0-8-7 wexercise-0-8-7-compiled-with-atsign "~@W")
160 (defmethod print-object :before ((wexerciser-0-8-7 wexerciser-0-8-7) stream)
161 (unless (and *print-level* *print-length*)
162 (error "gotcha coming")))
163 (let ((*print-level* 11)
164 (*print-length* 12))
165 (wexercise-0-8-7-interpreted "~W")
166 (wexercise-0-8-7-compiled-without-atsign))
167 (remove-method #'print-object
168 (find-method #'print-object
169 '(:before)
170 (mapcar #'find-class '(wexerciser-0-8-7 t))))
171 (defmethod print-object :before ((wexerciser-0-8-7 wexerciser-0-8-7) stream)
172 (when (or *print-level* *print-length*)
173 (error "gotcha going")))
174 (let ((*print-level* 11)
175 (*print-length* 12))
176 (wexercise-0-8-7-interpreted "~@W")
177 (wexercise-0-8-7-compiled-with-atsign))
179 ;;; WRITE-TO-STRING was erroneously DEFKNOWNed as FOLDABLE
181 ;;; This bug from PFD
182 (defpackage "SCRATCH-WRITE-TO-STRING" (:use))
183 (with-standard-io-syntax
184 (let* ((*package* (find-package "SCRATCH-WRITE-TO-STRING"))
185 (answer (write-to-string 'scratch-write-to-string::x :readably nil)))
186 (assert (string= answer "X"))))
187 ;;; and a couple from Bruno Haible
188 (defun my-pprint-reverse (out list)
189 (write-char #\( out)
190 (when (setq list (reverse list))
191 (loop
192 (write (pop list) :stream out)
193 (when (endp list) (return))
194 (write-char #\Space out)))
195 (write-char #\) out))
196 (with-standard-io-syntax
197 (let ((*print-pprint-dispatch* (copy-pprint-dispatch)))
198 (set-pprint-dispatch '(cons (member foo)) 'my-pprint-reverse 0)
199 (let ((answer (write-to-string '(foo bar :boo 1) :pretty t :escape t)))
200 (assert (string= answer "(1 :BOO BAR FOO)")))))
201 (defun my-pprint-logical (out list)
202 (pprint-logical-block (out list :prefix "(" :suffix ")")
203 (when list
204 (loop
205 (write-char #\? out)
206 (write (pprint-pop) :stream out)
207 (write-char #\? out)
208 (pprint-exit-if-list-exhausted)
209 (write-char #\Space out)))))
210 (with-standard-io-syntax
211 (let ((*print-pprint-dispatch* (copy-pprint-dispatch)))
212 (set-pprint-dispatch '(cons (member bar)) 'my-pprint-logical 0)
213 (let ((answer (write-to-string '(bar foo :boo 1) :pretty t :escape t)))
214 (assert (string= answer "(?BAR? ?FOO? ?:BOO? ?1?)")))))
216 ;;; FORMAT string compile-time checker failure, reported by Thomas
217 ;;; F. Burdick
218 (multiple-value-bind (f w-p f-p)
219 (compile nil '(lambda () (format nil "~{")))
220 (assert (and w-p f-p))
221 (assert (nth-value 1 (ignore-errors (funcall f)))))
223 ;;; floating point print/read consistency
224 (let ((x (/ -9.349640046247849d-21 -9.381494249123696d-11)))
225 (let ((y (read-from-string (write-to-string x :readably t))))
226 (assert (eql x y))))
228 (let ((x1 (float -5496527/100000000000000000))
229 (x2 (float -54965272/1000000000000000000)))
230 (assert (or (equal (multiple-value-list (integer-decode-float x1))
231 (multiple-value-list (integer-decode-float x2)))
232 (string/= (prin1-to-string x1) (prin1-to-string x2)))))
234 ;;; readable printing of arrays with *print-radix* t
235 (let ((*print-radix* t)
236 (*print-readably* t)
237 (*print-pretty* nil))
238 (let ((output (with-output-to-string (s)
239 (write #2a((t t) (nil nil)) :stream s))))
240 (assert (equalp (read-from-string output) #2a((t t) (nil nil))))))
242 ;;; NIL parameters to "interpreted" FORMAT directives
243 (assert (string= (format nil "~v%" nil) (string #\Newline)))
245 ;;; PRINC-TO-STRING should bind print-readably
246 (let ((*print-readably* t))
247 (assert (string= (princ-to-string #\7)
248 (write-to-string #\7 :escape nil :readably nil))))
250 ;;; in FORMAT, ~^ inside ~:{ should go to the next case, not break
251 ;;; iteration, even if one argument is just a one-element list.
252 (assert (string= (format nil "~:{~A~^~}" '((A) (C D))) "AC"))
254 ;;; errors should be raised if pprint and justification are mixed
255 ;;; injudiciously...
256 (dolist (x (list "~<~:;~>~_" "~<~:;~>~I" "~<~:;~>~W"
257 "~<~:;~>~:T" "~<~:;~>~<~:>" "~_~<~:;~>"
258 "~I~<~:;~>" "~W~<~:;~>" "~:T~<~:;~>" "~<~:>~<~:;~>"))
259 (assert (raises-error? (format nil x nil)))
260 (assert (raises-error? (format nil (eval `(formatter ,x)) nil))))
261 ;;; ...but not in judicious cases.
262 (dolist (x (list "~<~;~>~_" "~<~;~>~I" "~<~;~>~W"
263 "~<~;~>~:T" "~<~;~>~<~>" "~_~<~;~>"
264 "~I~<~;~>" "~W~<~;~>" "~:T~<~;~>" "~<~>~<~;~>"
265 "~<~:;~>~T" "~T~<~:;~>"))
266 (assert (format nil x nil))
267 (assert (format nil (eval `(formatter ,x)) nil)))
269 ;;; bug 350: bignum printing so memory-hungry that heap runs out
270 ;;; -- just don't stall here forever on a slow box
271 (handler-case
272 (with-timeout 10
273 (print (ash 1 1000000)))
274 (timeout ()
275 (print 'timeout!)))
277 ;;; bug 371: bignum print/read inconsistency
278 (defvar *bug-371* -7043009959286724629649270926654940933664689003233793014518979272497911394287216967075767325693021717277238746020477538876750544587281879084559996466844417586093291189295867052594478662802691926547232838591510540917276694295393715934079679531035912244103731582711556740654671309980075069010778644542022/670550434139267031632063192770201289106737062379324644110801846820471752716238484923370056920388400273070254958650831435834503195629325418985020030706879602898158806736813101434594805676212779217311897830937606064579213895527844045511878668289820732425014254579493444623868748969110751636786165152601)
279 (let ((*print-base* 5)
280 (*read-base* 5)
281 (*print-radix* nil))
282 (assert (= *bug-371* (read-from-string (prin1-to-string *bug-371*)))))
284 ;;; a spot of random-testing for rational printing
285 (defvar *seed-state* (make-random-state))
286 (print *seed-state*) ; so that we can reproduce errors
287 (let ((seed (make-random-state *seed-state*)))
288 (loop repeat 42
289 do (let ((n (random (ash 1 1000) seed))
290 (d (random (ash 1 1000) seed)))
291 (when (zerop (random 2 seed))
292 (setf n (- n)))
293 (let ((r (/ n d)))
294 (loop for base from 2 to 36
295 do (let ((*print-base* base)
296 (*read-base* base)
297 (*print-radix* nil))
298 (assert (= r (read-from-string (prin1-to-string r))))
299 (if (= 36 base)
300 (decf *read-base*)
301 (incf *read-base*))
302 (assert (not (eql r (read-from-string (prin1-to-string r)))))
303 (let ((*print-radix* t))
304 (assert (= r (read-from-string
305 (princ-to-string r)))))))))
306 (write-char #\.)
307 (finish-output)))
309 ;;;; Bugs, found by PFD
310 ;;; NIL parameter for ~^ means `not supplied'
311 (loop for (format arg result) in
312 '(("~:{~D~v^~D~}" ((3 1 4) (1 0 2) (7 nil) (5 nil 6)) "341756")
313 ("~:{~1,2,v^~A~}" ((nil 0) (3 1) (0 2)) "02"))
314 do (assert (string= (funcall #'format nil format arg) result))
315 do (assert (string= (with-output-to-string (s)
316 (funcall (eval `(formatter ,format)) s arg))
317 result)))
319 ;;; NIL first parameter for ~R is equivalent to no parameter.
320 (assert (string= (format nil "~VR" nil 5) "five"))
321 (assert (string= (format nil (formatter "~VR") nil 6) "six"))
323 ;;; CSR inserted a bug into Burger & Dybvig's float printer. Caught
324 ;;; by Raymond Toy
325 (assert (string= (format nil "~E" 1d23) "1.d+23"))
327 ;;; Fixed-format bugs from CLISP's test suite (reported by Bruno
328 ;;; Haible, bug 317)
329 (assert (string= (format nil "~1F" 10) "10."))
330 (assert (string= (format nil "~0F" 10) "10."))
331 (assert (string= (format nil "~2F" 1234567.1) "1234567."))
333 ;;; here's one that seems to fail most places. I think this is right,
334 ;;; and most of the other answers I've seen are definitely wrong.
335 (assert (string= (format nil "~G" 1d23) "100000000000000000000000. "))
337 ;;; Adam Warner's test case
338 (assert (string= (format nil "~@F" 1.23) "+1.23"))
341 ;;; New (2005-11-08, also known as CSR House day) float format test
342 ;;; cases. Simon Alexander, Raymond Toy, and others
343 (assert (string= (format nil "~9,4,,-7E" pi) ".00000003d+8"))
344 (assert (string= (format nil "~9,4,,-5E" pi) ".000003d+6"))
345 (assert (string= (format nil "~5,4,,7E" pi) "3141600.d-6"))
346 (assert (string= (format nil "~11,4,,3E" pi) " 314.16d-2"))
347 (assert (string= (format nil "~11,4,,5E" pi) " 31416.d-4"))
348 (assert (string= (format nil "~11,4,,0E" pi) " 0.3142d+1"))
349 (assert (string= (format nil "~9,,,-1E" pi) ".03142d+2"))
350 (assert (string= (format nil "~,,,-2E" pi) "0.003141592653589793d+3"))
351 (assert (string= (format nil "~,,,2E" pi) "31.41592653589793d-1"))
352 (assert (string= (format nil "~E" pi) "3.141592653589793d+0"))
353 (assert (string= (format nil "~9,5,,-1E" pi) ".03142d+2"))
354 (assert (string= (format nil "~11,5,,-1E" pi) " 0.03142d+2"))
355 (assert (string= (format nil "~G" pi) "3.141592653589793 "))
356 (assert (string= (format nil "~9,5G" pi) "3.1416 "))
357 (assert (string= (format nil "|~13,6,2,7E|" pi) "| 3141593.d-06|"))
358 (assert (string= (format nil "~9,3,2,0,'%E" pi) "0.314d+01"))
359 (assert (string= (format nil "~9,0,6f" pi) " 3141593."))
360 (assert (string= (format nil "~6,2,1,'*F" pi) " 31.42"))
361 (assert (string= (format nil "~6,2,1,'*F" (* 100 pi)) "******"))
362 (assert (string= (format nil "~9,3,2,-2,'%@E" pi) "+.003d+03"))
363 (assert (string= (format nil "~10,3,2,-2,'%@E" pi) "+0.003d+03"))
364 (assert (string= (format nil "~15,3,2,-2,'%,'=@E" pi) "=====+0.003d+03"))
365 (assert (string= (format nil "~9,3,2,-2,'%E" pi) "0.003d+03"))
366 (assert (string= (format nil "~8,3,2,-2,'%@E" pi) "%%%%%%%%"))
368 (assert (string= (format nil "~g" 1e0) "1. "))
369 (assert (string= (format nil "~g" 1.2d40) "12000000000000000000000000000000000000000. "))
371 (assert (string= (format nil "~e" 0) "0.0e+0"))
372 (assert (string= (format nil "~e" 0d0) "0.0d+0"))
373 (assert (string= (format nil "~9,,4e" 0d0) "0.0d+0000"))
375 (let ((table (make-hash-table)))
376 (setf (gethash 1 table) t)
377 (assert
378 (raises-error? (with-standard-io-syntax
379 (let ((*read-eval* nil)
380 (*print-readably* t))
381 (with-output-to-string (*standard-output*)
382 (prin1 table))))
383 print-not-readable)))
385 ;; Test that we can print characters readably regardless of the external format
386 ;; of the stream.
388 (defun test-readable-character (character external-format)
389 (let ((file "print.impure.tmp"))
390 (unwind-protect
391 (progn
392 (with-open-file (stream file
393 :direction :output
394 :external-format external-format
395 :if-exists :supersede)
396 (write character :stream stream :readably t))
397 (with-open-file (stream file
398 :direction :input
399 :external-format external-format
400 :if-does-not-exist :error)
401 (assert (char= (read stream) character))))
402 (ignore-errors
403 (delete-file file)))))
405 (with-test (:name (:print-readable :character :utf-8) :skipped-on '(not :sb-unicode))
406 (test-readable-character (code-char #xfffe) :utf-8))
408 (with-test (:name (:print-readable :character :iso-8859-1) :skipped-on '(not :sb-unicode))
409 (test-readable-character (code-char #xfffe) :iso-8859-1))
411 (assert (string= (eval '(format nil "~:C" #\a)) "a"))
412 (assert (string= (format nil (formatter "~:C") #\a) "a"))
414 ;;; This used to trigger an AVER instead.
415 (assert (raises-error? (eval '(formatter "~>")) sb-format:format-error))
416 (assert (raises-error? (eval '(format t "~>")) sb-format:format-error))
418 ;;; readably printing hash-tables, check for circularity
419 (let ((x (cons 1 2))
420 (h (make-hash-table))
421 (*print-readably* t)
422 (*print-circle* t)
423 (*read-eval* t))
424 (setf (gethash x h) h)
425 (destructuring-bind (x2 . h2) (read-from-string (write-to-string (cons x h)))
426 (assert (equal x x2))
427 (assert (eq h2 (gethash x2 h2)))))
429 ;;; an off-by-one error in the ~R format directive until 1.0.15.20
430 ;;; prevented printing cardinals and ordinals between (expt 10 63) and
431 ;;; (1- (expt 10 66))
432 (assert (string= (format nil "~R" (expt 10 63)) "one vigintillion"))
433 (assert (string= (format nil "~:R" (expt 10 63)) "one vigintillionth"))
435 ;;; too-clever cacheing for PRINT-OBJECT resulted in a bogus method
436 ;;; for printing RESTART objects. Check also CONTROL-STACK-EXHAUSTED
437 ;;; and HEAP-EXHAUSTED-ERROR.
438 (let ((result (with-output-to-string (*standard-output*)
439 (princ (find-restart 'abort)))))
440 (assert (string/= result "#<" :end1 2)))
441 (let ((result (with-output-to-string (*standard-output*)
442 (princ (make-condition 'sb-kernel::control-stack-exhausted)))))
443 (assert (string/= result "#<" :end1 2)))
444 (let ((result (with-output-to-string (*standard-output*)
445 (princ (make-condition 'sb-kernel::heap-exhausted-error)))))
446 (assert (string/= result "#<" :end1 2)))
448 (with-test (:name (:with-standard-io-syntax :bind-print-pprint-dispatch))
449 (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil)))
450 (set-pprint-dispatch 'symbol #'(lambda (stream obj)
451 (declare (ignore obj))
452 (write-string "FOO" stream)))
453 (with-standard-io-syntax
454 (let ((*print-pretty* t))
455 (assert (string= (princ-to-string 'bar) "BAR"))))))
457 ;;; bug-lp#488979
459 (defclass a-class-name () ())
461 (assert (find #\Newline
462 (let ((*print-pretty* t)
463 (*print-right-margin* 10))
464 (format nil "~A" (make-instance 'a-class-name)))
465 :test #'char=))
467 (assert (not (find #\Newline
468 (let ((*print-pretty* nil)
469 (*print-right-margin* 10))
470 (format nil "~A" (make-instance 'a-class-name)))
471 :test #'char=)))
473 ;;; The PRINT-OBJECT method for RANDOM-STATE used to have a bogus
474 ;;; dimension argument for MAKE-ARRAY.
475 (with-test (:name :print-random-state)
476 (assert (equalp *random-state*
477 (read-from-string
478 (write-to-string *random-state*)))))
480 (with-test (:name :write-return-value)
481 (assert (= 123 (funcall (compile nil (lambda ()
482 (write 123)))))))
484 (with-test (:name :write/write-to-string-compiler-macro-lp/598374+581564)
485 (let ((test (compile nil
486 `(lambda (object &optional output-stream)
487 (write object
488 :stream output-stream)))))
489 (assert (equal "(HELLO WORLD)"
490 (with-output-to-string (*standard-output*)
491 (let ((list '(hello world)))
492 (assert (eq list (funcall test list)))))))
493 (assert (equal "12"
494 (with-output-to-string (*standard-output*)
495 (assert (eql 12 (funcall test 12)))))))
496 (let ((test (compile nil
497 `(lambda ()
498 (let ((*print-length* 42))
499 (write-to-string *print-length* :length nil))))))
500 (assert (equal "42" (funcall test)))))
502 (with-test (:name (:format :compile-literal-dest-string))
503 (assert (eq :warned
504 (handler-case
505 (compile nil
506 `(lambda (x) (format "~A" x)))
507 ((and warning (not style-warning)) ()
508 :warned)))))
510 (with-test (:name :bug-308961)
511 (assert (string= (format nil "~4,1F" 0.001) " 0.0"))
512 (assert (string= (format nil "~4,1@F" 0.001) "+0.0"))
513 (assert (string= (format nil "~E" 0.01) "1.e-2"))
514 (assert (string= (format nil "~G" 0.01) "1.00e-2")))
516 (with-test (:name (:fp-read/print-consistency single-float))
517 (let ((*random-state* (make-random-state t))
518 (oops))
519 (loop for f = most-positive-single-float then (/ f 2.0)
520 while (> f 0.0)
521 do (loop repeat 10
522 for fr = (random f)
523 do (unless (eql fr (read-from-string (prin1-to-string fr)))
524 (push fr oops)
525 (return))))
526 (loop for f = most-negative-single-float then (/ f 2.0)
527 while (< f -0.0)
528 do (loop repeat 10
529 for fr = (- (random (- f)))
530 do (unless (eql fr (read-from-string (prin1-to-string fr)))
531 (push fr oops)
532 (return))))
533 (when oops
534 (error "FP read/print inconsistencies:~%~:{ ~S => ~S~%~}"
535 (mapcar (lambda (f)
536 (list f (read-from-string (prin1-to-string f))))
537 oops)))))
539 (with-test (:name (:fp-read/print-consistency double-float))
540 (let ((*random-state* (make-random-state t))
541 (oops))
542 ;; FIXME skipping denormalized floats due to bug 793774.
543 (loop for f = most-positive-double-float then (/ f 2d0)
544 while (> f 0d0)
545 do (loop repeat 10
546 for fr = (random f)
547 do (unless (float-denormalized-p fr)
548 (unless (eql fr (read-from-string (prin1-to-string fr)))
549 (push fr oops)
550 (return)))))
551 (loop for f = most-negative-double-float then (/ f 2d0)
552 while (< f -0d0)
553 do (loop repeat 10
554 for fr = (- (random (- f)))
555 do (unless (float-denormalized-p fr)
556 (unless (eql fr (read-from-string (prin1-to-string fr)))
557 (push fr oops)
558 (return)))))
559 (when oops
560 (error "FP read/print inconsistencies:~%~:{ ~S => ~S~%~}"
561 (mapcar (lambda (f)
562 (list f (read-from-string (prin1-to-string f))))
563 oops)))))
565 (with-test (:name :bug-811386)
566 (assert (equal " 0.00" (format nil "~7,2,-2f" 0)))
567 (assert (equal " 0.00" (format nil "~7,2,2f" 0)))
568 (assert (equal " 0.01" (format nil "~7,2,-2f" 1)))
569 (assert (equal " 100.00" (format nil "~7,2,2f" 1)))
570 (assert (equal " 0.00" (format nil "~7,2,-2f" 0.1)))
571 (assert (equal " 10.00" (format nil "~7,2,2f" 0.1)))
572 (assert (equal " 0.01" (format nil "~7,2,-2f" 0.5))))
574 (with-test (:name :bug-867684)
575 (assert (equal "ab" (format nil "a~0&b"))))
577 (with-test (:name :print-unreadably-function)
578 (assert (equal "\"foo\""
579 (handler-bind ((print-not-readable #'sb-ext:print-unreadably))
580 (write-to-string (coerce "foo" 'base-string) :readably t)))))
582 (with-test (:name :printing-specialized-arrays-readably)
583 (let ((*read-eval* t)
584 (dimss (loop repeat 10
585 collect (loop repeat (1+ (random 3))
586 collect (1+ (random 10)))))
587 (props sb-vm::*specialized-array-element-type-properties*))
588 (labels ((random-elt (type)
589 (case type
590 (base-char
591 (code-char (random 128)))
592 (character
593 (code-char (random char-code-limit)))
594 (single-float
595 (+ least-positive-normalized-single-float
596 (random most-positive-single-float)))
597 (double-float
598 (+ least-positive-normalized-double-float
599 (random most-positive-double-float)))
600 (bit
601 (random 2))
602 (fixnum
603 (random most-positive-fixnum))
604 ((t)
606 (otherwise
607 (destructuring-bind (type x) type
608 (ecase type
609 (unsigned-byte
610 (random (1- (expt 2 x))))
611 (signed-byte
612 (- (random (expt 2 (1- x)))))
613 (complex
614 (complex (random-elt x) (random-elt x)))))))))
615 (dotimes (i (length props))
616 (let ((et (sb-vm::saetp-specifier (aref props i))))
617 (when et
618 (when (eq 'base-char et)
619 ;; base-strings not included in the #. printing.
620 (go :next))
621 (dolist (dims dimss)
622 (let ((a (make-array dims :element-type et)))
623 (assert (equal et (array-element-type a)))
624 (dotimes (i (array-total-size a))
625 (setf (row-major-aref a i) (random-elt et)))
626 (let ((copy (read-from-string (write-to-string a :readably t))))
627 (assert (equal dims (array-dimensions copy)))
628 (assert (equal et (array-element-type copy)))
629 (assert (equal (array-total-size a) (array-total-size copy)))
630 (dotimes (i (array-total-size a))
631 (assert (equal (row-major-aref a i) (row-major-aref copy i)))))))))
632 :next))))
634 (with-test (:name (:format :negative-colinc-and-mincol))
635 (assert (raises-error? (format nil "~-2a" 1)))
636 (assert (raises-error? (format nil "~,0a" 1))))
638 ;;; success