1 | # Commands covered: lindex |
---|
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 Sun Microsystems, Inc. |
---|
9 | # Copyright (c) 1998-1999 by Scriptics Corporation. |
---|
10 | # Copyright (c) 2001 by Kevin B. Kenny. All rights reserved. |
---|
11 | # |
---|
12 | # See the file "license.terms" for information on usage and redistribution |
---|
13 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | # |
---|
15 | # RCS: @(#) $Id: lindex.test,v 1.17 2007/12/13 15:26:06 dgp Exp $ |
---|
16 | |
---|
17 | if {[lsearch [namespace children] ::tcltest] == -1} { |
---|
18 | package require tcltest 2.2 |
---|
19 | namespace import -force ::tcltest::* |
---|
20 | } |
---|
21 | |
---|
22 | set minus - |
---|
23 | testConstraint testevalex [llength [info commands testevalex]] |
---|
24 | |
---|
25 | # Tests of Tcl_LindexObjCmd, NOT COMPILED |
---|
26 | |
---|
27 | test lindex-1.1 {wrong # args} testevalex { |
---|
28 | list [catch {testevalex lindex} result] $result |
---|
29 | } "1 {wrong # args: should be \"lindex list ?index...?\"}" |
---|
30 | |
---|
31 | # Indices that are lists or convertible to lists |
---|
32 | |
---|
33 | test lindex-2.1 {empty index list} testevalex { |
---|
34 | set x {} |
---|
35 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
36 | } {{a b c} {a b c}} |
---|
37 | test lindex-2.2 {singleton index list} testevalex { |
---|
38 | set x { 1 } |
---|
39 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
40 | } {b b} |
---|
41 | test lindex-2.3 {multiple indices in list} testevalex { |
---|
42 | set x {1 2} |
---|
43 | list [testevalex {lindex {{a b c} {d e f}} $x}] \ |
---|
44 | [testevalex {lindex {{a b c} {d e f}} $x}] |
---|
45 | } {f f} |
---|
46 | test lindex-2.4 {malformed index list} testevalex { |
---|
47 | set x \{ |
---|
48 | list [catch { testevalex {lindex {a b c} $x} } result] $result |
---|
49 | } {1 bad\ index\ \"\{\":\ must\ be\ integer?\[+-\]integer?\ or\ end?\[+-\]integer?} |
---|
50 | |
---|
51 | # Indices that are integers or convertible to integers |
---|
52 | |
---|
53 | test lindex-3.1 {integer -1} testevalex { |
---|
54 | set x ${minus}1 |
---|
55 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
56 | } {{} {}} |
---|
57 | test lindex-3.2 {integer 0} testevalex { |
---|
58 | set x [string range 00 0 0] |
---|
59 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
60 | } {a a} |
---|
61 | test lindex-3.3 {integer 2} testevalex { |
---|
62 | set x [string range 22 0 0] |
---|
63 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
64 | } {c c} |
---|
65 | test lindex-3.4 {integer 3} testevalex { |
---|
66 | set x [string range 33 0 0] |
---|
67 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
68 | } {{} {}} |
---|
69 | test lindex-3.5 {bad octal} -constraints testevalex -body { |
---|
70 | set x 0o8 |
---|
71 | list [catch { testevalex {lindex {a b c} $x} } result] $result |
---|
72 | } -match glob -result {1 {*invalid octal number*}} |
---|
73 | test lindex-3.6 {bad octal} -constraints testevalex -body { |
---|
74 | set x -0o9 |
---|
75 | list [catch { testevalex {lindex {a b c} $x} } result] $result |
---|
76 | } -match glob -result {1 {*invalid octal number*}} |
---|
77 | test lindex-3.7 {indexes don't shimmer wide ints} { |
---|
78 | set x [expr {(wide(1)<<31) - 2}] |
---|
79 | list $x [lindex {1 2 3} $x] [incr x] [incr x] |
---|
80 | } {2147483646 {} 2147483647 2147483648} |
---|
81 | |
---|
82 | # Indices relative to end |
---|
83 | |
---|
84 | test lindex-4.1 {index = end} testevalex { |
---|
85 | set x end |
---|
86 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
87 | } {c c} |
---|
88 | test lindex-4.2 {index = end--1} testevalex { |
---|
89 | set x end--1 |
---|
90 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
91 | } {{} {}} |
---|
92 | test lindex-4.3 {index = end-0} testevalex { |
---|
93 | set x end-0 |
---|
94 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
95 | } {c c} |
---|
96 | test lindex-4.4 {index = end-2} testevalex { |
---|
97 | set x end-2 |
---|
98 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
99 | } {a a} |
---|
100 | test lindex-4.5 {index = end-3} testevalex { |
---|
101 | set x end-3 |
---|
102 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
103 | } {{} {}} |
---|
104 | test lindex-4.6 {bad octal} -constraints testevalex -body { |
---|
105 | set x end-0o8 |
---|
106 | list [catch { testevalex {lindex {a b c} $x} } result] $result |
---|
107 | } -match glob -result {1 {*invalid octal number*}} |
---|
108 | test lindex-4.7 {bad octal} -constraints testevalex -body { |
---|
109 | set x end--0o9 |
---|
110 | list [catch { testevalex {lindex {a b c} $x} } result] $result |
---|
111 | } -match glob -result {1 {*invalid octal number*}} |
---|
112 | test lindex-4.8 {bad integer, not octal} testevalex { |
---|
113 | set x end-0a2 |
---|
114 | list [catch { testevalex {lindex {a b c} $x} } result] $result |
---|
115 | } {1 {bad index "end-0a2": must be integer?[+-]integer? or end?[+-]integer?}} |
---|
116 | test lindex-4.9 {obsolete test} testevalex { |
---|
117 | set x end |
---|
118 | list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}] |
---|
119 | } {c c} |
---|
120 | test lindex-4.10 {incomplete end-} testevalex { |
---|
121 | set x end- |
---|
122 | list [catch { testevalex {lindex {a b c} $x} } result] $result |
---|
123 | } {1 {bad index "end-": must be integer?[+-]integer? or end?[+-]integer?}} |
---|
124 | |
---|
125 | test lindex-5.1 {bad second index} testevalex { |
---|
126 | list [catch { testevalex {lindex {a b c} 0 0a2} } result] $result |
---|
127 | } {1 {bad index "0a2": must be integer?[+-]integer? or end?[+-]integer?}} |
---|
128 | test lindex-5.2 {good second index} testevalex { |
---|
129 | testevalex {lindex {{a b c} {d e f} {g h i}} 1 2} |
---|
130 | } f |
---|
131 | test lindex-5.3 {three indices} testevalex { |
---|
132 | testevalex {lindex {{{a b} {c d}} {{e f} {g h}}} 1 0 1} |
---|
133 | } f |
---|
134 | |
---|
135 | test lindex-6.1 {error conditions in parsing list} testevalex { |
---|
136 | list [catch {testevalex {lindex "a \{" 2}} msg] $msg |
---|
137 | } {1 {unmatched open brace in list}} |
---|
138 | test lindex-6.2 {error conditions in parsing list} testevalex { |
---|
139 | list [catch {testevalex {lindex {a {b c}d e} 2}} msg] $msg |
---|
140 | } {1 {list element in braces followed by "d" instead of space}} |
---|
141 | test lindex-6.3 {error conditions in parsing list} testevalex { |
---|
142 | list [catch {testevalex {lindex {a "b c"def ghi} 2}} msg] $msg |
---|
143 | } {1 {list element in quotes followed by "def" instead of space}} |
---|
144 | |
---|
145 | test lindex-7.1 {quoted elements} testevalex { |
---|
146 | testevalex {lindex {a "b c" d} 1} |
---|
147 | } {b c} |
---|
148 | test lindex-7.2 {quoted elements} testevalex { |
---|
149 | testevalex {lindex {"{}" b c} 0} |
---|
150 | } {{}} |
---|
151 | test lindex-7.3 {quoted elements} testevalex { |
---|
152 | testevalex {lindex {ab "c d \" x" y} 1} |
---|
153 | } {c d " x} |
---|
154 | test lindex-7.4 {quoted elements} { |
---|
155 | lindex {a b {c d "e} {f g"}} 2 |
---|
156 | } {c d "e} |
---|
157 | |
---|
158 | test lindex-8.1 {data reuse} testevalex { |
---|
159 | set x 0 |
---|
160 | testevalex {lindex $x $x} |
---|
161 | } {0} |
---|
162 | test lindex-8.2 {data reuse} testevalex { |
---|
163 | set a 0 |
---|
164 | testevalex {lindex $a $a $a} |
---|
165 | } 0 |
---|
166 | test lindex-8.3 {data reuse} testevalex { |
---|
167 | set a 1 |
---|
168 | testevalex {lindex $a $a $a} |
---|
169 | } {} |
---|
170 | test lindex-8.4 {data reuse} testevalex { |
---|
171 | set x [list 0 0] |
---|
172 | testevalex {lindex $x $x} |
---|
173 | } {0} |
---|
174 | test lindex-8.5 {data reuse} testevalex { |
---|
175 | set x 0 |
---|
176 | testevalex {lindex $x [list $x $x]} |
---|
177 | } {0} |
---|
178 | test lindex-8.6 {data reuse} testevalex { |
---|
179 | set x [list 1 1] |
---|
180 | testevalex {lindex $x $x} |
---|
181 | } {} |
---|
182 | test lindex-8.7 {data reuse} testevalex { |
---|
183 | set x 1 |
---|
184 | testevalex {lindex $x [list $x $x]} |
---|
185 | } {} |
---|
186 | |
---|
187 | #---------------------------------------------------------------------- |
---|
188 | |
---|
189 | # Compilation tests for lindex |
---|
190 | |
---|
191 | test lindex-9.1 {wrong # args} { |
---|
192 | list [catch {lindex} result] $result |
---|
193 | } "1 {wrong # args: should be \"lindex list ?index...?\"}" |
---|
194 | test lindex-9.2 {ensure that compilation works in the right order} { |
---|
195 | proc foo {} { |
---|
196 | rename foo {} |
---|
197 | lindex 1 0 |
---|
198 | } |
---|
199 | foo |
---|
200 | } 1 |
---|
201 | |
---|
202 | # Indices that are lists or convertible to lists |
---|
203 | |
---|
204 | test lindex-10.1 {empty index list} { |
---|
205 | set x {} |
---|
206 | catch { |
---|
207 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
208 | } result |
---|
209 | set result |
---|
210 | } {{a b c} {a b c}} |
---|
211 | test lindex-10.2 {singleton index list} { |
---|
212 | set x { 1 } |
---|
213 | catch { |
---|
214 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
215 | } result |
---|
216 | set result |
---|
217 | } {b b} |
---|
218 | test lindex-10.3 {multiple indices in list} { |
---|
219 | set x {1 2} |
---|
220 | catch { |
---|
221 | list [lindex {{a b c} {d e f}} $x] [lindex {{a b c} {d e f}} $x] |
---|
222 | } result |
---|
223 | set result |
---|
224 | } {f f} |
---|
225 | test lindex-10.4 {malformed index list} { |
---|
226 | set x \{ |
---|
227 | list [catch { lindex {a b c} $x } result] $result |
---|
228 | } {1 bad\ index\ \"\{\":\ must\ be\ integer?\[+-\]integer?\ or\ end?\[+-\]integer?} |
---|
229 | |
---|
230 | # Indices that are integers or convertible to integers |
---|
231 | |
---|
232 | test lindex-11.1 {integer -1} { |
---|
233 | set x ${minus}1 |
---|
234 | catch { |
---|
235 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
236 | } result |
---|
237 | set result |
---|
238 | } {{} {}} |
---|
239 | test lindex-11.2 {integer 0} { |
---|
240 | set x [string range 00 0 0] |
---|
241 | catch { |
---|
242 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
243 | } result |
---|
244 | set result |
---|
245 | } {a a} |
---|
246 | test lindex-11.3 {integer 2} { |
---|
247 | set x [string range 22 0 0] |
---|
248 | catch { |
---|
249 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
250 | } result |
---|
251 | set result |
---|
252 | } {c c} |
---|
253 | test lindex-11.4 {integer 3} { |
---|
254 | set x [string range 33 0 0] |
---|
255 | catch { |
---|
256 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
257 | } result |
---|
258 | set result |
---|
259 | } {{} {}} |
---|
260 | test lindex-11.5 {bad octal} -body { |
---|
261 | set x 0o8 |
---|
262 | list [catch { lindex {a b c} $x } result] $result |
---|
263 | } -match glob -result {1 {*invalid octal number*}} |
---|
264 | test lindex-11.6 {bad octal} -body { |
---|
265 | set x -0o9 |
---|
266 | list [catch { lindex {a b c} $x } result] $result |
---|
267 | } -match glob -result {1 {*invalid octal number*}} |
---|
268 | |
---|
269 | # Indices relative to end |
---|
270 | |
---|
271 | test lindex-12.1 {index = end} { |
---|
272 | set x end |
---|
273 | catch { |
---|
274 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
275 | } result |
---|
276 | set result |
---|
277 | } {c c} |
---|
278 | test lindex-12.2 {index = end--1} { |
---|
279 | set x end--1 |
---|
280 | catch { |
---|
281 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
282 | } result |
---|
283 | set result |
---|
284 | } {{} {}} |
---|
285 | test lindex-12.3 {index = end-0} { |
---|
286 | set x end-0 |
---|
287 | catch { |
---|
288 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
289 | } result |
---|
290 | set result |
---|
291 | } {c c} |
---|
292 | test lindex-12.4 {index = end-2} { |
---|
293 | set x end-2 |
---|
294 | catch { |
---|
295 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
296 | } result |
---|
297 | set result |
---|
298 | } {a a} |
---|
299 | test lindex-12.5 {index = end-3} { |
---|
300 | set x end-3 |
---|
301 | catch { |
---|
302 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
303 | } result |
---|
304 | set result |
---|
305 | } {{} {}} |
---|
306 | test lindex-12.6 {bad octal} -body { |
---|
307 | set x end-0o8 |
---|
308 | list [catch { lindex {a b c} $x } result] $result |
---|
309 | } -match glob -result {1 {*invalid octal number*}} |
---|
310 | test lindex-12.7 {bad octal} -body { |
---|
311 | set x end--0o9 |
---|
312 | list [catch { lindex {a b c} $x } result] $result |
---|
313 | } -match glob -result {1 {*invalid octal number*}} |
---|
314 | test lindex-12.8 {bad integer, not octal} { |
---|
315 | set x end-0a2 |
---|
316 | list [catch { lindex {a b c} $x } result] $result |
---|
317 | } {1 {bad index "end-0a2": must be integer?[+-]integer? or end?[+-]integer?}} |
---|
318 | test lindex-12.9 {obsolete test} { |
---|
319 | set x end |
---|
320 | catch { |
---|
321 | list [lindex {a b c} $x] [lindex {a b c} $x] |
---|
322 | } result |
---|
323 | set result |
---|
324 | } {c c} |
---|
325 | test lindex-12.10 {incomplete end-} { |
---|
326 | set x end- |
---|
327 | list [catch { lindex {a b c} $x } result] $result |
---|
328 | } {1 {bad index "end-": must be integer?[+-]integer? or end?[+-]integer?}} |
---|
329 | |
---|
330 | test lindex-13.1 {bad second index} { |
---|
331 | list [catch { lindex {a b c} 0 0a2 } result] $result |
---|
332 | } {1 {bad index "0a2": must be integer?[+-]integer? or end?[+-]integer?}} |
---|
333 | test lindex-13.2 {good second index} { |
---|
334 | catch { |
---|
335 | lindex {{a b c} {d e f} {g h i}} 1 2 |
---|
336 | } result |
---|
337 | set result |
---|
338 | } f |
---|
339 | test lindex-13.3 {three indices} { |
---|
340 | catch { |
---|
341 | lindex {{{a b} {c d}} {{e f} {g h}}} 1 0 1 |
---|
342 | } result |
---|
343 | set result |
---|
344 | } f |
---|
345 | |
---|
346 | test lindex-14.1 {error conditions in parsing list} { |
---|
347 | list [catch { lindex "a \{" 2 } msg] $msg |
---|
348 | } {1 {unmatched open brace in list}} |
---|
349 | test lindex-14.2 {error conditions in parsing list} { |
---|
350 | list [catch { lindex {a {b c}d e} 2 } msg] $msg |
---|
351 | } {1 {list element in braces followed by "d" instead of space}} |
---|
352 | test lindex-14.3 {error conditions in parsing list} { |
---|
353 | list [catch { lindex {a "b c"def ghi} 2 } msg] $msg |
---|
354 | } {1 {list element in quotes followed by "def" instead of space}} |
---|
355 | |
---|
356 | test lindex-15.1 {quoted elements} { |
---|
357 | catch { |
---|
358 | lindex {a "b c" d} 1 |
---|
359 | } result |
---|
360 | set result |
---|
361 | } {b c} |
---|
362 | test lindex-15.2 {quoted elements} { |
---|
363 | catch { |
---|
364 | lindex {"{}" b c} 0 |
---|
365 | } result |
---|
366 | set result |
---|
367 | } {{}} |
---|
368 | test lindex-15.3 {quoted elements} { |
---|
369 | catch { |
---|
370 | lindex {ab "c d \" x" y} 1 |
---|
371 | } result |
---|
372 | set result |
---|
373 | } {c d " x} |
---|
374 | test lindex-15.4 {quoted elements} { |
---|
375 | catch { |
---|
376 | lindex {a b {c d "e} {f g"}} 2 |
---|
377 | } result |
---|
378 | set result |
---|
379 | } {c d "e} |
---|
380 | |
---|
381 | test lindex-16.1 {data reuse} { |
---|
382 | set x 0 |
---|
383 | catch { |
---|
384 | lindex $x $x |
---|
385 | } result |
---|
386 | set result |
---|
387 | } {0} |
---|
388 | test lindex-16.2 {data reuse} { |
---|
389 | set a 0 |
---|
390 | catch { |
---|
391 | lindex $a $a $a |
---|
392 | } result |
---|
393 | set result |
---|
394 | } 0 |
---|
395 | test lindex-16.3 {data reuse} { |
---|
396 | set a 1 |
---|
397 | catch { |
---|
398 | lindex $a $a $a |
---|
399 | } result |
---|
400 | set result |
---|
401 | } {} |
---|
402 | test lindex-16.4 {data reuse} { |
---|
403 | set x [list 0 0] |
---|
404 | catch { |
---|
405 | lindex $x $x |
---|
406 | } result |
---|
407 | set result |
---|
408 | } {0} |
---|
409 | test lindex-16.5 {data reuse} { |
---|
410 | set x 0 |
---|
411 | catch { |
---|
412 | lindex $x [list $x $x] |
---|
413 | } result |
---|
414 | set result |
---|
415 | } {0} |
---|
416 | test lindex-16.6 {data reuse} { |
---|
417 | set x [list 1 1] |
---|
418 | catch { |
---|
419 | lindex $x $x |
---|
420 | } result |
---|
421 | set result |
---|
422 | } {} |
---|
423 | test lindex-16.7 {data reuse} { |
---|
424 | set x 1 |
---|
425 | catch { |
---|
426 | lindex $x [list $x $x] |
---|
427 | } result |
---|
428 | set result |
---|
429 | } {} |
---|
430 | |
---|
431 | test lindex-17.0 {Bug 1718580} {*}{ |
---|
432 | -body { |
---|
433 | lindex {} end foo |
---|
434 | } |
---|
435 | -match glob |
---|
436 | -result {bad index "foo"*} |
---|
437 | -returnCodes 1 |
---|
438 | } |
---|
439 | |
---|
440 | test lindex-17.1 {Bug 1718580} {*}{ |
---|
441 | -body { |
---|
442 | lindex a end foo |
---|
443 | } |
---|
444 | -match glob |
---|
445 | -result {bad index "foo"*} |
---|
446 | -returnCodes 1 |
---|
447 | } |
---|
448 | |
---|
449 | catch { unset minus } |
---|
450 | |
---|
451 | # cleanup |
---|
452 | ::tcltest::cleanupTests |
---|
453 | return |
---|
454 | |
---|
455 | # Local Variables: |
---|
456 | # mode: tcl |
---|
457 | # End: |
---|