os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/compile.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
# This file contains tests for the files tclCompile.c, tclCompCmds.c
sl@0
     2
# and tclLiteral.c
sl@0
     3
#
sl@0
     4
# This file contains a collection of tests for one or more of the Tcl
sl@0
     5
# built-in commands.  Sourcing this file into Tcl runs the tests and
sl@0
     6
# generates output for errors.  No output means no errors were found.
sl@0
     7
#
sl@0
     8
# Copyright (c) 1997 by 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: compile.test,v 1.24.2.3 2004/10/26 20:14:36 dgp Exp $
sl@0
    15
sl@0
    16
package require tcltest 2
sl@0
    17
namespace import -force ::tcltest::*
sl@0
    18
sl@0
    19
# The following tests are very incomplete, although the rest of the
sl@0
    20
# test suite covers this file fairly well.
sl@0
    21
sl@0
    22
catch {rename p ""}
sl@0
    23
catch {namespace delete test_ns_compile}
sl@0
    24
catch {unset x}
sl@0
    25
catch {unset y}
sl@0
    26
catch {unset a}
sl@0
    27
sl@0
    28
test compile-1.1 {TclCompileString: look up cmds in proc ns, not current ns} {
sl@0
    29
    catch {namespace delete test_ns_compile}
sl@0
    30
    catch {unset x}
sl@0
    31
    set x 123
sl@0
    32
    namespace eval test_ns_compile {
sl@0
    33
        proc set {args} {
sl@0
    34
            global x
sl@0
    35
            lappend x test_ns_compile::set
sl@0
    36
        }
sl@0
    37
        proc p {} {
sl@0
    38
            set 0
sl@0
    39
        }
sl@0
    40
    }
sl@0
    41
    list [test_ns_compile::p] [set x]
sl@0
    42
} {{123 test_ns_compile::set} {123 test_ns_compile::set}}
sl@0
    43
