summaryrefslogtreecommitdiffstats
path: root/tests/listObj.test
blob: d7fb46c1ade0b437a3b51a0566e428817a9c3b47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Functionality covered: operation of the procedures in tclListObj.c that
# implement the Tcl type manager for the list object type.
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands. Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1995-1996 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import -force ::tcltest::*
}

::tcltest::loadTestedCommands
catch [list package require -exact Tcltest [info patchlevel]]

testConstraint testobj [llength [info commands testobj]]

catch {unset x}
test listobj-1.1 {Tcl_GetListObjType} emptyTest {
    # Test removed; tested an internal detail
    # that's no longer correct, and duplicated test obj-1.1
} {}

test listobj-2.1 {Tcl_SetListObj, use in lappend} {
    catch {unset x}
    list [lappend x 1 abc def] [lappend x 1 ghi jkl] $x
} {{1 abc def} {1 abc def 1 ghi jkl} {1 abc def 1 ghi jkl}}
test listobj-2.2 {Tcl_SetListObj, use in ObjInterpProc} {
    proc return_args {args} {
        return $args
    }
    list [return_args] [return_args x] [return_args x y]
} {{} x {x y}}
test listobj-2.3 {Tcl_SetListObj, zero element count} {
    list
} {}

test listobj-3.1 {Tcl_ListObjAppend, list conversion} {
    catch {unset x}
    list [lappend x 1 2 abc "long string"] $x
} {{1 2 abc {long string}} {1 2 abc {long string}}}
test listobj-3.2 {Tcl_ListObjAppend, list conversion} {
    set x ""
    list [lappend x first second] [lappend x third fourth] $x
} {{first second} {first second third fourth} {first second third fourth}}
test listobj-3.3 {Tcl_ListObjAppend, list conversion} {
    set x "abc def"
    list [lappend x first second] $x
} {{abc def first second} {abc def first second}}
test listobj-3.4 {Tcl_ListObjAppend, error in conversion} {
    set x " \{"
    list [catch {lappend x abc def} msg] $msg
} {1 {unmatched open brace in list}}
test listobj-3.5 {Tcl_ListObjAppend, force internal rep array to grow} {
    set x ""
    list [lappend x 1 1] [lappend x 2 2] [lappend x 3 3] [lappend x 4 4] \
        [lappend x 5 5] [lappend x 6 6] [lappend x 7 7] [lappend x 8 8] $x
} {{1 1} {1 1 2 2} {1 1 2 2 3 3} {1 1 2 2 3 3 4 4} {1 1 2 2 3 3 4 4 5 5} {1 1 2 2 3 3 4 4 5 5 6 6} {1 1 2 2 3 3 4 4 5 5 6 6 7 7} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}}

test listobj-4.1 {Tcl_ListObjAppendElement, list conversion} {
    catch {unset x}
    list [lappend x 1] $x
} {1 1}
test listobj-4.2 {Tcl_ListObjAppendElement, list conversion} {
    set x ""
    list [lappend x first] [lappend x second] $x
} {first {first second} {first second}}
test listobj-4.3 {Tcl_ListObjAppendElement, list conversion} {
    set x "abc def"
    list [lappend x first] $x
} {{abc def first} {abc def first}}
test listobj-4.4 {Tcl_ListObjAppendElement, error in conversion} {
    set x " \{"
    list [catch {lappend x abc} msg] $msg
} {1 {unmatched open brace in list}}
test listobj-4.5 {Tcl_ListObjAppendElement, force internal rep array to grow} {
    set x ""
    list [lappend x 1] [lappend x 2] [lappend x 3] [lappend x 4] \
        [lappend x 5] [lappend x 6] [lappend x 7] [lappend x 8] $x
} {1 {1 2} {1 2 3} {1 2 3 4} {1 2 3 4 5} {1 2 3 4 5 6} {1 2 3 4 5 6 7} {1 2 3 4 5 6 7 8} {1 2 3 4 5 6 7 8}}

test listobj-5.1 {Tcl_ListObjIndex, basic tests} {
    lindex {a b c} 0
} a
test listobj-5.2 {Tcl_ListObjIndex, basic tests} {
    lindex a 0
} a
test listobj-5.3 {Tcl_ListObjIndex, basic tests} {
    lindex {a {b c d} x} 1
} {b c d}
test listobj-5.4 {Tcl_ListObjIndex, basic tests} {
    lindex {a b c} 3
} {}
test listobj-5.5 {Tcl_ListObjIndex, basic tests} {
    lindex {a b c} 100
} {}
test listobj-5.6 {Tcl_ListObjIndex, basic tests} {
    lindex a 100
} {}
test listobj-5.7 {Tcl_ListObjIndex, basic tests} {
    lindex {} -1
} {}
test listobj-5.8 {Tcl_ListObjIndex, error in conversion} {
    set x " \{"
    list [catch {lindex $x 0} msg] $msg
} {1 {unmatched open brace in list}}

test listobj-6.1 {Tcl_ListObjLength} {
    llength {a b c d}
} 4
test listobj-6.2 {Tcl_ListObjLength} {
    llength {a b c {a b {c d}} d}
} 5
test listobj-6.3 {Tcl_ListObjLength} {
    llength {}
} 0
test listobj-6.4 {Tcl_ListObjLength, convert from non-list} {
    llength 123
} 1
test listobj-6.5 {Tcl_ListObjLength, error converting from non-list} {
    list [catch {llength "a b c \{"} msg] $msg
} {1 {unmatched open brace in list}}
test listobj-6.6 {Tcl_ListObjLength, error converting from non-list} {
    list [catch {llength "a {b}c"} msg] $msg
} {1 {list element in braces followed by "c" instead of space}}

test listobj-7.1 {Tcl_ListObjReplace, conversion from non-list} {
    lreplace 123 0 0 x
} {x}
test listobj-7.2 {Tcl_ListObjReplace, error converting from non-list} {
    list [catch {lreplace "a b c \{" 1 1 x} msg] $msg
} {1 {unmatched open brace in list}}
test listobj-7.3 {Tcl_ListObjReplace, error converting from non-list} {
    list [catch {lreplace "a {b}c" 1 2 x} msg] $msg
} {1 {list element in braces followed by "c" instead of space}}
test listobj-7.4 {Tcl_ListObjReplace, negative first element index} {
    lreplace {1 2 3 4 5} -1 1 a
} {a 3 4 5}
test listobj-7.5 {Tcl_ListObjReplace, last element index >= num elems} {
    lreplace {1 2 3 4 5} 3 7 a b c
} {1 2 3 a b c}
test listobj-7.6 {Tcl_ListObjReplace, first element index > last index} {
    lreplace {1 2 3 4 5} 3 1 a b c
} {1 2 3 a b c 4 5}
test listobj-7.7 {Tcl_ListObjReplace, no new elements} {
    lreplace {1 2 3 4 5} 1 1
} {1 3 4 5}
test listobj-7.8 {Tcl_ListObjReplace, shrink array in place} {
    lreplace {1 2 3 4 5 6 7} 4 5
} {1 2 3 4 7}
test listobj-7.9 {Tcl_ListObjReplace, grow array in place} {
    lreplace {1 2 3 4 5 6 7} 1 3 a b c d e
} {1 a b c d e 5 6 7}
test listobj-7.10 {Tcl_ListObjReplace, replace tail of array} {
    lreplace {1 2 3 4 5 6 7} 3 6 a
} {1 2 3 a}
test listobj-7.11 {Tcl_ListObjReplace, must grow internal array} {
    lreplace {1 2 3 4 5} 2 3 a b c d e f g h i j k l
} {1 2 a b c d e f g h i j k l 5}
test listobj-7.12 {Tcl_ListObjReplace, grow array, insert at start} {
    lreplace {1 2 3 4 5} -1 -1 a b c d e f g h i j k l
} {a b c d e f g h i j k l 1 2 3 4 5}
test listobj-7.13 {Tcl_ListObjReplace, grow array, insert at end} {
    lreplace {1 2 3 4 5} 4 1 a b c d e f g h i j k l
} {1 2 3 4 a b c d e f g h i j k l 5}

test listobj-8.1 {SetListFromAny} {
    lindex {0 foo\x00help 2} 1
} "foo\x00help"

test listobj-9.1 {UpdateStringOfList} {
    string length [list foo\x00help]
} 8

test listobj-10.1 {Bug [2971669]} {*}{
    -constraints testobj
    -setup {
	testobj freeallvars
    }
    -body {
	set result {}
	lappend result \
	    [testlistobj set 1 a b c d e] \
	    [testlistobj replace 1 0x7fffffff 0x7fffffff f] \
	    [testlistobj get 1]
    }
    -cleanup {
	testobj freeallvars
    }
    -result {{a b c d e} {} {a b c d e f}}
}

test listobj-11.1 {Bug 3598580: Tcl_ListObjReplace refcount management} testobj {
    testobj bug3598580
} 123

# cleanup
::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End: