os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/lsearch.test
Update contrib.
1 # Commands covered: lsearch
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) 1994 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: lsearch.test,v 1.10.2.3 2005/12/09 14:39:25 dkf Exp $
16 if {[lsearch [namespace children] ::tcltest] == -1} {
17 package require tcltest
18 namespace import -force ::tcltest::*
21 set x {abcd bbcd 123 234 345}
22 test lsearch-1.1 {lsearch command} {
25 test lsearch-1.2 {lsearch command} {
28 test lsearch-1.3 {lsearch command} {
31 test lsearch-1.4 {lsearch command} {
35 test lsearch-2.1 {search modes} {
36 lsearch -exact {xyz bbcc *bc*} *bc*
38 test lsearch-2.2 {search modes} {
39 lsearch -exact {b.x ^bc xy bcx} ^bc
41 test lsearch-2.3 {search modes} {
42 lsearch -exact {foo bar cat} ba
44 test lsearch-2.4 {search modes} {
45 lsearch -exact {foo bar cat} bart
47 test lsearch-2.5 {search modes} {
48 lsearch -exact {foo bar cat} bar
50 test 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}}
53 test lsearch-2.7 {search modes} {
54 lsearch -regexp {b.x ^bc xy bcx} ^bc
56 test lsearch-2.8 {search modes} {
57 lsearch -glob {xyz bbcc *bc*} *bc*
59 test lsearch-2.9 {search modes} {
60 lsearch -glob {b.x ^bc xy bcx} ^bc
62 test 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, -inline, -integer, -not, -real, -regexp, -sorted, or -start}}
66 test lsearch-3.1 {lsearch errors} {
67 list [catch lsearch msg] $msg
68 } {1 {wrong # args: should be "lsearch ?options? list pattern"}}
69 test lsearch-3.2 {lsearch errors} {
70 list [catch {lsearch a} msg] $msg
71 } {1 {wrong # args: should be "lsearch ?options? list pattern"}}
72 test lsearch-3.3 {lsearch errors} {
73 list [catch {lsearch a b c} msg] $msg
74 } {1 {bad option "a": must be -all, -ascii, -decreasing, -dictionary, -exact, -glob, -increasing, -inline, -integer, -not, -real, -regexp, -sorted, or -start}}
75 test lsearch-3.4 {lsearch errors} {
76 list [catch {lsearch a b c d} msg] $msg
77 } {1 {bad option "a": must be -all, -ascii, -decreasing, -dictionary, -exact, -glob, -increasing, -inline, -integer, -not, -real, -regexp, -sorted, or -start}}
78 test lsearch-3.5 {lsearch errors} {
79 list [catch {lsearch "\{" b} msg] $msg
80 } {1 {unmatched open brace in list}}
82 test lsearch-4.1 {binary data} {
83 lsearch -exact [list foo one\000two bar] bar
85 test lsearch-4.2 {binary data} {
89 lsearch -exact [list foo one\000two bar] $x
95 for {set i 0} {$i < 100} {incr i} {
97 lappend l2 [expr {double($i)/2}]
99 set increasingIntegers [lsort -integer $l]
100 set decreasingIntegers [lsort -decreasing -integer $l]
101 set increasingDoubles [lsort -real $l2]
102 set decreasingDoubles [lsort -decreasing -real $l2]
103 set increasingStrings [lsort {48 6a 18b 22a 21aa 35 36}]
104 set decreasingStrings [lsort -decreasing {48 6a 18b 22a 21aa 35 36}]
105 set increasingDictionary [lsort -dictionary {48 6a 18b 22a 21aa 35 36}]
106 set decreasingDictionary [lsort -dictionary -decreasing $increasingDictionary]
109 for {set i 0} {$i < 10} {incr i} {
110 lappend l $i $i $i $i $i
112 set repeatingIncreasingIntegers [lsort -integer $l]
113 set repeatingDecreasingIntegers [lsort -integer -decreasing $l]
115 test lsearch-5.1 {binary search} {
117 for {set i 0} {$i < 100} {incr i} {
118 lappend res [lsearch -integer -sorted $increasingIntegers $i]
121 } $increasingIntegers
122 test lsearch-5.2 {binary search} {
124 for {set i 0} {$i < 100} {incr i} {
125 lappend res [lsearch -integer -decreasing -sorted \
126 $decreasingIntegers $i]
129 } $decreasingIntegers
130 test lsearch-5.3 {binary search finds leftmost occurances} {
132 for {set i 0} {$i < 10} {incr i} {
133 lappend res [lsearch -integer -sorted $repeatingIncreasingIntegers $i]
136 } [list 0 5 10 15 20 25 30 35 40 45]
137 test lsearch-5.4 {binary search -decreasing finds leftmost occurances} {
139 for {set i 9} {$i >= 0} {incr i -1} {
140 lappend res [lsearch -sorted -integer -decreasing \
141 $repeatingDecreasingIntegers $i]
144 } [list 0 5 10 15 20 25 30 35 40 45]
146 test lsearch-6.1 {integer search} {
148 for {set i 0} {$i < 100} {incr i} {
149 lappend res [lsearch -exact -integer $increasingIntegers $i]
152 } [lrange $increasingIntegers 0 99]
153 test lsearch-6.2 {decreasing integer search} {
155 for {set i 0} {$i < 100} {incr i} {
156 lappend res [lsearch -exact -integer -decreasing \
157 $decreasingIntegers $i]
160 } [lrange $decreasingIntegers 0 99]
161 test lsearch-6.3 {sorted integer search} {
163 for {set i 0} {$i < 100} {incr i} {
164 lappend res [lsearch -sorted -integer $increasingIntegers $i]
167 } [lrange $increasingIntegers 0 99]
168 test lsearch-6.4 {sorted decreasing integer search} {
170 for {set i 0} {$i < 100} {incr i} {
171 lappend res [lsearch -integer -sorted -decreasing \
172 $decreasingIntegers $i]
175 } [lrange $decreasingIntegers 0 99]
177 test lsearch-7.1 {double search} {
179 for {set i 0} {$i < 100} {incr i} {
180 lappend res [lsearch -exact -real $increasingDoubles \
181 [expr {double($i)/2}]]
184 } [lrange $increasingIntegers 0 99]
185 test lsearch-7.2 {decreasing double search} {
187 for {set i 0} {$i < 100} {incr i} {
188 lappend res [lsearch -exact -real -decreasing \
189 $decreasingDoubles [expr {double($i)/2}]]
192 } [lrange $decreasingIntegers 0 99]
193 test lsearch-7.3 {sorted double search} {
195 for {set i 0} {$i < 100} {incr i} {
196 lappend res [lsearch -sorted -real \
197 $increasingDoubles [expr {double($i)/2}]]
200 } [lrange $increasingIntegers 0 99]
201 test lsearch-7.4 {sorted decreasing double search} {
203 for {set i 0} {$i < 100} {incr i} {
204 lappend res [lsearch -sorted -real -decreasing \
205 $decreasingDoubles [expr {double($i)/2}]]
208 } [lrange $decreasingIntegers 0 99]
210 test lsearch-8.1 {dictionary search} {
212 foreach val {6a 18b 21aa 22a 35 36 48} {
213 lappend res [lsearch -exact -dictionary $increasingDictionary $val]
216 } [list 0 1 2 3 4 5 6]
217 test lsearch-8.2 {decreasing dictionary search} {
219 foreach val {6a 18b 21aa 22a 35 36 48} {
220 lappend res [lsearch -exact -dictionary $decreasingDictionary $val]
223 } [list 6 5 4 3 2 1 0]
224 test lsearch-8.3 {sorted dictionary search} {
226 foreach val {6a 18b 21aa 22a 35 36 48} {
227 lappend res [lsearch -sorted -dictionary $increasingDictionary $val]
230 } [list 0 1 2 3 4 5 6]
231 test lsearch-8.4 {decreasing sorted dictionary search} {
233 foreach val {6a 18b 21aa 22a 35 36 48} {
234 lappend res [lsearch -decreasing -sorted -dictionary \
235 $decreasingDictionary $val]
238 } [list 6 5 4 3 2 1 0]
240 test lsearch-9.1 {ascii search} {
242 foreach val {18b 21aa 22a 35 36 48 6a} {
243 lappend res [lsearch -exact -ascii $increasingStrings $val]
246 } [list 0 1 2 3 4 5 6]
247 test lsearch-9.2 {decreasing ascii search} {
249 foreach val {18b 21aa 22a 35 36 48 6a} {
250 lappend res [lsearch -exact -ascii $decreasingStrings $val]
253 } [list 6 5 4 3 2 1 0]
254 test lsearch-9.3 {sorted ascii search} {
256 foreach val {18b 21aa 22a 35 36 48 6a} {
257 lappend res [lsearch -sorted -ascii $increasingStrings $val]
260 } [list 0 1 2 3 4 5 6]
261 test lsearch-9.4 {decreasing sorted ascii search} {
263 foreach val {18b 21aa 22a 35 36 48 6a} {
264 lappend res [lsearch -decreasing -sorted -ascii \
265 $decreasingStrings $val]
268 } [list 6 5 4 3 2 1 0]
270 test lsearch-10.1 {offset searching} {
271 lsearch -start 2 {a b c a b c} a
273 test lsearch-10.2 {offset searching} {
274 lsearch -start 2 {a b c d e f} a
276 test lsearch-10.3 {offset searching} {
277 lsearch -start end-4 {a b c a b c} a
279 test lsearch-10.4 {offset searching} {
280 list [catch {lsearch -start foobar {a b c a b c} a} msg] $msg
281 } {1 {bad index "foobar": must be integer or end?-integer?}}
282 test lsearch-10.5 {offset searching} {
283 list [catch {lsearch -start 1 2} msg] $msg
284 } {1 {missing starting index}}
285 test lsearch-10.6 {binary search with offset} {
287 for {set i 0} {$i < 100} {incr i} {
288 lappend res [lsearch -integer -start 2 -sorted $increasingIntegers $i]
291 } [concat -1 -1 [lrange $increasingIntegers 2 end]]
292 test lsearch-10.7 {offset searching with an empty list} {
293 # Stop bug #694232 from reocurring
294 lsearch -start 0 {} x
296 test lsearch-10.8 {offset searching past the end of the list} {
297 # Stop [Bug 1374778] from reoccurring
298 lsearch -start 10 {a b c} c
300 test lsearch-10.9 {offset searching past the end of the list} {
301 # Stop [Bug 1374778] from reoccurring
302 lsearch -start 10 -all {a b c} c
304 test lsearch-10.10 {offset searching past the end of the list} {
305 # Stop [Bug 1374778] from reoccurring
306 lsearch -start 10 -inline {a b c} c
309 test lsearch-11.1 {negated searches} {
310 lsearch -not {a a a b a a a} a
312 test lsearch-11.2 {negated searches} {
313 lsearch -not {a a a a a a a} a
316 test lsearch-12.1 {return values instead of indices} {
317 lsearch -glob -inline {a1 b2 c3 d4} c*
319 test lsearch-12.2 {return values instead of indices} {
320 lsearch -glob -inline {a1 b2 c3 d4} e*
323 test lsearch-13.1 {search for all matches} {
324 lsearch -all {a b a c a d} 1
326 test lsearch-13.2 {search for all matches} {
327 lsearch -all {a b a c a d} a
330 test lsearch-14.1 {combinations: -all and -inline} {
331 lsearch -all -inline -glob {a1 b2 a3 c4 a5 d6} a*
333 test lsearch-14.2 {combinations: -all, -inline and -not} {
334 lsearch -all -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
336 test lsearch-14.3 {combinations: -all and -not} {
337 lsearch -all -not -glob {a1 b2 a3 c4 a5 d6} a*
339 test lsearch-14.4 {combinations: -inline and -not} {
340 lsearch -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
342 test lsearch-14.5 {combinations: -start, -all and -inline} {
343 lsearch -start 2 -all -inline -glob {a1 b2 a3 c4 a5 d6} a*
345 test lsearch-14.6 {combinations: -start, -all, -inline and -not} {
346 lsearch -start 2 -all -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
348 test lsearch-14.7 {combinations: -start, -all and -not} {
349 lsearch -start 2 -all -not -glob {a1 b2 a3 c4 a5 d6} a*
351 test lsearch-14.8 {combinations: -start, -inline and -not} {
352 lsearch -start 2 -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
355 test lsearch-15.1 {make sure no shimmering occurs} {
356 set x [expr int(sin(0))]
357 lsearch -start $x $x $x
360 test lsearch-16.1 {lsearch -regexp shared object} {
362 lsearch -regexp $str $str
365 test lsearch-16.2 {lsearch -regexp allows internal backrefs} {
366 lsearch -regexp {a aa b} {(.)\1}
371 catch {unset increasingIntegers}
372 catch {unset decreasingIntegers}
373 catch {unset increasingDoubles}
374 catch {unset decreasingDoubles}
375 catch {unset increasingStrings}
376 catch {unset decreasingStrings}
377 catch {unset increasingDictionary}
378 catch {unset decreasingDictionary}
379 ::tcltest::cleanupTests