Debian Codegraph @

tests/scan.test

raw
1 # Commands covered: scan
2 #
3 # This file contains a collection of tests for one or more of the Tcl built-in
4 # commands. Sourcing this file into Tcl runs the tests and generates output
5 # 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-1997 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 #
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
14 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18
19 # procedure that returns the range of integers
20
21 proc int_range {} {
22 for { set MIN_INT 1 } { int($MIN_INT) > 0 } {} {
23 set MIN_INT [expr { $MIN_INT << 1 }]
24 }
25 set MIN_INT [expr {int($MIN_INT)}]
26 set MAX_INT [expr { ~ $MIN_INT }]
27 return [list $MIN_INT $MAX_INT]
28 }
29
30 # Big test for correct ordering of data in [expr]
31
32 proc testIEEE {} {
33 variable ieeeValues
34 binary scan [binary format dd -1.0 1.0] c* c
35 switch -exact -- $c {
36 {0 0 0 0 0 0 -16 -65 0 0 0 0 0 0 -16 63} {
37 # little endian
38 binary scan \x00\x00\x00\x00\x00\x00\xf0\xff d \
39 ieeeValues(-Infinity)
40 binary scan \x00\x00\x00\x00\x00\x00\xf0\xbf d \
41 ieeeValues(-Normal)
42 binary scan \x00\x00\x00\x00\x00\x00\x08\x80 d \
43 ieeeValues(-Subnormal)
44 binary scan \x00\x00\x00\x00\x00\x00\x00\x80 d \
45 ieeeValues(-0)
46 binary scan \x00\x00\x00\x00\x00\x00\x00\x00 d \
47 ieeeValues(+0)
48 binary scan \x00\x00\x00\x00\x00\x00\x08\x00 d \
49 ieeeValues(+Subnormal)
50 binary scan \x00\x00\x00\x00\x00\x00\xf0\x3f d \
51 ieeeValues(+Normal)
52 binary scan \x00\x00\x00\x00\x00\x00\xf0\x7f d \
53 ieeeValues(+Infinity)
54 binary scan \x00\x00\x00\x00\x00\x00\xf8\x7f d \
55 ieeeValues(NaN)
56 set ieeeValues(littleEndian) 1
57 return 1
58 }
59 {-65 -16 0 0 0 0 0 0 63 -16 0 0 0 0 0 0} {
60 binary scan \xff\xf0\x00\x00\x00\x00\x00\x00 d \
61 ieeeValues(-Infinity)
62 binary scan \xbf\xf0\x00\x00\x00\x00\x00\x00 d \
63 ieeeValues(-Normal)
64 binary scan \x80\x08\x00\x00\x00\x00\x00\x00 d \
65 ieeeValues(-Subnormal)
66 binary scan \x80\x00\x00\x00\x00\x00\x00\x00 d \
67 ieeeValues(-0)
68 binary scan \x00\x00\x00\x00\x00\x00\x00\x00 d \
69 ieeeValues(+0)
70 binary scan \x00\x08\x00\x00\x00\x00\x00\x00 d \
71 ieeeValues(+Subnormal)
72 binary scan \x3f\xf0\x00\x00\x00\x00\x00\x00 d \
73 ieeeValues(+Normal)
74 binary scan \x7f\xf0\x00\x00\x00\x00\x00\x00 d \
75 ieeeValues(+Infinity)
76 binary scan \x7f\xf8\x00\x00\x00\x00\x00\x00 d \
77 ieeeValues(NaN)
78 set ieeeValues(littleEndian) 0
79 return 1
80 }
81 default {
82 return 0
83 }
84 }
85 }
86
87 testConstraint ieeeFloatingPoint [testIEEE]
88 testConstraint wideIs64bit \
89 [expr {(wide(0x80000000) > 0) && (wide(0x8000000000000000) < 0)}]
90
91 test scan-1.1 {BuildCharSet, CharInSet} {
92 list [scan foo {%[^o]} x] $x
93 } {1 f}
94 test scan-1.2 {BuildCharSet, CharInSet} {
95 list [scan \]foo {%[]f]} x] $x
96 } {1 \]f}
97 test scan-1.3 {BuildCharSet, CharInSet} {
98 list [scan abc-def {%[a-c]} x] $x
99 } {1 abc}
100 test scan-1.4 {BuildCharSet, CharInSet} {
101 list [scan abc-def {%[a-c]} x] $x
102 } {1 abc}
103 test scan-1.5 {BuildCharSet, CharInSet} {
104 list [scan -abc-def {%[-ac]} x] $x
105 } {1 -a}
106 test scan-1.6 {BuildCharSet, CharInSet} {
107 list [scan -abc-def {%[ac-]} x] $x
108 } {1 -a}
109 test scan-1.7 {BuildCharSet, CharInSet} {
110 list [scan abc-def {%[c-a]} x] $x
111 } {1 abc}
112 test scan-1.8 {BuildCharSet, CharInSet} {
113 list [scan def-abc {%[^c-a]} x] $x
114 } {1 def-}
115 test scan-1.9 {BuildCharSet, CharInSet no match} -setup {
116 unset -nocomplain x
117 } -body {
118 list [scan {= f} {= %[TF]} x] [info exists x]
119 } -result {0 0}
120
121 test scan-2.1 {ReleaseCharSet} {
122 list [scan abcde {%[abc]} x] $x
123 } {1 abc}
124 test scan-2.2 {ReleaseCharSet} {
125 list [scan abcde {%[a-c]} x] $x
126 } {1 abc}
127
128 test scan-3.1 {ValidateFormat} -returnCodes error -body {
129 scan {} {%d%1$d} x
130 } -result {cannot mix "%" and "%n$" conversion specifiers}
131 test scan-3.2 {ValidateFormat} -returnCodes error -body {
132 scan {} {%d%1$d} x
133 } -result {cannot mix "%" and "%n$" conversion specifiers}
134 test scan-3.3 {ValidateFormat} -returnCodes error -body {
135 scan {} {%2$d%d} x
136 } -result {"%n$" argument index out of range}
137 test scan-3.4 {ValidateFormat} {
138 # degenerate case, before changed from 8.2 to 8.3
139 list [catch {scan {} %d} msg] $msg
140 } {0 {}}
141 test scan-3.5 {ValidateFormat} -returnCodes error -body {
142 scan {} {%10c} a
143 } -result {field width may not be specified in %c conversion}
144 test scan-3.6 {ValidateFormat} -returnCodes error -body {
145 scan {} {%*1$d} a
146 } -result {bad scan conversion character "$"}
147 test scan-3.7 {ValidateFormat} -returnCodes error -body {
148 scan {} {%1$d%1$d} a
149 } -result {variable is assigned by multiple "%n$" conversion specifiers}
150 test scan-3.8 {ValidateFormat} -returnCodes error -body {
151 scan {} a x
152 } -result {variable is not assigned by any conversion specifiers}
153 test scan-3.9 {ValidateFormat} -returnCodes error -body {
154 scan {} {%2$s} x y
155 } -result {variable is not assigned by any conversion specifiers}
156 test scan-3.10 {ValidateFormat} -returnCodes error -body {
157 scan {} {%[a} x
158 } -result {unmatched [ in format string}
159 test scan-3.11 {ValidateFormat} -returnCodes error -body {
160 scan {} {%[^a} x
161 } -result {unmatched [ in format string}
162 test scan-3.12 {ValidateFormat} -returnCodes error -body {
163 scan {} {%[]a} x
164 } -result {unmatched [ in format string}
165 test scan-3.13 {ValidateFormat} -returnCodes error -body {
166 scan {} {%[^]a} x
167 } -result {unmatched [ in format string}
168
169 test scan-4.1 {Tcl_ScanObjCmd, argument checks} -returnCodes error -body {
170 scan
171 } -result {wrong # args: should be "scan string format ?varName ...?"}
172 test scan-4.2 {Tcl_ScanObjCmd, argument checks} -returnCodes error -body {
173 scan string
174 } -result {wrong # args: should be "scan string format ?varName ...?"}
175 test scan-4.3 {Tcl_ScanObjCmd, argument checks} {
176 # degenerate case, before changed from 8.2 to 8.3
177 list [catch {scan string format} msg] $msg
178 } {0 {}}
179 test scan-4.4 {Tcl_ScanObjCmd, whitespace} {
180 list [scan { abc def } {%s%s} x y] $x $y
181 } {2 abc def}
182 test scan-4.5 {Tcl_ScanObjCmd, whitespace} {
183 list [scan { abc def } { %s %s } x y] $x $y
184 } {2 abc def}
185 test scan-4.6 {Tcl_ScanObjCmd, whitespace} {
186 list [scan { abc def } { %s %s } x y] $x $y
187 } {2 abc def}
188 test scan-4.7 {Tcl_ScanObjCmd, literals} {
189 # degenerate case, before changed from 8.2 to 8.3
190 scan { abc def } { abc def }
191 } {}
192 test scan-4.8 {Tcl_ScanObjCmd, literals} {
193 set x {}
194 list [scan { abcg} { abc def %1s} x] $x
195 } {0 {}}
196 test scan-4.9 {Tcl_ScanObjCmd, literals} {
197 list [scan { abc%defghi} { abc %% def%n } x] $x
198 } {1 10}
199 test scan-4.10 {Tcl_ScanObjCmd, assignment suppression} {
200 list [scan { abc def } { %*c%s def } x] $x
201 } {1 bc}
202 test scan-4.11 {Tcl_ScanObjCmd, XPG3-style} {
203 list [scan { abc def } {%2$s %1$s} x y] $x $y
204 } {2 def abc}
205 test scan-4.12 {Tcl_ScanObjCmd, width specifiers} {
206 list [scan {abc123456789012} {%3s%3d%3f%3[0-9]%s} a b c d e] $a $b $c $d $e
207 } {5 abc 123 456.0 789 012}
208 test scan-4.13 {Tcl_ScanObjCmd, width specifiers} {
209 list [scan {abc123456789012} {%3s%3d%3f%3[0-9]%s} a b c d e] $a $b $c $d $e
210 } {5 abc 123 456.0 789 012}
211 test scan-4.14 {Tcl_ScanObjCmd, underflow} {
212 set x {}
213 list [scan {a} {a%d} x] $x
214 } {-1 {}}
215 test scan-4.15 {Tcl_ScanObjCmd, underflow} {
216 set x {}
217 list [scan {} {a%d} x] $x
218 } {-1 {}}
219 test scan-4.16 {Tcl_ScanObjCmd, underflow} {
220 set x {}
221 list [scan {ab} {a%d} x] $x
222 } {0 {}}
223 test scan-4.17 {Tcl_ScanObjCmd, underflow} {
224 set x {}
225 list [scan {a } {a%d} x] $x
226 } {-1 {}}
227 test scan-4.18 {Tcl_ScanObjCmd, skipping whitespace} {
228 list [scan { b} {%c%s} x y] $x $y
229 } {2 32 b}
230 test scan-4.19 {Tcl_ScanObjCmd, skipping whitespace} {
231 list [scan { b} {%[^b]%s} x y] $x $y
232 } {2 { } b}
233 test scan-4.20 {Tcl_ScanObjCmd, string scanning} {
234 list [scan {abc def} {%s} x] $x
235 } {1 abc}
236 test scan-4.21 {Tcl_ScanObjCmd, string scanning} {
237 list [scan {abc def} {%0s} x] $x
238 } {1 abc}
239 test scan-4.22 {Tcl_ScanObjCmd, string scanning} {
240 list [scan {abc def} {%2s} x] $x
241 } {1 ab}
242 test scan-4.23 {Tcl_ScanObjCmd, string scanning} {
243 list [scan {abc def} {%*s%n} x] $x
244 } {1 3}
245 test scan-4.24 {Tcl_ScanObjCmd, charset scanning} {
246 list [scan {abcdef} {%[a-c]} x] $x
247 } {1 abc}
248 test scan-4.25 {Tcl_ScanObjCmd, charset scanning} {
249 list [scan {abcdef} {%0[a-c]} x] $x
250 } {1 abc}
251 test scan-4.26 {Tcl_ScanObjCmd, charset scanning} {
252 list [scan {abcdef} {%2[a-c]} x] $x
253 } {1 ab}
254 test scan-4.27 {Tcl_ScanObjCmd, charset scanning} {
255 list [scan {abcdef} {%*[a-c]%n} x] $x
256 } {1 3}
257 test scan-4.28 {Tcl_ScanObjCmd, character scanning} {
258 list [scan {abcdef} {%c} x] $x
259 } {1 97}
260 test scan-4.29 {Tcl_ScanObjCmd, character scanning} {
261 list [scan {abcdef} {%*c%n} x] $x
262 } {1 1}
263
264 test scan-4.30 {Tcl_ScanObjCmd, base-10 integer scanning} -setup {
265 set x {}
266 } -body {
267 list [scan {1234567890a} {%3d} x] $x
268 } -result {1 123}
269 test scan-4.31 {Tcl_ScanObjCmd, base-10 integer scanning} -setup {
270 set x {}
271 } -body {
272 list [scan {1234567890a} {%d} x] $x
273 } -result {1 1234567890}
274 test scan-4.32 {Tcl_ScanObjCmd, base-10 integer scanning} -setup {
275 set x {}
276 } -body {
277 list [scan {01234567890a} {%d} x] $x
278 } -result {1 1234567890}
279 test scan-4.33 {Tcl_ScanObjCmd, base-10 integer scanning} -setup {
280 set x {}
281 } -body {
282 list [scan {+01234} {%d} x] $x
283 } -result {1 1234}
284 test scan-4.34 {Tcl_ScanObjCmd, base-10 integer scanning} -setup {
285 set x {}
286 } -body {
287 list [scan {-01234} {%d} x] $x
288 } -result {1 -1234}
289 test scan-4.35 {Tcl_ScanObjCmd, base-10 integer scanning} -setup {
290 set x {}
291 } -body {
292 list [scan {a01234} {%d} x] $x
293 } -result {0 {}}
294 test scan-4.36 {Tcl_ScanObjCmd, base-10 integer scanning} -setup {
295 set x {}
296 } -body {
297 list [scan {0x10} {%d} x] $x
298 } -result {1 0}
299 test scan-4.37 {Tcl_ScanObjCmd, base-8 integer scanning} -setup {
300 set x {}
301 } -body {
302 list [scan {012345678} {%o} x] $x
303 } -result {1 342391}
304 test scan-4.38 {Tcl_ScanObjCmd, base-8 integer scanning} -setup {
305 set x {}
306 } -body {
307 list [scan {+1238 -1239 123a} {%o%*s%o%*s%o} x y z] $x $y $z
308 } -result {3 83 -83 83}
309 test scan-4.39 {Tcl_ScanObjCmd, base-16 integer scanning} -setup {
310 set x {}
311 } -body {
312 list [scan {+1238 -123a 0123} {%x%x%x} x y z] $x $y $z
313 } -result {3 4664 -4666 291}
314 test scan-4.40 {Tcl_ScanObjCmd, base-16 integer scanning} -setup {
315 set x {}
316 } -body {
317 # The behavior changed in 8.4a4/8.3.4cvs (6 Feb) to correctly
318 # return '1' for 0x1 scanned via %x, to comply with 8.0 and C scanf.
319 # Bug #495213
320 list [scan {aBcDeF AbCdEf 0x1} {%x%x%x} x y z] $x $y $z
321 } -result {3 11259375 11259375 1}
322 test scan-4.40.1 {Tcl_ScanObjCmd, base-16 integer scanning} -setup {
323 set x {}
324 } -body {
325 list [scan {0xF 0x00A0B 0X0XF} {%x %x %x} x y z] $x $y $z
326 } -result {3 15 2571 0}
327 test scan-4.40.2 {Tcl_ScanObjCmd, base-16 integer scanning} -setup {
328 unset -nocomplain x
329 } -body {
330 list [scan {xF} {%x} x] [info exists x]
331 } -result {0 0}
332 test scan-4.40.3 {Tcl_ScanObjCmd, base-2 integer scanning} -setup {
333 set x {}
334 } -body {
335 list [scan {1001 0b101 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000} {%b %b %llb} x y z] $x $y $z
336 } -result {3 9 5 340282366920938463463374607431768211456}
337 test scan-4.41 {Tcl_ScanObjCmd, base-unknown integer scanning} -setup {
338 set x {}
339 } -body {
340 list [scan {10 010 0x10 0b10} {%i%i%i%i} x y z t] $x $y $z $t
341 } -result {4 10 8 16 0}
342 test scan-4.42 {Tcl_ScanObjCmd, base-unknown integer scanning} -setup {
343 set x {}
344 } -body {
345 list [scan {10 010 0X10} {%i%i%i} x y z] $x $y $z
346 } -result {3 10 8 16}
347 test scan-4.43 {Tcl_ScanObjCmd, integer scanning, odd cases} -setup {
348 set x {}
349 } -body {
350 list [scan {+ } {%i} x] $x
351 } -result {0 {}}
352 test scan-4.44 {Tcl_ScanObjCmd, integer scanning, odd cases} -setup {
353 set x {}
354 } -body {
355 list [scan {+} {%i} x] $x
356 } -result {-1 {}}
357 test scan-4.45 {Tcl_ScanObjCmd, integer scanning, odd cases} -setup {
358 set x {}
359 } -body {
360 list [scan {0x} {%i%s} x y] $x $y
361 } -result {2 0 x}
362 test scan-4.46 {Tcl_ScanObjCmd, integer scanning, odd cases} -setup {
363 set x {}
364 } -body {
365 list [scan {0X} {%i%s} x y] $x $y
366 } -result {2 0 X}
367 test scan-4.47 {Tcl_ScanObjCmd, integer scanning, suppressed} -setup {
368 set x {}
369 } -body {
370 list [scan {123def} {%*i%s} x] $x
371 } -result {1 def}
372 test scan-4.48 {Tcl_ScanObjCmd, float scanning} {
373 list [scan {1 2 3} {%e %f %g} x y z] $x $y $z
374 } {3 1.0 2.0 3.0}
375 test scan-4.49 {Tcl_ScanObjCmd, float scanning} {
376 list [scan {.1 0.2 3.} {%e %f %g} x y z] $x $y $z
377 } {3 0.1 0.2 3.0}
378 test scan-4.49-uc-1 {Tcl_ScanObjCmd, float scanning} {
379 list [scan {0.5*0.75} {%E%c%G} x y z] $x $y $z
380 } {3 0.5 42 0.75}
381 test scan-4.49-uc-2 {Tcl_ScanObjCmd, float scanning} {
382 list [scan {5e-1*75E-2} {%E%c%G} x y z] $x $y $z
383 } {3 0.5 42 0.75}
384 test scan-4.50 {Tcl_ScanObjCmd, float scanning} {
385 list [scan {1234567890a} %f x] $x
386 } {1 1234567890.0}
387 test scan-4.51 {Tcl_ScanObjCmd, float scanning} {
388 list [scan {+123+45} %f x] $x
389 } {1 123.0}
390 test scan-4.52 {Tcl_ScanObjCmd, float scanning} {
391 list [scan {-123+45} %f x] $x
392 } {1 -123.0}
393 test scan-4.53 {Tcl_ScanObjCmd, float scanning} {
394 list [scan {1.0e1} %f x] $x
395 } {1 10.0}
396 test scan-4.54 {Tcl_ScanObjCmd, float scanning} {
397 list [scan {1.0e-1} %f x] $x
398 } {1 0.1}
399 test scan-4.55 {Tcl_ScanObjCmd, odd cases} -setup {
400 set x {}
401 } -body {
402 list [scan {+} %f x] $x
403 } -result {-1 {}}
404 test scan-4.56 {Tcl_ScanObjCmd, odd cases} -setup {
405 set x {}
406 } -body {
407 list [scan {1.0e} %f%s x y] $x $y
408 } -result {2 1.0 e}
409 test scan-4.57 {Tcl_ScanObjCmd, odd cases} -setup {
410 set x {}
411 } -body {
412 list [scan {1.0e+} %f%s x y] $x $y
413 } -result {2 1.0 e+}
414 test scan-4.58 {Tcl_ScanObjCmd, odd cases} -setup {
415 set x {}
416 set y {}
417 } -body {
418 list [scan {e1} %f%s x y] $x $y
419 } -result {0 {} {}}
420 test scan-4.59 {Tcl_ScanObjCmd, float scanning} {
421 list [scan {1.0e-1x} %*f%n x] $x
422 } {1 6}
423
424 test scan-4.60 {Tcl_ScanObjCmd, set errors} -setup {
425 set x {}
426 set y {}
427 unset -nocomplain z
428 } -body {
429 array set z {}
430 list [catch {scan {abc def ghi} {%s%s%s} x z y} msg] $msg $x $y
431 } -cleanup {
432 unset -nocomplain z
433 } -result {1 {can't set "z": variable is array} abc ghi}
434 test scan-4.61 {Tcl_ScanObjCmd, set errors} -setup {
435 set x {}
436 unset -nocomplain y
437 unset -nocomplain z
438 } -body {
439 array set y {}
440 array set z {}
441 list [catch {scan {abc def ghi} {%s%s%s} x z y} msg] $msg $x
442 } -cleanup {
443 unset -nocomplain y
444 unset -nocomplain z
445 } -result {1 {can't set "z": variable is array} abc}
446
447 test scan-4.62 {scanning of large and negative octal integers} {
448 lassign [int_range] MIN_INT MAX_INT
449 set scanstring [format {%o %o %o} -1 $MIN_INT $MAX_INT]
450 list [scan $scanstring {%o %o %o} a b c] \
451 [expr { $a == -1 }] [expr { $b == $MIN_INT }] [expr { $c == $MAX_INT }]
452 } {3 1 1 1}
453 test scan-4.63 {scanning of large and negative hex integers} {
454 lassign [int_range] MIN_INT MAX_INT
455 set scanstring [format {%x %x %x} -1 $MIN_INT $MAX_INT]
456 list [scan $scanstring {%x %x %x} a b c] \
457 [expr { $a == -1 }] [expr { $b == $MIN_INT }] [expr { $c == $MAX_INT }]
458 } {3 1 1 1}
459 test scan-4.64 {scanning of hex with %X} {
460 scan "123 abc f78" %X%X%X
461 } {291 2748 3960}
462
463 test scan-5.1 {integer scanning} -setup {
464 set a {}; set b {}; set c {}; set d {}
465 } -body {
466 list [scan "-20 1476 \n33 0" "%d %d %d %d" a b c d] $a $b $c $d
467 } -result {4 -20 1476 33 0}
468 test scan-5.2 {integer scanning} -setup {
469 set a {}; set b {}; set c {}
470 } -body {
471 list [scan "-45 16 7890 +10" "%2d %*d %10d %d" a b c] $a $b $c
472 } -result {3 -4 16 7890}
473 test scan-5.3 {integer scanning} -setup {
474 set a {}; set b {}; set c {}; set d {}
475 } -body {
476 list [scan "-45 16 +10 987" "%ld %d %ld %d" a b c d] $a $b $c $d
477 } -result {4 -45 16 10 987}
478 test scan-5.4 {integer scanning} -setup {
479 set a {}; set b {}; set c {}; set d {}
480 } -body {
481 list [scan "14 1ab 62 10" "%d %x %lo %x" a b c d] $a $b $c $d
482 } -result {4 14 427 50 16}
483 test scan-5.5 {integer scanning} -setup {
484 set a {}; set b {}; set c {}; set d {}
485 } -body {
486 list [scan "12345670 1234567890ab cdefg" "%o %o %x %lx" a b c d] \
487 $a $b $c $d
488 } -result {4 2739128 342391 561323 52719}
489 test scan-5.6 {integer scanning} -setup {
490 set a {}; set b {}; set c {}; set d {}
491 } -body {
492 list [scan "ab123-24642" "%2x %3x %3o %2o" a b c d] $a $b $c $d
493 } -result {4 171 291 -20 52}
494 test scan-5.7 {integer scanning} -setup {
495 set a {}; set b {}
496 } -body {
497 list [scan "1234567 234 567 " "%*3x %x %*o %4o" a b] $a $b
498 } -result {2 17767 375}
499 test scan-5.8 {integer scanning} -setup {
500 set a {}; set b {}
501 } -body {
502 list [scan "a 1234" "%d %d" a b] $a $b
503 } -result {0 {} {}}
504 test scan-5.9 {integer scanning} -setup {
505 set a {}; set b {}; set c {}; set d {}
506 } -body {
507 list [scan "12345678" "%2d %2d %2ld %2d" a b c d] $a $b $c $d
508 } -result {4 12 34 56 78}
509 test scan-5.10 {integer scanning} -setup {
510 set a {}; set b {}; set c {}; set d {}
511 } -body {
512 list [scan "1 2 " "%hd %d %d %d" a b c d] $a $b $c $d
513 } -result {2 1 2 {} {}}
514 #
515 # The behavior for scanning integers larger than MAX_INT is not defined by the
516 # ANSI spec. Some implementations wrap the input (-16) some return MAX_INT.
517 #
518 test scan-5.11 {integer scanning} -constraints {nonPortable} -setup {
519 set a {}; set b {}
520 } -body {
521 list [scan "4294967280 4294967280" "%u %d" a b] $a \
522 [expr {$b == -16 || $b == 0x7fffffff}]
523 } -result {2 4294967280 1}
524 test scan-5.12 {integer scanning} -constraints {wideIs64bit} -setup {
525 set a {}; set b {}; set c {}
526 } -body {
527 list [scan "7810179016327718216,6c63546f6c6c6548,661432506755433062510" \
528 %ld,%lx,%lo a b c] $a $b $c
529 } -result {3 7810179016327718216 7810179016327718216 7810179016327718216}
530 test scan-5.13 {integer scanning and overflow} {
531 # This test used to fail on some 64-bit systems. [Bug 1011860]
532 scan {300000000 3000000000 30000000000} {%ld %ld %ld}
533 } {300000000 3000000000 30000000000}
534
535 test scan-5.14 {integer scanning} {
536 scan 0xff %u
537 } 0
538 test scan-5.15 {Bug be003d570f} {
539 scan 0x40 %o
540 } 0
541 test scan-5.16 {Bug be003d570f} {
542 scan 0x40 %b
543 } 0
544 test scan-5.17 {bigint scanning} -setup {
545 set a {}; set b {}; set c {}
546 } -body {
547 list [scan "207698809136909011942886895,abcdef0123456789abcdef,125715736004432126361152746757" \
548 %lld,%llx,%llo a b c] $a $b $c
549 } -result {3 207698809136909011942886895 207698809136909011942886895 207698809136909011942886895}
550 test scan-5.18 {bigint scanning underflow} -setup {
551 set a {};
552 } -body {
553 list [scan "-207698809136909011942886895" \
554 %llu a] $a
555 } -returnCodes 1 -result {unsigned bignum scans are invalid}
556 test scan-5.19 {bigint scanning invalid} -setup {
557 set a {};
558 } -body {
559 list [scan "207698809136909011942886895" \
560 %llu a] $a
561 } -returnCodes 1 -result {unsigned bignum scans are invalid}
562
563 test scan-6.1 {floating-point scanning} -setup {
564 set a {}; set b {}; set c {}; set d {}
565 } -body {
566 list [scan "2.1 -3.0e8 .99962 a" "%f%g%e%f" a b c d] $a $b $c $d
567 } -result {3 2.1 -300000000.0 0.99962 {}}
568 test scan-6.2 {floating-point scanning} -setup {
569 set a {}; set b {}; set c {}; set d {}
570 } -body {
571 list [scan "-1.2345 +8.2 9" "%3e %3lf %f %f" a b c d] $a $b $c $d
572 } -result {4 -1.0 234.0 5.0 8.2}
573 test scan-6.3 {floating-point scanning} -setup {
574 set a {}; set b {}; set c {}
575 } -body {
576 list [scan "1e00004 332E-4 3e+4" "%Lf %*2e %f %f" a b c] $a $c
577 } -result {3 10000.0 30000.0}
578 #
579 # Some libc implementations consider 3.e- bad input. The ANSI spec states
580 # that digits must follow the - sign.
581 #
582 test scan-6.4 {floating-point scanning} -setup {
583 set a {}; set b {}; set c {}
584 } -body {
585 list [scan "1. 47.6 2.e2 3.e-" "%f %*f %f %f" a b c] $a $b $c
586 } -result {3 1.0 200.0 3.0}
587 test scan-6.5 {floating-point scanning} -setup {
588 set a {}; set b {}; set c {}; set d {}
589 } -body {
590 list [scan "4.6 99999.7 876.43e-1 118" "%f %f %f %e" a b c d] $a $b $c $d
591 } -result {4 4.6 99999.7 87.643 118.0}
592 test scan-6.6 {floating-point scanning} -setup {
593 set a {}; set b {}; set c {}; set d {}
594 } -body {
595 list [scan "1.2345 697.0e-3 124 .00005" "%f %e %f %e" a b c d] $a $b $c $d
596 } -result {4 1.2345 0.697 124.0 5e-5}
597 test scan-6.7 {floating-point scanning} -setup {
598 set a {}; set b {}; set c {}; set d {}
599 } -body {
600 list [scan "4.6abc" "%f %f %f %f" a b c d] $a $b $c $d
601 } -result {1 4.6 {} {} {}}
602 test scan-6.8 {floating-point scanning} -setup {
603 set a {}; set b {}; set c {}; set d {}
604 } -body {
605 list [scan "4.6 5.2" "%f %f %f %f" a b c d] $a $b $c $d
606 } -result {2 4.6 5.2 {} {}}
607
608 test scan-7.1 {string and character scanning} -setup {
609 set a {}; set b {}; set c {}; set d {}
610 } -body {
611 list [scan "abc defghijk dum " "%s %3s %20s %s" a b c d] $a $b $c $d
612 } -result {4 abc def ghijk dum}
613 test scan-7.2 {string and character scanning} -setup {
614 set a {}; set b {}; set c {}; set d {}
615 } -body {
616 list [scan "a bcdef" "%c%c%1s %s" a b c d] $a $b $c $d
617 } -result {4 97 32 b cdef}
618 test scan-7.3 {string and character scanning} -setup {
619 set a {}; set b {}; set c {}
620 } -body {
621 list [scan "123456 test " "%*c%*s %s %s %s" a b c] $a $b $c
622 } -result {1 test {} {}}
623 test scan-7.4 {string and character scanning} -setup {
624 set a {}; set b {}; set c {}; set d {}
625 } -body {
626 list [scan "ababcd01234 f 123450" {%4[abcd] %4[abcd] %[^abcdef] %[^0]} a b c d] $a $b $c $d
627 } -result {4 abab cd {01234 } {f 12345}}
628 test scan-7.5 {string and character scanning} -setup {
629 set a {}; set b {}; set c {}
630 } -body {
631 list [scan "aaaaaabc aaabcdefg + + XYZQR" {%*4[a] %s %*4[a]%s%*4[ +]%c} a b c] $a $b $c
632 } -result {3 aabc bcdefg 43}
633 test scan-7.6 {string and character scanning, unicode} -setup {
634 set a {}; set b {}; set c {}; set d {}
635 } -body {
636 list [scan "abc d\u00c7fghijk dum " "%s %3s %20s %s" a b c d] $a $b $c $d
637 } -result "4 abc d\u00c7f ghijk dum"
638 test scan-7.7 {string and character scanning, unicode} -setup {
639 set a {}; set b {}
640 } -body {
641 list [scan "ab\u00c7cdef" "ab%c%c" a b] $a $b
642 } -result "2 199 99"
643 test scan-7.8 {string and character scanning, unicode} -setup {
644 set a {}; set b {}
645 } -body {
646 list [scan "ab\ufeffdef" "%\[ab\ufeff\]" a] $a
647 } -result "1 ab\ufeff"
648
649 test scan-8.1 {error conditions} -body {
650 scan a
651 } -returnCodes error -match glob -result *
652 test scan-8.2 {error conditions} -returnCodes error -body {
653 scan a
654 } -result {wrong # args: should be "scan string format ?varName ...?"}
655 test scan-8.3 {error conditions} -returnCodes error -body {
656 scan a %D x
657 } -result {bad scan conversion character "D"}
658 test scan-8.4 {error conditions} -returnCodes error -body {
659 scan a %O x
660 } -result {bad scan conversion character "O"}
661 test scan-8.5 {error conditions} -returnCodes error -body {
662 scan a %B x
663 } -result {bad scan conversion character "B"}
664 test scan-8.6 {error conditions} -returnCodes error -body {
665 scan a %F x
666 } -result {bad scan conversion character "F"}
667 test scan-8.7 {error conditions} -returnCodes error -body {
668 scan a %p x
669 } -result {bad scan conversion character "p"}
670 test scan-8.8 {error conditions} -returnCodes error -body {
671 scan a "%d %d" a
672 } -result {different numbers of variable names and field specifiers}
673 test scan-8.9 {error conditions} -returnCodes error -body {
674 scan a "%d %d" a b c
675 } -result {variable is not assigned by any conversion specifiers}
676 test scan-8.10 {error conditions} -setup {
677 set a {}; set b {}; set c {}; set d {}
678 } -body {
679 list [expr {[scan " a" " a %d %d %d %d" a b c d] <= 0}] $a $b $c $d
680 } -result {1 {} {} {} {}}
681 test scan-8.11 {error conditions} -setup {
682 set a {}; set b {}; set c {}; set d {}
683 } -body {
684 list [scan "1 2" "%d %d %d %d" a b c d] $a $b $c $d
685 } -result {2 1 2 {} {}}
686 test scan-8.12 {error conditions} -setup {
687 unset -nocomplain a
688 } -body {
689 set a(0) 44
690 scan 44 %d a
691 } -returnCodes error -cleanup {
692 unset -nocomplain a
693 } -result {can't set "a": variable is array}
694 test scan-8.13 {error conditions} -setup {
695 unset -nocomplain a
696 } -body {
697 set a(0) 44
698 scan 44 %c a
699 } -returnCodes error -cleanup {
700 unset -nocomplain a
701 } -result {can't set "a": variable is array}
702 test scan-8.14 {error conditions} -setup {
703 unset -nocomplain a
704 } -body {
705 set a(0) 44
706 scan 44 %s a
707 } -returnCodes error -cleanup {
708 unset -nocomplain a
709 } -result {can't set "a": variable is array}
710 test scan-8.15 {error conditions} -setup {
711 unset -nocomplain a
712 } -body {
713 set a(0) 44
714 scan 44 %f a
715 } -returnCodes error -cleanup {
716 unset -nocomplain a
717 } -result {can't set "a": variable is array}
718 test scan-8.16 {error conditions} -setup {
719 unset -nocomplain a
720 } -body {
721 set a(0) 44
722 scan 44 %f a
723 } -returnCodes error -cleanup {
724 unset -nocomplain a
725 } -result {can't set "a": variable is array}
726 test scan-8.17 {error conditions} -returnCodes error -body {
727 scan 44 %2c a
728 } -result {field width may not be specified in %c conversion}
729 test scan-8.18 {error conditions} -returnCodes error -body {
730 scan abc {%[} x
731 } -result {unmatched [ in format string}
732 test scan-8.19 {error conditions} -returnCodes error -body {
733 scan abc {%[^a} x
734 } -result {unmatched [ in format string}
735 test scan-8.20 {error conditions} -returnCodes error -body {
736 scan abc {%[^]a} x
737 } -result {unmatched [ in format string}
738 test scan-8.21 {error conditions} -returnCodes error -body {
739 scan abc {%[]a} x
740 } -result {unmatched [ in format string}
741
742 test scan-9.1 {lots of arguments} {
743 scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20
744 } 20
745 test scan-9.2 {lots of arguments} {
746 scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20
747 set a20
748 } 200
749
750 test scan-10.1 {miscellaneous tests} -setup {
751 set a {}
752 } -body {
753 list [scan ab16c ab%dc a] $a
754 } -result {1 16}
755 test scan-10.2 {miscellaneous tests} -setup {
756 set a {}
757 } -body {
758 list [scan ax16c ab%dc a] $a
759 } -result {0 {}}
760 test scan-10.3 {miscellaneous tests} -setup {
761 set a {}
762 } -body {
763 list [catch {scan ab%c114 ab%%c%d a} msg] $msg $a
764 } -result {0 1 114}
765 test scan-10.4 {miscellaneous tests} -setup {
766 set a {}
767 } -body {
768 list [catch {scan ab%c14 ab%%c%d a} msg] $msg $a
769 } -result {0 1 14}
770 test scan-10.5 {miscellaneous tests} -setup {
771 unset -nocomplain arr
772 } -body {
773 set arr(2) {}
774 list [catch {scan ab%c14 ab%%c%d arr(2)} msg] $msg $arr(2)
775 } -result {0 1 14}
776 test scan-10.6 {miscellaneous tests} {
777 scan 5a {%i%[a]}
778 } {5 a}
779 test scan-10.7 {miscellaneous tests} {
780 scan {5 a} {%i%[a]}
781 } {5 {}}
782
783 test scan-11.1 {alignment in results array (TCL_ALIGN)} {
784 scan "123 13.6" "%s %f" a b
785 set b
786 } 13.6
787 test scan-11.2 {alignment in results array (TCL_ALIGN)} {
788 scan "1234567 13.6" "%s %f" a b
789 set b
790 } 13.6
791 test scan-11.3 {alignment in results array (TCL_ALIGN)} {
792 scan "12345678901 13.6" "%s %f" a b
793 set b
794 } 13.6
795 test scan-11.4 {alignment in results array (TCL_ALIGN)} {
796 scan "123456789012345 13.6" "%s %f" a b
797 set b
798 } 13.6
799 test scan-11.5 {alignment in results array (TCL_ALIGN)} {
800 scan "1234567890123456789 13.6" "%s %f" a b
801 set b
802 } 13.6
803
804 test scan-12.1 {Tcl_ScanObjCmd, inline case} {
805 scan a %c
806 } 97
807 test scan-12.2 {Tcl_ScanObjCmd, inline case} {
808 scan abc %c%c%c%c
809 } {97 98 99 {}}
810 test scan-12.3 {Tcl_ScanObjCmd, inline case} {
811 scan abc %s%c
812 } {abc {}}
813 test scan-12.4 {Tcl_ScanObjCmd, inline case, underflow} {
814 scan abc abc%c
815 } {}
816 test scan-12.5 {Tcl_ScanObjCmd, inline case} {
817 scan abc bogus%c%c%c
818 } {{} {} {}}
819 test scan-12.6 {Tcl_ScanObjCmd, inline case} {
820 # degenerate case, behavior changed from 8.2 to 8.3
821 list [catch {scan foo foobar} msg] $msg
822 } {0 {}}
823 test scan-12.7 {Tcl_ScanObjCmd, inline case lots of arguments} {
824 scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140\
825 150 160 170 180 190 200" \
826 "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d"
827 } {10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 {}}
828
829 test scan-13.1 {Tcl_ScanObjCmd, inline XPG case} {
830 scan a {%1$c}
831 } 97
832 test scan-13.2 {Tcl_ScanObjCmd, inline XPG case} {
833 scan abc {%1$c%2$c%3$c%4$c}
834 } {97 98 99 {}}
835 test scan-13.3 {Tcl_ScanObjCmd, inline XPG case} -returnCodes error -body {
836 scan abc {%1$c%1$c}
837 } -result {variable is assigned by multiple "%n$" conversion specifiers}
838 test scan-13.4 {Tcl_ScanObjCmd, inline XPG case} {
839 scan abc {%2$s%1$c}
840 } {{} abc}
841 test scan-13.5 {Tcl_ScanObjCmd, inline XPG case, underflow} {
842 scan abc {abc%5$c}
843 } {}
844 test scan-13.6 {Tcl_ScanObjCmd, inline XPG case} {
845 catch {scan abc {bogus%1$c%5$c%10$c}} msg
846 list [llength $msg] $msg
847 } {10 {{} {} {} {} {} {} {} {} {} {}}}
848 test scan-13.7 {Tcl_ScanObjCmd, inline XPG case lots of arguments} {
849 scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200" {%20$d %18$d %17$d %16$d %15$d %14$d %13$d %12$d %11$d %10$d %9$d %8$d %7$d %6$d %5$d %4$d %3$d %2$d %1$d}
850 } {190 180 170 160 150 140 130 120 110 100 90 80 70 60 50 40 30 20 {} 10}
851 test scan-13.8 {Tcl_ScanObjCmd, inline XPG case lots of arguments} {
852 set msg [scan "10 20 30" {%100$d %5$d %200$d}]
853 list [llength $msg] [lindex $msg 99] [lindex $msg 4] [lindex $msg 199]
854 } {200 10 20 30}
855 test scan-13.9 {Tcl_ScanObjCmd, inline XPG case limit error} -body {
856 # Note this applies to 64-bit builds as well so long as max number of
857 # command line arguments allowed for scan command is INT_MAX
858 scan abc {%2147483648$s}
859 } -result {"%n$" argument index out of range} -returnCodes error
860
861 # scan infinities - not working
862
863 test scan-14.1 {positive infinity} {
864 scan Inf %g d
865 return $d
866 } Inf
867 test scan-14.2 {negative infinity} {
868 scan -Inf %g d
869 return $d
870 } -Inf
871
872 test scan-15.1 {scan %g overflow for small numbers and big mantissa bug 42d14c495a} {
873 set result [list]
874 # xfail: n<num> = not expected at all, x<num> expected when unfixed
875 foreach {exp numdig ret xfail} {
876 -321 190 1.1116477031428047e-321 n0
877 -321 191 1.1116477031428047e-321 x1
878 -321 300 1.1116477031428047e-321 x2
879 -321 1600 1.1116477031428047e-321 x3
880 -400 110 0 n4
881 -400 111 0 n5
882 -400 300 0 n6
883 -221 290 1.111111111111111e-221 n7
884 -221 291 1.111111111111111e-221 x8
885 -221 400 1.111111111111111e-221 x9
886 -221 1600 1.111111111111111e-221 x10
887 -121 390 1.1111111111111112e-121 n11
888 -121 391 1.1111111111111112e-121 x12
889 -121 500 1.1111111111111112e-121 x13
890 -121 1600 1.1111111111111112e-121 x14
891 308 202 1.1111111111111112e+308 n15
892 308 203 1.1111111111111112e+308 n16
893 308 300 1.1111111111111112e+308 n17
894 308 1600 1.1111111111111112e+308 x18
895 400 110 *inf n19
896 400 111 *inf n20
897 400 300 *inf n21
898 221 291 1.1111111111111112e+221 n22
899 221 292 1.1111111111111112e+221 n23
900 221 400 1.1111111111111112e+221 n24
901 221 1600 1.1111111111111112e+221 x25
902 121 391 1.1111111111111111e+121 n26
903 121 392 1.1111111111111111e+121 n27
904 121 500 1.1111111111111111e+121 n28
905 121 1600 1.1111111111111111e+121 x29
906 } {
907 set s 1.[string repeat 1 $numdig]e$exp
908 set d "no_scan"
909 scan $s %g d
910 set r [format %.17g $d]
911 if {![string match -nocase $ret $r]} {
912 lappend result $xfail=[format %.17g $d]
913 }
914 }
915 set result
916 } {}
917
918 # TODO - also need to scan NaN's
919
920 catch {rename int_range {}}
921
922 # cleanup
923 ::tcltest::cleanupTests
924 return
925
926 # Local Variables:
927 # mode: tcl
928 # End: