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 ;;; We should be able to output X readably (at least when *READ-EVAL*).
15 (defun assert-readable-output (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
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
)))
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
))
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
)
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
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
))
108 (multiple-value-bind (fun failure-p
)
109 (checked-compile `(lambda (format nil
,@args
))
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
))
131 (multiple-value-bind (fun failure-p
)
132 (checked-compile `(lambda () (format nil
,@args
))
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
"~<~@>"))
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
153 (with-output-to-string (s)
154 (let ((*print-readably
* t
))
155 (print (make-array '(1 2 0)) s
)))))
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
)
165 (write-to-string array
:readably t
))
166 ;; it might not be readably-printable
167 (or (typep error
'print-not-readable
)
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
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)))
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))
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)
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
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)
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
)
243 (when (setq list
(reverse list
))
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
")")
261 (write (pprint-pop) :stream 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
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
)
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
))))
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
)
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
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
)
340 (print (ash 1 1000000) (make-broadcast-stream)))
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)
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))
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
*)))
362 do
(let ((n (random (ash 1 1000) seed
))
363 (d (random (ash 1 1000) seed
)))
364 (when (zerop (random 2 seed
))
367 (loop for base from
2 to
36
368 do
(let ((*print-base
* base
)
371 (assert (= r
(read-from-string (prin1-to-string r
))))
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
))
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
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
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
*)
461 print-not-readable
)))
463 ;; Test that we can print characters readably regardless of the external format
466 (defun test-readable-character (character external-format
)
467 (let ((file (scratch-file-name "tmp")))
470 (with-open-file (stream file
472 :external-format external-format
473 :if-exists
:supersede
)
474 (write character
:stream stream
:readably t
))
475 (with-open-file (stream file
477 :external-format external-format
478 :if-does-not-exist
:error
)
479 (assert (char= (read stream
) character
))))
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
*))
501 (h (make-hash-table))
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
)))
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
*
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
)))))))
577 (with-output-to-string (*standard-output
*)
578 (assert (eql 12 (funcall test
12)))))))
579 (checked-compile-and-assert ()
581 (let ((*print-length
* 42))
582 (write-to-string *print-length
* :length nil
)))
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
))
589 (declare (ignore fun
))
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
))
602 (loop for f
= most-positive-single-float then
(/ f
2.0)
604 do
(loop for fr
= (random f
)
606 do
(unless (eql fr
(read-from-string (prin1-to-string fr
)))
609 (loop for f
= most-negative-single-float then
(/ f
2.0)
611 do
(loop for fr
= (- (random (- f
)))
613 do
(unless (eql fr
(read-from-string (prin1-to-string fr
)))
617 (error "FP print-read inconsistencies:~%~:{ ~S => ~S~%~}"
619 (list f
(read-from-string (prin1-to-string f
))))
622 (with-test (:name
(:fp-print-read-consistency double-float
))
623 (let ((*random-state
* (make-random-state t
))
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
)
629 do
(unless (eql fr
(read-from-string (prin1-to-string fr
)))
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
)))
636 do
(unless (eql fr
(read-from-string (prin1-to-string fr
)))
640 (error "FP print-read inconsistencies:~%~:{ ~S => ~S~%~}"
642 (list f
(read-from-string (prin1-to-string f
))))
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)
672 (code-char (random 128)))
674 (code-char (random char-code-limit
)))
676 (+ least-positive-normalized-single-float
677 (random most-positive-single-float
)))
679 (+ least-positive-normalized-double-float
680 (random most-positive-double-float
)))
684 (random most-positive-fixnum
))
688 (destructuring-bind (type x
) type
691 (random (1- (expt 2 x
))))
693 (- (random (expt 2 (1- x
)))))
695 (complex (random-elt x
) (random-elt x
)))))))))
696 (dotimes (i (length props
))
697 (let ((et (sb-vm:saetp-specifier
(aref props i
))))
699 (when (eq 'base-char et
)
700 ;; base-strings not included in the #. printing.
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
)))))))))
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.
724 (assert-error (format nil
"e~8,0s" 12395)))
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-error (format nil
"~r" 1.32) sb-format
:format-error
)
731 (assert-error (format nil
"~c" 1.32) sb-format
:format-error
)
732 (assert (equal "1/10" (eval '(format nil
"~2r" 1/2))))
733 (assert-error (eval '(format nil
"~r" 1.32)) sb-format
:format-error
)
734 (assert-error (eval '(format nil
"~c" 1.32)) sb-format
:format-error
))
736 ;; Setup for test of print-object on a class with no proper name.
737 ;; There are various PRINT-OBJECT tests strewn throughout, all of which
738 ;; are not in the obvious place to me, except for perhaps 'clos.impure'
739 ;; which is also not the right place because that file concerns CLOS
740 ;; behavior in general, not to mention being far too noisy in its output.
741 (defclass fruit
() (a))
742 (defvar *fruit1
* (find-class 'fruit
))
743 (setf (find-class 'fruit
) nil
)
744 (defclass fruit
() (n o
))
745 (defvar *fruit2
* (find-class 'fruit
))
746 (setf (find-class 'fruit
) nil
)
747 (defclass fruit
() (x))
748 (with-test (:name
(print-object :improper-class-name
))
749 (assert (string/= (write-to-string *fruit1
*) (write-to-string *fruit2
*)))
750 (assert (string/= (write-to-string (find-class 'fruit
))
751 (write-to-string *fruit1
*))))
753 (defclass f-s-o-for-print-object
(sb-mop:funcallable-standard-object
)
755 (:metaclass sb-mop
:funcallable-standard-class
))
756 (with-test (:name
(print-object sb-mop
:funcallable-standard-object
))
757 (let ((instance (make-instance 'f-s-o-for-print-object
)))
758 (assert (eql 0 (search "#<F-S-O-FOR-PRINT-OBJECT"
759 (with-output-to-string (stream)
760 (print-object instance stream
)))))))
762 (with-test (:name
(format :readably
))
763 (let ((*print-readably
* t
))
764 (assert (format nil
"~$" #'format
))
765 (assert (format nil
"~d" #'format
))
766 (assert (format nil
"~x" #'format
))
767 (assert (format nil
"~b" #'format
))
768 (assert (format nil
"~3r" #'format
))
770 (declare (notinline format
))
771 (assert (format nil
"~$" #'format
))
772 (assert (format nil
"~d" #'format
))
773 (assert (format nil
"~x" #'format
))
774 (assert (format nil
"~b" #'format
))
775 (assert (format nil
"~3r" #'format
)))))
777 (with-test (:name
(format *print-base
*))
778 (let ((*print-base
* 3))
779 (assert (equal (format nil
"~g" '(123)) "(123)"))
780 (assert (equal (format nil
"~f" '(123)) "(123)"))
781 (assert (equal (format nil
"~e" '(123)) "(123)"))
782 (assert (equal (format nil
"~$" '(123)) "(123)"))
784 (declare (notinline format
))
785 (assert (equal (format nil
"~g" '(123)) "(123)"))
786 (assert (equal (format nil
"~f" '(123)) "(123)"))
787 (assert (equal (format nil
"~e" '(123)) "(123)"))
788 (assert (equal (format nil
"~$" '(123)) "(123)")))))
790 (with-test (:name
(format :concatenate
))
791 (checked-compile-and-assert ()
792 `(lambda (x) (format nil
"~s" (the string x
)))
793 (("\\") (prin1-to-string "\\"))))
795 (with-test (:name
(write :stream nil
))
798 (with-output-to-string (*standard-output
*)
799 (funcall (checked-compile `(lambda () (write "xx" :stream nil
)))))
802 (define-condition foo
() (a))
803 (defvar *ccc
* (make-condition 'foo
))
804 (handler-bind ((warning #'muffle-warning
)) (define-condition foo
(warning) (a)))
805 (with-test (:name
:write-obsolete-condition
)
806 (assert (search "UNPRINTABLE" (write-to-string *ccc
*))))
808 (with-test (:name
(format :no-overeager-compile-time-processing
))
809 (checked-compile '(lambda (x) (format t
"~/nopackage:nofun/" x
))))
811 (with-test (:name
(write :case
:capitalize
))
812 (assert (string= (write-to-string 'fluffy-bunny-count
:case
:capitalize
)
813 "Fluffy-Bunny-Count")))
815 (defclass foo2
() ())
816 (with-test (:name
(print :random standard-object
:lp1654550
))
817 (assert (search "#<FOO2 {" (write-to-string (make-instance 'foo2
) :pretty nil
)))
818 (assert (search "#<FOO2 {" (write-to-string (make-instance 'foo2
) :pretty t
))))
820 ;; STRINGIFY-OBJECT failed to use the pretty-print dispatch table
822 (with-test (:name
:stringify-pretty-integer
)
825 (set-pprint-dispatch 'fixnum
827 (format stream
"#{Fixnum ")
828 (write obj
:stream stream
:pretty nil
)
829 (write-char #\
} stream
)))
830 (assert (string= (write-to-string 92 :pretty t
)
832 (set-pprint-dispatch 'fixnum nil
)))
834 (with-test (:name
:readable-vector-circularity
)
835 (let ((x (make-array 1 :element-type
'base-char
:initial-contents
"x"))
836 (y (make-array 1 :element-type
'base-char
:initial-contents
"y")))
837 (assert (equal (read-from-string (write-to-string (list x y
) :readably t
:circle t
))
840 (with-test (:name
:format-tilde-at-newline
)
841 (let ((control "hi.~@
843 (declare (notinline format
))
844 (assert (string= (format nil control
) "hi.
847 (with-test (:name
:sharp-s-respect-io-syntax
)
848 (let ((s (sb-c::make-definition-source-location
)))
849 (let ((str (write-to-string s
:escape t
:pretty t
)))
850 (assert (eql (search "#S(SB-C:DEFINITION-SOURCE-LOCATION" str
) 0)))
851 (let ((str (write-to-string s
:escape t
:pretty nil
)))
852 (assert (eql (search "#S(SB-C:DEFINITION-SOURCE-LOCATION" str
) 0)))
853 (let ((str (write-to-string s
:escape nil
:pretty t
)))
854 (assert (eql (search "#S(DEFINITION-SOURCE-LOCATION" str
) 0)))
855 (let ((str (write-to-string s
:escape nil
:pretty nil
)))
856 (assert (eql (search "#S(DEFINITION-SOURCE-LOCATION" str
) 0)))))
858 (with-test (:name
:print-layoutless-instance
)
859 (let ((x (sb-kernel:%make-instance
5)))
860 (let ((str (write-to-string x
:pretty nil
)))
861 (assert (search "#<SB-KERNEL:INSTANCE {" str
)))
862 ;; pretty goes through the non-pretty function unless you've
863 ;; modified the pprint-dispatch table, in which case this crashes.
864 ;; The unmodified dispatch table never attempts to handle a subtype of INSTANCE.
865 (let ((str (with-standard-io-syntax
866 (write-to-string x
:pretty t
:readably nil
))))
867 (assert (search "#<SB-KERNEL:INSTANCE {" str
))))
868 (let ((x (test-util::make-funcallable-instance
5)))
869 (let ((str (write-to-string x
:pretty nil
)))
870 (assert (search "#<SB-KERNEL:FUNCALLABLE-INSTANCE {" str
)))))
872 (with-test (:name
:functionless-funcallable-instance
)
873 (let ((x (sb-pcl::%make-standard-funcallable-instance
875 #-executable-funinstances
#xdead
)))
876 (assert (search "#<SB-PCL::STANDARD-FUNCALLABLE-INSTANCE"
877 (write-to-string x
:pretty nil
))))) ; should not crash
879 (with-test (:name
:bug-885320
)
880 (let* ((form `(format nil
"~E"))
881 (fun (compile nil
`(lambda (x) (,@form x
)))))
882 (assert (string= (funcall fun
1.0) "1.0e+0"))
883 (assert (string= (funcall fun
1.0d0
) "1.0d+0"))
884 (assert (string= (apply (car form
) (append (cdr form
) (list 1.0))) "1.0e+0"))
885 (assert (string= (apply (car form
) (append (cdr form
) (list 1.0d0
))) "1.0d+0"))))
887 (with-test (:name
(:bug-885320
:d-k
+1))
888 (let* ((form `(format nil
"~,2,,3E"))
889 (fun (compile nil
`(lambda (x) (,@form x
)))))
890 (assert (string= (funcall fun
1.0) "100.e-2"))
891 (assert (string= (funcall fun
1.0d0
) "100.d-2"))
892 (assert (string= (apply (car form
) (append (cdr form
) (list 1.0))) "100.e-2"))
893 (assert (string= (apply (car form
) (append (cdr form
) (list 1.0d0
))) "100.d-2"))))