os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/lindex.test
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 # Commands covered:  lindex
     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 # Copyright (c) 2001 by Kevin B. Kenny.  All rights reserved.
    11 #
    12 # See the file "license.terms" for information on usage and redistribution
    13 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    14 #
    15 # RCS: @(#) $Id: lindex.test,v 1.10 2002/04/19 13:08:56 dkf Exp $
    16 
    17 if {[lsearch [namespace children] ::tcltest] == -1} {
    18     package require tcltest
    19     namespace import -force ::tcltest::*
    20 }
    21 
    22 set lindex lindex
    23 set minus -
    24 
    25 # Tests of Tcl_LindexObjCmd, NOT COMPILED
    26 
    27 test lindex-1.1 {wrong # args} {
    28     list [catch {eval $lindex} result] $result
    29 } "1 {wrong # args: should be \"lindex list ?index...?\"}"
    30 
    31 # Indices that are lists or convertible to lists
    32 
    33 test lindex-2.1 {empty index list} {
    34     set x {}
    35     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
    36 } {{a b c} {a b c}}
    37 
    38 test lindex-2.2 {singleton index list} {
    39     set x { 1 }
    40     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
    41 } {b b}
    42 
    43 test lindex-2.3 {multiple indices in list} {
    44     set x {1 2}
    45     list [eval [list $lindex {{a b c} {d e f}} $x]] \
    46 	[eval [list $lindex {{a b c} {d e f}} $x]]
    47 } {f f}
    48 
    49 test lindex-2.4 {malformed index list} {
    50     set x \{
    51     list [catch { eval [list $lindex {a b c} $x] } result] $result
    52 } {1 bad\ index\ \"\{\":\ must\ be\ integer\ or\ end?-integer?}
    53 
    54 # Indices that are integers or convertible to integers
    55 
    56 test lindex-3.1 {integer -1} {
    57     set x ${minus}1
    58     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
    59 } {{} {}}
    60 
    61 test lindex-3.2 {integer 0} {
    62     set x [string range 00 0 0]
    63     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
    64 } {a a}
    65 
    66 test lindex-3.3 {integer 2} {
    67     set x [string range 22 0 0]
    68     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
    69 } {c c}
    70 
    71 test lindex-3.4 {integer 3} {
    72     set x [string range 33 0 0]
    73     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
    74 } {{} {}}
    75 
    76 test lindex-3.5 {bad octal} {
    77     set x 08
    78     list [catch { eval [list $lindex {a b c} $x] } result] $result
    79 } "1 {bad index \"08\": must be integer or end?-integer? (looks like invalid octal number)}"
    80 
    81 test lindex-3.6 {bad octal} {
    82     set x -09
    83     list [catch { eval [list $lindex {a b c} $x] } result] $result
    84 } "1 {bad index \"-09\": must be integer or end?-integer? (looks like invalid octal number)}"
    85 
    86 test lindex-3.7 {indexes don't shimmer wide ints} {
    87     set x [expr {(wide(1)<<31) - 2}]
    88     list $x [lindex {1 2 3} $x] [incr x] [incr x]
    89 } {2147483646 {} 2147483647 2147483648}
    90 
    91 # Indices relative to end
    92 
    93 test lindex-4.1 {index = end} {
    94     set x end
    95     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
    96 } {c c}
    97 
    98 test lindex-4.2 {index = end--1} {
    99     set x end--1
   100     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
   101 } {{} {}}
   102 
   103 test lindex-4.3 {index = end-0} {
   104     set x end-0
   105     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
   106 } {c c}
   107 
   108 test lindex-4.4 {index = end-2} {
   109     set x end-2
   110     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
   111 } {a a}
   112 
   113 test lindex-4.5 {index = end-3} {
   114     set x end-3
   115     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
   116 } {{} {}}
   117 
   118 test lindex-4.6 {bad octal} {
   119     set x end-08
   120     list [catch { eval [list $lindex {a b c} $x] } result] $result
   121 } "1 {bad index \"end-08\": must be integer or end?-integer? (looks like invalid octal number)}"
   122 
   123 test lindex-4.7 {bad octal} {
   124     set x end--09
   125     list [catch { eval [list $lindex {a b c} $x] } result] $result
   126 } "1 {bad index \"end--09\": must be integer or end?-integer?}"
   127 
   128 test lindex-4.8 {bad integer, not octal} {
   129     set x end-0a2
   130     list [catch { eval [list $lindex {a b c} $x] } result] $result
   131 } "1 {bad index \"end-0a2\": must be integer or end?-integer?}"
   132 
   133 test lindex-4.9 {incomplete end} {
   134     set x en
   135     list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
   136 } {c c}
   137 
   138 test lindex-4.10 {incomplete end-} {
   139     set x end-
   140     list [catch { eval [list $lindex {a b c} $x] } result] $result
   141 } "1 {bad index \"end-\": must be integer or end?-integer?}"
   142 
   143 test lindex-5.1 {bad second index} {
   144     list [catch { eval [list $lindex {a b c} 0 0a2] } result] $result
   145 } "1 {bad index \"0a2\": must be integer or end?-integer?}"
   146 
   147 test lindex-5.2 {good second index} {
   148     eval [list $lindex {{a b c} {d e f} {g h i}} 1 2]
   149 } f
   150 
   151 test lindex-5.3 {three indices} {
   152     eval [list $lindex {{{a b} {c d}} {{e f} {g h}}} 1 0 1]
   153 } f
   154 test lindex-6.1 {error conditions in parsing list} {
   155     list [catch {eval [list $lindex "a \{" 2]} msg] $msg
   156 } {1 {unmatched open brace in list}}
   157 test lindex-6.2 {error conditions in parsing list} {
   158     list [catch {eval [list $lindex {a {b c}d e} 2]} msg] $msg
   159 } {1 {list element in braces followed by "d" instead of space}}
   160 test lindex-6.3 {error conditions in parsing list} {
   161     list [catch {eval [list $lindex {a "b c"def ghi} 2]} msg] $msg
   162 } {1 {list element in quotes followed by "def" instead of space}}
   163 
   164 test lindex-7.1 {quoted elements} {
   165     eval [list $lindex {a "b c" d} 1]
   166 } {b c}
   167 test lindex-7.2 {quoted elements} {
   168     eval [list $lindex {"{}" b c} 0]
   169 } {{}}
   170 test lindex-7.3 {quoted elements} {
   171     eval [list $lindex {ab "c d \" x" y} 1]
   172 } {c d " x}
   173 test lindex-7.4 {quoted elements} {
   174     lindex {a b {c d "e} {f g"}} 2
   175 } {c d "e}
   176 
   177 test lindex-8.1 {data reuse} {
   178     set x 0
   179     eval [list $lindex $x $x]
   180 } {0}
   181 
   182 test lindex-8.2 {data reuse} {
   183     set a 0
   184     eval [list $lindex $a $a $a]
   185 } 0
   186 test lindex-8.3 {data reuse} {
   187     set a 1
   188     eval [list $lindex $a $a $a]
   189 } {}
   190 
   191 test lindex-8.4 {data reuse} {
   192     set x [list 0 0]
   193     eval [list $lindex $x $x]
   194 } {0}
   195 
   196 test lindex-8.5 {data reuse} {
   197     set x 0
   198     eval [list $lindex $x [list $x $x]]
   199 } {0}
   200 
   201 test lindex-8.6 {data reuse} {
   202     set x [list 1 1]
   203     eval [list $lindex $x $x]
   204 } {}
   205 
   206 test lindex-8.7 {data reuse} {
   207     set x 1
   208     eval [list lindex $x [list $x $x]]
   209 } {}
   210 
   211 #----------------------------------------------------------------------
   212 
   213 # Compilation tests for lindex
   214 
   215 test lindex-9.1 {wrong # args} {
   216     list [catch {lindex} result] $result
   217 } "1 {wrong # args: should be \"lindex list ?index...?\"}"
   218 
   219 # Indices that are lists or convertible to lists
   220 
   221 test lindex-10.1 {empty index list} {
   222     set x {}
   223     catch {
   224 	list [lindex {a b c} $x] [lindex {a b c} $x]
   225     } result
   226     set result
   227 } {{a b c} {a b c}}
   228 
   229 test lindex-10.2 {singleton index list} {
   230     set x { 1 }
   231     catch {
   232 	list [lindex {a b c} $x] [lindex {a b c} $x]
   233     } result
   234     set result
   235 } {b b}
   236 
   237 test lindex-10.3 {multiple indices in list} {
   238     set x {1 2}
   239     catch {
   240 	list [lindex {{a b c} {d e f}} $x] [lindex {{a b c} {d e f}} $x]
   241     } result
   242     set result
   243 } {f f}
   244 
   245 test lindex-10.4 {malformed index list} {
   246     set x \{
   247     list [catch { lindex {a b c} $x } result] $result
   248 } {1 bad\ index\ \"\{\":\ must\ be\ integer\ or\ end?-integer?}
   249 
   250 # Indices that are integers or convertible to integers
   251 
   252 test lindex-11.1 {integer -1} {
   253     set x ${minus}1
   254     catch {
   255 	list [lindex {a b c} $x] [lindex {a b c} $x]
   256     } result
   257     set result
   258 } {{} {}}
   259 
   260 test lindex-11.2 {integer 0} {
   261     set x [string range 00 0 0]
   262     catch {
   263 	list [lindex {a b c} $x] [lindex {a b c} $x]
   264     } result
   265     set result
   266 } {a a}
   267 
   268 test lindex-11.3 {integer 2} {
   269     set x [string range 22 0 0]
   270     catch {
   271 	list [lindex {a b c} $x] [lindex {a b c} $x]
   272     } result
   273     set result
   274 } {c c}
   275 
   276 test lindex-11.4 {integer 3} {
   277     set x [string range 33 0 0]
   278     catch {
   279 	list [lindex {a b c} $x] [lindex {a b c} $x]
   280     } result
   281     set result
   282 } {{} {}}
   283 
   284 test lindex-11.5 {bad octal} {
   285     set x 08
   286     list [catch { lindex {a b c} $x } result] $result
   287 } "1 {bad index \"08\": must be integer or end?-integer? (looks like invalid octal number)}"
   288 
   289 test lindex-11.6 {bad octal} {
   290     set x -09
   291     list [catch { lindex {a b c} $x } result] $result
   292 } "1 {bad index \"-09\": must be integer or end?-integer? (looks like invalid octal number)}"
   293 
   294 # Indices relative to end
   295 
   296 test lindex-12.1 {index = end} {
   297     set x end
   298     catch {
   299 	list [lindex {a b c} $x] [lindex {a b c} $x]
   300     } result
   301     set result
   302 } {c c}
   303 
   304 test lindex-12.2 {index = end--1} {
   305     set x end--1
   306     catch {
   307 	list [lindex {a b c} $x] [lindex {a b c} $x]
   308     } result
   309     set result
   310 } {{} {}}
   311 
   312 test lindex-12.3 {index = end-0} {
   313     set x end-0
   314     catch {
   315 	list [lindex {a b c} $x] [lindex {a b c} $x]
   316     } result
   317     set result
   318 } {c c}
   319 
   320 test lindex-12.4 {index = end-2} {
   321     set x end-2
   322     catch {
   323 	list [lindex {a b c} $x] [lindex {a b c} $x]
   324     } result
   325     set result
   326 } {a a}
   327 
   328 test lindex-12.5 {index = end-3} {
   329     set x end-3
   330     catch {
   331 	list [lindex {a b c} $x] [lindex {a b c} $x]
   332     } result
   333     set result
   334 } {{} {}}
   335 
   336 test lindex-12.6 {bad octal} {
   337     set x end-08
   338     list [catch { lindex {a b c} $x } result] $result
   339 } "1 {bad index \"end-08\": must be integer or end?-integer? (looks like invalid octal number)}"
   340 
   341 test lindex-12.7 {bad octal} {
   342     set x end--09
   343     list [catch { lindex {a b c} $x } result] $result
   344 } "1 {bad index \"end--09\": must be integer or end?-integer?}"
   345 
   346 test lindex-12.8 {bad integer, not octal} {
   347     set x end-0a2
   348     list [catch { lindex {a b c} $x } result] $result
   349 } "1 {bad index \"end-0a2\": must be integer or end?-integer?}"
   350 
   351 test lindex-12.9 {incomplete end} {
   352     set x en
   353     catch {
   354 	list [lindex {a b c} $x] [lindex {a b c} $x]
   355     } result
   356     set result
   357 } {c c}
   358 
   359 test lindex-12.10 {incomplete end-} {
   360     set x end-
   361     list [catch { lindex {a b c} $x } result] $result
   362 } "1 {bad index \"end-\": must be integer or end?-integer?}"
   363 
   364 test lindex-13.1 {bad second index} {
   365     list [catch { lindex {a b c} 0 0a2 } result] $result
   366 } "1 {bad index \"0a2\": must be integer or end?-integer?}"
   367 
   368 test lindex-13.2 {good second index} {
   369     catch {
   370 	lindex {{a b c} {d e f} {g h i}} 1 2
   371     } result
   372     set result
   373 } f
   374 
   375 test lindex-13.3 {three indices} {
   376     catch {
   377 	lindex {{{a b} {c d}} {{e f} {g h}}} 1 0 1
   378     } result
   379     set result
   380 } f
   381 
   382 test lindex-14.1 {error conditions in parsing list} {
   383     list [catch { lindex "a \{" 2 } msg] $msg
   384 } {1 {unmatched open brace in list}}
   385 test lindex-14.2 {error conditions in parsing list} {
   386     list [catch { lindex {a {b c}d e} 2 } msg] $msg
   387 } {1 {list element in braces followed by "d" instead of space}}
   388 test lindex-14.3 {error conditions in parsing list} {
   389     list [catch { lindex {a "b c"def ghi} 2 } msg] $msg
   390 } {1 {list element in quotes followed by "def" instead of space}}
   391 
   392 test lindex-15.1 {quoted elements} {
   393     catch {
   394 	lindex {a "b c" d} 1
   395     } result
   396     set result
   397 } {b c}
   398 test lindex-15.2 {quoted elements} {
   399     catch {
   400 	lindex {"{}" b c} 0
   401     } result
   402     set result
   403 } {{}}
   404 test lindex-15.3 {quoted elements} {
   405     catch {
   406 	lindex {ab "c d \" x" y} 1
   407     } result
   408     set result
   409 } {c d " x}
   410 test lindex-15.4 {quoted elements} {
   411     catch {
   412 	lindex {a b {c d "e} {f g"}} 2
   413     } result
   414     set result
   415 } {c d "e}
   416 
   417 test lindex-16.1 {data reuse} {
   418     set x 0
   419     catch {
   420 	lindex $x $x
   421     } result
   422     set result
   423 } {0}
   424 
   425 test lindex-16.2 {data reuse} {
   426     set a 0
   427     catch {
   428 	lindex $a $a $a
   429     } result
   430     set result
   431 } 0
   432 test lindex-16.3 {data reuse} {
   433     set a 1
   434     catch {
   435 	lindex $a $a $a
   436     } result
   437     set result
   438 } {}
   439 
   440 test lindex-16.4 {data reuse} {
   441     set x [list 0 0]
   442     catch {
   443 	lindex $x $x
   444     } result
   445     set result
   446 } {0}
   447 
   448 test lindex-16.5 {data reuse} {
   449     set x 0
   450     catch {
   451 	lindex $x [list $x $x]
   452     } result
   453     set result
   454 } {0}
   455 
   456 test lindex-16.6 {data reuse} {
   457     set x [list 1 1]
   458     catch {
   459 	lindex $x $x
   460     } result
   461     set result
   462 } {}
   463 
   464 test lindex-16.7 {data reuse} {
   465     set x 1
   466     catch {
   467 	lindex $x [list $x $x]
   468     } result
   469     set result
   470 } {}
   471 
   472 catch { unset lindex}
   473 catch { unset minus }
   474 
   475 # cleanup
   476 ::tcltest::cleanupTests
   477 return