tcltest: do a better job of cleanup up after tests
[jimtcl.git] / tests / regexp2.test
blobf7cf516c144cb3f7878011ae1c82bcc5d7c36301
1 # Commands covered:  regexp, regsub
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 # Copyright (c) 1991-1993 The Regents of the University of California.
8 # Copyright (c) 1998 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 # RCS: @(#) $Id$
16 source [file dirname [info script]]/testing.tcl
18 needs cmd regexp
19 testConstraint regexp_are [regexp {\d} 1]
20 needs constraint regexp_are
22 # Procedure to evaluate a script within a proc, to test compilation
23 # functionality
25 proc evalInProc { script } {
26     proc testProc {} $script
27     set status [catch {
28         testProc 
29     } result]
30     rename testProc {}
31     return $result
32     #return [list $status $result]
35 catch {unset foo}
36 test regexpComp-1.1 {basic regexp operation} {
37     evalInProc {
38         regexp ab*c abbbc
39     }
40 } 1
41 test regexpComp-1.2 {basic regexp operation} {
42     evalInProc {
43         regexp ab*c ac
44     }
45 } 1
46 test regexpComp-1.3 {basic regexp operation} {
47     evalInProc {
48         regexp ab*c ab
49     }
50 } 0
51 test regexpComp-1.4 {basic regexp operation} {
52     evalInProc {
53         regexp -- -gorp abc-gorpxxx
54     }
55 } 1
56 test regexpComp-1.5 {basic regexp operation} {
57     evalInProc {
58         regexp {^([^ ]*)[ ]*([^ ]*)} "" a
59     }
60 } 1
61 test regexpComp-1.6 {basic regexp operation} {
62     list [catch {regexp {} abc} msg] $msg
63 } {0 1}
64 test regexpComp-1.7 {regexp utf compliance} {
65     # if not UTF-8 aware, result is "0 1"
66     evalInProc {
67         set foo "\u4e4eb q"
68         regexp "\u4e4eb q" "a\u4e4eb qw\u5e4e\x4e wq" bar
69         list [string compare $foo $bar] [regexp 4 $bar]
70     }
71 } {0 0}
73 test regexpComp-2.1 {getting substrings back from regexp} {
74     evalInProc {
75         set foo {}
76         list [regexp ab*c abbbbc foo] $foo
77     }
78 } {1 abbbbc}
79 test regexpComp-2.2 {getting substrings back from regexp} {
80     evalInProc {
81         set foo {}
82         set f2 {}
83         list [regexp a(b*)c abbbbc foo f2] $foo $f2
84     }
85 } {1 abbbbc bbbb}
86 test regexpComp-2.3 {getting substrings back from regexp} {
87     evalInProc {
88         set foo {}
89         set f2 {}
90         list [regexp a(b*)(c) abbbbc foo f2] $foo $f2
91     }
92 } {1 abbbbc bbbb}
93 test regexpComp-2.4 {getting substrings back from regexp} {
94     evalInProc {
95         set foo {}
96         set f2 {}
97         set f3 {}
98         list [regexp a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
99     }
100 } {1 abbbbc bbbb c}
101 test regexpComp-2.5 {getting substrings back from regexp} {
102     evalInProc {
103         set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
104         set f6 {}; set f7 {}; set f8 {}; set f9 {}; set fa {}; set fb {};
105         list [regexp (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*)(a*)(b*) \
106                 12223345556789999aabbb \
107                 foo f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb] $foo $f1 $f2 $f3 $f4 $f5 \
108                 $f6 $f7 $f8 $f9 $fa $fb
109     }
110 } {1 12223345556789999aabbb 1 222 33 4 555 6 7 8 9999 aa bbb}
111 test regexpComp-2.6 {getting substrings back from regexp} {
112     evalInProc {
113         set foo 2; set f2 2; set f3 2; set f4 2
114         list [regexp (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
115     }
116 } {1 a a {} {}}
117 test regexpComp-2.7 {getting substrings back from regexp} {
118     evalInProc {
119         set foo 1; set f2 1; set f3 1; set f4 1
120         list [regexp (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
121     }
122 } {1 ac a {} c}
123 test regexpComp-2.8 {getting substrings back from regexp} {
124     evalInProc {
125         set match {}
126         list [regexp {^a*b} aaaab match] $match
127     }
128 } {1 aaaab}
130 test regexpComp-3.1 {-indices option to regexp} {
131     evalInProc {
132         set foo {}
133         list [regexp -indices ab*c abbbbc foo] $foo
134     }
135 } {1 {0 5}}
136 test regexpComp-3.2 {-indices option to regexp} {
137     evalInProc {
138         set foo {}
139         set f2 {}
140         list [regexp -indices a(b*)c abbbbc foo f2] $foo $f2
141     }
142 } {1 {0 5} {1 4}}
143 test regexpComp-3.3 {-indices option to regexp} {
144     evalInProc {
145         set foo {}
146         set f2 {}
147         list [regexp -indices a(b*)(c) abbbbc foo f2] $foo $f2
148     }
149 } {1 {0 5} {1 4}}
150 test regexpComp-3.4 {-indices option to regexp} {
151     evalInProc {
152         set foo {}
153         set f2 {}
154         set f3 {}
155         list [regexp -indices a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
156     }
157 } {1 {0 5} {1 4} {5 5}}
158 test regexpComp-3.5 {-indices option to regexp} {
159     evalInProc {
160         set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
161         set f6 {}; set f7 {}; set f8 {}; set f9 {}
162         list [regexp -indices (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*) \
163                 12223345556789999 \
164                 foo f1 f2 f3 f4 f5 f6 f7 f8 f9] $foo $f1 $f2 $f3 $f4 $f5 \
165                 $f6 $f7 $f8 $f9
166     }
167 } {1 {0 16} {0 0} {1 3} {4 5} {6 6} {7 9} {10 10} {11 11} {12 12} {13 16}}
168 test regexpComp-3.6 {getting substrings back from regexp} {
169     evalInProc {
170         set foo 2; set f2 2; set f3 2; set f4 2
171         list [regexp -indices (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
172     }
173 } {1 {1 1} {1 1} {-1 -1} {-1 -1}}
174 test regexpComp-3.7 {getting substrings back from regexp} {
175     evalInProc {
176         set foo 1; set f2 1; set f3 1; set f4 1
177         list [regexp -indices (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
178     }
179 } {1 {1 2} {1 1} {-1 -1} {2 2}}
181 test regexpComp-4.1 {-nocase option to regexp} {
182     evalInProc {
183         regexp -nocase foo abcFOo
184     }
185 } 1
186 test regexpComp-4.2 {-nocase option to regexp} {
187     evalInProc {
188         set f1 22
189         set f2 33
190         set f3 44
191         list [regexp -nocase {a(b*)([xy]*)z} aBbbxYXxxZ22 f1 f2 f3] $f1 $f2 $f3
192     }
193 } {1 aBbbxYXxxZ Bbb xYXxx}
194 test regexpComp-4.3 {-nocase option to regexp} {
195     evalInProc {
196         regexp -nocase FOo abcFOo
197     }
198 } 1
199 set ::x abcdefghijklmnopqrstuvwxyz1234567890
200 set ::x $x$x$x$x$x$x$x$x$x$x$x$x
201 test regexpComp-4.4 {case conversion in regexp} {
202     evalInProc {
203         list [regexp -nocase $::x $::x foo] $foo
204     }
205 } "1 $x"
206 catch {unset ::x}
208 test regexpComp-5.1 {exercise cache of compiled expressions} {
209     evalInProc {
210         regexp .*a b
211         regexp .*b c
212         regexp .*c d
213         regexp .*d e
214         regexp .*e f
215         regexp .*a bbba
216     }
217 } 1
218 test regexpComp-5.2 {exercise cache of compiled expressions} {
219     evalInProc {
220         regexp .*a b
221         regexp .*b c
222         regexp .*c d
223         regexp .*d e
224         regexp .*e f
225         regexp .*b xxxb
226     }
227 } 1
228 test regexpComp-5.3 {exercise cache of compiled expressions} {
229     evalInProc {
230         regexp .*a b
231         regexp .*b c
232         regexp .*c d
233         regexp .*d e
234         regexp .*e f
235         regexp .*c yyyc
236     }
237 } 1
238 test regexpComp-5.4 {exercise cache of compiled expressions} {
239     evalInProc {
240         regexp .*a b
241         regexp .*b c
242         regexp .*c d
243         regexp .*d e
244         regexp .*e f
245         regexp .*d 1d
246     }
247 } 1
248 test regexpComp-5.5 {exercise cache of compiled expressions} {
249     evalInProc {
250         regexp .*a b
251         regexp .*b c
252         regexp .*c d
253         regexp .*d e
254         regexp .*e f
255         regexp .*e xe
256     }
257 } 1
259 test regexpComp-6.4 {regexp errors} {
260     evalInProc {
261         list [catch {regexp a( b} msg] $msg
262     }
263 } {1 {couldn't compile regular expression pattern: parentheses () not balanced}}
264 test regexpComp-6.5 {regexp errors} {
265     evalInProc {
266         list [catch {regexp a( b} msg] $msg
267     }
268 } {1 {couldn't compile regular expression pattern: parentheses () not balanced}}
269 test regexpComp-6.6 {regexp errors} {
270     evalInProc {
271         list [catch {regexp a a f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1} msg] $msg
272     }
273 } {0 1}
274 test regexpComp-6.7 {regexp errors} {
275     evalInProc {
276         list [catch {regexp (x)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.) xyzzy} msg] $msg
277     }
278 } {0 0}
279 test regexpComp-6.8 {regexp errors} {
280     evalInProc {
281         catch {unset f1}
282         set f1 44
283         catch {regexp abc abc f1(f2)} msg
284     }
285 } {1}
286 test regexpComp-6.9 {regexp errors, -start bad int check} {
287     evalInProc {
288         list [catch {regexp -start bogus {^$} {}} msg] $msg
289     }
290 } {1 {bad index "bogus": must be integer?[+-]integer? or end?[+-]integer?}}
292 test regexpComp-7.1 {basic regsub operation} {
293     evalInProc {
294         list [regsub aa+ xaxaaaxaa 111&222 foo] $foo
295     }
296 } {1 xax111aaa222xaa}
297 test regexpComp-7.2 {basic regsub operation} {
298     evalInProc {
299         list [regsub aa+ aaaxaa &111 foo] $foo
300     }
301 } {1 aaa111xaa}
302 test regexpComp-7.3 {basic regsub operation} {
303     evalInProc {
304         list [regsub aa+ xaxaaa 111& foo] $foo
305     }
306 } {1 xax111aaa}
307 test regexpComp-7.4 {basic regsub operation} {
308     evalInProc {
309         list [regsub aa+ aaa 11&2&333 foo] $foo
310     }
311 } {1 11aaa2aaa333}
312 test regexpComp-7.5 {basic regsub operation} {
313     evalInProc {
314         list [regsub aa+ xaxaaaxaa &2&333 foo] $foo
315     }
316 } {1 xaxaaa2aaa333xaa}
317 test regexpComp-7.6 {basic regsub operation} {
318     evalInProc {
319         list [regsub aa+ xaxaaaxaa 1&22& foo] $foo
320     }
321 } {1 xax1aaa22aaaxaa}
322 test regexpComp-7.7 {basic regsub operation} {
323     evalInProc {
324         list [regsub a(a+) xaxaaaxaa {1\122\1} foo] $foo
325     }
326 } {1 xax1aa22aaxaa}
327 test regexpComp-7.8 {basic regsub operation} {
328     evalInProc {
329         list [regsub a(a+) xaxaaaxaa {1\\\122\1} foo] $foo
330     }
331 } "1 {xax1\\aa22aaxaa}"
332 test regexpComp-7.9 {basic regsub operation} {
333     evalInProc {
334         list [regsub a(a+) xaxaaaxaa {1\\122\1} foo] $foo
335     }
336 } "1 {xax1\\122aaxaa}"
337 test regexpComp-7.10 {basic regsub operation} {
338     evalInProc {
339         list [regsub a(a+) xaxaaaxaa {1\\&\1} foo] $foo
340     }
341 } "1 {xax1\\aaaaaxaa}"
342 test regexpComp-7.11 {basic regsub operation} {
343     evalInProc {
344         list [regsub a(a+) xaxaaaxaa {1\&\1} foo] $foo
345     }
346 } {1 xax1&aaxaa}
347 test regexpComp-7.12 {basic regsub operation} {
348     evalInProc {
349         list [regsub a(a+) xaxaaaxaa {\1\1\1\1&&} foo] $foo
350     }
351 } {1 xaxaaaaaaaaaaaaaaxaa}
352 test regexpComp-7.13 {basic regsub operation} {
353     evalInProc {
354         set foo xxx
355         list [regsub abc xyz 111 foo] $foo
356     }
357 } {0 xyz}
358 test regexpComp-7.14 {basic regsub operation} {
359     evalInProc {
360         set foo xxx
361         list [regsub ^ xyz "111 " foo] $foo
362     }
363 } {1 {111 xyz}}
364 test regexpComp-7.15 {basic regsub operation} {
365     evalInProc {
366         set foo xxx
367         list [regsub -- -foo abc-foodef "111 " foo] $foo
368     }
369 } {1 {abc111 def}}
370 test regexpComp-7.16 {basic regsub operation} {
371     evalInProc {
372         set foo xxx
373         list [regsub x "" y foo] $foo
374     }
375 } {0 {}}
376 test regexpComp-7.17 {regsub utf compliance} {
377     evalInProc {
378         # if not UTF-8 aware, result is "0 1"
379         set foo "xyz555ijka\u4e4ebpqr"
380         regsub a\u4e4eb xyza\u4e4ebijka\u4e4ebpqr 555 bar
381         list [string compare $foo $bar] [regexp 4 $bar]
382     }
383 } {0 0}
385 test regexpComp-7.18 {regsub utf8 in char range} utf8 {
386     regsub {[\u4e4ex]b} xyza\u4e4ebijka\u4e4ebpqr 555
387 } xyza555ijka\u4e4ebpqr
389 test regexpComp-7.19 {regsub utf8 in complemented char range} utf8 {
390     regsub -all {[^x\u4e4e]b} xyza\u4e4ebizbjxbka\u4e4fbpqr 555
391 } xyza\u4e4ebi555jxbka555pqr
393 test regexpComp-8.1 {case conversion in regsub} {
394     evalInProc {
395         list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
396     }
397 } {1 xaAAaAAay}
398 test regexpComp-8.2 {case conversion in regsub} {
399     evalInProc {
400         list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
401     }
402 } {1 xaAAaAAay}
403 test regexpComp-8.3 {case conversion in regsub} {
404     evalInProc {
405         set foo 123
406         list [regsub a(a+) xaAAaAAay & foo] $foo
407     }
408 } {0 xaAAaAAay}
409 test regexpComp-8.4 {case conversion in regsub} {
410     evalInProc {
411         set foo 123
412         list [regsub -nocase a CaDE b foo] $foo
413     }
414 } {1 CbDE}
415 test regexpComp-8.5 {case conversion in regsub} {
416     evalInProc {
417         set foo 123
418         list [regsub -nocase XYZ CxYzD b foo] $foo
419     }
420 } {1 CbD}
421 test regexpComp-8.6 {case conversion in regsub} {
422     evalInProc {
423         set x abcdefghijklmnopqrstuvwxyz1234567890
424         set x $x$x$x$x$x$x$x$x$x$x$x$x
425         set foo 123
426         list [regsub -nocase $x $x b foo] $foo
427     }
428 } {1 b}
430 test regexpComp-9.1 {-all option to regsub} {
431     evalInProc {
432         set foo 86
433         list [regsub -all x+ axxxbxxcxdx |&| foo] $foo
434     }
435 } {4 a|xxx|b|xx|c|x|d|x|}
436 test regexpComp-9.2 {-all option to regsub} {
437     evalInProc {
438         set foo 86
439         list [regsub -nocase -all x+ aXxXbxxcXdx |&| foo] $foo
440     }
441 } {4 a|XxX|b|xx|c|X|d|x|}
442 test regexpComp-9.3 {-all option to regsub} {
443     evalInProc {
444         set foo 86
445         list [regsub x+ axxxbxxcxdx |&| foo] $foo
446     }
447 } {1 a|xxx|bxxcxdx}
448 test regexpComp-9.4 {-all option to regsub} {
449     evalInProc {
450         set foo 86
451         list [regsub -all bc axxxbxxcxdx |&| foo] $foo
452     }
453 } {0 axxxbxxcxdx}
454 test regexpComp-9.5 {-all option to regsub} {
455     evalInProc {
456         set foo xxx
457         list [regsub -all node "node node more" yy foo] $foo
458     }
459 } {2 {yy yy more}}
460 test regexpComp-9.6 {-all option to regsub} {
461     evalInProc {
462         set foo xxx
463         list [regsub -all ^ xxx 123 foo] $foo
464     }
465 } {1 123xxx}
467 #test regexpComp-10.1 {expanded syntax in regsub} {
468 #    evalInProc {
469 #       set foo xxx
470 #       list [regsub -expanded ". \#comment\n  . \#comment2" abc def foo] $foo
471 #    }
472 #} {1 defc}
473 test regexpComp-10.2 {newline sensitivity in regsub} {
474     evalInProc {
475         set foo xxx
476         list [regsub -line {^a.*b$} "dabc\naxyb\n" 123 foo] $foo
477     }
478 } "1 {dabc\n123\n}"
479 test regexpComp-10.3 {newline sensitivity in regsub} {
480     evalInProc {
481         set foo xxx
482         list [regsub -line {^a.*b$} "dabc\naxyb\nxb" 123 foo] $foo
483     }
484 } "1 {dabc\n123\nxb}"
485 #test regexpComp-10.4 {partial newline sensitivity in regsub} {
486 #    evalInProc {
487 #       set foo xxx
488 #       list [regsub -lineanchor {^a.*b$} "da\naxyb\nxb" 123 foo] $foo
489 #    }
490 #} "1 {da\n123}"
491 #test regexpComp-10.5 {inverse partial newline sensitivity in regsub} {
492 #    evalInProc {
493 #       set foo xxx
494 #       list [regsub -linestop {a.*b} "da\nbaxyb\nxb" 123 foo] $foo
495 #    }
496 #} "1 {da\nb123\nxb}"
498 test regexpComp-11.1 {regsub errors} {
499     evalInProc {
500         list [catch {regsub a b} msg] $msg
501     }
502 } {1 {wrong # args: should be "regsub ?-switch ...? exp string subSpec ?varName?"}}
503 test regexpComp-11.2 {regsub errors} {
504     evalInProc {
505         list [catch {regsub -nocase a b} msg] $msg
506     }
507 } {1 {wrong # args: should be "regsub ?-switch ...? exp string subSpec ?varName?"}}
508 test regexpComp-11.3 {regsub errors} {
509     evalInProc {
510         list [catch {regsub -nocase -all a b} msg] $msg
511     }
512 } {1 {wrong # args: should be "regsub ?-switch ...? exp string subSpec ?varName?"}}
513 test regexpComp-11.4 {regsub errors} {
514     evalInProc {
515         list [catch {regsub a b c d e f} msg] $msg
516     }
517 } {1 {wrong # args: should be "regsub ?-switch ...? exp string subSpec ?varName?"}}
518 #test regexpComp-11.5 {regsub errors} {
519 #    evalInProc {
520 #       list [catch {regsub -gorp a b c} msg] $msg
521 #    }
522 #} {1 {bad switch "-gorp": must be -all, -nocase, -expanded, -line, -linestop, -lineanchor, -start, or --}}
523 test regexpComp-11.6 {regsub errors} {
524     evalInProc {
525         list [catch {regsub -nocase a( b c d} msg] $msg
526     }
527 } {1 {couldn't compile regular expression pattern: parentheses () not balanced}}
528 test regexpComp-11.7 {regsub errors} {
529     evalInProc {
530         catch {unset f1}
531         set f1 44
532         catch {regsub -nocase aaa aaa xxx f1(f2)} msg
533     }
534 } {1}
535 test regexpComp-11.8 {regsub errors, -start bad int check} {
536     evalInProc {
537         list [catch {regsub -start bogus pattern string rep var} msg] $msg
538     }
539 } {1 {bad index "bogus": must be integer?[+-]integer? or end?[+-]integer?}}
541 # This test crashes on the Mac unless you increase the Stack Space to about 1
542 # Meg.  This is probably bigger than most users want... 
543 # 8.2.3 regexp reduced stack space requirements, but this should be
544 # tested again
545 test regexpComp-12.1 {Tcl_RegExpExec: large number of subexpressions} {
546     evalInProc {
547         list [regexp (.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.) abcdefghijklmnopqrstuvwxyz all a b c d e f g h i j k l m n o p q r s t u v w x y z] $all $a $b $c $d $e $f $g $h $i $j $k $l $m $n $o $p $q $r $s $t $u $v $w $x $y $z
548     }
549 } {1 abcdefghijklmnopqrstuvwxyz a b c d e f g h i j k l m n o p q r s t u v w x y z}
551 test regexpComp-13.1 {regsub of a very large string} {
552     # This test is designed to stress the memory subsystem in order
553     # to catch Bug #933.  It only fails if the Tcl memory allocator
554     # is in use.
556     set line {BEGIN_TABLE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END_TABLE}
557     set filedata [string repeat $line 200]
558     for {set i 1} {$i<10} {incr i} {
559         regsub -all "BEGIN_TABLE " $filedata "" newfiledata
560     }
561     set x done
562 } {done}
564 test regexpComp-14.1 {CompileRegexp: regexp cache} {
565     evalInProc {
566         regexp .*a b
567         regexp .*b c
568         regexp .*c d
569         regexp .*d e
570         regexp .*e f
571         set x .
572         append x *a
573         regexp $x bbba
574     }
575 } 1
576 test regexpComp-14.2 {CompileRegexp: regexp cache, different flags} {
577     evalInProc {
578         regexp .*a b
579         regexp .*b c
580         regexp .*c d
581         regexp .*d e
582         regexp .*e f
583         set x .
584         append x *a
585         regexp -nocase $x bbba
586     }
587 } 1
589 test regexpComp-15.1 {regexp -start} {
590     catch {unset x}
591     list [regexp -start -10 {\d} 1abc2de3 x] $x
592 } {1 1}
593 test regexpComp-15.2 {regexp -start} {
594     catch {unset x}
595     list [regexp -start 2 {\d} 1abc2de3 x] $x
596 } {1 2}
597 test regexpComp-15.3 {regexp -start} {
598     catch {unset x}
599     list [regexp -start 4 {\d} 1abc2de3 x] $x
600 } {1 2}
601 test regexpComp-15.4 {regexp -start} {
602     catch {unset x}
603     list [regexp -start 5 {\d} 1abc2de3 x] $x
604 } {1 3}
605 test regexpComp-15.5 {regexp -start, over end of string} {
606     catch {unset x}
607     list [regexp -start [string length 1abc2de3] {\d} 1abc2de3 x] [info exists x]
608 } {0 0}
609 test regexpComp-15.6 {regexp -start, loss of ^$ behavior} {
610     list [regexp -start 2 {^$} {}]
611 } {0}
613 test regexpComp-16.1 {regsub -start} {
614     catch {unset x}
615     list [regsub -all -start 2 {\d} a1b2c3d4e5 {/&} x] $x
616 } {4 a1b/2c/3d/4e/5}
617 test regexpComp-16.2 {regsub -start} {
618     catch {unset x}
619     list [regsub -all -start -25 {z} hello {/&} x] $x
620 } {0 hello}
621 test regexpComp-16.3 {regsub -start} {
622     catch {unset x}
623     list [regsub -all -start 3 {z} hello {/&} x] $x
624 } {0 hello}
625 #test regexpComp-16.4 {regsub -start, \A behavior} {
626 #    set out {}
627 #    lappend out [regsub -start 0 -all {\A(\w)} {abcde} {/\1} x] $x
628 #    lappend out [regsub -start 2 -all {\A(\w)} {abcde} {/\1} x] $x
629 #} {5 /a/b/c/d/e 3 ab/c/d/e}
631 test regexpComp-17.1 {regexp -inline} {
632     regexp -inline b ababa
633 } {b}
634 test regexpComp-17.2 {regexp -inline} {
635     regexp -inline (b) ababa
636 } {b b}
637 test regexpComp-17.3 {regexp -inline -indices} {
638     regexp -inline -indices (b) ababa
639 } {{1 1} {1 1}}
640 test regexpComp-17.4 {regexp -inline} {
641     regexp -inline {\w(\d+)\w} "   hello 23 there456def "
642 } {e456d 456}
643 test regexpComp-17.5 {regexp -inline no matches} {
644     regexp -inline {\w(\d+)\w} ""
645 } {}
646 test regexpComp-17.6 {regexp -inline no matches} {
647     regexp -inline hello goodbye
648 } {}
649 test regexpComp-17.7 {regexp -inline, no matchvars allowed} {
650     list [catch {regexp -inline b abc match} msg] $msg
651 } {1 {regexp match variables not allowed when using -inline}}
653 test regexpComp-18.1 {regexp -all} {
654     regexp -all b bbbbb
655 } {5}
656 test regexpComp-18.2 {regexp -all} {
657     regexp -all b abababbabaaaaaaaaaab
658 } {6}
659 test regexpComp-18.3 {regexp -all -inline} {
660     regexp -all -inline b abababbabaaaaaaaaaab
661 } {b b b b b b}
662 test regexpComp-18.4 {regexp -all -inline} {
663     regexp -all -inline {\w(\w)} abcdefg
664 } {ab b cd d ef f}
665 test regexpComp-18.5 {regexp -all -inline} {
666     regexp -all -inline {\w(\w)$} abcdefg
667 } {fg g}
668 test regexpComp-18.6 {regexp -all -inline} {
669     regexp -all -inline {\d+} 10:20:30:40
670 } {10 20 30 40}
671 test regexpComp-18.7 {regexp -all -inline} {
672     list [catch {regexp -all -inline b abc match} msg] $msg
673 } {1 {regexp match variables not allowed when using -inline}}
674 test regexpComp-18.8 {regexp -all} {
675     # This should not cause an infinite loop
676     regexp -all -inline {a*} a
677 } {a}
678 test regexpComp-18.9 {regexp -all} {
679     # Yes, the expected result is {a {}}.  Here's why:
680     # Start at index 0; a* matches the "a" there then stops.
681     # Go to index 1; a* matches the lambda (or {}) there then stops.  Recall
682     #   that a* matches zero or more "a"'s; thus it matches the string "b", as
683     #   there are zero or more "a"'s there.
684     # Go to index 2; this is past the end of the string, so stop.
685     regexp -all -inline {a*} ab
686 } {a {}}
687 test regexpComp-18.10 {regexp -all} {
688     # Yes, the expected result is {a {} a}.  Here's why:
689     # Start at index 0; a* matches the "a" there then stops.
690     # Go to index 1; a* matches the lambda (or {}) there then stops.   Recall
691     #   that a* matches zero or more "a"'s; thus it matches the string "b", as
692     #   there are zero or more "a"'s there.
693     # Go to index 2; a* matches the "a" there then stops.
694     # Go to index 3; this is past the end of the string, so stop.
695     regexp -all -inline {a*} aba
696 } {a {} a}
697 test regexpComp-18.11 {regexp -all} {
698     evalInProc {
699         regexp -all -inline {^a} aaaa
700     }
701 } {a}
702 test regexpComp-18.12 {regexp -all -inline -indices} {
703     evalInProc {
704         regexp -all -inline -indices a(b(c)d|e(f)g)h abcdhaefgh
705     }
706 } {{0 4} {1 3} {2 2} {-1 -1} {5 9} {6 8} {-1 -1} {7 7}}
708 test regexpComp-19.1 {regsub null replacement} {
709     evalInProc {
710         regsub -all {@} {@hel@lo@} "\0a\0" result
711         list $result [string length $result]
712     }
713 } "\0a\0hel\0a\0lo\0a\0 14"
715 test regexpComp-20.1 {regsub shared object shimmering} {
716     evalInProc {
717         # Bug #461322
718         set a abcdefghijklmnopqurstuvwxyz 
719         set b $a 
720         set c abcdefghijklmnopqurstuvwxyz0123456789 
721         regsub $a $c $b d 
722         list $d [string length $d] [string bytelength $d]
723     }
724 } [list abcdefghijklmnopqurstuvwxyz0123456789 37 37]
725 #test regexpComp-20.2 {regsub shared object shimmering with -about} {
726 #    evalInProc {
727 #       eval regexp -about abc
728 #    }
729 #} {0 {}}
731 test regexpComp-21.1 {regexp command compiling tests} {
732     evalInProc {
733         regexp foo bar
734     }
735 } 0
736 test regexpComp-21.2 {regexp command compiling tests} {
737     evalInProc {
738         regexp {^foo$} dogfood
739     }
740 } 0
741 test regexpComp-21.3 {regexp command compiling tests} {
742     evalInProc {
743         set a foo
744         regexp {^foo$} $a
745     }
746 } 1
747 test regexpComp-21.4 {regexp command compiling tests} {
748     evalInProc {
749         regexp foo dogfood
750     }
751 } 1
752 test regexpComp-21.5 {regexp command compiling tests} {
753     evalInProc {
754         regexp -nocase FOO dogfod
755     }
756 } 0
757 test regexpComp-21.6 {regexp command compiling tests} {
758     evalInProc {
759         regexp -n foo dogfoOd
760     }
761 } 1
762 test regexpComp-21.7 {regexp command compiling tests} {
763     evalInProc {
764         regexp -no -- FoO dogfood
765     }
766 } 1
767 test regexpComp-21.8 {regexp command compiling tests} {
768     evalInProc {
769         regexp -- foo dogfod
770     }
771 } 0
772 test regexpComp-21.9 {regexp command compiling tests} {
773     evalInProc {
774         list [catch {regexp -- -nocase foo dogfod} msg] $msg
775     }
776 } {0 0}
777 test regexpComp-21.10 {regexp command compiling tests} {
778     evalInProc {
779         list [regsub -all "" foo bar str] $str
780     }
781 } {3 barfbarobaro}
782 # This useless expression fails. Jim returns "bar"
783 #test regexpComp-21.11 {regexp command compiling tests} {
784 #    evalInProc {
785 #       list [regsub -all "" "" bar str] $str
786 #    }
787 #} {0 {}}
789 # We can forgive the underlying regexp engine for not supporting this.
790 # Why not use this instead? "((^X)*|\$)"
791 #test regexpComp-22.0.1 {Bug 1810038} {
792 #    evalInProc {
793 #       regexp ($|^X)* {}
794 #    }
795 #} 1
797 set i 0
798 foreach {str exp result} {
799     foo         ^foo            1
800     foobar      ^foobar$        1
801     foobar      bar$            1
802     foobar      ^$              0
803     ""          ^$              1
804     anything    $               1
805     anything    ^.*$            1
806     anything    ^.*a$           0
807     anything    ^.*a.*$         1
808     anything    ^.*.*$          1
809     anything    ^.*..*$         1
810     anything    ^.*b$           0
811     anything    ^a.*$           1
812 } {
813     test regexpComp-22.[incr i] {regexp command compiling tests} \
814              [subst {evalInProc {set a "$str"; regexp {$exp} \$a}}] $result
817 set i 0
818 foreach {str exp result} {
819     foo         ^foo            1
820     foobar      ^foobar$        1
821     foobar      bar$            1
822     foobar      ^$              0
823     ""          ^$              1
824     anything    $               1
825     anything    ^.*$            1
826     anything    ^.*a$           0
827     anything    ^.*a.*$         1
828     anything    ^.*.*$          1
829     anything    ^.*..*$         1
830     anything    ^.*b$           0
831     anything    ^a.*$           1
832 } {
833     test regexpComp-23.[incr i] {regexp command compiling tests INST_REGEXP} \
834         [list regexp $exp $str] $result
837 test regexpComp-24.1 {regexp command compiling tests} {
838     evalInProc {
839         set re foo
840         regexp -nocase $re bar
841     }
842 } 0
843 test regexpComp-24.2 {regexp command compiling tests} {
844     evalInProc {
845         set re {^foo$}
846         regexp $re dogfood
847     }
848 } 0
849 test regexpComp-24.3 {regexp command compiling tests} {
850     evalInProc {
851         set a foo
852         set re {^foo$}
853         regexp $re $a
854     }
855 } 1
856 test regexpComp-24.4 {regexp command compiling tests} {
857     evalInProc {
858         set re foo
859         regexp $re dogfood
860     }
861 } 1
862 test regexpComp-24.5 {regexp command compiling tests} {
863     evalInProc {
864         set re FOO
865         regexp -nocase $re dogfod
866     }
867 } 0
868 test regexpComp-24.6 {regexp command compiling tests} {
869     evalInProc {
870         set re foo
871         regexp -n $re dogfoOd
872     }
873 } 1
874 test regexpComp-24.7 {regexp command compiling tests} {
875     evalInProc {
876         set re FoO
877         regexp -no -- $re dogfood
878     }
879 } 1
880 test regexpComp-24.8 {regexp command compiling tests} {
881     evalInProc {
882         set re foo
883         regexp -- $re dogfod
884     }
885 } 0
886 test regexpComp-24.9 {regexp command compiling tests} {
887     evalInProc {
888         set re "("
889         list [catch {regexp -- $re dogfod} msg] $msg
890     }
891 } {1 {couldn't compile regular expression pattern: parentheses () not balanced}}
892 test regexpComp-24.10 {regexp command compiling tests} {
893     # Bug 1902436 - last * escaped
894     evalInProc {
895         set text {this is *bold* !}
896         set re {\*bold\*}
897         regexp -- $re $text
898     }
899 } 1
900 test regexpComp-24.11 {regexp command compiling tests} {
901     # Bug 1902436 - last * escaped
902     evalInProc {
903         set text {this is *bold* !}
904         set re {\*bold\*.*!}
905         regexp -- $re $text
906     }
907 } 1
909 test regexp-25.1 {Repeat on escaped char} {
910     regexp {\x41\x42*} bc
911 } 0
913 test regexp-25.2 {Single braced count} {
914     regexp "a{4}" baaaad
915 } 1
917 testreport