| 1 |
# Commands covered: string |
| 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 |
# This differs from the original string tests in that the tests call |
| 8 |
# things in procs, which uses the compiled string code instead of |
| 9 |
# the runtime parse string code. The tests of import should match |
| 10 |
# their equivalent number in string.test. |
| 11 |
# |
| 12 |
# Copyright (c) 2001 by ActiveState Corporation. |
| 13 |
# Copyright (c) 2001 by Kevin B. Kenny. All rights reserved. |
| 14 |
# |
| 15 |
# See the file "license.terms" for information on usage and redistribution |
| 16 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 17 |
|
| 18 |
if {"::tcltest" ni [namespace children]} { |
| 19 |
package require tcltest 2.5 |
| 20 |
namespace import -force ::tcltest::* |
| 21 |
} |
| 22 |
|
| 23 |
::tcltest::loadTestedCommands |
| 24 |
catch [list package require -exact Tcltest [info patchlevel]] |
| 25 |
|
| 26 |
# Some tests require the testobj command |
| 27 |
|
| 28 |
testConstraint testobj [expr {[info commands testobj] != {}}] |
| 29 |
testConstraint memory [llength [info commands memory]] |
| 30 |
if {[testConstraint memory]} { |
| 31 |
proc getbytes {} { |
| 32 |
set lines [split [memory info] \n] |
| 33 |
return [lindex $lines 3 3] |
| 34 |
} |
| 35 |
proc leaktest {script {iterations 3}} { |
| 36 |
set end [getbytes] |
| 37 |
for {set i 0} {$i < $iterations} {incr i} { |
| 38 |
uplevel 1 $script |
| 39 |
set tmp $end |
| 40 |
set end [getbytes] |
| 41 |
} |
| 42 |
return [expr {$end - $tmp}] |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
test stringComp-1.1 {error conditions} { |
| 47 |
proc foo {} {string gorp a b} |
| 48 |
list [catch {foo} msg] $msg |
| 49 |
} {1 {unknown or ambiguous subcommand "gorp": must be bytelength, cat, compare, equal, first, index, is, last, length, map, match, range, repeat, replace, reverse, tolower, totitle, toupper, trim, trimleft, trimright, wordend, or wordstart}} |
| 50 |
test stringComp-1.2 {error conditions} { |
| 51 |
proc foo {} {string} |
| 52 |
list [catch {foo} msg] $msg |
| 53 |
} {1 {wrong # args: should be "string subcommand ?arg ...?"}} |
| 54 |
test stringComp-1.3 {error condition - undefined method during compile} { |
| 55 |
# We don't want this to complain about 'never' because it may never |
| 56 |
# be called, or string may get redefined. This must compile OK. |
| 57 |
proc foo {str i} { |
| 58 |
if {"yes" == "no"} { string never called but complains here } |
| 59 |
string index $str $i |
| 60 |
} |
| 61 |
foo abc 0 |
| 62 |
} a |
| 63 |
|
| 64 |
## Test string compare|equal over equal constraints |
| 65 |
## Use result for string compare, and negate it for string equal |
| 66 |
## The body will be tested both in and outside a proc |
| 67 |
set i 0 |
| 68 |
foreach {tname tbody tresult tcode} { |
| 69 |
{too few args} { |
| 70 |
string compare a |
| 71 |
} {wrong # args: should be "string compare ?-nocase? ?-length int? string1 string2"} {error} |
| 72 |
{bad args} { |
| 73 |
string compare a b c |
| 74 |
} {bad option "a": must be -nocase or -length} {error} |
| 75 |
{bad args} { |
| 76 |
string compare -length -nocase str1 str2 |
| 77 |
} {expected integer but got "-nocase"} {error} |
| 78 |
{too many args} { |
| 79 |
string compare -length 10 -nocase str1 str2 str3 |
| 80 |
} {wrong # args: should be "string compare ?-nocase? ?-length int? string1 string2"} {error} |
| 81 |
{compare with length unspecified} { |
| 82 |
string compare -length 10 10 |
| 83 |
} {wrong # args: should be "string compare ?-nocase? ?-length int? string1 string2"} {error} |
| 84 |
{basic operation fail} { |
| 85 |
string compare abcde abdef |
| 86 |
} {-1} {} |
| 87 |
{basic operation success} { |
| 88 |
string compare abcde abcde |
| 89 |
} {0} {} |
| 90 |
{with length} { |
| 91 |
string compare -length 2 abcde abxyz |
| 92 |
} {0} {} |
| 93 |
{with special index} { |
| 94 |
string compare -length end-3 abcde abxyz |
| 95 |
} {expected integer but got "end-3"} {error} |
| 96 |
{unicode} { |
| 97 |
string compare ab\u7266 ab\u7267 |
| 98 |
} {-1} {} |
| 99 |
{unicode} {string compare \334 \u00dc} 0 {} |
| 100 |
{unicode} {string compare \334 \u00fc} -1 {} |
| 101 |
{unicode} {string compare \334\334\334\374\374 \334\334\334\334\334} 1 {} |
| 102 |
{high bit} { |
| 103 |
# This test fails if the underlying comparison |
| 104 |
# is using signed chars instead of unsigned chars. |
| 105 |
# (like SunOS's default memcmp thus the compat/memcmp.c) |
| 106 |
string compare "\x80" "@" |
| 107 |
# Nb this tests works also in utf-8 space because \x80 is |
| 108 |
# translated into a 2 or more bytelength but whose first byte has |
| 109 |
# the high bit set. |
| 110 |
} {1} {} |
| 111 |
{-nocase 1} {string compare -nocase abcde abdef} {-1} {} |
| 112 |
{-nocase 2} {string compare -nocase abcde Abdef} {-1} {} |
| 113 |
{-nocase 3} {string compare -nocase abcde ABCDE} {0} {} |
| 114 |
{-nocase 4} {string compare -nocase abcde abcde} {0} {} |
| 115 |
{-nocase unicode} { |
| 116 |
string compare -nocase \334 \u00dc |
| 117 |
} 0 {} |
| 118 |
{-nocase unicode} { |
| 119 |
string compare -nocase \334\334\334\374\u00fc \334\334\334\334\334 |
| 120 |
} 0 {} |
| 121 |
{-nocase with length} { |
| 122 |
string compare -length 2 -nocase abcde Abxyz |
| 123 |
} {0} {} |
| 124 |
{-nocase with length} { |
| 125 |
string compare -nocase -length 3 abcde Abxyz |
| 126 |
} {-1} {} |
| 127 |
{-nocase with length <= 0} { |
| 128 |
string compare -nocase -length -1 abcde AbCdEf |
| 129 |
} {-1} {} |
| 130 |
{-nocase with excessive length} { |
| 131 |
string compare -nocase -length 50 AbCdEf abcde |
| 132 |
} {1} {} |
| 133 |
{-len unicode} { |
| 134 |
# These are strings that are 6 BYTELENGTH long, but the length |
| 135 |
# shouldn't make a different because there are actually 3 CHARS long |
| 136 |
string compare -len 5 \334\334\334 \334\334\374 |
| 137 |
} -1 {} |
| 138 |
{-nocase with special index} { |
| 139 |
string compare -nocase -length end-3 Abcde abxyz |
| 140 |
} {expected integer but got "end-3"} error |
| 141 |
{null strings} { |
| 142 |
string compare "" "" |
| 143 |
} 0 {} |
| 144 |
{null strings} { |
| 145 |
string compare "" foo |
| 146 |
} -1 {} |
| 147 |
{null strings} { |
| 148 |
string compare foo "" |
| 149 |
} 1 {} |
| 150 |
{-nocase null strings} { |
| 151 |
string compare -nocase "" "" |
| 152 |
} 0 {} |
| 153 |
{-nocase null strings} { |
| 154 |
string compare -nocase "" foo |
| 155 |
} -1 {} |
| 156 |
{-nocase null strings} { |
| 157 |
string compare -nocase foo "" |
| 158 |
} 1 {} |
| 159 |
{with length, unequal strings, partial first string} { |
| 160 |
string compare -length 2 abc abde |
| 161 |
} 0 {} |
| 162 |
{with length, unequal strings 2, full first string} { |
| 163 |
string compare -length 2 ab abde |
| 164 |
} 0 {} |
| 165 |
{with NUL character vs. other ASCII} { |
| 166 |
# Be careful here, since UTF-8 rep comparison with memcmp() of |
| 167 |
# these puts chars in the wrong order |
| 168 |
string compare \x00 \x01 |
| 169 |
} -1 {} |
| 170 |
{high bit} { |
| 171 |
string compare "a\x80" "a@" |
| 172 |
} 1 {} |
| 173 |
{high bit} { |
| 174 |
string compare "a\x00" "a\x01" |
| 175 |
} -1 {} |
| 176 |
{high bit} { |
| 177 |
string compare "\x00\x00" "\x00\x01" |
| 178 |
} -1 {} |
| 179 |
{binary equal} { |
| 180 |
string compare [binary format a100 0] [binary format a100 0] |
| 181 |
} 0 {} |
| 182 |
{binary neq} { |
| 183 |
string compare [binary format a100a 0 1] [binary format a100a 0 0] |
| 184 |
} 1 {} |
| 185 |
{binary neq inequal length} { |
| 186 |
string compare [binary format a20a 0 1] [binary format a100a 0 0] |
| 187 |
} 1 {} |
| 188 |
} { |
| 189 |
if {$tname eq ""} { continue } |
| 190 |
if {$tcode eq ""} { set tcode ok } |
| 191 |
test stringComp-2.[incr i] "string compare, $tname" \ |
| 192 |
-body [list eval $tbody] \ |
| 193 |
-returnCodes $tcode -result $tresult |
| 194 |
test stringComp-2.[incr i] "string compare bc, $tname" \ |
| 195 |
-body "[list proc foo {} $tbody];foo" \ |
| 196 |
-returnCodes $tcode -result $tresult |
| 197 |
if {"error" ni $tcode} { |
| 198 |
set tresult [expr {!$tresult}] |
| 199 |
} else { |
| 200 |
set tresult [string map {compare equal} $tresult] |
| 201 |
} |
| 202 |
set tbody [string map {compare equal} $tbody] |
| 203 |
test stringComp-2.[incr i] "string equal, $tname" \ |
| 204 |
-body [list eval $tbody] \ |
| 205 |
-returnCodes $tcode -result $tresult |
| 206 |
test stringComp-2.[incr i] "string equal bc, $tname" \ |
| 207 |
-body "[list proc foo {} $tbody];foo" \ |
| 208 |
-returnCodes $tcode -result $tresult |
| 209 |
} |
| 210 |
|
| 211 |
# need a few extra tests short abbr cmd |
| 212 |
test stringComp-3.1 {string compare, shortest method name} { |
| 213 |
proc foo {} {string co abcde ABCDE} |
| 214 |
foo |
| 215 |
} 1 |
| 216 |
test stringComp-3.2 {string equal, shortest method name} { |
| 217 |
proc foo {} {string e abcde ABCDE} |
| 218 |
foo |
| 219 |
} 0 |
| 220 |
test stringComp-3.3 {string equal -nocase} { |
| 221 |
proc foo {} {string eq -nocase abcde ABCDE} |
| 222 |
foo |
| 223 |
} 1 |
| 224 |
|
| 225 |
test stringComp-4.1 {string first, too few args} { |
| 226 |
proc foo {} {string first a} |
| 227 |
list [catch {foo} msg] $msg |
| 228 |
} {1 {wrong # args: should be "string first needleString haystackString ?startIndex?"}} |
| 229 |
test stringComp-4.2 {string first, bad args} { |
| 230 |
proc foo {} {string first a b c} |
| 231 |
list [catch {foo} msg] $msg |
| 232 |
} {1 {bad index "c": must be integer?[+-]integer? or end?[+-]integer?}} |
| 233 |
test stringComp-4.3 {string first, too many args} { |
| 234 |
proc foo {} {string first a b 5 d} |
| 235 |
list [catch {foo} msg] $msg |
| 236 |
} {1 {wrong # args: should be "string first needleString haystackString ?startIndex?"}} |
| 237 |
test stringComp-4.4 {string first} { |
| 238 |
proc foo {} {string first bq abcdefgbcefgbqrs} |
| 239 |
foo |
| 240 |
} 12 |
| 241 |
test stringComp-4.5 {string first} { |
| 242 |
proc foo {} {string fir bcd abcdefgbcefgbqrs} |
| 243 |
foo |
| 244 |
} 1 |
| 245 |
test stringComp-4.6 {string first} { |
| 246 |
proc foo {} {string f b abcdefgbcefgbqrs} |
| 247 |
foo |
| 248 |
} 1 |
| 249 |
test stringComp-4.7 {string first} { |
| 250 |
proc foo {} {string first xxx x123xx345xxx789xxx012} |
| 251 |
foo |
| 252 |
} 9 |
| 253 |
test stringComp-4.8 {string first} { |
| 254 |
proc foo {} {string first "" x123xx345xxx789xxx012} |
| 255 |
foo |
| 256 |
} -1 |
| 257 |
test stringComp-4.9 {string first, unicode} { |
| 258 |
proc foo {} {string first x abc\u7266x} |
| 259 |
foo |
| 260 |
} 4 |
| 261 |
test stringComp-4.10 {string first, unicode} { |
| 262 |
proc foo {} {string first \u7266 abc\u7266x} |
| 263 |
foo |
| 264 |
} 3 |
| 265 |
test stringComp-4.11 {string first, start index} { |
| 266 |
proc foo {} {string first \u7266 abc\u7266x 3} |
| 267 |
foo |
| 268 |
} 3 |
| 269 |
test stringComp-4.12 {string first, start index} { |
| 270 |
proc foo {} {string first \u7266 abc\u7266x 4} |
| 271 |
foo |
| 272 |
} -1 |
| 273 |
test stringComp-4.13 {string first, start index} { |
| 274 |
proc foo {} {string first \u7266 abc\u7266x end-2} |
| 275 |
foo |
| 276 |
} 3 |
| 277 |
test stringComp-4.14 {string first, negative start index} { |
| 278 |
proc foo {} {string first b abc -1} |
| 279 |
foo |
| 280 |
} 1 |
| 281 |
|
| 282 |
test stringComp-5.1 {string index} { |
| 283 |
proc foo {} {string index} |
| 284 |
list [catch {foo} msg] $msg |
| 285 |
} {1 {wrong # args: should be "string index string charIndex"}} |
| 286 |
test stringComp-5.2 {string index} { |
| 287 |
proc foo {} {string index a b c} |
| 288 |
list [catch {foo} msg] $msg |
| 289 |
} {1 {wrong # args: should be "string index string charIndex"}} |
| 290 |
test stringComp-5.3 {string index} { |
| 291 |
proc foo {} {string index abcde 0} |
| 292 |
foo |
| 293 |
} a |
| 294 |
test stringComp-5.4 {string index} { |
| 295 |
proc foo {} {string in abcde 4} |
| 296 |
foo |
| 297 |
} e |
| 298 |
test stringComp-5.5 {string index} { |
| 299 |
proc foo {} {string index abcde 5} |
| 300 |
foo |
| 301 |
} {} |
| 302 |
test stringComp-5.6 {string index} { |
| 303 |
proc foo {} {string index abcde -10} |
| 304 |
list [catch {foo} msg] $msg |
| 305 |
} {0 {}} |
| 306 |
test stringComp-5.7 {string index} { |
| 307 |
proc foo {} {string index a xyz} |
| 308 |
list [catch {foo} msg] $msg |
| 309 |
} {1 {bad index "xyz": must be integer?[+-]integer? or end?[+-]integer?}} |
| 310 |
test stringComp-5.8 {string index} { |
| 311 |
proc foo {} {string index abc end} |
| 312 |
foo |
| 313 |
} c |
| 314 |
test stringComp-5.9 {string index} { |
| 315 |
proc foo {} {string index abc end-1} |
| 316 |
foo |
| 317 |
} b |
| 318 |
test stringComp-5.10 {string index, unicode} { |
| 319 |
proc foo {} {string index abc\u7266d 4} |
| 320 |
foo |
| 321 |
} d |
| 322 |
test stringComp-5.11 {string index, unicode} { |
| 323 |
proc foo {} {string index abc\u7266d 3} |
| 324 |
foo |
| 325 |
} \u7266 |
| 326 |
test stringComp-5.12 {string index, unicode over char length, under byte length} { |
| 327 |
proc foo {} {string index \334\374\334\374 6} |
| 328 |
foo |
| 329 |
} {} |
| 330 |
test stringComp-5.13 {string index, bytearray object} { |
| 331 |
proc foo {} {string index [binary format a5 fuz] 0} |
| 332 |
foo |
| 333 |
} f |
| 334 |
test stringComp-5.14 {string index, bytearray object} { |
| 335 |
proc foo {} {string index [binary format I* {0x50515253 0x52}] 3} |
| 336 |
foo |
| 337 |
} S |
| 338 |
test stringComp-5.15 {string index, bytearray object} { |
| 339 |
proc foo {} { |
| 340 |
set b [binary format I* {0x50515253 0x52}] |
| 341 |
set i1 [string index $b end-6] |
| 342 |
set i2 [string index $b 1] |
| 343 |
string compare $i1 $i2 |
| 344 |
} |
| 345 |
foo |
| 346 |
} 0 |
| 347 |
test stringComp-5.16 {string index, bytearray object with string obj shimmering} { |
| 348 |
proc foo {} { |
| 349 |
set str "0123456789\x00 abcdedfghi" |
| 350 |
binary scan $str H* dump |
| 351 |
string compare [string index $str 10] \x00 |
| 352 |
} |
| 353 |
foo |
| 354 |
} 0 |
| 355 |
test stringComp-5.17 {string index, bad integer} -body { |
| 356 |
proc foo {} {string index "abc" 0o8} |
| 357 |
list [catch {foo} msg] $msg |
| 358 |
} -match glob -result {1 {*invalid octal number*}} |
| 359 |
test stringComp-5.18 {string index, bad integer} -body { |
| 360 |
proc foo {} {string index "abc" end-0o0289} |
| 361 |
list [catch {foo} msg] $msg |
| 362 |
} -match glob -result {1 {*invalid octal number*}} |
| 363 |
test stringComp-5.19 {string index, bytearray object out of bounds} { |
| 364 |
proc foo {} {string index [binary format I* {0x50515253 0x52}] -1} |
| 365 |
foo |
| 366 |
} {} |
| 367 |
test stringComp-5.20 {string index, bytearray object out of bounds} { |
| 368 |
proc foo {} {string index [binary format I* {0x50515253 0x52}] 20} |
| 369 |
foo |
| 370 |
} {} |
| 371 |
|
| 372 |
|
| 373 |
proc largest_int {} { |
| 374 |
# This will give us what the largest valid int on this machine is, |
| 375 |
# so we can test for overflow properly below on >32 bit systems |
| 376 |
set int 1 |
| 377 |
set exp 7; # assume we get at least 8 bits |
| 378 |
while {$int > 0} { set int [expr {1 << [incr exp]}] } |
| 379 |
return [expr {$int-1}] |
| 380 |
} |
| 381 |
|
| 382 |
## string is |
| 383 |
## not yet bc |
| 384 |
|
| 385 |
catch {rename largest_int {}} |
| 386 |
|
| 387 |
## string last |
| 388 |
## not yet bc |
| 389 |
|
| 390 |
## string length |
| 391 |
## not yet bc |
| 392 |
test stringComp-8.1 {string bytelength} { |
| 393 |
proc foo {} {string bytelength} |
| 394 |
list [catch {foo} msg] $msg |
| 395 |
} {1 {wrong # args: should be "string bytelength string"}} |
| 396 |
test stringComp-8.2 {string bytelength} { |
| 397 |
proc foo {} {string bytelength a b} |
| 398 |
list [catch {foo} msg] $msg |
| 399 |
} {1 {wrong # args: should be "string bytelength string"}} |
| 400 |
test stringComp-8.3 {string bytelength} { |
| 401 |
proc foo {} {string bytelength "\u00c7"} |
| 402 |
foo |
| 403 |
} 2 |
| 404 |
test stringComp-8.4 {string bytelength} { |
| 405 |
proc foo {} {string b ""} |
| 406 |
foo |
| 407 |
} 0 |
| 408 |
|
| 409 |
## string length |
| 410 |
## |
| 411 |
test stringComp-9.1 {string length} { |
| 412 |
proc foo {} {string length} |
| 413 |
list [catch {foo} msg] $msg |
| 414 |
} {1 {wrong # args: should be "string length string"}} |
| 415 |
test stringComp-9.2 {string length} { |
| 416 |
proc foo {} {string length a b} |
| 417 |
list [catch {foo} msg] $msg |
| 418 |
} {1 {wrong # args: should be "string length string"}} |
| 419 |
test stringComp-9.3 {string length} { |
| 420 |
proc foo {} {string length "a little string"} |
| 421 |
foo |
| 422 |
} 15 |
| 423 |
test stringComp-9.4 {string length} { |
| 424 |
proc foo {} {string le ""} |
| 425 |
foo |
| 426 |
} 0 |
| 427 |
test stringComp-9.5 {string length, unicode} { |
| 428 |
proc foo {} {string le "abcd\u7266"} |
| 429 |
foo |
| 430 |
} 5 |
| 431 |
test stringComp-9.6 {string length, bytearray object} { |
| 432 |
proc foo {} {string length [binary format a5 foo]} |
| 433 |
foo |
| 434 |
} 5 |
| 435 |
test stringComp-9.7 {string length, bytearray object} { |
| 436 |
proc foo {} {string length [binary format I* {0x50515253 0x52}]} |
| 437 |
foo |
| 438 |
} 8 |
| 439 |
|
| 440 |
## string map |
| 441 |
## not yet bc |
| 442 |
|
| 443 |
## string match |
| 444 |
## |
| 445 |
test stringComp-11.1 {string match, too few args} { |
| 446 |
proc foo {} {string match a} |
| 447 |
list [catch {foo} msg] $msg |
| 448 |
} {1 {wrong # args: should be "string match ?-nocase? pattern string"}} |
| 449 |
test stringComp-11.2 {string match, too many args} { |
| 450 |
proc foo {} {string match a b c d} |
| 451 |
list [catch {foo} msg] $msg |
| 452 |
} {1 {wrong # args: should be "string match ?-nocase? pattern string"}} |
| 453 |
test stringComp-11.3 {string match} { |
| 454 |
proc foo {} {string match abc abc} |
| 455 |
foo |
| 456 |
} 1 |
| 457 |
test stringComp-11.4 {string match} { |
| 458 |
proc foo {} {string mat abc abd} |
| 459 |
foo |
| 460 |
} 0 |
| 461 |
test stringComp-11.5 {string match} { |
| 462 |
proc foo {} {string match ab*c abc} |
| 463 |
foo |
| 464 |
} 1 |
| 465 |
test stringComp-11.6 {string match} { |
| 466 |
proc foo {} {string match ab**c abc} |
| 467 |
foo |
| 468 |
} 1 |
| 469 |
test stringComp-11.7 {string match} { |
| 470 |
proc foo {} {string match ab* abcdef} |
| 471 |
foo |
| 472 |
} 1 |
| 473 |
test stringComp-11.8 {string match} { |
| 474 |
proc foo {} {string match *c abc} |
| 475 |
foo |
| 476 |
} 1 |
| 477 |
test stringComp-11.9 {string match} { |
| 478 |
proc foo {} {string match *3*6*9 0123456789} |
| 479 |
foo |
| 480 |
} 1 |
| 481 |
test stringComp-11.10 {string match} { |
| 482 |
proc foo {} {string match *3*6*9 01234567890} |
| 483 |
foo |
| 484 |
} 0 |
| 485 |
test stringComp-11.11 {string match} { |
| 486 |
proc foo {} {string match a?c abc} |
| 487 |
foo |
| 488 |
} 1 |
| 489 |
test stringComp-11.12 {string match} { |
| 490 |
proc foo {} {string match a??c abc} |
| 491 |
foo |
| 492 |
} 0 |
| 493 |
test stringComp-11.13 {string match} { |
| 494 |
proc foo {} {string match ?1??4???8? 0123456789} |
| 495 |
foo |
| 496 |
} 1 |
| 497 |
test stringComp-11.14 {string match} { |
| 498 |
proc foo {} {string match {[abc]bc} abc} |
| 499 |
foo |
| 500 |
} 1 |
| 501 |
test stringComp-11.15 {string match} { |
| 502 |
proc foo {} {string match {a[abc]c} abc} |
| 503 |
foo |
| 504 |
} 1 |
| 505 |
test stringComp-11.16 {string match} { |
| 506 |
proc foo {} {string match {a[xyz]c} abc} |
| 507 |
foo |
| 508 |
} 0 |
| 509 |
test stringComp-11.17 {string match} { |
| 510 |
proc foo {} {string match {12[2-7]45} 12345} |
| 511 |
foo |
| 512 |
} 1 |
| 513 |
test stringComp-11.18 {string match} { |
| 514 |
proc foo {} {string match {12[ab2-4cd]45} 12345} |
| 515 |
foo |
| 516 |
} 1 |
| 517 |
test stringComp-11.19 {string match} { |
| 518 |
proc foo {} {string match {12[ab2-4cd]45} 12b45} |
| 519 |
foo |
| 520 |
} 1 |
| 521 |
test stringComp-11.20 {string match} { |
| 522 |
proc foo {} {string match {12[ab2-4cd]45} 12d45} |
| 523 |
foo |
| 524 |
} 1 |
| 525 |
test stringComp-11.21 {string match} { |
| 526 |
proc foo {} {string match {12[ab2-4cd]45} 12145} |
| 527 |
foo |
| 528 |
} 0 |
| 529 |
test stringComp-11.22 {string match} { |
| 530 |
proc foo {} {string match {12[ab2-4cd]45} 12545} |
| 531 |
foo |
| 532 |
} 0 |
| 533 |
test stringComp-11.23 {string match} { |
| 534 |
proc foo {} {string match {a\*b} a*b} |
| 535 |
foo |
| 536 |
} 1 |
| 537 |
test stringComp-11.24 {string match} { |
| 538 |
proc foo {} {string match {a\*b} ab} |
| 539 |
foo |
| 540 |
} 0 |
| 541 |
test stringComp-11.25 {string match} { |
| 542 |
proc foo {} {string match {a\*\?\[\]\\\x} "a*?\[\]\\x"} |
| 543 |
foo |
| 544 |
} 1 |
| 545 |
test stringComp-11.26 {string match} { |
| 546 |
proc foo {} {string match ** ""} |
| 547 |
foo |
| 548 |
} 1 |
| 549 |
test stringComp-11.27 {string match} { |
| 550 |
proc foo {} {string match *. ""} |
| 551 |
foo |
| 552 |
} 0 |
| 553 |
test stringComp-11.28 {string match} { |
| 554 |
proc foo {} {string match "" ""} |
| 555 |
foo |
| 556 |
} 1 |
| 557 |
test stringComp-11.29 {string match} { |
| 558 |
proc foo {} {string match \[a a} |
| 559 |
foo |
| 560 |
} 1 |
| 561 |
test stringComp-11.30 {string match, bad args} { |
| 562 |
proc foo {} {string match - b c} |
| 563 |
list [catch {foo} msg] $msg |
| 564 |
} {1 {bad option "-": must be -nocase}} |
| 565 |
test stringComp-11.31 {string match case} { |
| 566 |
proc foo {} {string match a A} |
| 567 |
foo |
| 568 |
} 0 |
| 569 |
test stringComp-11.32 {string match nocase} { |
| 570 |
proc foo {} {string match -n a A} |
| 571 |
foo |
| 572 |
} 1 |
| 573 |
test stringComp-11.33 {string match nocase} { |
| 574 |
proc foo {} {string match -nocase a\334 A\374} |
| 575 |
foo |
| 576 |
} 1 |
| 577 |
test stringComp-11.34 {string match nocase} { |
| 578 |
proc foo {} {string match -nocase a*f ABCDEf} |
| 579 |
foo |
| 580 |
} 1 |
| 581 |
test stringComp-11.35 {string match case, false hope} { |
| 582 |
# This is true because '_' lies between the A-Z and a-z ranges |
| 583 |
proc foo {} {string match {[A-z]} _} |
| 584 |
foo |
| 585 |
} 1 |
| 586 |
test stringComp-11.36 {string match nocase range} { |
| 587 |
# This is false because although '_' lies between the A-Z and a-z ranges, |
| 588 |
# we lower case the end points before checking the ranges. |
| 589 |
proc foo {} {string match -nocase {[A-z]} _} |
| 590 |
foo |
| 591 |
} 0 |
| 592 |
test stringComp-11.37 {string match nocase} { |
| 593 |
proc foo {} {string match -nocase {[A-fh-Z]} g} |
| 594 |
foo |
| 595 |
} 0 |
| 596 |
test stringComp-11.38 {string match case, reverse range} { |
| 597 |
proc foo {} {string match {[A-fh-Z]} g} |
| 598 |
foo |
| 599 |
} 1 |
| 600 |
test stringComp-11.39 {string match, *\ case} { |
| 601 |
proc foo {} {string match {*\abc} abc} |
| 602 |
foo |
| 603 |
} 1 |
| 604 |
test stringComp-11.40 {string match, *special case} { |
| 605 |
proc foo {} {string match {*[ab]} abc} |
| 606 |
foo |
| 607 |
} 0 |
| 608 |
test stringComp-11.41 {string match, *special case} { |
| 609 |
proc foo {} {string match {*[ab]*} abc} |
| 610 |
foo |
| 611 |
} 1 |
| 612 |
test stringComp-11.42 {string match, *special case} { |
| 613 |
proc foo {} {string match "*\\" "\\"} |
| 614 |
foo |
| 615 |
} 0 |
| 616 |
test stringComp-11.43 {string match, *special case} { |
| 617 |
proc foo {} {string match "*\\\\" "\\"} |
| 618 |
foo |
| 619 |
} 1 |
| 620 |
test stringComp-11.44 {string match, *special case} { |
| 621 |
proc foo {} {string match "*???" "12345"} |
| 622 |
foo |
| 623 |
} 1 |
| 624 |
test stringComp-11.45 {string match, *special case} { |
| 625 |
proc foo {} {string match "*???" "12"} |
| 626 |
foo |
| 627 |
} 0 |
| 628 |
test stringComp-11.46 {string match, *special case} { |
| 629 |
proc foo {} {string match "*\\*" "abc*"} |
| 630 |
foo |
| 631 |
} 1 |
| 632 |
test stringComp-11.47 {string match, *special case} { |
| 633 |
proc foo {} {string match "*\\*" "*"} |
| 634 |
foo |
| 635 |
} 1 |
| 636 |
test stringComp-11.48 {string match, *special case} { |
| 637 |
proc foo {} {string match "*\\*" "*abc"} |
| 638 |
foo |
| 639 |
} 0 |
| 640 |
test stringComp-11.49 {string match, *special case} { |
| 641 |
proc foo {} {string match "?\\*" "a*"} |
| 642 |
foo |
| 643 |
} 1 |
| 644 |
test stringComp-11.50 {string match, *special case} { |
| 645 |
proc foo {} {string match "\\" "\\"} |
| 646 |
foo |
| 647 |
} 0 |
| 648 |
test stringComp-11.51 {string match; *, -nocase and UTF-8} { |
| 649 |
proc foo {} {string match -nocase [binary format I 717316707] \ |
| 650 |
[binary format I 2028036707]} |
| 651 |
foo |
| 652 |
} 1 |
| 653 |
test stringComp-11.52 {string match, null char in string} { |
| 654 |
proc foo {} { |
| 655 |
set ptn "*abc*" |
| 656 |
foreach elem [list "\u0000@abc" "@abc" "\u0000@abc\u0000" "blahabcblah"] { |
| 657 |
lappend out [string match $ptn $elem] |
| 658 |
} |
| 659 |
set out |
| 660 |
} |
| 661 |
foo |
| 662 |
} {1 1 1 1} |
| 663 |
test stringComp-11.53 {string match, null char in pattern} { |
| 664 |
proc foo {} { |
| 665 |
set out "" |
| 666 |
foreach {ptn elem} [list \ |
| 667 |
"*\u0000abc\u0000" "\u0000abc\u0000" \ |
| 668 |
"*\u0000abc\u0000" "\u0000abc\u0000ef" \ |
| 669 |
"*\u0000abc\u0000*" "\u0000abc\u0000ef" \ |
| 670 |
"*\u0000abc\u0000" "@\u0000abc\u0000ef" \ |
| 671 |
"*\u0000abc\u0000*" "@\u0000abc\u0000ef" \ |
| 672 |
] { |
| 673 |
lappend out [string match $ptn $elem] |
| 674 |
} |
| 675 |
set out |
| 676 |
} |
| 677 |
foo |
| 678 |
} {1 0 1 0 1} |
| 679 |
test stringComp-11.54 {string match, failure} { |
| 680 |
proc foo {} { |
| 681 |
set longString "" |
| 682 |
for {set i 0} {$i < 10} {incr i} { |
| 683 |
append longString "abcdefghijklmnopqrstuvwxy\u0000z01234567890123" |
| 684 |
} |
| 685 |
list [string match *cba* $longString] \ |
| 686 |
[string match *a*l*\u0000* $longString] \ |
| 687 |
[string match *a*l*\u0000*123 $longString] \ |
| 688 |
[string match *a*l*\u0000*123* $longString] \ |
| 689 |
[string match *a*l*\u0000*cba* $longString] \ |
| 690 |
[string match *===* $longString] |
| 691 |
} |
| 692 |
foo |
| 693 |
} {0 1 1 1 0 0} |
| 694 |
|
| 695 |
## string range |
| 696 |
test stringComp-12.1 {Bug 3588366: end-offsets before start} { |
| 697 |
apply {s { |
| 698 |
string range $s 0 end-5 |
| 699 |
}} 12345 |
| 700 |
} {} |
| 701 |
|
| 702 |
## string repeat |
| 703 |
## not yet bc |
| 704 |
|
| 705 |
## string replace |
| 706 |
test stringComp-14.1 {Bug 82e7f67325} { |
| 707 |
apply {x { |
| 708 |
set a [join $x {}] |
| 709 |
lappend b [string length [string replace ___! 0 2 $a]] |
| 710 |
lappend b [string length [string replace ___! 0 2 $a[unset a]]] |
| 711 |
}} {a b} |
| 712 |
} {3 3} |
| 713 |
test stringComp-14.2 {Bug 82e7f67325} memory { |
| 714 |
# As in stringComp-14.1, but make sure we don't retain too many refs |
| 715 |
leaktest { |
| 716 |
apply {x { |
| 717 |
set a [join $x {}] |
| 718 |
lappend b [string length [string replace ___! 0 2 $a]] |
| 719 |
lappend b [string length [string replace ___! 0 2 $a[unset a]]] |
| 720 |
}} {a b} |
| 721 |
} |
| 722 |
} {0} |
| 723 |
test stringComp-14.3 {Bug 0dca3bfa8f} { |
| 724 |
apply {arg { |
| 725 |
set argCopy $arg |
| 726 |
set arg [string replace $arg 1 2 aa] |
| 727 |
# Crashes in comparison before fix |
| 728 |
expr {$arg ne $argCopy} |
| 729 |
}} abcde |
| 730 |
} 1 |
| 731 |
test stringComp-14.4 {Bug 1af8de570511} { |
| 732 |
apply {{x y} { |
| 733 |
# Generate an unshared string value |
| 734 |
set val "" |
| 735 |
for { set i 0 } { $i < $x } { incr i } { |
| 736 |
set val [format "0%s" $val] |
| 737 |
} |
| 738 |
string replace $val[unset val] 1 1 $y |
| 739 |
}} 4 x |
| 740 |
} 0x00 |
| 741 |
test stringComp-14.5 {} { |
| 742 |
string length [string replace [string repeat a\u00fe 2] 3 end {}] |
| 743 |
} 3 |
| 744 |
|
| 745 |
## string tolower |
| 746 |
## not yet bc |
| 747 |
|
| 748 |
## string toupper |
| 749 |
## not yet bc |
| 750 |
|
| 751 |
## string totitle |
| 752 |
## not yet bc |
| 753 |
|
| 754 |
## string trim* |
| 755 |
## not yet bc |
| 756 |
|
| 757 |
## string word* |
| 758 |
## not yet bc |
| 759 |
|
| 760 |
## string cat |
| 761 |
test stringComp-29.1 {string cat, no arg} { |
| 762 |
proc foo {} {string cat} |
| 763 |
foo |
| 764 |
} "" |
| 765 |
test stringComp-29.2 {string cat, single arg} { |
| 766 |
proc foo {} { |
| 767 |
set x FOO |
| 768 |
string compare $x [string cat $x] |
| 769 |
} |
| 770 |
foo |
| 771 |
} 0 |
| 772 |
test stringComp-29.3 {string cat, two args} { |
| 773 |
proc foo {} { |
| 774 |
set x FOO |
| 775 |
string compare $x$x [string cat $x $x] |
| 776 |
} |
| 777 |
foo |
| 778 |
} 0 |
| 779 |
test stringComp-29.4 {string cat, many args} { |
| 780 |
proc foo {} { |
| 781 |
set x FOO |
| 782 |
set n 260 |
| 783 |
set xx [string repeat $x $n] |
| 784 |
set vv [string repeat {$x} $n] |
| 785 |
set vvs [string repeat {$x } $n] |
| 786 |
set r1 [string compare $xx [subst $vv]] |
| 787 |
set r2 [string compare $xx [eval "string cat $vvs"]] |
| 788 |
list $r1 $r2 |
| 789 |
} |
| 790 |
foo |
| 791 |
} {0 0} |
| 792 |
|
| 793 |
|
| 794 |
# cleanup |
| 795 |
catch {rename foo {}} |
| 796 |
::tcltest::cleanupTests |
| 797 |
return |
| 798 |
|
| 799 |
# Local Variables: |
| 800 |
# mode: tcl |
| 801 |
# End: |