Debian Codegraph @

tests/format.test

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