format.test 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (test-format)
  20. #:use-module (test-suite lib)
  21. #:use-module (ice-9 i18n)
  22. #:use-module (ice-9 format))
  23. ;;; FORMAT Basic Output
  24. (with-test-prefix "format basic output"
  25. (pass-if "default to Unicode-capable port"
  26. ;; `(format #f ...)' should be able to deal with Unicode characters.
  27. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  28. (let ((alpha (integer->char #x03b1))) ; GREEK SMALL LETTER ALPHA
  29. (= 1 (string-length (format #f (string alpha)))))))
  30. (pass-if "format ~% produces a new line"
  31. (string=? (format #f "~%") "\n"))
  32. (pass-if "format ~& starts a fresh line"
  33. (string=? (format #f "~&abc~&~&") "abc\n"))
  34. (pass-if "format ~& is stateless but works properly across outputs via port-column"
  35. (string=?
  36. (with-output-to-string
  37. (lambda ()
  38. (display "xyz")
  39. (format #t "~&abc")
  40. (format #f "~&") ; shall have no effect
  41. (format #t "~&~&")))
  42. "xyz\nabc\n"))
  43. (pass-if "format ~F (format-out-substr) maintains the column correctly"
  44. (= (string-length (format #f "~@F~20T" 1)) 20)))
  45. ;;;
  46. ;;; misc
  47. ;;;
  48. (with-test-prefix "format"
  49. ;; in guile 1.6.4 and earlier, excess arguments were an error, but this
  50. ;; changed to follow the common lisp spec
  51. (pass-if "excess arguments ignored A"
  52. (string=? (format #f "" 1 2 3 4) ""))
  53. (pass-if "excess arguments ignored B"
  54. (string=? (format #f "~a ~a" 1 2 3 4) "1 2")))
  55. ;;;
  56. ;;; ~d
  57. ;;;
  58. (with-test-prefix "~d decimal integer"
  59. (with-test-prefix "~@d"
  60. (pass-if "-1"
  61. (string=? (format #f "~@d" -1) "-1"))
  62. ;; in guile 1.6.4 and earlier, ~@d gave "0" but we think "+0" is what the
  63. ;; common lisp spec intendes
  64. (pass-if "+0"
  65. (string=? (format #f "~@d" 0) "+0"))
  66. (pass-if "+1"
  67. (string=? (format #f "~@d" 1) "+1"))))
  68. ;;;
  69. ;;; ~f
  70. ;;;
  71. (with-test-prefix "~f fixed-point"
  72. (pass-if "1.5"
  73. (string=? "1.5" (format #f "~f" 1.5)))
  74. (pass-if "3/2"
  75. (string=? "1.5" (format #f "~f" 3/2)))
  76. ;; in guile prior to 1.6.9 and 1.8.1, leading zeros were incorrectly
  77. ;; stripped, moving the decimal point and giving "25.0" here
  78. (pass-if "string 02.5"
  79. (string=? "2.5" (format #f "~f" "02.5"))))
  80. ;;;
  81. ;;; ~h
  82. ;;;
  83. (setlocale LC_ALL "C")
  84. (with-test-prefix "~h localized number"
  85. (pass-if "1234.5"
  86. (string=? (format #f "~h" 1234.5) "1234.5"))
  87. (pass-if "padding"
  88. (string=? (format #f "~6h" 123.2) " 123.2"))
  89. (pass-if "padchar"
  90. (string=? (format #f "~8,,'*h" 123.2) "***123.2"))
  91. (pass-if "decimals"
  92. (string=? (format #f "~,2h" 123.4567)
  93. "123.45"))
  94. (pass-if "locale"
  95. (string=? (format #f "~,3:h, ~a" 1234.5678
  96. %global-locale "approximately")
  97. "1234.567, approximately")))
  98. ;;;
  99. ;;; ~{
  100. ;;;
  101. (with-test-prefix "~{ iteration"
  102. ;; In Guile 1.6.4 and earlier, the maximum iterations parameter defaulted
  103. ;; to 100, but it's now like Common Lisp where the default is no limit
  104. (pass-if "no arbitrary iteration limit"
  105. (= (string-length (format #f "~{~a~}" (make-list 200 #\b))) 200)))