OSDN Git Service

416d47e9c0d5328ac295326c54e9bbcd243dd458
[pf3gnuchains/sourceware.git] / tcl / tests / format.test
1 # Commands covered:  format
2 #
3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands.  Sourcing this file into Tcl runs the tests and
5 # generates output for errors.  No output means no errors were found.
6 #
7 # Copyright (c) 1991-1994 The Regents of the University of California.
8 # Copyright (c) 1994-1998 Sun Microsystems, Inc.
9 #
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 #
13 # RCS: @(#) $Id$
14
15 if {[lsearch [namespace children] ::tcltest] == -1} {
16     package require tcltest
17     namespace import -force ::tcltest::*
18 }
19
20 # The following code is needed because some versions of SCO Unix have
21 # a round-off error in sprintf which would cause some of the tests to
22 # fail.  Someday I hope this code shouldn't be necessary (code added
23 # 9/9/91).
24
25 set roundOffBug 0
26 if {"[format %7.1e  68.514]" == "6.8e+01"} {
27     puts stdout "Note: this system has a sprintf round-off bug, some tests skipped\n"
28     set roundOffBug 1
29 }
30
31 test format-1.1 {integer formatting} {
32     format "%*d %d %d %d" 6 34 16923 -12 -1
33 } {    34 16923 -12 -1}
34 test format-1.2 {integer formatting} {nonPortable} {
35     format "%4d %4d %4d %4d %d %#x %#X" 6 34 16923 -12 -1 14 12
36 } {   6   34 16923  -12 -1 0xe 0XC}
37
38 # %u output depends on word length, so this test is not portable.
39
40 test format-1.3 {integer formatting} {nonPortable} {
41     format "%4u %4u %4u %4u %d %#o" 6 34 16923 -12 -1 0
42 } {   6   34 16923 4294967284 -1 0}
43 test format-1.4 {integer formatting} {
44     format "%-4d %-4i %-4d %-4ld" 6 34 16923 -12 -1
45 } {6    34   16923 -12 }
46 test format-1.5 {integer formatting} {
47     format "%04d %04d %04d %04i" 6 34 16923 -12 -1
48 } {0006 0034 16923 -012}
49 test format-1.6 {integer formatting} {
50     format "%00*d" 6 34
51 } {000034}
52
53 # Printing negative numbers in hex or octal format depends on word
54 # length, so these tests are not portable.
55
56 test format-1.7 {integer formatting} {nonPortable} {
57     format "%4x %4x %4x %4x" 6 34 16923 -12 -1
58 } {   6   22 421b fffffff4}
59 test format-1.8 {integer formatting} {nonPortable} {
60     format "%#x %#X %#X %#x" 6 34 16923 -12 -1
61 } {0x6 0X22 0X421B 0xfffffff4}
62 test format-1.9 {integer formatting} {nonPortable} {
63     format "%#20x %#20x %#20x %#20x" 6 34 16923 -12 -1
64 } {                 0x6                 0x22               0x421b           0xfffffff4}
65 test format-1.10 {integer formatting} {nonPortable} {
66     format "%-#20x %-#20x %-#20x %-#20x" 6 34 16923 -12 -1
67 } {0x6                  0x22                 0x421b               0xfffffff4          }
68 test format-1.11 {integer formatting} {nonPortable} {
69     format "%-#20o %#-20o %#-20o %#-20o" 6 34 16923 -12 -1
70 } {06                   042                  041033               037777777764        }
71
72 test format-2.1 {string formatting} {
73     format "%s %s %c %s" abcd {This is a very long test string.} 120 x
74 } {abcd This is a very long test string. x x}
75 test format-2.2 {string formatting} {
76     format "%20s %20s %20c %20s" abcd {This is a very long test string.} 120 x
77 } {                abcd This is a very long test string.                    x                    x}
78 test format-2.3 {string formatting} {
79     format "%.10s %.10s %c %.10s" abcd {This is a very long test string.} 120 x
80 } {abcd This is a  x x}
81 test format-2.4 {string formatting} {
82     format "%s %s %% %c %s" abcd {This is a very long test string.} 120 x
83 } {abcd This is a very long test string. % x x}
84 test format-2.5 {string formatting, embedded nulls} {
85     format "%10s" abc\0def
86 } "   abc\0def"
87 test format-2.6 {string formatting, international chars} {
88     format "%10s" abc\ufeffdef
89 } "   abc\ufeffdef"
90 test format-2.6 {string formatting, international chars} {
91     format "%.5s" abc\ufeffdef
92 } "abc\ufeffd"
93 test format-2.7 {string formatting, international chars} {
94     format "foo\ufeffbar%s" baz
95 } "foo\ufeffbarbaz"
96 test format-2.8 {string formatting, width} {
97     format "a%5sa" f
98 } "a    fa"
99 test format-2.8 {string formatting, width} {
100     format "a%-5sa" f
101 } "af    a"
102 test format-2.8 {string formatting, width} {
103     format "a%2sa" foo
104 } "afooa"
105 test format-2.8 {string formatting, width} {
106     format "a%0sa" foo
107 } "afooa"
108 test format-2.8 {string formatting, precision} {
109     format "a%.2sa" foobarbaz
110 } "afoa"
111 test format-2.8 {string formatting, precision} {
112     format "a%.sa" foobarbaz
113 } "aa"
114 test format-2.8 {string formatting, precision} {
115     list [catch {format "a%.-2sa" foobarbaz} msg] $msg
116 } {1 {bad field specifier "-"}}
117 test format-2.8 {string formatting, width and precision} {
118     format "a%5.2sa" foobarbaz
119 } "a   foa"
120 test format-2.8 {string formatting, width and precision} {
121     format "a%5.7sa" foobarbaz
122 } "afoobarba"
123
124 test format-3.1 {Tcl_FormatObjCmd: character formatting} {
125     format "|%c|%0c|%-1c|%1c|%-6c|%6c|%*c|%*c|" 65 65 65 65 65 65 3 65 -4 65
126 } "|A|A|A|A|A     |     A|  A|A   |"
127 test format-3.2 {Tcl_FormatObjCmd: international character formatting} {
128     format "|%c|%0c|%-1c|%1c|%-6c|%6c|%*c|%*c|" 0xa2 0x4e4e 0x25a 0xc3 0xff08 0 3 0x6575 -4 0x4e4f
129 } "|\ua2|\u4e4e|\u25a|\uc3|\uff08     |     \0|  \u6575|\u4e4f   |"
130
131 test format-4.1 {e and f formats} {eformat} {
132     format "%e %e %e %e" 34.2e12 68.514 -.125 -16000. .000053
133 } {3.420000e+13 6.851400e+01 -1.250000e-01 -1.600000e+04}
134 test format-4.2 {e and f formats} {eformat} {
135     format "%20e %20e %20e %20e" 34.2e12 68.514 -.125 -16000. .000053
136 } {        3.420000e+13         6.851400e+01        -1.250000e-01        -1.600000e+04}
137 if {!$roundOffBug} {
138     test format-4.3 {e and f formats} {eformat} {
139         format "%.1e %.1e %.1e %.1e" 34.2e12 68.514 -.126 -16000. .000053
140     } {3.4e+13 6.9e+01 -1.3e-01 -1.6e+04}
141     test format-4.4 {e and f formats} {eformat} {
142         format "%020e %020e %020e %020e" 34.2e12 68.514 -.126 -16000. .000053
143     } {000000003.420000e+13 000000006.851400e+01 -00000001.260000e-01 -00000001.600000e+04}
144     test format-4.5 {e and f formats} {eformat} {
145         format "%7.1e %7.1e %7.1e %7.1e" 34.2e12 68.514 -.126 -16000. .000053
146     } {3.4e+13 6.9e+01 -1.3e-01 -1.6e+04}
147     test format-4.6 {e and f formats} {
148         format "%f %f %f %f" 34.2e12 68.514 -.125 -16000. .000053
149     } {34200000000000.000000 68.514000 -0.125000 -16000.000000}
150 }
151 test format-4.7 {e and f formats} {nonPortable} {
152     format "%.4f %.4f %.4f %.4f %.4f" 34.2e12 68.514 -.125 -16000. .000053
153 } {34200000000000.0000 68.5140 -0.1250 -16000.0000 0.0001}
154 test format-4.8 {e and f formats} {eformat} {
155     format "%.4e %.5e %.6e" -9.99996 -9.99996 9.99996
156 } {-1.0000e+01 -9.99996e+00 9.999960e+00}
157 test format-4.9 {e and f formats} {
158     format "%.4f %.5f %.6f" -9.99996 -9.99996 9.99996
159 } {-10.0000 -9.99996 9.999960}
160 test format-4.10 {e and f formats} {
161     format "%20f %-20f %020f" -9.99996 -9.99996 9.99996
162 } {           -9.999960 -9.999960            0000000000009.999960}
163 test format-4.11 {e and f formats} {
164     format "%-020f %020f" -9.99996 -9.99996 9.99996
165 } {-9.999960            -000000000009.999960}
166 test format-4.12 {e and f formats} {eformat} {
167     format "%.0e %#.0e" -9.99996 -9.99996 9.99996
168 } {-1e+01 -1.e+01}
169 test format-4.13 {e and f formats} {
170     format "%.0f %#.0f" -9.99996 -9.99996 9.99996
171 } {-10 -10.}
172 test format-4.14 {e and f formats} {
173     format "%.4f %.5f %.6f" -9.99996 -9.99996 9.99996
174 } {-10.0000 -9.99996 9.999960}
175 test format-4.15 {e and f formats} {
176     format "%3.0f %3.0f %3.0f %3.0f" 1.0 1.1 1.01 1.001
177 } {  1   1   1   1}
178 test format-4.16 {e and f formats} {
179     format "%3.1f %3.1f %3.1f %3.1f" 0.0 0.1 0.01 0.001
180 } {0.0 0.1 0.0 0.0}
181
182 test format-5.1 {g-format} {eformat} {
183     format "%.3g" 12341.0
184 } {1.23e+04}
185 test format-5.2 {g-format} {eformat} {
186     format "%.3G" 1234.12345
187 } {1.23E+03}
188 test format-5.3 {g-format} {
189     format "%.3g" 123.412345
190 } {123}
191 test format-5.4 {g-format} {
192     format "%.3g" 12.3412345
193 } {12.3}
194 test format-5.5 {g-format} {
195     format "%.3g" 1.23412345
196 } {1.23}
197 test format-5.6 {g-format} {
198     format "%.3g" 1.23412345
199 } {1.23}
200 test format-5.7 {g-format} {
201     format "%.3g" .123412345
202 } {0.123}
203 test format-5.8 {g-format} {
204     format "%.3g" .012341
205 } {0.0123}
206 test format-5.9 {g-format} {
207     format "%.3g" .0012341
208 } {0.00123}
209 test format-5.10 {g-format} {
210     format "%.3g" .00012341
211 } {0.000123}
212 test format-5.11 {g-format} {eformat} {
213     format "%.3g" .00001234
214 } {1.23e-05}
215 test format-5.12 {g-format} {eformat} {
216     format "%.4g" 9999.6
217 } {1e+04}
218 test format-5.13 {g-format} {
219     format "%.4g" 999.96
220 } {1000}
221 test format-5.14 {g-format} {
222     format "%.3g" 1.0
223 } {1}
224 test format-5.15 {g-format} {
225     format "%.3g" .1
226 } {0.1}
227 test format-5.16 {g-format} {
228     format "%.3g" .01
229 } {0.01}
230 test format-5.17 {g-format} {
231     format "%.3g" .001
232 } {0.001}
233 test format-5.18 {g-format} {eformat} {
234     format "%.3g" .00001
235 } {1e-05}
236 test format-5.19 {g-format} {eformat} {
237     format "%#.3g" 1234.0
238 } {1.23e+03}
239 test format-5.20 {g-format} {eformat} {
240     format "%#.3G" 9999.5
241 } {1.00E+04}
242
243 test format-6.1 {floating-point zeroes} {eformat} {
244     format "%e %f %g" 0.0 0.0 0.0 0.0
245 } {0.000000e+00 0.000000 0}
246 test format-6.2 {floating-point zeroes} {eformat} {
247     format "%.4e %.4f %.4g" 0.0 0.0 0.0 0.0
248 } {0.0000e+00 0.0000 0}
249 test format-6.3 {floating-point zeroes} {eformat} {
250     format "%#.4e %#.4f %#.4g" 0.0 0.0 0.0 0.0
251 } {0.0000e+00 0.0000 0.000}
252 test format-6.4 {floating-point zeroes} {eformat} {
253     format "%.0e %.0f %.0g" 0.0 0.0 0.0 0.0
254 } {0e+00 0 0}
255 test format-6.5 {floating-point zeroes} {eformat} {
256     format "%#.0e %#.0f %#.0g" 0.0 0.0 0.0 0.0
257 } {0.e+00 0. 0.}
258 test format-6.6 {floating-point zeroes} {
259     format "%3.0f %3.0f %3.0f %3.0f" 0.0 0.0 0.0 0.0
260 } {  0   0   0   0}
261 test format-6.7 {floating-point zeroes} {
262     format "%3.0f %3.0f %3.0f %3.0f" 1.0 1.1 1.01 1.001
263 } {  1   1   1   1}
264 test format-6.8 {floating-point zeroes} {
265     format "%3.1f %3.1f %3.1f %3.1f" 0.0 0.1 0.01 0.001
266 } {0.0 0.1 0.0 0.0}
267
268 test format-7.1 {various syntax features} {
269     format "%*.*f" 12 3 12.345678901
270 } {      12.346}
271 test format-7.2 {various syntax features} {
272     format "%0*.*f" 12 3 12.345678901
273 } {00000012.346}
274 test format-7.3 {various syntax features} {
275     format "\*\t\\n"
276 } {*    \n}
277
278 test format-8.1 {error conditions} {
279     catch format
280 } 1
281 test format-8.2 {error conditions} {
282     catch format msg
283     set msg
284 } {wrong # args: should be "format formatString ?arg arg ...?"}
285 test format-8.3 {error conditions} {
286     catch {format %*d}
287 } 1
288 test format-8.4 {error conditions} {
289     catch {format %*d} msg
290     set msg
291 } {not enough arguments for all format specifiers}
292 test format-8.5 {error conditions} {
293     catch {format %*.*f 12}
294 } 1
295 test format-8.6 {error conditions} {
296     catch {format %*.*f 12} msg
297     set msg
298 } {not enough arguments for all format specifiers}
299 test format-8.7 {error conditions} {
300     catch {format %*.*f 12 3}
301 } 1
302 test format-8.8 {error conditions} {
303     catch {format %*.*f 12 3} msg
304     set msg
305 } {not enough arguments for all format specifiers}
306 test format-8.9 {error conditions} {
307     list [catch {format %*d x 3} msg] $msg
308 } {1 {expected integer but got "x"}}
309 test format-8.10 {error conditions} {
310     list [catch {format %*.*f 2 xyz 3} msg] $msg
311 } {1 {expected integer but got "xyz"}}
312 test format-8.11 {error conditions} {
313     catch {format %d 2a}
314 } 1
315 test format-8.12 {error conditions} {
316     catch {format %d 2a} msg
317     set msg
318 } {expected integer but got "2a"}
319 test format-8.13 {error conditions} {
320     catch {format %c 2x}
321 } 1
322 test format-8.14 {error conditions} {
323     catch {format %c 2x} msg
324     set msg
325 } {expected integer but got "2x"}
326 test format-8.15 {error conditions} {
327     catch {format %f 2.1z}
328 } 1
329 test format-8.16 {error conditions} {
330     catch {format %f 2.1z} msg
331     set msg
332 } {expected floating-point number but got "2.1z"}
333 test format-8.17 {error conditions} {
334     catch {format ab%}
335 } 1
336 test format-8.18 {error conditions} {
337     catch {format ab% 12} msg
338     set msg
339 } {format string ended in middle of field specifier}
340 test format-8.19 {error conditions} {
341     catch {format %q x}
342 } 1
343 test format-8.20 {error conditions} {
344     catch {format %q x} msg
345     set msg
346 } {bad field specifier "q"}
347 test format-8.21 {error conditions} {
348     catch {format %d}
349 } 1
350 test format-8.22 {error conditions} {
351     catch {format %d} msg
352     set msg
353 } {not enough arguments for all format specifiers}
354 test format-8.23 {error conditions} {
355     catch {format "%d %d" 24 xyz} msg
356     set msg
357 } {expected integer but got "xyz"}
358
359 test format-9.1 {long result} {
360     set a {1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}
361     format {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG %s %s} $a $a
362 } {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}
363
364 test format-10.1 {"h" format specifier} {nonPortable} {
365     format %hd 0xffff
366 } -1
367 test format-10.2 {"h" format specifier} {nonPortable} {
368     format %hx 0x10fff
369 } fff
370 test format-10.3 {"h" format specifier} {nonPortable} {
371     format %hd 0x10000
372 } 0
373
374 test format-11.1 {XPG3 %$n specifiers} {
375     format {%2$d %1$d} 4 5
376 } {5 4}
377 test format-11.2 {XPG3 %$n specifiers} {
378     format {%2$d %1$d %1$d %3$d} 4 5 6
379 } {5 4 4 6}
380 test format-11.3 {XPG3 %$n specifiers} {
381     list [catch {format {%2$d %3$d} 4 5} msg] $msg
382 } {1 {"%n$" argument index out of range}}
383 test format-11.4 {XPG3 %$n specifiers} {
384     list [catch {format {%2$d %0$d} 4 5 6} msg] $msg
385 } {1 {"%n$" argument index out of range}}
386 test format-11.5 {XPG3 %$n specifiers} {
387     list [catch {format {%d %1$d} 4 5 6} msg] $msg
388 } {1 {cannot mix "%" and "%n$" conversion specifiers}}
389 test format-11.6 {XPG3 %$n specifiers} {
390     list [catch {format {%2$d %d} 4 5 6} msg] $msg
391 } {1 {cannot mix "%" and "%n$" conversion specifiers}}
392 test format-11.7 {XPG3 %$n specifiers} {
393     list [catch {format {%2$d %3d} 4 5 6} msg] $msg
394 } {1 {cannot mix "%" and "%n$" conversion specifiers}}
395 test format-11.8 {XPG3 %$n specifiers} {
396     format {%2$*d %3$d} 1 10 4
397 } {         4 4}
398 test format-11.9 {XPG3 %$n specifiers} {
399     format {%2$.*s %4$d} 1 5 abcdefghijklmnop 44
400 } {abcde 44}
401 test format-11.10 {XPG3 %$n specifiers} {
402     list [catch {format {%2$*d} 4} msg] $msg
403 } {1 {"%n$" argument index out of range}}
404 test format-11.11 {XPG3 %$n specifiers} {
405     list [catch {format {%2$*d} 4 5} msg] $msg
406 } {1 {"%n$" argument index out of range}}
407 test format-11.12 {XPG3 %$n specifiers} {
408     list [catch {format {%2$*d} 4 5 6} msg] $msg
409 } {0 {    6}}
410
411 test format-12.1 {negative width specifiers} {
412     format "%*d" -47 25
413 } {25                                             }
414 test format-13.1 {tcl_precision fuzzy comparison} {
415     catch {unset a}
416     catch {unset b}
417     catch {unset c}
418     catch {unset d}
419     set a 0.0000000000001
420     set b 0.00000000000001
421     set c 0.00000000000000001
422     set d [expr $a + $b + $c]
423     format {%0.10f %0.12f %0.15f %0.17f} $d $d $d $d
424 } {0.0000000000 0.000000000000 0.000000000000110 0.00000000000011001}
425 test format-13.2 {tcl_precision fuzzy comparison} {
426     catch {unset a}
427     catch {unset b}
428     catch {unset c}
429     catch {unset d}
430     set a 0.000000000001
431     set b 0.000000000000005
432     set c 0.0000000000000008
433     set d [expr $a + $b + $c]
434     format {%0.10f %0.12f %0.15f %0.17f} $d $d $d $d
435 } {0.0000000000 0.000000000001 0.000000000001006 0.00000000000100580}
436 test format-13.3 {tcl_precision fuzzy comparison} {
437     catch {unset a}
438     catch {unset b}
439     catch {unset c}
440     set a 0.00000000000099
441     set b 0.000000000000011
442     set c [expr $a + $b]
443     format {%0.10f %0.12f %0.15f %0.17f} $c $c $c $c
444 } {0.0000000000 0.000000000001 0.000000000001001 0.00000000000100100}
445 test format-13.4 {tcl_precision fuzzy comparison} {
446     catch {unset a}
447     catch {unset b}
448     catch {unset c}
449     set a 0.444444444444
450     set b 0.33333333333333
451     set c [expr $a + $b]
452     format {%0.10f %0.12f %0.15f %0.16f} $c $c $c $c
453 } {0.7777777778 0.777777777777 0.777777777777330 0.7777777777773300}
454 test format-13.5 {tcl_precision fuzzy comparison} {
455     catch {unset a}
456     catch {unset b}
457     catch {unset c}
458     set a 0.444444444444
459     set b 0.99999999999999
460     set c [expr $a + $b]
461     format {%0.10f %0.12f %0.15f} $c $c $c
462 } {1.4444444444 1.444444444444 1.444444444443990}
463 test format-14.1 {testing MAX_FLOAT_SIZE for 0 and 1} {
464     format {%s} ""
465 } {}
466 test format-14.2 {testing MAX_FLOAT_SIZE for 0 and 1} {
467     format {%s} "a"
468 } {a}
469
470 test format-15.1 {testing %0..s 0 padding for chars/strings} {
471     format %05s a
472 } {0000a}
473 test format-15.2 {testing %0..s 0 padding for chars/strings} {
474     format "% 5s" a
475 } {    a}
476 test format-15.3 {testing %0..s 0 padding for chars/strings} {
477     format %5s a
478 } {    a}
479 test format-15.4 {testing %0..s 0 padding for chars/strings} {
480     format %05c 61
481 } {0000=}
482
483 set a "0123456789"
484 set b ""
485 for {set i 0} {$i < 290} {incr i} {
486     append b $a
487 }
488 for {set i 290} {$i < 400} {incr i} {
489     test format-15.[expr $i -290] {testing MAX_FLOAT_SIZE} {
490         format {%s} $b    
491     } $b
492     append b "x"
493 }
494
495 # cleanup
496 catch {unset a}
497 catch {unset b}
498 catch {unset c}
499 catch {unset d}
500 ::tcltest::cleanupTests
501 return
502
503
504
505
506
507
508
509
510
511
512
513
514