os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/append.test
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
# Commands covered:  append lappend
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-1996 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: append.test,v 1.7.12.1 2006/10/05 11:44:04 msofer 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
catch {unset x}
sl@0
    21
sl@0
    22
test append-1.1 {append command} {
sl@0
    23
    catch {unset x}
sl@0
    24
    list [append x 1 2 abc "long string"] $x
sl@0
    25
} {{12abclong string} {12abclong string}}
sl@0
    26
test append-1.2 {append command} {
sl@0
    27
    set x ""
sl@0
    28
    list [append x first] [append x second] [append x third] $x
sl@0
    29
} {first firstsecond firstsecondthird firstsecondthird}
sl@0
    30
test append-1.3 {append command} {
sl@0
    31
    set x "abcd"
sl@0
    32
    append x
sl@0
    33
} abcd
sl@0
    34
sl@0
    35
test append-2.1 {long appends} {
sl@0
    36
    set x ""
sl@0
    37
    for {set i 0} {$i < 1000} {set i [expr $i+1]} {
sl@0
    38
	append x "foobar "
sl@0
    39
    }
sl@0
    40
    set y "foobar"
sl@0
    41
    set y "$y $y $y $y $y $y $y $y $y $y"
sl@0
    42
    set y "$y $y $y $y $y $y $y $y $y $y"
sl@0
    43
    set y "$y $y $y $y $y $y $y $y $y $y "
sl@0
    44
    expr {$x == $y}
sl@0
    45
} 1
sl@0
    46
sl@0
    47
