formatst.scm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. ;; "formatst.scm" SLIB FORMAT Version 3.0 conformance test
  2. ; Written by Dirk Lutzebaeck (lutzeb@cs.tu-berlin.de)
  3. ; Modified for Kawa by Per Bothner <per@bothner.com>.
  4. ;
  5. ; This code is in the public domain.
  6. ;; Test run: (slib:load "formatst")
  7. ; Failure reports for various scheme interpreters:
  8. ;
  9. ; SCM4d
  10. ; None.
  11. ; Elk 2.2:
  12. ; None.
  13. ; MIT C-Scheme 7.1:
  14. ; The empty list is always evaluated as a boolean and consequently
  15. ; represented as `#f'.
  16. ; Scheme->C 01nov91:
  17. ; None, if format:symbol-case-conv and format:iobj-case-conv are set
  18. ; to string-downcase.
  19. ;(require 'format)
  20. ;(if (not (string=? format:version "3.0"))
  21. ; (begin
  22. ; (display "You have format version ")
  23. ; (display format:version)
  24. ; (display ". This test is for format version 3.0!")
  25. ; (newline)
  26. ; (format:abort)))
  27. (test-begin "format" 432)
  28. (define-syntax test
  29. (syntax-rules ()
  30. ((test format-args out-str)
  31. (test-equal out-str (apply format #f format-args)))))
  32. (define slib:tab (integer->char 9))
  33. (define slib:form-feed (integer->char 12))
  34. (define format:symbol-case-conv #f)
  35. (define format:floats #t)
  36. (define format:complex-numbers #f)
  37. (define format:iobj-case-conv #f)
  38. ;; As format:symbol-case-conv but applies for the representation of
  39. ;; implementation internal objects.
  40. ;; format:iobj->str reveals the implementation dependent representation of
  41. ;; #<...> objects with the use of display and call-with-output-string.
  42. (define format:read-proof #f)
  43. (define (format:iobj->str iobj)
  44. (if format:iobj-case-conv
  45. (string-append
  46. (if format:iobj-case-conv
  47. (format:iobj-case-conv
  48. (call-with-output-string (lambda (p) (display iobj p))))
  49. (call-with-output-string (lambda (p) (display iobj p)))))
  50. (call-with-output-string (lambda (p) (display iobj p)))))
  51. ; ensure format default configuration
  52. ;(set! format:symbol-case-conv #f)
  53. ;(set! format:iobj-case-conv #f)
  54. ;; Slib specific: (format #t "~q")
  55. ;; Kawa does have complex numbers, but does not support the
  56. ;; non-standard ~i format specifier.
  57. ;;(format #t "This implementation has~@[ no~] flonums ~
  58. ;; ~:[but no~;and~] complex numbers~%"
  59. ;; (not format:floats) format:complex-numbers)
  60. ; any object test
  61. (test '("abc") "abc")
  62. (test '("~a" 10) "10")
  63. (test '("~a" -1.2) "-1.2")
  64. (test '("~a" a) "a")
  65. (test '("~a" #t) "#t")
  66. (test '("~a" #f) "#f")
  67. (test '("~a" "abc") "abc")
  68. ;;SLIB version: (test '("~a" '#(1 2 3)) "#(1 2 3)")
  69. (test '("~a" #(1 2 3)) "#(1 2 3)")
  70. (test '("~a" ()) "()")
  71. (test '("~a" (a)) "(a)")
  72. (test '("~a" (a b)) "(a b)")
  73. (test '("~a" (a (b c) d)) "(a (b c) d)")
  74. (test '("~a" (a . b)) "(a . b)")
  75. ;;SLIB version: (test '("~a" (a (b c . d))) "(a (b . (c . d)))") ; this is ugly
  76. (test '("~a" (a (b c . d))) "(a (b c . d))")
  77. (test `("~a" ,display) (format:iobj->str display))
  78. (test `("~a" ,(current-input-port)) (format:iobj->str (current-input-port)))
  79. (test `("~a" ,(current-output-port)) (format:iobj->str (current-output-port)))
  80. ; # argument test
  81. (test '("~a ~a" 10 20) "10 20")
  82. (test '("~a abc ~a def" 10 20) "10 abc 20 def")
  83. ; numerical test
  84. (test '("~d" 100) "100")
  85. (test '("~x" 100) "64")
  86. (test '("~o" 100) "144")
  87. (test '("~b" 100) "1100100")
  88. (test '("~@d" 100) "+100")
  89. (test '("~@d" -100) "-100")
  90. (test '("~@x" 100) "+64")
  91. (test '("~@o" 100) "+144")
  92. (test '("~@b" 100) "+1100100")
  93. (test '("~10d" 100) " 100")
  94. (test '("~:d" 123) "123")
  95. (test '("~:d" 1234) "1,234")
  96. (test '("~:d" 12345) "12,345")
  97. (test '("~:d" 123456) "123,456")
  98. (test '("~:d" 12345678) "12,345,678")
  99. (test '("~:d" -123) "-123")
  100. (test '("~:d" -1234) "-1,234")
  101. (test '("~:d" -12345) "-12,345")
  102. (test '("~:d" -123456) "-123,456")
  103. (test '("~:d" -12345678) "-12,345,678")
  104. (test '("~10:d" 1234) " 1,234")
  105. (test '("~10:d" -1234) " -1,234")
  106. (test '("~10,'*d" 100) "*******100")
  107. (test '("~10,,'|:d" 12345678) "12|345|678")
  108. (test '("~10,,,2:d" 12345678) "12,34,56,78")
  109. (test '("~14,'*,'|,4:@d" 12345678) "****+1234|5678")
  110. (test '("~10r" 100) "100")
  111. (test '("~2r" 100) "1100100")
  112. (test '("~8r" 100) "144")
  113. (test '("~16r" 100) "64")
  114. (test '("~16,10,'*r" 100) "********64")
  115. ; roman numeral test
  116. (test '("~@r" 4) "IV")
  117. (test '("~@r" 19) "XIX")
  118. (test '("~@r" 50) "L")
  119. (test '("~@r" 100) "C")
  120. (test '("~@r" 1000) "M")
  121. (test '("~@r" 99) "XCIX")
  122. (test '("~@r" 1994) "MCMXCIV")
  123. ; old roman numeral test
  124. (test '("~:@r" 4) "IIII")
  125. (test '("~:@r" 5) "V")
  126. (test '("~:@r" 10) "X")
  127. (test '("~:@r" 9) "VIIII")
  128. ; cardinal/ordinal English number test
  129. (test '("~r" 4) "four")
  130. (test '("~r" 10) "ten")
  131. (test '("~r" 19) "nineteen")
  132. (test '("~r" 1984) "one thousand, nine hundred eighty-four")
  133. (test '("~:r" -1984) "minus one thousand, nine hundred eighty-fourth")
  134. ; character test
  135. (test '("~c" #\a) "a")
  136. (test '("~@c" #\a) "#\\a")
  137. (test `("~@c" ,(integer->char 32)) "#\\space")
  138. (test `("~@c" ,(integer->char 0)) "#\\null")
  139. (test `("~@c" ,(integer->char 27)) "#\\escape")
  140. (test `("~@c" ,(integer->char 127)) "#\\delete")
  141. (test `("~@c" ,(integer->char 128)) "#\\x80")
  142. (test `("~@c" ,(integer->char 255)) "#\\xff")
  143. (test '("~65c") "A")
  144. (test '("~7@c") "#\\alarm")
  145. (test '("~:c" #\a) "a")
  146. (test `("~:c" ,(integer->char 1)) "^A")
  147. (test `("~:c" ,(integer->char 27)) "^[")
  148. (test '("~7:c") "^G")
  149. (test `("~:c" ,(integer->char 128)) "#\\x80")
  150. (test `("~:c" ,(integer->char 127)) "#\\x7f")
  151. (test `("~:c" ,(integer->char 255)) "#\\xff")
  152. ; plural test
  153. (test '("test~p" 1) "test")
  154. (test '("test~p" 2) "tests")
  155. (test '("test~p" 0) "tests")
  156. (test '("tr~@p" 1) "try")
  157. (test '("tr~@p" 2) "tries")
  158. (test '("tr~@p" 0) "tries")
  159. (test '("~a test~:p" 10) "10 tests")
  160. (test '("~a test~:p" 1) "1 test")
  161. ; tilde test
  162. (test '("~~~~") "~~")
  163. (test '("~3~") "~~~")
  164. ; whitespace character test
  165. (test '("~%") "
  166. ")
  167. (test '("~3%") "
  168. ")
  169. (test '("~&") "")
  170. (test '("abc~&") "abc
  171. ")
  172. (test '("abc~&def") "abc
  173. def")
  174. ;; SLIB incorrectly add an extra Newline.
  175. (test '("~&") "")
  176. (test '("~3&") "
  177. ")
  178. (test '("abc~3&") "abc
  179. ")
  180. (test '("~|") (string slib:form-feed))
  181. ;; SLIB specific, conflicts with CommonLisp: (test '("~_~_~_") " ")
  182. ;; SLIB specific, conflicts with CommonLisp: (test '("~3_") " ")
  183. ;; SLIB specific, conflicts with CommonLisp: (test '("~/") (string slib:tab))
  184. ;; SLIB specific: (test '("~3/") (make-string 3 slib:tab))
  185. ; tabulate test
  186. (test '("~0&~3t") " ")
  187. (test '("~0&~10t") " ")
  188. (test '("~10t") " ")
  189. (test '("~0&1234567890~,8tABC") "1234567890 ABC")
  190. (test '("~0&1234567890~0,8tABC") "1234567890 ABC")
  191. (test '("~0&1234567890~1,8tABC") "1234567890 ABC")
  192. (test '("~0&1234567890~2,8tABC") "1234567890 ABC")
  193. (test '("~0&1234567890~3,8tABC") "1234567890 ABC")
  194. (test '("~0&1234567890~4,8tABC") "1234567890 ABC")
  195. (test '("~0&1234567890~5,8tABC") "1234567890 ABC")
  196. (test '("~0&1234567890~6,8tABC") "1234567890 ABC")
  197. (test '("~0&1234567890~7,8tABC") "1234567890 ABC")
  198. (test '("~0&1234567890~8,8tABC") "1234567890 ABC")
  199. (test '("~0&1234567890~9,8tABC") "1234567890 ABC")
  200. (test '("~0&1234567890~10,8tABC") "1234567890 ABC")
  201. (test '("~0&1234567890~11,8tABC") "1234567890 ABC")
  202. (test '("~0&12345~,8tABCDE~,8tXYZ") "12345 ABCDE XYZ")
  203. (test '("~,8t+++~,8t===") " +++ ===")
  204. (test '("~0&ABC~,8,'.tDEF") "ABC......DEF")
  205. (test '("~0&~3,8@tABC") " ABC")
  206. (test '("~0&1234~3,8@tABC") "1234 ABC")
  207. (test '("~0&12~3,8@tABC~3,8@tDEF") "12 ABC DEF")
  208. ; indirection test
  209. (test '("~a ~? ~a" 10 "~a ~a" (20 30) 40) "10 20 30 40")
  210. (test '("~a ~@? ~a" 10 "~a ~a" 20 30 40) "10 20 30 40")
  211. ; field test
  212. (test '("~10a" "abc") "abc ")
  213. (test '("~10@a" "abc") " abc")
  214. (test '("~10a" "0123456789abc") "0123456789abc")
  215. (test '("~10@a" "0123456789abc") "0123456789abc")
  216. ; pad character test
  217. (test '("~10,,,'*a" "abc") "abc*******")
  218. (test '("~10,,,'Xa" "abc") "abcXXXXXXX")
  219. (test '("~10,,,42a" "abc") "abc*******")
  220. (test '("~10,,,'*@a" "abc") "*******abc")
  221. (test '("~10,,3,'*a" "abc") "abc*******")
  222. (test '("~10,,3,'*a" "0123456789abc") "0123456789abc***") ; min. padchar length
  223. (test '("~10,,3,'*@a" "0123456789abc") "***0123456789abc")
  224. ; colinc, minpad padding test
  225. (test '("~10,8,0,'*a" 123) "123********")
  226. (test '("~10,9,0,'*a" 123) "123*********")
  227. (test '("~10,10,0,'*a" 123) "123**********")
  228. (test '("~10,11,0,'*a" 123) "123***********")
  229. (test '("~8,1,0,'*a" 123) "123*****")
  230. (test '("~8,2,0,'*a" 123) "123******")
  231. (test '("~8,3,0,'*a" 123) "123******")
  232. (test '("~8,4,0,'*a" 123) "123********")
  233. (test '("~8,5,0,'*a" 123) "123*****")
  234. (test '("~8,1,3,'*a" 123) "123*****")
  235. (test '("~8,1,5,'*a" 123) "123*****")
  236. (test '("~8,1,6,'*a" 123) "123******")
  237. (test '("~8,1,9,'*a" 123) "123*********")
  238. ; slashify test
  239. (test '("~s" "abc") "\"abc\"")
  240. (test '("~s" "abc \\ abc") "\"abc \\\\ abc\"")
  241. (test '("~a" "abc \\ abc") "abc \\ abc")
  242. (test '("~s" "abc \" abc") "\"abc \\\" abc\"")
  243. (test '("~a" "abc \" abc") "abc \" abc")
  244. (test '("~s" #\space) "#\\space")
  245. (test '("~s" #\newline) "#\\newline")
  246. ;; SLIB has: (test '("~s" #\tab) "#\\ht")
  247. (test '("~s" #\tab) "#\\tab")
  248. (test '("~s" #\a) "#\\a")
  249. (test '("~a" (a "b" c)) "(a b c)")
  250. ; symbol case force test
  251. (define format:old-scc format:symbol-case-conv)
  252. (set! format:symbol-case-conv string-upcase)
  253. (test-expect-fail 1) ; format:symbol-case-conv not implemented
  254. (test '("~a" abc) "ABC")
  255. (set! format:symbol-case-conv string-downcase)
  256. (test '("~s" abc) "abc")
  257. (set! format:symbol-case-conv string-capitalize)
  258. (test-expect-fail 1) ; format:symbol-case-conv not implemented
  259. (test '("~s" abc) "Abc")
  260. (set! format:symbol-case-conv format:old-scc)
  261. ; read proof test
  262. (test `("~:s" ,display) (format:iobj->str display))
  263. (test `("~:a" ,display) (format:iobj->str display))
  264. (test `("~:a" (1 2 ,display))
  265. (string-append "(1 2 " (format:iobj->str display) ")"))
  266. (test '("~:a" "abc") "abc")
  267. ; internal object case type force test
  268. (set! format:iobj-case-conv string-upcase)
  269. (test-expect-fail 1) ; format:iobj-case-conv not implemented
  270. (test `("~a" ,display) (string-upcase (format:iobj->str display)))
  271. (set! format:iobj-case-conv string-downcase)
  272. (test `("~s" ,display) (string-downcase (format:iobj->str display)))
  273. (set! format:iobj-case-conv string-capitalize)
  274. (test-expect-fail 1) ; format:iobj-case-conv not implemented
  275. (test `("~s" ,display) (string-capitalize (format:iobj->str display)))
  276. (set! format:iobj-case-conv #f)
  277. ; continuation line test
  278. (test '("abc~
  279. 123") "abc123")
  280. (test '("abc~
  281. 123") "abc123")
  282. (test '("abc~
  283. ") "abc")
  284. (test '("abc~:
  285. def") "abc def")
  286. (test '("abc~@
  287. def")
  288. "abc
  289. def")
  290. ; flush output (can't test it here really)
  291. (test '("abc ~! xyz") "abc xyz")
  292. ; string case conversion
  293. (test '("~a ~(~a~) ~a" "abc" "HELLO WORLD" "xyz") "abc hello world xyz")
  294. (test '("~a ~:(~a~) ~a" "abc" "HELLO WORLD" "xyz") "abc Hello World xyz")
  295. (test '("~a ~@(~a~) ~a" "abc" "HELLO WORLD" "xyz") "abc Hello world xyz")
  296. (test '("~a ~:@(~a~) ~a" "abc" "hello world" "xyz") "abc HELLO WORLD xyz")
  297. (test '("~:@(~a~)" (a b c)) "(A B C)")
  298. (test '("~:@(~x~)" 255) "FF")
  299. (test '("~:@(~p~)" 2) "S")
  300. (test `("~:@(~a~)" ,display) (string-upcase (format:iobj->str display)))
  301. (test '("~:(~a ~a ~a~) ~a" "abc" "xyz" "123" "world") "Abc Xyz 123 world")
  302. ; variable parameter
  303. (test '("~va" 10 "abc") "abc ")
  304. (test '("~v,,,va" 10 42 "abc") "abc*******")
  305. ; number of remaining arguments as parameter
  306. (test '("~#,,,'*@a ~a ~a ~a" 1 1 1 1) "***1 1 1 1")
  307. ; argument jumping
  308. (test '("~a ~* ~a" 10 20 30) "10 30")
  309. (test '("~a ~2* ~a" 10 20 30 40) "10 40")
  310. (test '("~a ~:* ~a" 10) "10 10")
  311. (test '("~a ~a ~2:* ~a ~a" 10 20) "10 20 10 20")
  312. (test '("~a ~a ~@* ~a ~a" 10 20) "10 20 10 20")
  313. (test '("~a ~a ~4@* ~a ~a" 10 20 30 40 50 60) "10 20 50 60")
  314. ; conditionals
  315. (test '("~[abc~;xyz~]" 0) "abc")
  316. (test '("~[abc~;xyz~]" 1) "xyz")
  317. (test '("~[abc~;xyz~:;456~]" 99) "456")
  318. (test '("~0[abc~;xyz~:;456~]") "abc")
  319. (test '("~1[abc~;xyz~:;456~] ~a" 100) "xyz 100")
  320. (test '("~#[no arg~;~a~;~a and ~a~;~a, ~a and ~a~]") "no arg")
  321. (test '("~#[no arg~;~a~;~a and ~a~;~a, ~a and ~a~]" 10) "10")
  322. (test '("~#[no arg~;~a~;~a and ~a~;~a, ~a and ~a~]" 10 20) "10 and 20")
  323. (test '("~#[no arg~;~a~;~a and ~a~;~a, ~a and ~a~]" 10 20 30) "10, 20 and 30")
  324. (test '("~:[hello~;world~] ~a" #t 10) "world 10")
  325. (test '("~:[hello~;world~] ~a" #f 10) "hello 10")
  326. (test '("~@[~a tests~]" #f) "")
  327. (test '("~@[~a tests~]" 10) "10 tests")
  328. (test '("~@[~a test~:p~] ~a" 10 done) "10 tests done")
  329. (test '("~@[~a test~:p~] ~a" 1 done) "1 test done")
  330. (test '("~@[~a test~:p~] ~a" 0 done) "0 tests done")
  331. (test '("~@[~a test~:p~] ~a" #f done) " done")
  332. (test '("~@[ level = ~d~]~@[ length = ~d~]" #f 5) " length = 5")
  333. (test '("~[abc~;~[4~;5~;6~]~;xyz~]" 0) "abc") ; nested conditionals (irrghh)
  334. (test '("~[abc~;~[4~;5~;6~]~;xyz~]" 2) "xyz")
  335. (test '("~[abc~;~[4~;5~;6~]~;xyz~]" 1 2) "6")
  336. ; iteration
  337. (test '("~{ ~a ~}" (a b c)) " a b c ")
  338. (test '("~{ ~a ~}" ()) "")
  339. (test '("~{ ~a ~5,,,'*a~}" (a b c d)) " a b**** c d****")
  340. (test '("~{ ~a,~a ~}" (a 1 b 2 c 3)) " a,1 b,2 c,3 ")
  341. (test '("~2{ ~a,~a ~}" (a 1 b 2 c 3)) " a,1 b,2 ")
  342. (test '("~3{~a ~} ~a" (a b c d e) 100) "a b c 100")
  343. (test '("~0{~a ~} ~a" (a b c d e) 100) " 100")
  344. (test '("~:{ ~a,~a ~}" ((a b) (c d e f) (g h))) " a,b c,d g,h ")
  345. (test '("~2:{ ~a,~a ~}" ((a b) (c d e f) (g h))) " a,b c,d ")
  346. (test '("~@{ ~a,~a ~}" a 1 b 2 c 3) " a,1 b,2 c,3 ")
  347. (test '("~2@{ ~a,~a ~} <~a|~a>" a 1 b 2 c 3) " a,1 b,2 <c|3>")
  348. (test '("~:@{ ~a,~a ~}" (a 1) (b 2) (c 3)) " a,1 b,2 c,3 ")
  349. (test '("~2:@{ ~a,~a ~} ~a" (a 1) (b 2) (c 3)) " a,1 b,2 (c 3)")
  350. (test '("~{~}" "<~a,~a>" (a 1 b 2 c 3)) "<a,1><b,2><c,3>")
  351. (test '("~{ ~a ~{<~a>~}~} ~a" (a (1 2) b (3 4)) 10) " a <1><2> b <3><4> 10")
  352. ; up and out
  353. (test '("abc ~^ xyz") "abc ")
  354. ;; SLIB has: (test '("~@(abc ~^ xyz~) ~a" 10) "ABC xyz 10")
  355. (test '("~@(abc ~^ xyz~) ~a" 10) "Abc xyz 10")
  356. (test '("done. ~^ ~d warning~:p. ~^ ~d error~:p.") "done. ")
  357. (test '("done. ~^ ~d warning~:p. ~^ ~d error~:p." 10) "done. 10 warnings. ")
  358. (test '("done. ~^ ~d warning~:p. ~^ ~d error~:p." 10 1)
  359. "done. 10 warnings. 1 error.")
  360. (test '("~{ ~a ~^<~a>~} ~a" (a b c d e f) 10) " a <b> c <d> e <f> 10")
  361. (test '("~{ ~a ~^<~a>~} ~a" (a b c d e) 10) " a <b> c <d> e 10")
  362. (test '("abc~0^ xyz") "abc")
  363. (test '("abc~9^ xyz") "abc xyz")
  364. (test '("abc~7,4^ xyz") "abc xyz")
  365. (test '("abc~7,7^ xyz") "abc")
  366. (test '("abc~3,7,9^ xyz") "abc")
  367. (test '("abc~8,7,9^ xyz") "abc xyz")
  368. (test '("abc~3,7,5^ xyz") "abc xyz")
  369. ; complexity tests (oh my god, I hardly understand them myself (see CL std))
  370. (define fmt "Items:~#[ none~; ~a~; ~a and ~a~:;~@{~#[~; and~] ~a~^,~}~].")
  371. (test `(,fmt ) "Items: none.")
  372. (test `(,fmt foo) "Items: foo.")
  373. (test `(,fmt foo bar) "Items: foo and bar.")
  374. (test `(,fmt foo bar baz) "Items: foo, bar, and baz.")
  375. (test `(,fmt foo bar baz zok) "Items: foo, bar, baz, and zok.")
  376. ; fixed floating points
  377. (cond
  378. (format:floats
  379. (test '("~6,2f" 3.14159) " 3.14")
  380. (test '("~6,1f" 3.14159) " 3.1")
  381. (test '("~6,0f" 3.14159) " 3.")
  382. (test '("~5,1f" 0) " 0.0")
  383. (test '("~10,7f" 3.14159) " 3.1415900")
  384. (test '("~10,7f" -3.14159) "-3.1415900")
  385. (test '("~10,7@f" 3.14159) "+3.1415900")
  386. (test '("~6,3f" 0.0) " 0.000")
  387. (test '("~6,4f" 0.007) "0.0070")
  388. (test '("~6,3f" 0.007) " 0.007")
  389. (test '("~6,2f" 0.007) " 0.01")
  390. (test '("~3,2f" 0.007) ".01")
  391. (test '("~3,2f" -0.007) "-.01")
  392. (test '("~6,2,,,'*f" 3.14159) "**3.14")
  393. (test '("~6,3,,'?f" 12345.56789) "??????")
  394. (test '("~6,3f" 12345.6789) "12345.679")
  395. (test '("~,3f" 12345.6789) "12345.679")
  396. (test '("~,3f" 9.9999) "10.000")
  397. (test '("~6f" 23.4) " 23.4")
  398. (test '("~6f" 1234.5) "1234.5")
  399. (test '("~6f" 12345678) "12345678.")
  400. (test '("~6,,,'?f" 12345678) "??????")
  401. (test '("~6f" 123.56789) "123.57")
  402. (test '("~6f" 123.0) " 123.0")
  403. (test '("~6f" -123.0) "-123.0")
  404. (test '("~6f" 0.0) " 0.0")
  405. (test '("~3f" 3.141) "3.1")
  406. (test '("~2f" 3.141) "3.")
  407. ;; SLIB incorrectly has: (test '("~1f" 3.141) "3.141")
  408. (test '("~1f" 3.141) "3.")
  409. (test '("~f" 123.56789) "123.56789")
  410. (test '("~f" -314.0) "-314.0")
  411. (test '("~f" 0.6) "0.6")
  412. (test '("~f" 0.7) "0.7")
  413. (test '("~f" 1e4) "10000.0")
  414. (test '("~f" -1.23e10) "-12300000000.0")
  415. (test '("~f" 1e-4) "0.0001")
  416. (test '("~f" -1.23e-10) "-0.000000000123")
  417. (test '("~@f" 314.0) "+314.0")
  418. (test '("~,,3f" 0.123456) "123.456")
  419. (test '("~,,-3f" -123.456) "-0.123456")
  420. (test '("~5,,3f" 0.123456) "123.5")
  421. ))
  422. ; exponent floating points
  423. (cond
  424. (format:floats
  425. (test '("~e" 3.14159) "3.14159E+0")
  426. (test '("~e" 0.00001234) "1.234E-5")
  427. (test '("~e" 0.6) "6.0E-1")
  428. (test '("~e" 0.7) "7.0E-1")
  429. (test '("~,,,0e" 0.00001234) "0.1234E-4")
  430. (test '("~,3e" 3.14159) "3.142E+0")
  431. (test '("~,3@e" 3.14159) "+3.142E+0")
  432. (test '("~,3@e" 0.0) "+0.000E+0")
  433. (test '("~,0e" 3.141) "3.E+0")
  434. (test '("~,3,,0e" 3.14159) "0.314E+1")
  435. (test '("~,5,3,-2e" 3.14159) "0.00314E+003")
  436. (test '("~,5,3,-5e" -3.14159) "-0.00000E+006")
  437. (test '("~,5,2,2e" 3.14159) "31.4159E-01")
  438. (test '("~,5,2,,,,'ee" 0.0) "0.00000e+00")
  439. (test '("~12,3e" -3.141) " -3.141E+0")
  440. (test '("~12,3,,,,'#e" -3.141) "###-3.141E+0")
  441. (test '("~10,2e" -1.236e-4) " -1.24E-4")
  442. (test '("~5,3e" -3.141) "-3.141E+0")
  443. (test '("~5,3,,,'*e" -3.141) "*****")
  444. ;; SLIB has (test '("~3e" 3.14159) "3.14159E+0") - which is rather dubious.
  445. (test '("~3e" 3.14159) "3.E+0")
  446. ;; SLIB has (test '("~4e" 3.14159) "3.14159E+0")
  447. (test '("~4e" 3.14159) "3.E+0")
  448. (test '("~5e" 3.14159) "3.E+0")
  449. (test '("~5,,,,'*e" 3.14159) "3.E+0")
  450. (test '("~6e" 3.14159) "3.1E+0")
  451. (test '("~7e" 3.14159) "3.14E+0")
  452. (test '("~7e" -3.14159) "-3.1E+0")
  453. (test '("~8e" 3.14159) "3.142E+0")
  454. (test '("~9e" 3.14159) "3.1416E+0")
  455. (test '("~9,,,,,,'ee" 3.14159) "3.1416e+0")
  456. (test '("~10e" 3.14159) "3.14159E+0")
  457. (test '("~11e" 3.14159) " 3.14159E+0")
  458. (test '("~12e" 3.14159) " 3.14159E+0")
  459. (test '("~13,6,2,-5e" 3.14159) " 0.000003E+06")
  460. (test '("~13,6,2,-4e" 3.14159) " 0.000031E+05")
  461. (test '("~13,6,2,-3e" 3.14159) " 0.000314E+04")
  462. (test '("~13,6,2,-2e" 3.14159) " 0.003142E+03")
  463. (test '("~13,6,2,-1e" 3.14159) " 0.031416E+02")
  464. (test '("~13,6,2,0e" 3.14159) " 0.314159E+01")
  465. (test '("~13,6,2,1e" 3.14159) " 3.141590E+00")
  466. (test '("~13,6,2,2e" 3.14159) " 31.41590E-01")
  467. (test '("~13,6,2,3e" 3.14159) " 314.1590E-02")
  468. (test '("~13,6,2,4e" 3.14159) " 3141.590E-03")
  469. (test '("~13,6,2,5e" 3.14159) " 31415.90E-04")
  470. (test '("~13,6,2,6e" 3.14159) " 314159.0E-05")
  471. (test '("~13,6,2,7e" 3.14159) " 3141590.E-06")
  472. (test '("~13,6,2,8e" 3.14159) "31415900.E-07")
  473. (test '("~7,3,,-2e" 0.001) ".001E+0")
  474. (test '("~8,3,,-2@e" 0.001) "+.001E+0")
  475. (test '("~8,3,,-2@e" -0.001) "-.001E+0")
  476. (test '("~8,3,,-2e" 0.001) "0.001E+0")
  477. ;;SLIB incorrectly has: (test '("~7,,,-2e" 0.001) "0.00E+0")
  478. (test '("~7,,,-2e" 0.001) ".001E+0")
  479. (test '("~12,3,1e" 3.14159e12) " 3.142E+12")
  480. (test '("~12,3,1,,'*e" 3.14159e12) "************")
  481. (test '("~5,3,1e" 3.14159e12) "3.142E+12")
  482. ))
  483. ; general floating point (this test is from Steele's CL book)
  484. (cond
  485. (format:floats
  486. (test '("~9,2,1,,'*g|~9,3,2,3,'?,,'$g|~9,3,2,0,'%g|~9,2g"
  487. 0.0314159 0.0314159 0.0314159 0.0314159)
  488. " 3.14E-2|314.2$-04|0.314E-01| 3.14E-2")
  489. (test '("~9,2,1,,'*g|~9,3,2,3,'?,,'$g|~9,3,2,0,'%g|~9,2g"
  490. 0.314159 0.314159 0.314159 0.314159)
  491. " 0.31 |0.314 |0.314 | 0.31 ")
  492. (test '("~9,2,1,,'*g|~9,3,2,3,'?,,'$g|~9,3,2,0,'%g|~9,2g"
  493. 3.14159 3.14159 3.14159 3.14159)
  494. " 3.1 | 3.14 | 3.14 | 3.1 ")
  495. (test '("~9,2,1,,'*g|~9,3,2,3,'?,,'$g|~9,3,2,0,'%g|~9,2g"
  496. 31.4159 31.4159 31.4159 31.4159)
  497. " 31. | 31.4 | 31.4 | 31. ")
  498. (test '("~9,2,1,,'*g|~9,3,2,3,'?,,'$g|~9,3,2,0,'%g|~9,2g"
  499. 314.159 314.159 314.159 314.159)
  500. " 3.14E+2| 314. | 314. | 3.14E+2")
  501. (test '("~9,2,1,,'*g|~9,3,2,3,'?,,'$g|~9,3,2,0,'%g|~9,2g"
  502. 3141.59 3141.59 3141.59 3141.59)
  503. " 3.14E+3|314.2$+01|0.314E+04| 3.14E+3")
  504. (test '("~9,2,1,,'*g|~9,3,2,3,'?,,'$g|~9,3,2,0,'%g|~9,2g"
  505. 3.14E12 3.14E12 3.14E12 3.14E12)
  506. "*********|314.0$+10|0.314E+13| 3.14E+12")
  507. (test '("~9,2,1,,'*g|~9,3,2,3,'?,,'$g|~9,3,2,0,'%g|~9,2g"
  508. 3.14E120 3.14E120 3.14E120 3.14E120)
  509. "*********|?????????|%%%%%%%%%|3.14E+120")
  510. (test '("~g" 0.0) "0.0 ") ; further ~g tests
  511. (test '("~8g" 0.1) " 0.1 ")
  512. (test '("~g" 0.1) "0.1 ")
  513. (test '("~g" 0.01) "1.0E-2")
  514. (test '("~g" 123.456) "123.456 ")
  515. (test '("~g" 123456.7) "123456.7 ")
  516. (test '("~g" 123456.78) "123456.78 ")
  517. (test '("~g" 0.9282) "0.9282 ")
  518. (test '("~g" 0.09282) "9.282E-2")
  519. (test '("~g" 1) "1.0 ")
  520. (test '("~g" 12) "12.0 ")
  521. ))
  522. ; dollar floating point
  523. (cond
  524. (format:floats
  525. (test '("~$" 1.23) "1.23")
  526. (test '("~$" 1.2) "1.20")
  527. (test '("~$" 0.0) "0.00")
  528. (test '("~$" 9.999) "10.00")
  529. (test '("~3$" 9.9999) "10.000")
  530. (test '("~,4$" 3.2) "0003.20")
  531. (test '("~,4$" 10000.2) "10000.20")
  532. (test '("~,4,10$" 3.2) " 0003.20")
  533. (test '("~,4,10@$" 3.2) " +0003.20")
  534. (test '("~,4,10:@$" 3.2) "+ 0003.20")
  535. (test '("~,4,10:$" -3.2) "- 0003.20")
  536. (test '("~,4,10$" -3.2) " -0003.20")
  537. (test '("~,,10@$" 3.2) " +3.20")
  538. (test '("~,,10:@$" 3.2) "+ 3.20")
  539. (test '("~,,10:@$" -3.2) "- 3.20")
  540. (test '("~,,10,'_@$" 3.2) "_____+3.20")
  541. (test '("~,,4$" 1234.4) "1234.40")
  542. ))
  543. ; complex numbers
  544. (cond
  545. (format:complex-numbers
  546. (test '("~i" 3.0) "3.0+0.0i")
  547. (test '("~,3i" 3.0) "3.000+0.000i")
  548. (test `("~7,2i" ,(string->number "3.0+5.0i")) " 3.00 +5.00i")
  549. (test `("~7,2,1i" ,(string->number "3.0+5.0i")) " 30.00 +50.00i")
  550. (test `("~7,2@i" ,(string->number "3.0+5.0i")) " +3.00 +5.00i")
  551. (test `("~7,2,,,'*@i" ,(string->number "3.0+5.0i")) "**+3.00**+5.00i")
  552. )) ; note: some parsers choke syntactically on reading a complex
  553. ; number though format:complex is #f; this is why we put them in
  554. ; strings
  555. (test '("{~:<[~3d]~:>}" (2 3)) "{([ 2])}")
  556. (test '("{~:<[~3d]~:>}" 23) "{23}")
  557. (test '("~:<~W ~@_~:I~W ~:_~W~1I ~_~W~:>" (defun prod (x y) (* x y)))
  558. "(defun prod (x y) (* x y))")
  559. (test '("~:<~W.~:I~W.~W~1I.~W~:>" (defun prod (x y) (* x y)))
  560. "(defun.prod.(x y).(* x y))")
  561. ;; From email by Ken Dicky posted to SRFI-48 mailing list 2005-06-07:
  562. (test '("~10,3F" 1.02) " 1.020")
  563. (test '("~10,3F" 1.025) " 1.025")
  564. (test '("~10,3F" 1.0256) " 1.026")
  565. (test '("~10,3F" 1.002) " 1.002")
  566. (test '("~10,3F" 1.0025) " 1.002")
  567. (test '("~10,3F" 1.00256) " 1.003")
  568. (test '("~6,3F" 1/3) " 0.333") ;;; " .333" OK
  569. ;(test '("~4F" 12) " 12")
  570. (test '("~8,3F" 12.3456) " 12.346")
  571. (test '("~6,3F" 123.3456) "123.346")
  572. (test '("~4,3F" 123.3456) "123.346")
  573. (test-expect-fail 1) ; ~F doesn't properly support complex numbers
  574. (test `("~8,3F" ,(sqrt -3.8)) "0.000+1.949i")
  575. (test '("~6,2F" 32) " 32.00")
  576. ;; NB: (not (and (exact? 32.) (integer? 32.)))
  577. #| SRFI-48 results
  578. (test '("~6F" 32.) " 32.") ;; " 32.0" OK
  579. (test '("~6F" 32) " 32")
  580. (test '("~8F" 32e45) " 3.2e46")
  581. (test '("~8,2F" 3.4567e20) " 3.46e20")
  582. (test '("~8,2F" 3.4567e21) " 3.46e21")
  583. (test '("~8,2F" 3.4567e22) " 3.46e22")
  584. (test '("~8,2F" 3.4567e23) " 3.46e23")
  585. (test '("~8,0F" 3.4567e24) " 3.e24")
  586. (test '("~8,1F" 3.4567e24) " 3.5e24")
  587. (test '("~8,2F" 3.4567e24) " 3.46e24")
  588. (test '("~8,3F" 3.4567e24) "3.457e24")
  589. (test '("~8,0F" 3.5567e24) " 4.e24")
  590. (test '("~8,1F" 3.5567e24) " 3.6e24")
  591. (test '("~8,2F" 3.5567e24) " 3.56e24")
  592. (test '("~8F" 32e20) " 3.2e21")
  593. |#
  594. ;; Common Lisp results.
  595. (test '("~6F" 32.) " 32.0")
  596. (test '("~8F" 32e17) "3200000000000000000.")
  597. (test '("~8F" 32e-45) " 0.0"); CLisp: ".00000000" SRFI-48: " 3.2e-44"
  598. (test '("~8F" 32e20) "3200000000000000000000.")
  599. ;;(expect " 3.2e6" (format "~8F" 32e5)) ;; ok. converted in input to 3200000.0
  600. (test '("~8F" 32e2) " 3200.0") ;; " 3200." OK for SRFI-48
  601. (test '("~8,2F" 32e10) "320000000000.00") ;; SRFI-48: " 3.20e11"
  602. (test '("~12F" 1.2345) " 1.2345")
  603. (test '("~12,2F" 1.2345) " 1.23")
  604. (test '("~12,3F" 1.2345) " 1.234")
  605. (test `("~20,3F" ,(sqrt -3.8)) "+1.9493588689617927i") ; SRFI-48: " 0.000+1.949i"
  606. (test `("~8,3F" ,(sqrt -3.8)) "+1.9493588689617927i"); SRFI-48: "0.000+1.949i")
  607. (test '("~8,2F" 3.4567e11) "345670000000.00") ; SRFI-48: " 3.46e11")
  608. ; (expect "#1=(a b c . #1#)"
  609. ; (format "~w" (let ( (c '(a b c)) ) (set-cdr! (cddr c) c) c)))
  610. (test `("~A~A~&" ,(list->string (list #\newline)) "") "\n")
  611. (test '("~a ~? ~a" a "~s" (new) test) "a new test")
  612. (test '("~a ~?, ~a!" a "~s ~a" (new test) yes) "a new test, yes!")
  613. #|
  614. Does not match implementation - or Common Lisp.
  615. |#
  616. (test '("~10,0F" -3e-4) " -0.")
  617. (test '("~10,1F" -3e-4) " -0.0")
  618. (test '("~10,2F" -3e-4) " -0.00")
  619. (test '("~10,3F" -3e-4) " -0.000")
  620. (test '("~10,4F" -3e-4) " -0.0003")
  621. (test '("~10,5F" -3e-4) " -0.00030")
  622. (test '("~10,3F" 1.02) " 1.020")
  623. (test '("~10,3F" 1.025) " 1.025")
  624. (test '("~10,3F" 1.0256) " 1.026")
  625. (test '("~10,3F" 1.002) " 1.002")
  626. (test '("~10,3F" 1.0025) " 1.002")
  627. (test '("~10,3F" 1.00256) " 1.003")
  628. (test '("<~a~a>" 3 4) "<34>")
  629. (test `("<~a>" ,(values 3 4)) "<3 4>")
  630. ;; Pretty-Printing
  631. (test '("~<~a~:>" abc) "abc")
  632. ;; Justification
  633. (test '("~<~a~>" 123) "123")
  634. (test '("~10<~a~;~a~>" 12 34) "12 34")
  635. (test '("~10<~a~;~a~;~a~>" 1 2 3) "1 2 3")
  636. (test '("~10<~a~;~a~;~a~;~a~>" 1 2 3 4) "1 2 3 4")
  637. (test '("~10<~a~;~a~;~a~>" 1 2 34567) "1 2 34567")
  638. (test '("~<~a~;~a~;~a~>" 1 2 34567) "1234567")
  639. ; inquiry test
  640. ;; SLIB specific: (test '("~:q") format:version)
  641. ;(if (not test-verbose) (display "done."))
  642. ;(format #t "~%~a Test~:p completed. (~a failure~:p)~2%" total fails)
  643. ; eof
  644. (test-end)