os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/stringComp.test
First public contribution.
1 # Commands covered: string
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.
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.
12 # Copyright (c) 2001 by ActiveState Corporation.
13 # Copyright (c) 2001 by Kevin B. Kenny. All rights reserved.
15 # See the file "license.terms" for information on usage and redistribution
16 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
18 # RCS: @(#) $Id: stringComp.test,v 1.6.2.1 2004/10/28 00:01:12 dgp Exp $
20 if {[lsearch [namespace children] ::tcltest] == -1} {
21 package require tcltest
22 namespace import -force ::tcltest::*
25 # Some tests require the testobj command
27 set ::tcltest::testConstraints(testobj) \
28 [expr {[info commands testobj] != {}}]
30 test stringComp-1.1 {error conditions} {
31 proc foo {} {string gorp a b}
32 list [catch {foo} msg] $msg
33 } {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}}
34 test stringComp-1.2 {error conditions} {
36 list [catch {foo} msg] $msg
37 } {1 {wrong # args: should be "string option arg ?arg ...?"}}
38 test stringComp-1.3 {error condition - undefined method during compile} {
39 # We don't want this to complain about 'never' because it may never
40 # be called, or string may get redefined. This must compile OK.
42 if {"yes" == "no"} { string never called but complains here }
48 test stringComp-2.1 {string compare, too few args} {
49 proc foo {} {string compare a}
50 list [catch {foo} msg] $msg
51 } {1 {wrong # args: should be "string compare ?-nocase? ?-length int? string1 string2"}}
52 test stringComp-2.2 {string compare, bad args} {
53 proc foo {} {string compare a b c}
54 list [catch {foo} msg] $msg
55 } {1 {bad option "a": must be -nocase or -length}}
56 test stringComp-2.3 {string compare, bad args} {
57 list [catch {string compare -length -nocase str1 str2} msg] $msg
58 } {1 {expected integer but got "-nocase"}}
59 test stringComp-2.4 {string compare, too many args} {
60 list [catch {string compare -length 10 -nocase str1 str2 str3} msg] $msg
61 } {1 {wrong # args: should be "string compare ?-nocase? ?-length int? string1 string2"}}
62 test stringComp-2.5 {string compare with length unspecified} {
63 list [catch {string compare -length 10 10} msg] $msg
64 } {1 {wrong # args: should be "string compare ?-nocase? ?-length int? string1 string2"}}
65 test stringComp-2.6 {string compare} {
66 proc foo {} {string compare abcde abdef}
69 test stringComp-2.7 {string compare, shortest method name} {
70 proc foo {} {string c abcde ABCDE}
73 test stringComp-2.8 {string compare} {
74 proc foo {} {string compare abcde abcde}
77 test stringComp-2.9 {string compare with length} {
78 proc foo {} {string compare -length 2 abcde abxyz}
81 test stringComp-2.10 {string compare with special index} {
82 proc foo {} {string compare -length end-3 abcde abxyz}
83 list [catch {foo} msg] $msg
84 } {1 {expected integer but got "end-3"}}
85 test stringComp-2.11 {string compare, unicode} {
86 proc foo {} {string compare ab\u7266 ab\u7267}
89 test stringComp-2.12 {string compare, high bit} {
90 # This test will fail if the underlying comparaison
91 # is using signed chars instead of unsigned chars.
92 # (like SunOS's default memcmp thus the compat/memcmp.c)
93 proc foo {} {string compare "\x80" "@"}
95 # Nb this tests works also in utf8 space because \x80 is
96 # translated into a 2 or more bytelength but whose first byte has
99 test stringComp-2.13 {string compare -nocase} {
100 proc foo {} {string compare -nocase abcde abdef}
103 test stringComp-2.14 {string compare -nocase} {
104 proc foo {} {string c -nocase abcde ABCDE}
107 test stringComp-2.15 {string compare -nocase} {
108 proc foo {} {string compare -nocase abcde abcde}
111 test stringComp-2.16 {string compare -nocase with length} {
112 proc foo {} {string compare -length 2 -nocase abcde Abxyz}
115 test stringComp-2.17 {string compare -nocase with length} {
116 proc foo {} {string compare -nocase -length 3 abcde Abxyz}
119 test stringComp-2.18 {string compare -nocase with length <= 0} {
120 proc foo {} {string compare -nocase -length -1 abcde AbCdEf}
123 test stringComp-2.19 {string compare -nocase with excessive length} {
124 proc foo {} {string compare -nocase -length 50 AbCdEf abcde}
127 test stringComp-2.20 {string compare -len unicode} {
128 # These are strings that are 6 BYTELENGTH long, but the length
129 # shouldn't make a different because there are actually 3 CHARS long
130 proc foo {} {string compare -len 5 \334\334\334 \334\334\374}
133 test stringComp-2.21 {string compare -nocase with special index} {
134 proc foo {} {string compare -nocase -length end-3 Abcde abxyz}
135 list [catch {foo} msg] $msg
136 } {1 {expected integer but got "end-3"}}
137 test stringComp-2.22 {string compare, null strings} {
138 proc foo {} {string compare "" ""}
141 test stringComp-2.23 {string compare, null strings} {
142 proc foo {} {string compare "" foo}
145 test stringComp-2.24 {string compare, null strings} {
146 proc foo {} {string compare foo ""}
149 test stringComp-2.25 {string compare -nocase, null strings} {
150 proc foo {} {string compare -nocase "" ""}
153 test stringComp-2.26 {string compare -nocase, null strings} {
154 proc foo {} {string compare -nocase "" foo}
157 test stringComp-2.27 {string compare -nocase, null strings} {
158 proc foo {} {string compare -nocase foo ""}
161 test stringComp-2.28 {string compare with length, unequal strings} {
162 proc foo {} {string compare -length 2 abc abde}
165 test stringComp-2.29 {string compare with length, unequal strings} {
166 proc foo {} {string compare -length 2 ab abde}
169 test stringComp-2.30 {string compare with NUL character vs. other ASCII} {
170 # Be careful here, since UTF-8 rep comparison with memcmp() of
171 # these puts chars in the wrong order
172 proc foo {} {string compare \x00 \x01}
175 test stringComp-2.31 {string compare, high bit} {
176 proc foo {} {string compare "a\x80" "a@"}
179 test stringComp-2.32 {string compare, high bit} {
180 proc foo {} {string compare "a\x00" "a\x01"}
183 test stringComp-2.33 {string compare, high bit} {
184 proc foo {} {string compare "\x00\x00" "\x00\x01"}
188 # only need a few tests on equal, since it uses the same code as
189 # string compare, but just modifies the return output
190 test stringComp-3.1 {string equal} {
191 proc foo {} {string equal abcde abdef}
194 test stringComp-3.2 {string equal} {
195 proc foo {} {string eq abcde ABCDE}
198 test stringComp-3.3 {string equal} {
199 proc foo {} {string equal abcde abcde}
202 test stringComp-3.4 {string equal -nocase} {
203 proc foo {} {string equal -nocase \334\334\334\334\374\374\374\374 \334\334\334\334\334\334\334\334}
206 test stringComp-3.5 {string equal -nocase} {
207 proc foo {} {string equal -nocase abcde abdef}
210 test stringComp-3.6 {string equal -nocase} {
211 proc foo {} {string eq -nocase abcde ABCDE}
214 test stringComp-3.7 {string equal -nocase} {
215 proc foo {} {string equal -nocase abcde abcde}
218 test stringComp-3.8 {string equal with length, unequal strings} {
219 proc foo {} {string equal -length 2 abc abde}
223 test stringComp-4.1 {string first, too few args} {
224 proc foo {} {string first a}
225 list [catch {foo} msg] $msg
226 } {1 {wrong # args: should be "string first subString string ?startIndex?"}}
227 test stringComp-4.2 {string first, bad args} {
228 proc foo {} {string first a b c}
229 list [catch {foo} msg] $msg
230 } {1 {bad index "c": must be integer or end?-integer?}}
231 test stringComp-4.3 {string first, too many args} {
232 proc foo {} {string first a b 5 d}
233 list [catch {foo} msg] $msg
234 } {1 {wrong # args: should be "string first subString string ?startIndex?"}}
235 test stringComp-4.4 {string first} {
236 proc foo {} {string first bq abcdefgbcefgbqrs}
239 test stringComp-4.5 {string first} {
240 proc foo {} {string fir bcd abcdefgbcefgbqrs}
243 test stringComp-4.6 {string first} {
244 proc foo {} {string f b abcdefgbcefgbqrs}
247 test stringComp-4.7 {string first} {
248 proc foo {} {string first xxx x123xx345xxx789xxx012}
251 test stringComp-4.8 {string first} {
252 proc foo {} {string first "" x123xx345xxx789xxx012}
255 test stringComp-4.9 {string first, unicode} {
256 proc foo {} {string first x abc\u7266x}
259 test stringComp-4.10 {string first, unicode} {
260 proc foo {} {string first \u7266 abc\u7266x}
263 test stringComp-4.11 {string first, start index} {
264 proc foo {} {string first \u7266 abc\u7266x 3}
267 test stringComp-4.12 {string first, start index} {
268 proc foo {} {string first \u7266 abc\u7266x 4}
271 test stringComp-4.13 {string first, start index} {
272 proc foo {} {string first \u7266 abc\u7266x end-2}
275 test stringComp-4.14 {string first, negative start index} {
276 proc foo {} {string first b abc -1}
280 test stringComp-5.1 {string index} {
281 proc foo {} {string index}
282 list [catch {foo} msg] $msg
283 } {1 {wrong # args: should be "string index string charIndex"}}
284 test stringComp-5.2 {string index} {
285 proc foo {} {string index a b c}
286 list [catch {foo} msg] $msg
287 } {1 {wrong # args: should be "string index string charIndex"}}
288 test stringComp-5.3 {string index} {
289 proc foo {} {string index abcde 0}
292 test stringComp-5.4 {string index} {
293 proc foo {} {string in abcde 4}
296 test stringComp-5.5 {string index} {
297 proc foo {} {string index abcde 5}
300 test stringComp-5.6 {string index} {
301 proc foo {} {string index abcde -10}
302 list [catch {foo} msg] $msg
304 test stringComp-5.7 {string index} {
305 proc foo {} {string index a xyz}
306 list [catch {foo} msg] $msg
307 } {1 {bad index "xyz": must be integer or end?-integer?}}
308 test stringComp-5.8 {string index} {
309 proc foo {} {string index abc end}
312 test stringComp-5.9 {string index} {
313 proc foo {} {string index abc end-1}
316 test stringComp-5.10 {string index, unicode} {
317 proc foo {} {string index abc\u7266d 4}
320 test stringComp-5.11 {string index, unicode} {
321 proc foo {} {string index abc\u7266d 3}
324 test stringComp-5.12 {string index, unicode over char length, under byte length} {
325 proc foo {} {string index \334\374\334\374 6}
328 test stringComp-5.13 {string index, bytearray object} {
329 proc foo {} {string index [binary format a5 fuz] 0}
332 test stringComp-5.14 {string index, bytearray object} {
333 proc foo {} {string index [binary format I* {0x50515253 0x52}] 3}
336 test stringComp-5.15 {string index, bytearray object} {
338 set b [binary format I* {0x50515253 0x52}]
339 set i1 [string index $b end-6]
340 set i2 [string index $b 1]
341 string compare $i1 $i2
345 test stringComp-5.16 {string index, bytearray object with string obj shimmering} {
347 set str "0123456789\x00 abcdedfghi"
348 binary scan $str H* dump
349 string compare [string index $str 10] \x00
353 test stringComp-5.17 {string index, bad integer} {
354 proc foo {} {string index "abc" 08}
355 list [catch {foo} msg] $msg
356 } {1 {bad index "08": must be integer or end?-integer? (looks like invalid octal number)}}
357 test stringComp-5.18 {string index, bad integer} {
358 proc foo {} {string index "abc" end-00289}
359 list [catch {foo} msg] $msg
360 } {1 {bad index "end-00289": must be integer or end?-integer? (looks like invalid octal number)}}
361 test stringComp-5.19 {string index, bytearray object out of bounds} {
362 proc foo {} {string index [binary format I* {0x50515253 0x52}] -1}
365 test stringComp-5.20 {string index, bytearray object out of bounds} {
366 proc foo {} {string index [binary format I* {0x50515253 0x52}] 20}
371 proc largest_int {} {
372 # This will give us what the largest valid int on this machine is,
373 # so we can test for overflow properly below on >32 bit systems
375 set exp 7; # assume we get at least 8 bits
376 while {$int > 0} { set int [expr {1 << [incr exp]}] }
377 return [expr {$int-1}]
383 catch {rename largest_int {}}
390 test stringComp-8.1 {string bytelength} {
391 proc foo {} {string bytelength}
392 list [catch {foo} msg] $msg
393 } {1 {wrong # args: should be "string bytelength string"}}
394 test stringComp-8.2 {string bytelength} {
395 proc foo {} {string bytelength a b}
396 list [catch {foo} msg] $msg
397 } {1 {wrong # args: should be "string bytelength string"}}
398 test stringComp-8.3 {string bytelength} {
399 proc foo {} {string bytelength "\u00c7"}
402 test stringComp-8.4 {string bytelength} {
403 proc foo {} {string b ""}
409 test stringComp-9.1 {string length} {
410 proc foo {} {string length}
411 list [catch {foo} msg] $msg
412 } {1 {wrong # args: should be "string length string"}}
413 test stringComp-9.2 {string length} {
414 proc foo {} {string length a b}
415 list [catch {foo} msg] $msg
416 } {1 {wrong # args: should be "string length string"}}
417 test stringComp-9.3 {string length} {
418 proc foo {} {string length "a little string"}
421 test stringComp-9.4 {string length} {
422 proc foo {} {string le ""}
425 test stringComp-9.5 {string length, unicode} {
426 proc foo {} {string le "abcd\u7266"}
429 test stringComp-9.6 {string length, bytearray object} {
430 proc foo {} {string length [binary format a5 foo]}
433 test stringComp-9.7 {string length, bytearray object} {
434 proc foo {} {string length [binary format I* {0x50515253 0x52}]}
443 test stringComp-11.1 {string match, too few args} {
444 proc foo {} {string match a}
445 list [catch {foo} msg] $msg
446 } {1 {wrong # args: should be "string match ?-nocase? pattern string"}}
447 test stringComp-11.2 {string match, too many args} {
448 proc foo {} {string match a b c d}
449 list [catch {foo} msg] $msg
450 } {1 {wrong # args: should be "string match ?-nocase? pattern string"}}
451 test stringComp-11.3 {string match} {
452 proc foo {} {string match abc abc}
455 test stringComp-11.4 {string match} {
456 proc foo {} {string mat abc abd}
459 test stringComp-11.5 {string match} {
460 proc foo {} {string match ab*c abc}
463 test stringComp-11.6 {string match} {
464 proc foo {} {string match ab**c abc}
467 test stringComp-11.7 {string match} {
468 proc foo {} {string match ab* abcdef}
471 test stringComp-11.8 {string match} {
472 proc foo {} {string match *c abc}
475 test stringComp-11.9 {string match} {
476 proc foo {} {string match *3*6*9 0123456789}
479 test stringComp-11.10 {string match} {
480 proc foo {} {string match *3*6*9 01234567890}
483 test stringComp-11.11 {string match} {
484 proc foo {} {string match a?c abc}
487 test stringComp-11.12 {string match} {
488 proc foo {} {string match a??c abc}
491 test stringComp-11.13 {string match} {
492 proc foo {} {string match ?1??4???8? 0123456789}
495 test stringComp-11.14 {string match} {
496 proc foo {} {string match {[abc]bc} abc}
499 test stringComp-11.15 {string match} {
500 proc foo {} {string match {a[abc]c} abc}
503 test stringComp-11.16 {string match} {
504 proc foo {} {string match {a[xyz]c} abc}
507 test stringComp-11.17 {string match} {
508 proc foo {} {string match {12[2-7]45} 12345}
511 test stringComp-11.18 {string match} {
512 proc foo {} {string match {12[ab2-4cd]45} 12345}
515 test stringComp-11.19 {string match} {
516 proc foo {} {string match {12[ab2-4cd]45} 12b45}
519 test stringComp-11.20 {string match} {
520 proc foo {} {string match {12[ab2-4cd]45} 12d45}
523 test stringComp-11.21 {string match} {
524 proc foo {} {string match {12[ab2-4cd]45} 12145}
527 test stringComp-11.22 {string match} {
528 proc foo {} {string match {12[ab2-4cd]45} 12545}
531 test stringComp-11.23 {string match} {
532 proc foo {} {string match {a\*b} a*b}
535 test stringComp-11.24 {string match} {
536 proc foo {} {string match {a\*b} ab}
539 test stringComp-11.25 {string match} {
540 proc foo {} {string match {a\*\?\[\]\\\x} "a*?\[\]\\x"}
543 test stringComp-11.26 {string match} {
544 proc foo {} {string match ** ""}
547 test stringComp-11.27 {string match} {
548 proc foo {} {string match *. ""}
551 test stringComp-11.28 {string match} {
552 proc foo {} {string match "" ""}
555 test stringComp-11.29 {string match} {
556 proc foo {} {string match \[a a}
559 test stringComp-11.30 {string match, bad args} {
560 proc foo {} {string match - b c}
561 list [catch {foo} msg] $msg
562 } {1 {bad option "-": must be -nocase}}
563 test stringComp-11.31 {string match case} {
564 proc foo {} {string match a A}
567 test stringComp-11.32 {string match nocase} {
568 proc foo {} {string match -n a A}
571 test stringComp-11.33 {string match nocase} {
572 proc foo {} {string match -nocase a\334 A\374}
575 test stringComp-11.34 {string match nocase} {
576 proc foo {} {string match -nocase a*f ABCDEf}
579 test stringComp-11.35 {string match case, false hope} {
580 # This is true because '_' lies between the A-Z and a-z ranges
581 proc foo {} {string match {[A-z]} _}
584 test stringComp-11.36 {string match nocase range} {
585 # This is false because although '_' lies between the A-Z and a-z ranges,
586 # we lower case the end points before checking the ranges.
587 proc foo {} {string match -nocase {[A-z]} _}
590 test stringComp-11.37 {string match nocase} {
591 proc foo {} {string match -nocase {[A-fh-Z]} g}
594 test stringComp-11.38 {string match case, reverse range} {
595 proc foo {} {string match {[A-fh-Z]} g}
598 test stringComp-11.39 {string match, *\ case} {
599 proc foo {} {string match {*\abc} abc}
602 test stringComp-11.40 {string match, *special case} {
603 proc foo {} {string match {*[ab]} abc}
606 test stringComp-11.41 {string match, *special case} {
607 proc foo {} {string match {*[ab]*} abc}
610 test stringComp-11.42 {string match, *special case} {
611 proc foo {} {string match "*\\" "\\"}
614 test stringComp-11.43 {string match, *special case} {
615 proc foo {} {string match "*\\\\" "\\"}
618 test stringComp-11.44 {string match, *special case} {
619 proc foo {} {string match "*???" "12345"}
622 test stringComp-11.45 {string match, *special case} {
623 proc foo {} {string match "*???" "12"}
626 test stringComp-11.46 {string match, *special case} {
627 proc foo {} {string match "*\\*" "abc*"}
630 test stringComp-11.47 {string match, *special case} {
631 proc foo {} {string match "*\\*" "*"}
634 test stringComp-11.48 {string match, *special case} {
635 proc foo {} {string match "*\\*" "*abc"}
638 test stringComp-11.49 {string match, *special case} {
639 proc foo {} {string match "?\\*" "a*"}
642 test stringComp-11.50 {string match, *special case} {
643 proc foo {} {string match "\\" "\\"}
646 test stringComp-11.51 {string match; *, -nocase and UTF-8} {
647 proc foo {} {string match -nocase [binary format I 717316707] \
648 [binary format I 2028036707]}
651 test stringComp-11.52 {string match, null char in string} {
654 foreach elem [list "\u0000@abc" "@abc" "\u0000@abc\u0000" "blahabcblah"] {
655 lappend out [string match $ptn $elem]
661 test stringComp-11.53 {string match, null char in pattern} {
664 foreach {ptn elem} [list \
665 "*\u0000abc\u0000" "\u0000abc\u0000" \
666 "*\u0000abc\u0000" "\u0000abc\u0000ef" \
667 "*\u0000abc\u0000*" "\u0000abc\u0000ef" \
668 "*\u0000abc\u0000" "@\u0000abc\u0000ef" \
669 "*\u0000abc\u0000*" "@\u0000abc\u0000ef" \
671 lappend out [string match $ptn $elem]
677 test stringComp-11.54 {string match, failure} {
680 for {set i 0} {$i < 10} {incr i} {
681 append longString "abcdefghijklmnopqrstuvwxy\u0000z01234567890123"
683 list [string match *cba* $longString] \
684 [string match *a*l*\u0000* $longString] \
685 [string match *a*l*\u0000*123 $longString] \
686 [string match *a*l*\u0000*123* $longString] \
687 [string match *a*l*\u0000*cba* $longString] \
688 [string match *===* $longString]
718 ::tcltest::cleanupTests