riscv-openocd-wch/jimtcl/tests/lsort.test

242 lines
9.7 KiB
Plaintext

# This file contains a collection of tests for the procedures in the
# file tclCmdIL.c. Sourcing this file into Tcl runs the tests and
# generates output for errors. No output means no errors were found.
#
# Copyright (c) 1997 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: lsort.test,v 1.12.2.2 2001/10/08 15:50:24 dkf Exp $
source [file dirname [info script]]/testing.tcl
test lsort-1.1 {Tcl_LsortObjCmd procedure} jim {
list [catch {lsort} msg] $msg
} {1 {wrong # args: should be "lsort ?options? list"}}
test lsort-1.2 {Tcl_LsortObjCmd procedure} jim {
list [catch {lsort -foo {1 3 2 5}} msg] $msg
} {1 {bad option "-foo": must be -ascii, -command, -decreasing, -increasing, -index, -integer, -nocase, -real, -stride, or -unique}}
test lsort-1.3 {Tcl_LsortObjCmd procedure, default options} {
lsort {d e c b a \{ d35 d300}
} {a b c d d300 d35 e \{}
test lsort-1.4 {Tcl_LsortObjCmd procedure, -ascii option} {
lsort -integer -ascii {d e c b a d35 d300}
} {a b c d d300 d35 e}
test lsort-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 lsort-1.6 {Tcl_LsortObjCmd procedure, -command option} {
proc cmp {a b} {
set rc [expr {[string match x* $b] - [string match x* $a]}]
if {$rc == 0} {
set rc [string compare $a $b]
}
return $rc
}
lsort -command cmp {x1 abc x2 def x3 x4}
} {x1 x2 x3 x4 abc def}
test lsort-1.7 {Tcl_LsortObjCmd procedure, -decreasing option} {
lsort -decreasing {d e c b a d35 d300}
} {e d35 d300 d c b a}
test lsort-1.8 {Tcl_LsortObjCmd procedure, -real option} {
lsort -real {24.2 6e3 150e-1}
} {150e-1 24.2 6e3}
test lsort-1.10 {Tcl_LsortObjCmd procedure, -increasing option} {
lsort -decreasing -increasing {d e c b a d35 d300}
} {a b c d d300 d35 e}
test lsort-1.11 {Tcl_LsortObjCmd procedure, -index option} {
list [catch {lsort -index {1 3 2 5}} msg] $msg
} {1 {"-index" option must be followed by list index}}
test lsort-1.12 {Tcl_LsortObjCmd procedure, -index option} {
list [catch {lsort -index foo {1 3 2 5}} msg] $msg
} {1 {bad index "foo": must be intexpr or end?[+-]intexpr?}}
test lsort-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}}
test lsort-1.14 {Tcl_LsortObjCmd procedure, -index option} {
lsort -index 1 -integer {{1 25 100} {3 16 42} {10 20 50}}
} {{3 16 42} {10 20 50} {1 25 100}}
test lsort-1.15 {Tcl_LsortObjCmd procedure, -integer option} {
lsort -integer {24 6 300 18}
} {6 18 24 300}
test lsort-1.16 {Tcl_LsortObjCmd procedure, -integer option} {
list [catch {lsort -integer {1 3 2.4}} msg] $msg
} {1 {expected integer but got "2.4"}}
test lsort-1.19 {Tcl_LsortObjCmd procedure, empty list} {
lsort {}
} {}
test lsort-1.24 {Tcl_LsortObjCmd procedure, order of -index and -command} {
catch {rename 1 ""}
proc testcmp {a b} {return [string compare $a $b]}
set l [list [list a b] [list c d]]
set result [list [catch {lsort -command testcmp -index 1 $l} msg] $msg]
rename testcmp ""
set result
} [list 0 [list [list a b] [list c d]]]
test lsort-1.25 {Tcl_LsortObjCmd procedure, order of -index and -command} {
catch {rename 1 ""}
proc testcmp {a b} {return [string compare $a $b]}
set l [list [list a b] [list c d]]
set result [list [catch {lsort -index 1 -command testcmp $l} msg] $msg]
rename testcmp ""
set 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 lsort-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}}
# Can't think of any good tests for the MergeSort and MergeLists
# procedures, except a bunch of random lists to sort.
test lsort-2.1 {MergeSort and MergeLists procedures} {
set result {}
set r 1435753299
proc rand {} {
global r
set r [expr {(16807 * $r) % (0x7fffffff)}]
}
for {set i 0} {$i < 150} {incr i} {
set x {}
for {set j 0} {$j < $i} {incr j} {
lappend x [expr {[rand] & 0xfff}]
}
set y [lsort -integer $x]
set old -1
foreach el $y {
if {$el < $old} {
append result "list {$x} sorted to {$y}, element $el out of order\n"
break
}
set old $el
}
}
set result
} {}
test lsort-3.1 {SortCompare procedure, skip comparisons after error} {
set x 0
proc cmp {a b} {
global x
incr x
error "error #$x"
}
list [catch {lsort -integer -command cmp {48 6 28 190 16 2 3 6 1}} msg] \
$msg $x
} {1 {error #1} 1}
test lsort-3.2 {lsort -real, returning indices} {
lsort -decreasing -real {1.2 34.5 34.5 5.6}
} {34.5 34.5 5.6 1.2}
test lsort-3.3 {SortCompare procedure, -index option} -body {
lsort -integer -index 2 {{20 10} {15 30 40}}
} -returnCodes error -result {element 2 missing from sublist "20 10"}
test lsort-3.5 {SortCompare procedure, -index option} -body {
lsort -integer -index 2 {{20 10 13} {15}}
} -returnCodes error -result {index "2" out of range}
test lsort-3.6 {SortCompare procedure, -index option} {
lsort -integer -index 2 {{1 15 30} {2 5 25} {3 25 20}}
} {{3 25 20} {2 5 25} {1 15 30}}
test lsort-3.7 {SortCompare procedure, -ascii option} {
lsort -ascii {d e c b a d35 d300 100 20}
} {100 20 a b c d d300 d35 e}
test lsort-3.9 {SortCompare procedure, -integer option} {
list [catch {lsort -integer {x 3}} msg] $msg
} {1 {expected integer but got "x"}}
test lsort-3.10 {SortCompare procedure, -integer option} {
list [catch {lsort -integer {3 q}} msg] $msg
} {1 {expected integer but got "q"}}
# JimTCL specifically does not adhere to the octal default for numbers starting with zero
test lsort-3.11 {SortCompare procedure, -integer option} -constraints jim -body {
lsort -integer {35 21 0x20 30 023 100 8}
} -result {8 21 023 30 0x20 35 100}
test lsort-3.15 {SortCompare procedure, -command option} {
proc cmp {a b} {
error "comparison error"
}
list [catch {lsort -command cmp {48 6}} msg] $msg
} {1 {comparison error}}
test lsort-3.16 {SortCompare procedure, -command option, long command} {
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 lsort-3.17 {SortCompare procedure, -command option, non-integer result} jim {
proc cmp {a b} {
return foow
}
list [catch {lsort -command cmp {48 6}} msg] $msg
} {1 {expected integer but got "foow"}}
test lsort-3.18 {SortCompare procedure, -command option} {
proc cmp {a b} {
expr {$b - $a}
}
lsort -command cmp {48 6 18 22 21 35 36}
} {48 36 35 22 21 18 6}
# JimTCL specifically does not adhere to the octal default for numbers starting with zero
test lsort-3.19 {SortCompare procedure, -decreasing optio} -constraints jim -body {
lsort -decreasing -integer {35 21 0x20 30 023 100 8}
} -result {100 35 0x20 30 023 21 8}
test lsort-3.20 {SortCompare procedure, -real option} -body {
lsort -real {6...4 3}
} -returnCodes error -result {expected floating-point number but got "6...4"}
test lsort-3.21 {lsort, unique sort} {
lsort -integer -unique {3 1 2 3 1 4 3}
} {1 2 3 4}
test lsort-3.22 {lsort, unique sort with index} {
# lsort -unique should return the last unique item
# Note that lsort is not guarunteed to be a stable sort, so
# the resulting list is converted integers to allow
# for different ordering of items that have the same value
set vallist {}
foreach val [lsort -int -unique {0 5 05 00 004 4}] {
lappend vallist [expr int($val)]
}
set vallist
} {0 4 5}
test lsort-5.1 "Sort case insensitive" {
lsort -nocase {ba aB aa ce}
} {aa aB ba ce}
test cmdIL-1.30 {Tcl_LsortObjCmd procedure, -stride option} {
lsort -stride 2 {f e d c b a}
} {b a d c f e}
test cmdIL-1.31 {Tcl_LsortObjCmd procedure, -stride option} {
lsort -stride 3 {f e d c b a}
} {c b a f e d}
test cmdIL-1.32 {lsort -stride errors} -returnCodes error -body {
lsort -stride foo bar
} -result {expected integer but got "foo"}
test cmdIL-1.33 {lsort -stride errors} -returnCodes error -body {
lsort -stride 1 bar
} -result {stride length must be at least 2}
test cmdIL-1.34 {lsort -stride errors} -returnCodes error -body {
lsort -stride 2 {a b c}
} -result {list size must be a multiple of the stride length}
test cmdIL-1.35 {lsort -stride errors} -returnCodes error -body {
lsort -stride 2 -index 3 {a b c d}
} -match glob -result {*}
test cmdIL-1.36 {lsort -stride and -index: Bug 2918962} {
lsort -stride 2 -index {0 1} {
{{c o d e} 54321} {{b l a h} 94729}
{{b i g} 12345} {{d e m o} 34512}
}
} {{{b i g} 12345} {{d e m o} 34512} {{c o d e} 54321} {{b l a h} 94729}}
test cmdIL-1.41 {lsort -stride and -index} -body {
lsort -stride 2 -index -2 {a 2 b 1}
} -returnCodes error -result {index "-2" out of range}
test cmdIL-1.42 {lsort -stride and-index} -body {
lsort -stride 2 -index -1-1 {a 2 b 1}
} -returnCodes error -result {index "-1-1" out of range}
testreport