format.test 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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, 2017 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. (define-module (test-format)
  21. #:use-module (test-suite lib)
  22. #:use-module (ice-9 i18n)
  23. #:use-module (ice-9 format))
  24. (with-test-prefix "simple-format"
  25. (pass-if-exception "current-output-port is closed"
  26. exception:wrong-type-arg
  27. ;; This used to segfault in Guile <= 2.0.10.
  28. (let ((old (current-output-port))
  29. (new (%make-void-port "w")))
  30. (dynamic-wind
  31. (lambda ()
  32. (set-current-output-port new)
  33. (close-port new))
  34. (lambda ()
  35. (simple-format #t "hello, closed port!")
  36. #t)
  37. (lambda ()
  38. (set-current-output-port old))))))
  39. ;;; FORMAT Basic Output
  40. (with-test-prefix "format basic output"
  41. (pass-if "default to Unicode-capable port"
  42. ;; `(format #f ...)' should be able to deal with Unicode characters.
  43. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  44. (let ((alpha (integer->char #x03b1))) ; GREEK SMALL LETTER ALPHA
  45. (= 1 (string-length (format #f (string alpha)))))))
  46. (pass-if "format ~% produces a new line"
  47. (string=? (format #f "~%") "\n"))
  48. (pass-if "format ~& starts a fresh line"
  49. (string=? (format #f "~&abc~&~&") "abc\n"))
  50. (pass-if "format ~& is stateless but works properly across outputs via port-column"
  51. (string=?
  52. (with-output-to-string
  53. (lambda ()
  54. (display "xyz")
  55. (format #t "~&abc")
  56. (format #f "~&") ; shall have no effect
  57. (format #t "~&~&")))
  58. "xyz\nabc\n"))
  59. (pass-if "format ~F (format-out-substr) maintains the column correctly"
  60. (= (string-length (format #f "~@F~20T" 1)) 20)))
  61. ;;;
  62. ;;; misc
  63. ;;;
  64. (with-test-prefix "format"
  65. ;; in guile 1.6.4 and earlier, excess arguments were an error, but this
  66. ;; changed to follow the common lisp spec
  67. (pass-if "excess arguments ignored A"
  68. (string=? (format #f "" 1 2 3 4) ""))
  69. (pass-if "excess arguments ignored B"
  70. (string=? (format #f "~a ~a" 1 2 3 4) "1 2")))
  71. ;;;
  72. ;;; ~d
  73. ;;;
  74. (with-test-prefix "~d decimal integer"
  75. (with-test-prefix "~@d"
  76. (pass-if "-1"
  77. (string=? (format #f "~@d" -1) "-1"))
  78. ;; in guile 1.6.4 and earlier, ~@d gave "0" but we think "+0" is what the
  79. ;; common lisp spec intendes
  80. (pass-if "+0"
  81. (string=? (format #f "~@d" 0) "+0"))
  82. (pass-if "+1"
  83. (string=? (format #f "~@d" 1) "+1"))))
  84. ;;;
  85. ;;; ~f
  86. ;;;
  87. (with-test-prefix "~f fixed-point"
  88. (pass-if "1.5"
  89. (string=? "1.5" (format #f "~f" 1.5)))
  90. (pass-if "3/2"
  91. (string=? "1.5" (format #f "~f" 3/2)))
  92. (pass-if "~2f"
  93. (string=? "10." (format #f "~2f" 9.9)))
  94. (pass-if "~2,1f"
  95. (string=? "9.9" (format #f "~2,1f" 9.9)))
  96. (pass-if "~2,2f"
  97. (string=? "9.90" (format #f "~2,2f" 9.9)))
  98. ;; in guile prior to 1.6.9 and 1.8.1, leading zeros were incorrectly
  99. ;; stripped, moving the decimal point and giving "25.0" here
  100. (pass-if "string 02.5"
  101. (string=? "2.5" (format #f "~f" "02.5"))))
  102. ;;;
  103. ;;; ~h
  104. ;;;
  105. (when (defined? 'setlocale)
  106. (setlocale LC_ALL "C"))
  107. (with-test-prefix "~h localized number"
  108. (pass-if "1234.5"
  109. (string=? (format #f "~h" 1234.5) "1234.5"))
  110. (pass-if "padding"
  111. (string=? (format #f "~6h" 123.2) " 123.2"))
  112. (pass-if "padchar"
  113. (string=? (format #f "~8,,'*h" 123.2) "***123.2"))
  114. (pass-if "decimals"
  115. (string=? (format #f "~,2h" 123.4567)
  116. "123.46"))
  117. (pass-if "locale"
  118. (string=? (format #f "~,3:h, ~a" 1234.5678
  119. %global-locale "approximately")
  120. "1234.568, approximately")))
  121. ;;;
  122. ;;; ~{
  123. ;;;
  124. (with-test-prefix "~{ iteration"
  125. ;; In Guile 1.6.4 and earlier, the maximum iterations parameter defaulted
  126. ;; to 100, but it's now like Common Lisp where the default is no limit
  127. (pass-if "no arbitrary iteration limit"
  128. (= (string-length (format #f "~{~a~}" (make-list 200 #\b))) 200)))