sl@0: # This file contains a collection of tests for the procedures in the sl@0: # file tclCmdIL.c. Sourcing this file into Tcl runs the tests and sl@0: # generates output for errors. No output means no errors were found. sl@0: # sl@0: # Copyright (c) 1997 Sun Microsystems, Inc. sl@0: # Copyright (c) 1998-1999 by Scriptics Corporation. sl@0: # sl@0: # See the file "license.terms" for information on usage and redistribution sl@0: # of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: # sl@0: # RCS: @(#) $Id: cmdIL.test,v 1.14.6.2 2007/03/10 14:57:38 dkf Exp $ sl@0: sl@0: if {[lsearch [namespace children] ::tcltest] == -1} { sl@0: package require tcltest sl@0: namespace import -force ::tcltest::* sl@0: } sl@0: sl@0: test cmdIL-1.1 {Tcl_LsortObjCmd procedure} { sl@0: list [catch {lsort} msg] $msg sl@0: } {1 {wrong # args: should be "lsort ?options? list"}} sl@0: test cmdIL-1.2 {Tcl_LsortObjCmd procedure} { sl@0: list [catch {lsort -foo {1 3 2 5}} msg] $msg sl@0: } {1 {bad option "-foo": must be -ascii, -command, -decreasing, -dictionary, -increasing, -index, -integer, -real, or -unique}} sl@0: test cmdIL-1.3 {Tcl_LsortObjCmd procedure, default options} { sl@0: lsort {d e c b a \{ d35 d300} sl@0: } {a b c d d300 d35 e \{} sl@0: test cmdIL-1.4 {Tcl_LsortObjCmd procedure, -ascii option} { sl@0: lsort -integer -ascii {d e c b a d35 d300} sl@0: } {a b c d d300 d35 e} sl@0: test cmdIL-1.5 {Tcl_LsortObjCmd procedure, -command option} { sl@0: list [catch {lsort -command {1 3 2 5}} msg] $msg sl@0: } {1 {"-command" option must be followed by comparison command}} sl@0: test cmdIL-1.6 {Tcl_LsortObjCmd procedure, -command option} { sl@0: proc cmp {a b} { sl@0: expr {[string match x* $b] - [string match x* $a]} sl@0: } sl@0: lsort -command cmp {x1 abc x2 def x3 x4} sl@0: } {x1 x2 x3 x4 abc def} sl@0: test cmdIL-1.7 {Tcl_LsortObjCmd procedure, -decreasing option} { sl@0: lsort -decreasing {d e c b a d35 d300} sl@0: } {e d35 d300 d c b a} sl@0: test cmdIL-1.8 {Tcl_LsortObjCmd procedure, -dictionary option} { sl@0: lsort -dictionary {d e c b a d35 d300} sl@0: } {a b c d d35 d300 e} sl@0: test cmdIL-1.9 {Tcl_LsortObjCmd procedure, -dictionary option} { sl@0: lsort -dictionary {1k 0k 10k} sl@0: } {0k 1k 10k} sl@0: test cmdIL-1.10 {Tcl_LsortObjCmd procedure, -increasing option} { sl@0: lsort -decreasing -increasing {d e c b a d35 d300} sl@0: } {a b c d d300 d35 e} sl@0: test cmdIL-1.11 {Tcl_LsortObjCmd procedure, -index option} { sl@0: list [catch {lsort -index {1 3 2 5}} msg] $msg sl@0: } {1 {"-index" option must be followed by list index}} sl@0: test cmdIL-1.12 {Tcl_LsortObjCmd procedure, -index option} { sl@0: list [catch {lsort -index foo {1 3 2 5}} msg] $msg sl@0: } {1 {bad index "foo": must be integer or end?-integer?}} sl@0: test cmdIL-1.13 {Tcl_LsortObjCmd procedure, -index option} { sl@0: lsort -index end -integer {{2 25} {10 20 50 100} {3 16 42} 1} sl@0: } {1 {2 25} {3 16 42} {10 20 50 100}} sl@0: test cmdIL-1.14 {Tcl_LsortObjCmd procedure, -index option} { sl@0: lsort -index 1 -integer {{1 25 100} {3 16 42} {10 20 50}} sl@0: } {{3 16 42} {10 20 50} {1 25 100}} sl@0: test cmdIL-1.15 {Tcl_LsortObjCmd procedure, -integer option} { sl@0: lsort -integer {24 6 300 18} sl@0: } {6 18 24 300} sl@0: test cmdIL-1.16 {Tcl_LsortObjCmd procedure, -integer option} { sl@0: list [catch {lsort -integer {1 3 2.4}} msg] $msg sl@0: } {1 {expected integer but got "2.4"}} sl@0: test cmdIL-1.17 {Tcl_LsortObjCmd procedure, -real option} { sl@0: lsort -real {24.2 6e3 150e-1} sl@0: } {150e-1 24.2 6e3} sl@0: test cmdIL-1.18 {Tcl_LsortObjCmd procedure, bogus list} { sl@0: list [catch {lsort "1 2 3 \{ 4"} msg] $msg sl@0: } {1 {unmatched open brace in list}} sl@0: test cmdIL-1.19 {Tcl_LsortObjCmd procedure, empty list} { sl@0: lsort {} sl@0: } {} sl@0: test cmdIL-1.22 {Tcl_LsortObjCmd procedure, unique sort} { sl@0: lsort -integer -unique {3 1 2 3 1 4 3} sl@0: } {1 2 3 4} sl@0: test cmdIL-1.23 {Tcl_LsortObjCmd procedure, unique sort with index} { sl@0: # lsort -unique should return the last unique item sl@0: lsort -unique -index 0 {{a b} {c b} {a c} {d a}} sl@0: } {{a c} {c b} {d a}} sl@0: test cmdIL-1.24 {Tcl_LsortObjCmd procedure, order of -index and -command} { sl@0: catch {rename 1 ""} sl@0: proc testcmp {a b} {return [string compare $a $b]} sl@0: set l [list [list a b] [list c d]] sl@0: set result [list [catch {lsort -command testcmp -index 1 $l} msg] $msg] sl@0: rename testcmp "" sl@0: set result sl@0: } [list 0 [list [list a b] [list c d]]] sl@0: test cmdIL-1.25 {Tcl_LsortObjCmd procedure, order of -index and -command} { sl@0: catch {rename 1 ""} sl@0: proc testcmp {a b} {return [string compare $a $b]} sl@0: set l [list [list a b] [list c d]] sl@0: set result [list [catch {lsort -index 1 -command testcmp $l} msg] $msg] sl@0: rename testcmp "" sl@0: set result sl@0: } [list 0 [list [list a b] [list c d]]] sl@0: # Note that the required order only exists in the end-1'th element; sl@0: # indexing using the end element or any fixed offset from the start sl@0: # will not work... sl@0: test cmdIL-1.26 {Tcl_LsortObjCmd procedure, offset indexing from end} { sl@0: lsort -index end-1 {{a 1 e i} {b 2 3 f g} {c 4 5 6 d h}} sl@0: } {{c 4 5 6 d h} {a 1 e i} {b 2 3 f g}} sl@0: test cmdIL-1.29 {Tcl_LsortObjCmd procedure, loss of list rep during sorting} { sl@0: set l {1 2 3} sl@0: proc testcmp args {string length $::l} sl@0: string length [lsort -command testcmp $l] sl@0: } 5 sl@0: sl@0: # Can't think of any good tests for the MergeSort and MergeLists sl@0: # procedures, except a bunch of random lists to sort. sl@0: sl@0: test cmdIL-2.1 {MergeSort and MergeLists procedures} { sl@0: set result {} sl@0: set r 1435753299 sl@0: proc rand {} { sl@0: global r sl@0: set r [expr {(16807 * $r) % (0x7fffffff)}] sl@0: } sl@0: for {set i 0} {$i < 150} {incr i} { sl@0: set x {} sl@0: for {set j 0} {$j < $i} {incr j} { sl@0: lappend x [expr {[rand] & 0xfff}] sl@0: } sl@0: set y [lsort -integer $x] sl@0: set old -1 sl@0: foreach el $y { sl@0: if {$el < $old} { sl@0: append result "list {$x} sorted to {$y}, element $el out of order\n" sl@0: break sl@0: } sl@0: set old $el sl@0: } sl@0: } sl@0: set result sl@0: } {} sl@0: sl@0: test cmdIL-3.1 {SortCompare procedure, skip comparisons after error} { sl@0: set x 0 sl@0: proc cmp {a b} { sl@0: global x sl@0: incr x sl@0: error "error #$x" sl@0: } sl@0: list [catch {lsort -integer -command cmp {48 6 28 190 16 2 3 6 1}} msg] \ sl@0: $msg $x sl@0: } {1 {error #1} 1} sl@0: test cmdIL-3.2 {SortCompare procedure, -index option} { sl@0: list [catch {lsort -integer -index 2 "\\\{ {30 40 50}"} msg] $msg sl@0: } {1 {unmatched open brace in list}} sl@0: test cmdIL-3.3 {SortCompare procedure, -index option} { sl@0: list [catch {lsort -integer -index 2 {{20 10} {15 30 40}}} msg] $msg sl@0: } {1 {element 2 missing from sublist "20 10"}} sl@0: test cmdIL-3.4 {SortCompare procedure, -index option} { sl@0: list [catch {lsort -integer -index 2 "{a b c} \\\{"} msg] $msg sl@0: } {1 {unmatched open brace in list}} sl@0: test cmdIL-3.5 {SortCompare procedure, -index option} { sl@0: list [catch {lsort -integer -index 2 {{20 10 13} {15}}} msg] $msg sl@0: } {1 {element 2 missing from sublist "15"}} sl@0: test cmdIL-3.6 {SortCompare procedure, -index option} { sl@0: lsort -integer -index 2 {{1 15 30} {2 5 25} {3 25 20}} sl@0: } {{3 25 20} {2 5 25} {1 15 30}} sl@0: test cmdIL-3.7 {SortCompare procedure, -ascii option} { sl@0: lsort -ascii {d e c b a d35 d300 100 20} sl@0: } {100 20 a b c d d300 d35 e} sl@0: test cmdIL-3.8 {SortCompare procedure, -dictionary option} { sl@0: lsort -dictionary {d e c b a d35 d300 100 20} sl@0: } {20 100 a b c d d35 d300 e} sl@0: test cmdIL-3.9 {SortCompare procedure, -integer option} { sl@0: list [catch {lsort -integer {x 3}} msg] $msg sl@0: } {1 {expected integer but got "x"}} sl@0: test cmdIL-3.10 {SortCompare procedure, -integer option} { sl@0: list [catch {lsort -integer {3 q}} msg] $msg sl@0: } {1 {expected integer but got "q"}} sl@0: test cmdIL-3.11 {SortCompare procedure, -integer option} { sl@0: lsort -integer {35 21 0x20 30 023 100 8} sl@0: } {8 023 21 30 0x20 35 100} sl@0: test cmdIL-3.12 {SortCompare procedure, -real option} { sl@0: list [catch {lsort -real {6...4 3}} msg] $msg sl@0: } {1 {expected floating-point number but got "6...4"}} sl@0: test cmdIL-3.13 {SortCompare procedure, -real option} { sl@0: list [catch {lsort -real {3 1x7}} msg] $msg sl@0: } {1 {expected floating-point number but got "1x7"}} sl@0: test cmdIL-3.14 {SortCompare procedure, -real option} { sl@0: lsort -real {24 2.5e01 16.7 85e-1 10.004} sl@0: } {85e-1 10.004 16.7 24 2.5e01} sl@0: test cmdIL-3.15 {SortCompare procedure, -command option} { sl@0: proc cmp {a b} { sl@0: error "comparison error" sl@0: } sl@0: list [catch {lsort -command cmp {48 6}} msg] $msg $errorInfo sl@0: } {1 {comparison error} {comparison error sl@0: while executing sl@0: "error "comparison error"" sl@0: (procedure "cmp" line 2) sl@0: invoked from within sl@0: "cmp 48 6" sl@0: (-compare command) sl@0: invoked from within sl@0: "lsort -command cmp {48 6}"}} sl@0: test cmdIL-3.16 {SortCompare procedure, -command option, long command} { sl@0: proc cmp {dummy a b} { sl@0: string compare $a $b sl@0: } sl@0: 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}} sl@0: } {{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}} sl@0: test cmdIL-3.17 {SortCompare procedure, -command option, non-integer result} { sl@0: proc cmp {a b} { sl@0: return foow sl@0: } sl@0: list [catch {lsort -command cmp {48 6}} msg] $msg sl@0: } {1 {-compare command returned non-integer result}} sl@0: test cmdIL-3.18 {SortCompare procedure, -command option} { sl@0: proc cmp {a b} { sl@0: expr {$b - $a} sl@0: } sl@0: lsort -command cmp {48 6 18 22 21 35 36} sl@0: } {48 36 35 22 21 18 6} sl@0: test cmdIL-3.19 {SortCompare procedure, -decreasing option} { sl@0: lsort -decreasing -integer {35 21 0x20 30 023 100 8} sl@0: } {100 35 0x20 30 21 023 8} sl@0: sl@0: test cmdIL-4.1 {DictionaryCompare procedure, numerics, leading zeros} { sl@0: lsort -dictionary {a003b a03b} sl@0: } {a03b a003b} sl@0: test cmdIL-4.2 {DictionaryCompare procedure, numerics, leading zeros} { sl@0: lsort -dictionary {a3b a03b} sl@0: } {a3b a03b} sl@0: test cmdIL-4.3 {DictionaryCompare procedure, numerics, leading zeros} { sl@0: lsort -dictionary {a3b A03b} sl@0: } {A03b a3b} sl@0: test cmdIL-4.4 {DictionaryCompare procedure, numerics, leading zeros} { sl@0: lsort -dictionary {a3b a03B} sl@0: } {a3b a03B} sl@0: test cmdIL-4.5 {DictionaryCompare procedure, numerics, leading zeros} { sl@0: lsort -dictionary {00000 000} sl@0: } {000 00000} sl@0: test cmdIL-4.6 {DictionaryCompare procedure, numerics, different lengths} { sl@0: lsort -dictionary {a321b a03210b} sl@0: } {a321b a03210b} sl@0: test cmdIL-4.7 {DictionaryCompare procedure, numerics, different lengths} { sl@0: lsort -dictionary {a03210b a321b} sl@0: } {a321b a03210b} sl@0: test cmdIL-4.8 {DictionaryCompare procedure, numerics} { sl@0: lsort -dictionary {48 6a 18b 22a 21aa 35 36} sl@0: } {6a 18b 21aa 22a 35 36 48} sl@0: test cmdIL-4.9 {DictionaryCompare procedure, numerics} { sl@0: lsort -dictionary {a123x a123b} sl@0: } {a123b a123x} sl@0: test cmdIL-4.10 {DictionaryCompare procedure, numerics} { sl@0: lsort -dictionary {a123b a123x} sl@0: } {a123b a123x} sl@0: test cmdIL-4.11 {DictionaryCompare procedure, numerics} { sl@0: lsort -dictionary {a1b aab} sl@0: } {a1b aab} sl@0: test cmdIL-4.12 {DictionaryCompare procedure, numerics} { sl@0: lsort -dictionary {a1b a!b} sl@0: } {a!b a1b} sl@0: test cmdIL-4.13 {DictionaryCompare procedure, numerics} { sl@0: lsort -dictionary {a1b2c a1b1c} sl@0: } {a1b1c a1b2c} sl@0: test cmdIL-4.14 {DictionaryCompare procedure, numerics} { sl@0: lsort -dictionary {a1b2c a1b3c} sl@0: } {a1b2c a1b3c} sl@0: test cmdIL-4.15 {DictionaryCompare procedure, long numbers} { sl@0: lsort -dictionary {a7654884321988762b a7654884321988761b} sl@0: } {a7654884321988761b a7654884321988762b} sl@0: test cmdIL-4.16 {DictionaryCompare procedure, long numbers} { sl@0: lsort -dictionary {a8765488432198876b a7654884321988761b} sl@0: } {a7654884321988761b a8765488432198876b} sl@0: test cmdIL-4.17 {DictionaryCompare procedure, case} { sl@0: lsort -dictionary {aBCd abcc} sl@0: } {abcc aBCd} sl@0: test cmdIL-4.18 {DictionaryCompare procedure, case} { sl@0: lsort -dictionary {aBCd abce} sl@0: } {aBCd abce} sl@0: test cmdIL-4.19 {DictionaryCompare procedure, case} { sl@0: lsort -dictionary {abcd ABcc} sl@0: } {ABcc abcd} sl@0: test cmdIL-4.20 {DictionaryCompare procedure, case} { sl@0: lsort -dictionary {abcd ABce} sl@0: } {abcd ABce} sl@0: test cmdIL-4.21 {DictionaryCompare procedure, case} { sl@0: lsort -dictionary {abCD ABcd} sl@0: } {ABcd abCD} sl@0: test cmdIL-4.22 {DictionaryCompare procedure, case} { sl@0: lsort -dictionary {ABcd aBCd} sl@0: } {ABcd aBCd} sl@0: test cmdIL-4.23 {DictionaryCompare procedure, case} { sl@0: lsort -dictionary {ABcd AbCd} sl@0: } {ABcd AbCd} sl@0: test cmdIL-4.24 {DictionaryCompare procedure, international characters} {hasIsoLocale} { sl@0: ::tcltest::set_iso8859_1_locale sl@0: set result [lsort -dictionary "a b c A B C \xe3 \xc4"] sl@0: ::tcltest::restore_locale sl@0: set result sl@0: } "A a B b C c \xe3 \xc4" sl@0: test cmdIL-4.25 {DictionaryCompare procedure, international characters} {hasIsoLocale} { sl@0: ::tcltest::set_iso8859_1_locale sl@0: set result [lsort -dictionary "a23\xe3 a23\xc5 a23\xe4"] sl@0: ::tcltest::restore_locale sl@0: set result sl@0: } "a23\xe3 a23\xe4 a23\xc5" sl@0: test cmdIL-4.26 {DefaultCompare procedure, signed characters} { sl@0: set l [lsort [list "abc\200" "abc"]] sl@0: set viewlist {} sl@0: foreach s $l { sl@0: set viewelem "" sl@0: set len [string length $s] sl@0: for {set i 0} {$i < $len} {incr i} { sl@0: set c [string index $s $i] sl@0: scan $c %c d sl@0: if {$d > 0 && $d < 128} { sl@0: append viewelem $c sl@0: } else { sl@0: append viewelem "\\[format %03o $d]" sl@0: } sl@0: } sl@0: lappend viewlist $viewelem sl@0: } sl@0: set viewlist sl@0: } [list "abc" "abc\\200"] sl@0: test cmdIL-4.27 {DictionaryCompare procedure, signed characters} { sl@0: set l [lsort -dictionary [list "abc\200" "abc"]] sl@0: set viewlist {} sl@0: foreach s $l { sl@0: set viewelem "" sl@0: set len [string length $s] sl@0: for {set i 0} {$i < $len} {incr i} { sl@0: set c [string index $s $i] sl@0: scan $c %c d sl@0: if {$d > 0 && $d < 128} { sl@0: append viewelem $c sl@0: } else { sl@0: append viewelem "\\[format %03o $d]" sl@0: } sl@0: } sl@0: lappend viewlist $viewelem sl@0: } sl@0: set viewlist sl@0: } [list "abc" "abc\\200"] sl@0: test cmdIL-4.28 {DictionaryCompare procedure, chars between Z and a in ASCII} { sl@0: lsort -dictionary [list AA ` c CC] sl@0: } [list ` AA c CC] sl@0: test cmdIL-4.29 {DictionaryCompare procedure, chars between Z and a in ASCII} { sl@0: lsort -dictionary [list AA ` c ^ \\ CC \[ \]] sl@0: } [list \[ \\ \] ^ ` AA c CC] sl@0: test cmdIL-4.30 {DictionaryCompare procedure, chars between Z and a in ASCII} { sl@0: lsort -dictionary [list AA ` c ^ _ \\ CC \[ dude \] funky] sl@0: } [list \[ \\ \] ^ _ ` AA c CC dude funky] sl@0: test cmdIL-4.31 {DictionaryCompare procedure, chars between Z and a in ASCII} { sl@0: lsort -dictionary [list AA c ` CC] sl@0: } [list ` AA c CC] sl@0: test cmdIL-4.32 {DictionaryCompare procedure, chars between Z and a in ASCII} { sl@0: lsort -dictionary [list AA c CC `] sl@0: } [list ` AA c CC] sl@0: test cmdIL-4.33 {DictionaryCompare procedure, chars between Z and a in ASCII} { sl@0: lsort -dictionary [list AA ! c CC `] sl@0: } [list ! ` AA c CC] sl@0: sl@0: # cleanup sl@0: ::tcltest::cleanupTests sl@0: return