aboutsummaryrefslogtreecommitdiff
path: root/tests/coverage.test
blob: 95933a3a5c5ff96e22937279e94e055fef19de64 (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
# various tests to improve code coverage

source [file dirname [info script]]/testing.tcl

constraint cmd getref
constraint cmd rand
constraint cmd namespace

constraint cmd {debug invstr}

test dupobj-1 {duplicate script object} {
	set y {expr 2}
	# make y a script
	eval $y
	# Now treat it as a list that needs duplicating
	lset y 0 abc
	set y
} {abc 2}

test dupobj-2 {duplicate expr object} {
	set y {2 + 1}
	# make y an expression
	expr $y
	# Now treat it as a list that needs duplicating
	lset y 0 abc
	set y
} {abc + 1}

test dupobj-3 {duplicate interpolated object} namespace {
	set w 4
	set y def($w)
	# Now treat it as a namespace object that needs duplicating
	namespace eval $y {}
	apply [list x {set x 1} $y] x
} {1}

test dupobj-4 {duplicate dict subst object} namespace {
	# make y a dict subst
	set def(4) 5
	set y def(4)
	incr $y
	# Now treat it as a namespace object that needs duplicating
	namespace eval $y {}
	apply [list x {set x 1} $y] x
} {1}

test dupobj-5 {duplicate object with no string rep} namespace {
	# A sorted list has no string rep
	set y [lsort {abc def}]
	# Now treat it as a namespace object that needs duplicating
	namespace eval $y {}
	apply [list x {set x 1} $y] x
} {1}

test dupobj-6 {duplicate object with no type dup proc} namespace {
	set x 6
	incr x
	# x is now an int, an object with no dup proc
	# using as a namespace requires the object to be duplicated
	namespace eval $x {
		proc a {} {}
		rename a ""
	}
} {}

test dupobj-7 {duplicate scan obj} namespace {
	set x "%d %d"
	scan "1 4" $x y z
	# Now treat it as a namespace object that needs duplicating
	namespace eval $x {}
	apply [list x {set x 1} $x] x
} {1}


test script-1 {convert empty object to script} {
	set empty [foreach a {} {}]
	eval $empty
} {}

test ref-1 {treat something as a reference} getref {
	set ref [ref abc tag]
	getref $ref
} {abc}

test ref-2 {getref invalid reference} -constraints getref -body {
	getref "<reference.<tag____>.99999999999999000000>"
} -returnCodes error -match glob -result {invalid reference id *}

test ref-3 {getref invalid reference tag} -constraints getref -body {
	getref "<reference.<tag!%(*>.99999999999999000000>"
} -returnCodes error -match glob -result {expected reference but got "<reference.<tag!%(*>.99999999999999000000>"}

test ref-4 {finalize} getref {
	finalize $ref
} {}

test ref-5 {finalize} getref {
	finalize $ref cleanup
	finalize $ref cleanup2
	finalize $ref
} {cleanup2}

test ref-6 {finalize get invalid reference} -constraints getref -body {
	finalize "<reference.<tag____>.99999999999999000000>"
} -returnCodes error -match glob -result {invalid reference id *}

test ref-7 {finalize set invalid reference} -constraints getref -body {
	finalize "<reference.<tag____>.99999999999999000000>" cleanup
} -returnCodes error -match glob -result {invalid reference id *}

test collect-1 {recursive collect} getref {
	set ref2 [ref dummy cleanup2]
	unset ref2
	proc cleanup2 {ref value} {
		# Try to call collect
		stdout puts "in cleanup2: ref=$ref, value=$value"
		if {[collect]} {
			error "Should return 0"
		}
	}
	collect
} {1}

test scan-1 {update string of scan obj} debug-invstr {
	set x "%d %d"
	scan "1 4" $x y z
	debug invstr $x
	# x is now of scanfmt type with no string rep
	set x
} {%d %d}

# It is too hard to do this one without debug invstr
test index-1 {update string of index} debug-invstr {
	set x end-1
	lindex {a b c} $x
	debug invstr $x
	# x is now of index type with no string rep
	set x
} {end-1}

test index-2 {update string of index} debug-invstr {
	set x end
	lindex {a b c} $x
	debug invstr $x
	# x is now of index type with no string rep
	set x
} {end}

test index-3 {update string of index} debug-invstr {
	set x 2
	lindex {a b c} $x
	debug invstr $x
	# x is now of index type with no string rep
	set x
} {2}

test index-4 {index > INT_MAX} debug-invstr {
	set x 99999999999
	incr x
	# x is now of int type > INT_MAX
	lindex {a b c} $x
} {}

test index-5 {update string of index} debug-invstr {
	set x -1
	lindex {a b c} $x
	debug invstr $x
	# x is now of index type with no string rep
	set x
} {-2147483647}

test cmd-1 {standard -commands} jim {
	expr {"length" in [string -commands]}
} {1}

test rand-1 {rand} -constraints rand -body {
	rand 1 2 3
} -returnCodes error -result {wrong # args: should be "rand ?min? max"}

test rand-2 {rand} -constraints rand -body {
	rand foo
} -returnCodes error -match glob -result {expected integer *but got "foo"}

test rand-3 {rand} -constraints rand -body {
	rand 2 bar
} -returnCodes error -match glob -result {expected integer *but got "bar"}

test rand-4 {rand} rand {
	string is integer [rand]
} {1}

test rand-5 {srand} rand {
	set x [expr {srand(123)}]
	if {$x >= 0 && $x <= 1} {
		return 1
	} else {
		return 0
	}
} {1}

test lreverse-1 {lreverse} -body {
	lreverse
} -returnCodes error -result {wrong # args: should be "lreverse list"}

test divide-1 {expr} -constraints jim -body {
	/ 2 0
} -returnCodes error -result {Division by zero}

test variable-1 {upvar, name with embedded null} -constraints jim -body {
	proc a {} {
		upvar var\0null abc
		incr abc
	}
	set var\0null 2
	a
} -returnCodes ok -result {3}

test variable-2 {upvar to global name} {
	set ::globalvar 1
	proc a {} {
		upvar ::globalvar abc
		incr abc
	}
	a
} {2}

test unknown-1 {recursive unknown} -body {
	# unknown will call itself a maximum of 50 times before simply returning an error
	proc unknown {args} {
		nonexistent 3
	}
	nonexistent 4
} -returnCodes error -result {invalid command name "nonexistent"} -cleanup {
	rename unknown {}
}

test interpolate-1 {interpolate} -body {
	unset -nocomplain a
	for {set i 0} {$i < 10} {incr i} {
		set a($i) $i
	}
	set x "$a(0)$a(1)$a(2)$a(3)$a(4)$a(5)$a(6)$a(7)$a(8)$a(9)$nonexistent"
	set x
} -returnCodes error -result {can't read "nonexistent": no such variable}


testreport