Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/append.test @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

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