os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/lsearch.test
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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.10.2.3 2005/12/09 14:39:25 dkf Exp $
    15 
    16 if {[lsearch [namespace children] ::tcltest] == -1} {
    17     package require tcltest
    18     namespace import -force ::tcltest::*
    19 }
    20 
    21 set x {abcd bbcd 123 234 345}
    22 test lsearch-1.1 {lsearch command} {
    23     lsearch $x 123
    24 } 2
    25 test lsearch-1.2 {lsearch command} {
    26     lsearch $x 3456
    27 } -1
    28 test lsearch-1.3 {lsearch command} {
    29     lsearch $x *5
    30 } 4
    31 test lsearch-1.4 {lsearch command} {
    32     lsearch $x *bc*
    33 } 0
    34 
    35 test lsearch-2.1 {search modes} {
    36     lsearch -exact {xyz bbcc *bc*} *bc*
    37 } 2
    38 test lsearch-2.2 {search modes} {
    39     lsearch -exact {b.x ^bc xy bcx} ^bc
    40 } 1
    41 test lsearch-2.3 {search modes} {
    42     lsearch -exact {foo bar cat} ba
    43 } -1
    44 test lsearch-2.4 {search modes} {
    45     lsearch -exact {foo bar cat} bart
    46 } -1
    47 test lsearch-2.5 {search modes} {
    48     lsearch -exact {foo bar cat} bar
    49 } 1
    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
    55 } 3
    56 test lsearch-2.8 {search modes} {
    57     lsearch -glob {xyz bbcc *bc*} *bc*
    58 } 1
    59 test lsearch-2.9 {search modes} {
    60     lsearch -glob {b.x ^bc xy bcx} ^bc
    61 } 1
    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}}
    65 
    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}}
    81 
    82 test lsearch-4.1 {binary data} {
    83     lsearch -exact [list foo one\000two bar] bar
    84 } 2
    85 test lsearch-4.2 {binary data} {
    86     set x one
    87     append x \x00
    88     append x two
    89     lsearch -exact [list foo one\000two bar] $x
    90 } 1
    91 
    92 # Make a sorted list
    93 set l {}
    94 set l2 {}
    95 for {set i 0} {$i < 100} {incr i} {
    96     lappend l $i
    97     lappend l2 [expr {double($i)/2}]
    98 }
    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]
   107 
   108 set l {}
   109 for {set i 0} {$i < 10} {incr i} {
   110     lappend l $i $i $i $i $i
   111 }
   112 set repeatingIncreasingIntegers [lsort -integer $l]
   113 set repeatingDecreasingIntegers [lsort -integer -decreasing $l]
   114 
   115 test lsearch-5.1 {binary search} {
   116     set res {}
   117     for {set i 0} {$i < 100} {incr i} {
   118 	lappend res [lsearch -integer -sorted $increasingIntegers $i]
   119     }
   120     set res
   121 } $increasingIntegers
   122 test lsearch-5.2 {binary search} {
   123     set res {}
   124     for {set i 0} {$i < 100} {incr i} {
   125 	lappend res [lsearch -integer -decreasing -sorted \
   126 		$decreasingIntegers $i]
   127     }
   128     set res
   129 } $decreasingIntegers
   130 test lsearch-5.3 {binary search finds leftmost occurances} {
   131     set res {}
   132     for {set i 0} {$i < 10} {incr i} {
   133 	lappend res [lsearch -integer -sorted $repeatingIncreasingIntegers $i]
   134     }
   135     set res
   136 } [list 0 5 10 15 20 25 30 35 40 45]
   137 test lsearch-5.4 {binary search -decreasing finds leftmost occurances} {
   138     set res {}
   139     for {set i 9} {$i >= 0} {incr i -1} {
   140 	lappend res [lsearch -sorted -integer -decreasing \
   141 		$repeatingDecreasingIntegers $i]
   142     }
   143     set res
   144 } [list 0 5 10 15 20 25 30 35 40 45]
   145 
   146 test lsearch-6.1 {integer search} {
   147     set res {}
   148     for {set i 0} {$i < 100} {incr i} {
   149 	lappend res [lsearch -exact -integer $increasingIntegers $i]
   150     }
   151     set res
   152 } [lrange $increasingIntegers 0 99]
   153 test lsearch-6.2 {decreasing integer search} {
   154     set res {}
   155     for {set i 0} {$i < 100} {incr i} {
   156 	lappend res [lsearch -exact -integer -decreasing \
   157 		$decreasingIntegers $i]
   158     }
   159     set res
   160 } [lrange $decreasingIntegers 0 99]
   161 test lsearch-6.3 {sorted integer search} {
   162     set res {}
   163     for {set i 0} {$i < 100} {incr i} {
   164 	lappend res [lsearch -sorted -integer $increasingIntegers $i]
   165     }
   166     set res
   167 } [lrange $increasingIntegers 0 99]
   168 test lsearch-6.4 {sorted decreasing integer search} {
   169     set res {}
   170     for {set i 0} {$i < 100} {incr i} {
   171 	lappend res [lsearch -integer -sorted -decreasing \
   172 		$decreasingIntegers $i]
   173     }
   174     set res
   175 } [lrange $decreasingIntegers 0 99]
   176 
   177 test lsearch-7.1 {double search} {
   178     set res {}
   179     for {set i 0} {$i < 100} {incr i} {
   180 	lappend res [lsearch -exact -real $increasingDoubles \
   181 		[expr {double($i)/2}]]
   182     }
   183     set res
   184 } [lrange $increasingIntegers 0 99]
   185 test lsearch-7.2 {decreasing double search} {
   186     set res {}
   187     for {set i 0} {$i < 100} {incr i} {
   188 	lappend res [lsearch -exact -real -decreasing \
   189 		$decreasingDoubles [expr {double($i)/2}]]
   190     }
   191     set res
   192 } [lrange $decreasingIntegers 0 99]
   193 test lsearch-7.3 {sorted double search} {
   194     set res {}
   195     for {set i 0} {$i < 100} {incr i} {
   196 	lappend res [lsearch -sorted -real \
   197 		$increasingDoubles [expr {double($i)/2}]]
   198     }
   199     set res
   200 } [lrange $increasingIntegers 0 99]
   201 test lsearch-7.4 {sorted decreasing double search} {
   202     set res {}
   203     for {set i 0} {$i < 100} {incr i} {
   204 	lappend res [lsearch -sorted -real -decreasing \
   205 		$decreasingDoubles [expr {double($i)/2}]]
   206     }
   207     set res
   208 } [lrange $decreasingIntegers 0 99]
   209 
   210 test lsearch-8.1 {dictionary search} {
   211     set res {}
   212     foreach val {6a 18b 21aa 22a 35 36 48} {
   213 	lappend res [lsearch -exact -dictionary $increasingDictionary $val]
   214     }
   215     set res
   216 } [list 0 1 2 3 4 5 6]
   217 test lsearch-8.2 {decreasing dictionary search} {
   218     set res {}
   219     foreach val {6a 18b 21aa 22a 35 36 48} {
   220 	lappend res [lsearch -exact -dictionary $decreasingDictionary $val]
   221     }
   222     set res
   223 } [list 6 5 4 3 2 1 0]
   224 test lsearch-8.3 {sorted dictionary search} {
   225     set res {}
   226     foreach val {6a 18b 21aa 22a 35 36 48} {
   227 	lappend res [lsearch -sorted -dictionary $increasingDictionary $val]
   228     }
   229     set res
   230 } [list 0 1 2 3 4 5 6]
   231 test lsearch-8.4 {decreasing sorted dictionary search} {
   232     set res {}
   233     foreach val {6a 18b 21aa 22a 35 36 48} {
   234 	lappend res [lsearch -decreasing -sorted -dictionary \
   235 		$decreasingDictionary $val]
   236     }
   237     set res
   238 } [list 6 5 4 3 2 1 0]
   239 
   240 test lsearch-9.1 {ascii search} {
   241     set res {}
   242     foreach val {18b 21aa 22a 35 36 48 6a} {
   243 	lappend res [lsearch -exact -ascii $increasingStrings $val]
   244     }
   245     set res
   246 } [list 0 1 2 3 4 5 6]
   247 test lsearch-9.2 {decreasing ascii search} {
   248     set res {}
   249     foreach val {18b 21aa 22a 35 36 48 6a} {
   250 	lappend res [lsearch -exact -ascii $decreasingStrings $val]
   251     }
   252     set res
   253 } [list 6 5 4 3 2 1 0]
   254 test lsearch-9.3 {sorted ascii search} {
   255     set res {}
   256     foreach val {18b 21aa 22a 35 36 48 6a} {
   257 	lappend res [lsearch -sorted -ascii $increasingStrings $val]
   258     }
   259     set res
   260 } [list 0 1 2 3 4 5 6]
   261 test lsearch-9.4 {decreasing sorted ascii search} {
   262     set res {}
   263     foreach val {18b 21aa 22a 35 36 48 6a} {
   264 	lappend res [lsearch -decreasing -sorted -ascii \
   265 		$decreasingStrings $val]
   266     }
   267     set res
   268 } [list 6 5 4 3 2 1 0]
   269 
   270 test lsearch-10.1 {offset searching} {
   271     lsearch -start 2 {a b c a b c} a
   272 } 3
   273 test lsearch-10.2 {offset searching} {
   274     lsearch -start 2 {a b c d e f} a
   275 } -1
   276 test lsearch-10.3 {offset searching} {
   277     lsearch -start end-4 {a b c a b c} a
   278 } 3
   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} {
   286     set res {}
   287     for {set i 0} {$i < 100} {incr i} {
   288 	lappend res [lsearch -integer -start 2 -sorted $increasingIntegers $i]
   289     }
   290     set res
   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
   295 } -1
   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
   299 } -1
   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
   303 } {}
   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
   307 } {}
   308 
   309 test lsearch-11.1 {negated searches} {
   310     lsearch -not {a a a b a a a} a
   311 } 3
   312 test lsearch-11.2 {negated searches} {
   313     lsearch -not {a a a a a a a} a
   314 } -1
   315 
   316 test lsearch-12.1 {return values instead of indices} {
   317     lsearch -glob -inline {a1 b2 c3 d4} c*
   318 } c3
   319 test lsearch-12.2 {return values instead of indices} {
   320     lsearch -glob -inline {a1 b2 c3 d4} e*
   321 } {}
   322 
   323 test lsearch-13.1 {search for all matches} {
   324     lsearch -all {a b a c a d} 1
   325 } {}
   326 test lsearch-13.2 {search for all matches} {
   327     lsearch -all {a b a c a d} a
   328 } {0 2 4}
   329 
   330 test lsearch-14.1 {combinations: -all and -inline} {
   331     lsearch -all -inline -glob {a1 b2 a3 c4 a5 d6} a*
   332 } {a1 a3 a5}
   333 test lsearch-14.2 {combinations: -all, -inline and -not} {
   334     lsearch -all -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
   335 } {b2 c4 d6}
   336 test lsearch-14.3 {combinations: -all and -not} {
   337     lsearch -all -not -glob {a1 b2 a3 c4 a5 d6} a*
   338 } {1 3 5}
   339 test lsearch-14.4 {combinations: -inline and -not} {
   340     lsearch -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
   341 } {b2}
   342 test lsearch-14.5 {combinations: -start, -all and -inline} {
   343     lsearch -start 2 -all -inline -glob {a1 b2 a3 c4 a5 d6} a*
   344 } {a3 a5}
   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*
   347 } {c4 d6}
   348 test lsearch-14.7 {combinations: -start, -all and -not} {
   349     lsearch -start 2 -all -not -glob {a1 b2 a3 c4 a5 d6} a*
   350 } {3 5}
   351 test lsearch-14.8 {combinations: -start, -inline and -not} {
   352     lsearch -start 2 -inline -not -glob {a1 b2 a3 c4 a5 d6} a*
   353 } {c4}
   354 
   355 test lsearch-15.1 {make sure no shimmering occurs} {
   356     set x [expr int(sin(0))]
   357     lsearch -start $x $x $x
   358 } 0
   359 
   360 test lsearch-16.1 {lsearch -regexp shared object} {
   361     set str a
   362     lsearch -regexp $str $str
   363 } 0
   364 # Bug 1366683
   365 test lsearch-16.2 {lsearch -regexp allows internal backrefs} {
   366     lsearch -regexp {a aa b} {(.)\1}
   367 } 1
   368 
   369 # cleanup
   370 catch {unset res}
   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
   380 return