Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/lsearch.test @ 47

Last change on this file since 47 was 25, checked in by landauf, 17 years ago

added tcl to libs

File size: 16.2 KB
Line 
1# Commands covered:  lsearch
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# Copyright (c) 1991-1993 The Regents of the University of California.
8# Copyright (c) 1994 Sun Microsystems, Inc.
9# Copyright (c) 1998-1999 by Scriptics Corporation.
10#
11# See the file "license.terms" for information on usage and redistribution
12# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13#
14# RCS: @(#) $Id: lsearch.test,v 1.20 2007/12/13 15:26:06 dgp Exp $
15
16if {[lsearch [namespace children] ::tcltest] == -1} {
17    package require tcltest
18    namespace import -force ::tcltest::*
19}
20
21set x {abcd bbcd 123 234 345}
22test lsearch-1.1 {lsearch command} {
23    lsearch $x 123
24} 2
25test lsearch-1.2 {lsearch command} {
26    lsearch $x 3456
27} -1
28test lsearch-1.3 {lsearch command} {
29    lsearch $x *5
30} 4
31test lsearch-1.4 {lsearch command} {
32    lsearch $x *bc*
33} 0
34
35test lsearch-2.1 {search modes} {
36    lsearch -exact {xyz bbcc *bc*} *bc*
37} 2
38test lsearch-2.2 {search modes} {
39    lsearch -exact {b.x ^bc xy bcx} ^bc
40} 1
41test lsearch-2.3 {search modes} {
42    lsearch -exact {foo bar cat} ba
43} -1
44test lsearch-2.4 {search modes} {
45    lsearch -exact {foo bar cat} bart
46} -1
47test lsearch-2.5 {search modes} {
48    lsearch -exact {foo bar cat} bar
49} 1
50test lsearch-2.6 {search modes} {
51    list [catch {lsearch -regexp {xyz bbcc *bc*} *bc*} msg] $msg
52} {1 {couldn't compile regular expression pattern: quantifier operand invalid}}
53test lsearch-2.7 {search modes} {
54    lsearch -regexp {b.x ^bc xy bcx} ^bc
55} 3
56test lsearch-2.8 {search modes} {
57    lsearch -glob {xyz bbcc *bc*} *bc*
58} 1
59test lsearch-2.9 {search modes} {
60    lsearch -glob {b.x ^bc xy bcx} ^bc
61} 1
62test lsearch-2.10 {search modes} {
63    list [catch {lsearch -glib {b.x bx xy bcx} b.x} msg] $msg
64} {1 {bad option "-glib": must be -all, -ascii, -decreasing, -dictionary, -exact, -glob, -increasing, -index, -inline, -integer, -nocase, -not, -real, -regexp, -sorted, -start, or -subindices}}
65test lsearch-2.11 {search modes with -nocase} {
66    lsearch -exact -nocase {a b c A B C} A
67} 0
68test lsearch-2.12 {search modes with -nocase} {
69    lsearch -glob -nocase {a b c A B C} A*
70} 0
71test lsearch-2.13 {search modes with -nocase} {
72    lsearch -regexp -nocase {a b c A B C} ^A\$
73} 0
74test lsearch-2.14 {search modes without -nocase} {
75    lsearch -exact {a b c A B C} A
76} 3
77test lsearch-2.15 {search modes without -nocase} {
78    lsearch -glob {a b c A B C} A*
79} 3
80test lsearch-2.16 {search modes without -nocase} {
81    lsearch -regexp {a b c A B C} ^A\$
82} 3
83
84test lsearch-3.1 {lsearch errors} {
85    list [catch lsearch msg] $msg
86} {1 {wrong # args: should be "lsearch ?options? list pattern"}}
87test lsearch-3.2 {lsearch errors} {
88    list [catch {lsearch a} msg] $msg
89} {1 {wrong # args: should be "lsearch ?options? list pattern"}}
90test lsearch-3.3 {lsearch errors} {
91    list [catch {lsearch a b c} msg] $msg
92} {1 {bad option "a": must be -all, -ascii, -decreasing, -dictionary, -exact, -glob, -increasing, -index, -inline, -integer, -nocase, -not, -real, -regexp, -sorted, -start, or -subindices}}
93test lsearch-3.4 {lsearch errors} {
94    list [catch {lsearch a b c d} msg] $msg
95} {1 {bad option "a": must be -all, -ascii, -decreasing, -dictionary, -exact, -glob, -increasing, -index, -inline, -integer, -nocase, -not, -real, -regexp, -sorted, -start, or -subindices}}
96test lsearch-3.5 {lsearch errors} {
97    list [catch {lsearch "\{" b} msg] $msg
98} {1 {unmatched open brace in list}}
99test lsearch-3.6 {lsearch errors} {
100    list [catch {lsearch -index a b} msg] $msg
101} {1 {"-index" option must be followed by list index}}
102test lsearch-3.7 {lsearch errors} {
103    list [catch {lsearch -subindices -exact a b} msg] $msg
104} {1 {-subindices cannot be used without -index option}}
105
106test lsearch-4.1 {binary data} {
107    lsearch -exact [list foo one\000two bar] bar
108} 2
109test lsearch-4.2 {binary data} {
110    set x one
111    append x \x00
112    append x two
113    lsearch -exact [list foo one\000two bar] $x
114} 1
115
116# Make a sorted list
117set l {}
118set l2 {}
119for {set i 0} {$i < 100} {incr i} {
120    lappend l $i
121    lappend l2 [expr {double($i)/2}]
122}
123set increasingIntegers [lsort -integer $l]
124set decreasingIntegers [lsort -decreasing -integer $l]
125set increasingDoubles [lsort -real $l2]
126set decreasingDoubles [lsort -decreasing -real $l2]
127set increasingStrings [lsort {48 6a 18b 22a 21aa 35 36}]
128set decreasingStrings [lsort -decreasing {48 6a 18b 22a 21aa 35 36}]
129set increasingDictionary [lsort -dictionary {48 6a 18b 22a 21aa 35 36}]
130set decreasingDictionary [lsort -dictionary -decreasing $increasingDictionary]
131
132set l {}
133for {set i 0} {$i < 10} {incr i} {
134    lappend l $i $i $i $i $i
135}
136set repeatingIncreasingIntegers [lsort -integer $l]
137set repeatingDecreasingIntegers [lsort -integer -decreasing $l]
138
139test lsearch-5.1 {binary search} {
140    set res {}
141    for {set i 0} {$i < 100} {incr i} {
142        lappend res [lsearch -integer -sorted $increasingIntegers $i]
143    }
144    set res
145} $increasingIntegers
146test lsearch-5.2 {binary search} {
147    set res {}
148    for {set i 0} {$i < 100} {incr i} {
149        lappend res [lsearch -integer -decreasing -sorted \
150                $decreasingIntegers $i]
151    }
152    set res
153} $decreasingIntegers
154test lsearch-5.3 {binary search finds leftmost occurances} {
155    set res {}
156    for {set i 0} {$i < 10} {incr i} {
157        lappend res [lsearch -integer -sorted $repeatingIncreasingIntegers $i]
158    }
159    set res
160} [list 0 5 10 15 20 25 30 35 40 45]
161test lsearch-5.4 {binary search -decreasing finds leftmost occurances} {
162    set res {}
163    for {set i 9} {$i >= 0} {incr i -1} {
164        lappend res [lsearch -sorted -integer -decreasing \
165                $repeatingDecreasingIntegers $i]
166    }
167    set res
168} [list 0 5 10 15 20 25 30 35 40 45]
169
170test lsearch-6.1 {integer search} {
171    set res {}
172    for {set i 0} {$i < 100} {incr i} {
173        lappend res [lsearch -exact -integer $increasingIntegers $i]
174    }
175    set res
176} [lrange $increasingIntegers 0 99]
177test lsearch-6.2 {decreasing integer search} {
178    set res {}
179    for {set i 0} {$i < 100} {incr i} {
180        lappend res [lsearch -exact -integer -decreasing \
181                $decreasingIntegers $i]
182    }
183    set res
184} [lrange $decreasingIntegers 0 99]
185test lsearch-6.3 {sorted integer search} {
186    set res {}
187    for {set i 0} {$i < 100} {incr i} {
188        lappend res [lsearch -sorted -integer $increasingIntegers $i]
189    }
190    set res
191} [lrange $increasingIntegers 0 99]
192test lsearch-6.4 {sorted decreasing integer search} {
193    set res {}
194    for {set i 0} {$i < 100} {incr i} {
195        lappend res [lsearch -integer -sorted -decreasing \
196                $decreasingIntegers $i]
197    }
198    set res
199} [lrange $decreasingIntegers 0 99]
200
201test lsearch-7.1 {double search} {
202    set res {}
203    for {set i 0} {$i < 100} {incr i} {
204        lappend res [lsearch -exact -real $increasingDoubles \
205                [expr {double($i)/2}]]
206    }
207    set res
208} [lrange $increasingIntegers 0 99]
209test lsearch-7.2 {decreasing double search} {
210    set res {}
211    for {set i 0} {$i < 100} {incr i} {
212        lappend res [lsearch -exact -real -decreasing \
213                $decreasingDoubles [expr {double($i)/2}]]
214    }
215    set res
216} [lrange $decreasingIntegers 0 99]
217test lsearch-7.3 {sorted double search} {
218    set res {}
219    for {set i 0} {$i < 100} {incr i} {
220        lappend res [lsearch -sorted -real \
221                $increasingDoubles [expr {double($i)/2}]]
222    }
223    set res
224} [lrange $increasingIntegers 0 99]
225test lsearch-7.4 {sorted decreasing double search} {
226    set res {}
227    for {set i 0} {$i < 100} {incr i} {
228        lappend res [lsearch -sorted -real -decreasing \
229                $decreasingDoubles [expr {double($i)/2}]]
230    }
231    set res
232} [lrange $decreasingIntegers 0 99]
233
234test lsearch-8.1 {dictionary search} {
235    set res {}
236    foreach val {6a 18b 21aa 22a 35 36 48} {
237        lappend res [lsearch -exact -dictionary $increasingDictionary $val]
238    }
239    set res
240} [list 0 1 2 3 4 5 6]
241test lsearch-8.2 {decreasing dictionary search} {
242    set res {}
243    foreach val {6a 18b 21aa 22a 35 36 48} {
244        lappend res [lsearch -exact -dictionary $decreasingDictionary $val]
245    }
246    set res
247} [list 6 5 4 3 2 1 0]
248test lsearch-8.3 {sorted dictionary search} {
249    set res {}
250    foreach val {6a 18b 21aa 22a 35 36 48} {
251        lappend res [lsearch -sorted -dictionary $increasingDictionary $val]
252    }
253    set res
254} [list 0 1 2 3 4 5 6]
255test lsearch-8.4 {decreasing sorted dictionary search} {
256    set res {}
257    foreach val {6a 18b 21aa 22a 35 36 48} {
258        lappend res [lsearch -decreasing -sorted -dictionary \
259                $decreasingDictionary $val]
260    }
261    set res
262} [list 6 5 4 3 2 1 0]
263
264test lsearch-9.1 {ascii search} {
265    set res {}
266    foreach val {18b 21aa 22a 35 36 48 6a} {
267        lappend res [lsearch -exact -ascii $increasingStrings $val]
268    }
269    set res
270} [list 0 1 2 3 4 5 6]
271test lsearch-9.2 {decreasing ascii search} {
272    set res {}
273    foreach val {18b 21aa 22a 35 36 48 6a} {
274        lappend res [lsearch -exact -ascii $decreasingStrings $val]
275    }
276    set res
277} [list 6 5 4 3 2 1 0]
278test lsearch-9.3 {sorted ascii search} {
279    set res {}
280    foreach val {18b 21aa 22a 35 36 48 6a} {
281        lappend res [lsearch -sorted -ascii $increasingStrings $val]
282    }
283    set res
284} [list 0 1 2 3 4 5 6]
285test lsearch-9.4 {decreasing sorted ascii search} {
286    set res {}
287    foreach val {18b 21aa 22a 35 36 48 6a} {
288        lappend res [lsearch -decreasing -sorted -ascii \
289                $decreasingStrings $val]
290    }
291    set res
292} [list 6 5 4 3 2 1 0]
293
294test lsearch-10.1 {offset searching} {
295    lsearch -start 2 {a b c a b c} a
296} 3
297test lsearch-10.2 {offset searching} {
298    lsearch -start 2 {a b c d e f} a
299} -1
300test lsearch-10.3 {offset searching} {
301    lsearch -start end-4 {a b c a b c} a
302} 3
303test lsearch-10.4 {offset searching} {
304    list [catch {lsearch -start foobar {a b c a b c} a} msg] $msg
305} {1 {bad index "foobar": must be integer?[+-]integer? or end?[+-]integer?}}
306test lsearch-10.5 {offset searching} {
307    list [catch {lsearch -start 1 2} msg] $msg
308} {1 {missing starting index}}
309test lsearch-10.6 {binary search with offset} {
310    set res {}
311    for {set i 0} {$i < 100} {incr i} {
312        lappend res [lsearch -integer -start 2 -sorted $increasingIntegers $i]
313    }
314    set res
315} [concat -1 -1 [lrange $increasingIntegers 2 end]]
316test lsearch-10.7 {offset searching with an empty list} {
317    # Stop bug #694232 from reocurring
318    lsearch -start 0 {} x
319} -1
320test lsearch-10.8 {offset searching past the end of the list} {
321    # Stop [Bug 1374778] from reoccurring
322    lsearch -start 10 {a b c} c
323} -1
324test lsearch-10.9 {offset searching past the end of the list} {
325    # Stop [Bug 1374778] from reoccurring
326    lsearch -start 10 -all {a b c} c
327} {}
328test lsearch-10.10 {offset searching past the end of the list} {
329    # Stop [Bug 1374778] from reoccurring
330    lsearch -start 10 -inline {a b c} c
331} {}
332
333test lsearch-11.1 {negated searches} {
334    lsearch -not {a a a b a a a} a
335} 3
336test lsearch-11.2 {negated searches} {
337    lsearch -not {a a a a a a a} a
338} -1
339
340test lsearch-12.1 {return values instead of indices} {
341    lsearch -glob -inline {a1 b2 c3 d4} c*
342} c3
343test lsearch-12.2 {return values instead of indices} {
344    lsearch -glob -inline {a1 b2 c3 d4} e*
345} {}
346
347test lsearch-13.1 {search for all matches} {
348    lsearch -all {a b a c a d} 1
349} {}
350test lsearch-13.2 {search for all matches} {
351    lsearch -all {a b a c a d} a
352} {0 2 4}
353test lsearch-13.3 {search for all matches with -nocase} {
354    lsearch -all -exact -nocase {a b c A B C} A
355} {0 3}
356test lsearch-13.4 {search for all matches with -nocase} {
357    lsearch -all -glob -nocase {a b c A B C} A*
358} {0 3}
359test lsearch-13.5 {search for all matches with -nocase} {
360    lsearch -all -regexp -nocase {a b c A B C} ^A\$
361} {0 3}
362
363test lsearch-14.1 {combinations: -all and -inline} {
364    lsearch -all -inline -glob {a1 b2 a3 c4 a5 d6} a*
365} {a1 a3 a5}
366test lsearch-14.2 {combinations: -all, -inline and -not} {
367    lsearch -all -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
368} {b2 c4 d6}
369test lsearch-14.3 {combinations: -all and -not} {
370    lsearch -all -not -glob {a1 b2 a3 c4 a5 d6} a*
371} {1 3 5}
372test lsearch-14.4 {combinations: -inline and -not} {
373    lsearch -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
374} {b2}
375test lsearch-14.5 {combinations: -start, -all and -inline} {
376    lsearch -start 2 -all -inline -glob {a1 b2 a3 c4 a5 d6} a*
377} {a3 a5}
378test lsearch-14.6 {combinations: -start, -all, -inline and -not} {
379    lsearch -start 2 -all -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
380} {c4 d6}
381test lsearch-14.7 {combinations: -start, -all and -not} {
382    lsearch -start 2 -all -not -glob {a1 b2 a3 c4 a5 d6} a*
383} {3 5}
384test lsearch-14.8 {combinations: -start, -inline and -not} {
385    lsearch -start 2 -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
386} {c4}
387
388test lsearch-15.1 {make sure no shimmering occurs} {
389    set x [expr int(sin(0))]
390    lsearch -start $x $x $x
391} 0
392
393test lsearch-16.1 {lsearch -regexp shared object} {
394    set str a
395    lsearch -regexp $str $str
396} 0
397# Bug 1366683
398test lsearch-16.2 {lsearch -regexp allows internal backrefs} {
399    lsearch -regexp {a aa b} {(.)\1}
400} 1
401
402test lsearch-17.1 {lsearch -index option, basic functionality} {
403    lsearch -index 1 {{a c} {a b} {a a}} a
404} 2
405test lsearch-17.2 {lsearch -index option, basic functionality} {
406    lsearch -index 1 -exact {{a c} {a b} {a a}} a
407} 2
408test lsearch-17.3 {lsearch -index option, basic functionality} {
409    lsearch -index 1 -glob {{ab cb} {ab bb} {ab ab}} b*
410} 1
411test lsearch-17.4 {lsearch -index option, basic functionality} {
412    lsearch -index 1 -regexp {{ab cb} {ab bb} {ab ab}} {[cb]b}
413} 0
414test lsearch-17.5 {lsearch -index option, basic functionality} {
415    lsearch -all -index 0 -exact {{a c} {a b} {d a}} a
416} {0 1}
417test lsearch-17.6 {lsearch -index option, basic functionality} {
418    lsearch -all -index 1 -glob {{ab cb} {ab bb} {db bx}} b*
419} {1 2}
420test lsearch-17.7 {lsearch -index option, basic functionality} {
421    lsearch -all -index 1 -regexp {{ab cb} {ab bb} {ab ab}} {[cb]b}
422} {0 1}
423
424test lsearch-18.1 {lsearch -index option, list as index basic functionality} {
425    lsearch -index {0 0} {{{x x} {x b} {a d}} {{a c} {a b} {a a}}} a
426} 1
427test lsearch-18.2 {lsearch -index option, list as index basic functionality} {
428    lsearch -index {2 0} -exact {{{x x} {x b} {a d}} {{a c} {a b} {a a}}} a
429} 0
430test lsearch-18.3 {lsearch -index option, list as index basic functionality} {
431    lsearch -index {1 1} -glob {{{ab cb} {ab bb} {ab ab}} {{ab cb} {ab bb} {ab ab}}} b*
432} 0
433test lsearch-18.4 {lsearch -index option, list as index basic functionality} {
434    lsearch -index {0 1} -regexp {{{ab cb} {ab bb} {ab ab}} {{ab cb} {ab bb} {ab ab}}} {[cb]b}
435} 0
436test lsearch-18.5 {lsearch -index option, list as index basic functionality} {
437    lsearch -all -index {0 0} -exact {{{a c} {a b} {d a}} {{a c} {a b} {d a}}} a
438} {0 1}
439
440test lsearch-19.1 {lsearch -sunindices option} {
441    lsearch -subindices -index {0 0} {{{x x} {x b} {a d}} {{a c} {a b} {a a}}} a
442} {1 0 0}
443test lsearch-19.2 {lsearch -sunindices option} {
444    lsearch -subindices -index {2 0} -exact {{{x x} {x b} {a d}} {{a c} {a b} {a a}}} a
445} {0 2 0}
446test lsearch-19.3 {lsearch -sunindices option} {
447    lsearch -subindices -index {1 1} -glob {{{ab cb} {ab bb} {ab ab}} {{ab cb} {ab bb} {ab ab}}} b*
448} {0 1 1}
449test lsearch-19.4 {lsearch -sunindices option} {
450    lsearch -subindices -index {0 1} -regexp {{{ab cb} {ab bb} {ab ab}} {{ab cb} {ab bb} {ab ab}}} {[cb]b}
451} {0 0 1}
452test lsearch-19.5 {lsearch -sunindices option} {
453    lsearch -subindices -all -index {0 0} -exact {{{a c} {a b} {d a}} {{a c} {a b} {d a}}} a
454} {{0 0 0} {1 0 0}}
455
456test lsearch-20.1 {lsearch -index option, index larger than sublists} {
457    list [catch {lsearch -index 2 {{a c} {a b} {a a}} a} msg] $msg
458} {1 {element 2 missing from sublist "a c"}}
459test lsearch-20.2 {lsearch -index option, malformed index} {
460    list [catch {lsearch -index foo {{a c} {a b} {a a}} a} msg] $msg
461} {1 {bad index "foo": must be integer?[+-]integer? or end?[+-]integer?}}
462test lsearch-20.3 {lsearch -index option, malformed index} {
463    list [catch {lsearch -index \{ {{a c} {a b} {a a}} a} msg] $msg
464} {1 {unmatched open brace in list}}
465
466test lsearch-21.1 {lsearch shimmering crash} {
467    set x 0
468    lsearch -exact -integer $x $x
469} 0
470test lsearch-21.2 {lsearch shimmering crash} {
471    set x 0.5
472    lsearch -exact -real $x $x
473} 0
474
475# cleanup
476catch {unset res}
477catch {unset increasingIntegers}
478catch {unset decreasingIntegers}
479catch {unset increasingDoubles}
480catch {unset decreasingDoubles}
481catch {unset increasingStrings}
482catch {unset decreasingStrings}
483catch {unset increasingDictionary}
484catch {unset decreasingDictionary}
485::tcltest::cleanupTests
486return
Note: See TracBrowser for help on using the repository browser.