| 1 |
# The tests in this file cover the procedures in tclCmdMZ.c. |
| 2 |
# |
| 3 |
# This file contains a collection of tests for one or more of the Tcl built-in |
| 4 |
# commands. Sourcing this file into Tcl runs the tests and generates output |
| 5 |
# 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 of |
| 12 |
# this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 |
|
| 14 |
if {[catch {package require tcltest 2.1}]} { |
| 15 |
puts stderr "Skipping tests in [info script]. tcltest 2.1 required." |
| 16 |
return |
| 17 |
} |
| 18 |
|
| 19 |
namespace eval ::tcl::test::cmdMZ { |
| 20 |
namespace import ::tcltest::cleanupTests |
| 21 |
namespace import ::tcltest::customMatch |
| 22 |
namespace import ::tcltest::makeFile |
| 23 |
namespace import ::tcltest::removeFile |
| 24 |
namespace import ::tcltest::temporaryDirectory |
| 25 |
namespace import ::tcltest::testConstraint |
| 26 |
namespace import ::tcltest::test |
| 27 |
|
| 28 |
testConstraint memory [llength [info commands memory]] |
| 29 |
|
| 30 |
if {[namespace which -command ::tcl::unsupported::timerate] ne ""} { |
| 31 |
namespace import ::tcl::unsupported::timerate |
| 32 |
} |
| 33 |
|
| 34 |
proc ListGlobMatch {expected actual} { |
| 35 |
if {[llength $expected] != [llength $actual]} { |
| 36 |
return 0 |
| 37 |
} |
| 38 |
foreach e $expected a $actual { |
| 39 |
if {![string match $e $a]} { |
| 40 |
return 0 |
| 41 |
} |
| 42 |
} |
| 43 |
return 1 |
| 44 |
} |
| 45 |
customMatch listGlob [namespace which ListGlobMatch] |
| 46 |
|
| 47 |
# Tcl_PwdObjCmd |
| 48 |
|
| 49 |
test cmdMZ-1.1 {Tcl_PwdObjCmd} -returnCodes error -body { |
| 50 |
pwd a |
| 51 |
} -result {wrong # args: should be "pwd"} |
| 52 |
test cmdMZ-1.2 {Tcl_PwdObjCmd: simple pwd} { |
| 53 |
catch pwd |
| 54 |
} 0 |
| 55 |
test cmdMZ-1.3 {Tcl_PwdObjCmd: simple pwd} -body { |
| 56 |
pwd |
| 57 |
} -match glob -result {?*} |
| 58 |
test cmdMZ-1.4 {Tcl_PwdObjCmd: failure} -setup { |
| 59 |
set cwd [pwd] |
| 60 |
set foodir [file join [temporaryDirectory] foo] |
| 61 |
file delete -force $foodir |
| 62 |
file mkdir $foodir |
| 63 |
cd $foodir |
| 64 |
} -constraints {unix nonPortable} -body { |
| 65 |
# This test fails on various Unix platforms (eg Linux) where permissions |
| 66 |
# caching causes this to fail. The caching is strictly incorrect, but we |
| 67 |
# have no control over that. |
| 68 |
file attr . -permissions 0o000 |
| 69 |
pwd |
| 70 |
} -returnCodes error -cleanup { |
| 71 |
cd $cwd |
| 72 |
file delete -force $foodir |
| 73 |
} -result {error getting working directory name: permission denied} |
| 74 |
|
| 75 |
# The tests for Tcl_RegexpObjCmd, Tcl_RegsubObjCmd are in regexp.test |
| 76 |
|
| 77 |
# Tcl_RenameObjCmd |
| 78 |
|
| 79 |
test cmdMZ-2.1 {Tcl_RenameObjCmd: error conditions} -returnCodes error -body { |
| 80 |
rename r1 |
| 81 |
} -result {wrong # args: should be "rename oldName newName"} |
| 82 |
test cmdMZ-2.2 {Tcl_RenameObjCmd: error conditions} -returnCodes error -body { |
| 83 |
rename r1 r2 r3 |
| 84 |
} -result {wrong # args: should be "rename oldName newName"} |
| 85 |
test cmdMZ-2.3 {Tcl_RenameObjCmd: success} -setup { |
| 86 |
catch {rename r2 {}} |
| 87 |
} -body { |
| 88 |
proc r1 {} {return "r1"} |
| 89 |
rename r1 r2 |
| 90 |
r2 |
| 91 |
} -result {r1} |
| 92 |
test cmdMZ-2.4 {Tcl_RenameObjCmd: success} { |
| 93 |
proc r1 {} {return "r1"} |
| 94 |
rename r1 {} |
| 95 |
list [catch {r1} msg] $msg |
| 96 |
} {1 {invalid command name "r1"}} |
| 97 |
|
| 98 |
# Some tests for Tcl_ReturnObjCmd are in proc-old.test |
| 99 |
|
| 100 |
test cmdMZ-return-1.0 {return checks for bad option values} -body { |
| 101 |
return -options foo |
| 102 |
} -returnCodes error -match glob -result {bad -options value:*} |
| 103 |
test cmdMZ-return-1.1 {return checks for bad option values} -body { |
| 104 |
return -code err |
| 105 |
} -returnCodes error -match glob -result {bad completion code "err": must be ok, error, return, break, continue*, or an integer} |
| 106 |
test cmdMZ-return-1.2 {return checks for bad option values} -body { |
| 107 |
return -code 0x100000000 |
| 108 |
} -returnCodes error -match glob -result {bad completion code "0x100000000": must be ok, error, return, break, continue*, or an integer} |
| 109 |
test cmdMZ-return-1.3 {return checks for bad option values} -body { |
| 110 |
return -level foo |
| 111 |
} -returnCodes error -match glob -result {bad -level value: *} |
| 112 |
test cmdMZ-return-1.4 {return checks for bad option values} -body { |
| 113 |
return -level -1 |
| 114 |
} -returnCodes error -match glob -result {bad -level value: *} |
| 115 |
test cmdMZ-return-1.5 {return checks for bad option values} -body { |
| 116 |
return -level 3.1415926 |
| 117 |
} -returnCodes error -match glob -result {bad -level value: *} |
| 118 |
|
| 119 |
proc dictSort {d} { |
| 120 |
set result {} |
| 121 |
foreach k [lsort [dict keys $d]] { |
| 122 |
dict set result $k [dict get $d $k] |
| 123 |
} |
| 124 |
return $result |
| 125 |
} |
| 126 |
|
| 127 |
test cmdMZ-return-2.0 {return option handling} { |
| 128 |
list [catch return -> foo] [dictSort $foo] |
| 129 |
} {2 {-code 0 -level 1}} |
| 130 |
test cmdMZ-return-2.1 {return option handling} { |
| 131 |
list [catch {return -bar soom} -> foo] [dictSort $foo] |
| 132 |
} {2 {-bar soom -code 0 -level 1}} |
| 133 |
test cmdMZ-return-2.2 {return option handling} { |
| 134 |
list [catch {return -code return} -> foo] [dictSort $foo] |
| 135 |
} {2 {-code 0 -level 2}} |
| 136 |
test cmdMZ-return-2.3 {return option handling} { |
| 137 |
list [catch {return -code return -level 10} -> foo] [dictSort $foo] |
| 138 |
} {2 {-code 0 -level 11}} |
| 139 |
test cmdMZ-return-2.4 {return option handling} -body { |
| 140 |
return -level 0 -code error |
| 141 |
} -returnCodes error -result {} |
| 142 |
test cmdMZ-return-2.5 {return option handling} -body { |
| 143 |
return -level 0 -code return |
| 144 |
} -returnCodes return -result {} |
| 145 |
test cmdMZ-return-2.6 {return option handling} -body { |
| 146 |
return -level 0 -code break |
| 147 |
} -returnCodes break -result {} |
| 148 |
test cmdMZ-return-2.7 {return option handling} -body { |
| 149 |
return -level 0 -code continue |
| 150 |
} -returnCodes continue -result {} |
| 151 |
test cmdMZ-return-2.8 {return option handling} -body { |
| 152 |
return -level 0 -code -1 |
| 153 |
} -returnCodes -1 -result {} |
| 154 |
test cmdMZ-return-2.9 {return option handling} -body { |
| 155 |
return -level 0 -code 10 |
| 156 |
} -returnCodes 10 -result {} |
| 157 |
test cmdMZ-return-2.10 {return option handling} -body { |
| 158 |
list [catch {return -level 0 -code error} -> foo] [dictSort $foo] |
| 159 |
} -match glob -result {1 {-code 1 -errorcode NONE -errorinfo { |
| 160 |
while executing |
| 161 |
"return -level 0 -code error"} -errorline 1 -errorstack * -level 0}} |
| 162 |
test cmdMZ-return-2.11 {return option handling} { |
| 163 |
list [catch {return -level 0 -code break} -> foo] [dictSort $foo] |
| 164 |
} {3 {-code 3 -level 0}} |
| 165 |
test cmdMZ-return-2.12 {return option handling} -body { |
| 166 |
return -level 0 -code error -options {-code ok} |
| 167 |
} -returnCodes ok -result {} |
| 168 |
test cmdMZ-return-2.13 {return option handling} -body { |
| 169 |
return -level 0 -code error -options {-code err} |
| 170 |
} -returnCodes error -match glob -result {bad completion code "err": must be ok, error, return, break, continue*, or an integer} |
| 171 |
test cmdMZ-return-2.14 {return option handling} -body { |
| 172 |
return -level 0 -code error -options {-code foo -options {-code break}} |
| 173 |
} -returnCodes break -result {} |
| 174 |
test cmdMZ-return-2.15 {return opton handling} { |
| 175 |
list [catch { |
| 176 |
apply {{} { |
| 177 |
return -code error -errorcode {a b} c |
| 178 |
}} |
| 179 |
} result] $result $::errorCode |
| 180 |
} {1 c {a b}} |
| 181 |
test cmdMZ-return-2.16 {return opton handling} { |
| 182 |
list [catch { |
| 183 |
apply {{} { |
| 184 |
return -code error -errorcode [list a b] c |
| 185 |
}} |
| 186 |
} result] $result $::errorCode |
| 187 |
} {1 c {a b}} |
| 188 |
test cmdMZ-return-2.17 {return opton handling} { |
| 189 |
list [catch { |
| 190 |
apply {{} { |
| 191 |
return -code error -errorcode a\ b c |
| 192 |
}} |
| 193 |
} result] $result $::errorCode |
| 194 |
} {1 c {a b}} |
| 195 |
test cmdMZ-return-2.18 {return option handling} { |
| 196 |
list [catch { |
| 197 |
return -code error -errorstack [list CALL a CALL b] yo |
| 198 |
} -> foo] [dictSort $foo] [info errorstack] |
| 199 |
} {2 {-code 1 -errorcode NONE -errorstack {CALL a CALL b} -level 1} {CALL a CALL b}} |
| 200 |
test cmdMZ-return-2.19 {return option handling} -body { |
| 201 |
return -level 0 -options {-options {-code break} -code continue} |
| 202 |
} -returnCodes continue -result {} |
| 203 |
test cmdMZ-return-2.20 {return option handling} { |
| 204 |
list [catch { |
| 205 |
return -level 0 -options {-foo 1} -options {-bar 2} |
| 206 |
} -> foo] $foo |
| 207 |
} {0 {-foo 1 -bar 2 -code 0 -level 0}} |
| 208 |
test cmdMZ-return-2.21 {return option handling} { |
| 209 |
list [catch { |
| 210 |
return -level 0 -options {-options {-foo 1} -options {-bar 2}} |
| 211 |
} -> foo] $foo |
| 212 |
} {0 {-foo 1 -bar 2 -code 0 -level 0}} |
| 213 |
|
| 214 |
# Check that the result of a [return -options $opts $result] is |
| 215 |
# indistinguishable from that of the originally caught script, no matter what |
| 216 |
# the script is/does. (TIP 90) |
| 217 |
foreach {testid script} { |
| 218 |
cmdMZ-return-3.0 {} |
| 219 |
cmdMZ-return-3.1 {format x} |
| 220 |
cmdMZ-return-3.2 {set} |
| 221 |
cmdMZ-return-3.3 {set a 1} |
| 222 |
cmdMZ-return-3.4 {error} |
| 223 |
cmdMZ-return-3.5 {error foo} |
| 224 |
cmdMZ-return-3.6 {error foo bar} |
| 225 |
cmdMZ-return-3.7 {error foo bar baz} |
| 226 |
cmdMZ-return-3.8 {return -level 0} |
| 227 |
cmdMZ-return-3.9 {return -code error} |
| 228 |
cmdMZ-return-3.10 {return -code error -errorinfo foo} |
| 229 |
cmdMZ-return-3.11 {return -code error -errorinfo foo -errorcode bar} |
| 230 |
cmdMZ-return-3.12 {return -code error -errorinfo foo -errorcode bar -errorline 10} |
| 231 |
cmdMZ-return-3.12.1 {return -code error -errorinfo foo -errorcode bar -errorline 10 -errorstack baz} |
| 232 |
cmdMZ-return-3.13 {return -options {x y z 2}} |
| 233 |
cmdMZ-return-3.14 {return -level 3 -code break sdf} |
| 234 |
} { |
| 235 |
test $testid "check that return after a catch is same:\n$script" { |
| 236 |
set one [list [catch $script foo bar] $foo [dictSort $bar] \ |
| 237 |
$::errorCode $::errorInfo] |
| 238 |
set two [list [catch {return -options $bar $foo} foo2 bar2] \ |
| 239 |
$foo2 [dictSort $bar2] $::errorCode $::errorInfo] |
| 240 |
string equal $one $two |
| 241 |
} 1 |
| 242 |
} |
| 243 |
|
| 244 |
# The tests for Tcl_ScanObjCmd are in scan.test |
| 245 |
|
| 246 |
# Tcl_SourceObjCmd |
| 247 |
# More tests of Tcl_SourceObjCmd are in source.test |
| 248 |
|
| 249 |
test cmdMZ-3.3 {Tcl_SourceObjCmd: error conditions} -constraints { |
| 250 |
unixOrWin |
| 251 |
} -returnCodes error -body { |
| 252 |
source |
| 253 |
} -match glob -result {wrong # args: should be "source*fileName"} |
| 254 |
test cmdMZ-3.4 {Tcl_SourceObjCmd: error conditions} -constraints { |
| 255 |
unixOrWin |
| 256 |
} -returnCodes error -body { |
| 257 |
source a b |
| 258 |
} -match glob -result {wrong # args: should be "source*fileName"} |
| 259 |
test cmdMZ-3.5 {Tcl_SourceObjCmd: error in script} -body { |
| 260 |
set file [makeFile { |
| 261 |
set x 146 |
| 262 |
error "error in sourced file" |
| 263 |
set y $x |
| 264 |
} source.file] |
| 265 |
list [catch {source $file} msg] $msg $::errorInfo |
| 266 |
} -cleanup { |
| 267 |
removeFile source.file |
| 268 |
} -match listGlob -result {1 {error in sourced file} {error in sourced file |
| 269 |
while executing |
| 270 |
"error "error in sourced file"" |
| 271 |
(file "*" line 3) |
| 272 |
invoked from within |
| 273 |
"source $file"}} |
| 274 |
test cmdMZ-3.6 {Tcl_SourceObjCmd: simple script} -body { |
| 275 |
set file [makeFile {list ok} source.file] |
| 276 |
source $file |
| 277 |
} -cleanup { |
| 278 |
removeFile source.file |
| 279 |
} -result ok |
| 280 |
|
| 281 |
# Tcl_SplitObjCmd |
| 282 |
|
| 283 |
test cmdMZ-4.1 {Tcl_SplitObjCmd: split errors} -returnCodes error -body { |
| 284 |
split |
| 285 |
} -result {wrong # args: should be "split string ?splitChars?"} |
| 286 |
test cmdMZ-4.2 {Tcl_SplitObjCmd: split errors} -returnCodes error -body { |
| 287 |
split a b c |
| 288 |
} -result {wrong # args: should be "split string ?splitChars?"} |
| 289 |
test cmdMZ-4.3 {Tcl_SplitObjCmd: basic split commands} { |
| 290 |
split "a\n b\t\r c\n " |
| 291 |
} {a {} b {} {} c {} {}} |
| 292 |
test cmdMZ-4.4 {Tcl_SplitObjCmd: basic split commands} { |
| 293 |
split "word 1xyzword 2zword 3" xyz |
| 294 |
} {{word 1} {} {} {word 2} {word 3}} |
| 295 |
test cmdMZ-4.5 {Tcl_SplitObjCmd: basic split commands} { |
| 296 |
split "12345" {} |
| 297 |
} {1 2 3 4 5} |
| 298 |
test cmdMZ-4.6 {Tcl_SplitObjCmd: basic split commands} { |
| 299 |
split "a\}b\[c\{\]\$" |
| 300 |
} "a\\\}b\\\[c\\\{\\\]\\\$" |
| 301 |
test cmdMZ-4.7 {Tcl_SplitObjCmd: basic split commands} { |
| 302 |
split {} {} |
| 303 |
} {} |
| 304 |
test cmdMZ-4.8 {Tcl_SplitObjCmd: basic split commands} { |
| 305 |
split {} |
| 306 |
} {} |
| 307 |
test cmdMZ-4.9 {Tcl_SplitObjCmd: basic split commands} { |
| 308 |
split { } |
| 309 |
} {{} {} {} {}} |
| 310 |
test cmdMZ-4.10 {Tcl_SplitObjCmd: basic split commands} { |
| 311 |
apply {{} { |
| 312 |
set x {} |
| 313 |
foreach f [split {]\n} {}] { |
| 314 |
append x $f |
| 315 |
} |
| 316 |
return $x |
| 317 |
}} |
| 318 |
} {]\n} |
| 319 |
test cmdMZ-4.11 {Tcl_SplitObjCmd: basic split commands} { |
| 320 |
apply {{} { |
| 321 |
set x ab\000c |
| 322 |
set y [split $x {}] |
| 323 |
binary scan $y c* z |
| 324 |
return $z |
| 325 |
}} |
| 326 |
} {97 32 98 32 0 32 99} |
| 327 |
test cmdMZ-4.12 {Tcl_SplitObjCmd: basic split commands} { |
| 328 |
split "a0ab1b2bbb3\000c4" ab\000c |
| 329 |
} {{} 0 {} 1 2 {} {} 3 {} 4} |
| 330 |
test cmdMZ-4.13 {Tcl_SplitObjCmd: basic split commands} { |
| 331 |
# if not UTF-8 aware, result is "a {} {} b qw\xe5 {} N wq" |
| 332 |
split "a\u4e4eb qw\u5e4e\x4e wq" " \u4e4e" |
| 333 |
} "a b qw\u5e4eN wq" |
| 334 |
|
| 335 |
# The tests for Tcl_StringObjCmd are in string.test |
| 336 |
# The tests for Tcl_SubstObjCmd are in subst.test |
| 337 |
# The tests for Tcl_SwitchObjCmd are in switch.test |
| 338 |
|
| 339 |
# todo: rewrite this if monotonic clock is provided resp. command "after" |
| 340 |
# gets microsecond accuracy (RFE [fdfbd5e10] gets merged): |
| 341 |
proc _nrt_sleep {msec} { |
| 342 |
set stime [clock microseconds] |
| 343 |
set usec [expr {$msec * 1000}] |
| 344 |
set etime [expr {$stime + $usec}] |
| 345 |
while {[set tm [clock microseconds]] < $etime} { |
| 346 |
# don't use after 0 unless it's NRT-capable, so yes - busy-wait (but it's more precise): |
| 347 |
# after 0 |
| 348 |
if {$tm < $stime} { # avoid too long delays by backwards time jumps, simply skip test |
| 349 |
tcltest::Skip "time-jump?" |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
_nrt_sleep 0; # warm up (clock, compile, etc) |
| 354 |
|
| 355 |
test cmdMZ-5.1 {Tcl_TimeObjCmd: basic format of command} -body { |
| 356 |
time |
| 357 |
} -returnCodes error -result {wrong # args: should be "time command ?count?"} |
| 358 |
test cmdMZ-5.2 {Tcl_TimeObjCmd: basic format of command} -body { |
| 359 |
time a b c |
| 360 |
} -returnCodes error -result {wrong # args: should be "time command ?count?"} |
| 361 |
test cmdMZ-5.3 {Tcl_TimeObjCmd: basic format of command} -body { |
| 362 |
time a b |
| 363 |
} -returnCodes error -result {expected integer but got "b"} |
| 364 |
test cmdMZ-5.4 {Tcl_TimeObjCmd: nothing happens with negative iteration counts} { |
| 365 |
time bogusCmd -12456 |
| 366 |
} {0 microseconds per iteration} |
| 367 |
test cmdMZ-5.5 {Tcl_TimeObjCmd: result format} -body { |
| 368 |
time {format 1} |
| 369 |
} -match regexp -result {^\d+ microseconds per iteration} |
| 370 |
test cmdMZ-5.6 {Tcl_TimeObjCmd: slower commands take longer} -body { |
| 371 |
set m1 [lindex [time {_nrt_sleep 0.01}] 0] |
| 372 |
set m2 [lindex [time {_nrt_sleep 10.0}] 0] |
| 373 |
list \ |
| 374 |
[expr {$m1 < $m2}] \ |
| 375 |
$m1 $m2; # interesting only in error case. |
| 376 |
} -match glob -result [list 1 *] |
| 377 |
test cmdMZ-5.7 {Tcl_TimeObjCmd: errors generate right trace} { |
| 378 |
list [catch {time {error foo}} msg] $msg $::errorInfo |
| 379 |
} {1 foo {foo |
| 380 |
while executing |
| 381 |
"error foo" |
| 382 |
invoked from within |
| 383 |
"time {error foo}"}} |
| 384 |
test cmdMZ-5.7.1 {Tcl_TimeObjCmd: return from time} { |
| 385 |
set x 0 |
| 386 |
proc r1 {} {upvar x x; time {incr x; return "r1"; incr x} 10} |
| 387 |
list [r1] $x |
| 388 |
} {r1 1} |
| 389 |
test cmdMZ-5.8 {Tcl_TimeObjCmd: done optimization: nested call of self inside time (if compiled)} { |
| 390 |
set x [set y 0] |
| 391 |
set m1 { |
| 392 |
if {[incr x] <= 5} { |
| 393 |
# nested call should return result, so covering that: |
| 394 |
if {![string is integer -strict [eval $m1]]} {error unexpected} |
| 395 |
} |
| 396 |
# increase again (no "continue" from nested call): |
| 397 |
incr x |
| 398 |
} |
| 399 |
time {incr y; eval $m1} 5 |
| 400 |
list $y $x |
| 401 |
} {5 20} |
| 402 |
|
| 403 |
test cmdMZ-6.1 {Tcl_TimeRateObjCmd: basic format of command} { |
| 404 |
list [catch {timerate} msg] $msg |
| 405 |
} {1 {wrong # args: should be "timerate ?-direct? ?-calibrate? ?-overhead double? command ?time ?max-count??"}} |
| 406 |
test cmdMZ-6.2.1 {Tcl_TimeRateObjCmd: basic format of command} { |
| 407 |
list [catch {timerate a b c d} msg] $msg |
| 408 |
} {1 {wrong # args: should be "timerate ?-direct? ?-calibrate? ?-overhead double? command ?time ?max-count??"}} |
| 409 |
test cmdMZ-6.2.2 {Tcl_TimeRateObjCmd: basic format of command} { |
| 410 |
list [catch {timerate a b c} msg] $msg |
| 411 |
} {1 {expected integer but got "b"}} |
| 412 |
test cmdMZ-6.2.3 {Tcl_TimeRateObjCmd: basic format of command} { |
| 413 |
list [catch {timerate a b} msg] $msg |
| 414 |
} {1 {expected integer but got "b"}} |
| 415 |
test cmdMZ-6.3 {Tcl_TimeRateObjCmd: basic format of command} { |
| 416 |
list [catch {timerate -overhead b {} a b} msg] $msg |
| 417 |
} {1 {expected floating-point number but got "b"}} |
| 418 |
test cmdMZ-6.4 {Tcl_TimeRateObjCmd: compile of script happens even with negative iteration counts} { |
| 419 |
list [catch {timerate "foreach a {c d e} \{" -12456} msg] $msg |
| 420 |
} {1 {missing close-brace}} |
| 421 |
test cmdMZ-6.5a {Tcl_TimeRateObjCmd: result format and one iteration} { |
| 422 |
regexp {^\d+(?:\.\d+)? \ws/# 1 # \d+(?:\.\d+)? #/sec \d+(?:\.\d+)? net-ms$} [timerate {} 0] |
| 423 |
} 1 |
| 424 |
test cmdMZ-6.5b {Tcl_TimeRateObjCmd: result format without iterations} { |
| 425 |
regexp {^0 \ws/# 0 # 0 #/sec 0 net-ms$} [timerate {} 0 0] |
| 426 |
} 1 |
| 427 |
test cmdMZ-6.6 {Tcl_TimeRateObjCmd: slower commands take longer, but it remains almost the same time of measurement} -body { |
| 428 |
set m1 [timerate {_nrt_sleep 0.01} 50] |
| 429 |
set m2 [timerate {_nrt_sleep 1.00} 50] |
| 430 |
if {[testConstraint valgrind] && ([lindex $m1 0] >= 100 || [lindex $m1 2] <= 500)} { |
| 431 |
tcltest::Skip "too-slow-by-valgrind" |
| 432 |
} |
| 433 |
list [list \ |
| 434 |
[expr {[lindex $m1 0] < [lindex $m2 0]}] \ |
| 435 |
[expr {[lindex $m1 0] < 100}] \ |
| 436 |
[expr {[lindex $m2 0] > 100}] \ |
| 437 |
[expr {[lindex $m1 2] > 500}] \ |
| 438 |
[expr {[lindex $m2 2] < 500}] \ |
| 439 |
[expr {[lindex $m1 4] > 10000}] \ |
| 440 |
[expr {[lindex $m2 4] < 10000}] \ |
| 441 |
[expr {[lindex $m1 6] > 5 && [lindex $m1 6] < 100}] \ |
| 442 |
[expr {[lindex $m2 6] > 5 && [lindex $m2 6] < 100}] \ |
| 443 |
] $m1 $m2; # interesting only in error case. |
| 444 |
} -match glob -result [list [lrepeat 9 1] *] |
| 445 |
test cmdMZ-6.7 {Tcl_TimeRateObjCmd: errors generate right trace} { |
| 446 |
list [catch {timerate {error foo} 1} msg] $msg $::errorInfo |
| 447 |
} {1 foo {foo |
| 448 |
while executing |
| 449 |
"error foo" |
| 450 |
invoked from within |
| 451 |
"timerate {error foo} 1"}} |
| 452 |
test cmdMZ-6.7.1 {Tcl_TimeRateObjCmd: return from timerate} { |
| 453 |
set x 0 |
| 454 |
proc r1 {} {upvar x x; timerate {incr x; return "r1"; incr x} 1000 10} |
| 455 |
list [r1] $x |
| 456 |
} {r1 1} |
| 457 |
test cmdMZ-6.8 {Tcl_TimeRateObjCmd: allow (conditional) break from timerate} -body { |
| 458 |
set m1 [timerate {break}] |
| 459 |
list [list \ |
| 460 |
[expr {[lindex $m1 0] < 1000}] \ |
| 461 |
[expr {[lindex $m1 2] == 1}] \ |
| 462 |
[expr {[lindex $m1 4] > 1000}] \ |
| 463 |
[expr {[lindex $m1 6] < 10}] \ |
| 464 |
] $m1; # interesting only in error case. |
| 465 |
} -match glob -result [list {1 1 1 1} *] |
| 466 |
test cmdMZ-6.8.1 {Tcl_TimeRateObjCmd: allow (conditional) continue in timerate} -body { |
| 467 |
set m1 [timerate {continue; return -code error "unexpected"} 1000 10] |
| 468 |
list [list \ |
| 469 |
[expr {[lindex $m1 0] < 1000}] \ |
| 470 |
[expr {[lindex $m1 2] == 10}] \ |
| 471 |
[expr {[lindex $m1 4] > 1000}] \ |
| 472 |
[expr {[lindex $m1 6] < 100}] \ |
| 473 |
] $m1; # interesting only in error case. |
| 474 |
} -match glob -result [list {1 1 1 1} *] |
| 475 |
test cmdMZ-6.9 {Tcl_TimeRateObjCmd: max count of iterations} { |
| 476 |
set m1 [timerate {} 1000 5]; # max-count wins |
| 477 |
set m2 [timerate {_nrt_sleep 20} 1 5]; # max-time wins |
| 478 |
list [lindex $m1 2] [lindex $m2 2] |
| 479 |
} {5 1} |
| 480 |
test cmdMZ-6.10 {Tcl_TimeRateObjCmd: huge overhead cause 0us result} -body { |
| 481 |
set m1 [timerate -overhead 1e6 {_nrt_sleep 10} 100 1] |
| 482 |
list [list \ |
| 483 |
[expr {[lindex $m1 0] == 0.0}] \ |
| 484 |
[expr {[lindex $m1 2] == 1}] \ |
| 485 |
[expr {[lindex $m1 4] == 1000000}] \ |
| 486 |
[expr {[lindex $m1 6] <= 0.001}] \ |
| 487 |
] $m1; # interesting only in error case. |
| 488 |
} -match glob -result [list {1 1 1 1} *] |
| 489 |
test cmdMZ-6.11 {Tcl_TimeRateObjCmd: done/continue optimization rollback} { |
| 490 |
set m1 {set m2 ok} |
| 491 |
if 1 $m1 |
| 492 |
timerate $m1 1000 10 |
| 493 |
if 1 $m1; # if rollback is missing throws an error: invoked "continue" outside of a loop |
| 494 |
} ok |
| 495 |
test cmdMZ-6.12 {Tcl_TimeRateObjCmd: done optimization: nested call of self inside timerate} { |
| 496 |
set x 0 |
| 497 |
set m1 { |
| 498 |
if {[incr x] <= 5} { |
| 499 |
# nested call should return result, so covering that: |
| 500 |
if {![string is integer -strict [eval $m1]]} {error unexpected} |
| 501 |
} |
| 502 |
# increase again (no "continue" from nested call): |
| 503 |
incr x |
| 504 |
} |
| 505 |
list [lindex [timerate $m1 1000 5] 2] $x |
| 506 |
} {5 20} |
| 507 |
test cmdMZ-6.13 {Tcl_TimeRateObjCmd: stability by O(n**2), avoid long execution time on growing iteration time} {!memory} { |
| 508 |
# don't run this test on memory-debug builds - it seems to be very time-consuming there, |
| 509 |
# what may led to an unexpectedly high increase by such complexity and it'd fail... |
| 510 |
set result {} |
| 511 |
# test the function with quadratic complexity (iteration growth 2x, 10x, 100x): |
| 512 |
foreach e {2 10 100} { |
| 513 |
set x 1 |
| 514 |
set m1 [timerate { |
| 515 |
apply {x { |
| 516 |
while {[incr x -1]} {} |
| 517 |
}} [set x [expr {$x*$e}]] |
| 518 |
} 50] |
| 519 |
lappend result "${e}x" |
| 520 |
# check it was too slow (it is OK to use factor 10 to prevent sporadic |
| 521 |
# errors on some slow systems or timing issues, because if it is not fixed, |
| 522 |
# the execution time may grow hundreds and thousand times): |
| 523 |
if {[lindex $m1 6] > 50 * 10} { |
| 524 |
lappend result "unexpected long: $m1" |
| 525 |
} |
| 526 |
} |
| 527 |
set result |
| 528 |
} {2x 10x 100x} |
| 529 |
|
| 530 |
# The tests for Tcl_WhileObjCmd are in while.test |
| 531 |
|
| 532 |
# cleanup |
| 533 |
cleanupTests |
| 534 |
} |
| 535 |
namespace delete ::tcl::test::cmdMZ |
| 536 |
return |
| 537 |
|
| 538 |
# Local Variables: |
| 539 |
# mode: tcl |
| 540 |
# End: |