test compile-1.2 {TclCompileString, error result is reset if TclGetLong determines word isn't an integer} {
sl@0
    44
    proc p {x} {info commands 3m}
sl@0
    45
    list [catch {p} msg] $msg
sl@0
    46
} {1 {wrong # args: should be "p x"}}
sl@0
    47
test compile-2.1 {TclCompileDollarVar: global scalar name with ::s} {
sl@0
    48
    catch {unset x}
sl@0
    49
    set x 123
sl@0
    50
    list $::x [expr {[lsearch -exact [info globals] x] != 0}]
sl@0
    51
} {123 1}
sl@0
    52
test compile-2.2 {TclCompileDollarVar: global scalar name with ::s} {
sl@0
    53
    catch {unset y}
sl@0
    54
    proc p {} {
sl@0
    55
        set ::y 789
sl@0
    56
        return $::y
sl@0
    57
    }
sl@0
    58
    list [p] $::y [expr {[lsearch -exact [info globals] y] != 0}]
sl@0
    59
} {789 789 1}
sl@0
    60
test compile-2.3 {TclCompileDollarVar: global array name with ::s} {
sl@0
    61
    catch {unset a}
sl@0
    62
    set ::a(1) 2
sl@0
    63
    list $::a(1) [set ::a($::a(1)) 3] $::a(2) [expr {[lsearch -exact [info globals] a] != 0}]
sl@0
    64
} {2 3 3 1}
sl@0
    65
test compile-2.4 {TclCompileDollarVar: global scalar name with ::s} {
sl@0
    66
    catch {unset a}
sl@0
    67
    proc p {} {
sl@0
    68
        set ::a(1) 1
sl@0
    69
        return $::a($::a(1))
sl@0
    70
    }
sl@0
    71
    list [p] $::a(1) [expr {[lsearch -exact [info globals] a] != 0}]
sl@0
    72
} {1 1 1}
sl@0
    73
test compile-2.5 {TclCompileDollarVar: global array, called as ${arrName(0)}} {
sl@0
    74
    catch {unset a}
sl@0
    75
    proc p {} {
sl@0
    76
	global a
sl@0
    77
        set a(1) 1
sl@0
    78
        return ${a(1)}$::a(1)$a(1)
sl@0
    79
    }
sl@0
    80
    list [p] $::a(1) [expr {[lsearch -exact [info globals] a] != 0}]
sl@0
    81
} {111 1 1}
sl@0
    82
sl@0
    83
test compile-3.1 {TclCompileCatchCmd: only catch cmds with scalar vars are compiled inline} {
sl@0
    84
    catch {unset a}
sl@0
    85
    set a(1) xyzzyx
sl@0
    86
    proc p {} {
sl@0
    87
        global a
sl@0
    88
        catch {set x 123} a(1)
sl@0
    89
    }
sl@0
    90
    list [p] $a(1)
sl@0
    91
} {0 123}
sl@0
    92
test compile-3.2 {TclCompileCatchCmd: non-local variables} {
sl@0
    93
    set ::foo 1
sl@0
    94
    proc catch-test {} {
sl@0
    95
	catch {set x 3} ::foo
sl@0
    96
    }
sl@0
    97
    catch-test
sl@0
    98
    set ::foo
sl@0
    99
} 3
sl@0
   100
test compile-3.3 {TclCompileCatchCmd: overagressive compiling [bug 219184]} {
sl@0
   101
    proc catch-test {str} {
sl@0
   102
	catch [eval $str GOOD]
sl@0
   103
	error BAD
sl@0
   104
    }
sl@0
   105
    catch {catch-test error} ::foo
sl@0
   106
    set ::foo
sl@0
   107
} {GOOD}
sl@0
   108
test compile-3.4 {TclCompileCatchCmd: bcc'ed [return] is caught} {
sl@0
   109
    proc foo {} {
sl@0
   110
	set fail [catch {
sl@0
   111
	    return 1
sl@0
   112
	}] ; # {}	
sl@0
   113
	return 2
sl@0
   114
    }
sl@0
   115
    foo
sl@0
   116
} {2}
sl@0
   117
sl@0
   118
test compile-3.5 {TclCompileCatchCmd: recover from error, [Bug 705406]} {
sl@0
   119
    proc foo {} {
sl@0
   120
	catch {
sl@0
   121
	    if {[a]} {
sl@0
   122
		if b {}
sl@0
   123
	    }   
sl@0
   124
	}   
sl@0
   125
    }
sl@0
   126
    list [catch foo msg] $msg
sl@0
   127
} {0 1}
sl@0
   128
sl@0
   129
test compile-4.1 {TclCompileForCmd: command substituted test expression} {
sl@0
   130
    set i 0
sl@0
   131
    set j 0
sl@0
   132
    # Should be "forever"
sl@0
   133
    for {} [expr $i < 3] {} {
sl@0
   134
	set j [incr i]
sl@0
   135
	if {$j > 3} break
sl@0
   136
    }
sl@0
   137
    set j
sl@0
   138
} {4}
sl@0
   139
sl@0
   140
test compile-5.1 {TclCompileForeachCmd: exception stack} {
sl@0
   141
    proc foreach-exception-test {} {
sl@0
   142
	foreach array(index) [list 1 2 3] break
sl@0
   143
	foreach array(index) [list 1 2 3] break
sl@0
   144
	foreach scalar [list 1 2 3] break
sl@0
   145
    }
sl@0
   146
    list [catch foreach-exception-test result] $result
sl@0
   147
} {0 {}}
sl@0
   148
test compile-5.2 {TclCompileForeachCmd: non-local variables} {
sl@0
   149
    set ::foo 1
sl@0
   150
    proc foreach-test {} {
sl@0
   151
	foreach ::foo {1 2 3} {}
sl@0
   152
    }
sl@0
   153
    foreach-test
sl@0
   154
    set ::foo
sl@0
   155
} 3
sl@0
   156
sl@0
   157
test compile-6.1 {TclCompileSetCmd: global scalar names with ::s} {
sl@0
   158
    catch {unset x}
sl@0
   159
    catch {unset y}
sl@0
   160
    set x 123
sl@0
   161
    proc p {} {
sl@0
   162
        set ::y 789
sl@0
   163
        return $::y
sl@0
   164
    }
sl@0
   165
    list $::x [expr {[lsearch -exact [info globals] x] != 0}] \
sl@0
   166
         [p] $::y [expr {[lsearch -exact [info globals] y] != 0}]
sl@0
   167
} {123 1 789 789 1}
sl@0
   168
test compile-6.2 {TclCompileSetCmd: global array names with ::s} {
sl@0
   169
    catch {unset a}
sl@0
   170
    set ::a(1) 2
sl@0
   171
    proc p {} {
sl@0
   172
        set ::a(1) 1
sl@0
   173
        return $::a($::a(1))
sl@0
   174
    }
sl@0
   175
    list $::a(1) [p] [set ::a($::a(1)) 3] $::a(1) [expr {[lsearch -exact [info globals] a] != 0}]
sl@0
   176
} {2 1 3 3 1}
sl@0
   177
test compile-6.3 {TclCompileSetCmd: namespace var names with ::s} {
sl@0
   178
    catch {namespace delete test_ns_compile}
sl@0
   179
    catch {unset x}
sl@0
   180
    namespace eval test_ns_compile {
sl@0
   181
        variable v hello
sl@0
   182
        variable arr
sl@0
   183
        set ::x $::test_ns_compile::v
sl@0
   184
	set ::test_ns_compile::arr(1) 123
sl@0
   185
    }
sl@0
   186
    list $::x $::test_ns_compile::arr(1)
sl@0
   187
} {hello 123}
sl@0
   188
sl@0
   189
test compile-7.1 {TclCompileWhileCmd: command substituted test expression} {
sl@0
   190
    set i 0
sl@0
   191
    set j 0
sl@0
   192
    # Should be "forever"
sl@0
   193
    while [expr $i < 3] {
sl@0
   194
	set j [incr i]
sl@0
   195
	if {$j > 3} break
sl@0
   196
    }
sl@0
   197
    set j
sl@0
   198
} {4}
sl@0
   199
sl@0
   200
test compile-8.1 {CollectArgInfo: binary data} {
sl@0
   201
    list [catch "string length \000foo" msg] $msg
sl@0
   202
} {0 4}
sl@0
   203
test compile-8.2 {CollectArgInfo: binary data} {
sl@0
   204
    list [catch "string length foo\000" msg] $msg
sl@0
   205
} {0 4}
sl@0
   206
test compile-8.3 {CollectArgInfo: handle "]" at end of command properly} {
sl@0
   207
    set x ]
sl@0
   208
} {]}
sl@0
   209