test append-3.1 {append errors} {
sl@0
    48
    list [catch {append} msg] $msg
sl@0
    49
} {1 {wrong # args: should be "append varName ?value value ...?"}}
sl@0
    50
test append-3.2 {append errors} {
sl@0
    51
    set x ""
sl@0
    52
    list [catch {append x(0) 44} msg] $msg
sl@0
    53
} {1 {can't set "x(0)": variable isn't array}}
sl@0
    54
test append-3.3 {append errors} {
sl@0
    55
    catch {unset x}
sl@0
    56
    list [catch {append x} msg] $msg
sl@0
    57
} {1 {can't read "x": no such variable}}
sl@0
    58
sl@0
    59
test append-4.1 {lappend command} {
sl@0
    60
    catch {unset x}
sl@0
    61
    list [lappend x 1 2 abc "long string"] $x
sl@0
    62
} {{1 2 abc {long string}} {1 2 abc {long string}}}
sl@0
    63
test append-4.2 {lappend command} {
sl@0
    64
    set x ""
sl@0
    65
    list [lappend x first] [lappend x second] [lappend x third] $x
sl@0
    66
} {first {first second} {first second third} {first second third}}
sl@0
    67
test append-4.3 {lappend command} {
sl@0
    68
    proc foo {} {
sl@0
    69
	global x
sl@0
    70
	set x old
sl@0
    71
	unset x
sl@0
    72
	lappend x new
sl@0
    73
    }
sl@0
    74
    set result [foo]
sl@0
    75
    rename foo {}
sl@0
    76
    set result
sl@0
    77
} {new}
sl@0
    78
test append-4.4 {lappend command} {
sl@0
    79
    set x {}
sl@0
    80
    lappend x \{\  abc
sl@0
    81
} {\{\  abc}
sl@0
    82
test append-4.5 {lappend command} {
sl@0
    83
    set x {}
sl@0
    84
    lappend x \{ abc
sl@0
    85
} {\{ abc}
sl@0
    86
test append-4.6 {lappend command} {
sl@0
    87
    set x {1 2 3}
sl@0
    88
    lappend x
sl@0
    89
} {1 2 3}
sl@0
    90
test append-4.7 {lappend command} {
sl@0
    91
    set x "a\{"
sl@0
    92
    lappend x abc
sl@0
    93
} "a\\\{ abc"
sl@0
    94
test append-4.8 {lappend command} {
sl@0
    95
    set x "\\\{"
sl@0
    96
    lappend x abc
sl@0
    97
} "\\{ abc"
sl@0
    98
test append-4.9 {lappend command} {
sl@0
    99
    set x " \{"
sl@0
   100
    list [catch {lappend x abc} msg] $msg
sl@0
   101
} {1 {unmatched open brace in list}}
sl@0
   102
test append-4.10 {lappend command} {
sl@0
   103
    set x "	\{"
sl@0
   104
    list [catch {lappend x abc} msg] $msg
sl@0
   105
} {1 {unmatched open brace in list}}
sl@0
   106
test append-4.11 {lappend command} {
sl@0
   107
    set x "\{\{\{"
sl@0
   108
    list [catch {lappend x abc} msg] $msg
sl@0
   109
} {1 {unmatched open brace in list}}
sl@0
   110
test append-4.12 {lappend command} {
sl@0
   111
    set x "x \{\{\{"
sl@0
   112
    list [catch {lappend x abc} msg] $msg
sl@0
   113
} {1 {unmatched open brace in list}}
sl@0
   114
test append-4.13 {lappend command} {
sl@0
   115
    set x "x\{\{\{"
sl@0
   116
    lappend x abc
sl@0
   117
} "x\\\{\\\{\\\{ abc"
sl@0
   118
test append-4.14 {lappend command} {
sl@0
   119
    set x " "
sl@0
   120
    lappend x abc
sl@0
   121
} "abc"
sl@0
   122
test append-4.15 {lappend command} {
sl@0
   123
    set x "\\ "
sl@0
   124
    lappend x abc
sl@0
   125
} "{ } abc"
sl@0
   126
test append-4.16 {lappend command} {
sl@0
   127
    set x "x "
sl@0
   128
    lappend x abc
sl@0
   129
} "x abc"
sl@0
   130
test append-4.17 {lappend command} {
sl@0
   131
    catch {unset x}
sl@0
   132
    lappend x
sl@0
   133
} {}
sl@0
   134
test append-4.18 {lappend command} {
sl@0
   135
    catch {unset x}
sl@0
   136
    lappend x {}
sl@0
   137
} {{}}
sl@0
   138
test append-4.19 {lappend command} {
sl@0
   139
    catch {unset x}
sl@0
   140
    lappend x(0)
sl@0
   141
} {}
sl@0
   142
test append-4.20 {lappend command} {
sl@0
   143
    catch {unset x}
sl@0
   144
    lappend x(0) abc
sl@0
   145
} {abc}
sl@0
   146
unset x
sl@0
   147
test append-4.21 {lappend command} {
sl@0
   148
    set x \"
sl@0
   149
    list [catch {lappend x} msg] $msg
sl@0
   150
} {1 {unmatched open quote in list}}
sl@0
   151
test append-4.22 {lappend command} {
sl@0
   152
    set x \"
sl@0
   153
    list [catch {lappend x abc} msg] $msg
sl@0
   154
} {1 {unmatched open quote in list}}
sl@0
   155
sl@0
   156
proc check {var size} {
sl@0
   157
    set l [llength $var]
sl@0
   158
    if {$l != $size} {
sl@0
   159
	return "length mismatch: should have been $size, was $l"
sl@0
   160
    }
sl@0
   161
    for {set i 0} {$i < $size} {set i [expr $i+1]} {
sl@0
   162
	set j [lindex $var $i]
sl@0
   163
	if {$j != "item $i"} {
sl@0
   164
	    return "element $i should have been \"item $i\", was \"$j\""
sl@0
   165
	}
sl@0
   166
    }
sl@0
   167
    return ok
sl@0
   168
}
sl@0
   169
test append-5.1 {long lappends} {
sl@0
   170
    catch {unset x}
sl@0
   171
    set x ""
sl@0
   172
    for {set i 0} {$i < 300} {set i [expr $i+1]} {
sl@0
   173
	lappend x "item $i"
sl@0
   174
    }
sl@0
   175
    check $x 300
sl@0
   176
} ok
sl@0
   177
sl@0
   178
test append-6.1 {lappend errors} {
sl@0
   179
    list [catch {lappend} msg] $msg
sl@0
   180
} {1 {wrong # args: should be "lappend varName ?value value ...?"}}
sl@0
   181
test append-6.2 {lappend errors} {
sl@0
   182
    set x ""
sl@0
   183
    list [catch {lappend x(0) 44} msg] $msg
sl@0
   184
} {1 {can't set "x(0)": variable isn't array}}
sl@0
   185
sl@0
   186
test append-7.1 {lappend-created var and error in trace on that var} {
sl@0
   187
    catch {rename foo ""}
sl@0
   188
    catch {unset x}
sl@0
   189
    trace variable x w foo
sl@0
   190
    proc foo {} {global x; unset x}
sl@0
   191
    catch {lappend x 1}
sl@0
   192
    proc foo {args} {global x; unset x}
sl@0
   193
    info exists x
sl@0
   194
    set x
sl@0
   195
    lappend x 1
sl@0
   196
    list [info exists x] [catch {set x} msg] $msg
sl@0
   197
} {0 1 {can't read "x": no such variable}}
sl@0
   198
test append-7.2 {lappend var triggers read trace} {
sl@0
   199
    catch {unset myvar}
sl@0
   200
    catch {unset ::result}
sl@0
   201
    trace variable myvar r foo
sl@0
   202
    proc foo {args} {append ::result $args}
sl@0
   203
    lappend myvar a
sl@0
   204
    list [catch {set ::result} msg] $msg
sl@0
   205
} {0 {myvar {} r}}
sl@0
   206
test append-7.3 {lappend var triggers read trace, array var} {
sl@0
   207
    # The behavior of read triggers on lappend changed in 8.0 to
sl@0
   208
    # not trigger them, and was changed back in 8.4.
sl@0
   209
    catch {unset myvar}
sl@0
   210
    catch {unset ::result}
sl@0
   211
    trace variable myvar r foo
sl@0
   212
    proc foo {args} {append ::result $args}
sl@0
   213
    lappend myvar(b) a
sl@0
   214
    list [catch {set ::result} msg] $msg
sl@0
   215
} {0 {myvar b r}}
sl@0
   216
test append-7.4 {lappend var triggers read trace, array var exists} {
sl@0
   217
    catch {unset myvar}
sl@0
   218
    catch {unset ::result}
sl@0
   219
    set myvar(0) 1
sl@0
   220
    trace variable myvar r foo
sl@0
   221
    proc foo {args} {append ::result $args}
sl@0
   222
    lappend myvar(b) a
sl@0
   223
    list [catch {set ::result} msg] $msg
sl@0
   224
} {0 {myvar b r}}
sl@0
   225
test append-7.5 {append var does not trigger read trace} {
sl@0
   226
    catch {unset myvar}
sl@0
   227
    catch {unset ::result}
sl@0
   228
    trace variable myvar r foo
sl@0
   229
    proc foo {args} {append ::result $args}
sl@0
   230
    append myvar a
sl@0
   231
    info exists ::result
sl@0
   232
} {0}
sl@0
   233
sl@0
   234
sl@0
   235
catch {unset i x result y}
sl@0
   236
catch {rename foo ""}
sl@0
   237
catch {rename check ""}
sl@0
   238
sl@0
   239
# cleanup
sl@0
   240
::tcltest::cleanupTests
sl@0
   241
return