1 ;;;; miscellaneous tests of printing stuff
3 ;;;; This software is part of the SBCL system. See the README file for
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
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)
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
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
)))
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
))
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
)
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
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
))
111 (multiple-value-bind (fun failure-p
)
112 (checked-compile `(lambda (format nil
,@args
))
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
))
134 (multiple-value-bind (fun failure-p
)
135 (checked-compile `(lambda () (format nil
,@args
))
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
"~<~@>"))
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
156 (with-output-to-string (s)
157 (let ((*print-readably
* t
))
158 (print (make-array '(1 2 0)) s
)))))
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
)
168 (write-to-string array
:readably t
))
169 ;; it might not be readably-printable
170 (or (typep error
'print-not-readable
)
172 ;; or else it had better have the same dimensions
173 (equal (array-dimensions result
) (array-dimensions array
))
174 ;; and the same element-type
175 (equal (array-element-type result
) (array-element-type array
))))))))
177 ;;; before 0.8.0.66 it signalled UNBOUND-VARIABLE
178 (with-test (:name
(write vector
:smoke
))
179 (let ((*standard-output
* (make-broadcast-stream)))
180 (write #(1 2 3) :pretty nil
:readably t
)))
182 ;;; another UNBOUND-VARIABLE, this time due to a bug in FORMATTER
184 (with-test (:name
(formatter :smoke
))
185 (funcall (formatter "~@<~A~:*~A~:>") (make-broadcast-stream) 3))
187 ;;; the PPC floating point backend was at one point sufficiently
188 ;;; broken that this looped infinitely or caused segmentation
189 ;;; violations through stack corruption.
190 (with-test (:name
(print float
:smoke
))
191 (let ((*standard-output
* (make-broadcast-stream)))
194 ;;; In sbcl-0.8.7, the ~W format directive interpreter implemented the
195 ;;; sense of the colon and at-sign modifiers exactly backwards.
197 ;;; (Yes, the test for this *is* substantially hairier than the fix;
198 ;;; wanna make something of it?)
199 (cl:in-package
:cl-user
)
200 (defstruct wexerciser-0-8-7
)
201 (defun wexercise-0-8-7-interpreted (wformat)
202 (format t wformat
(make-wexerciser-0-8-7)))
203 (defmacro define-compiled-wexercise-0-8-7
(wexercise wformat
)
204 `(defun ,wexercise
()
205 (declare (optimize (speed 3) (space 1)))
206 (format t
,wformat
(make-wexerciser-0-8-7))
208 (define-compiled-wexercise-0-8-7 wexercise-0-8-7-compiled-without-atsign
"~W")
209 (define-compiled-wexercise-0-8-7 wexercise-0-8-7-compiled-with-atsign
"~@W")
210 (defmethod print-object :before
((wexerciser-0-8-7 wexerciser-0-8-7
) stream
)
211 (unless (and *print-level
* *print-length
*)
212 (error "gotcha coming")))
213 (with-test (:name
(format :write-directive
:colon
:at-sign
1))
214 (let ((*print-level
* 11)
216 (*standard-output
* (make-broadcast-stream)))
217 (wexercise-0-8-7-interpreted "~W")
218 (wexercise-0-8-7-compiled-without-atsign)))
219 (remove-method #'print-object
220 (find-method #'print-object
222 (mapcar #'find-class
'(wexerciser-0-8-7 t
))))
223 (defmethod print-object :before
((wexerciser-0-8-7 wexerciser-0-8-7
) stream
)
224 (when (or *print-level
* *print-length
*)
225 (error "gotcha going")))
226 (with-test (:name
(format :write-directive
:colon
:at-sign
2))
227 (let ((*print-level
* 11)
229 (*standard-output
* (make-broadcast-stream)))
230 (wexercise-0-8-7-interpreted "~@W")
231 (wexercise-0-8-7-compiled-with-atsign)))
233 ;;; WRITE-TO-STRING was erroneously DEFKNOWNed as FOLDABLE
235 ;;; This bug from PFD
236 (defpackage "SCRATCH-WRITE-TO-STRING" (:use
))
237 (with-test (:name
(write symbol
*package
*))
238 (with-standard-io-syntax
239 (let* ((*package
* (find-package "SCRATCH-WRITE-TO-STRING"))
240 (answer (write-to-string 'scratch-write-to-string
::x
:readably nil
)))
241 (assert (string= answer
"X")))))
243 ;;; and a couple from Bruno Haible
244 (defun my-pprint-reverse (out list
)
246 (when (setq list
(reverse list
))
248 (write (pop list
) :stream out
)
249 (when (endp list
) (return))
250 (write-char #\Space out
)))
251 (write-char #\
) out
))
252 (with-test (:name
(write *print-pprint-dispatch
* 1))
253 (with-standard-io-syntax
254 (let ((*print-pprint-dispatch
* (copy-pprint-dispatch)))
255 (set-pprint-dispatch '(cons (member foo
)) 'my-pprint-reverse
0)
256 (let ((answer (write-to-string '(foo bar
:boo
1) :pretty t
:escape t
)))
257 (assert (string= answer
"(1 :BOO BAR FOO)"))))))
259 (defun my-pprint-logical (out list
)
260 (pprint-logical-block (out list
:prefix
"(" :suffix
")")
264 (write (pprint-pop) :stream out
)
266 (pprint-exit-if-list-exhausted)
267 (write-char #\Space out
)))))
268 (with-test (:name
(write *print-pprint-dispatch
* 2))
269 (with-standard-io-syntax
270 (let ((*print-pprint-dispatch
* (copy-pprint-dispatch)))
271 (set-pprint-dispatch '(cons (member bar
)) 'my-pprint-logical
0)
272 (let ((answer (write-to-string '(bar foo
:boo
1) :pretty t
:escape t
)))
273 (assert (string= answer
"(?BAR? ?FOO? ?:BOO? ?1?)"))))))
275 ;;; FORMAT string compile-time checker failure, reported by Thomas
277 (with-test (:name
(format :compile-time-check
))
278 (multiple-value-bind (fun failure-p warnings
)
279 (checked-compile '(lambda () (format nil
"~{"))
280 :allow-failure t
:allow-warnings t
)
282 (assert (= (length warnings
) 1))
283 (assert-error (funcall fun
))))
285 ;;; floating point print/read consistency
286 (with-test (:name
(read print float
:consistency
))
287 (let* ((x (/ -
9.349640046247849d-21 -
9.381494249123696d-11
))
288 (y (read-from-string (write-to-string x
:readably t
))))
291 (let ((x1 (float -
5496527/100000000000000000))
292 (x2 (float -
54965272/1000000000000000000)))
293 (assert (or (equal (multiple-value-list (integer-decode-float x1
))
294 (multiple-value-list (integer-decode-float x2
)))
295 (string/= (prin1-to-string x1
) (prin1-to-string x2
))))))
297 ;;; readable printing of arrays with *print-radix* t
298 (with-test (:name
(write read array
*print-radix
*))
299 (let ((*print-radix
* t
)
301 (*print-pretty
* nil
))
302 (let ((output (with-output-to-string (s)
303 (write #2a
((t t
) (nil nil
)) :stream s
))))
304 (assert (equalp (read-from-string output
) #2a
((t t
) (nil nil
)))))))
306 ;;; NIL parameters to "interpreted" FORMAT directives
307 (with-test (:name
(format :v-directive-arg nil
))
308 (assert (string= (format nil
"~v%" nil
) (string #\Newline
))))
310 ;;; PRINC-TO-STRING should bind print-readably
311 (with-test (:name
(princ-to-string *print-readably
*))
312 (let ((*print-readably
* t
))
313 (assert (string= (princ-to-string #\
7)
314 (write-to-string #\
7 :escape nil
:readably nil
)))))
316 ;;; in FORMAT, ~^ inside ~:{ should go to the next case, not break
317 ;;; iteration, even if one argument is just a one-element list.
318 (with-test (:name
(format :escape-upward-directive
:in
:iteration-directive
))
319 (assert (string= (format nil
"~:{~A~^~}" '((A) (C D
))) "AC")))
321 ;;; errors should be signaled if pprint and justification are mixed
323 (with-test (:name
(format :mixing
:justification-directive
:pprint-directives
:illegal
))
324 (dolist (x (list "~<~:;~>~_" "~<~:;~>~I" "~<~:;~>~W"
325 "~<~:;~>~:T" "~<~:;~>~<~:>" "~_~<~:;~>"
326 "~I~<~:;~>" "~W~<~:;~>" "~:T~<~:;~>" "~<~:>~<~:;~>"))
327 (assert-error (format nil x nil
))
328 (assert-error (format nil
(eval `(formatter ,x
)) nil
))))
329 ;;; ...but not in judicious cases.
330 (with-test (:name
(format :mixing
:justification-directive
:pprint-directives
:legal
))
331 (dolist (x (list "~<~;~>~_" "~<~;~>~I" "~<~;~>~W"
332 "~<~;~>~:T" "~<~;~>~<~>" "~_~<~;~>"
333 "~I~<~;~>" "~W~<~;~>" "~:T~<~;~>" "~<~>~<~;~>"
334 "~<~:;~>~T" "~T~<~:;~>"))
335 (assert (format nil x nil
))
336 (assert (format nil
(eval `(formatter ,x
)) nil
))))
338 ;;; bug 350: bignum printing so memory-hungry that heap runs out
339 ;;; -- just don't stall here forever on a slow box
340 (with-test (:name
:bug-350
)
343 (print (ash 1 1000000) (make-broadcast-stream)))
347 ;;; bug 371: bignum print/read inconsistency
348 (defvar *bug-371
* -
7043009959286724629649270926654940933664689003233793014518979272497911394287216967075767325693021717277238746020477538876750544587281879084559996466844417586093291189295867052594478662802691926547232838591510540917276694295393715934079679531035912244103731582711556740654671309980075069010778644542022/670550434139267031632063192770201289106737062379324644110801846820471752716238484923370056920388400273070254958650831435834503195629325418985020030706879602898158806736813101434594805676212779217311897830937606064579213895527844045511878668289820732425014254579493444623868748969110751636786165152601)
349 (with-test (:name
(read print bignum
:consistency
:bug-371
))
350 (let ((*print-base
* 5)
353 (assert (= *bug-371
* (read-from-string (prin1-to-string *bug-371
*))))))
355 ;;; a spot of random-testing for rational printing
356 (defvar *seed-state
* (make-random-state))
357 (with-open-file (f "last-random-state.lisp-expr"
358 :direction
:output
:if-exists
:supersede
)
359 ;; I don't want to see this every time
360 (write *seed-state
* :pretty nil
:stream f
)) ; so that we can reproduce errors
361 (with-test (:name
(read print rational
:consistency
))
362 (let ((seed (make-random-state *seed-state
*)))
364 do
(let ((n (random (ash 1 1000) seed
))
365 (d (random (ash 1 1000) seed
)))
366 (when (zerop (random 2 seed
))
369 (loop for base from
2 to
36
370 do
(let ((*print-base
* base
)
373 (assert (= r
(read-from-string (prin1-to-string r
))))
377 (assert (not (eql r
(read-from-string (prin1-to-string r
)))))
378 (let ((*print-radix
* t
))
379 (assert (= r
(read-from-string
380 (princ-to-string r
))))))))))))
382 ;;;; Bugs, found by PFD
383 ;;; NIL parameter for ~^ means `not supplied'
384 (with-test (:name
(format :escape-upward-directive
:v-directive-arg nil
))
385 (loop for
(format arg result
) in
386 '(("~:{~D~v^~D~}" ((3 1 4) (1 0 2) (7 nil
) (5 nil
6)) "341756")
387 ("~:{~1,2,v^~A~}" ((nil 0) (3 1) (0 2)) "02"))
388 do
(assert (string= (funcall #'format nil format arg
) result
))
389 do
(assert (string= (with-output-to-string (s)
390 (funcall (eval `(formatter ,format
)) s arg
))
393 ;;; NIL first parameter for ~R is equivalent to no parameter.
394 (with-test (:name
(format :radix-directive nil
:argument
))
395 (assert (string= (format nil
"~VR" nil
5) "five"))
396 (assert (string= (format nil
(formatter "~VR") nil
6) "six")))
398 ;;; CSR inserted a bug into Burger & Dybvig's float printer. Caught
400 (with-test (:name
(format :exponential-floating-point-directive
:smoke
))
401 (assert (string= (format nil
"~E" 1d23
) "1.d+23")))
403 ;;; Fixed-format bugs from CLISP's test suite (reported by Bruno
405 (with-test (:name
(format :fixed-format-floating-point-directive
:bug-317
))
406 (assert (string= (format nil
"~1F" 10) "10."))
407 (assert (string= (format nil
"~0F" 10) "10."))
408 (assert (string= (format nil
"~2F" 1234567.1) "1234567.")))
410 ;;; here's one that seems to fail most places. I think this is right,
411 ;;; and most of the other answers I've seen are definitely wrong.
412 (with-test (:name
(format :general-floating-point-directive
:smoke
))
413 (assert (string= (format nil
"~G" 1d23
) "100000000000000000000000. ")))
415 ;;; Adam Warner's test case
416 (with-test (:name
(format :fixed-format-floating-point-directive
:at-sign
))
417 (assert (string= (format nil
"~@F" 1.23) "+1.23")))
420 ;;; New (2005-11-08, also known as CSR House day) float format test
421 ;;; cases. Simon Alexander, Raymond Toy, and others
422 (with-test (:name
(format :floating-point-directives
:misc
))
423 (assert (string= (format nil
"~9,4,,-7E" pi
) ".00000003d+8"))
424 (assert (string= (format nil
"~9,4,,-5E" pi
) ".000003d+6"))
425 (assert (string= (format nil
"~5,4,,7E" pi
) "3141600.d-6"))
426 (assert (string= (format nil
"~11,4,,3E" pi
) " 314.16d-2"))
427 (assert (string= (format nil
"~11,4,,5E" pi
) " 31416.d-4"))
428 (assert (string= (format nil
"~11,4,,0E" pi
) " 0.3142d+1"))
429 (assert (string= (format nil
"~9,,,-1E" pi
) ".03142d+2"))
430 (assert (string= (format nil
"~,,,-2E" pi
) "0.003141592653589793d+3"))
431 (assert (string= (format nil
"~,,,2E" pi
) "31.41592653589793d-1"))
432 (assert (string= (format nil
"~E" pi
) "3.141592653589793d+0"))
433 (assert (string= (format nil
"~9,5,,-1E" pi
) ".03142d+2"))
434 (assert (string= (format nil
"~11,5,,-1E" pi
) " 0.03142d+2"))
435 (assert (string= (format nil
"~G" pi
) "3.141592653589793 "))
436 (assert (string= (format nil
"~9,5G" pi
) "3.1416 "))
437 (assert (string= (format nil
"|~13,6,2,7E|" pi
) "| 3141593.d-06|"))
438 (assert (string= (format nil
"~9,3,2,0,'%E" pi
) "0.314d+01"))
439 (assert (string= (format nil
"~9,0,6f" pi
) " 3141593."))
440 (assert (string= (format nil
"~6,2,1,'*F" pi
) " 31.42"))
441 (assert (string= (format nil
"~6,2,1,'*F" (* 100 pi
)) "******"))
442 (assert (string= (format nil
"~9,3,2,-2,'%@E" pi
) "+.003d+03"))
443 (assert (string= (format nil
"~10,3,2,-2,'%@E" pi
) "+0.003d+03"))
444 (assert (string= (format nil
"~15,3,2,-2,'%,'=@E" pi
) "=====+0.003d+03"))
445 (assert (string= (format nil
"~9,3,2,-2,'%E" pi
) "0.003d+03"))
446 (assert (string= (format nil
"~8,3,2,-2,'%@E" pi
) "%%%%%%%%"))
448 (assert (string= (format nil
"~g" 1e0
) "1. "))
449 (assert (string= (format nil
"~g" 1.2d40
) "12000000000000000000000000000000000000000. "))
451 (assert (string= (format nil
"~e" 0) "0.0e+0"))
452 (assert (string= (format nil
"~e" 0d0
) "0.0d+0"))
453 (assert (string= (format nil
"~9,,4e" 0d0
) "0.0d+0000")))
455 (with-test (:name
(print hash-table print-not-readable
))
456 (let ((table (make-hash-table)))
457 (setf (gethash 1 table
) t
)
458 (assert-error (with-standard-io-syntax
459 (let ((*read-eval
* nil
)
460 (*print-readably
* t
))
461 (with-output-to-string (*standard-output
*)
463 print-not-readable
)))
465 ;; Test that we can print characters readably regardless of the external format
468 (defun test-readable-character (character external-format
)
469 (let ((file "print.impure.tmp"))
472 (with-open-file (stream file
474 :external-format external-format
475 :if-exists
:supersede
)
476 (write character
:stream stream
:readably t
))
477 (with-open-file (stream file
479 :external-format external-format
480 :if-does-not-exist
:error
)
481 (assert (char= (read stream
) character
))))
483 (delete-file file
)))))
485 (with-test (:name
(:print-readable
:character
:utf-8
) :skipped-on
(not :sb-unicode
))
486 (test-readable-character (code-char #xfffe
) :utf-8
))
488 (with-test (:name
(:print-readable
:character
:iso-8859-1
) :skipped-on
(not :sb-unicode
))
489 (test-readable-character (code-char #xfffe
) :iso-8859-1
))
491 (with-test (:name
(format :character-directive
:colon
))
492 (assert (string= (eval '(format nil
"~:C" #\a)) "a"))
493 (assert (string= (format nil
(formatter "~:C") #\a) "a")))
495 ;;; This used to trigger an AVER instead.
496 (with-test (:name
(format :end-of-justification-directive
:mismatch
))
497 (assert-error (eval '(formatter "~>")) sb-format
:format-error
)
498 (assert-error (eval '(format t
"~>")) sb-format
:format-error
))
500 ;;; readably printing hash-tables, check for circularity
501 (with-test (:name
(print read hash-table
*print-circle
*))
503 (h (make-hash-table))
507 (setf (gethash x h
) h
)
508 (destructuring-bind (x2 . h2
) (read-from-string (write-to-string (cons x h
)))
509 (assert (equal x x2
))
510 (assert (eq h2
(gethash x2 h2
))))))
512 ;;; an off-by-one error in the ~R format directive until 1.0.15.20
513 ;;; prevented printing cardinals and ordinals between (expt 10 63) and
514 ;;; (1- (expt 10 66))
515 (with-test (:name
(format :radix-directive
:large-values
))
516 (assert (string= (format nil
"~R" (expt 10 63)) "one vigintillion"))
517 (assert (string= (format nil
"~:R" (expt 10 63)) "one vigintillionth")))
519 ;;; too-clever cacheing for PRINT-OBJECT resulted in a bogus method
520 ;;; for printing RESTART objects. Check also CONTROL-STACK-EXHAUSTED
521 ;;; and HEAP-EXHAUSTED-ERROR.
522 (with-test (:name
(print-object restart condition
))
523 (let ((result (with-output-to-string (*standard-output
*)
524 (princ (find-restart 'abort
)))))
525 (assert (string/= result
"#<" :end1
2)))
526 (let ((result (with-output-to-string (*standard-output
*)
527 (princ (make-condition 'sb-kernel
::control-stack-exhausted
)))))
528 (assert (string/= result
"#<" :end1
2)))
529 (let ((result (with-output-to-string (*standard-output
*)
530 (princ (make-condition 'sb-kernel
::heap-exhausted-error
)))))
531 (assert (string/= result
"#<" :end1
2))))
533 (with-test (:name
(:with-standard-io-syntax
:bind-print-pprint-dispatch
))
534 (let ((*print-pprint-dispatch
* (copy-pprint-dispatch nil
)))
535 (set-pprint-dispatch 'symbol
#'(lambda (stream obj
)
536 (declare (ignore obj
))
537 (write-string "FOO" stream
)))
538 (with-standard-io-syntax
539 (let ((*print-pretty
* t
))
540 (assert (string= (princ-to-string 'bar
) "BAR"))))))
542 ;;; lp#1398290 (which obsoletes lp#488979)
544 (defclass a-class-name
() ())
546 (with-test (:name
:print-unreadable-no-conditional-newline
)
547 (flet ((test (pretty)
548 (assert (not (find #\Newline
549 (let ((*print-pretty
* pretty
)
550 (*print-right-margin
* 10))
551 (format nil
"~A" (make-instance 'a-class-name
)))
556 ;;; The PRINT-OBJECT method for RANDOM-STATE used to have a bogus
557 ;;; dimension argument for MAKE-ARRAY.
558 (with-test (:name
(print random-state
))
559 (assert (equalp *random-state
*
561 (write-to-string *random-state
*)))))
563 (with-test (:name
(write :return-value
))
564 ;; COMPILE is called explicitly because there was a bug in the
565 ;; compiler-macro for WRITE, which isn't expanded by the evaluator.
566 (checked-compile-and-assert ()
567 '(lambda (s) (write 123 :stream s
))
568 (((make-broadcast-stream)) 123)))
570 (with-test (:name
(write write-to-string compiler-macro
:lp598374
:lp581564
))
571 (let ((test (checked-compile
572 `(lambda (object &optional output-stream
)
573 (write object
:stream output-stream
)))))
574 (assert (equal "(HELLO WORLD)"
575 (with-output-to-string (*standard-output
*)
576 (let ((list '(hello world
)))
577 (assert (eq list
(funcall test list
)))))))
579 (with-output-to-string (*standard-output
*)
580 (assert (eql 12 (funcall test
12)))))))
581 (checked-compile-and-assert ()
583 (let ((*print-length
* 42))
584 (write-to-string *print-length
* :length nil
)))
587 (with-test (:name
(format :compile-literal-dest-string
))
588 (multiple-value-bind (fun failure-p warnings
)
589 (checked-compile `(lambda (x) (format "~A" x
))
591 (declare (ignore fun
))
593 (assert (= (length warnings
) 1))))
595 (with-test (:name
(format :bug-308961
))
596 (assert (string= (format nil
"~4,1F" 0.001) " 0.0"))
597 (assert (string= (format nil
"~4,1@F" 0.001) "+0.0"))
598 (assert (string= (format nil
"~E" 0.01) "1.e-2"))
599 (assert (string= (format nil
"~G" 0.01) "1.00e-2")))
601 (with-test (:name
(:fp-print-read-consistency single-float
))
602 (let ((*random-state
* (make-random-state t
))
604 (loop for f
= most-positive-single-float then
(/ f
2.0)
608 do
(unless (eql fr
(read-from-string (prin1-to-string fr
)))
611 (loop for f
= most-negative-single-float then
(/ f
2.0)
614 for fr
= (- (random (- f
)))
615 do
(unless (eql fr
(read-from-string (prin1-to-string fr
)))
619 (error "FP print-read inconsistencies:~%~:{ ~S => ~S~%~}"
621 (list f
(read-from-string (prin1-to-string f
))))
624 (with-test (:name
(:fp-print-read-consistency double-float
))
625 (let ((*random-state
* (make-random-state t
))
627 ;; FIXME skipping denormalized floats due to bug 793774.
628 (loop for f
= most-positive-double-float then
(/ f
2d0
)
632 do
(unless (float-denormalized-p fr
)
633 (unless (eql fr
(read-from-string (prin1-to-string fr
)))
636 (loop for f
= most-negative-double-float then
(/ f
2d0
)
639 for fr
= (- (random (- f
)))
640 do
(unless (float-denormalized-p fr
)
641 (unless (eql fr
(read-from-string (prin1-to-string fr
)))
645 (error "FP print-read inconsistencies:~%~:{ ~S => ~S~%~}"
647 (list f
(read-from-string (prin1-to-string f
))))
650 (with-test (:name
(format :bug-811386
))
651 (assert (equal " 0.00" (format nil
"~7,2,-2f" 0)))
652 (assert (equal " 0.00" (format nil
"~7,2,2f" 0)))
653 (assert (equal " 0.01" (format nil
"~7,2,-2f" 1)))
654 (assert (equal " 100.00" (format nil
"~7,2,2f" 1)))
655 (assert (equal " 0.00" (format nil
"~7,2,-2f" 0.1)))
656 (assert (equal " 10.00" (format nil
"~7,2,2f" 0.1)))
657 (assert (equal " 0.01" (format nil
"~7,2,-2f" 0.5))))
659 (with-test (:name
(format :bug-867684
))
660 (assert (equal "ab" (format nil
"a~0&b"))))
662 (with-test (:name
:print-unreadably-function
)
663 (assert (search "#<HASH-TABLE"
664 (handler-bind ((print-not-readable #'sb-ext
:print-unreadably
))
665 (let ((*read-eval
* nil
))
666 (write-to-string (make-hash-table) :readably t
))))))
668 (with-test (:name
:printing-specialized-arrays-readably
)
669 (let ((*read-eval
* t
)
670 (dimss (loop repeat
10
671 collect
(loop repeat
(1+ (random 3))
672 collect
(1+ (random 10)))))
673 (props sb-vm
::*specialized-array-element-type-properties
*))
674 (labels ((random-elt (type)
677 (code-char (random 128)))
679 (code-char (random char-code-limit
)))
681 (+ least-positive-normalized-single-float
682 (random most-positive-single-float
)))
684 (+ least-positive-normalized-double-float
685 (random most-positive-double-float
)))
689 (random most-positive-fixnum
))
693 (destructuring-bind (type x
) type
696 (random (1- (expt 2 x
))))
698 (- (random (expt 2 (1- x
)))))
700 (complex (random-elt x
) (random-elt x
)))))))))
701 (dotimes (i (length props
))
702 (let ((et (sb-vm::saetp-specifier
(aref props i
))))
704 (when (eq 'base-char et
)
705 ;; base-strings not included in the #. printing.
708 (let ((a (make-array dims
:element-type et
)))
709 (assert (equal et
(array-element-type a
)))
710 (dotimes (i (array-total-size a
))
711 (setf (row-major-aref a i
) (random-elt et
)))
712 (let ((copy (read-from-string (write-to-string a
:readably t
))))
713 (assert (equal dims
(array-dimensions copy
)))
714 (assert (equal et
(array-element-type copy
)))
715 (assert (equal (array-total-size a
) (array-total-size copy
)))
716 (dotimes (i (array-total-size a
))
717 (assert (equal (row-major-aref a i
) (row-major-aref copy i
)))))))))
720 (with-test (:name
(format :negative-colinc-and-mincol
))
721 (assert-error (format nil
"~-2a" 1))
722 (assert-error (format nil
"~,0a" 1)))
724 (with-test (:name
(format :bug-905817
))
725 ;; The bug manifests itself in an endless loop in FORMAT.
726 ;; Correct behaviour is to signal an error.
729 (assert-error (format nil
"e~8,0s" 12395)))
731 (error "Endless loop in FORMAT"))))
733 (with-test (:name
(format :type-check
))
734 (assert (equal "1/10" (format nil
"~2r" 1/2)))
735 (assert-error (format nil
"~r" 1.32) sb-format
:format-error
)
736 (assert-error (format nil
"~c" 1.32) sb-format
:format-error
)
737 (assert (equal "1/10" (eval '(format nil
"~2r" 1/2))))
738 (assert-error (eval '(format nil
"~r" 1.32)) sb-format
:format-error
)
739 (assert-error (eval '(format nil
"~c" 1.32)) sb-format
:format-error
))
741 ;; Setup for test of print-object on a class with no proper name.
742 ;; There are various PRINT-OBJECT tests strewn throughout, all of which
743 ;; are not in the obvious place to me, except for perhaps 'clos.impure'
744 ;; which is also not the right place because that file concerns CLOS
745 ;; behavior in general, not to mention being far too noisy in its output.
746 (defclass fruit
() (a))
747 (defvar *fruit1
* (find-class 'fruit
))
748 (setf (find-class 'fruit
) nil
)
749 (defclass fruit
() (n o
))
750 (defvar *fruit2
* (find-class 'fruit
))
751 (setf (find-class 'fruit
) nil
)
752 (defclass fruit
() (x))
753 (with-test (:name
(print-object :improper-class-name
))
754 (assert (string/= (write-to-string *fruit1
*) (write-to-string *fruit2
*)))
755 (assert (string/= (write-to-string (find-class 'fruit
))
756 (write-to-string *fruit1
*))))
758 (defclass f-s-o-for-print-object
(sb-mop:funcallable-standard-object
)
760 (:metaclass sb-mop
:funcallable-standard-class
))
761 (with-test (:name
(print-object sb-mop
:funcallable-standard-object
))
762 (let ((instance (make-instance 'f-s-o-for-print-object
)))
763 (assert (eql 0 (search "#<F-S-O-FOR-PRINT-OBJECT"
764 (with-output-to-string (stream)
765 (print-object instance stream
)))))))
767 (with-test (:name
(format :readably
))
768 (let ((*print-readably
* t
))
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 (declare (notinline format
))
776 (assert (format nil
"~$" #'format
))
777 (assert (format nil
"~d" #'format
))
778 (assert (format nil
"~x" #'format
))
779 (assert (format nil
"~b" #'format
))
780 (assert (format nil
"~3r" #'format
)))))
782 (with-test (:name
(format *print-base
*))
783 (let ((*print-base
* 3))
784 (assert (equal (format nil
"~g" '(123)) "(123)"))
785 (assert (equal (format nil
"~f" '(123)) "(123)"))
786 (assert (equal (format nil
"~e" '(123)) "(123)"))
787 (assert (equal (format nil
"~$" '(123)) "(123)"))
789 (declare (notinline format
))
790 (assert (equal (format nil
"~g" '(123)) "(123)"))
791 (assert (equal (format nil
"~f" '(123)) "(123)"))
792 (assert (equal (format nil
"~e" '(123)) "(123)"))
793 (assert (equal (format nil
"~$" '(123)) "(123)")))))
795 (with-test (:name
(format :concatenate
))
796 (checked-compile-and-assert ()
797 `(lambda (x) (format nil
"~s" (the string x
)))
798 (("\\") (prin1-to-string "\\"))))
800 (with-test (:name
(write :stream nil
))
803 (with-output-to-string (*standard-output
*)
804 (funcall (checked-compile `(lambda () (write "xx" :stream nil
)))))
807 (define-condition foo
() (a))
808 (defvar *ccc
* (make-condition 'foo
))
809 (handler-bind ((warning #'muffle-warning
)) (define-condition foo
(warning) (a)))
810 (with-test (:name
:write-obsolete-condition
)
811 (assert (search "UNPRINTABLE" (write-to-string *ccc
*))))
813 (with-test (:name
(format :no-overeager-compile-time-processing
))
814 (checked-compile '(lambda (x) (format t
"~/nopackage:nofun/" x
))))
816 (with-test (:name
(write :case
:capitalize
))
817 (assert (string= (write-to-string 'fluffy-bunny-count
:case
:capitalize
)
818 "Fluffy-Bunny-Count")))
820 (defclass foo2
() ())
821 (with-test (:name
(print :random standard-object
:lp1654550
))
822 (assert (search "#<FOO2 {" (write-to-string (make-instance 'foo2
) :pretty nil
)))
823 (assert (search "#<FOO2 {" (write-to-string (make-instance 'foo2
) :pretty t
))))
825 ;; STRINGIFY-OBJECT failed to use the pretty-print dispatch table
827 (with-test (:name
:stringify-pretty-integer
)
830 (set-pprint-dispatch 'fixnum
832 (format stream
"#{Fixnum ")
833 (write obj
:stream stream
:pretty nil
)
834 (write-char #\
} stream
)))
835 (assert (string= (write-to-string 92 :pretty t
)
837 (set-pprint-dispatch 'fixnum nil
)))
839 (with-test (:name
:readable-vector-circularity
)
840 (let ((x (make-array 1 :element-type
'base-char
:initial-contents
"x"))
841 (y (make-array 1 :element-type
'base-char
:initial-contents
"y")))
842 (assert (equal (read-from-string (write-to-string (list x y
) :readably t
:circle t
))