Debian Codegraph @

tests/lrange.test

raw
1 # Commands covered: lrange
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 #
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 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18
19 ::tcltest::loadTestedCommands
20 catch [list package require -exact Tcltest [info patchlevel]]
21
22 testConstraint testpurebytesobj [llength [info commands testpurebytesobj]]
23
24
25 test lrange-1.1 {range of list elements} {
26 lrange {a b c d} 1 2
27 } {b c}
28 test lrange-1.2 {range of list elements} {
29 lrange {a {bcd e {f g {}}} l14 l15 d} 1 1
30 } {{bcd e {f g {}}}}
31 test lrange-1.3 {range of list elements} {
32 lrange {a {bcd e {f g {}}} l14 l15 d} 3 end
33 } {l15 d}
34 test lrange-1.4 {range of list elements} {
35 lrange {a {bcd e {f g {}}} l14 l15 d} 4 10000
36 } {d}
37 test lrange-1.5 {range of list elements} {
38 lrange {a {bcd e {f g {}}} l14 l15 d} 4 3
39 } {}
40 test lrange-1.6 {range of list elements} {
41 lrange {a {bcd e {f g {}}} l14 l15 d} 10 11
42 } {}
43 test lrange-1.7 {range of list elements} {
44 lrange {a b c d e} -1 2
45 } {a b c}
46 test lrange-1.8 {range of list elements} {
47 lrange {a b c d e} -2 -1
48 } {}
49 test lrange-1.9 {range of list elements} {
50 lrange {a b c d e} -2 end
51 } {a b c d e}
52 test lrange-1.10 {range of list elements} {
53 lrange "a b\{c d" 1 2
54 } "b\\{c d"
55 test lrange-1.11 {range of list elements} {
56 lrange "a b c d" end end
57 } d
58 test lrange-1.12 {range of list elements} {
59 lrange "a b c d" end 100000
60 } d
61 test lrange-1.13 {range of list elements} {
62 lrange "a b c d" end 3
63 } d
64 test lrange-1.14 {range of list elements} {
65 lrange "a b c d" end 2
66 } {}
67 test lrange-1.15 {range of list elements} {
68 concat \"[lrange {a b \{\ } 0 2]"
69 } {"a b \{\ "}
70 # emacs highlighting bug workaround --> "
71 test lrange-1.16 {list element quoting} {
72 lrange {[append a .b]} 0 end
73 } {{[append} a .b\]}
74
75 test lrange-2.1 {error conditions} {
76 list [catch {lrange a b} msg] $msg
77 } {1 {wrong # args: should be "lrange list first last"}}
78 test lrange-2.2 {error conditions} {
79 list [catch {lrange a b 6 7} msg] $msg
80 } {1 {wrong # args: should be "lrange list first last"}}
81 test lrange-2.3 {error conditions} {
82 list [catch {lrange a b 6} msg] $msg
83 } {1 {bad index "b": must be integer?[+-]integer? or end?[+-]integer?}}
84 test lrange-2.4 {error conditions} {
85 list [catch {lrange a 0 enigma} msg] $msg
86 } {1 {bad index "enigma": must be integer?[+-]integer? or end?[+-]integer?}}
87 test lrange-2.5 {error conditions} {
88 list [catch {lrange "a \{b c" 3 4} msg] $msg
89 } {1 {unmatched open brace in list}}
90 test lrange-2.6 {error conditions} {
91 list [catch {lrange "a b c \{ d e" 1 4} msg] $msg
92 } {1 {unmatched open brace in list}}
93
94 test lrange-3.1 {Bug 3588366: end-offsets before start} {
95 apply {l {
96 lrange $l 0 end-5
97 }} {1 2 3 4 5}
98 } {}
99
100 test lrange-3.2 {compiled with static indices out of range, negative} {
101 list [lrange {a b c} -1 -2] [lrange {a b c} -2 -1] [lrange {a b c} -3 -2] [lrange {a b c} -2 -3]
102 } [lrepeat 4 {}]
103 test lrange-3.3 {compiled with calculated indices out of range, negative constant} {
104 list [lrange {a b c} 0-1 -1-1] [lrange {a b c} -2+0 0-1] [lrange {a b c} -2-1 -2+1] [lrange {a b c} -2+1 -2-1]
105 } [lrepeat 4 {}]
106 test lrange-3.4 {compiled with calculated indices out of range, after end} {
107 list [lrange {a b c} end+1 end+2] [lrange {a b c} end+2 end+1] [lrange {a b c} end+2 end+3] [lrange {a b c} end+3 end+2]
108 } [lrepeat 4 {}]
109
110 test lrange-3.5 {compiled with calculated indices, start out of range (negative)} {
111 list [lrange {a b c} -1 1] [lrange {a b c} -1+0 end-1] [lrange {a b c} -2 1] [lrange {a b c} -2+0 0+1]
112 } [lrepeat 4 {a b}]
113 test lrange-3.6 {compiled with calculated indices, end out of range (after end)} {
114 list [lrange {a b c} 1 end+1] [lrange {a b c} 1+0 2+1] [lrange {a b c} 1 end+1] [lrange {a b c} end-1 3+1]
115 } [lrepeat 4 {b c}]
116
117 test lrange-3.7a {compiled on empty not canonical list (with static and dynamic indices), regression test, bug [cc1e91552c]} {
118 list [lrange { } 0 1] [lrange [format %c 32] 0 1] [lrange [set a { }] 0 1] \
119 [lrange { } 0-1 end+1] [lrange [format %c 32] 0-1 end+1] [lrange $a 0-1 end+1]
120 } [lrepeat 6 {}]
121 test lrange-3.7b {not compiled on empty not canonical list (with static and dynamic indices), regression test, bug [cc1e91552c]} {
122 set cmd lrange
123 list [$cmd { } 0 1] [$cmd [format %c 32] 0 1] [$cmd [set a { }] 0 1] \
124 [$cmd { } 0-1 end+1] [$cmd [format %c 32] 0-1 end+1] [$cmd $a 0-1 end+1]
125 } [lrepeat 6 {}]
126 # following 4 tests could cause a segfault on empty non-lists with tclEmptyStringRep
127 # (as before the fix [58c46e74b931d3a1]):
128 test lrange-3.7a.2 {compiled on empty not list object, 2nd regression test, bug [cc1e91552c]} {
129 list [lrange {} 0 1] [lrange [lindex a -1] 0 1] [lrange [set a {}] 0 1] \
130 [lrange {} 0-1 end+1] [lrange [lindex a -1] 0-1 end+1] [lrange $a 0-1 end+1]
131 } [lrepeat 6 {}]
132 test lrange-3.7b.2 {not compiled on empty not list object, 2nd regression test, bug [cc1e91552c]} {
133 set cmd lrange
134 list [$cmd {} 0 1] [$cmd [lindex a -1] 0 1] [$cmd [set a {}] 0 1] \
135 [$cmd {} 0-1 end+1] [$cmd [lindex a -1] 0-1 end+1] [$cmd $a 0-1 end+1]
136 } [lrepeat 6 {}]
137 test lrange-3.7c.2 {compiled on empty pure bytes object, 2nd regression test, bug [cc1e91552c]} -constraints {
138 testpurebytesobj
139 } -body {
140 list [lrange [testpurebytesobj] 0 1] [lrange [testpurebytesobj { }] 0 1] [lrange [set a [testpurebytesobj {}]] 0 1] \
141 [lrange [testpurebytesobj] 0-1 end+1] [lrange [testpurebytesobj { }] 0-1 end+1] [lrange $a 0-1 end+1]
142 } -result [lrepeat 6 {}]
143 test lrange-3.7d.2 {not compiled on empty pure bytes object, 2nd regression test, bug [cc1e91552c]} -constraints {
144 testpurebytesobj
145 } -body {
146 set cmd lrange
147 list [$cmd [testpurebytesobj] 0 1] [$cmd [testpurebytesobj { }] 0 1] [$cmd [set a [testpurebytesobj {}]] 0 1] \
148 [$cmd [testpurebytesobj] 0-1 end+1] [$cmd [testpurebytesobj { }] 0-1 end+1] [$cmd $a 0-1 end+1]
149 } -result [lrepeat 6 {}]
150
151
152 # cleanup
153 ::tcltest::cleanupTests
154 return
155
156 # Local Variables:
157 # mode: tcl
158 # End: