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