lreplace: Implement TIP #505
[jimtcl.git] / tests / lreplace.test
blob32a21115f324377c1b253253209e4a6d42850c33
1 # Commands covered:  lreplace
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.
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.
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 source [file dirname [info script]]/testing.tcl
16 test lreplace-1.1 {lreplace command} {
17     lreplace {1 2 3 4 5} 0 0 a
18 } {a 2 3 4 5}
19 test lreplace-1.2 {lreplace command} {
20     lreplace {1 2 3 4 5} 1 1 a
21 } {1 a 3 4 5}
22 test lreplace-1.3 {lreplace command} {
23     lreplace {1 2 3 4 5} 2 2 a
24 } {1 2 a 4 5}
25 test lreplace-1.4 {lreplace command} {
26     lreplace {1 2 3 4 5} 3 3 a
27 } {1 2 3 a 5}
28 test lreplace-1.5 {lreplace command} {
29     lreplace {1 2 3 4 5} 4 4 a
30 } {1 2 3 4 a}
31 test lreplace-1.6 {lreplace command} {
32     lreplace {1 2 3 4 5} 4 5 a
33 } {1 2 3 4 a}
34 test lreplace-1.7 {lreplace command} {
35     lreplace {1 2 3 4 5} -1 -1 a
36 } {a 1 2 3 4 5}
37 test lreplace-1.8 {lreplace command} {
38     lreplace {1 2 3 4 5} 2 end a b c d
39 } {1 2 a b c d}
40 test lreplace-1.9 {lreplace command} {
41     lreplace {1 2 3 4 5} 0 3
42 } {5}
43 test lreplace-1.10 {lreplace command} {
44     lreplace {1 2 3 4 5} 0 4
45 } {}
46 test lreplace-1.11 {lreplace command} {
47     lreplace {1 2 3 4 5} 0 1
48 } {3 4 5}
49 test lreplace-1.12 {lreplace command} {
50     lreplace {1 2 3 4 5} 2 3
51 } {1 2 5}
52 test lreplace-1.13 {lreplace command} {
53     lreplace {1 2 3 4 5} 3 end
54 } {1 2 3}
55 test lreplace-1.14 {lreplace command} {
56     lreplace {1 2 3 4 5} -1 4 a b c
57 } {a b c}
58 test lreplace-1.15 {lreplace command} {
59     lreplace {a b "c c" d e f} 3 3
60 } {a b {c c} e f}
61 test lreplace-1.16 {lreplace command} {
62     lreplace { 1 2 3 4 5} 0 0 a
63 } {a 2 3 4 5}
64 test lreplace-1.17 {lreplace command} {
65     lreplace {1 2 3 4 "5 6"} 4 4 a
66 } {1 2 3 4 a}
67 test lreplace-1.18 {lreplace command} {
68     lreplace {1 2 3 4 {5 6}} 4 4 a
69 } {1 2 3 4 a}
70 test lreplace-1.19 {lreplace command} {
71     lreplace {1 2 3 4} 2 end x y z
72 } {1 2 x y z}
73 test lreplace-1.20 {lreplace command} {
74     lreplace {1 2 3 4} end end a
75 } {1 2 3 a}
76 test lreplace-1.21 {lreplace command} {
77     lreplace {1 2 3 4} end 3 a
78 } {1 2 3 a}
79 test lreplace-1.22 {lreplace command} {
80     lreplace {1 2 3 4} end end
81 } {1 2 3}
82 test lreplace-1.23 {lreplace command} {
83     lreplace {1 2 3 4} 2 -1 xy
84 } {1 2 xy 3 4}
85 test lreplace-1.24 {lreplace command} {
86     lreplace {1 2 3 4} end -1 z
87 } {1 2 3 z 4}
88 test lreplace-1.25 {lreplace command} {
89     concat \"[lreplace {\}\     hello} end end]\"
90 } {"\}\ "}
91 test lreplace-1.26 {lreplace command} {
92     catch {unset foo}
93     set foo {a b}
94     list [set foo [lreplace $foo end end]] \
95         [set foo [lreplace $foo end end]] \
96         [set foo [lreplace $foo end end]]
97 } {a {} {}}
98 test lreplace-1.27 {lreplace command} -body {
99     lreplace x 1 1
100 } -result x
101 test lreplace-1.28 {lreplace command} -body {
102     lreplace x 1 1 y
103 } -result {x y}
104 test lreplace-1.29 {lreplace command} -body {
105     lreplace x 1 1 [error foo]
106 } -returnCodes 1 -result {foo}
107 test lreplace-1.30 {lreplace command} -body {
108     lreplace {not {}alist} 0 0 [error foo]
109 } -returnCodes 1 -result {foo}
111 test lreplace-2.1 {lreplace errors} {
112     list [catch lreplace msg] $msg
113 } {1 {wrong # args: should be "lreplace list first last ?element ...?"}}
114 test lreplace-2.2 {lreplace errors} {
115     list [catch {lreplace a b} msg] $msg
116 } {1 {wrong # args: should be "lreplace list first last ?element ...?"}}
117 test lreplace-2.3 {lreplace errors} {
118     list [catch {lreplace x a 10} msg] $msg
119 } {1 {bad index "a": must be integer?[+-]integer? or end?[+-]integer?}}
120 test lreplace-2.4 {lreplace errors} {
121     list [catch {lreplace x 10 x} msg] $msg
122 } {1 {bad index "x": must be integer?[+-]integer? or end?[+-]integer?}}
123 test lreplace-2.5 {lreplace errors} {
124     list [catch {lreplace x 10 1x} msg] $msg
125 } {1 {bad index "1x": must be integer?[+-]integer? or end?[+-]integer?}}
126 test lreplace-2.6 {lreplace errors} {
127     list [catch {lreplace x 3 2} msg] $msg
128 } {0 x}
129 test lreplace-2.7 {lreplace errors} {
130     list [catch {lreplace x 1 1} msg] $msg
131 } {0 x}
133 test lreplace-3.1 {lreplace won't modify shared argument objects} {
134     proc p {} {
135         lreplace "a b c" 1 1 "x y"
136         return "a b c"
137     }
138     p
139 } "a b c"
141 test lreplace-4.1 {Bug ccc2c2cc98: lreplace edge case} {
142     lreplace {} 1 1
143 } {}
144 test lreplace-4.2 {Bug ccc2c2cc98: lreplace edge case} {
145     lreplace { } 1 1
146 } {}
147 test lreplace-4.3 {lreplace edge case} {
148     lreplace {1 2 3} 2 0
149 } {1 2 3}
150 test lreplace-4.4 {lreplace edge case} {
151     lreplace {1 2 3 4 5} 3 1
152 } {1 2 3 4 5}
153 test lreplace-4.5 {lreplace edge case} {
154     lreplace {1 2 3 4 5} 3 0 _
155 } {1 2 3 _ 4 5}
156 test lreplace-4.6 {lreplace end-x: bug a4cb3f06c4} {
157     lreplace {0 1 2 3 4} 0 end-2
158 } {3 4}
159 test lreplace-4.6.1 {lreplace end-x: bug a4cb3f06c4} {
160     lreplace {0 1 2 3 4} 0 end-2 a b c
161 } {a b c 3 4}
162 test lreplace-4.7 {lreplace with two end-indexes: increasing} {
163     lreplace {0 1 2 3 4} end-2 end-1
164 } {0 1 4}
165 test lreplace-4.7.1 {lreplace with two end-indexes: increasing} {
166     lreplace {0 1 2 3 4} end-2 end-1 a b c
167 } {0 1 a b c 4}
168 test lreplace-4.8 {lreplace with two end-indexes: equal} {
169     lreplace {0 1 2 3 4} end-2 end-2
170 } {0 1 3 4}
171 test lreplace-4.8.1 {lreplace with two end-indexes: equal} {
172     lreplace {0 1 2 3 4} end-2 end-2 a b c
173 } {0 1 a b c 3 4}
174 test lreplace-4.9 {lreplace with two end-indexes: decreasing} {
175     lreplace {0 1 2 3 4} end-2 end-3
176 } {0 1 2 3 4}
177 test lreplace-4.9.1 {lreplace with two end-indexes: decreasing} {
178     lreplace {0 1 2 3 4} end-2 end-3 a b c
179 } {0 1 a b c 2 3 4}
180 test lreplace-4.10 {lreplace with two equal indexes} {
181     lreplace {0 1 2 3 4} 2 2
182 } {0 1 3 4}
183 test lreplace-4.10.1 {lreplace with two equal indexes} {
184     lreplace {0 1 2 3 4} 2 2 a b c
185 } {0 1 a b c 3 4}
186 test lreplace-4.11 {lreplace end index first} {
187     lreplace {0 1 2 3 4} end-2 1 a b c
188 } {0 1 a b c 2 3 4}
189 test lreplace-4.12 {lreplace end index first} {
190     lreplace {0 1 2 3 4} end-2 2 a b c
191 } {0 1 a b c 3 4}
192 test lreplace-4.13 {lreplace empty list} {
193     lreplace {} 1 1 1
194 } 1
195 test lreplace-4.14 {lreplace empty list} {
196     lreplace {} 2 2 2
197 } 2
199 test lreplace-5.1 {compiled lreplace: Bug 47ac84309b} {
200     apply {x {
201         lreplace $x end 0
202     }} {a b c}
203 } {a b c}
204 test lreplace-5.2 {compiled lreplace: Bug 47ac84309b} {
205     apply {x {
206         lreplace $x end 0 A
207     }} {a b c}
208 } {a b A c}
210 # cleanup
211 catch {unset foo}
212 ::tcltest::cleanupTests
213 return