0.8.16.36: fixed #350
[sbcl.git] / tests / print.impure.lisp
index 9a3a2c3..9d9fd62 100644 (file)
                  (write #2a((t t) (nil nil)) :stream s))))
     (assert (equalp (read-from-string output) #2a((t t) (nil nil))))))
 
+;;; NIL parameters to "interpreted" FORMAT directives
+(assert (string= (format nil "~v%" nil) (string #\Newline)))
+
+;;; PRINC-TO-STRING should bind print-readably
+(let ((*print-readably* t))
+  (assert (string= (princ-to-string #\7)
+                  (write-to-string #\7 :escape nil :readably nil))))
+
+;;; in FORMAT, ~^ inside ~:{ should go to the next case, not break
+;;; iteration, even if one argument is just a one-element list.
+(assert (string= (format nil "~:{~A~^~}" '((A) (C D))) "AC"))
+
+;;; errors should be raised if pprint and justification are mixed
+;;; injudiciously...
+(dolist (x (list "~<~:;~>~_" "~<~:;~>~I" "~<~:;~>~W"
+                 "~<~:;~>~:T" "~<~:;~>~<~:>" "~_~<~:;~>"
+                 "~I~<~:;~>" "~W~<~:;~>" "~:T~<~:;~>" "~<~:>~<~:;~>"))
+  (assert (raises-error? (format nil x nil)))
+  (assert (raises-error? (format nil (eval `(formatter ,x)) nil))))
+;;; ...but not in judicious cases.
+(dolist (x (list "~<~;~>~_" "~<~;~>~I" "~<~;~>~W"
+                 "~<~;~>~:T" "~<~;~>~<~>" "~_~<~;~>"
+                 "~I~<~;~>" "~W~<~;~>" "~:T~<~;~>" "~<~>~<~;~>"
+                 "~<~:;~>~T" "~T~<~:;~>"))
+  (assert (format nil x nil))
+  (assert (format nil (eval `(formatter ,x)) nil)))
+
+;;; bug 350: bignum printing so memory-hungry that heap runs out
+;;; -- just don't stall here forever on a slow box
+(handler-case
+    (with-timeout 10
+      (print (ash 1 1000000)))
+  (timeout ()
+    (print 'timeout!)))
+
+;;; a spot of random-testing for rational printing
+(defvar *seed-state* (make-random-state))
+(prin1 *seed-state*) ; so that we can reproduce errors
+(let ((seed (make-random-state *seed-state*)))
+  (loop repeat 42
+     do (let ((n (random (ash 1 1000) seed))
+              (d (random (ash 1 1000) seed)))
+          (when (zerop (random 2 seed))
+            (setf n (- n)))
+          (let ((r (/ n d)))
+            (loop for base from 2 to 36
+               do (let ((*print-base* base)
+                        (*read-base* base)
+                        (*print-radix* nil))
+                    (assert (= r (read-from-string (prin1-to-string r))))
+                    (if (= 36 base)
+                        (decf *read-base*)
+                        (incf *read-base*))
+                    (assert (not (eql r (read-from-string (prin1-to-string r)))))
+                    (let ((*print-radix* t))
+                      (assert (= r (read-from-string 
+                                    (princ-to-string r)))))))))
+       (write-char #\.)
+       (finish-output)))
 ;;; success
 (quit :unix-status 104)