1 ;;;; format.test --- test suite for Guile's CL-ish format -*- scheme -*-
2 ;;;; Matthias Koeppe <mkoeppe@mail.math.uni-magdeburg.de> --- June 2001
4 ;;;; Copyright (C) 2001, 2003, 2004, 2006, 2010, 2011, 2012,
5 ;;;; 2014 Free Software Foundation, Inc.
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.
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.
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
21 (define-module (test-format)
22 #:use-module (test-suite lib)
23 #:use-module (ice-9 i18n)
24 #:use-module (ice-9 format))
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")))
35 (set-current-output-port new)
38 (simple-format #t "hello, closed port!")
41 (set-current-output-port old))))))
43 ;;; FORMAT Basic Output
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)))))))
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"
58 (with-output-to-string
62 (format #f "~&") ; shall have no effect
65 (pass-if "format ~F (format-out-substr) maintains the column correctly"
66 (= (string-length (format #f "~@F~20T" 1)) 20)))
72 (with-test-prefix "format"
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")))
85 (with-test-prefix "~d decimal integer"
87 (with-test-prefix "~@d"
90 (string=? (format #f "~@d" -1) "-1"))
92 ;; in guile 1.6.4 and earlier, ~@d gave "0" but we think "+0" is what the
93 ;; common lisp spec intendes
95 (string=? (format #f "~@d" 0) "+0"))
98 (string=? (format #f "~@d" 1) "+1"))))
104 (with-test-prefix "~f fixed-point"
107 (string=? "1.5" (format #f "~f" 1.5)))
110 (string=? "1.5" (format #f "~f" 3/2)))
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"))))
121 (when (defined? 'setlocale)
122 (setlocale LC_ALL "C"))
124 (with-test-prefix "~h localized number"
127 (string=? (format #f "~h" 1234.5) "1234.5"))
130 (string=? (format #f "~6h" 123.2) " 123.2"))
133 (string=? (format #f "~8,,'*h" 123.2) "***123.2"))
136 (string=? (format #f "~,2h" 123.4567)
140 (string=? (format #f "~,3:h, ~a" 1234.5678
141 %global-locale "approximately")
142 "1234.567, approximately")))
148 (with-test-prefix "~{ iteration"
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)))