1 # Commands covered: lindex
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.
10 # Copyright (c) 2001 by Kevin B. Kenny. All rights reserved.
12 # See the file "license.terms" for information on usage and redistribution
13 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 if {"::tcltest" ni [namespace children]} {
16 package require tcltest 2.5
17 namespace import -force ::tcltest::*
20 ::tcltest::loadTestedCommands
21 catch [list package require -exact Tcltest [info patchlevel]]
24 testConstraint testevalex [llength [info commands testevalex]]
26 # Tests of Tcl_LindexObjCmd, NOT COMPILED
28 test lindex-1.1 {wrong # args} testevalex {
29 list [catch {testevalex lindex} result] $result
30 } "1 {wrong # args: should be \"lindex list ?index ...?\"}"
32 # Indices that are lists or convertible to lists
34 test lindex-2.1 {empty index list} testevalex {
36 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
38 test lindex-2.2 {singleton index list} testevalex {
40 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
42 test lindex-2.3 {multiple indices in list} testevalex {
44 list [testevalex {lindex {{a b c} {d e f}} $x}] \
45 [testevalex {lindex {{a b c} {d e f}} $x}]
47 test lindex-2.4 {malformed index list} testevalex {
49 list [catch { testevalex {lindex {a b c} $x} } result] $result
50 } {1 bad\ index\ \"\{\":\ must\ be\ integer?\[+-\]integer?\ or\ end?\[+-\]integer?}
52 # Indices that are integers or convertible to integers
54 test lindex-3.1 {integer -1} testevalex {
56 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
58 test lindex-3.2 {integer 0} testevalex {
59 set x [string range 00 0 0]
60 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
62 test lindex-3.3 {integer 2} testevalex {
63 set x [string range 22 0 0]
64 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
66 test lindex-3.4 {integer 3} testevalex {
67 set x [string range 33 0 0]
68 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
70 test lindex-3.5 {bad octal} -constraints testevalex -body {
72 list [catch { testevalex {lindex {a b c} $x} } result] $result
73 } -match glob -result {1 {*invalid octal number*}}
74 test lindex-3.6 {bad octal} -constraints testevalex -body {
76 list [catch { testevalex {lindex {a b c} $x} } result] $result
77 } -match glob -result {1 {*invalid octal number*}}
78 test lindex-3.7 {indexes don't shimmer wide ints} {
79 set x [expr {(wide(1)<<31) - 2}]
80 list $x [lindex {1 2 3} $x] [incr x] [incr x]
81 } {2147483646 {} 2147483647 2147483648}
82 test lindex-3.8 {compiled with static indices out of range, negative} {
83 list [lindex {a b c} -1] [lindex {a b c} -2] [lindex {a b c} -3]
85 test lindex-3.9 {compiled with calculated indices out of range, negative constant} {
86 list [lindex {a b c} -1-1] [lindex {a b c} -2+0] [lindex {a b c} -2+1]
88 test lindex-3.10 {compiled with calculated indices out of range, after end} {
89 list [lindex {a b c} end+1] [lindex {a b c} end+2] [lindex {a b c} end+3]
92 # Indices relative to end
94 test lindex-4.1 {index = end} testevalex {
96 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
98 test lindex-4.2 {index = end--1} testevalex {
100 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
102 test lindex-4.3 {index = end-0} testevalex {
104 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
106 test lindex-4.4 {index = end-2} testevalex {
108 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
110 test lindex-4.5 {index = end-3} testevalex {
112 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
114 test lindex-4.6 {bad octal} -constraints testevalex -body {
116 list [catch { testevalex {lindex {a b c} $x} } result] $result
117 } -match glob -result {1 {*invalid octal number*}}
118 test lindex-4.7 {bad octal} -constraints testevalex -body {
120 list [catch { testevalex {lindex {a b c} $x} } result] $result
121 } -match glob -result {1 {*invalid octal number*}}
122 test lindex-4.8 {bad integer, not octal} testevalex {
124 list [catch { testevalex {lindex {a b c} $x} } result] $result
125 } {1 {bad index "end-0a2": must be integer?[+-]integer? or end?[+-]integer?}}
126 test lindex-4.9 {obsolete test} testevalex {
128 list [testevalex {lindex {a b c} $x}] [testevalex {lindex {a b c} $x}]
130 test lindex-4.10 {incomplete end-} testevalex {
132 list [catch { testevalex {lindex {a b c} $x} } result] $result
133 } {1 {bad index "end-": must be integer?[+-]integer? or end?[+-]integer?}}
135 test lindex-5.1 {bad second index} testevalex {
136 list [catch { testevalex {lindex {a b c} 0 0a2} } result] $result
137 } {1 {bad index "0a2": must be integer?[+-]integer? or end?[+-]integer?}}
138 test lindex-5.2 {good second index} testevalex {
139 testevalex {lindex {{a b c} {d e f} {g h i}} 1 2}
141 test lindex-5.3 {three indices} testevalex {
142 testevalex {lindex {{{a b} {c d}} {{e f} {g h}}} 1 0 1}
145 test lindex-6.1 {error conditions in parsing list} testevalex {
146 list [catch {testevalex {lindex "a \{" 2}} msg] $msg
147 } {1 {unmatched open brace in list}}
148 test lindex-6.2 {error conditions in parsing list} testevalex {
149 list [catch {testevalex {lindex {a {b c}d e} 2}} msg] $msg
150 } {1 {list element in braces followed by "d" instead of space}}
151 test lindex-6.3 {error conditions in parsing list} testevalex {
152 list [catch {testevalex {lindex {a "b c"def ghi} 2}} msg] $msg
153 } {1 {list element in quotes followed by "def" instead of space}}
155 test lindex-7.1 {quoted elements} testevalex {
156 testevalex {lindex {a "b c" d} 1}
158 test lindex-7.2 {quoted elements} testevalex {
159 testevalex {lindex {"{}" b c} 0}
161 test lindex-7.3 {quoted elements} testevalex {
162 testevalex {lindex {ab "c d \" x" y} 1}
164 test lindex-7.4 {quoted elements} {
165 lindex {a b {c d "e} {f g"}} 2
168 test lindex-8.1 {data reuse} testevalex {
170 testevalex {lindex $x $x}
172 test lindex-8.2 {data reuse} testevalex {
174 testevalex {lindex $a $a $a}
176 test lindex-8.3 {data reuse} testevalex {
178 testevalex {lindex $a $a $a}
180 test lindex-8.4 {data reuse} testevalex {
182 testevalex {lindex $x $x}
184 test lindex-8.5 {data reuse} testevalex {
186 testevalex {lindex $x [list $x $x]}
188 test lindex-8.6 {data reuse} testevalex {
190 testevalex {lindex $x $x}
192 test lindex-8.7 {data reuse} testevalex {
194 testevalex {lindex $x [list $x $x]}
197 #----------------------------------------------------------------------
199 # Compilation tests for lindex
201 test lindex-9.1 {wrong # args} {
202 list [catch {lindex} result] $result
203 } "1 {wrong # args: should be \"lindex list ?index ...?\"}"
204 test lindex-9.2 {ensure that compilation works in the right order} {
212 # Indices that are lists or convertible to lists
214 test lindex-10.1 {empty index list} {
217 list [lindex {a b c} $x] [lindex {a b c} $x]
221 test lindex-10.2 {singleton index list} {
224 list [lindex {a b c} $x] [lindex {a b c} $x]
228 test lindex-10.3 {multiple indices in list} {
231 list [lindex {{a b c} {d e f}} $x] [lindex {{a b c} {d e f}} $x]
235 test lindex-10.4 {malformed index list} {
237 list [catch { lindex {a b c} $x } result] $result
238 } {1 bad\ index\ \"\{\":\ must\ be\ integer?\[+-\]integer?\ or\ end?\[+-\]integer?}
240 # Indices that are integers or convertible to integers
242 test lindex-11.1 {integer -1} {
245 list [lindex {a b c} $x] [lindex {a b c} $x]
249 test lindex-11.2 {integer 0} {
250 set x [string range 00 0 0]
252 list [lindex {a b c} $x] [lindex {a b c} $x]
256 test lindex-11.3 {integer 2} {
257 set x [string range 22 0 0]
259 list [lindex {a b c} $x] [lindex {a b c} $x]
263 test lindex-11.4 {integer 3} {
264 set x [string range 33 0 0]
266 list [lindex {a b c} $x] [lindex {a b c} $x]
270 test lindex-11.5 {bad octal} -body {
272 list [catch { lindex {a b c} $x } result] $result
273 } -match glob -result {1 {*invalid octal number*}}
274 test lindex-11.6 {bad octal} -body {
276 list [catch { lindex {a b c} $x } result] $result
277 } -match glob -result {1 {*invalid octal number*}}
279 # Indices relative to end
281 test lindex-12.1 {index = end} {
284 list [lindex {a b c} $x] [lindex {a b c} $x]
288 test lindex-12.2 {index = end--1} {
291 list [lindex {a b c} $x] [lindex {a b c} $x]
295 test lindex-12.3 {index = end-0} {
298 list [lindex {a b c} $x] [lindex {a b c} $x]
302 test lindex-12.4 {index = end-2} {
305 list [lindex {a b c} $x] [lindex {a b c} $x]
309 test lindex-12.5 {index = end-3} {
312 list [lindex {a b c} $x] [lindex {a b c} $x]
316 test lindex-12.6 {bad octal} -body {
318 list [catch { lindex {a b c} $x } result] $result
319 } -match glob -result {1 {*invalid octal number*}}
320 test lindex-12.7 {bad octal} -body {
322 list [catch { lindex {a b c} $x } result] $result
323 } -match glob -result {1 {*invalid octal number*}}
324 test lindex-12.8 {bad integer, not octal} {
326 list [catch { lindex {a b c} $x } result] $result
327 } {1 {bad index "end-0a2": must be integer?[+-]integer? or end?[+-]integer?}}
328 test lindex-12.9 {obsolete test} {
331 list [lindex {a b c} $x] [lindex {a b c} $x]
335 test lindex-12.10 {incomplete end-} {
337 list [catch { lindex {a b c} $x } result] $result
338 } {1 {bad index "end-": must be integer?[+-]integer? or end?[+-]integer?}}
340 test lindex-13.1 {bad second index} {
341 list [catch { lindex {a b c} 0 0a2 } result] $result
342 } {1 {bad index "0a2": must be integer?[+-]integer? or end?[+-]integer?}}
343 test lindex-13.2 {good second index} {
345 lindex {{a b c} {d e f} {g h i}} 1 2
349 test lindex-13.3 {three indices} {
351 lindex {{{a b} {c d}} {{e f} {g h}}} 1 0 1
356 test lindex-14.1 {error conditions in parsing list} {
357 list [catch { lindex "a \{" 2 } msg] $msg
358 } {1 {unmatched open brace in list}}
359 test lindex-14.2 {error conditions in parsing list} {
360 list [catch { lindex {a {b c}d e} 2 } msg] $msg
361 } {1 {list element in braces followed by "d" instead of space}}
362 test lindex-14.3 {error conditions in parsing list} {
363 list [catch { lindex {a "b c"def ghi} 2 } msg] $msg
364 } {1 {list element in quotes followed by "def" instead of space}}
366 test lindex-15.1 {quoted elements} {
372 test lindex-15.2 {quoted elements} {
378 test lindex-15.3 {quoted elements} {
380 lindex {ab "c d \" x" y} 1
384 test lindex-15.4 {quoted elements} {
386 lindex {a b {c d "e} {f g"}} 2
391 test lindex-16.1 {data reuse} {
398 test lindex-16.2 {data reuse} {
405 test lindex-16.3 {data reuse} {
412 test lindex-16.4 {data reuse} {
419 test lindex-16.5 {data reuse} {
422 lindex $x [list $x $x]
426 test lindex-16.6 {data reuse} {
433 test lindex-16.7 {data reuse} {
436 lindex $x [list $x $x]
441 test lindex-17.0 {Bug 1718580} {*}{
446 -result {bad index "foo"*}
450 test lindex-17.1 {Bug 1718580} {*}{
455 -result {bad index "foo"*}
459 catch { unset minus }
462 ::tcltest::cleanupTests