aboutsummaryrefslogtreecommitdiff
path: root/tests/misc.test
blob: f9a2de752c767d06bb26d5cf136013579ce84017 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
source [file dirname [info script]]/testing.tcl

needs constraint jim
needs cmd gets tclcompat
needs cmd array

catch {unset a b}
test regr-1.1 "Double dereference arrays" {
	array set a {one ONE two TWO three THREE}
	array set b {ONE 1 TWO 2 THREE 3}
	set chan two
	set b($a($chan))
} {2}

# Will assert on exit if the bug exists
test regr-1.2 "Reference count shared literals" {
	proc a {} {
		while {1} {break}
	}
	a
	rename a ""
	return 1
} {1}

test regr-1.3 "Invalid for expression" jim {
	# Crashes with invalid expression
	catch {
		for {set i 0} {$i < n} {incr i} {
			set a(b) $i
			set a(c) $i
			break
		}
	}
} 1

test regr-1.4 "format double percent" {
	format (%d%%) 12
} {(12%)}

test regr-1.5 "lassign with empty list" {
	unset -nocomplain a b c
	lassign {} a b c
	info exists c
} {1}

test io-1.1 "Read last line with no newline" {
	set lines 0
	set f [open $testdir/testio.in]
	while {[gets $f buf] >= 0} {
		incr lines
	}
	close $f
	list $lines
} {2}

set g1 1
set g2 2
array set g3 {4 5 6 7}

