os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/upvar.test
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 # Commands covered:  upvar
     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: upvar.test,v 1.7 2000/04/10 17:19:05 ericm Exp $
    15 
    16 if {[lsearch [namespace children] ::tcltest] == -1} {
    17     package require tcltest
    18     namespace import -force ::tcltest::*
    19 }
    20 
    21 test upvar-1.1 {reading variables with upvar} {
    22     proc p1 {a b} {set c 22; set d 33; p2}
    23     proc p2 {} {upvar a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
    24     p1 foo bar
    25 } {foo bar 22 33 abc}
    26 test upvar-1.2 {reading variables with upvar} {
    27     proc p1 {a b} {set c 22; set d 33; p2}
    28     proc p2 {} {p3}
    29     proc p3 {} {upvar 2 a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
    30     p1 foo bar
    31 } {foo bar 22 33 abc}
    32 test upvar-1.3 {reading variables with upvar} {
    33     proc p1 {a b} {set c 22; set d 33; p2}
    34     proc p2 {} {p3}
    35     proc p3 {} {
    36 	upvar #1 a x1 b x2 c x3 d x4
    37 	set a abc
    38 	list $x1 $x2 $x3 $x4 $a
    39     }
    40     p1 foo bar
    41 } {foo bar 22 33 abc}
    42 test upvar-1.4 {reading variables with upvar} {
    43     set x1 44
    44     set x2 55
    45     proc p1 {} {p2}
    46     proc p2 {} {
    47 	upvar 2 x1 x1 x2 a
    48 	upvar #0 x1 b
    49 	set c $b
    50 	incr b 3
    51 	list $x1 $a $b
    52     }
    53     p1
    54 } {47 55 47}
    55 test upvar-1.5 {reading array elements with upvar} {
    56     proc p1 {} {set a(0) zeroth; set a(1) first; p2}
    57     proc p2 {} {upvar a(0) x; set x}
    58     p1
    59 } {zeroth}
    60 
    61 test upvar-2.1 {writing variables with upvar} {
    62     proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
    63     proc p2 {} {
    64 	upvar a x1 b x2 c x3 d x4
    65 	set x1 14
    66 	set x4 88
    67     }
    68     p1 foo bar
    69 } {14 bar 22 88}
    70 test upvar-2.2 {writing variables with upvar} {
    71     set x1 44
    72     set x2 55
    73     proc p1 {x1 x2} {
    74 	upvar #0 x1 a
    75 	upvar x2 b
    76 	set a $x1
    77 	set b $x2
    78     }
    79     p1 newbits morebits
    80     list $x1 $x2
    81 } {newbits morebits}
    82 test upvar-2.3 {writing variables with upvar} {
    83     catch {unset x1}
    84     catch {unset x2}
    85     proc p1 {x1 x2} {
    86 	upvar #0 x1 a
    87 	upvar x2 b
    88 	set a $x1
    89 	set b $x2
    90     }
    91     p1 newbits morebits
    92     list [catch {set x1} msg] $msg [catch {set x2} msg] $msg
    93 } {0 newbits 0 morebits}
    94 test upvar-2.4 {writing array elements with upvar} {
    95     proc p1 {} {set a(0) zeroth; set a(1) first; list [p2] $a(0)}
    96     proc p2 {} {upvar a(0) x; set x xyzzy}
    97     p1
    98 } {xyzzy xyzzy}
    99 
   100 test upvar-3.1 {unsetting variables with upvar} {
   101     proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
   102     proc p2 {} {
   103 	upvar 1 a x1 d x2
   104 	unset x1 x2
   105     }
   106     p1 foo bar
   107 } {b c}
   108 test upvar-3.2 {unsetting variables with upvar} {
   109     proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
   110     proc p2 {} {
   111 	upvar 1 a x1 d x2
   112 	unset x1 x2
   113 	set x2 28
   114     }
   115     p1 foo bar
   116 } {b c d}
   117 test upvar-3.3 {unsetting variables with upvar} {
   118     set x1 44
   119     set x2 55
   120     proc p1 {} {p2}
   121     proc p2 {} {
   122 	upvar 2 x1 a
   123 	upvar #0 x2 b
   124 	unset a b
   125     }
   126     p1
   127     list [info exists x1] [info exists x2]
   128 } {0 0}
   129 test upvar-3.4 {unsetting variables with upvar} {
   130     set x1 44
   131     set x2 55
   132     proc p1 {} {
   133 	upvar x1 a x2 b
   134 	unset a b
   135 	set b 118
   136     }
   137     p1
   138     list [info exists x1] [catch {set x2} msg] $msg
   139 } {0 0 118}
   140 test upvar-3.5 {unsetting array elements with upvar} {
   141     proc p1 {} {
   142 	set a(0) zeroth
   143 	set a(1) first
   144 	set a(2) second
   145 	p2
   146 	array names a
   147     }
   148     proc p2 {} {upvar a(0) x; unset x}
   149     p1
   150 } {1 2}
   151 test upvar-3.6 {unsetting then resetting array elements with upvar} {
   152     proc p1 {} {
   153 	set a(0) zeroth
   154 	set a(1) first
   155 	set a(2) second
   156 	p2
   157 	list [array names a] [catch {set a(0)} msg] $msg
   158     }
   159     proc p2 {} {upvar a(0) x; unset x; set x 12345}
   160     p1
   161 } {{0 1 2} 0 12345}
   162 
   163 test upvar-4.1 {nested upvars} {
   164     set x1 88
   165     proc p1 {a b} {set c 22; set d 33; p2}
   166     proc p2 {} {global x1; upvar c x2; p3}
   167     proc p3 {} {
   168 	upvar x1 a x2 b
   169 	list $a $b
   170     }
   171     p1 14 15
   172 } {88 22}
   173 test upvar-4.2 {nested upvars} {
   174     set x1 88
   175     proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
   176     proc p2 {} {global x1; upvar c x2; p3}
   177     proc p3 {} {
   178 	upvar x1 a x2 b
   179 	set a foo
   180 	set b bar
   181     }
   182     list [p1 14 15] $x1
   183 } {{14 15 bar 33} foo}
   184 
   185 proc tproc {args} {global x; set x [list $args [uplevel info vars]]}
   186 test upvar-5.1 {traces involving upvars} {
   187     proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2}
   188     proc p2 {} {upvar c x1; set x1 22}
   189     set x ---
   190     p1 foo bar
   191     set x
   192 } {{x1 {} w} x1}
   193 test upvar-5.2 {traces involving upvars} {
   194     proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2}
   195     proc p2 {} {upvar c x1; set x1}
   196     set x ---
   197     p1 foo bar
   198     set x
   199 } {{x1 {} r} x1}
   200 test upvar-5.3 {traces involving upvars} {
   201     proc p1 {a b} {set c 22; set d 33; trace var c rwu tproc; p2}
   202     proc p2 {} {upvar c x1; unset x1}
   203     set x ---
   204     p1 foo bar
   205     set x
   206 } {{x1 {} u} x1}
   207 
   208 test upvar-6.1 {retargeting an upvar} {
   209     proc p1 {} {
   210 	set a(0) zeroth
   211 	set a(1) first
   212 	set a(2) second
   213 	p2
   214     }
   215     proc p2 {} {
   216 	upvar a x
   217 	set result {}
   218 	foreach i [array names x] {
   219 	    upvar a($i) x
   220 	    lappend result $x
   221 	}
   222 	lsort $result
   223     }
   224     p1
   225 } {first second zeroth}
   226 test upvar-6.2 {retargeting an upvar} {
   227     set x 44
   228     set y abcde
   229     proc p1 {} {
   230 	global x
   231 	set result $x
   232 	upvar y x
   233 	lappend result $x
   234     }
   235     p1
   236 } {44 abcde}
   237 test upvar-6.3 {retargeting an upvar} {
   238     set x 44
   239     set y abcde
   240     proc p1 {} {
   241 	upvar y x
   242 	lappend result $x
   243 	global x
   244 	lappend result $x
   245     }
   246     p1
   247 } {abcde 44}
   248 
   249 test upvar-7.1 {upvar to same level} {
   250     set x 44
   251     set y 55
   252     catch {unset uv}
   253     upvar #0 x uv
   254     set uv abc
   255     upvar 0 y uv
   256     set uv xyzzy
   257     list $x $y
   258 } {abc xyzzy}
   259 test upvar-7.2 {upvar to same level} {
   260     set x 1234
   261     set y 4567
   262     proc p1 {x y} {
   263 	upvar 0 x uv
   264 	set uv $y
   265 	return "$x $y"
   266     }
   267     p1 44 89
   268 } {89 89}
   269 test upvar-7.3 {upvar to same level} {
   270     set x 1234
   271     set y 4567
   272     proc p1 {x y} {
   273 	upvar #1 x uv
   274 	set uv $y
   275 	return "$x $y"
   276     }
   277     p1 xyz abc
   278 } {abc abc}
   279 test upvar-7.4 {upvar to same level: tricky problems when deleting variable table} {
   280     proc tt {} {upvar #1 toto loc;  return $loc}
   281     list [catch tt msg] $msg
   282 } {1 {can't read "loc": no such variable}}
   283 test upvar-7.5 {potential memory leak when deleting variable table} {
   284     proc leak {} {
   285 	array set foo {1 2 3 4}
   286 	upvar 0 foo(1) bar
   287     }
   288     leak
   289 } {}
   290 
   291 test upvar-8.1 {errors in upvar command} {
   292     list [catch upvar msg] $msg
   293 } {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
   294 test upvar-8.2 {errors in upvar command} {
   295     list [catch {upvar 1} msg] $msg
   296 } {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
   297 test upvar-8.3 {errors in upvar command} {
   298     proc p1 {} {upvar a b c}
   299     list [catch p1 msg] $msg
   300 } {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
   301 test upvar-8.4 {errors in upvar command} {
   302     proc p1 {} {upvar 0 b b}
   303     list [catch p1 msg] $msg
   304 } {1 {can't upvar from variable to itself}}
   305 test upvar-8.5 {errors in upvar command} {
   306     proc p1 {} {upvar 0 a b; upvar 0 b a}
   307     list [catch p1 msg] $msg
   308 } {1 {can't upvar from variable to itself}}
   309 test upvar-8.6 {errors in upvar command} {
   310     proc p1 {} {set a 33; upvar b a}
   311     list [catch p1 msg] $msg
   312 } {1 {variable "a" already exists}}
   313 test upvar-8.7 {errors in upvar command} {
   314     proc p1 {} {trace variable a w foo; upvar b a}
   315     list [catch p1 msg] $msg
   316 } {1 {variable "a" has traces: can't use for upvar}}
   317 test upvar-8.8 {create nested array with upvar} {
   318     proc p1 {} {upvar x(a) b; set b(2) 44}
   319     catch {unset x}
   320     list [catch p1 msg] $msg
   321 } {1 {can't set "b(2)": variable isn't array}}
   322 test upvar-8.9 {upvar won't create namespace variable that refers to procedure variable} {
   323     catch {eval namespace delete [namespace children :: test_ns_*]}
   324     catch {rename MakeLink ""}
   325     namespace eval ::test_ns_1 {}
   326     proc MakeLink {a} {
   327         namespace eval ::test_ns_1 {
   328 	    upvar a a
   329         }
   330         unset ::test_ns_1::a
   331     }
   332     list [catch {MakeLink 1} msg] $msg
   333 } {1 {bad variable name "a": upvar won't create namespace variable that refers to procedure variable}}
   334 test upvar-8.10 {upvar will create element alias for new array element} {
   335     catch {unset upvarArray}
   336     array set upvarArray {}
   337     catch {upvar 0 upvarArray(elem) upvarArrayElemAlias}
   338 } {0}
   339 
   340 if {[info commands testupvar] != {}} {
   341     test upvar-9.1 {Tcl_UpVar2 procedure} {
   342 	list [catch {testupvar xyz a {} x global} msg] $msg
   343     } {1 {bad level "xyz"}}
   344     test upvar-9.2 {Tcl_UpVar2 procedure} {
   345 	catch {unset a}
   346 	catch {unset x}
   347 	set a 44
   348 	list [catch {testupvar #0 a 1 x global} msg] $msg
   349     } {1 {can't access "a(1)": variable isn't array}}
   350     test upvar-9.3 {Tcl_UpVar2 procedure} {
   351 	proc foo {} {
   352 	    testupvar 1 a {} x local
   353 	    set x
   354 	}
   355 	catch {unset a}
   356 	catch {unset x}
   357 	set a 44
   358 	foo
   359     } {44}
   360     test upvar-9.4 {Tcl_UpVar2 procedure} {
   361 	proc foo {} {
   362 	    testupvar 1 a {} _up_ global
   363 	    list [catch {set x} msg] $msg
   364 	}
   365 	catch {unset a}
   366 	catch {unset _up_}
   367 	set a 44
   368 	concat [foo] $_up_
   369     } {1 {can't read "x": no such variable} 44}
   370     test upvar-9.5 {Tcl_UpVar2 procedure} {
   371 	proc foo {} {
   372 	    testupvar 1 a b x local
   373 	    set x
   374 	}
   375 	catch {unset a}
   376 	catch {unset x}
   377 	set a(b) 1234
   378 	foo
   379     } {1234}
   380     test upvar-9.6 {Tcl_UpVar procedure} {
   381 	proc foo {} {
   382 	    testupvar 1 a x local
   383 	    set x
   384 	}
   385 	catch {unset a}
   386 	catch {unset x}
   387 	set a xyzzy
   388 	foo
   389     } {xyzzy}
   390     test upvar-9.7 {Tcl_UpVar procedure} {
   391 	proc foo {} {
   392 	    testupvar #0 a(b) x local
   393 	    set x
   394 	}
   395 	catch {unset a}
   396 	catch {unset x}
   397 	set a(b) 1234
   398 	foo
   399     } {1234}
   400 }
   401 catch {unset a}
   402 
   403 # cleanup
   404 ::tcltest::cleanupTests
   405 return
   406 
   407 
   408 
   409 
   410 
   411 
   412 
   413 
   414 
   415 
   416 
   417