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.
15 if {[lsearch [namespace children] ::tcltest] == -1} {
16 package require tcltest
17 namespace import -force ::tcltest::*
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
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"
31 test format-1.1 {integer formatting} {
32 format "%*d %d %d %d" 6 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}
38 # %u output depends on word length, so this test is not portable.
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
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} {
53 # Printing negative numbers in hex or octal format depends on word
54 # length, so these tests are not portable.
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 }
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
87 test format-2.6 {string formatting, international chars} {
88 format "%10s" abc\ufeffdef
90 test format-2.6 {string formatting, international chars} {
91 format "%.5s" abc\ufeffdef
93 test format-2.7 {string formatting, international chars} {
94 format "foo\ufeffbar%s" baz
96 test format-2.8 {string formatting, width} {
99 test format-2.8 {string formatting, width} {
102 test format-2.8 {string formatting, width} {
105 test format-2.8 {string formatting, width} {
108 test format-2.8 {string formatting, precision} {
109 format "a%.2sa" foobarbaz
111 test format-2.8 {string formatting, precision} {
112 format "a%.sa" foobarbaz
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
120 test format-2.8 {string formatting, width and precision} {
121 format "a%5.7sa" foobarbaz
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 |"
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}
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}
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
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} {eformat} {
183 format "%.3g" 12341.0
185 test format-5.2 {g-format} {eformat} {
186 format "%.3G" 1234.12345
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} {eformat} {
213 format "%.3g" .00001234
215 test format-5.12 {g-format} {eformat} {
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} {eformat} {
236 test format-5.19 {g-format} {eformat} {
237 format "%#.3g" 1234.0
239 test format-5.20 {g-format} {eformat} {
240 format "%#.3G" 9999.5
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
255 test format-6.5 {floating-point zeroes} {eformat} {
256 format "%#.0e %#.0f %#.0g" 0.0 0.0 0.0 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} {
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} {
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} {nonPortable} {
367 test format-10.2 {"h" format specifier} {nonPortable} {
370 test format-10.3 {"h" format specifier} {nonPortable} {
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} {
500 ::tcltest::cleanupTests