| 1 | ;;;; format.test --- test suite for Guile's CL-ish format -*- scheme -*- |
| 2 | ;;;; Matthias Koeppe <mkoeppe@mail.math.uni-magdeburg.de> --- June 2001 |
| 3 | ;;;; |
| 4 | ;;;; Copyright (C) 2001, 2003, 2004, 2006, 2010, 2011, 2012, |
| 5 | ;;;; 2014 Free Software Foundation, Inc. |
| 6 | ;;;; |
| 7 | ;;;; This library is free software; you can redistribute it and/or |
| 8 | ;;;; modify it under the terms of the GNU Lesser General Public |
| 9 | ;;;; License as published by the Free Software Foundation; either |
| 10 | ;;;; version 3 of the License, or (at your option) any later version. |
| 11 | ;;;; |
| 12 | ;;;; This library is distributed in the hope that it will be useful, |
| 13 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | ;;;; Lesser General Public License for more details. |
| 16 | ;;;; |
| 17 | ;;;; You should have received a copy of the GNU Lesser General Public |
| 18 | ;;;; License along with this library; if not, write to the Free Software |
| 19 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | |
| 21 | (define-module (test-format) |
| 22 | #:use-module (test-suite lib) |
| 23 | #:use-module (ice-9 i18n) |
| 24 | #:use-module (ice-9 format)) |
| 25 | |
| 26 | |
| 27 | (with-test-prefix "simple-format" |
| 28 | (pass-if-exception "current-output-port is closed" |
| 29 | exception:wrong-type-arg |
| 30 | ;; This used to segfault in Guile <= 2.0.10. |
| 31 | (let ((old (current-output-port)) |
| 32 | (new (%make-void-port "w"))) |
| 33 | (dynamic-wind |
| 34 | (lambda () |
| 35 | (set-current-output-port new) |
| 36 | (close-port new)) |
| 37 | (lambda () |
| 38 | (simple-format #t "hello, closed port!") |
| 39 | #t) |
| 40 | (lambda () |
| 41 | (set-current-output-port old)))))) |
| 42 | |
| 43 | ;;; FORMAT Basic Output |
| 44 | |
| 45 | (with-test-prefix "format basic output" |
| 46 | (pass-if "default to Unicode-capable port" |
| 47 | ;; `(format #f ...)' should be able to deal with Unicode characters. |
| 48 | (with-fluids ((%default-port-encoding "ISO-8859-1")) |
| 49 | (let ((alpha (integer->char #x03b1))) ; GREEK SMALL LETTER ALPHA |
| 50 | (= 1 (string-length (format #f (string alpha))))))) |
| 51 | |
| 52 | (pass-if "format ~% produces a new line" |
| 53 | (string=? (format #f "~%") "\n")) |
| 54 | (pass-if "format ~& starts a fresh line" |
| 55 | (string=? (format #f "~&abc~&~&") "abc\n")) |
| 56 | (pass-if "format ~& is stateless but works properly across outputs via port-column" |
| 57 | (string=? |
| 58 | (with-output-to-string |
| 59 | (lambda () |
| 60 | (display "xyz") |
| 61 | (format #t "~&abc") |
| 62 | (format #f "~&") ; shall have no effect |
| 63 | (format #t "~&~&"))) |
| 64 | "xyz\nabc\n")) |
| 65 | (pass-if "format ~F (format-out-substr) maintains the column correctly" |
| 66 | (= (string-length (format #f "~@F~20T" 1)) 20))) |
| 67 | |
| 68 | ;;; |
| 69 | ;;; misc |
| 70 | ;;; |
| 71 | |
| 72 | (with-test-prefix "format" |
| 73 | |
| 74 | ;; in guile 1.6.4 and earlier, excess arguments were an error, but this |
| 75 | ;; changed to follow the common lisp spec |
| 76 | (pass-if "excess arguments ignored A" |
| 77 | (string=? (format #f "" 1 2 3 4) "")) |
| 78 | (pass-if "excess arguments ignored B" |
| 79 | (string=? (format #f "~a ~a" 1 2 3 4) "1 2"))) |
| 80 | |
| 81 | ;;; |
| 82 | ;;; ~d |
| 83 | ;;; |
| 84 | |
| 85 | (with-test-prefix "~d decimal integer" |
| 86 | |
| 87 | (with-test-prefix "~@d" |
| 88 | |
| 89 | (pass-if "-1" |
| 90 | (string=? (format #f "~@d" -1) "-1")) |
| 91 | |
| 92 | ;; in guile 1.6.4 and earlier, ~@d gave "0" but we think "+0" is what the |
| 93 | ;; common lisp spec intendes |
| 94 | (pass-if "+0" |
| 95 | (string=? (format #f "~@d" 0) "+0")) |
| 96 | |
| 97 | (pass-if "+1" |
| 98 | (string=? (format #f "~@d" 1) "+1")))) |
| 99 | |
| 100 | ;;; |
| 101 | ;;; ~f |
| 102 | ;;; |
| 103 | |
| 104 | (with-test-prefix "~f fixed-point" |
| 105 | |
| 106 | (pass-if "1.5" |
| 107 | (string=? "1.5" (format #f "~f" 1.5))) |
| 108 | |
| 109 | (pass-if "3/2" |
| 110 | (string=? "1.5" (format #f "~f" 3/2))) |
| 111 | |
| 112 | ;; in guile prior to 1.6.9 and 1.8.1, leading zeros were incorrectly |
| 113 | ;; stripped, moving the decimal point and giving "25.0" here |
| 114 | (pass-if "string 02.5" |
| 115 | (string=? "2.5" (format #f "~f" "02.5")))) |
| 116 | |
| 117 | ;;; |
| 118 | ;;; ~h |
| 119 | ;;; |
| 120 | |
| 121 | (when (defined? 'setlocale) |
| 122 | (setlocale LC_ALL "C")) |
| 123 | |
| 124 | (with-test-prefix "~h localized number" |
| 125 | |
| 126 | (pass-if "1234.5" |
| 127 | (string=? (format #f "~h" 1234.5) "1234.5")) |
| 128 | |
| 129 | (pass-if "padding" |
| 130 | (string=? (format #f "~6h" 123.2) " 123.2")) |
| 131 | |
| 132 | (pass-if "padchar" |
| 133 | (string=? (format #f "~8,,'*h" 123.2) "***123.2")) |
| 134 | |
| 135 | (pass-if "decimals" |
| 136 | (string=? (format #f "~,2h" 123.4567) |
| 137 | "123.45")) |
| 138 | |
| 139 | (pass-if "locale" |
| 140 | (string=? (format #f "~,3:h, ~a" 1234.5678 |
| 141 | %global-locale "approximately") |
| 142 | "1234.567, approximately"))) |
| 143 | |
| 144 | ;;; |
| 145 | ;;; ~{ |
| 146 | ;;; |
| 147 | |
| 148 | (with-test-prefix "~{ iteration" |
| 149 | |
| 150 | ;; In Guile 1.6.4 and earlier, the maximum iterations parameter defaulted |
| 151 | ;; to 100, but it's now like Common Lisp where the default is no limit |
| 152 | (pass-if "no arbitrary iteration limit" |
| 153 | (= (string-length (format #f "~{~a~}" (make-list 200 #\b))) 200))) |