X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=tests%2Fprint.impure.lisp;h=54c8c9b0818c722aa0753f336612d819759ae37a;hb=f3491f128307938cc56367f739b8fbf9e5d503b6;hp=a54996ede2e731a91c296d860e747787f373f319;hpb=9faac297a083fbc59011e54ca6ccdc8c611e7ffb;p=sbcl.git diff --git a/tests/print.impure.lisp b/tests/print.impure.lisp index a54996e..54c8c9b 100644 --- a/tests/print.impure.lisp +++ b/tests/print.impure.lisp @@ -322,7 +322,7 @@ ;;; CSR inserted a bug into Burger & Dybvig's float printer. Caught ;;; by Raymond Toy -(assert (string= (format nil "~E" 1d23) "1.0d+23")) +(assert (string= (format nil "~E" 1d23) "1.d+23")) ;;; Fixed-format bugs from CLISP's test suite (reported by Bruno ;;; Haible, bug 317) @@ -337,6 +337,41 @@ ;;; Adam Warner's test case (assert (string= (format nil "~@F" 1.23) "+1.23")) + +;;; New (2005-11-08, also known as CSR House day) float format test +;;; cases. Simon Alexander, Raymond Toy, and others +(assert (string= (format nil "~9,4,,-7E" pi) ".00000003d+8")) +(assert (string= (format nil "~9,4,,-5E" pi) ".000003d+6")) +(assert (string= (format nil "~5,4,,7E" pi) "3141600.d-6")) +(assert (string= (format nil "~11,4,,3E" pi) " 314.16d-2")) +(assert (string= (format nil "~11,4,,5E" pi) " 31416.d-4")) +(assert (string= (format nil "~11,4,,0E" pi) " 0.3142d+1")) +(assert (string= (format nil "~9,,,-1E" pi) ".03142d+2")) +(assert (string= (format nil "~,,,-2E" pi) "0.003141592653589793d+3")) +(assert (string= (format nil "~,,,2E" pi) "31.41592653589793d-1")) +(assert (string= (format nil "~E" pi) "3.141592653589793d+0")) +(assert (string= (format nil "~9,5,,-1E" pi) ".03142d+2")) +(assert (string= (format nil "~11,5,,-1E" pi) " 0.03142d+2")) +(assert (string= (format nil "~G" pi) "3.141592653589793 ")) +(assert (string= (format nil "~9,5G" pi) "3.1416 ")) +(assert (string= (format nil "|~13,6,2,7E|" pi) "| 3141593.d-06|")) +(assert (string= (format nil "~9,3,2,0,'%E" pi) "0.314d+01")) +(assert (string= (format nil "~9,0,6f" pi) " 3141593.")) +(assert (string= (format nil "~6,2,1,'*F" pi) " 31.42")) +(assert (string= (format nil "~6,2,1,'*F" (* 100 pi)) "******")) +(assert (string= (format nil "~9,3,2,-2,'%@E" pi) "+.003d+03")) +(assert (string= (format nil "~10,3,2,-2,'%@E" pi) "+0.003d+03")) +(assert (string= (format nil "~15,3,2,-2,'%,'=@E" pi) "=====+0.003d+03")) +(assert (string= (format nil "~9,3,2,-2,'%E" pi) "0.003d+03")) +(assert (string= (format nil "~8,3,2,-2,'%@E" pi) "%%%%%%%%")) + +(assert (string= (format nil "~g" 1e0) "1. ")) +(assert (string= (format nil "~g" 1.2d40) "12000000000000000000000000000000000000000. ")) + +(assert (string= (format nil "~e" 0) "0.0e+0")) +(assert (string= (format nil "~e" 0d0) "0.0d+0")) +(assert (string= (format nil "~9,,4e" 0d0) "0.0d+0000")) + (let ((table (make-hash-table))) (setf (gethash 1 table) t) (assert @@ -347,4 +382,196 @@ (prin1 table)))) print-not-readable))) +;; Test that we can print characters readably regardless of the external format +;; of the stream. + +(defun test-readable-character (character external-format) + (let ((file "print.impure.tmp")) + (unwind-protect + (progn + (with-open-file (stream file + :direction :output + :external-format external-format + :if-exists :supersede) + (write character :stream stream :readably t)) + (with-open-file (stream file + :direction :input + :external-format external-format + :if-does-not-exist :error) + (assert (char= (read stream) character)))) + (ignore-errors + (delete-file file))))) + +(with-test (:name (:print-readable :character :utf-8) :skipped-on '(not :sb-unicode)) + (test-readable-character (code-char #xfffe) :utf-8)) + +(with-test (:name (:print-readable :character :iso-8859-1) :skipped-on '(not :sb-unicode)) + (test-readable-character (code-char #xfffe) :iso-8859-1)) + +(assert (string= (eval '(format nil "~:C" #\a)) "a")) +(assert (string= (format nil (formatter "~:C") #\a) "a")) + +;;; This used to trigger an AVER instead. +(assert (raises-error? (eval '(formatter "~>")) sb-format:format-error)) +(assert (raises-error? (eval '(format t "~>")) sb-format:format-error)) + +;;; readably printing hash-tables, check for circularity +(let ((x (cons 1 2)) + (h (make-hash-table)) + (*print-readably* t) + (*print-circle* t) + (*read-eval* t)) + (setf (gethash x h) h) + (destructuring-bind (x2 . h2) (read-from-string (write-to-string (cons x h))) + (assert (equal x x2)) + (assert (eq h2 (gethash x2 h2))))) + +;;; an off-by-one error in the ~R format directive until 1.0.15.20 +;;; prevented printing cardinals and ordinals between (expt 10 63) and +;;; (1- (expt 10 66)) +(assert (string= (format nil "~R" (expt 10 63)) "one vigintillion")) +(assert (string= (format nil "~:R" (expt 10 63)) "one vigintillionth")) + +;;; too-clever cacheing for PRINT-OBJECT resulted in a bogus method +;;; for printing RESTART objects. Check also CONTROL-STACK-EXHAUSTED +;;; and HEAP-EXHAUSTED-ERROR. +(let ((result (with-output-to-string (*standard-output*) + (princ (find-restart 'abort))))) + (assert (string/= result "#<" :end1 2))) +(let ((result (with-output-to-string (*standard-output*) + (princ (make-condition 'sb-kernel::control-stack-exhausted))))) + (assert (string/= result "#<" :end1 2))) +(let ((result (with-output-to-string (*standard-output*) + (princ (make-condition 'sb-kernel::heap-exhausted-error))))) + (assert (string/= result "#<" :end1 2))) + +(with-test (:name (:with-standard-io-syntax :bind-print-pprint-dispatch)) + (let ((*print-pprint-dispatch* (copy-pprint-dispatch nil))) + (set-pprint-dispatch 'symbol #'(lambda (stream obj) + (declare (ignore obj)) + (write-string "FOO" stream))) + (with-standard-io-syntax + (let ((*print-pretty* t)) + (assert (string= (princ-to-string 'bar) "BAR")))))) + +;;; bug-lp#488979 + +(defclass a-class-name () ()) + +(assert (find #\Newline + (let ((*print-pretty* t) + (*print-right-margin* 10)) + (format nil "~A" (make-instance 'a-class-name))) + :test #'char=)) + +(assert (not (find #\Newline + (let ((*print-pretty* nil) + (*print-right-margin* 10)) + (format nil "~A" (make-instance 'a-class-name))) + :test #'char=))) + +;;; The PRINT-OBJECT method for RANDOM-STATE used to have a bogus +;;; dimension argument for MAKE-ARRAY. +(with-test (:name :print-random-state) + (assert (equalp *random-state* + (read-from-string + (write-to-string *random-state*))))) + +(with-test (:name :write-return-value) + (assert (= 123 (funcall (compile nil (lambda () + (write 123))))))) + +(with-test (:name :write/write-to-string-compiler-macro-lp/598374+581564) + (let ((test (compile nil + `(lambda (object &optional output-stream) + (write object + :stream output-stream))))) + (assert (equal "(HELLO WORLD)" + (with-output-to-string (*standard-output*) + (let ((list '(hello world))) + (assert (eq list (funcall test list))))))) + (assert (equal "12" + (with-output-to-string (*standard-output*) + (assert (eql 12 (funcall test 12))))))) + (let ((test (compile nil + `(lambda () + (let ((*print-length* 42)) + (write-to-string *print-length* :length nil)))))) + (assert (equal "42" (funcall test))))) + +(with-test (:name (:format :compile-literal-dest-string)) + (assert (eq :warned + (handler-case + (compile nil + `(lambda (x) (format "~A" x))) + ((and warning (not style-warning)) () + :warned))))) + +(with-test (:name :bug-308961) + (assert (string= (format nil "~4,1F" 0.001) " 0.0")) + (assert (string= (format nil "~4,1@F" 0.001) "+0.0")) + (assert (string= (format nil "~E" 0.01) "1.e-2")) + (assert (string= (format nil "~G" 0.01) "1.00e-2"))) + +(with-test (:name (:fp-read/print-consistency single-float)) + (let ((*random-state* (make-random-state t)) + (oops)) + (loop for f = most-positive-single-float then (/ f 2.0) + while (> f 0.0) + do (loop repeat 10 + for fr = (random f) + do (unless (eql fr (read-from-string (prin1-to-string fr))) + (push fr oops) + (return)))) + (loop for f = most-negative-single-float then (/ f 2.0) + while (< f -0.0) + do (loop repeat 10 + for fr = (- (random (- f))) + do (unless (eql fr (read-from-string (prin1-to-string fr))) + (push fr oops) + (return)))) + (when oops + (error "FP read/print inconsistencies:~%~:{ ~S => ~S~%~}" + (mapcar (lambda (f) + (list f (read-from-string (prin1-to-string f)))) + oops))))) + +(with-test (:name (:fp-read/print-consistency double-float)) + (let ((*random-state* (make-random-state t)) + (oops)) + ;; FIXME skipping denormalized floats due to bug 793774. + (loop for f = most-positive-double-float then (/ f 2d0) + while (> f 0d0) + do (loop repeat 10 + for fr = (random f) + do (unless (float-denormalized-p fr) + (unless (eql fr (read-from-string (prin1-to-string fr))) + (push fr oops) + (return))))) + (loop for f = most-negative-double-float then (/ f 2d0) + while (< f -0d0) + do (loop repeat 10 + for fr = (- (random (- f))) + do (unless (float-denormalized-p fr) + (unless (eql fr (read-from-string (prin1-to-string fr))) + (push fr oops) + (return))))) + (when oops + (error "FP read/print inconsistencies:~%~:{ ~S => ~S~%~}" + (mapcar (lambda (f) + (list f (read-from-string (prin1-to-string f)))) + oops))))) + +(with-test (:name :bug-811386) + (assert (equal " 0.00" (format nil "~7,2,-2f" 0))) + (assert (equal " 0.00" (format nil "~7,2,2f" 0))) + (assert (equal " 0.01" (format nil "~7,2,-2f" 1))) + (assert (equal " 100.00" (format nil "~7,2,2f" 1))) + (assert (equal " 0.00" (format nil "~7,2,-2f" 0.1))) + (assert (equal " 10.00" (format nil "~7,2,2f" 0.1))) + (assert (equal " 0.01" (format nil "~7,2,-2f" 0.5)))) + +(with-test (:name :bug-867684) + (assert (equal "ab" (format nil "a~0&b")))) + ;;; success