sl@0
   210
test compile-9.1 {UpdateStringOfByteCode: called for duplicate of compiled empty object} {
sl@0
   211
    proc p {} {
sl@0
   212
        set x {}
sl@0
   213
        eval $x
sl@0
   214
        append x { }
sl@0
   215
        eval $x
sl@0
   216
    }
sl@0
   217
    p
sl@0
   218
} {}
sl@0
   219
sl@0
   220
test compile-10.1 {BLACKBOX: exception stack overflow} {
sl@0
   221
    set x {{0}}
sl@0
   222
    set y 0
sl@0
   223
    while {$y < 100} {
sl@0
   224
	if !$x {incr y}
sl@0
   225
    }
sl@0
   226
} {}
sl@0
   227
sl@0
   228
test compile-11.1 {Tcl_Append*: ensure Tcl_ResetResult is used properly} {
sl@0
   229
    proc p {} {
sl@0
   230
	# shared object - Interp result && Var 'r'
sl@0
   231
	set r [list foobar]
sl@0
   232
	# command that will add error to result
sl@0
   233
	lindex a bogus
sl@0
   234
    }
sl@0
   235
    list [catch {p} msg] $msg
sl@0
   236
} {1 {bad index "bogus": must be integer or end?-integer?}}
sl@0
   237
test compile-11.2 {Tcl_Append*: ensure Tcl_ResetResult is used properly} {
sl@0
   238
    proc p {} { set r [list foobar] ; string index a bogus }
sl@0
   239
    list [catch {p} msg] $msg
sl@0
   240
} {1 {bad index "bogus": must be integer or end?-integer?}}
sl@0
   241
