⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string.test

📁 tcl是工具命令语言
💻 TEST
字号:
# Commands covered:  string## 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-1993 The Regents of the University of California.# Copyright (c) 1994 Sun Microsystems, Inc.# Copyright (c) 1998-1999 by Scriptics Corporation.# Copyright (c) 2001 by Kevin B. Kenny.  All rights reserved.## See the file "license.terms" for information on usage and redistribution# of this file, and for a DISCLAIMER OF ALL WARRANTIES.## RCS: @(#) $Id: string.test,v 1.36 2003/02/18 02:25:45 hobbs Exp $if {[lsearch [namespace children] ::tcltest] == -1} {    package require tcltest    namespace import -force ::tcltest::*}# Some tests require the testobj commandset ::tcltest::testConstraints(testobj) \	[expr {[info commands testobj] != {}}]test string-1.1 {error conditions} {    list [catch {string gorp a b} msg] $msg} {1 {bad option "gorp": must be bytelength, compare, equal, first, index, is, last, length, map, match, range, repeat, replace, tolower, toupper, totitle, trim, trimleft, trimright, wordend, or wordstart}}test string-1.2 {error conditions} {    list [catch {string} msg] $msg} {1 {wrong # args: should be "string option arg ?arg ...?"}}test string-2.1 {string compare, too few args} {    list [catch {string compare a} msg] $msg} {1 {wrong # args: should be "string compare ?-nocase? ?-length int? string1 string2"}}test string-2.2 {string compare, bad args} {    list [catch {string compare a b c} msg] $msg} {1 {bad option "a": must be -nocase or -length}}test string-2.3 {string compare, bad args} {    list [catch {string compare -length -nocase str1 str2} msg] $msg} {1 {expected integer but got "-nocase"}}test string-2.4 {string compare, too many args} {    list [catch {string compare -length 10 -nocase str1 str2 str3} msg] $msg} {1 {wrong # args: should be "string compare ?-nocase? ?-length int? string1 string2"}}test string-2.5 {string compare with length unspecified} {    list [catch {string compare -length 10 10} msg] $msg} {1 {wrong # args: should be "string compare ?-nocase? ?-length int? string1 string2"}}test string-2.6 {string compare} {    string compare abcde abdef} -1test string-2.7 {string compare, shortest method name} {    string c abcde ABCDE} 1test string-2.8 {string compare} {    string compare abcde abcde} 0test string-2.9 {string compare with length} {    string compare -length 2 abcde abxyz} 0test string-2.10 {string compare with special index} {    list [catch {string compare -length end-3 abcde abxyz} msg] $msg} {1 {expected integer but got "end-3"}}test string-2.11 {string compare, unicode} {    string compare ab\u7266 ab\u7267} -1test string-2.12 {string compare, high bit} {    # This test will fail if the underlying comparaison    # is using signed chars instead of unsigned chars.    # (like SunOS's default memcmp thus the compat/memcmp.c)    string compare "\x80" "@"    # Nb this tests works also in utf8 space because \x80 is    # translated into a 2 or more bytelength but whose first byte has    # the high bit set.} 1test string-2.13 {string compare -nocase} {    string compare -nocase abcde abdef} -1test string-2.14 {string compare -nocase} {    string c -nocase abcde ABCDE} 0test string-2.15 {string compare -nocase} {    string compare -nocase abcde abcde} 0test string-2.16 {string compare -nocase with length} {    string compare -length 2 -nocase abcde Abxyz} 0test string-2.17 {string compare -nocase with length} {    string compare -nocase -length 3 abcde Abxyz} -1test string-2.18 {string compare -nocase with length <= 0} {    string compare -nocase -length -1 abcde AbCdEf} -1test string-2.19 {string compare -nocase with excessive length} {    string compare -nocase -length 50 AbCdEf abcde} 1test string-2.20 {string compare -len unicode} {    # These are strings that are 6 BYTELENGTH long, but the length    # shouldn't make a different because there are actually 3 CHARS long    string compare -len 5 \334\334\334 \334\334\374} -1test string-2.21 {string compare -nocase with special index} {    list [catch {string compare -nocase -length end-3 Abcde abxyz} msg] $msg} {1 {expected integer but got "end-3"}}test string-2.22 {string compare, null strings} {    string compare "" ""} 0test string-2.23 {string compare, null strings} {    string compare "" foo} -1test string-2.24 {string compare, null strings} {    string compare foo ""} 1test string-2.25 {string compare -nocase, null strings} {    string compare -nocase "" ""} 0test string-2.26 {string compare -nocase, null strings} {    string compare -nocase "" foo} -1test string-2.27 {string compare -nocase, null strings} {    string compare -nocase foo ""} 1test string-2.28 {string compare with length, unequal strings} {    string compare -length 2 abc abde} 0test string-2.29 {string compare with length, unequal strings} {    string compare -length 2 ab abde} 0test string-2.30 {string compare with NUL character vs. other ASCII} {    # Be careful here, since UTF-8 rep comparison with memcmp() of    # these puts chars in the wrong order    string compare \x00 \x01} -1test string-2.31 {string compare, high bit} {    proc foo {} {string compare "a\x80" "a@"}    foo} 1test string-2.32 {string compare, high bit} {    proc foo {} {string compare "a\x00" "a\x01"}    foo} -1test string-2.33 {string compare, high bit} {    proc foo {} {string compare "\x00\x00" "\x00\x01"}    foo} -1# only need a few tests on equal, since it uses the same code as# string compare, but just modifies the return outputtest string-3.1 {string equal} {    string equal abcde abdef} 0test string-3.2 {string equal} {    string eq abcde ABCDE} 0test string-3.3 {string equal} {    string equal abcde abcde} 1test string-3.4 {string equal -nocase} {    string equal -nocase \334\334\334\334\374\374\374\374 \334\334\334\334\334\334\334\334} 1test string-3.5 {string equal -nocase} {    string equal -nocase abcde abdef} 0test string-3.6 {string equal -nocase} {    string eq -nocase abcde ABCDE} 1test string-3.7 {string equal -nocase} {    string equal -nocase abcde abcde} 1test string-3.8 {string equal with length, unequal strings} {    string equal -length 2 abc abde} 1test string-4.1 {string first, too few args} {    list [catch {string first a} msg] $msg} {1 {wrong # args: should be "string first subString string ?startIndex?"}}test string-4.2 {string first, bad args} {    list [catch {string first a b c} msg] $msg} {1 {bad index "c": must be integer or end?-integer?}}test string-4.3 {string first, too many args} {    list [catch {string first a b 5 d} msg] $msg} {1 {wrong # args: should be "string first subString string ?startIndex?"}}test string-4.4 {string first} {    string first bq abcdefgbcefgbqrs} 12test string-4.5 {string first} {    string fir bcd abcdefgbcefgbqrs} 1test string-4.6 {string first} {    string f b abcdefgbcefgbqrs} 1test string-4.7 {string first} {    string first xxx x123xx345xxx789xxx012} 9test string-4.8 {string first} {    string first "" x123xx345xxx789xxx012} -1test string-4.9 {string first, unicode} {    string first x abc\u7266x} 4test string-4.10 {string first, unicode} {    string first \u7266 abc\u7266x} 3test string-4.11 {string first, start index} {    string first \u7266 abc\u7266x 3} 3test string-4.12 {string first, start index} {    string first \u7266 abc\u7266x 4} -1test string-4.13 {string first, start index} {    string first \u7266 abc\u7266x end-2} 3test string-4.14 {string first, negative start index} {    string first b abc -1} 1test string-5.1 {string index} {    list [catch {string index} msg] $msg} {1 {wrong # args: should be "string index string charIndex"}}test string-5.2 {string index} {    list [catch {string index a b c} msg] $msg} {1 {wrong # args: should be "string index string charIndex"}}test string-5.3 {string index} {    string index abcde 0} atest string-5.4 {string index} {    string in abcde 4} etest string-5.5 {string index} {    string index abcde 5} {}test string-5.6 {string index} {    list [catch {string index abcde -10} msg] $msg} {0 {}}test string-5.7 {string index} {    list [catch {string index a xyz} msg] $msg} {1 {bad index "xyz": must be integer or end?-integer?}}test string-5.8 {string index} {    string index abc end} ctest string-5.9 {string index} {    string index abc end-1} btest string-5.10 {string index, unicode} {    string index abc\u7266d 4} dtest string-5.11 {string index, unicode} {    string index abc\u7266d 3} \u7266test string-5.12 {string index, unicode over char length, under byte length} {    string index \334\374\334\374 6} {}test string-5.13 {string index, bytearray object} {    string index [binary format a5 fuz] 0} ftest string-5.14 {string index, bytearray object} {    string index [binary format I* {0x50515253 0x52}] 3} Stest string-5.15 {string index, bytearray object} {    set b [binary format I* {0x50515253 0x52}]    set i1 [string index $b end-6]    set i2 [string index $b 1]    string compare $i1 $i2} 0test string-5.16 {string index, bytearray object with string obj shimmering} {    set str "0123456789\x00 abcdedfghi"    binary scan $str H* dump    string compare [string index $str 10] \x00} 0test string-5.17 {string index, bad integer} {    list [catch {string index "abc" 08} msg] $msg} {1 {bad index "08": must be integer or end?-integer? (looks like invalid octal number)}}test string-5.18 {string index, bad integer} {    list [catch {string index "abc" end-00289} msg] $msg} {1 {bad index "end-00289": must be integer or end?-integer? (looks like invalid octal number)}}test string-5.19 {string index, bytearray object out of bounds} {    string index [binary format I* {0x50515253 0x52}] -1} {}test string-5.20 {string index, bytearray object out of bounds} {    string index [binary format I* {0x50515253 0x52}] 20} {}proc largest_int {} {    # This will give us what the largest valid int on this machine is,    # so we can test for overflow properly below on >32 bit systems    set int 1    set exp 7; # assume we get at least 8 bits    while {$int > 0} { set int [expr {wide(1) << [incr exp]}] }    return [expr {$int-1}]}test string-6.1 {string is, too few args} {    list [catch {string is} msg] $msg} {1 {wrong # args: should be "string is class ?-strict? ?-failindex var? str"}}test string-6.2 {string is, too few args} {    list [catch {string is alpha} msg] $msg} {1 {wrong # args: should be "string is class ?-strict? ?-failindex var? str"}}test string-6.3 {string is, bad args} {    list [catch {string is alpha -failin str} msg] $msg} {1 {wrong # args: should be "string is alpha ?-strict? ?-failindex var? str"}}test string-6.4 {string is, too many args} {    list [catch {string is alpha -failin var -strict str more} msg] $msg} {1 {wrong # args: should be "string is class ?-strict? ?-failindex var? str"}}test string-6.5 {string is, class check} {    list [catch {string is bogus str} msg] $msg} {1 {bad class "bogus": must be alnum, alpha, ascii, control, boolean, digit, double, false, graph, integer, lower, print, punct, space, true, upper, wordchar, or xdigit}}test string-6.6 {string is, ambiguous class} {    list [catch {string is al str} msg] $msg} {1 {ambiguous class "al": must be alnum, alpha, ascii, control, boolean, digit, double, false, graph, integer, lower, print, punct, space, true, upper, wordchar, or xdigit}}test string-6.7 {string is alpha, all ok} {    string is alpha -strict -failindex var abc} 1test string-6.8 {string is, error in var} {    list [string is alpha -failindex var abc5def] $var} {0 3}test string-6.9 {string is, var shouldn't get set} {    catch {unset var}    list [catch {string is alpha -failindex var abc; set var} msg] $msg} {1 {can't read "var": no such variable}}test string-6.10 {string is, ok on empty} {    string is alpha {}} 1test string-6.11 {string is, -strict check against empty} {    string is alpha -strict {}} 0test string-6.12 {string is alnum, true} {    string is alnum abc123} 1test string-6.13 {string is alnum, false} {    list [string is alnum -failindex var abc1.23] $var} {0 4}test string-6.14 {string is alnum, unicode} {    string is alnum abc

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -