source: project/release/5/format/trunk/tests/format-test.scm @ 36265

Last change on this file since 36265 was 36265, checked in by felix winkelmann, 20 months ago

format 3.2.0

File size: 5.8 KB
Line 
1(import scheme)
2(import (chicken format))
3(import format)
4
5(define *passed* 0)
6(define *failed* 0)
7
8(define (test expected . rest)
9        (let ([result (apply format #f rest)])
10                (if (string=? result expected)
11                        (begin
12                                (set! *passed* (add1 *passed*))
13                                (printf "Passed: (~S)~%Produce: ~S~%Expect:  ~S~%~%" rest result expected))
14                        (begin
15                                (set! *failed* (add1 *failed*))
16                                (printf "Failed: (~S)~%Produce: ~S~%Expect:  ~S~%~%" rest result expected)))))
17
18; make sure both ~A and ~a work:
19(test "hey there" "~a ~A" "hey" "there")
20
21(test "7 tries/1 win" "~D tr~:@P/~D win~:P" 7 1)
22(test "1 try/0 wins" "~D tr~:@P/~D win~:P" 1 0)
23(test "1 try/1 win" "~D tr~:@P/~D win~:P" 1 1)
24(test "2 tries/3 wins" "~D tr~:@P/~D win~:P" 2 3)
25
26(test "Results: NONE\n" "Results: ~#[NONE~;~A~;~A ~0[and~;y~] ~A~;~A, ~A and ~A~]~%")
27(test "Results: 1\n" "Results: ~#[NONE~;~A~;~A ~0[and~;y~] ~A~;~A, ~A and ~A~]~%" 1)
28(test "Results: 1 and 2\n" "Results: ~#[NONE~;~A~;~A ~0[and~;y~] ~A~;~A, ~A and ~A~]~%" 1 2)
29(test "Results: 1, 2 and 3\n" "Results: ~#[NONE~;~A~;~A ~0[and~;y~] ~A~;~A, ~A and ~A~]~%" 1 2 3)
30
31(test "Zero" "~0[Zero~;One~:;Other~]")
32(test "One" "~1[Zero~;One~:;Other~]")
33(test "Other" "~2[Zero~;One~:;Other~]")
34(test "Other" "~999[Zero~;One~:;Other~]")
35
36(test "[false]" "[~:[false~;true~]]" #f)
37(test "[true]" "[~:[false~;true~]]" 34)
38(test "[]" "[~@[true~]]" #f)
39(test "[true]" "[~@[true~]]" 39)
40
41(test "<Foo 5> 7" "~? ~D" "<~A ~D>" '("Foo" 5) 7)
42(test "<Foo 5> 7" "~? ~D" "<~A ~D>" '("Foo" 5 14) 7)
43(test "<Foo 5> 7" "~@? ~D" "<~A ~D>" "Foo" 5 7)
44;;Error: (format) 1: "superfluous arguments"
45;;when configured w/ format:unprocessed-arguments-error? = #t
46(test "<Foo 5> 14" "~@? ~D" "<~A ~D>" "Foo" 5 14 7)
47
48(test "The winners are: fred harry jill." "The winners are:~{ ~S~}." '(fred harry jill))
49
50(test "Pairs: <a,1> <b,2> <c,3>." "Pairs:~{ <~S,~S>~}." '(a 1 b 2 c 3))
51(test "Pairs: <a,1> <b,2> <c,3>." "Pairs:~:{ <~S,~S>~}." '((a 1) (b 2) (c 3)))
52(test "Pairs: <a,1> <b,2> <c,3>." "Pairs:~@{ <~S,~S>~}." 'a 1 'b 2 'c 3)
53(test "Pairs: <a,1> <b,2> <c,3>." "Pairs:~:@{ <~S,~S>~}." '(a 1) '(b 2) '(c 3))
54
55(test "[]" "[~{hey~}]" '())
56
57(test "Done." "Done.~^  ~D warning~:P.~^  ~D error~:P." )
58(test "Done.  3 warnings." "Done.~^  ~D warning~:P.~^  ~D error~:P." 3)
59(test "Done.  1 warning.  5 errors." "Done.~^  ~D warning~:P.~^  ~D error~:P." 1 5)
60
61(test "/hot .../hamburger/ice .../french ..." "~:{/~S~^ ...~}" '((hot dog) (hamburger) (ice cream) (french fries)))
62
63;;Error: (format) 1: "superfluous arguments"
64;;when configured w/ format:unprocessed-arguments-error? = #t
65(test "None" "~[None~;~:{/~S~^...~}~]" 0 '((h e) (d) (h d)))
66
67(test "/h.../d/h..." "~[None~;~:{/~S~^...~}~]" 1 '((h e) (d) (h d)))
68
69(test "TheData: 0" "~1{~:}" "TheData: ~A" '(0))
70
71;;Failed: Produce: [0][1][2][3][4] Expect: [0][1]
72;;when configured w/ format:iteration-bounded = #f
73(test "[0][1]" "~2{[~A]~}" '(0 1 2 3 4))
74
75(test "a \n a ^J #\\a #\\newline" "~C ~C ~:C ~:C ~@C ~@C" #\a #\newline #\a #\newline #\a #\newline)
76
77(test "XXIII MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMCCCLXXXIIII DCCCCXXIII" "~:@R ~:@R ~:@R" 23 32384 923)
78(test "MCMLXXX IV CCCXCIII MMMMMMMMMCCXXXIX" "~@R ~@R ~@R ~@R" 1980 4 393 9239)
79
80(test "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)
81(test "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)
82
83(test " -68." "~5,0F" -67.77)
84(test "-67.8" "~5,1F" -67.77)
85(test "1.000" "~,3F" 1)
86(test "1.0" "~F" 1)
87(test "***" "~3,3,0,'*F" 2)
88(test "1234.0" "~,,3F" 1.234)
89
90(define (foo expected x)
91  (test expected "~6,2F|~6,2,1,'*F|~6,2,,'?F|~6F|~,2F|~F" x x x x x x))
92
93(foo "  3.14| 31.42|  3.14|3.1416|3.14|3.14159" 3.14159)
94(foo " -3.14|-31.42| -3.14|-3.142|-3.14|-3.14159" -3.14159)
95(foo "100.00|******|100.00| 100.0|100.00|100.0" 100.0)
96(foo "1234.00|******|??????|1234.0|1234.00|1234.0" 1234.0)
97(foo "  0.01|  0.06|  0.01| 0.006|0.01|0.006" 0.006)
98
99(test "1.23243E+2" "~E" 123.243)
100(test "1.0E+0" "~E" 1)
101(test "    1.0E+0" "~10E" 1)
102(test "1.000E+0" "~,3E" 1)
103(test "1.0E-4" "~E" 0.0001)
104(test "2.3E+00001" "~,,5E" 23)
105(test "===" "~3,,,,'=E" 23)
106(test "####2.3E+1" "~10,,,,,'#E" 23)
107(test "1.0$+0" "~,,,,,,'$E" 1)
108
109(define (foo expected x)
110  (test expected "~9,2,1,,'*E|~10,3,2,2,'?,,'$E|~9,3,2,-2,'%@E|~9,2E" x x x x))
111
112;; Fixed - wasn't leaving off leading 0 when result-len > len so considered overflow
113(foo "  3.14E+0| 31.42$-01|+.003E+03|  3.14E+0" 3.14159)
114(foo " -3.14E+0|-31.42$-01|-.003E+03| -3.14E+0" -3.14159)
115(foo "  1.10E+3| 11.00$+02|+.001E+06|  1.10E+3" 1100.0)
116(foo "*********| 11.00$+12|+.001E+16| 1.10E+13" 1.1E13)
117
118(define (foo expected x)
119  (test expected "~9,2,1,,'*G|~9,3,2,3,'?,,'$G|~9,3,2,0,'%G|~9,2G" x x x x))
120
121(foo  "  3.14E-2|314.2$-04|0.314E-01|  3.14E-2" 0.0314159)
122(foo  "  3.14E+3|314.2$+01|0.314E+04|  3.14E+3" 3141.59)
123(foo  "  0.31   |0.314    |0.314    | 0.31    " 0.314159)
124(foo  "   3.1   | 3.14    | 3.14    |  3.1    " 3.14159)
125(foo  "   31.   | 31.4    | 31.4    |  31.    " 31.4159)
126(foo  "  3.14E+2| 314.    | 314.    |  3.14E+2" 314.159)
127(foo  "*********|314.0$+10|0.314E+13| 3.14E+12" 3.14E12)
128
129(test " "         "~T")
130(test "  "        "  ~T")
131(test "    "      "~4T")
132(test "     "     "  ~5,6T")
133(test "        "  "      ~4,4T")
134(test "        "  "     ~4,4T")
135;; the original test was wrong
136(test "       "    "     ~4,3T")
137(test "    "      "~4,4@T")
138(test "      "    "~4,3@T")
139(test "         " "   ~4,3@T")
140
141(printf "Passed: ~S Failed: ~S~%" *passed* *failed*)
142
143(exit (if (not (zero? *failed*)) 1 0))
Note: See TracBrowser for help on using the repository browser.