1 # Commands covered: format
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.
7 # Copyright (c) 1991-1994 The Regents of the University of California.
8 # Copyright (c) 1994-1998 Sun Microsystems, Inc.
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 # RCS: @(#) $Id: format.test,v 1.8 2000/04/10 17:18:59 ericm Exp $
15 source [file dirname [info script]]/testing.tcl
19 # The following code is needed because some versions of SCO Unix have
20 # a round-off error in sprintf which would cause some of the tests to
21 # fail. Someday I hope this code shouldn't be necessary (code added
25 if {"[format %7.1e 68.514]" == "6.8e+01"} {
26 puts stdout "Note: this system has a sprintf round-off bug, some tests skipped\n"
30 test format-1.1 {integer formatting} {
31 format "%*d %d %d %d" 6 34 16923 -12 -1
33 test format-1.2 {integer formatting} {
34 format "%4d %4d %4d %4d %d %#x %#X" 6 34 16923 -12 -1 14 12
35 } { 6 34 16923 -12 -1 0xe 0XC}
37 # %u output depends on word length, so this test is not portable.
39 test format-1.3 {integer formatting} {
40 format "%4u %4u %4u %4u %d %#o" 6 34 16923 -12 -1 0
41 } { 6 34 16923 18446744073709551604 -1 0}
42 test format-1.4 {integer formatting} {
43 format "%-4d %-4i %-4d %-4ld" 6 34 16923 -12 -1
45 test format-1.5 {integer formatting} {
46 format "%04d %04d %04d %04i" 6 34 16923 -12 -1
47 } {0006 0034 16923 -012}
48 test format-1.6 {integer formatting} {
52 # Printing negative numbers in hex or octal format depends on word
53 # length, so these tests are not portable.
55 test format-1.7 {integer formatting} {
56 format "%4x %4x %4x %4x" 6 34 16923 -12 -1
57 } { 6 22 421b fffffffffffffff4}
58 test format-1.8 {integer formatting} {
59 format "%#x %#X %#X %#x" 6 34 16923 -12 -1
60 } {0x6 0X22 0X421B 0xfffffffffffffff4}
61 test format-1.9 {integer formatting} {
62 format "%#20x %#20x %#20x %#20x" 6 34 16923 -12 -1
63 } { 0x6 0x22 0x421b 0xfffffffffffffff4}
64 test format-1.10 {integer formatting} {
65 format "%-#20x %-#20x %-#20x %-#20x" 6 34 16923 -12 -1
66 } {0x6 0x22 0x421b 0xfffffffffffffff4 }
67 test format-1.11 {integer formatting} {
68 format "%-#20o %#-20o %#-20o %#-20o" 6 34 16923 -12 -1
69 } {06 042 041033 01777777777777777777764}
70 test format-1.12 {no sign extend large 32 bit values} {
74 test format-2.1 {string formatting} {
75 format "%s %s %c %s" abcd {This is a very long test string.} 120 x
76 } {abcd This is a very long test string. x x}
77 test format-2.2 {string formatting} {
78 format "%20s %20s %20c %20s" abcd {This is a very long test string.} 120 x
79 } { abcd This is a very long test string. x x}
80 test format-2.3 {string formatting} {
81 format "%.10s %.10s %c %.10s" abcd {This is a very long test string.} 120 x
82 } {abcd This is a x x}
83 test format-2.4 {string formatting} {
84 format "%s %s %% %c %s" abcd {This is a very long test string.} 120 x
85 } {abcd This is a very long test string. % x x}
86 test format-2.5 {string formatting, embedded nulls} {
87 format "%10s" abc\0def
89 test format-2.6 {string formatting, international chars} utf8 {
90 format "%10s" abc\ufeffdef
92 test format-2.6 {string formatting, international chars} utf8 {
93 format "%.5s" abc\ufeffdef
95 test format-2.7 {string formatting, international chars} {
96 format "foo\ufeffbar%s" baz
98 test format-2.8 {string formatting, width} {
101 test format-2.8 {string formatting, width} {
104 test format-2.8 {string formatting, width} {
107 test format-2.8 {string formatting, width} {
110 test format-2.8 {string formatting, precision} {
111 format "a%.2sa" foobarbaz
113 test format-2.8 {string formatting, precision} {
114 format "a%.sa" foobarbaz
116 test format-2.8 {string formatting, precision} {
117 list [catch {format "a%.-2sa" foobarbaz} msg] $msg
118 } {1 {bad field specifier "-"}}
119 test format-2.8 {string formatting, width and precision} {
120 format "a%5.2sa" foobarbaz
122 test format-2.8 {string formatting, width and precision} {
123 format "a%5.7sa" foobarbaz
125 test format-3.1 {Tcl_FormatObjCmd: character formatting} utf8 {
126 format "|%c|%0c|%-1c|%1c|%-6c|%6c|%*c|%*c|" 65 65 65 65 65 65 3 65 -4 65
127 } "|A|A|A|A|A | A| A|A |"
128 test format-3.2 {Tcl_FormatObjCmd: international character formatting} utf8 {
129 format "|%c|%0c|%-1c|%1c|%-6c|%6c|%*c|%*c|" 0xa2 0x4e4e 0x25a 0xc3 0xff08 0 3 0x6575 -4 0x4e4f
130 } "|\ua2|\u4e4e|\u25a|\uc3|\uff08 | \0| \u6575|\u4e4f |"
131 test format-4.1 {e and f formats} -body {
132 format "%e %e %e %e" 34.2e12 68.514 -.125 -16000. .000053
133 } -match regexp -result {3.420000e\+0?13 6.851400e\+0?01 -1.250000e-0?01 -1.600000e\+0?04}
134 test format-4.2 {e and f formats} -body {
135 format "%20e %20e %20e %20e" 34.2e12 68.514 -.125 -16000. .000053
136 } -match regexp -result {( 3.420000e\+13 6.851400e\+01 -1.250000e-01 -1.600000e\+04| 3.420000e\+013 6.851400e\+001 -1.250000e-001 -1.600000e\+004)}
138 test format-4.3 {e and f formats} -body {
139 format "%.1e %.1e %.1e %.1e" 34.2e12 68.514 -.126 -16000. .000053
140 } -match regexp -result {3.4e\+0?13 6.9e\+0?01 -1.3e-0?01 -1.6e\+0?04}
141 test format-4.4 {e and f formats} -body {
142 format "%020e %020e %020e %020e" 34.2e12 68.514 -.126 -16000. .000053
143 } -match regexp -result {(000000003.420000e\+13 000000006.851400e\+01 -00000001.260000e-01 -00000001.600000e\+04|00000003.420000e\+013 00000006.851400e\+001 -0000001.260000e-001 -0000001.600000e\+004)}
145 test format-4.5 {e and f formats} -body {
146 format "%7.1e %7.1e %7.1e %7.1e" 34.2e12 68.514 -.126 -16000. .000053
147 } -match regexp -result {3.4e\+0?13 6.9e\+0?01 -1.3e-0?01 -1.6e\+0?04}
148 test format-4.6 {e and f formats} {
149 format "%f %f %f %f" 34.2e12 68.514 -.125 -16000. .000053
150 } {34200000000000.000000 68.514000 -0.125000 -16000.000000}
151 test format-4.7 {e and f formats} {
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} -body {
155 format "%.4e %.5e %.6e" -9.99996 -9.99996 9.99996
156 } -match regexp -result {\-1.0000e\+0?01 -9.99996e\+0?00 9.999960e\+0?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} -body {
167 format "%.0e %#.0e" -9.99996 -9.99996 9.99996
168 } -match regexp -result {\-1e\+0?01 -1.e\+0?01}
169 test format-4.13 {e and f formats} {
170 format "%.0f %#.0f" -9.99996 -9.99996 9.99996
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
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
182 test format-5.1 {g-format} -body {
183 format "%.3g" 12341.0
184 } -match regexp -result {1.23e\+0?04}
185 test format-5.2 {g-format} -body {
186 format "%.3G" 1234.12345
187 } -match regexp -result {1.23E\+0?03}
188 test format-5.3 {g-format} {
189 format "%.3g" 123.412345
191 test format-5.4 {g-format} {
192 format "%.3g" 12.3412345
194 test format-5.5 {g-format} {
195 format "%.3g" 1.23412345
197 test format-5.6 {g-format} {
198 format "%.3g" 1.23412345
200 test format-5.7 {g-format} {
201 format "%.3g" .123412345
203 test format-5.8 {g-format} {
204 format "%.3g" .012341
206 test format-5.9 {g-format} {
207 format "%.3g" .0012341
209 test format-5.10 {g-format} {
210 format "%.3g" .00012341
212 test format-5.11 {g-format} -body {
213 format "%.3g" .00001234
214 } -match regexp -result {1.23e-0?05}
215 test format-5.12 {g-format} -body {
217 } -match regexp -result {1e\+0?04}
218 test format-5.13 {g-format} {
221 test format-5.14 {g-format} {
224 test format-5.15 {g-format} {
227 test format-5.16 {g-format} {
230 test format-5.17 {g-format} {
233 test format-5.18 {g-format} -body {
235 } -match regexp -result {1e-0?05}
236 test format-5.19 {g-format} -body {
237 format "%#.3g" 1234.0
238 } -match regexp -result {1.23e\+0?03}
239 test format-5.20 {g-format} -body {
240 format "%#.3G" 9999.5
241 } -match regexp -result {1.00E\+0?04}
243 test format-6.1 {floating-point zeroes} -body {
244 format "%e %f %g" 0.0 0.0 0.0 0.0
245 } -match regexp -result {0.000000e\+0?00 0.000000 0}
246 test format-6.2 {floating-point zeroes} -body {
247 format "%.4e %.4f %.4g" 0.0 0.0 0.0 0.0
248 } -match regexp -result {0.0000e\+0?00 0.0000 0}
249 test format-6.3 {floating-point zeroes} -body {
250 format "%#.4e %#.4f %#.4g" 0.0 0.0 0.0 0.0
251 } -match regexp -result {0.0000e\+0?00 0.0000 0.000}
252 test format-6.4 {floating-point zeroes} -body {
253 format "%.0e %.0f %.0g" 0.0 0.0 0.0 0.0
254 } -match regexp -result {0e\+0?00 0 0}
255 test format-6.5 {floating-point zeroes} -body {
256 format "%#.0e %#.0f %#.0g" 0.0 0.0 0.0 0.0
257 } -match regexp -result {0.e\+0?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
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
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
268 test format-7.1 {various syntax features} {
269 format "%*.*f" 12 3 12.345678901
271 test format-7.2 {various syntax features} {
272 format "%0*.*f" 12 3 12.345678901
274 test format-7.3 {various syntax features} {
278 test format-8.1 {error conditions} {
281 test format-8.2 {error conditions} jim {
284 } {wrong # args: should be "format formatString ?arg arg ...?"}
285 test format-8.3 {error conditions} {
288 test format-8.4 {error conditions} {
289 catch {format %*d} msg
291 } {not enough arguments for all format specifiers}
292 test format-8.5 {error conditions} {
293 catch {format %*.*f 12}
295 test format-8.6 {error conditions} {
296 catch {format %*.*f 12} msg
298 } {not enough arguments for all format specifiers}
299 test format-8.7 {error conditions} {
300 catch {format %*.*f 12 3}
302 test format-8.8 {error conditions} {
303 catch {format %*.*f 12 3} 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} {
315 test format-8.12 {error conditions} {
316 catch {format %d 2a} msg
318 } {expected integer but got "2a"}
319 test format-8.13 {error conditions} {
322 test format-8.14 {error conditions} {
323 catch {format %c 2x} msg
325 } {expected integer but got "2x"}
326 test format-8.15 {error conditions} {
327 catch {format %f 2.1z}
329 test format-8.16 {error conditions} jim {
330 catch {format %f 2.1z} msg
332 } {expected floating-point number but got "2.1z"}
333 test format-8.17 {error conditions} {
336 test format-8.18 {error conditions} {
337 catch {format ab% 12} msg
339 } {format string ended in middle of field specifier}
340 test format-8.19 {error conditions} {
343 test format-8.20 {error conditions} {
344 catch {format %q x} msg
346 } {bad field specifier "q"}
347 test format-8.21 {error conditions} {
350 test format-8.22 {error conditions} {
351 catch {format %d} msg
353 } {not enough arguments for all format specifiers}
354 test format-8.23 {error conditions} {
355 catch {format "%d %d" 24 xyz} msg
357 } {expected integer but got "xyz"}
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}
364 test format-10.1 {"h" format specifier} {
367 test format-10.2 {"h" format specifier} {
370 test format-10.3 {"h" format specifier} {
374 test format-11.1 {XPG3 %$n specifiers} {
375 format {%2$d %1$d} 4 5
377 test format-11.2 {XPG3 %$n specifiers} {
378 format {%2$d %1$d %1$d %3$d} 4 5 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
398 test format-11.9 {XPG3 %$n specifiers} {
399 format {%2$.*s %4$d} 1 5 abcdefghijklmnop 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
411 test format-12.1 {negative width specifiers} {
414 test format-13.1 {tcl_precision fuzzy comparison} {
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} {
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} {
440 set a 0.00000000000099
441 set b 0.000000000000011
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} {
450 set b 0.33333333333333
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} {
459 set b 0.99999999999999
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} {
466 test format-14.2 {testing MAX_FLOAT_SIZE for 0 and 1} {
470 test format-15.1 {testing %0..s 0 padding for chars/strings} {
473 test format-15.2 {testing %0..s 0 padding for chars/strings} {
476 test format-15.3 {testing %0..s 0 padding for chars/strings} {
479 test format-15.4 {testing %0..s 0 padding for chars/strings} {
485 for {set i 0} {$i < 290} {incr i} {
488 for {set i 290} {$i < 400} {incr i} {
489 test format-15.[expr $i -290] {testing MAX_FLOAT_SIZE} {
495 test format-16.1 {format %b} {
499 test format-16.2 {format %b} {
503 test format-16.3 {format %b} {
507 test format-16.4 {format %b} {
509 } {100101101011010000111}