test compile-11.3 {Tcl_Append*: ensure Tcl_ResetResult is used properly} {
sl@0
   242
    proc p {} { set r [list foobar] ; string index a 09 }
sl@0
   243
    list [catch {p} msg] $msg
sl@0
   244
} {1 {bad index "09": must be integer or end?-integer? (looks like invalid octal number)}}
sl@0
   245
test compile-11.4 {Tcl_Append*: ensure Tcl_ResetResult is used properly} {
sl@0
   246
    proc p {} { set r [list foobar] ; array set var {one two many} }
sl@0
   247
    list [catch {p} msg] $msg
sl@0
   248
} {1 {list must have an even number of elements}}
sl@0
   249
test compile-11.5 {Tcl_Append*: ensure Tcl_ResetResult is used properly} {
sl@0
   250
    proc p {} { set r [list foobar] ; incr foo }
sl@0
   251
    list [catch {p} msg] $msg
sl@0
   252
} {1 {can't read "foo": no such variable}}
sl@0
   253
test compile-11.6 {Tcl_Append*: ensure Tcl_ResetResult is used properly} {
sl@0
   254
    proc p {} { set r [list foobar] ; incr foo bogus }
sl@0
   255
    list [catch {p} msg] $msg
sl@0
   256
} {1 {expected integer but got "bogus"}}
sl@0
   257
test compile-11.7 {Tcl_Append*: ensure Tcl_ResetResult is used properly} {
sl@0
   258
    proc p {} { set r [list foobar] ; expr !a }
sl@0
   259
    list [catch {p} msg] $msg
sl@0
   260
} {1 {syntax error in expression "!a": variable references require preceding $}}
sl@0
   261
test compile-11.8 {Tcl_Append*: ensure Tcl_ResetResult is used properly} {
sl@0
   262
    proc p {} { set r [list foobar] ; expr {!a} }
sl@0
   263
    list [catch {p} msg] $msg
sl@0
   264
} {1 {syntax error in expression "!a": variable references require preceding $}}
sl@0
   265
test compile-11.9 {Tcl_Append*: ensure Tcl_ResetResult is used properly} {
sl@0
   266
    proc p {} { set r [list foobar] ; llength "\{" }
sl@0
   267
    list [catch {p} msg] $msg
sl@0
   268
} {1 {unmatched open brace in list}}
sl@0
   269
sl@0
   270
# 
sl@0
   271
# Special section for tests of tclLiteral.c
sl@0
   272
# The following tests check for incorrect memory handling in
sl@0
   273
# TclReleaseLiteral. They are only effective when tcl is compiled 
sl@0
   274
# with TCL_MEM_DEBUG
sl@0
   275
#
sl@0
   276
# Special test for leak on interp delete [Bug 467523]. 
sl@0
   277
::tcltest::testConstraint exec [llength [info commands exec]]
sl@0
   278
::tcltest::testConstraint memDebug [llength [info commands memory]]
sl@0
   279
sl@0
   280
test compile-12.1 {testing literal leak on interp delete} {memDebug} {
sl@0
   281
    proc getbytes {} {
sl@0
   282
	set lines [split [memory info] "\n"]
sl@0
   283
	lindex [lindex $lines 3] 3
sl@0
   284
    }
sl@0
   285
    
sl@0
   286
    set end [getbytes]
sl@0
   287
    for {set i 0} {$i < 5} {incr i} {
sl@0
   288
	interp create foo 
sl@0
   289
	foo eval { 
sl@0
   290
	    namespace eval bar {}
sl@0
   291
	} 
sl@0
   292
	interp delete foo
sl@0
   293
	set tmp $end
sl@0
   294
	set end [getbytes]
sl@0
   295
    }    
sl@0
   296
    rename getbytes {}
sl@0
   297
    set leak [expr {$end - $tmp}]
sl@0
   298
} 0
sl@0
   299
# Special test for a memory error in a preliminary fix of [Bug 467523]. 
sl@0
   300
# It requires executing a helpfile.  Presumably the child process is
sl@0
   301
# used because when this test fails, it crashes.
sl@0
   302
test compile-12.2 {testing error on literal deletion} {memDebug exec} {
sl@0
   303
    makeFile {
sl@0
   304
	for {set i 0} {$i < 5} {incr i} {
sl@0
   305
	    namespace eval bar {}
sl@0
   306
	    namespace delete bar
sl@0
   307
	}
sl@0
   308
	puts 0
sl@0
   309
    } source.file
sl@0
   310
    set res [catch {
sl@0
   311
	exec [interpreter] source.file 
sl@0
   312
    }]
sl@0
   313
    catch {removeFile source.file}
sl@0
   314
    set res
sl@0
   315
} 0
sl@0
   316
# Test to catch buffer overrun in TclCompileTokens from buf 530320
sl@0
   317
test compile-12.3 {check for a buffer overrun} {
sl@0
   318
    proc crash {} {
sl@0
   319
	puts $array([expr {a+2}])
sl@0
   320
    }
sl@0
   321
    list [catch crash msg] $msg
sl@0
   322
} {1 {syntax error in expression "a+2": variable references require preceding $}}
sl@0
   323
sl@0
   324
test compile-12.4 {TclCleanupLiteralTable segfault} {
sl@0
   325
    # Tcl Bug 1001997
sl@0
   326
    # Here, we're trying to test a case that causes a crash in
sl@0
   327
    # TclCleanupLiteralTable.  The conditions that we're trying to
sl@0
   328
    # establish are:
sl@0
   329
    # - TclCleanupLiteralTable is attempting to clean up a bytecode
sl@0
   330
    #   object in the literal table.
sl@0
   331
    # - The bytecode object in question contains the only reference
sl@0
   332
    #   to another literal.
sl@0
   333
    # - The literal in question is in the same hash bucket as the bytecode
sl@0
   334
    #   object, and immediately follows it in the chain.
sl@0
   335
    # Since newly registered literals are added at the FRONT of the
sl@0
   336
    # bucket chains, and since the bytecode object is registered before
sl@0
   337
    # its literals, this is difficult to achieve.  What we do is:
sl@0
   338
    #  (a) do a [namespace eval] of a string that's calculated to
sl@0
   339
    #      hash into the same bucket as a literal that it contains.
sl@0
   340
    #      In this case, the script and the variable 'bugbug' 
sl@0
   341
    #      land in the same bucket.
sl@0
   342
    #  (b) do a [namespace eval] of a string that contains enough
sl@0
   343
    #      literals to force TclRegisterLiteral to rebuild the global
sl@0
   344
    #      literal table.  The newly created hash buckets will contain
sl@0
   345
    #      the literals, IN REVERSE ORDER, thus putting the bytecode
sl@0
   346
    #      immediately ahead of 'bugbug' and 'bug4345bug'.  The bytecode
sl@0
   347
    #      object will contain the only references to those two literals.
sl@0
   348
    #  (c) Delete the interpreter to invoke TclCleanupLiteralTable
sl@0
   349
    #      and tickle the bug.
sl@0
   350
    proc foo {} {
sl@0
   351
    set i [interp create]
sl@0
   352
    $i eval {
sl@0
   353
        namespace eval ::w {concat 4649; variable bugbug}
sl@0
   354
        namespace eval ::w {
sl@0
   355
            concat x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 \
sl@0
   356
                x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 \
sl@0
   357
                x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 \
sl@0
   358
                x31 x32 X33 X34 X35 X36 X37 X38 X39 X40 \
sl@0
   359
                x41 x42 x43 x44 x45 x46 x47 x48 x49 x50 \
sl@0
   360
                x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 \
sl@0
   361
                x61 x62 x63 x64
sl@0
   362
            concat y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 \
sl@0
   363
                y11 y12 y13 y14 y15 y16 y17 y18 y19 y20 \
sl@0
   364
                y21 y22 y23 y24 y25 y26 y27 y28 y29 y30 \
sl@0
   365
                y31 y32 Y33 Y34 Y35 Y36 Y37 Y38 Y39 Y40 \
sl@0
   366
                y41 y42 y43 y44 y45 y46 y47 y48 y49 y50 \
sl@0
   367
                y51 y52 y53 y54 y55 y56 y57 y58 y59 y60 \
sl@0
   368
                y61 y62 y63 y64
sl@0
   369
            concat z1 z2 z3 z4 z5 z6 z7 z8 z9 z10 \
sl@0
   370
                z11 z12 z13 z14 z15 z16 z17 z18 z19 z20 \
sl@0
   371
                z21 z22 z23 z24 z25 z26 z27 z28 z29 z30 \
sl@0
   372
                z31 z32
sl@0
   373
        }
sl@0
   374
    }
sl@0
   375
    interp delete $i; # must not crash
sl@0
   376
    return ok
sl@0
   377
    }
sl@0
   378
    foo
sl@0
   379
} ok
sl@0
   380
sl@0
   381
sl@0
   382
# Special test for underestimating the maxStackSize required for a
sl@0
   383
# compiled command. A failure will cause a segfault in the child 
sl@0
   384
# process.
sl@0
   385
test compile-13.1 {testing underestimate of maxStackSize in list cmd} {exec} {
sl@0
   386
    set body {set x [list}
sl@0
   387
    for {set i 0} {$i < 3000} {incr i} {
sl@0
   388
	append body " $i"
sl@0
   389
    }
sl@0
   390
    append body {]; puts OK}
sl@0
   391
    regsub BODY {proc crash {} {BODY}; crash} $body script
sl@0
   392
    list [catch {exec [interpreter] << $script} msg] $msg
sl@0
   393
} {0 OK}
sl@0
   394
sl@0
   395
# Special test for compiling tokens from a copy of the source
sl@0
   396
# string [Bug #599788]
sl@0
   397
test compile-14.1 {testing errors in element name; segfault?} {} {
sl@0
   398
     catch {set a([error])} msg1
sl@0
   399
     catch {set bubba([join $abba $jubba]) $vol} msg2
sl@0
   400
     list $msg1 $msg2
sl@0
   401
} {{wrong # args: should be "error message ?errorInfo? ?errorCode?"} {can't read "abba": no such variable}}
sl@0
   402
sl@0
   403
# Next 4 tests cover Tcl Bug 633204
sl@0
   404
test compile-15.1 {proper TCL_RETURN code from [return]} {
sl@0
   405
    proc p {} {catch return}
sl@0
   406
    set result [p]
sl@0
   407
    rename p {}
sl@0
   408
    set result
sl@0
   409
} 2
sl@0
   410
test compile-15.2 {proper TCL_RETURN code from [return]} {
sl@0
   411
    proc p {} {catch {return foo}}
sl@0
   412
    set result [p]
sl@0
   413
    rename p {}
sl@0
   414
    set result
sl@0
   415
} 2
sl@0
   416
test compile-15.3 {proper TCL_RETURN code from [return]} {
sl@0
   417
    proc p {} {catch {return $::tcl_library}}
sl@0
   418
    set result [p]
sl@0
   419
    rename p {}
sl@0
   420
    set result
sl@0
   421
} 2
sl@0
   422
test compile-15.4 {proper TCL_RETURN code from [return]} {
sl@0
   423
    proc p {} {catch {return [info library]}}
sl@0
   424
    set result [p]
sl@0
   425
    rename p {}
sl@0
   426
    set result
sl@0
   427
} 2
sl@0
   428
test compile-15.5 {proper TCL_RETURN code from [return]} {
sl@0
   429
    proc p {} {catch {set a 1}; return}
sl@0
   430
    set result [p]
sl@0
   431
    rename p {}
sl@0
   432
    set result
sl@0
   433
} ""
sl@0
   434
sl@0
   435
sl@0
   436
# cleanup
sl@0
   437
catch {rename p ""}
sl@0
   438
catch {namespace delete test_ns_compile}
sl@0
   439
catch {unset x}
sl@0
   440
catch {unset y}
sl@0
   441
catch {unset a}
sl@0
   442
::tcltest::cleanupTests
sl@0
   443
return