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
13if {"::tcltest" ni [namespace children]} {
14    package require tcltest 2.5
15    namespace import -force ::tcltest::*
16}
17
18# %u output depends on word length, so this test is not portable.
19testConstraint longIs32bit [expr {int(0x80000000) < 0}]
20testConstraint longIs64bit [expr {int(0x8000000000000000) < 0}]
21testConstraint wideIs64bit [expr {
22    (wide(0x80000000) > 0) && (wide(0x8000000000000000) < 0)}]
23testConstraint wideBiggerThanInt [expr {wide(0x80000000) != int(0x80000000)}]
24# MSVC uses a broken libc that gets sprintf("%g") wrong. This is a pain
25# particularly in Continuous Integration, and there isn't anything much we can
26# do about it.
27testConstraint notWinCI [expr {
28    ($::tcl_platform(platform) ne "windows") || ![info exists ::env(CI)]}]
29
30test format-1.1 {integer formatting} {
31    format "%*d %d %d %d" 6 34 16923 -12 -1
32} {    34 16923 -12 -1}
33test 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}
36test format-1.3 {integer formatting} longIs32bit {
37    format "%4u %4u %4u %4u %d %#o" 6 34 16923 -12 -1 0
38} {   6   34 16923 4294967284 -1 0}
39test format-1.3.1 {integer formatting} longIs64bit {
40    format "%4u %4u %4u %4u %d %#o" 6 34 16923 -12 -1 0
41} {   6   34 16923 18446744073709551604 -1 0}
42test format-1.4 {integer formatting} {
43    format "%-4d %-4i %-4d %-4ld" 6 34 16923 -12 -1
44} {6    34   16923 -12 }
45test format-1.5 {integer formatting} {
46    format "%04d %04d %04d %04i" 6 34 16923 -12 -1
47} {0006 0034 16923 -012}
48test format-1.6 {integer formatting} {
49    format "%00*d" 6 34
50} {000034}
51# Printing negative numbers in hex or octal format depends on word
52# length, so these tests are not portable.
53test format-1.7 {integer formatting} longIs32bit {
54    format "%4x %4x %4x %4x" 6 34 16923 -12 -1
55} {   6   22 421b fffffff4}
56test format-1.7.1 {integer formatting} longIs64bit {
57    format "%4x %4x %4x %4x" 6 34 16923 -12 -1
58} {   6   22 421b fffffffffffffff4}
59test format-1.8 {integer formatting} longIs32bit {
60    format "%#x %#x %#X %#X %#x" 0 6 34 16923 -12 -1
61} {0x0 0x6 0X22 0X421B 0xfffffff4}
62test format-1.8.1 {integer formatting} longIs64bit {
63    format "%#x %#x %#X %#X %#x" 0 6 34 16923 -12 -1
64} {0x0 0x6 0X22 0X421B 0xfffffffffffffff4}
65test format-1.9 {integer formatting} longIs32bit {
66    format "%#5x %#20x %#20x %#20x %#20x" 0 6 34 16923 -12 -1
67} {  0x0                  0x6                 0x22               0x421b           0xfffffff4}
68test format-1.9.1 {integer formatting} longIs64bit {
69    format "%#5x %#20x %#20x %#20x %#20x" 0 6 34 16923 -12 -1
70} {  0x0                  0x6                 0x22               0x421b   0xfffffffffffffff4}
71test format-1.10 {integer formatting} longIs32bit {
72    format "%-#5x %-#20x %-#20x %-#20x %-#20x" 0 6 34 16923 -12 -1
73} {0x0   0x6                  0x22                 0x421b               0xfffffff4          }
74test format-1.10.1 {integer formatting} longIs64bit {
75    format "%-#5x %-#20x %-#20x %-#20x %-#20x" 0 6 34 16923 -12 -1
76} {0x0   0x6                  0x22                 0x421b               0xfffffffffffffff4  }
77test format-1.11 {integer formatting} longIs32bit {
78    format "%-#5o %-#20o %#-20o %#-20o %#-20o" 0 6 34 16923 -12 -1
79} {0     06                   042                  041033               037777777764        }
80test format-1.11.1 {integer formatting} longIs64bit {
81    format "%-#5o %-#20o %#-20o %#-20o %#-20o" 0 6 34 16923 -12 -1
82} {0     06                   042                  041033               01777777777777777777764}
83test format-1.12 {integer formatting} {
84    format "%b %#b %#b %llb" 5 0 5 [expr {2**100}]
85} {101 0b0 0b101 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}
86test format-1.13 {integer formatting} longIs32bit {
87    format "%#d %#d %#d %#d %#d" 0 6 34 16923 -12 -1
88} {0 6 34 16923 -12}
89test format-1.13.1 {integer formatting} longIs64bit {
90    format "%#d %#d %#d %#d %#d" 0 6 34 16923 -12 -1
91} {0 6 34 16923 -12}
92test format-1.14 {integer formatting} longIs32bit {
93    format "%#5d %#20d %#20d %#20d %#20d" 0 6 34 16923 -12 -1
94} {    0                    6                   34                16923                  -12}
95test format-1.14.1 {integer formatting} longIs64bit {
96    format "%#5d %#20d %#20d %#20d %#20d" 0 6 34 16923 -12 -1
97} {    0                    6                   34                16923                  -12}
98test format-1.15 {integer formatting} longIs32bit {
99    format "%-#5d %-#20d %-#20d %-#20d %-#20d" 0 6 34 16923 -12 -1
100} {0     6                    34                   16923                -12                 }
101test format-1.15.1 {integer formatting} longIs64bit {
102    format "%-#5d %-#20d %-#20d %-#20d %-#20d" 0 6 34 16923 -12 -1
103} {0     6                    34                   16923                -12                 }
104
105
106test format-2.1 {string formatting} {
107    format "%s %s %c %s" abcd {This is a very long test string.} 120 x
108} {abcd This is a very long test string. x x}
109test format-2.2 {string formatting} {
110    format "%20s %20s %20c %20s" abcd {This is a very long test string.} 120 x
111} {                abcd This is a very long test string.                    x                    x}
112test format-2.3 {string formatting} {
113    format "%.10s %.10s %c %.10s" abcd {This is a very long test string.} 120 x
114} {abcd This is a  x x}
115test format-2.4 {string formatting} {
116    format "%s %s %% %c %s" abcd {This is a very long test string.} 120 x
117} {abcd This is a very long test string. % x x}
118test format-2.5 {string formatting, embedded nulls} {
119    format "%10s" abc\0def
120} "   abc\0def"
121test format-2.6 {string formatting, international chars} {
122    format "%10s" abc\ufeffdef
123} "   abc\ufeffdef"
124test format-2.7 {string formatting, international chars} {
125    format "%.5s" abc\ufeffdef
126} "abc\ufeffd"
127test format-2.8 {string formatting, international chars} {
128    format "foo\ufeffbar%s" baz
129} "foo\ufeffbarbaz"
130test format-2.9 {string formatting, width} {
131    format "a%5sa" f
132} "a    fa"
133test format-2.10 {string formatting, width} {
134    format "a%-5sa" f
135} "af    a"
136test format-2.11 {string formatting, width} {
137    format "a%2sa" foo
138} "afooa"
139test format-2.12 {string formatting, width} {
140    format "a%0sa" foo
141} "afooa"
142test format-2.13 {string formatting, precision} {
143    format "a%.2sa" foobarbaz
144} "afoa"
145test format-2.14 {string formatting, precision} {
146    format "a%.sa" foobarbaz
147} "aa"
148test format-2.15 {string formatting, precision} {
149    list [catch {format "a%.-2sa" foobarbaz} msg] $msg
150} {1 {bad field specifier "-"}}
151test format-2.16 {string formatting, width and precision} {
152    format "a%5.2sa" foobarbaz
153} "a   foa"
154test format-2.17 {string formatting, width and precision} {
155    format "a%5.7sa" foobarbaz
156} "afoobarba"
157
158test format-3.1 {Tcl_FormatObjCmd: character formatting} {
159    format "|%c|%0c|%-1c|%1c|%-6c|%6c|%*c|%*c|" 65 65 65 65 65 65 3 65 -4 65
160} "|A|A|A|A|A     |     A|  A|A   |"
161test format-3.2 {Tcl_FormatObjCmd: international character formatting} {
162    format "|%c|%0c|%-1c|%1c|%-6c|%6c|%*c|%*c|" 0xa2 0x4e4e 0x25a 0xc3 0xff08 0 3 0x6575 -4 0x4e4f
163} "|\ua2|\u4e4e|\u25a|\uc3|\uff08     |     \0|  \u6575|\u4e4f   |"
164
165test format-4.1 {e and f formats} {eformat} {
166    format "%e %e %e %e" 34.2e12 68.514 -.125 -16000. .000053
167} {3.420000e+13 6.851400e+01 -1.250000e-01 -1.600000e+04}
168test format-4.2 {e and f formats} {eformat} {
169    format "%20e %20e %20e %20e" 34.2e12 68.514 -.125 -16000. .000053
170} {        3.420000e+13         6.851400e+01        -1.250000e-01        -1.600000e+04}
171test format-4.3 {e and f formats} {eformat} {
172    format "%.1e %.1e %.1e %.1e" 34.2e12 68.514 -.126 -16000. .000053
173} {3.4e+13 6.9e+01 -1.3e-01 -1.6e+04}
174test format-4.4 {e and f formats} {eformat} {
175    format "%020e %020e %020e %020e" 34.2e12 68.514 -.126 -16000. .000053
176} {000000003.420000e+13 000000006.851400e+01 -00000001.260000e-01 -00000001.600000e+04}
177test format-4.5 {e and f formats} {eformat} {
178    format "%7.1e %7.1e %7.1e %7.1e" 34.2e12 68.514 -.126 -16000. .000053
179} {3.4e+13 6.9e+01 -1.3e-01 -1.6e+04}
180test format-4.6 {e and f formats} {
181    format "%f %f %f %f" 34.2e12 68.514 -.125 -16000. .000053
182} {34200000000000.000000 68.514000 -0.125000 -16000.000000}
183test format-4.7 {e and f formats} {
184    format "%.4f %.4f %.4f %.4f %.4f" 34.2e12 68.514 -.125 -16000. .000053
185} {34200000000000.0000 68.5140 -0.1250 -16000.0000 0.0001}
186test format-4.8 {e and f formats} {eformat} {
187    format "%.4e %.5e %.6e" -9.99996 -9.99996 9.99996
188} {-1.0000e+01 -9.99996e+00 9.999960e+00}
189test format-4.9 {e and f formats} {
190    format "%.4f %.5f %.6f" -9.99996 -9.99996 9.99996
191} {-10.0000 -9.99996 9.999960}
192test format-4.10 {e and f formats} {
193    format "%20f %-20f %020f" -9.99996 -9.99996 9.99996
194} {           -9.999960 -9.999960            0000000000009.999960}
195test format-4.11 {e and f formats} {
196    format "%-020f %020f" -9.99996 -9.99996 9.99996
197} {-9.999960            -000000000009.999960}
198test format-4.12 {e and f formats} {eformat} {
199    format "%.0e %#.0e" -9.99996 -9.99996 9.99996
200} {-1e+01 -1.e+01}
201test format-4.13 {e and f formats} {
202    format "%.0f %#.0f" -9.99996 -9.99996 9.99996
203} {-10 -10.}
204test format-4.14 {e and f formats} {
205    format "%.4f %.5f %.6f" -9.99996 -9.99996 9.99996
206} {-10.0000 -9.99996 9.999960}
207test format-4.15 {e and f formats} {
208    format "%3.0f %3.0f %3.0f %3.0f" 1.0 1.1 1.01 1.001
209} {  1   1   1   1}
210test format-4.16 {e and f formats} {
211    format "%3.1f %3.1f %3.1f %3.1f" 0.0 0.1 0.01 0.001
212} {0.0 0.1 0.0 0.0}
213
214test format-5.1 {g-format} {eformat} {
215    format "%.3g" 12341.0
216} {1.23e+04}
217test format-5.2 {g-format} {eformat} {
218    format "%.3G" 1234.12345
219} {1.23E+03}
220test format-5.3 {g-format} {
221    format "%.3g" 123.412345
222} {123}
223test format-5.4 {g-format} {
224    format "%.3g" 12.3412345
225} {12.3}
226test format-5.5 {g-format} {
227    format "%.3g" 1.23412345
228} {1.23}
229test format-5.6 {g-format} {
230    format "%.3g" 1.23412345
231} {1.23}
232test format-5.7 {g-format} {
233    format "%.3g" .123412345
234} {0.123}
235test format-5.8 {g-format} {
236    format "%.3g" .012341
237} {0.0123}
238test format-5.9 {g-format} {
239    format "%.3g" .0012341
240} {0.00123}
241test format-5.10 {g-format} {
242    format "%.3g" .00012341
243} {0.000123}
244test format-5.11 {g-format} {eformat} {
245    format "%.3g" .00001234
246} {1.23e-05}
247test format-5.12 {g-format} {eformat} {
248    format "%.4g" 9999.6
249} {1e+04}
250test format-5.13 {g-format} {
251    format "%.4g" 999.96
252} {1000}
253test format-5.14 {g-format} {
254    format "%.3g" 1.0
255} {1}
256test format-5.15 {g-format} {
257    format "%.3g" .1
258} {0.1}
259test format-5.16 {g-format} {
260    format "%.3g" .01
261} {0.01}
262test format-5.17 {g-format} {
263    format "%.3g" .001
264} {0.001}
265test format-5.18 {g-format} {eformat} {
266    format "%.3g" .00001
267} {1e-05}
268test format-5.19 {g-format} {eformat} {
269    format "%#.3g" 1234.0
270} {1.23e+03}
271test format-5.20 {g-format} {eformat} {
272    format "%#.3G" 9999.5
273} {1.00E+04}
274
275test format-6.1 {floating-point zeroes} {eformat} {
276    format "%e %f %g" 0.0 0.0 0.0 0.0
277} {0.000000e+00 0.000000 0}
278test format-6.2 {floating-point zeroes} {eformat} {
279    format "%.4e %.4f %.4g" 0.0 0.0 0.0 0.0
280} {0.0000e+00 0.0000 0}
281test format-6.3 {floating-point zeroes} {eformat notWinCI} {
282    format "%#.4e %#.4f %#.4g" 0.0 0.0 0.0 0.0
283} {0.0000e+00 0.0000 0.000}
284test format-6.4 {floating-point zeroes} {eformat} {
285    format "%.0e %.0f %.0g" 0.0 0.0 0.0 0.0
286} {0e+00 0 0}
287test format-6.5 {floating-point zeroes} {eformat notWinCI} {
288    format "%#.0e %#.0f %#.0g" 0.0 0.0 0.0 0.0
289} {0.e+00 0. 0.}
290test format-6.6 {floating-point zeroes} {
291    format "%3.0f %3.0f %3.0f %3.0f" 0.0 0.0 0.0 0.0
292} {  0   0   0   0}
293test format-6.7 {floating-point zeroes} {
294    format "%3.0f %3.0f %3.0f %3.0f" 1.0 1.1 1.01 1.001
295} {  1   1   1   1}
296test format-6.8 {floating-point zeroes} {
297    format "%3.1f %3.1f %3.1f %3.1f" 0.0 0.1 0.01 0.001
298} {0.0 0.1 0.0 0.0}
299
300test format-7.1 {various syntax features} {
301    format "%*.*f" 12 3 12.345678901
302} {      12.346}
303test format-7.2 {various syntax features} {
304    format "%0*.*f" 12 3 12.345678901
305} {00000012.346}
306test format-7.3 {various syntax features} {
307    format "\*\t\\n"
308} {*	\n}
309
310test format-8.1 {error conditions} {
311    catch format
312} 1
313test format-8.2 {error conditions} {
314    catch format msg
315    set msg
316} {wrong # args: should be "format formatString ?arg ...?"}
317test format-8.3 {error conditions} {
318    catch {format %*d}
319} 1
320test format-8.4 {error conditions} {
321    catch {format %*d} msg
322    set msg
323} {not enough arguments for all format specifiers}
324test format-8.5 {error conditions} {
325    catch {format %*.*f 12}
326} 1
327test format-8.6 {error conditions} {
328    catch {format %*.*f 12} msg
329    set msg
330} {not enough arguments for all format specifiers}
331test format-8.7 {error conditions} {
332    catch {format %*.*f 12 3}
333} 1
334test format-8.8 {error conditions} {
335    catch {format %*.*f 12 3} msg
336    set msg
337} {not enough arguments for all format specifiers}
338test format-8.9 {error conditions} {
339    list [catch {format %*d x 3} msg] $msg
340} {1 {expected integer but got "x"}}
341test format-8.10 {error conditions} {
342    list [catch {format %*.*f 2 xyz 3} msg] $msg
343} {1 {expected integer but got "xyz"}}
344test format-8.11 {error conditions} {
345    catch {format %d 2a}
346} 1
347test format-8.12 {error conditions} {
348    catch {format %d 2a} msg
349    set msg
350} {expected integer but got "2a"}
351test format-8.13 {error conditions} {
352    catch {format %c 2x}
353} 1
354test format-8.14 {error conditions} {
355    catch {format %c 2x} msg
356    set msg
357} {expected integer but got "2x"}
358test format-8.15 {error conditions} {
359    catch {format %f 2.1z}
360} 1
361test format-8.16 {error conditions} {
362    catch {format %f 2.1z} msg
363    set msg
364} {expected floating-point number but got "2.1z"}
365test format-8.17 {error conditions} {
366    catch {format ab%}
367} 1
368test format-8.18 {error conditions} {
369    catch {format ab% 12} msg
370    set msg
371} {format string ended in middle of field specifier}
372test format-8.19 {error conditions} {
373    catch {format %q x}
374} 1
375test format-8.20 {error conditions} {
376    catch {format %q x} msg
377    set msg
378} {bad field specifier "q"}
379test format-8.21 {error conditions} {
380    catch {format %d}
381} 1
382test format-8.22 {error conditions} {
383    catch {format %d} msg
384    set msg
385} {not enough arguments for all format specifiers}
386test format-8.23 {error conditions} {
387    catch {format "%d %d" 24 xyz} msg
388    set msg
389} {expected integer but got "xyz"}
390
391test format-9.1 {long result} {
392    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}
393    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
394} {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}
395
396test format-10.1 {"h" format specifier} {
397    format %hd 0xffff
398} -1
399test format-10.2 {"h" format specifier} {
400    format %hx 0x10fff
401} fff
402test format-10.3 {"h" format specifier} {
403    format %hd 0x10000
404} 0
405test format-10.4 {"h" format specifier} {
406    # Bug 1154163: This is minimal behaviour for %hx specifier!
407    format %hx 1
408} 1
409test format-10.5 {"h" format specifier} {
410    # Bug 1284178: Highly out-of-range values shouldn't cause errors
411    format %hu 0x100000000
412} 0
413
414test format-11.1 {XPG3 %$n specifiers} {
415    format {%2$d %1$d} 4 5
416} {5 4}
417test format-11.2 {XPG3 %$n specifiers} {
418    format {%2$d %1$d %1$d %3$d} 4 5 6
419} {5 4 4 6}
420test format-11.3 {XPG3 %$n specifiers} {
421    list [catch {format {%2$d %3$d} 4 5} msg] $msg
422} {1 {"%n$" argument index out of range}}
423test format-11.4 {XPG3 %$n specifiers} {
424    list [catch {format {%2$d %0$d} 4 5 6} msg] $msg
425} {1 {"%n$" argument index out of range}}
426test format-11.5 {XPG3 %$n specifiers} {
427    list [catch {format {%d %1$d} 4 5 6} msg] $msg
428} {1 {cannot mix "%" and "%n$" conversion specifiers}}
429test format-11.6 {XPG3 %$n specifiers} {
430    list [catch {format {%2$d %d} 4 5 6} msg] $msg
431} {1 {cannot mix "%" and "%n$" conversion specifiers}}
432test format-11.7 {XPG3 %$n specifiers} {
433    list [catch {format {%2$d %3d} 4 5 6} msg] $msg
434} {1 {cannot mix "%" and "%n$" conversion specifiers}}
435test format-11.8 {XPG3 %$n specifiers} {
436    format {%2$*d %3$d} 1 10 4
437} {         4 4}
438test format-11.9 {XPG3 %$n specifiers} {
439    format {%2$.*s %4$d} 1 5 abcdefghijklmnop 44
440} {abcde 44}
441test format-11.10 {XPG3 %$n specifiers} {
442    list [catch {format {%2$*d} 4} msg] $msg
443} {1 {"%n$" argument index out of range}}
444test format-11.11 {XPG3 %$n specifiers} {
445    list [catch {format {%2$*d} 4 5} msg] $msg
446} {1 {"%n$" argument index out of range}}
447test format-11.12 {XPG3 %$n specifiers} {
448    list [catch {format {%2$*d} 4 5 6} msg] $msg
449} {0 {    6}}
450
451test format-12.1 {negative width specifiers} {
452    format "%*d" -47 25
453} {25                                             }
454
455test format-13.1 {tcl_precision fuzzy comparison} {
456    catch {unset a}
457    catch {unset b}
458    catch {unset c}
459    catch {unset d}
460    set a 0.0000000000001
461    set b 0.00000000000001
462    set c 0.00000000000000001
463    set d [expr {$a + $b + $c}]
464    format {%0.10f %0.12f %0.15f %0.17f} $d $d $d $d
465} {0.0000000000 0.000000000000 0.000000000000110 0.00000000000011001}
466test format-13.2 {tcl_precision fuzzy comparison} {
467    catch {unset a}
468    catch {unset b}
469    catch {unset c}
470    catch {unset d}
471    set a 0.000000000001
472    set b 0.000000000000005
473    set c 0.0000000000000008
474    set d [expr {$a + $b + $c}]
475    format {%0.10f %0.12f %0.15f %0.17f} $d $d $d $d
476} {0.0000000000 0.000000000001 0.000000000001006 0.00000000000100580}
477test format-13.3 {tcl_precision fuzzy comparison} {
478    catch {unset a}
479    catch {unset b}
480    catch {unset c}
481    set a 0.00000000000099
482    set b 0.000000000000011
483    set c [expr {$a + $b}]
484    format {%0.10f %0.12f %0.15f %0.17f} $c $c $c $c
485} {0.0000000000 0.000000000001 0.000000000001001 0.00000000000100100}
486test format-13.4 {tcl_precision fuzzy comparison} {
487    catch {unset a}
488    catch {unset b}
489    catch {unset c}
490    set a 0.444444444444
491    set b 0.33333333333333
492    set c [expr {$a + $b}]
493    format {%0.10f %0.12f %0.15f %0.16f} $c $c $c $c
494} {0.7777777778 0.777777777777 0.777777777777330 0.7777777777773300}
495test format-13.5 {tcl_precision fuzzy comparison} {
496    catch {unset a}
497    catch {unset b}
498    catch {unset c}
499    set a 0.444444444444
500    set b 0.99999999999999
501    set c [expr {$a + $b}]
502    format {%0.10f %0.12f %0.15f} $c $c $c
503} {1.4444444444 1.444444444444 1.444444444443990}
504
505test format-14.1 {testing MAX_FLOAT_SIZE for 0 and 1} {
506    format {%s} ""
507} {}
508test format-14.2 {testing MAX_FLOAT_SIZE for 0 and 1} {
509    format {%s} "a"
510} {a}
511
512test format-15.1 {testing %0..s 0 padding for chars/strings} {
513    format %05s a
514} {0000a}
515test format-15.2 {testing %0..s 0 padding for chars/strings} {
516    format "% 5s" a
517} {    a}
518test format-15.3 {testing %0..s 0 padding for chars/strings} {
519    format %5s a
520} {    a}
521test format-15.4 {testing %0..s 0 padding for chars/strings} {
522    format %05c 61
523} {0000=}
524test format-15.5 {testing %d space padding for integers} {
525    format "(% 1d) (% 1d)" 10 -10
526} {( 10) (-10)}
527test format-15.6 {testing %d plus padding for integers} {
528    format "(%+1d) (%+1d)" 10 -10
529} {(+10) (-10)}
530
531set a "0123456789"
532set b ""
533for {set i 0} {$i < 290} {incr i} {
534    append b $a
535}
536for {set i 290} {$i < 400} {incr i} {
537    test format-16.[expr {$i -289}] {testing MAX_FLOAT_SIZE} {
538        format {%s} $b
539    } $b
540    append b "x"
541}
542
543test format-17.1 {testing %d with wide} {wideIs64bit wideBiggerThanInt} {
544    format %d 7810179016327718216
545} 1819043144
546test format-17.2 {testing %ld with wide} {wideIs64bit} {
547    format %ld 7810179016327718216
548} 7810179016327718216
549test format-17.3 {testing %ld with non-wide} {wideIs64bit} {
550    format %ld 42
551} 42
552test format-17.4 {testing %l with non-integer} {
553    format %lf 1
554} 1.000000
555test format-17.5 {testing %llu with positive bignum} -body {
556    format %llu 0xabcdef0123456789abcdef
557} -returnCodes 1 -result {unsigned bignum format is invalid}
558test format-17.6 {testing %llu with negative number} -body {
559    format %llu -1
560} -returnCodes 1 -result {unsigned bignum format is invalid}
561
562test format-18.1 {do not demote existing numeric values} {
563    set a 0xaaaaaaaa
564    # Ensure $a and $b are separate objects
565    set b 0xaaaa
566    append b aaaa
567    set result [expr {$a == $b}]
568    format %08lx $b
569    lappend result [expr {$a == $b}]
570    set b 0xaaaa
571    append b aaaa
572    lappend result [expr {$a == $b}]
573    format %08x $b
574    lappend result [expr {$a == $b}]
575} {1 1 1 1}
576test format-18.2 {do not demote existing numeric values} {wideBiggerThanInt} {
577    set a [expr {0xaaaaaaaaaa + 1}]
578    set b 0xaaaaaaaaab
579    list [format %08x $a] [expr {$a == $b}]
580} {aaaaaaab 1}
581
582test format-19.1 {regression test - tcl-core message by Brian Griffin on 26 0ctober 2004} -body {
583    set x 0x8fedc654
584    list [expr { ~ $x }] [format %08x [expr { ~$x }]]
585} -match regexp -result {-2414724693 f*701239ab}
586test format-19.2 {Bug 1867855} {
587    format %llx 0
588} 0
589test format-19.3 {Bug 2830354} {
590    string length [format %340f 0]
591} 340
592
593test format-19.4.1 {Bug d498578df4: width overflow should cause limit exceeded} \
594-constraints {longIs32bit} -body {
595    # in case of overflow into negative, it produces width -2 (and limit exceeded),
596    # in case of width will be unsigned, it will be outside limit (2GB for 32bit)...
597    # and it don't throw an error in case the bug is not fixed (and probably no segfault).
598    format %[expr {0xffffffff - 1}]g 0
599} -returnCodes error -result "max size for a Tcl value exceeded"
600
601test format-19.4.2 {Bug d498578df4: width overflow should cause limit exceeded} -body {
602    # limit should exceeds in any case,
603    # and it don't throw an error in case the bug is not fixed (and probably no segfault).
604    format %[expr {0xffffffffffffffff - 1}]g 0
605} -returnCodes error -result "max size for a Tcl value exceeded"
606
607# Note that this test may fail in future versions
608test format-20.1 {Bug 2932421: plain %s caused internalrep change of args} -body {
609    set x [dict create a b c d]
610    format %s $x
611    # After this, obj in $x should be a dict
612    # We are testing to make sure it has not been shimmered to a
613    # different internalrep when that is not necessary.
614    # Whether or not there is a string rep - we should not care!
615    tcl::unsupported::representation $x
616} -match glob -result {value is a dict *}
617
618# cleanup
619catch {unset a}
620catch {unset b}
621catch {unset c}
622catch {unset d}
623::tcltest::cleanupTests
624return
625
626# Local Variables:
627# mode: tcl
628# End:
629