proc test_unset {} {
	test unset-1.1 "Simple var" {
		set g4 4
		list [catch {unset g4; info exists g4} msg] $msg
	} {0 0}

	test unset-1.2 "Simple var" {
		list [catch {unset g4; info exists g4} msg] $msg
	} {1 {can't unset "g4": no such variable}}

	test unset-1.3 "Simple var" {
		list [catch {unset g2; info exists g2} msg] $msg
	} {1 {can't unset "g2": no such variable}}

	test unset-1.4 "Global via global" {
		global g1
		list [catch {unset g1; info exists g1} msg] $msg
	} {0 0}

	test unset-1.5 "Global error" {
		list [catch {unset ::g2; info exists ::g2} msg] $msg
	} {0 0}

	test unset-1.6 "Global array" {
		list [catch {unset ::g3; info exists ::g3} msg] $msg
	} {0 0}

	test unset-1.7 "Simple var -nocomplain" {
		list [catch {unset -nocomplain g2; info exists g2} msg] $msg
	} {0 0}

	test unset-1.8 "Simple var --" {
		list [catch {unset -- g2; info exists g2} msg] $msg
	} {1 {can't unset "g2": no such variable}}

	test unset-1.9 "Simple var -nocomplain --" {
		set g2 1
		list [catch {unset -nocomplain -- g2; info exists g2} msg] $msg
	} {0 0}

	test unset-1.10 "Var named -nocomplain with --" {
		set -nocomplain 1
		list [catch {unset -- -nocomplain; info exists -nocomplain} msg] $msg
	} {0 0}

	test unset-1.11 "Unset no args" {
		list [catch {unset} msg] $msg
	} {0 {}}
}

test_unset

test lrepeat-1.1 "Basic tests" {
	lrepeat 1 a
} {a}

test lrepeat-1.2 "Basic tests" {
	lrepeat 1 a b
} {a b}

test lrepeat-1.3 "Basic tests" {
	lrepeat 2 a b
} {a b a b}

test lrepeat-1.4 "Basic tests" {
	lrepeat 2 a
} {a a}

test lrepeat-1.5 "Errors" {
	catch {lrepeat}
} {1}

test lrepeat-1.6 "Errors" {
	lrepeat 1
} {}

test lrepeat-1.7 "Errors" {
	lrepeat 0 a b
} {}

test lrepeat-1.8 "Errors" {
	catch {lrepeat -10 a}
} {1}

test lindex-1.1 "Integer" {
	lindex {a b c} 0
} a

test lindex-1.2 "Integer" {
	lindex {a b c} 2
} c

test lindex-1.3 "Integer" {
	lindex {a b c} -1
} {}

test lindex-1.4 "Integer" {
	lindex {a b c} 4
} {}

test lindex-1.5 "end" {
	lindex {a b c} end
} c

test lindex-1.6 "end" {
	lindex {a b c} end-1
} b

test lindex-1.7 "end" {
	lindex {a b c} end-4
} {}

test lindex-1.8 "end + " {
	lindex {a b c} end+1
} {}

test lindex-1.9 "end + " {
	lindex {a b c} end+-1
} b

test lindex-1.10 "end - errors" {
	catch {lindex {a b c} end-}
} 1

test lindex-1.11 "end - errors" {
	catch {lindex {a b c} end-blah}
} 1

test lindex-1.12 "int+int, int-int" {
	lindex {a b c} 0+4
} {}

test lindex-1.13 "int+int, int-int" {
	lindex {a b c} 3-1
} c

test lindex-1.14 "int+int, int-int" {
	lindex {a b c} 1--1
} c

test lindex-1.15 "int+int, int-int" {
	set l {a b c}
	lindex $l [lsearch $l b]-1
} a

test lindex-1.16 "int+int, int-int" {
	lindex {a b c} 0+1
} b

test lindex-1.17 "int+int - errors" {
	catch {lindex {a b c} 5-blah}
} 1

test lindex-1.18 "int+int - errors" {
	catch {lindex {a b c} blah-2}
} 1

test lindex-1.19 "int+int - errors" {
	catch {lindex {a b c} 5+blah}
} 1

test lindex-1.20 "unary plus" {
	lindex {a b c} +2
} c

test incr-1.1 "incr unset" {
	unset -nocomplain a
	incr a
	set a
} 1

test incr-1.2 "incr, incr unset" {
	incr a
} 2

test incr-1.3 "incr unset array element" {
	unset -nocomplain a
	incr a(2)
	set a(2)
} 1

test incr-1.4 "incr array element - shimmering" {
	set b "$a(2)-test"
	incr a(2)
} 2

test catch-1.1 "catch ok" {
	list [catch {set abc 2} result] $result
} {0 2}

test catch-1.2 "catch error" {
	list [catch {error 3} result] $result
} {1 3}

test catch-1.3 "catch break" {
	list [catch {break} result] $result
} {3 {}}

test catch-1.4 "catch -nobreak" {
	set result {}
	foreach x {a b c} {
		lappend result $x
		# This acts just like break since it won't be caught by catch
		catch -nobreak {break} tmp
	}
	set result
} {a}

test catch-1.5 "catch -no3" {
	set result {}
	foreach x {a b c} {
		lappend result $x
		# Same as above, but specify as an integer
		catch -no3 {break} tmp
	}
	set result
} {a}

test catch-1.6 "catch break" {
	set result {}
	foreach x {a b c} {
		lappend result $x
		# This does nothing since the break is caught
		catch {break} tmp
	}
	set result
} {a b c}


test catch-1.7 "catch exit" {
	# Normally exit would not be caught
	dict get [info returncodes] [catch -exit {exit 5} result]
} {exit}

test catch-1.8 "catch error has -errorinfo" {
	set rc [catch {set undefined} msg opts]
	list $rc [info exists opts(-errorinfo)]
} {1 1}

test catch-1.9 "catch no error has no -errorinfo" {
	set rc [catch {set x 1} msg opts]
	list $rc [info exists opts(-errorinfo)]
} {0 0}

test return-1.1 "return can rethrow an error" {
	proc a {} { error "from a" }
	proc b {} { catch {a} msg opts; return {*}$opts $msg }
	set rc [catch {b} msg opts]
	list $rc $msg [llength $opts(-errorinfo)]
} {1 {from a} 6}

test return-1.2 "error can rethrow an error" {
	proc a {} { error "from a" }
	proc b {} { catch {a} msg; error $msg [info stacktrace] }
	set rc [catch {b} msg opts]
	list $rc $msg [llength $opts(-errorinfo)]
} {1 {from a} 9}

test return-1.3 "return can rethrow no error" {
	proc a {} { return "from a" }
	proc b {} { catch {a} msg opts; return {*}$opts $msg }
	set rc [catch {b} msg opts]
	#list $rc $msg [llength $opts(-errorinfo)]
	list $rc $msg [info exists opts(-errorinfo)]
} {0 {from a} 0}

test stringreverse-1.1 "Containing nulls" {
	string reverse abc\0def
} "fed\0cba"

test split-1.1 "Split with leading null" {
	split "\0abc\0def\0" \0
} {{} abc def {}}

test parsevar-1.1 "Variables should include double colons" {
	set ::a::b 2
	set x $::a::b
	unset ::a::b
	set x
} 2

testreport