os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/lset.test
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 # This file is a -*- tcl -*- test script
     2 
     3 # Commands covered: lset
     4 #
     5 # This file contains a collection of tests for one or more of the Tcl
     6 # built-in commands.  Sourcing this file into Tcl runs the tests and
     7 # generates output for errors.  No output means no errors were found.
     8 #
     9 # Copyright (c) 2001 by Kevin B. Kenny.  All rights reserved.
    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$
    15 
    16 if {[lsearch [namespace children] ::tcltest] == -1} {
    17     package require tcltest
    18     namespace import -force ::tcltest::*
    19 }
    20 
    21 proc failTrace {name1 name2 op} {
    22     error "trace failed"
    23 }
    24 
    25 set lset lset
    26 
    27 set noRead {}
    28 trace add variable noRead read failTrace
    29 set noWrite {a b c}
    30 trace add variable noWrite write failTrace
    31 
    32 test lset-1.1 {lset, not compiled, arg count} {
    33     list [catch {eval $lset} msg] $msg
    34 } "1 {wrong \# args: should be \"lset listVar index ?index...? value\"}"
    35 
    36 test lset-1.2 {lset, not compiled, no such var} {
    37     list [catch {eval [list $lset noSuchVar 0 {}]} msg] $msg
    38 } "1 {can't read \"noSuchVar\": no such variable}"
    39 
    40 test lset-1.3 {lset, not compiled, var not readable} {
    41     list [catch {eval [list $lset noRead 0 {}]} msg] $msg
    42 } "1 {can't read \"noRead\": trace failed}"
    43 
    44 test lset-2.1 {lset, not compiled, 3 args, second arg a plain index} {
    45     set x {0 1 2}
    46     list [eval [list $lset x 0 3]] $x
    47 } {{3 1 2} {3 1 2}}
    48 
    49 test lset-2.2 {lset, not compiled, 3 args, second arg neither index nor list} {
    50     set x {0 1 2}
    51     list [catch {
    52 	eval [list $lset x {{bad}1} 3]
    53     } msg] $msg
    54 } "1 {bad index \"{bad}1\": must be integer or end?-integer?}"
    55 
    56 test lset-3.1 {lset, not compiled, 3 args, data duplicated} {
    57     set x {0 1 2}
    58     list [eval [list $lset x 0 $x]] $x
    59 } {{{0 1 2} 1 2} {{0 1 2} 1 2}}
    60 
    61 test lset-3.2 {lset, not compiled, 3 args, data duplicated} {
    62     set x {0 1}
    63     set y $x
    64     list [eval [list $lset x 0 2]] $x $y
    65 } {{2 1} {2 1} {0 1}}
    66 
    67 test lset-3.3 {lset, not compiled, 3 args, data duplicated} {
    68     set x {0 1}
    69     set y $x
    70     list [eval [list $lset x 0 $x]] $x $y
    71 } {{{0 1} 1} {{0 1} 1} {0 1}}
    72 
    73 test lset-3.4 {lset, not compiled, 3 args, data duplicated} {
    74     set x {0 1 2}
    75     list [eval [list $lset x [list 0] $x]] $x
    76 } {{{0 1 2} 1 2} {{0 1 2} 1 2}}
    77 
    78 test lset-3.5 {lset, not compiled, 3 args, data duplicated} {
    79     set x {0 1}
    80     set y $x
    81     list [eval [list $lset x [list 0] 2]] $x $y
    82 } {{2 1} {2 1} {0 1}}
    83 
    84 test lset-3.6 {lset, not compiled, 3 args, data duplicated} {
    85     set x {0 1}
    86     set y $x
    87     list [eval [list $lset x [list 0] $x]] $x $y
    88 } {{{0 1} 1} {{0 1} 1} {0 1}}
    89 
    90 test lset-4.1 {lset, not compiled, 3 args, not a list} {
    91     set a "x \{"
    92     list [catch {
    93 	eval [list $lset a [list 0] y]
    94     } msg] $msg
    95 } {1 {unmatched open brace in list}}
    96 
    97 test lset-4.2 {lset, not compiled, 3 args, bad index} {
    98     set a {x y z}
    99     list [catch {
   100 	eval [list $lset a [list 2a2] w]
   101     } msg] $msg
   102 } {1 {bad index "2a2": must be integer or end?-integer?}}
   103 
   104 test lset-4.3 {lset, not compiled, 3 args, index out of range} {
   105     set a {x y z}
   106     list [catch {
   107 	eval [list $lset a [list -1] w]
   108     } msg] $msg
   109 } {1 {list index out of range}}
   110 
   111 test lset-4.4 {lset, not compiled, 3 args, index out of range} {
   112     set a {x y z}
   113     list [catch {
   114 	eval [list $lset a [list 3] w]
   115     } msg] $msg
   116 } {1 {list index out of range}}
   117 
   118 test lset-4.5 {lset, not compiled, 3 args, index out of range} {
   119     set a {x y z}
   120     list [catch {
   121 	eval [list $lset a [list end--1] w]
   122     } msg] $msg
   123 } {1 {list index out of range}}
   124 
   125 test lset-4.6 {lset, not compiled, 3 args, index out of range} {
   126     set a {x y z}
   127     list [catch {
   128 	eval [list $lset a [list end-3] w]
   129     } msg] $msg
   130 } {1 {list index out of range}}
   131 
   132 test lset-4.7 {lset, not compiled, 3 args, not a list} {
   133     set a "x \{"
   134     list [catch {
   135 	eval [list $lset a 0 y]
   136     } msg] $msg
   137 } {1 {unmatched open brace in list}}
   138 
   139 test lset-4.8 {lset, not compiled, 3 args, bad index} {
   140     set a {x y z}
   141     list [catch {
   142 	eval [list $lset a 2a2 w]
   143     } msg] $msg
   144 } {1 {bad index "2a2": must be integer or end?-integer?}}
   145 
   146 test lset-4.9 {lset, not compiled, 3 args, index out of range} {
   147     set a {x y z}
   148     list [catch {
   149 	eval [list $lset a -1 w]
   150     } msg] $msg
   151 } {1 {list index out of range}}
   152 
   153 test lset-4.10 {lset, not compiled, 3 args, index out of range} {
   154     set a {x y z}
   155     list [catch {
   156 	eval [list $lset a 3 w]
   157     } msg] $msg
   158 } {1 {list index out of range}}
   159 
   160 test lset-4.11 {lset, not compiled, 3 args, index out of range} {
   161     set a {x y z}
   162     list [catch {
   163 	eval [list $lset a end--1 w]
   164     } msg] $msg
   165 } {1 {list index out of range}}
   166 
   167 test lset-4.12 {lset, not compiled, 3 args, index out of range} {
   168     set a {x y z}
   169     list [catch {
   170 	eval [list $lset a end-3 w]
   171     } msg] $msg
   172 } {1 {list index out of range}}
   173 
   174 test lset-5.1 {lset, not compiled, 3 args, can't set variable} {
   175     list [catch {
   176 	eval [list $lset noWrite 0 d]
   177     } msg] $msg $noWrite
   178 } {1 {can't set "noWrite": trace failed} {d b c}}
   179 
   180 test lset-5.2 {lset, not compiled, 3 args, can't set variable} {
   181     list [catch {
   182 	eval [list $lset noWrite [list 0] d]
   183     } msg] $msg $noWrite
   184 } {1 {can't set "noWrite": trace failed} {d b c}}
   185 
   186 test lset-6.1 {lset, not compiled, 3 args, 1-d list basics} {
   187     set a {x y z}
   188     list [eval [list $lset a 0 a]] $a
   189 } {{a y z} {a y z}}
   190 
   191 test lset-6.2 {lset, not compiled, 3 args, 1-d list basics} {
   192     set a {x y z}
   193     list [eval [list $lset a [list 0] a]] $a
   194 } {{a y z} {a y z}}
   195 
   196 test lset-6.3 {lset, not compiled, 1-d list basics} {
   197     set a {x y z}
   198     list [eval [list $lset a 2 a]] $a
   199 } {{x y a} {x y a}}
   200 
   201 test lset-6.4 {lset, not compiled, 1-d list basics} {
   202     set a {x y z}
   203     list [eval [list $lset a [list 2] a]] $a
   204 } {{x y a} {x y a}}
   205 
   206 test lset-6.5 {lset, not compiled, 1-d list basics} {
   207     set a {x y z}
   208     list [eval [list $lset a end a]] $a
   209 } {{x y a} {x y a}}
   210 
   211 test lset-6.6 {lset, not compiled, 1-d list basics} {
   212     set a {x y z}
   213     list [eval [list $lset a [list end] a]] $a
   214 } {{x y a} {x y a}}
   215 
   216 test lset-6.7 {lset, not compiled, 1-d list basics} {
   217     set a {x y z}
   218     list [eval [list $lset a end-0 a]] $a
   219 } {{x y a} {x y a}}
   220 
   221 test lset-6.8 {lset, not compiled, 1-d list basics} {
   222     set a {x y z}
   223     list [eval [list $lset a [list end-0] a]] $a
   224 } {{x y a} {x y a}}
   225 
   226 test lset-6.9 {lset, not compiled, 1-d list basics} {
   227     set a {x y z}
   228     list [eval [list $lset a end-2 a]] $a
   229 } {{a y z} {a y z}}
   230 
   231 test lset-6.10 {lset, not compiled, 1-d list basics} {
   232     set a {x y z}
   233     list [eval [list $lset a [list end-2] a]] $a
   234 } {{a y z} {a y z}}
   235 
   236 test lset-7.1 {lset, not compiled, data sharing} {
   237     set a 0
   238     list [eval [list $lset a $a {gag me}]] $a
   239 } {{{gag me}} {{gag me}}}
   240 
   241 test lset-7.2 {lset, not compiled, data sharing} {
   242     set a [list 0]
   243     list [eval [list $lset a $a {gag me}]] $a
   244 } {{{gag me}} {{gag me}}}
   245 
   246 test lset-7.3 {lset, not compiled, data sharing} {
   247     set a {x y}
   248     list [eval [list $lset a 0 $a]] $a
   249 } {{{x y} y} {{x y} y}}
   250 
   251 test lset-7.4 {lset, not compiled, data sharing} {
   252     set a {x y}
   253     list [eval [list $lset a [list 0] $a]] $a
   254 } {{{x y} y} {{x y} y}}
   255 
   256 test lset-7.5 {lset, not compiled, data sharing} {
   257     set n 0
   258     set a {x y}
   259     list [eval [list $lset a $n $n]] $a $n
   260 } {{0 y} {0 y} 0}
   261 
   262 test lset-7.6 {lset, not compiled, data sharing} {
   263     set n [list 0]
   264     set a {x y}
   265     list [eval [list $lset a $n $n]] $a $n
   266 } {{0 y} {0 y} 0}
   267 
   268 test lset-7.7 {lset, not compiled, data sharing} {
   269     set n 0
   270     set a [list $n $n]
   271     list [eval [list $lset a $n 1]] $a $n
   272 } {{1 0} {1 0} 0}
   273 
   274 test lset-7.8 {lset, not compiled, data sharing} {
   275     set n [list 0]
   276     set a [list $n $n]
   277     list [eval [list $lset a $n 1]] $a $n
   278 } {{1 0} {1 0} 0}
   279 
   280 test lset-7.9 {lset, not compiled, data sharing} {
   281     set a 0
   282     list [eval [list $lset a $a $a]] $a
   283 } {0 0}
   284 
   285 test lset-7.10 {lset, not compiled, data sharing} {
   286     set a [list 0]
   287     list [eval [list $lset a $a $a]] $a
   288 } {0 0}
   289 
   290 test lset-8.1 {lset, not compiled, malformed sublist} {
   291     set a [list "a \{" b]
   292     list [catch {eval [list $lset a 0 1 c]} msg] $msg
   293 } {1 {unmatched open brace in list}}
   294 
   295 test lset-8.2 {lset, not compiled, malformed sublist} {
   296     set a [list "a \{" b]
   297     list [catch {eval [list $lset a {0 1} c]} msg] $msg
   298 } {1 {unmatched open brace in list}}
   299 
   300 test lset-8.3 {lset, not compiled, bad second index} {
   301     set a {{b c} {d e}}
   302     list [catch {eval [list $lset a 0 2a2 f]} msg] $msg
   303 } {1 {bad index "2a2": must be integer or end?-integer?}}
   304 
   305 test lset-8.4 {lset, not compiled, bad second index} {
   306     set a {{b c} {d e}}
   307     list [catch {eval [list $lset a {0 2a2} f]} msg] $msg
   308 } {1 {bad index "2a2": must be integer or end?-integer?}}
   309 
   310 test lset-8.5 {lset, not compiled, second index out of range} {
   311     set a {{b c} {d e} {f g}}
   312     list [catch {eval [list $lset a 2 -1 h]} msg] $msg
   313 } {1 {list index out of range}}
   314 
   315 test lset-8.6 {lset, not compiled, second index out of range} {
   316     set a {{b c} {d e} {f g}}
   317     list [catch {eval [list $lset a {2 -1} h]} msg] $msg
   318 } {1 {list index out of range}}
   319 
   320 test lset-8.7 {lset, not compiled, second index out of range} {
   321     set a {{b c} {d e} {f g}}
   322     list [catch {eval [list $lset a 2 2 h]} msg] $msg
   323 } {1 {list index out of range}}
   324 
   325 test lset-8.8 {lset, not compiled, second index out of range} {
   326     set a {{b c} {d e} {f g}}
   327     list [catch {eval [list $lset a {2 2} h]} msg] $msg
   328 } {1 {list index out of range}}
   329 
   330 test lset-8.9 {lset, not compiled, second index out of range} {
   331     set a {{b c} {d e} {f g}}
   332     list [catch {eval [list $lset a 2 end--1 h]} msg] $msg
   333 } {1 {list index out of range}}
   334 
   335 test lset-8.10 {lset, not compiled, second index out of range} {
   336     set a {{b c} {d e} {f g}}
   337     list [catch {eval [list $lset a {2 end--1} h]} msg] $msg
   338 } {1 {list index out of range}}
   339 
   340 test lset-8.11 {lset, not compiled, second index out of range} {
   341     set a {{b c} {d e} {f g}}
   342     list [catch {eval [list $lset a 2 end-2 h]} msg] $msg
   343 } {1 {list index out of range}}
   344 
   345 test lset-8.12 {lset, not compiled, second index out of range} {
   346     set a {{b c} {d e} {f g}}
   347     list [catch {eval [list $lset a {2 end-2} h]} msg] $msg
   348 } {1 {list index out of range}}
   349 
   350 test lset-9.1 {lset, not compiled, entire variable} {
   351     set a x
   352     list [eval [list $lset a y]] $a
   353 } {y y}
   354 
   355 test lset-9.2 {lset, not compiled, entire variable} {
   356     set a x
   357     list [eval [list $lset a {} y]] $a
   358 } {y y}
   359 
   360 test lset-10.1 {lset, not compiled, shared data} {
   361     set row {p q}
   362     set a [list $row $row]
   363     list [eval [list $lset a 0 0 x]] $a
   364 } {{{x q} {p q}} {{x q} {p q}}}
   365 
   366 test lset-10.2 {lset, not compiled, shared data} {
   367     set row {p q}
   368     set a [list $row $row]
   369     list [eval [list $lset a {0 0} x]] $a
   370 } {{{x q} {p q}} {{x q} {p q}}}
   371 
   372 test lset-11.1 {lset, not compiled, 2-d basics} {
   373     set a {{b c} {d e}}
   374     list [eval [list $lset a 0 0 f]] $a
   375 } {{{f c} {d e}} {{f c} {d e}}}
   376 
   377 test lset-11.2 {lset, not compiled, 2-d basics} {
   378     set a {{b c} {d e}}
   379     list [eval [list $lset a {0 0} f]] $a
   380 } {{{f c} {d e}} {{f c} {d e}}}
   381 
   382 test lset-11.3 {lset, not compiled, 2-d basics} {
   383     set a {{b c} {d e}}
   384     list [eval [list $lset a 0 1 f]] $a
   385 } {{{b f} {d e}} {{b f} {d e}}}
   386 
   387 test lset-11.4 {lset, not compiled, 2-d basics} {
   388     set a {{b c} {d e}}
   389     list [eval [list $lset a {0 1} f]] $a
   390 } {{{b f} {d e}} {{b f} {d e}}}
   391 
   392 test lset-11.5 {lset, not compiled, 2-d basics} {
   393     set a {{b c} {d e}}
   394     list [eval [list $lset a 1 0 f]] $a
   395 } {{{b c} {f e}} {{b c} {f e}}}
   396 
   397 test lset-11.6 {lset, not compiled, 2-d basics} {
   398     set a {{b c} {d e}}
   399     list [eval [list $lset a {1 0} f]] $a
   400 } {{{b c} {f e}} {{b c} {f e}}}
   401 
   402 test lset-11.7 {lset, not compiled, 2-d basics} {
   403     set a {{b c} {d e}}
   404     list [eval [list $lset a 1 1 f]] $a
   405 } {{{b c} {d f}} {{b c} {d f}}}
   406 
   407 test lset-11.8 {lset, not compiled, 2-d basics} {
   408     set a {{b c} {d e}}
   409     list [eval [list $lset a {1 1} f]] $a
   410 } {{{b c} {d f}} {{b c} {d f}}}
   411 
   412 test lset-12.0 {lset, not compiled, typical sharing pattern} {
   413     set zero 0
   414     set row [list $zero $zero $zero $zero]
   415     set ident [list $row $row $row $row]
   416     for { set i 0 } { $i < 4 } { incr i } {
   417 	eval [list $lset ident $i $i 1]
   418     }
   419     set ident
   420 } {{1 0 0 0} {0 1 0 0} {0 0 1 0} {0 0 0 1}}
   421 
   422 test lset-13.0 {lset, not compiled, shimmering hell} {
   423     set a 0
   424     list [eval [list $lset a $a $a $a $a {gag me}]] $a
   425 } {{{{{{gag me}}}}} {{{{{gag me}}}}}}
   426 
   427 test lset-13.1 {lset, not compiled, shimmering hell} {
   428     set a [list 0]
   429     list [eval [list $lset a $a $a $a $a {gag me}]] $a
   430 } {{{{{{gag me}}}}} {{{{{gag me}}}}}}
   431 
   432 test lset-13.2 {lset, not compiled, shimmering hell} {
   433     set a [list 0 0 0 0]
   434     list [eval [list $lset a $a {gag me}]] $a
   435 } {{{{{{gag me}}}} 0 0 0} {{{{{gag me}}}} 0 0 0}}
   436 
   437 test lset-14.1 {lset, not compiled, list args, is string rep preserved?} {
   438     set a { { 1 2 } { 3 4 } }
   439     catch { eval [list $lset a {1 5} 5] }
   440     list $a [lindex $a 1]
   441 } "{ { 1 2 } { 3 4 } } { 3 4 }"
   442 
   443 test lset-14.2 {lset, not compiled, flat args, is string rep preserved?} {
   444     set a { { 1 2 } { 3 4 } }
   445     catch { eval [list $lset a 1 5 5] }
   446     list $a [lindex $a 1]
   447 } "{ { 1 2 } { 3 4 } } { 3 4 }"
   448 
   449 catch {unset noRead}
   450 catch {unset noWrite}
   451 catch {rename failTrace {}}
   452 catch {unset ::x}
   453 catch {unset ::y}
   454 
   455 # cleanup
   456 ::tcltest::cleanupTests
   457 return