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