📄 scan.test
字号:
# Commands covered: scan## This file contains a collection of tests for one or more of the Tcl# built-in commands. Sourcing this file into Tcl runs the tests and# generates output for errors. No output means no errors were found.## Copyright (c) 1991-1994 The Regents of the University of California.# Copyright (c) 1994-1997 Sun Microsystems, Inc.# Copyright (c) 1998-1999 by Scriptics Corporation.## See the file "license.terms" for information on usage and redistribution# of this file, and for a DISCLAIMER OF ALL WARRANTIES.## RCS: @(#) $Id: scan.test,v 1.14 2002/06/22 04:19:47 dgp Exp $if {[lsearch [namespace children] ::tcltest] == -1} { package require tcltest 2 namespace import -force ::tcltest::*}::tcltest::testConstraint 64bitInts [expr {0x80000000 > 0}]test scan-1.1 {BuildCharSet, CharInSet} { list [scan foo {%[^o]} x] $x} {1 f}test scan-1.2 {BuildCharSet, CharInSet} { list [scan \]foo {%[]f]} x] $x} {1 \]f}test scan-1.3 {BuildCharSet, CharInSet} { list [scan abc-def {%[a-c]} x] $x} {1 abc}test scan-1.4 {BuildCharSet, CharInSet} { list [scan abc-def {%[a-c]} x] $x} {1 abc}test scan-1.5 {BuildCharSet, CharInSet} { list [scan -abc-def {%[-ac]} x] $x} {1 -a}test scan-1.6 {BuildCharSet, CharInSet} { list [scan -abc-def {%[ac-]} x] $x} {1 -a}test scan-1.7 {BuildCharSet, CharInSet} { list [scan abc-def {%[c-a]} x] $x} {1 abc}test scan-1.8 {BuildCharSet, CharInSet} { list [scan def-abc {%[^c-a]} x] $x} {1 def-}test scan-1.9 {BuildCharSet, CharInSet no match} { catch {unset x} list [scan {= f} {= %[TF]} x] [info exists x]} {0 0}test scan-2.1 {ReleaseCharSet} { list [scan abcde {%[abc]} x] $x} {1 abc}test scan-2.2 {ReleaseCharSet} { list [scan abcde {%[a-c]} x] $x} {1 abc}test scan-3.1 {ValidateFormat} { list [catch {scan {} {%d%1$d} x} msg] $msg} {1 {cannot mix "%" and "%n$" conversion specifiers}}test scan-3.2 {ValidateFormat} { list [catch {scan {} {%d%1$d} x} msg] $msg} {1 {cannot mix "%" and "%n$" conversion specifiers}}test scan-3.3 {ValidateFormat} { list [catch {scan {} {%2$d%d} x} msg] $msg} {1 {"%n$" argument index out of range}}test scan-3.4 {ValidateFormat} { # degenerate case, before changed from 8.2 to 8.3 list [catch {scan {} %d} msg] $msg} {0 {}}test scan-3.5 {ValidateFormat} { list [catch {scan {} {%10c} a} msg] $msg} {1 {field width may not be specified in %c conversion}}test scan-3.6 {ValidateFormat} { list [catch {scan {} {%*1$d} a} msg] $msg} {1 {bad scan conversion character "$"}}test scan-3.7 {ValidateFormat} { list [catch {scan {} {%1$d%1$d} a} msg] $msg} {1 {variable is assigned by multiple "%n$" conversion specifiers}}test scan-3.8 {ValidateFormat} { list [catch {scan {} a x} msg] $msg} {1 {variable is not assigned by any conversion specifiers}}test scan-3.9 {ValidateFormat} { list [catch {scan {} {%2$s} x y} msg] $msg} {1 {variable is not assigned by any conversion specifiers}}test scan-3.10 {ValidateFormat} { list [catch {scan {} {%[a} x} msg] $msg} {1 {unmatched [ in format string}}test scan-3.11 {ValidateFormat} { list [catch {scan {} {%[^a} x} msg] $msg} {1 {unmatched [ in format string}}test scan-3.12 {ValidateFormat} { list [catch {scan {} {%[]a} x} msg] $msg} {1 {unmatched [ in format string}}test scan-3.13 {ValidateFormat} { list [catch {scan {} {%[^]a} x} msg] $msg} {1 {unmatched [ in format string}}test scan-4.1 {Tcl_ScanObjCmd, argument checks} { list [catch {scan} msg] $msg} {1 {wrong # args: should be "scan string format ?varName varName ...?"}}test scan-4.2 {Tcl_ScanObjCmd, argument checks} { list [catch {scan string} msg] $msg} {1 {wrong # args: should be "scan string format ?varName varName ...?"}}test scan-4.3 {Tcl_ScanObjCmd, argument checks} { # degenerate case, before changed from 8.2 to 8.3 list [catch {scan string format} msg] $msg} {0 {}}test scan-4.4 {Tcl_ScanObjCmd, whitespace} { list [scan { abc def } {%s%s} x y] $x $y} {2 abc def}test scan-4.5 {Tcl_ScanObjCmd, whitespace} { list [scan { abc def } { %s %s } x y] $x $y} {2 abc def}test scan-4.6 {Tcl_ScanObjCmd, whitespace} { list [scan { abc def } { %s %s } x y] $x $y} {2 abc def}test scan-4.7 {Tcl_ScanObjCmd, literals} { # degenerate case, before changed from 8.2 to 8.3 scan { abc def } { abc def }} {}test scan-4.8 {Tcl_ScanObjCmd, literals} { set x {} list [scan { abcg} { abc def %1s} x] $x} {0 {}}test scan-4.9 {Tcl_ScanObjCmd, literals} { list [scan { abc%defghi} { abc %% def%n } x] $x} {1 10}test scan-4.10 {Tcl_ScanObjCmd, assignment suppression} { list [scan { abc def } { %*c%s def } x] $x} {1 bc}test scan-4.11 {Tcl_ScanObjCmd, XPG3-style} { list [scan { abc def } {%2$s %1$s} x y] $x $y} {2 def abc}test scan-4.12 {Tcl_ScanObjCmd, width specifiers} { list [scan {abc123456789012} {%3s%3d%3f%3[0-9]%s} a b c d e] $a $b $c $d $e} {5 abc 123 456.0 789 012}test scan-4.13 {Tcl_ScanObjCmd, width specifiers} { list [scan {abc123456789012} {%3s%3d%3f%3[0-9]%s} a b c d e] $a $b $c $d $e} {5 abc 123 456.0 789 012}test scan-4.14 {Tcl_ScanObjCmd, underflow} { set x {} list [scan {a} {a%d} x] $x} {-1 {}}test scan-4.15 {Tcl_ScanObjCmd, underflow} { set x {} list [scan {} {a%d} x] $x} {-1 {}}test scan-4.16 {Tcl_ScanObjCmd, underflow} { set x {} list [scan {ab} {a%d} x] $x} {0 {}}test scan-4.17 {Tcl_ScanObjCmd, underflow} { set x {} list [scan {a } {a%d} x] $x} {-1 {}}test scan-4.18 {Tcl_ScanObjCmd, skipping whitespace} { list [scan { b} {%c%s} x y] $x $y} {2 32 b}test scan-4.19 {Tcl_ScanObjCmd, skipping whitespace} { list [scan { b} {%[^b]%s} x y] $x $y} {2 { } b}test scan-4.20 {Tcl_ScanObjCmd, string scanning} { list [scan {abc def} {%s} x] $x} {1 abc}test scan-4.21 {Tcl_ScanObjCmd, string scanning} { list [scan {abc def} {%0s} x] $x} {1 abc}test scan-4.22 {Tcl_ScanObjCmd, string scanning} { list [scan {abc def} {%2s} x] $x} {1 ab}test scan-4.23 {Tcl_ScanObjCmd, string scanning} { list [scan {abc def} {%*s%n} x] $x} {1 3}test scan-4.24 {Tcl_ScanObjCmd, charset scanning} { list [scan {abcdef} {%[a-c]} x] $x} {1 abc}test scan-4.25 {Tcl_ScanObjCmd, charset scanning} { list [scan {abcdef} {%0[a-c]} x] $x} {1 abc}test scan-4.26 {Tcl_ScanObjCmd, charset scanning} { list [scan {abcdef} {%2[a-c]} x] $x} {1 ab}test scan-4.27 {Tcl_ScanObjCmd, charset scanning} { list [scan {abcdef} {%*[a-c]%n} x] $x} {1 3}test scan-4.28 {Tcl_ScanObjCmd, character scanning} { list [scan {abcdef} {%c} x] $x} {1 97}test scan-4.29 {Tcl_ScanObjCmd, character scanning} { list [scan {abcdef} {%*c%n} x] $x} {1 1}test scan-4.30 {Tcl_ScanObjCmd, base-10 integer scanning} { set x {} list [scan {1234567890a} {%3d} x] $x} {1 123}test scan-4.31 {Tcl_ScanObjCmd, base-10 integer scanning} { set x {} list [scan {1234567890a} {%d} x] $x} {1 1234567890}test scan-4.32 {Tcl_ScanObjCmd, base-10 integer scanning} { set x {} list [scan {01234567890a} {%d} x] $x} {1 1234567890}test scan-4.33 {Tcl_ScanObjCmd, base-10 integer scanning} { set x {} list [scan {+01234} {%d} x] $x} {1 1234}test scan-4.34 {Tcl_ScanObjCmd, base-10 integer scanning} { set x {} list [scan {-01234} {%d} x] $x} {1 -1234}test scan-4.35 {Tcl_ScanObjCmd, base-10 integer scanning} { set x {} list [scan {a01234} {%d} x] $x} {0 {}}test scan-4.36 {Tcl_ScanObjCmd, base-10 integer scanning} { set x {} list [scan {0x10} {%d} x] $x} {1 0}test scan-4.37 {Tcl_ScanObjCmd, base-8 integer scanning} { set x {} list [scan {012345678} {%o} x] $x} {1 342391}test scan-4.38 {Tcl_ScanObjCmd, base-8 integer scanning} { set x {} list [scan {+1238 -1239 123a} {%o%*s%o%*s%o} x y z] $x $y $z} {3 83 -83 83}test scan-4.39 {Tcl_ScanObjCmd, base-16 integer scanning} { set x {} list [scan {+1238 -123a 0123} {%x%x%x} x y z] $x $y $z} {3 4664 -4666 291}test scan-4.40 {Tcl_ScanObjCmd, base-16 integer scanning} { # The behavior changed in 8.4a4/8.3.4cvs (6 Feb) to correctly # return '1' for 0x1 scanned via %x, to comply with 8.0 and C scanf. # Bug #495213 set x {} list [scan {aBcDeF AbCdEf 0x1} {%x%x%x} x y z] $x $y $z} {3 11259375 11259375 1}test scan-4.40.1 {Tcl_ScanObjCmd, base-16 integer scanning} { set x {} list [scan {0xF 0x00A0B 0X0XF} {%x %x %x} x y z] $x $y $z} {3 15 2571 0}test scan-4.40.2 {Tcl_ScanObjCmd, base-16 integer scanning} { catch {unset x} list [scan {xF} {%x} x] [info exists x]} {0 0}test scan-4.41 {Tcl_ScanObjCmd, base-unknown integer scanning} { set x {} list [scan {10 010 0x10} {%i%i%i} x y z] $x $y $z} {3 10 8 16}test scan-4.42 {Tcl_ScanObjCmd, base-unknown integer scanning} { set x {} list [scan {10 010 0X10} {%i%i%i} x y z] $x $y $z} {3 10 8 16}test scan-4.43 {Tcl_ScanObjCmd, integer scanning, odd cases} { set x {} list [scan {+ } {%i} x] $x} {0 {}}test scan-4.44 {Tcl_ScanObjCmd, integer scanning, odd cases} { set x {} list [scan {+} {%i} x] $x} {-1 {}}test scan-4.45 {Tcl_ScanObjCmd, integer scanning, odd cases} { set x {} list [scan {0x} {%i%s} x y] $x $y} {2 0 x}test scan-4.46 {Tcl_ScanObjCmd, integer scanning, odd cases} { set x {} list [scan {0X} {%i%s} x y] $x $y} {2 0 X}test scan-4.47 {Tcl_ScanObjCmd, integer scanning, suppressed} { set x {} list [scan {123def} {%*i%s} x] $x} {1 def}test scan-4.48 {Tcl_ScanObjCmd, float scanning} { list [scan {1 2 3} {%e %f %g} x y z] $x $y $z} {3 1.0 2.0 3.0}test scan-4.49 {Tcl_ScanObjCmd, float scanning} { list [scan {.1 0.2 3.} {%e %f %g} x y z] $x $y $z} {3 0.1 0.2 3.0}test scan-4.50 {Tcl_ScanObjCmd, float scanning} { list [scan {1234567890a} %f x] $x} {1 1234567890.0}test scan-4.51 {Tcl_ScanObjCmd, float scanning} { list [scan {+123+45} %f x] $x} {1 123.0}test scan-4.52 {Tcl_ScanObjCmd, float scanning} { list [scan {-123+45} %f x] $x} {1 -123.0}test scan-4.53 {Tcl_ScanObjCmd, float scanning} { list [scan {1.0e1} %f x] $x} {1 10.0}test scan-4.54 {Tcl_ScanObjCmd, float scanning} { list [scan {1.0e-1} %f x] $x} {1 0.1}test scan-4.55 {Tcl_ScanObjCmd, odd cases} { set x {} list [scan {+} %f x] $x} {-1 {}}test scan-4.56 {Tcl_ScanObjCmd, odd cases} { set x {} list [scan {1.0e} %f%s x y] $x $y} {2 1.0 e}test scan-4.57 {Tcl_ScanObjCmd, odd cases} { set x {} list [scan {1.0e+} %f%s x y] $x $y} {2 1.0 e+}test scan-4.58 {Tcl_ScanObjCmd, odd cases} { set x {} set y {} list [scan {e1} %f%s x y] $x $y} {0 {} {}}test scan-4.59 {Tcl_ScanObjCmd, float scanning} { list [scan {1.0e-1x} %*f%n x] $x} {1 6}test scan-4.60 {Tcl_ScanObjCmd, set errors} { set x {} set y {} catch {unset z}; array set z {} set result [list [catch {scan {abc def ghi} {%s%s%s} x z y} msg] \ $msg $x $y] unset z set result} {1 {couldn't set variable "z"} abc ghi}test scan-4.61 {Tcl_ScanObjCmd, set errors} { set x {} catch {unset y}; array set y {} catch {unset z}; array set z {} set result [list [catch {scan {abc def ghi} {%s%s%s} x z y} msg] \ $msg $x] unset y unset z set result} {1 {couldn't set variable "z"couldn't set variable "y"} abc}# procedure that returns the range of integers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -