Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/scan.test @ 68

Last change on this file since 68 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 26.0 KB
Line 
1# Commands covered:  scan
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-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# RCS: @(#) $Id: scan.test,v 1.21 2006/04/25 17:15:25 dgp Exp $
15
16if {[lsearch [namespace children] ::tcltest] == -1} {
17    package require tcltest 2
18    namespace import -force ::tcltest::*
19}
20
21testConstraint wideIs64bit \
22        [expr {(wide(0x80000000) > 0) && (wide(0x8000000000000000) < 0)}]
23
24test scan-1.1 {BuildCharSet, CharInSet} {
25    list [scan foo {%[^o]} x] $x
26} {1 f}
27test scan-1.2 {BuildCharSet, CharInSet} {
28    list [scan \]foo {%[]f]} x] $x
29} {1 \]f}
30test scan-1.3 {BuildCharSet, CharInSet} {
31    list [scan abc-def {%[a-c]} x] $x
32} {1 abc}
33test scan-1.4 {BuildCharSet, CharInSet} {
34    list [scan abc-def {%[a-c]} x] $x
35} {1 abc}
36test scan-1.5 {BuildCharSet, CharInSet} {
37    list [scan -abc-def {%[-ac]} x] $x
38} {1 -a}
39test scan-1.6 {BuildCharSet, CharInSet} {
40    list [scan -abc-def {%[ac-]} x] $x
41} {1 -a}
42test scan-1.7 {BuildCharSet, CharInSet} {
43    list [scan abc-def {%[c-a]} x] $x
44} {1 abc}
45test scan-1.8 {BuildCharSet, CharInSet} {
46    list [scan def-abc {%[^c-a]} x] $x
47} {1 def-}
48test scan-1.9 {BuildCharSet, CharInSet no match} {
49    catch {unset x}
50    list [scan {= f} {= %[TF]} x] [info exists x]
51} {0 0}
52
53test scan-2.1 {ReleaseCharSet} {
54    list [scan abcde {%[abc]} x] $x
55} {1 abc}
56test scan-2.2 {ReleaseCharSet} {
57    list [scan abcde {%[a-c]} x] $x
58} {1 abc}
59
60test scan-3.1 {ValidateFormat} {
61    list [catch {scan {} {%d%1$d} x} msg] $msg
62} {1 {cannot mix "%" and "%n$" conversion specifiers}}
63test scan-3.2 {ValidateFormat} {
64    list [catch {scan {} {%d%1$d} x} msg] $msg
65} {1 {cannot mix "%" and "%n$" conversion specifiers}}
66test scan-3.3 {ValidateFormat} {
67    list [catch {scan {} {%2$d%d} x} msg] $msg
68} {1 {"%n$" argument index out of range}}
69test scan-3.4 {ValidateFormat} {
70    # degenerate case, before changed from 8.2 to 8.3
71    list [catch {scan {} %d} msg] $msg
72} {0 {}}
73test scan-3.5 {ValidateFormat} {
74    list [catch {scan {} {%10c} a} msg] $msg
75} {1 {field width may not be specified in %c conversion}}
76test scan-3.6 {ValidateFormat} {
77    list [catch {scan {} {%*1$d} a} msg] $msg
78} {1 {bad scan conversion character "$"}}
79test scan-3.7 {ValidateFormat} {
80    list [catch {scan {} {%1$d%1$d} a} msg] $msg
81} {1 {variable is assigned by multiple "%n$" conversion specifiers}}
82test scan-3.8 {ValidateFormat} {
83    list [catch {scan {} a x} msg] $msg
84} {1 {variable is not assigned by any conversion specifiers}}
85test scan-3.9 {ValidateFormat} {
86    list [catch {scan {} {%2$s} x y} msg] $msg
87} {1 {variable is not assigned by any conversion specifiers}}
88test scan-3.10 {ValidateFormat} {
89    list [catch {scan {} {%[a} x} msg] $msg
90} {1 {unmatched [ in format string}}
91test scan-3.11 {ValidateFormat} {
92    list [catch {scan {} {%[^a} x} msg] $msg
93} {1 {unmatched [ in format string}}
94test scan-3.12 {ValidateFormat} {
95    list [catch {scan {} {%[]a} x} msg] $msg
96} {1 {unmatched [ in format string}}
97test scan-3.13 {ValidateFormat} {
98    list [catch {scan {} {%[^]a} x} msg] $msg
99} {1 {unmatched [ in format string}}
100
101test scan-4.1 {Tcl_ScanObjCmd, argument checks} {
102    list [catch {scan} msg] $msg
103} {1 {wrong # args: should be "scan string format ?varName varName ...?"}}
104test scan-4.2 {Tcl_ScanObjCmd, argument checks} {
105    list [catch {scan string} msg] $msg
106} {1 {wrong # args: should be "scan string format ?varName varName ...?"}}
107test scan-4.3 {Tcl_ScanObjCmd, argument checks} {
108    # degenerate case, before changed from 8.2 to 8.3
109    list [catch {scan string format} msg] $msg
110} {0 {}}
111test scan-4.4 {Tcl_ScanObjCmd, whitespace} {
112    list [scan {   abc   def   } {%s%s} x y] $x $y
113} {2 abc def}
114test scan-4.5 {Tcl_ScanObjCmd, whitespace} {
115    list [scan {   abc   def   } { %s %s } x y] $x $y
116} {2 abc def}
117test scan-4.6 {Tcl_ScanObjCmd, whitespace} {
118    list [scan {   abc   def   } { %s %s } x y] $x $y
119} {2 abc def}
120test scan-4.7 {Tcl_ScanObjCmd, literals} {
121    # degenerate case, before changed from 8.2 to 8.3
122    scan {   abc   def   } { abc def }
123} {}
124test scan-4.8 {Tcl_ScanObjCmd, literals} {
125    set x {}
126    list [scan {   abcg} { abc def %1s} x] $x
127} {0 {}}
128test scan-4.9 {Tcl_ScanObjCmd, literals} {
129    list [scan {   abc%defghi} { abc %% def%n } x] $x
130} {1 10}
131test scan-4.10 {Tcl_ScanObjCmd, assignment suppression} {
132    list [scan {   abc   def   } { %*c%s def } x] $x
133} {1 bc}
134test scan-4.11 {Tcl_ScanObjCmd, XPG3-style} {
135    list [scan {   abc   def   } {%2$s %1$s} x y] $x $y
136} {2 def abc}
137test scan-4.12 {Tcl_ScanObjCmd, width specifiers} {
138    list [scan {abc123456789012} {%3s%3d%3f%3[0-9]%s} a b c d e] $a $b $c $d $e
139} {5 abc 123 456.0 789 012}
140test scan-4.13 {Tcl_ScanObjCmd, width specifiers} {
141    list [scan {abc123456789012} {%3s%3d%3f%3[0-9]%s} a b c d e] $a $b $c $d $e
142} {5 abc 123 456.0 789 012}
143test scan-4.14 {Tcl_ScanObjCmd, underflow} {
144    set x {}
145    list [scan {a} {a%d} x] $x
146} {-1 {}}
147test scan-4.15 {Tcl_ScanObjCmd, underflow} {
148    set x {}
149    list [scan {} {a%d} x] $x
150} {-1 {}}
151test scan-4.16 {Tcl_ScanObjCmd, underflow} {
152    set x {}
153    list [scan {ab} {a%d} x] $x
154} {0 {}}
155test scan-4.17 {Tcl_ScanObjCmd, underflow} {
156    set x {}
157    list [scan {a   } {a%d} x] $x
158} {-1 {}}
159test scan-4.18 {Tcl_ScanObjCmd, skipping whitespace} {
160    list [scan {  b} {%c%s} x y] $x $y
161} {2 32 b}
162test scan-4.19 {Tcl_ScanObjCmd, skipping whitespace} {
163    list [scan {  b} {%[^b]%s} x y] $x $y
164} {2 {  } b}
165test scan-4.20 {Tcl_ScanObjCmd, string scanning} {
166    list [scan {abc def} {%s} x] $x
167} {1 abc}
168test scan-4.21 {Tcl_ScanObjCmd, string scanning} {
169    list [scan {abc def} {%0s} x] $x
170} {1 abc}
171test scan-4.22 {Tcl_ScanObjCmd, string scanning} {
172    list [scan {abc def} {%2s} x] $x
173} {1 ab}
174test scan-4.23 {Tcl_ScanObjCmd, string scanning} {
175    list [scan {abc def} {%*s%n} x] $x
176} {1 3}
177test scan-4.24 {Tcl_ScanObjCmd, charset scanning} {
178    list [scan {abcdef} {%[a-c]} x] $x
179} {1 abc}
180test scan-4.25 {Tcl_ScanObjCmd, charset scanning} {
181    list [scan {abcdef} {%0[a-c]} x] $x
182} {1 abc}
183test scan-4.26 {Tcl_ScanObjCmd, charset scanning} {
184    list [scan {abcdef} {%2[a-c]} x] $x
185} {1 ab}
186test scan-4.27 {Tcl_ScanObjCmd, charset scanning} {
187    list [scan {abcdef} {%*[a-c]%n} x] $x
188} {1 3}
189test scan-4.28 {Tcl_ScanObjCmd, character scanning} {
190    list [scan {abcdef} {%c} x] $x
191} {1 97}
192test scan-4.29 {Tcl_ScanObjCmd, character scanning} {
193    list [scan {abcdef} {%*c%n} x] $x
194} {1 1}
195
196test scan-4.30 {Tcl_ScanObjCmd, base-10 integer scanning} {
197    set x {}
198    list [scan {1234567890a} {%3d} x] $x
199} {1 123}
200test scan-4.31 {Tcl_ScanObjCmd, base-10 integer scanning} {
201    set x {}
202    list [scan {1234567890a} {%d} x] $x
203} {1 1234567890}
204test scan-4.32 {Tcl_ScanObjCmd, base-10 integer scanning} {
205    set x {}
206    list [scan {01234567890a} {%d} x] $x
207} {1 1234567890}
208test scan-4.33 {Tcl_ScanObjCmd, base-10 integer scanning} {
209    set x {}
210    list [scan {+01234} {%d} x] $x
211} {1 1234}
212test scan-4.34 {Tcl_ScanObjCmd, base-10 integer scanning} {
213    set x {}
214    list [scan {-01234} {%d} x] $x
215} {1 -1234}
216test scan-4.35 {Tcl_ScanObjCmd, base-10 integer scanning} {
217    set x {}
218    list [scan {a01234} {%d} x] $x
219} {0 {}}
220test scan-4.36 {Tcl_ScanObjCmd, base-10 integer scanning} {
221    set x {}
222    list [scan {0x10} {%d} x] $x
223} {1 0}
224test scan-4.37 {Tcl_ScanObjCmd, base-8 integer scanning} {
225    set x {}
226    list [scan {012345678} {%o} x] $x
227} {1 342391}
228test scan-4.38 {Tcl_ScanObjCmd, base-8 integer scanning} {
229    set x {}
230    list [scan {+1238 -1239 123a} {%o%*s%o%*s%o} x y z] $x $y $z
231} {3 83 -83 83}
232test scan-4.39 {Tcl_ScanObjCmd, base-16 integer scanning} {
233    set x {}
234    list [scan {+1238 -123a 0123} {%x%x%x} x y z] $x $y $z
235} {3 4664 -4666 291}
236test scan-4.40 {Tcl_ScanObjCmd, base-16 integer scanning} {
237    # The behavior changed in 8.4a4/8.3.4cvs (6 Feb) to correctly
238    # return '1' for 0x1 scanned via %x, to comply with 8.0 and C scanf.
239    # Bug #495213
240    set x {}
241    list [scan {aBcDeF AbCdEf 0x1} {%x%x%x} x y z] $x $y $z
242} {3 11259375 11259375 1}
243test scan-4.40.1 {Tcl_ScanObjCmd, base-16 integer scanning} {
244    set x {}
245    list [scan {0xF 0x00A0B 0X0XF} {%x %x %x} x y z] $x $y $z
246} {3 15 2571 0}
247test scan-4.40.2 {Tcl_ScanObjCmd, base-16 integer scanning} {
248    catch {unset x}
249    list [scan {xF} {%x} x] [info exists x]
250} {0 0}
251test scan-4.41 {Tcl_ScanObjCmd, base-unknown integer scanning} {
252    set x {}
253    list [scan {10 010 0x10} {%i%i%i} x y z] $x $y $z
254} {3 10 8 16}
255test scan-4.42 {Tcl_ScanObjCmd, base-unknown integer scanning} {
256    set x {}
257    list [scan {10 010 0X10} {%i%i%i} x y z] $x $y $z
258} {3 10 8 16}
259test scan-4.43 {Tcl_ScanObjCmd, integer scanning, odd cases} {
260    set x {}
261    list [scan {+ } {%i} x] $x
262} {0 {}}
263test scan-4.44 {Tcl_ScanObjCmd, integer scanning, odd cases} {
264    set x {}
265    list [scan {+} {%i} x] $x
266} {-1 {}}
267test scan-4.45 {Tcl_ScanObjCmd, integer scanning, odd cases} {
268    set x {}
269    list [scan {0x} {%i%s} x y] $x $y
270} {2 0 x}
271test scan-4.46 {Tcl_ScanObjCmd, integer scanning, odd cases} {
272    set x {}
273    list [scan {0X} {%i%s} x y] $x $y
274} {2 0 X}
275test scan-4.47 {Tcl_ScanObjCmd, integer scanning, suppressed} {
276    set x {}
277    list [scan {123def} {%*i%s} x] $x
278} {1 def}
279test scan-4.48 {Tcl_ScanObjCmd, float scanning} {
280    list [scan {1 2 3} {%e %f %g} x y z] $x $y $z
281} {3 1.0 2.0 3.0}
282test scan-4.49 {Tcl_ScanObjCmd, float scanning} {
283    list [scan {.1 0.2 3.} {%e %f %g} x y z] $x $y $z
284} {3 0.1 0.2 3.0}
285test scan-4.50 {Tcl_ScanObjCmd, float scanning} {
286    list [scan {1234567890a} %f x] $x
287} {1 1234567890.0}
288test scan-4.51 {Tcl_ScanObjCmd, float scanning} {
289    list [scan {+123+45} %f x] $x
290} {1 123.0}
291test scan-4.52 {Tcl_ScanObjCmd, float scanning} {
292    list [scan {-123+45} %f x] $x
293} {1 -123.0}
294test scan-4.53 {Tcl_ScanObjCmd, float scanning} {
295    list [scan {1.0e1} %f x] $x
296} {1 10.0}
297test scan-4.54 {Tcl_ScanObjCmd, float scanning} {
298    list [scan {1.0e-1} %f x] $x
299} {1 0.1}
300test scan-4.55 {Tcl_ScanObjCmd, odd cases} {
301    set x {}
302    list [scan {+} %f x] $x
303} {-1 {}}
304test scan-4.56 {Tcl_ScanObjCmd, odd cases} {
305    set x {}
306    list [scan {1.0e} %f%s x y] $x $y
307} {2 1.0 e}
308test scan-4.57 {Tcl_ScanObjCmd, odd cases} {
309    set x {}
310    list [scan {1.0e+} %f%s x y] $x $y
311} {2 1.0 e+}
312test scan-4.58 {Tcl_ScanObjCmd, odd cases} {
313    set x {}
314    set y {}
315    list [scan {e1} %f%s x y] $x $y
316} {0 {} {}}
317test scan-4.59 {Tcl_ScanObjCmd, float scanning} {
318    list [scan {1.0e-1x} %*f%n x] $x
319} {1 6}
320
321test scan-4.60 {Tcl_ScanObjCmd, set errors} {
322    set x {}
323    set y {}
324    catch {unset z}; array set z {}
325    set result [list [catch {scan {abc def ghi} {%s%s%s} x z y} msg] \
326            $msg $x $y]
327    unset z
328    set result
329} {1 {couldn't set variable "z"} abc ghi}
330test scan-4.61 {Tcl_ScanObjCmd, set errors} {
331    set x {}
332    catch {unset y}; array set y {}
333    catch {unset z}; array set z {}
334    set result [list [catch {scan {abc def ghi} {%s%s%s} x z y} msg] \
335            $msg $x]
336    unset y
337    unset z
338    set result
339} {1 {couldn't set variable "z"couldn't set variable "y"} abc}
340
341# procedure that returns the range of integers
342
343proc int_range {} {
344    for { set MIN_INT 1 } { int($MIN_INT) > 0 } {} {
345        set MIN_INT [expr { $MIN_INT << 1 }]
346    }
347    set MIN_INT [expr {int($MIN_INT)}]
348    set MAX_INT [expr { ~ $MIN_INT }]
349    return [list $MIN_INT $MAX_INT]
350}
351
352test scan-4.62 {scanning of large and negative octal integers} {
353    foreach { MIN_INT MAX_INT } [int_range] {}
354    set scanstring [format {%o %o %o} -1 $MIN_INT $MAX_INT]
355    list [scan $scanstring {%o %o %o} a b c] \
356        [expr { $a == -1 }] [expr { $b == $MIN_INT }] [expr { $c == $MAX_INT }]
357} {3 1 1 1}
358test scan-4.63 {scanning of large and negative hex integers} {
359    foreach { MIN_INT MAX_INT } [int_range] {}
360    set scanstring [format {%x %x %x} -1 $MIN_INT $MAX_INT]
361    list [scan $scanstring {%x %x %x} a b c] \
362        [expr { $a == -1 }] [expr { $b == $MIN_INT }] [expr { $c == $MAX_INT }]
363} {3 1 1 1}
364
365# clean up from last two tests
366
367catch {
368    rename int_range {}
369}
370
371test scan-5.1 {integer scanning} {
372    set a {}; set b {}; set c {}; set d {}
373    list [scan "-20 1476 \n33 0" "%d %d %d %d" a b c d] $a $b $c $d
374} {4 -20 1476 33 0}
375test scan-5.2 {integer scanning} {
376    set a {}; set b {}; set c {}
377    list [scan "-45 16 7890 +10" "%2d %*d %10d %d" a b c] $a $b $c
378} {3 -4 16 7890}
379test scan-5.3 {integer scanning} {
380    set a {}; set b {}; set c {}; set d {}
381    list [scan "-45 16 +10 987" "%ld %d %ld %d" a b c d] $a $b $c $d
382} {4 -45 16 10 987}
383test scan-5.4 {integer scanning} {
384    set a {}; set b {}; set c {}; set d {}
385    list [scan "14 1ab 62 10" "%d %x %lo %x" a b c d] $a $b $c $d
386} {4 14 427 50 16}
387test scan-5.5 {integer scanning} {
388    set a {}; set b {}; set c {}; set d {}
389    list [scan "12345670 1234567890ab cdefg" "%o         %o %x %lx" a b c d] \
390            $a $b $c $d
391} {4 2739128 342391 561323 52719}
392test scan-5.6 {integer scanning} {
393    set a {}; set b {}; set c {}; set d {}
394    list [scan "ab123-24642" "%2x %3x %3o %2o" a b c d] $a $b $c $d
395} {4 171 291 -20 52}
396test scan-5.7 {integer scanning} {
397    set a {}; set b {}
398    list [scan "1234567 234 567  " "%*3x %x %*o %4o" a b] $a $b
399} {2 17767 375}
400test scan-5.8 {integer scanning} {
401    set a {}; set b {}
402    list [scan "a       1234" "%d %d" a b] $a $b
403} {0 {} {}}
404test scan-5.9 {integer scanning} {
405    set a {}; set b {}; set c {}; set d {};
406    list [scan "12345678" "%2d %2d %2ld %2d" a b c d] $a $b $c $d
407} {4 12 34 56 78}
408test scan-5.10 {integer scanning} {
409    set a {}; set b {}; set c {}; set d {}
410    list [scan "1 2 " "%hd %d %d %d" a b c d] $a $b $c $d
411} {2 1 2 {} {}}
412#
413# The behavior for scaning intergers larger than MAX_INT is
414# not defined by the ANSI spec.  Some implementations wrap the
415# input (-16) some return MAX_INT.
416#
417test scan-5.11 {integer scanning} {nonPortable} {
418    set a {}; set b {};
419    list [scan "4294967280 4294967280" "%u %d" a b] $a \
420            [expr {$b == -16 || $b == 0x7fffffff}]
421} {2 4294967280 1}
422test scan-5.12 {integer scanning} {wideIs64bit} {
423    set a {}; set b {}; set c {}
424    list [scan "7810179016327718216,6c63546f6c6c6548,661432506755433062510" \
425            %ld,%lx,%lo a b c] $a $b $c
426} {3 7810179016327718216 7810179016327718216 7810179016327718216}
427test scan-5.13 {integer scanning and overflow} {
428    # This test used to fail on some 64-bit systems. [Bug 1011860]
429    scan {300000000 3000000000 30000000000} {%ld %ld %ld}
430} {300000000 3000000000 30000000000}
431
432test scan-5.14 {integer scanning} {
433    scan 0xff %u
434} 0
435
436test scan-6.1 {floating-point scanning} {
437    set a {}; set b {}; set c {}; set d {}
438    list [scan "2.1 -3.0e8 .99962 a" "%f%g%e%f" a b c d] $a $b $c $d
439} {3 2.1 -300000000.0 0.99962 {}}
440test scan-6.2 {floating-point scanning} {
441    set a {}; set b {}; set c {}; set d {}
442    list [scan "-1.2345 +8.2 9" "%3e %3lf %f %f" a b c d] $a $b $c $d
443} {4 -1.0 234.0 5.0 8.2}
444test scan-6.3 {floating-point scanning} {
445    set a {}; set b {}; set c {}
446    list [scan "1e00004 332E-4 3e+4" "%Lf %*2e %f %f" a b c] $a $c
447} {3 10000.0 30000.0}
448#
449# Some libc implementations consider 3.e- bad input.  The ANSI
450# spec states that digits must follow the - sign.
451#
452test scan-6.4 {floating-point scanning} {
453    set a {}; set b {}; set c {}
454    list [scan "1. 47.6 2.e2 3.e-" "%f %*f %f %f" a b c] $a $b $c
455} {3 1.0 200.0 3.0}
456test scan-6.5 {floating-point scanning} {
457    set a {}; set b {}; set c {}; set d {}
458    list [scan "4.6 99999.7 876.43e-1 118" "%f %f %f %e" a b c d] $a $b $c $d
459} {4 4.6 99999.7 87.643 118.0}
460test scan-6.6 {floating-point scanning} {
461    set a {}; set b {}; set c {}; set d {}
462    list [scan "1.2345 697.0e-3 124 .00005" "%f %e %f %e" a b c d] $a $b $c $d
463} {4 1.2345 0.697 124.0 5e-5}
464test scan-6.7 {floating-point scanning} {
465    set a {}; set b {}; set c {}; set d {}
466    list [scan "4.6abc" "%f %f %f %f" a b c d] $a $b $c $d
467} {1 4.6 {} {} {}}
468test scan-6.8 {floating-point scanning} {
469    set a {}; set b {}; set c {}; set d {}
470    list [scan "4.6 5.2" "%f %f %f %f" a b c d] $a $b $c $d
471} {2 4.6 5.2 {} {}}
472
473test scan-7.1 {string and character scanning} {
474    set a {}; set b {}; set c {}; set d {}
475    list [scan "abc defghijk dum " "%s %3s %20s %s" a b c d] $a $b $c $d
476} {4 abc def ghijk dum}
477test scan-7.2 {string and character scanning} {
478    set a {}; set b {}; set c {}; set d {}
479    list [scan "a       bcdef" "%c%c%1s %s" a b c d] $a $b $c $d
480} {4 97 32 b cdef}
481test scan-7.3 {string and character scanning} {
482    set a {}; set b {}; set c {}
483    list [scan "123456 test " "%*c%*s %s %s %s" a b c] $a $b $c
484} {1 test {} {}}
485test scan-7.4 {string and character scanning} {
486    set a {}; set b {}; set c {}; set d
487    list [scan "ababcd01234  f 123450" {%4[abcd] %4[abcd] %[^abcdef] %[^0]} a b c d] $a $b $c $d
488} {4 abab cd {01234  } {f 12345}}
489test scan-7.5 {string and character scanning} {
490    set a {}; set b {}; set c {}
491    list [scan "aaaaaabc aaabcdefg  + +  XYZQR" {%*4[a] %s %*4[a]%s%*4[ +]%c} a b c] $a $b $c
492} {3 aabc bcdefg 43}
493test scan-7.6 {string and character scanning, unicode} {
494    set a {}; set b {}; set c {}; set d {}
495    list [scan "abc d\u00c7fghijk dum " "%s %3s %20s %s" a b c d] $a $b $c $d
496} "4 abc d\u00c7f ghijk dum"
497test scan-7.7 {string and character scanning, unicode} {
498    set a {}; set b {}
499    list [scan "ab\u00c7cdef" "ab%c%c" a b] $a $b
500} "2 199 99"
501test scan-7.8 {string and character scanning, unicode} {
502    set a {}; set b {}
503    list [scan "ab\ufeffdef" "%\[ab\ufeff\]" a] $a
504} "1 ab\ufeff"
505
506test scan-8.1 {error conditions} {
507    catch {scan a}
508} 1
509test scan-8.2 {error conditions} {
510    catch {scan a} msg
511    set msg
512} {wrong # args: should be "scan string format ?varName varName ...?"}
513test scan-8.3 {error conditions} {
514    list [catch {scan a %D x} msg] $msg
515} {1 {bad scan conversion character "D"}}
516test scan-8.4 {error conditions} {
517    list [catch {scan a %O x} msg] $msg
518} {1 {bad scan conversion character "O"}}
519test scan-8.5 {error conditions} {
520    list [catch {scan a %X x} msg] $msg
521} {1 {bad scan conversion character "X"}}
522test scan-8.6 {error conditions} {
523    list [catch {scan a %F x} msg] $msg
524} {1 {bad scan conversion character "F"}}
525test scan-8.7 {error conditions} {
526    list [catch {scan a %E x} msg] $msg
527} {1 {bad scan conversion character "E"}}
528test scan-8.8 {error conditions} {
529    list [catch {scan a "%d %d" a} msg] $msg
530} {1 {different numbers of variable names and field specifiers}}
531test scan-8.9 {error conditions} {
532    list [catch {scan a "%d %d" a b c} msg] $msg
533} {1 {variable is not assigned by any conversion specifiers}}
534test scan-8.10 {error conditions} {
535    set a {}; set b {}; set c {}; set d {}
536    list [expr {[scan "  a" " a %d %d %d %d" a b c d] <= 0}] $a $b $c $d
537} {1 {} {} {} {}}
538test scan-8.11 {error conditions} {
539    set a {}; set b {}; set c {}; set d {}
540    list [scan "1 2" "%d %d %d %d" a b c d] $a $b $c $d
541} {2 1 2 {} {}}
542test scan-8.12 {error conditions} {
543    catch {unset a}
544    set a(0) 44
545    list [catch {scan 44 %d a} msg] $msg
546} {1 {couldn't set variable "a"}}
547test scan-8.13 {error conditions} {
548    catch {unset a}
549    set a(0) 44
550    list [catch {scan 44 %c a} msg] $msg
551} {1 {couldn't set variable "a"}}
552test scan-8.14 {error conditions} {
553    catch {unset a}
554    set a(0) 44
555    list [catch {scan 44 %s a} msg] $msg
556} {1 {couldn't set variable "a"}}
557test scan-8.15 {error conditions} {
558    catch {unset a}
559    set a(0) 44
560    list [catch {scan 44 %f a} msg] $msg
561} {1 {couldn't set variable "a"}}
562test scan-8.16 {error conditions} {
563    catch {unset a}
564    set a(0) 44
565    list [catch {scan 44 %f a} msg] $msg
566} {1 {couldn't set variable "a"}}
567catch {unset a}
568test scan-8.17 {error conditions} {
569    list [catch {scan 44 %2c a} msg] $msg
570} {1 {field width may not be specified in %c conversion}}
571test scan-8.18 {error conditions} {
572    list [catch {scan abc {%[} x} msg] $msg
573} {1 {unmatched [ in format string}}
574test scan-8.19 {error conditions} {
575    list [catch {scan abc {%[^a} x} msg] $msg
576} {1 {unmatched [ in format string}}
577test scan-8.20 {error conditions} {
578    list [catch {scan abc {%[^]a} x} msg] $msg
579} {1 {unmatched [ in format string}}
580test scan-8.21 {error conditions} {
581    list [catch {scan abc {%[]a} x} msg] $msg
582} {1 {unmatched [ in format string}}
583
584test scan-9.1 {lots of arguments} {
585    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
586} 20
587test scan-9.2 {lots of arguments} {
588    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
589    set a20
590} 200
591
592test scan-10.1 {miscellaneous tests} {
593    set a {}
594    list [scan ab16c ab%dc a] $a
595} {1 16}
596test scan-10.2 {miscellaneous tests} {
597    set a {}
598    list [scan ax16c ab%dc a] $a
599} {0 {}}
600test scan-10.3 {miscellaneous tests} {
601    set a {}
602    list [catch {scan ab%c114 ab%%c%d a} msg] $msg $a
603} {0 1 114}
604test scan-10.4 {miscellaneous tests} {
605    set a {}
606    list [catch {scan ab%c14 ab%%c%d a} msg] $msg $a
607} {0 1 14}
608test scan-10.5 {miscellaneous tests} {
609    catch {unset arr}
610    set arr(2) {}
611    list [catch {scan ab%c14 ab%%c%d arr(2)} msg] $msg $arr(2)
612} {0 1 14}
613test scan-10.6 {miscellaneous tests} {
614    scan 5a {%i%[a]}
615} {5 a}
616test scan-10.7 {miscellaneous tests} {
617    scan {5 a} {%i%[a]}
618} {5 {}}
619
620test scan-11.1 {alignment in results array (TCL_ALIGN)} {
621    scan "123 13.6" "%s %f" a b
622    set b
623} 13.6
624test scan-11.2 {alignment in results array (TCL_ALIGN)} {
625    scan "1234567 13.6" "%s %f" a b
626    set b
627} 13.6
628test scan-11.3 {alignment in results array (TCL_ALIGN)} {
629    scan "12345678901 13.6" "%s %f" a b
630    set b
631} 13.6
632test scan-11.4 {alignment in results array (TCL_ALIGN)} {
633    scan "123456789012345 13.6" "%s %f" a b
634    set b
635} 13.6
636test scan-11.5 {alignment in results array (TCL_ALIGN)} {
637    scan "1234567890123456789 13.6" "%s %f" a b
638    set b
639} 13.6
640
641test scan-12.1 {Tcl_ScanObjCmd, inline case} {
642    scan a %c
643} 97
644test scan-12.2 {Tcl_ScanObjCmd, inline case} {
645    scan abc %c%c%c%c
646} {97 98 99 {}}
647test scan-12.3 {Tcl_ScanObjCmd, inline case} {
648    scan abc %s%c
649} {abc {}}
650test scan-12.4 {Tcl_ScanObjCmd, inline case, underflow} {
651    scan abc abc%c
652} {}
653test scan-12.5 {Tcl_ScanObjCmd, inline case} {
654    scan abc bogus%c%c%c
655} {{} {} {}}
656test scan-12.6 {Tcl_ScanObjCmd, inline case} {
657    # degenerate case, behavior changed from 8.2 to 8.3
658    list [catch {scan foo foobar} msg] $msg
659} {0 {}}
660test scan-12.7 {Tcl_ScanObjCmd, inline case lots of arguments} {
661    scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140\
662            150 160 170 180 190 200" \
663            "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d"
664} {10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 {}}
665
666test scan-13.1 {Tcl_ScanObjCmd, inline XPG case} {
667    scan a {%1$c}
668} 97
669test scan-13.2 {Tcl_ScanObjCmd, inline XPG case} {
670    scan abc {%1$c%2$c%3$c%4$c}
671} {97 98 99 {}}
672test scan-13.3 {Tcl_ScanObjCmd, inline XPG case} {
673    list [catch {scan abc {%1$c%1$c}} msg] $msg
674} {1 {variable is assigned by multiple "%n$" conversion specifiers}}
675test scan-13.4 {Tcl_ScanObjCmd, inline XPG case} {
676    scan abc {%2$s%1$c}
677} {{} abc}
678test scan-13.5 {Tcl_ScanObjCmd, inline XPG case, underflow} {
679    scan abc {abc%5$c}
680} {}
681test scan-13.6 {Tcl_ScanObjCmd, inline XPG case} {
682    catch {scan abc {bogus%1$c%5$c%10$c}} msg
683    list [llength $msg] $msg
684} {10 {{} {} {} {} {} {} {} {} {} {}}}
685test scan-13.7 {Tcl_ScanObjCmd, inline XPG case lots of arguments} {
686    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}
687} {190 180 170 160 150 140 130 120 110 100 90 80 70 60 50 40 30 20 {} 10}
688test scan-13.8 {Tcl_ScanObjCmd, inline XPG case lots of arguments} {
689    set msg [scan "10 20 30" {%100$d %5$d %200$d}]
690    list [llength $msg] [lindex $msg 99] [lindex $msg 4] [lindex $msg 199]
691} {200 10 20 30}
692
693# Big test for correct ordering of data in [expr]
694
695proc testIEEE {} {
696    variable ieeeValues
697    binary scan [binary format dd -1.0 1.0] c* c
698    switch -exact -- $c {
699        {0 0 0 0 0 0 -16 -65 0 0 0 0 0 0 -16 63} {
700            # little endian
701            binary scan \x00\x00\x00\x00\x00\x00\xf0\xff d \
702                ieeeValues(-Infinity)
703            binary scan \x00\x00\x00\x00\x00\x00\xf0\xbf d \
704                ieeeValues(-Normal)
705            binary scan \x00\x00\x00\x00\x00\x00\x08\x80 d \
706                ieeeValues(-Subnormal)
707            binary scan \x00\x00\x00\x00\x00\x00\x00\x80 d \
708                ieeeValues(-0)
709            binary scan \x00\x00\x00\x00\x00\x00\x00\x00 d \
710                ieeeValues(+0)
711            binary scan \x00\x00\x00\x00\x00\x00\x08\x00 d \
712                ieeeValues(+Subnormal)
713            binary scan \x00\x00\x00\x00\x00\x00\xf0\x3f d \
714                ieeeValues(+Normal)
715            binary scan \x00\x00\x00\x00\x00\x00\xf0\x7f d \
716                ieeeValues(+Infinity)
717            binary scan \x00\x00\x00\x00\x00\x00\xf8\x7f d \
718                ieeeValues(NaN)
719            set ieeeValues(littleEndian) 1
720            return 1
721        }
722        {-65 -16 0 0 0 0 0 0 63 -16 0 0 0 0 0 0} {
723            binary scan \xff\xf0\x00\x00\x00\x00\x00\x00 d \
724                ieeeValues(-Infinity)
725            binary scan \xbf\xf0\x00\x00\x00\x00\x00\x00 d \
726                ieeeValues(-Normal)
727            binary scan \x80\x08\x00\x00\x00\x00\x00\x00 d \
728                ieeeValues(-Subnormal)
729            binary scan \x80\x00\x00\x00\x00\x00\x00\x00 d \
730                ieeeValues(-0)
731            binary scan \x00\x00\x00\x00\x00\x00\x00\x00 d \
732                ieeeValues(+0)
733            binary scan \x00\x08\x00\x00\x00\x00\x00\x00 d \
734                ieeeValues(+Subnormal)
735            binary scan \x3f\xf0\x00\x00\x00\x00\x00\x00 d \
736                ieeeValues(+Normal)
737            binary scan \x7f\xf0\x00\x00\x00\x00\x00\x00 d \
738                ieeeValues(+Infinity)
739            binary scan \x7f\xf8\x00\x00\x00\x00\x00\x00 d \
740                ieeeValues(NaN)
741            set ieeeValues(littleEndian) 0
742            return 1
743        }
744        default {
745            return 0
746        }
747    }
748}
749
750testConstraint ieeeFloatingPoint [testIEEE]
751
752# scan infinities - not working
753
754test scan-14.1 {infinity} ieeeFloatingPoint {
755    scan Inf %g d
756    set d
757} Inf
758test scan-14.2 {infinity} ieeeFloatingPoint {
759    scan -Inf %g d
760    set d
761} -Inf
762
763# TODO - also need to scan NaN's
764
765# cleanup
766::tcltest::cleanupTests
767return
768
769# Local Variables:
770# mode: tcl
771# End:
Note: See TracBrowser for help on using the repository browser.