(import scheme) (import (except (chicken format) format)) (import format) (import test) (define-inline (test-format expected fmtstr . rest) (test fmtstr expected (apply format #f fmtstr rest)) ) ; make sure both ~A and ~a work: (test-format "hey there" "~a ~A" "hey" "there") (test-format "7 tries/1 win" "~D tr~:@P/~D win~:P" 7 1) (test-format "1 try/0 wins" "~D tr~:@P/~D win~:P" 1 0) (test-format "1 try/1 win" "~D tr~:@P/~D win~:P" 1 1) (test-format "2 tries/3 wins" "~D tr~:@P/~D win~:P" 2 3) (test-format "Results: NONE\n" "Results: ~#[NONE~;~A~;~A ~0[and~;y~] ~A~;~A, ~A and ~A~]~%") (test-format "Results: 1\n" "Results: ~#[NONE~;~A~;~A ~0[and~;y~] ~A~;~A, ~A and ~A~]~%" 1) (test-format "Results: 1 and 2\n" "Results: ~#[NONE~;~A~;~A ~0[and~;y~] ~A~;~A, ~A and ~A~]~%" 1 2) (test-format "Results: 1, 2 and 3\n" "Results: ~#[NONE~;~A~;~A ~0[and~;y~] ~A~;~A, ~A and ~A~]~%" 1 2 3) (test-format "Zero" "~0[Zero~;One~:;Other~]") (test-format "One" "~1[Zero~;One~:;Other~]") (test-format "Other" "~2[Zero~;One~:;Other~]") (test-format "Other" "~999[Zero~;One~:;Other~]") (test-format "[false]" "[~:[false~;true~]]" #f) (test-format "[true]" "[~:[false~;true~]]" 34) (test-format "[]" "[~@[true~]]" #f) (test-format "[true]" "[~@[true~]]" 39) (test-format " 7" "~? ~D" "<~A ~D>" '("Foo" 5) 7) (test-format " 7" "~? ~D" "<~A ~D>" '("Foo" 5 14) 7) (test-format " 7" "~@? ~D" "<~A ~D>" "Foo" 5 7) ;;Error: (format) 1: "superfluous arguments" ;;when configured w/ format:unprocessed-arguments-error? = #t (test-format " 14" "~@? ~D" "<~A ~D>" "Foo" 5 14 7) (test-format "The winners are: fred harry jill." "The winners are:~{ ~S~}." '(fred harry jill)) (test-format "Pairs: ." "Pairs:~{ <~S,~S>~}." '(a 1 b 2 c 3)) (test-format "Pairs: ." "Pairs:~:{ <~S,~S>~}." '((a 1) (b 2) (c 3))) (test-format "Pairs: ." "Pairs:~@{ <~S,~S>~}." 'a 1 'b 2 'c 3) (test-format "Pairs: ." "Pairs:~:@{ <~S,~S>~}." '(a 1) '(b 2) '(c 3)) (test-format "[]" "[~{hey~}]" '()) (test-format "Done." "Done.~^ ~D warning~:P.~^ ~D error~:P." ) (test-format "Done. 3 warnings." "Done.~^ ~D warning~:P.~^ ~D error~:P." 3) (test-format "Done. 1 warning. 5 errors." "Done.~^ ~D warning~:P.~^ ~D error~:P." 1 5) (test-format "/hot .../hamburger/ice .../french ..." "~:{/~S~^ ...~}" '((hot dog) (hamburger) (ice cream) (french fries))) ;;Error: (format) 1: "superfluous arguments" ;;when configured w/ format:unprocessed-arguments-error? = #t (test-format "None" "~[None~;~:{/~S~^...~}~]" 0 '((h e) (d) (h d))) (test-format "/h.../d/h..." "~[None~;~:{/~S~^...~}~]" 1 '((h e) (d) (h d))) (test-format "TheData: 0" "~1{~:}" "TheData: ~A" '(0)) ;;Failed: Produce: [0][1][2][3][4] Expect: [0][1] ;;when configured w/ format:iteration-bounded = #f (test-format "[0][1]" "~2{[~A]~}" '(0 1 2 3 4)) (test-format "a \n a ^J #\\a #\\newline" "~C ~C ~:C ~:C ~@C ~@C" #\a #\newline #\a #\newline #\a #\newline) (test-format "XXIII MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMCCCLXXXIIII DCCCCXXIII" "~:@R ~:@R ~:@R" 23 32384 923) (test-format "MCMLXXX IV CCCXCIII MMMMMMMMMCCXXXIX" "~@R ~@R ~@R ~@R" 1980 4 393 9239) (test-format "one hundred twenty-eight zero two million, nine hundred thirty-eight thousand, three hundred twenty-eight nine thousand, two hundred thirty-eight two thousand, eight hundred thirty-nine thirty-eight three thousand, eight hundred twenty-eight" "~R ~R ~R ~R ~R ~R ~R" 128 0 2938328 9238 2839 38 3828) (test-format "one hundred twenty-eighth zeroth two million, nine hundred thirty-eight thousand, three hundred twenty-eighth nine thousand, two hundred thirty-eighth two thousand, eight hundred thirty-ninth thirty-eighth three thousand, eight hundred twentieth" "~:R ~:R ~:R ~:R ~:R ~:R ~:R" 128 0 2938328 9238 2839 38 3820) (test-format " -68." "~5,0F" -67.77) (test-format "-67.8" "~5,1F" -67.77) (test-format "1.000" "~,3F" 1) (test-format "1.0" "~F" 1) (test-format "***" "~3,3,0,'*F" 2) (test-format "1234.0" "~,,3F" 1.234) (define (foo expected x) (test-format expected "~6,2F|~6,2,1,'*F|~6,2,,'?F|~6F|~,2F|~F" x x x x x x)) (foo " 3.14| 31.42| 3.14|3.1416|3.14|3.14159" 3.14159) (foo " -3.14|-31.42| -3.14|-3.142|-3.14|-3.14159" -3.14159) (foo "100.00|******|100.00| 100.0|100.00|100.0" 100.0) (foo "1234.00|******|??????|1234.0|1234.00|1234.0" 1234.0) (foo " 0.01| 0.06| 0.01| 0.006|0.01|0.006" 0.006) (test-format "1.23243E+2" "~E" 123.243) (test-format "1.0E+0" "~E" 1) (test-format " 1.0E+0" "~10E" 1) (test-format "1.000E+0" "~,3E" 1) (test-format "1.0E-4" "~E" 0.0001) (test-format "2.3E+00001" "~,,5E" 23) (test-format "===" "~3,,,,'=E" 23) (test-format "####2.3E+1" "~10,,,,,'#E" 23) (test-format "1.0$+0" "~,,,,,,'$E" 1) (define (foo expected x) (test-format expected "~9,2,1,,'*E|~10,3,2,2,'?,,'$E|~9,3,2,-2,'%@E|~9,2E" x x x x)) ;; Fixed - wasn't leaving off leading 0 when result-len > len so considered overflow (foo " 3.14E+0| 31.42$-01|+.003E+03| 3.14E+0" 3.14159) (foo " -3.14E+0|-31.42$-01|-.003E+03| -3.14E+0" -3.14159) (foo " 1.10E+3| 11.00$+02|+.001E+06| 1.10E+3" 1100.0) (foo "*********| 11.00$+12|+.001E+16| 1.10E+13" 1.1E13) (define (foo expected x) (test-format expected "~9,2,1,,'*G|~9,3,2,3,'?,,'$G|~9,3,2,0,'%G|~9,2G" x x x x)) (foo " 3.14E-2|314.2$-04|0.314E-01| 3.14E-2" 0.0314159) (foo " 3.14E+3|314.2$+01|0.314E+04| 3.14E+3" 3141.59) (foo " 0.31 |0.314 |0.314 | 0.31 " 0.314159) (foo " 3.1 | 3.14 | 3.14 | 3.1 " 3.14159) (foo " 31. | 31.4 | 31.4 | 31. " 31.4159) (foo " 3.14E+2| 314. | 314. | 3.14E+2" 314.159) (foo "*********|314.0$+10|0.314E+13| 3.14E+12" 3.14E12) (test-format " " "~T") (test-format " " " ~T") (test-format " " "~4T") (test-format " " " ~5,6T") (test-format " " " ~4,4T") (test-format " " " ~4,4T") ;; the original test was wrong (test-format " " " ~4,3T") (test-format " " "~4,4@T") (test-format " " "~4,3@T") (test-format " " " ~4,3@T") (test-exit)