diff options
Diffstat (limited to 'tests/cmdIL.test')
-rw-r--r-- | tests/cmdIL.test | 490 |
1 files changed, 457 insertions, 33 deletions
diff --git a/tests/cmdIL.test b/tests/cmdIL.test index 1f0665a..b387e71 100644 --- a/tests/cmdIL.test +++ b/tests/cmdIL.test @@ -9,16 +9,19 @@ # of this file, and for a DISCLAIMER OF ALL WARRANTIES. if {[lsearch [namespace children] ::tcltest] == -1} { - package require tcltest + package require tcltest 2 namespace import -force ::tcltest::* } +# Used for constraining memory leak tests +testConstraint memory [llength [info commands memory]] + test cmdIL-1.1 {Tcl_LsortObjCmd procedure} { list [catch {lsort} msg] $msg } {1 {wrong # args: should be "lsort ?options? list"}} test cmdIL-1.2 {Tcl_LsortObjCmd procedure} { list [catch {lsort -foo {1 3 2 5}} msg] $msg -} {1 {bad option "-foo": must be -ascii, -command, -decreasing, -dictionary, -increasing, -index, -integer, -real, or -unique}} +} {1 {bad option "-foo": must be -ascii, -command, -decreasing, -dictionary, -increasing, -index, -indices, -integer, -nocase, -real, or -unique}} test cmdIL-1.3 {Tcl_LsortObjCmd procedure, default options} { lsort {d e c b a \{ d35 d300} } {a b c d d300 d35 e \{} @@ -28,12 +31,15 @@ test cmdIL-1.4 {Tcl_LsortObjCmd procedure, -ascii option} { test cmdIL-1.5 {Tcl_LsortObjCmd procedure, -command option} { list [catch {lsort -command {1 3 2 5}} msg] $msg } {1 {"-command" option must be followed by comparison command}} -test cmdIL-1.6 {Tcl_LsortObjCmd procedure, -command option} { +test cmdIL-1.6 {Tcl_LsortObjCmd procedure, -command option} -setup { proc cmp {a b} { expr {[string match x* $b] - [string match x* $a]} } +} -body { lsort -command cmp {x1 abc x2 def x3 x4} -} {x1 x2 x3 x4 abc def} +} -result {x1 x2 x3 x4 abc def} -cleanup { + rename cmp "" +} test cmdIL-1.7 {Tcl_LsortObjCmd procedure, -decreasing option} { lsort -decreasing {d e c b a d35 d300} } {e d35 d300 d c b a} @@ -51,7 +57,7 @@ test cmdIL-1.11 {Tcl_LsortObjCmd procedure, -index option} { } {1 {"-index" option must be followed by list index}} test cmdIL-1.12 {Tcl_LsortObjCmd procedure, -index option} { list [catch {lsort -index foo {1 3 2 5}} msg] $msg -} {1 {bad index "foo": must be integer or end?-integer?}} +} {1 {bad index "foo": must be integer?[+-]integer? or end?[+-]integer?}} test cmdIL-1.13 {Tcl_LsortObjCmd procedure, -index option} { lsort -index end -integer {{2 25} {10 20 50 100} {3 16 42} 1} } {1 {2 25} {3 16 42} {10 20 50 100}} @@ -80,44 +86,52 @@ test cmdIL-1.23 {Tcl_LsortObjCmd procedure, unique sort with index} { # lsort -unique should return the last unique item lsort -unique -index 0 {{a b} {c b} {a c} {d a}} } {{a c} {c b} {d a}} -test cmdIL-1.24 {Tcl_LsortObjCmd procedure, order of -index and -command} { +test cmdIL-1.24 {Tcl_LsortObjCmd procedure, order of -index and -command} -setup { catch {rename 1 ""} proc testcmp {a b} {return [string compare $a $b]} +} -body { set l [list [list a b] [list c d]] - set result [list [catch {lsort -command testcmp -index 1 $l} msg] $msg] + list [catch {lsort -command testcmp -index 1 $l} msg] $msg +} -cleanup { rename testcmp "" - set result -} [list 0 [list [list a b] [list c d]]] -test cmdIL-1.25 {Tcl_LsortObjCmd procedure, order of -index and -command} { +} -result [list 0 [list [list a b] [list c d]]] +test cmdIL-1.25 {Tcl_LsortObjCmd procedure, order of -index and -command} -setup { catch {rename 1 ""} proc testcmp {a b} {return [string compare $a $b]} +} -body { set l [list [list a b] [list c d]] - set result [list [catch {lsort -index 1 -command testcmp $l} msg] $msg] + list [catch {lsort -index 1 -command testcmp $l} msg] $msg +} -cleanup { rename testcmp "" - set result -} [list 0 [list [list a b] [list c d]]] +} -result [list 0 [list [list a b] [list c d]]] # Note that the required order only exists in the end-1'th element; # indexing using the end element or any fixed offset from the start # will not work... test cmdIL-1.26 {Tcl_LsortObjCmd procedure, offset indexing from end} { lsort -index end-1 {{a 1 e i} {b 2 3 f g} {c 4 5 6 d h}} } {{c 4 5 6 d h} {a 1 e i} {b 2 3 f g}} +test cmdIL-1.27 {Tcl_LsortObjCmd procedure, returning indices} { + lsort -indices {a c b} +} {0 2 1} +test cmdIL-1.28 {Tcl_LsortObjCmd procedure, returning indices} { + lsort -indices -unique -decreasing -real {1.2 34.5 34.5 5.6} +} {2 3 0} test cmdIL-1.29 {Tcl_LsortObjCmd procedure, loss of list rep during sorting} { set l {1 2 3} - proc testcmp args {string length $::l} - string length [lsort -command testcmp $l] + string length [lsort -command {apply {args {string length $::l}}} $l] } 5 # Can't think of any good tests for the MergeSort and MergeLists # procedures, except a bunch of random lists to sort. -test cmdIL-2.1 {MergeSort and MergeLists procedures} { +test cmdIL-2.1 {MergeSort and MergeLists procedures} -setup { set result {} set r 1435753299 proc rand {} { global r set r [expr {(16807 * $r) % (0x7fffffff)}] } +} -body { for {set i 0} {$i < 150} {incr i} { set x {} for {set j 0} {$j < $i} {incr j} { @@ -134,18 +148,23 @@ test cmdIL-2.1 {MergeSort and MergeLists procedures} { } } set result -} {} +} -cleanup { + rename rand "" +} -result {} -test cmdIL-3.1 {SortCompare procedure, skip comparisons after error} { - set x 0 +test cmdIL-3.1 {SortCompare procedure, skip comparisons after error} -setup { proc cmp {a b} { global x incr x error "error #$x" } +} -body { + set x 0 list [catch {lsort -integer -command cmp {48 6 28 190 16 2 3 6 1}} msg] \ $msg $x -} {1 {error #1} 1} +} -cleanup { + rename cmp "" +} -result {1 {error #1} 1} test cmdIL-3.2 {SortCompare procedure, -index option} { list [catch {lsort -integer -index 2 "\\\{ {30 40 50}"} msg] $msg } {1 {unmatched open brace in list}} @@ -154,6 +173,9 @@ test cmdIL-3.3 {SortCompare procedure, -index option} { } {1 {element 2 missing from sublist "20 10"}} test cmdIL-3.4 {SortCompare procedure, -index option} { list [catch {lsort -integer -index 2 "{a b c} \\\{"} msg] $msg +} {1 {expected integer but got "c"}} +test cmdIL-3.4.1 {SortCompare procedure, -index option} { + list [catch {lsort -integer -index 2 "{1 2 3} \\\{"} msg] $msg } {1 {unmatched open brace in list}} test cmdIL-3.5 {SortCompare procedure, -index option} { list [catch {lsort -integer -index 2 {{20 10 13} {15}}} msg] $msg @@ -174,8 +196,8 @@ test cmdIL-3.10 {SortCompare procedure, -integer option} { list [catch {lsort -integer {3 q}} msg] $msg } {1 {expected integer but got "q"}} test cmdIL-3.11 {SortCompare procedure, -integer option} { - lsort -integer {35 21 0x20 30 023 100 8} -} {8 023 21 30 0x20 35 100} + lsort -integer {35 21 0x20 30 0o23 100 8} +} {8 0o23 21 30 0x20 35 100} test cmdIL-3.12 {SortCompare procedure, -real option} { list [catch {lsort -real {6...4 3}} msg] $msg } {1 {expected floating-point number but got "6...4"}} @@ -185,12 +207,14 @@ test cmdIL-3.13 {SortCompare procedure, -real option} { test cmdIL-3.14 {SortCompare procedure, -real option} { lsort -real {24 2.5e01 16.7 85e-1 10.004} } {85e-1 10.004 16.7 24 2.5e01} -test cmdIL-3.15 {SortCompare procedure, -command option} { +test cmdIL-3.15 {SortCompare procedure, -command option} -body { proc cmp {a b} { error "comparison error" } - list [catch {lsort -command cmp {48 6}} msg] $msg $errorInfo -} {1 {comparison error} {comparison error + list [catch {lsort -command cmp {48 6}} msg] $msg $::errorInfo +} -cleanup { + rename cmp "" +} -result {1 {comparison error} {comparison error while executing "error "comparison error"" (procedure "cmp" line 2) @@ -199,27 +223,33 @@ test cmdIL-3.15 {SortCompare procedure, -command option} { (-compare command) invoked from within "lsort -command cmp {48 6}"}} -test cmdIL-3.16 {SortCompare procedure, -command option, long command} { +test cmdIL-3.16 {SortCompare procedure, -command option, long command} -body { proc cmp {dummy a b} { string compare $a $b } lsort -command {cmp {this argument is very very long in order to make the dstring overflow its statically allocated space}} {{this first element is also long in order to help expand the dstring} {the second element, last but not least, is quite long also, in order to make absolutely sure that space is allocated dynamically for the dstring}} -} {{the second element, last but not least, is quite long also, in order to make absolutely sure that space is allocated dynamically for the dstring} {this first element is also long in order to help expand the dstring}} -test cmdIL-3.17 {SortCompare procedure, -command option, non-integer result} { +} -cleanup { + rename cmp "" +} -result {{the second element, last but not least, is quite long also, in order to make absolutely sure that space is allocated dynamically for the dstring} {this first element is also long in order to help expand the dstring}} +test cmdIL-3.17 {SortCompare procedure, -command option, non-integer result} -body { proc cmp {a b} { return foow } list [catch {lsort -command cmp {48 6}} msg] $msg -} {1 {-compare command returned non-integer result}} -test cmdIL-3.18 {SortCompare procedure, -command option} { +} -cleanup { + rename cmp "" +} -result {1 {-compare command returned non-integer result}} +test cmdIL-3.18 {SortCompare procedure, -command option} -body { proc cmp {a b} { expr {$b - $a} } lsort -command cmp {48 6 18 22 21 35 36} -} {48 36 35 22 21 18 6} +} -cleanup { + rename cmp "" +} -result {48 36 35 22 21 18 6} test cmdIL-3.19 {SortCompare procedure, -decreasing option} { - lsort -decreasing -integer {35 21 0x20 30 023 100 8} -} {100 35 0x20 30 21 023 8} + lsort -decreasing -integer {35 21 0x20 30 0o23 100 8} +} {100 35 0x20 30 21 0o23 8} test cmdIL-4.1 {DictionaryCompare procedure, numerics, leading zeros} { lsort -dictionary {a003b a03b} @@ -358,7 +388,401 @@ test cmdIL-4.32 {DictionaryCompare procedure, chars between Z and a in ASCII} { test cmdIL-4.33 {DictionaryCompare procedure, chars between Z and a in ASCII} { lsort -dictionary [list AA ! c CC `] } [list ! ` AA c CC] +test cmdIL-4.34 {SortCompare procedure, -ascii option with -nocase option} { + lsort -ascii -nocase {d e c b a d35 d300 100 20} +} {100 20 a b c d d300 d35 e} +test cmdIL-4.35 {SortCompare procedure, -ascii option with -nocase option} { + lsort -ascii -nocase {d E c B a D35 d300 100 20} +} {100 20 a B c d d300 D35 E} + +test cmdIL-5.1 {lsort with list style index} { + lsort -ascii -decreasing -index {0 1} { + {{Jim Alpha} 20000410} + {{Joe Bravo} 19990320} + {{Jacky Charlie} 19390911} + } +} {{{Jacky Charlie} 19390911} {{Joe Bravo} 19990320} {{Jim Alpha} 20000410}} +test cmdIL-5.2 {lsort with list style index} { + lsort -decreasing -index {0 1} { + {{Jim Alpha} 20000410} + {{Joe Bravo} 19990320} + {{Jacky Charlie} 19390911} + } +} {{{Jacky Charlie} 19390911} {{Joe Bravo} 19990320} {{Jim Alpha} 20000410}} +test cmdIL-5.3 {lsort with list style index} { + lsort -integer -increasing -index {1 end} { + {{Jim Alpha} 20000410} + {{Joe Bravo} 19990320} + {{Jacky Charlie} 19390911} + } +} {{{Jacky Charlie} 19390911} {{Joe Bravo} 19990320} {{Jim Alpha} 20000410}} +test cmdIL-5.4 {lsort with list style index} { + lsort -integer -index {1 end-1} { + {the {0 1 2 3 4 5} quick} + {brown {0 1 2 3 4} fox} + {jumps {30 31 2 33} over} + {the {0 1 2} lazy} + {dogs {0 1}} + } +} {{dogs {0 1}} {the {0 1 2} lazy} {jumps {30 31 2 33} over} {brown {0 1 2 3 4} fox} {the {0 1 2 3 4 5} quick}} +test cmdIL-5.5 {lsort with list style index and sharing} -body { + proc test_lsort {l} { + set n $l + foreach e $l {lappend n [list [expr {rand()}] $e]} + lindex [lsort -real -index $l $n] 1 1 + } + expr srand(1) + test_lsort 0 +} -result 0 -cleanup { + rename test_lsort "" +} + +# Compiled version +test cmdIL-6.1 {lassign command syntax} -body { + proc testLassign {} { + lassign + } + testLassign +} -returnCodes 1 -cleanup { + rename testLassign {} +} -result {wrong # args: should be "lassign list varName ?varName ...?"} +test cmdIL-6.2 {lassign command syntax} -body { + proc testLassign {} { + lassign x + } + testLassign +} -returnCodes 1 -cleanup { + rename testLassign {} +} -result {wrong # args: should be "lassign list varName ?varName ...?"} +test cmdIL-6.3 {lassign command} -body { + proc testLassign {} { + set x FAIL + list [lassign a x] $x + } + testLassign +} -result {{} a} -cleanup { + rename testLassign {} +} +test cmdIL-6.4 {lassign command} -body { + proc testLassign {} { + set x FAIL + set y FAIL + list [lassign a x y] $x $y + } + testLassign +} -result {{} a {}} -cleanup { + rename testLassign {} +} +test cmdIL-6.5 {lassign command} -body { + proc testLassign {} { + set x FAIL + set y FAIL + list [lassign {a b} x y] $x $y + } + testLassign +} -result {{} a b} -cleanup { + rename testLassign {} +} +test cmdIL-6.6 {lassign command} -body { + proc testLassign {} { + set x FAIL + set y FAIL + list [lassign {a b c} x y] $x $y + } + testLassign +} -result {c a b} -cleanup { + rename testLassign {} +} +test cmdIL-6.7 {lassign command} -body { + proc testLassign {} { + set x FAIL + set y FAIL + list [lassign {a b c d} x y] $x $y + } + testLassign +} -result {{c d} a b} -cleanup { + rename testLassign {} +} +test cmdIL-6.8 {lassign command - list format error} -body { + proc testLassign {} { + set x FAIL + set y FAIL + list [catch {lassign {a {b}c d} x y} msg] $msg $x $y + } + testLassign +} -result {1 {list element in braces followed by "c" instead of space} FAIL FAIL} -cleanup { + rename testLassign {} +} +test cmdIL-6.9 {lassign command - assignment to arrays} -body { + proc testLassign {} { + list [lassign {a b} x(x)] $x(x) + } + testLassign +} -result {b a} -cleanup { + rename testLassign {} +} +test cmdIL-6.10 {lassign command - variable update error} -body { + proc testLassign {} { + set x(x) {} + lassign a x + } + testLassign +} -returnCodes 1 -result {can't set "x": variable is array} -cleanup { + rename testLassign {} +} +test cmdIL-6.11 {lassign command - variable update error} -body { + proc testLassign {} { + set x(x) {} + set y FAIL + list [catch {lassign a y x} msg] $msg $y + } + testLassign +} -result {1 {can't set "x": variable is array} a} -cleanup { + rename testLassign {} +} +test cmdIL-6.12 {lassign command - memory leak testing} -setup { + unset -nocomplain x y + set x(x) {} + set y FAIL + proc getbytes {} { + set lines [split [memory info] "\n"] + lindex [lindex $lines 3] 3 + } + proc stress {} { + global x y + lassign {} y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + catch {lassign {} y y y y y y y y y y y y y y y y y y y y y y y y y x} + catch {lassign {} x} + } +} -constraints memory -body { + set end [getbytes] + for {set i 0} {$i < 5} {incr i} { + stress + set tmp $end + set end [getbytes] + } + expr {$end - $tmp} +} -result 0 -cleanup { + unset -nocomplain x y i tmp end + rename getbytes {} + rename stress {} +} +# Force non-compiled version +test cmdIL-6.13 {lassign command syntax} -body { + proc testLassign {} { + set lassign lassign + $lassign + } + testLassign +} -returnCodes 1 -cleanup { + rename testLassign {} +} -result {wrong # args: should be "lassign list varName ?varName ...?"} +test cmdIL-6.14 {lassign command syntax} -body { + proc testLassign {} { + set lassign lassign + $lassign x + } + testLassign +} -returnCodes 1 -cleanup { + rename testLassign {} +} -result {wrong # args: should be "lassign list varName ?varName ...?"} +test cmdIL-6.15 {lassign command} -body { + proc testLassign {} { + set lassign lassign + set x FAIL + list [$lassign a x] $x + } + testLassign +} -result {{} a} -cleanup { + rename testLassign {} +} +test cmdIL-6.16 {lassign command} -body { + proc testLassign {} { + set lassign lassign + set x FAIL + set y FAIL + list [$lassign a x y] $x $y + } + testLassign +} -result {{} a {}} -cleanup { + rename testLassign {} +} +test cmdIL-6.17 {lassign command} -body { + proc testLassign {} { + set lassign lassign + set x FAIL + set y FAIL + list [$lassign {a b} x y] $x $y + } + testLassign +} -result {{} a b} -cleanup { + rename testLassign {} +} +test cmdIL-6.18 {lassign command} -body { + proc testLassign {} { + set lassign lassign + set x FAIL + set y FAIL + list [$lassign {a b c} x y] $x $y + } + testLassign +} -result {c a b} -cleanup { + rename testLassign {} +} +test cmdIL-6.19 {lassign command} -body { + proc testLassign {} { + set lassign lassign + set x FAIL + set y FAIL + list [$lassign {a b c d} x y] $x $y + } + testLassign +} -result {{c d} a b} -cleanup { + rename testLassign {} +} +test cmdIL-6.20 {lassign command - list format error} -body { + proc testLassign {} { + set lassign lassign + set x FAIL + set y FAIL + list [catch {$lassign {a {b}c d} x y} msg] $msg $x $y + } + testLassign +} -result {1 {list element in braces followed by "c" instead of space} FAIL FAIL} -cleanup { + rename testLassign {} +} +test cmdIL-6.21 {lassign command - assignment to arrays} -body { + proc testLassign {} { + set lassign lassign + list [$lassign {a b} x(x)] $x(x) + } + testLassign +} -result {b a} -cleanup { + rename testLassign {} +} +test cmdIL-6.22 {lassign command - variable update error} -body { + proc testLassign {} { + set lassign lassign + set x(x) {} + $lassign a x + } + testLassign +} -returnCodes 1 -result {can't set "x": variable is array} -cleanup { + rename testLassign {} +} +test cmdIL-6.23 {lassign command - variable update error} -body { + proc testLassign {} { + set lassign lassign + set x(x) {} + set y FAIL + list [catch {$lassign a y x} msg] $msg $y + } + testLassign +} -result {1 {can't set "x": variable is array} a} -cleanup { + rename testLassign {} +} +test cmdIL-6.24 {lassign command - memory leak testing} -setup { + set x(x) {} + set y FAIL + proc getbytes {} { + set lines [split [memory info] "\n"] + lindex [lindex $lines 3] 3 + } + proc stress {} { + global x y + set lassign lassign + $lassign {} y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y + catch {$lassign {} y y y y y y y y y y y y y y y y y y y y y y y y y x} + catch {$lassign {} x} + } +} -constraints memory -body { + set end [getbytes] + for {set i 0} {$i < 5} {incr i} { + stress + set tmp $end + set end [getbytes] + } + expr {$end - $tmp} +} -result 0 -cleanup { + unset -nocomplain x y i tmp end + rename getbytes {} + rename stress {} +} +# Assorted shimmering problems +test cmdIL-6.25 {lassign command - shimmering protection} -body { + proc testLassign {} { + set x {a b c} + list [lassign $x $x y] $x [set $x] $y + } + testLassign +} -result {c {a b c} a b} -cleanup { + rename testLassign {} +} +test cmdIL-6.26 {lassign command - shimmering protection} -body { + proc testLassign {} { + set x {a b c} + set lassign lassign + list [$lassign $x $x y] $x [set $x] $y + } + testLassign +} -result {c {a b c} a b} -cleanup { + rename testLassign {} +} + +test cmdIL-7.1 {lreverse command} -body { + lreverse +} -returnCodes error -result "wrong # args: should be \"lreverse list\"" +test cmdIL-7.2 {lreverse command} -body { + lreverse a b +} -returnCodes error -result "wrong # args: should be \"lreverse list\"" +test cmdIL-7.3 {lreverse command} -body { + lreverse "not \{a list" +} -returnCodes error -result {unmatched open brace in list} +test cmdIL-7.4 {lreverse command - shared object} { + set x {a b {c d} e f} + lreverse $x +} {f e {c d} b a} +test cmdIL-7.5 {lreverse command - unshared object} { + lreverse [list a b {c d} e f] +} {f e {c d} b a} +test cmdIL-7.6 {lreverse command - unshared object [Bug 1672585]} { + lreverse [set x {1 2 3}][unset x] +} {3 2 1} +test cmdIL-7.7 {lreverse command - empty object [Bug 1876793]} { + lreverse [list] +} {} + +testConstraint testobj [llength [info commands testobj]] +test cmdIL-7.8 {lreverse command - shared intrep [Bug 1675044]} -setup { + teststringobj set 1 {1 2 3} + testobj convert 1 list + testobj duplicate 1 2 + variable x [teststringobj get 1] + variable y [teststringobj get 2] + testobj freeallvars + proc K {a b} {return $a} +} -constraints testobj -body { + lreverse [K $y [unset y]] + lindex $x 0 +} -cleanup { + unset -nocomplain x y + rename K {} +} -result 1 + +# This belongs in info test, but adding tests there breaks tests +# that compute source file line numbers. +test info-20.6 {Bug 3587651} -setup { + namespace eval my {namespace eval tcl {namespace eval mathfunc { + proc demo x {return 42} + }}}} -body { namespace eval my {expr {"demo" in [info functions]}}} -cleanup { + namespace delete my +} -result 1 + # cleanup ::tcltest::cleanupTests return + +# Local Variables: +# mode: tcl +# End: |