Trim bootstrap jimsh
[jimtcl.git] / tests / lsort.test
blobca2fc49be58f2f3069e0a7fe47a7aa7c781b229b
1 # This file contains a collection of tests for the procedures in the
2 # file tclCmdIL.c.  Sourcing this file into Tcl runs the tests and
3 # generates output for errors.  No output means no errors were found.
5 # Copyright (c) 1997 Sun Microsystems, Inc.
6 # Copyright (c) 1998-1999 by Scriptics Corporation.
8 # See the file "license.terms" for information on usage and redistribution
9 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 # RCS: @(#) $Id: lsort.test,v 1.12.2.2 2001/10/08 15:50:24 dkf Exp $
13 source [file dirname [info script]]/testing.tcl
15 test lsort-1.1 {Tcl_LsortObjCmd procedure} jim {
16     list [catch {lsort} msg] $msg
17 } {1 {wrong # args: should be "lsort ?options? list"}}
18 test lsort-1.2 {Tcl_LsortObjCmd procedure} jim {
19     list [catch {lsort -foo {1 3 2 5}} msg] $msg
20 } {1 {bad option "-foo": must be -ascii, -command, -decreasing, -increasing, -index, -integer, -nocase, -real, or -unique}}
21 test lsort-1.3 {Tcl_LsortObjCmd procedure, default options} {
22     lsort {d e c b a \{ d35 d300}
23 } {a b c d d300 d35 e \{}
24 test lsort-1.4 {Tcl_LsortObjCmd procedure, -ascii option} {
25     lsort -integer -ascii {d e c b a d35 d300}
26 } {a b c d d300 d35 e}
27 test lsort-1.5 {Tcl_LsortObjCmd procedure, -command option} {
28     list [catch {lsort -command {1 3 2 5}} msg] $msg
29 } {1 {"-command" option must be followed by comparison command}}
30 test lsort-1.6 {Tcl_LsortObjCmd procedure, -command option} {
31     proc cmp {a b} {
32                 set rc [expr {[string match x* $b] - [string match x* $a]}]
33                 if {$rc == 0} {
34                         set rc [string compare $a $b]
35                 }
36                 return $rc
37     }
38     lsort -command cmp {x1 abc x2 def x3 x4}
39 } {x1 x2 x3 x4 abc def}
40 test lsort-1.7 {Tcl_LsortObjCmd procedure, -decreasing option} {
41     lsort -decreasing {d e c b a d35 d300}
42 } {e d35 d300 d c b a}
43 test lsort-1.8 {Tcl_LsortObjCmd procedure, -real option} {
44     lsort -real {24.2 6e3 150e-1}
45 } {150e-1 24.2 6e3}
46 test lsort-1.10 {Tcl_LsortObjCmd procedure, -increasing option} {
47     lsort -decreasing -increasing {d e c b a d35 d300}
48 } {a b c d d300 d35 e}
49 test lsort-1.11 {Tcl_LsortObjCmd procedure, -index option} {
50     list [catch {lsort -index {1 3 2 5}} msg] $msg
51 } {1 {"-index" option must be followed by list index}}
52 test lsort-1.12 {Tcl_LsortObjCmd procedure, -index option} {
53     list [catch {lsort -index foo {1 3 2 5}} msg] $msg
54 } {1 {bad index "foo": must be integer?[+-]integer? or end?[+-]integer?}}
55 test lsort-1.13 {Tcl_LsortObjCmd procedure, -index option} {
56     lsort -index end -integer {{2 25} {10 20 50 100} {3 16 42} 1}
57 } {1 {2 25} {3 16 42} {10 20 50 100}}
58 test lsort-1.14 {Tcl_LsortObjCmd procedure, -index option} {
59     lsort -index 1 -integer {{1 25 100} {3 16 42} {10 20 50}}
60 } {{3 16 42} {10 20 50} {1 25 100}}
61 test lsort-1.15 {Tcl_LsortObjCmd procedure, -integer option} {
62     lsort -integer {24 6 300 18}
63 } {6 18 24 300}
64 test lsort-1.16 {Tcl_LsortObjCmd procedure, -integer option} {
65     list [catch {lsort -integer {1 3 2.4}} msg] $msg
66 } {1 {expected integer but got "2.4"}}
67 test lsort-1.19 {Tcl_LsortObjCmd procedure, empty list} {
68     lsort {}
69 } {}
70 test lsort-1.24 {Tcl_LsortObjCmd procedure, order of -index and -command} {
71     catch {rename 1 ""}
72     proc testcmp {a b} {return [string compare $a $b]}
73     set l [list [list a b] [list c d]]
74     set result [list [catch {lsort -command testcmp -index 1 $l} msg] $msg]
75     rename testcmp ""
76     set result
77 } [list 0 [list [list a b] [list c d]]]
78 test lsort-1.25 {Tcl_LsortObjCmd procedure, order of -index and -command} {
79     catch {rename 1 ""}
80     proc testcmp {a b} {return [string compare $a $b]}
81     set l [list [list a b] [list c d]]
82     set result [list [catch {lsort -index 1 -command testcmp $l} msg] $msg]
83     rename testcmp ""
84     set result
85 } [list 0 [list [list a b] [list c d]]]
86 # Note that the required order only exists in the end-1'th element;
87 # indexing using the end element or any fixed offset from the start
88 # will not work...
89 test lsort-1.26 {Tcl_LsortObjCmd procedure, offset indexing from end} {
90     lsort -index end-1 {{a 1 e i} {b 2 3 f g} {c 4 5 6 d h}}
91 } {{c 4 5 6 d h} {a 1 e i} {b 2 3 f g}}
93 # Can't think of any good tests for the MergeSort and MergeLists
94 # procedures, except a bunch of random lists to sort.
96 test lsort-2.1 {MergeSort and MergeLists procedures} {
97     set result {}
98     set r 1435753299
99     proc rand {} {
100         global r
101         set r [expr {(16807 * $r) % (0x7fffffff)}]
102     }
103     for {set i 0} {$i < 150} {incr i} {
104         set x {}
105         for {set j 0} {$j < $i} {incr j} {
106             lappend x [expr {[rand] & 0xfff}]
107         }
108         set y [lsort -integer $x]
109         set old -1
110         foreach el $y {
111             if {$el < $old} {
112                 append result "list {$x} sorted to {$y}, element $el out of order\n"
113                 break
114             }
115             set old $el
116         }
117     }
118     set result
119 } {}
121 test lsort-3.1 {SortCompare procedure, skip comparisons after error} {
122     set x 0
123     proc cmp {a b} {
124         global x
125         incr x
126         error "error #$x"
127     }
128     list [catch {lsort -integer -command cmp {48 6 28 190 16 2 3 6 1}} msg] \
129             $msg $x
130 } {1 {error #1} 1}
131 test lsort-3.2 {lsort -real, returning indices} {
132     lsort -decreasing -real {1.2 34.5 34.5 5.6}
133 } {34.5 34.5 5.6 1.2}
134 test lsort-3.3 {SortCompare procedure, -index option} jim {
135     list [catch {lsort -integer -index 2 {{20 10} {15 30 40}}} msg] $msg
136 } {1 {list index out of range}}
137 test lsort-3.5 {SortCompare procedure, -index option} jim {
138     list [catch {lsort -integer -index 2 {{20 10 13} {15}}} msg] $msg
139 } {1 {list index out of range}}
140 test lsort-3.6 {SortCompare procedure, -index option} {
141     lsort -integer -index 2 {{1 15 30} {2 5 25} {3 25 20}}
142 } {{3 25 20} {2 5 25} {1 15 30}}
143 test lsort-3.7 {SortCompare procedure, -ascii option} {
144     lsort -ascii {d e c b a d35 d300 100 20}
145 } {100 20 a b c d d300 d35 e}
146 test lsort-3.9 {SortCompare procedure, -integer option} {
147     list [catch {lsort -integer {x 3}} msg] $msg
148 } {1 {expected integer but got "x"}}
149 test lsort-3.10 {SortCompare procedure, -integer option} {
150     list [catch {lsort -integer {3 q}} msg] $msg
151 } {1 {expected integer but got "q"}}
152 test lsort-3.11 {SortCompare procedure, -integer option} {
153     lsort -integer {35 21 0x20 30 023 100 8}
154 } {8 21 023 30 0x20 35 100}
155 test lsort-3.15 {SortCompare procedure, -command option} {
156     proc cmp {a b} {
157         error "comparison error"
158     }
159     list [catch {lsort -command cmp {48 6}} msg] $msg
160 } {1 {comparison error}}
161 test lsort-3.16 {SortCompare procedure, -command option, long command} {
162     proc cmp {dummy a b} {
163         string compare $a $b
164     }
165     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}}
166 } {{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}}
167 test lsort-3.17 {SortCompare procedure, -command option, non-integer result} jim {
168     proc cmp {a b} {
169         return foow
170     }
171     list [catch {lsort -command cmp {48 6}} msg] $msg
172 } {1 {expected integer but got "foow"}}
173 test lsort-3.18 {SortCompare procedure, -command option} {
174     proc cmp {a b} {
175         expr {$b - $a}
176     }
177     lsort -command cmp {48 6 18 22 21 35 36}
178 } {48 36 35 22 21 18 6}
179 test lsort-3.19 {SortCompare procedure, -decreasing option} {
180     lsort -decreasing -integer {35 21 0x20 30 023 100 8}
181 } {100 35 0x20 30 023 21 8}
182 test lsort-3.20 {SortCompare procedure, -real option} -body {
183     lsort -real {6...4 3}
184 } -returnCodes error -result {expected floating-point number but got "6...4"}
185 test lsort-3.21 {lsort, unique sort} {
186     lsort -integer -unique {3 1 2 3 1 4 3}
187 } {1 2 3 4}
188 test lsort-3.22 {lsort, unique sort with index} {
189     # lsort -unique should return the last unique item
190         # Note that lsort is not guarunteed to be a stable sort, so
191         # the resulting list is converted integers to allow 
192         # for different ordering of items that have the same value
193     set vallist {}
194         foreach val [lsort -int -unique {0 5 05 00 004 4}] {
195                 lappend vallist [expr int($val)]
196         }
197         set vallist
198 } {0 4 5}
200 test lsort-4.26 {DefaultCompare procedure, signed characters} utf8 {
201     set l [lsort [list "abc\u80" "abc"]]
202     set viewlist {}
203     foreach s $l {
204         set viewelem ""
205         set len [string length $s]
206         for {set i 0} {$i < $len} {incr i} {
207             set c [string index $s $i]
208             scan $c %c d
209             if {$d > 0 && $d < 128} {
210                 append viewelem $c
211             } else {
212                 append viewelem "\\[format %03o [expr {$d & 0xff}]]"
213             }
214         }
215         lappend viewlist $viewelem
216     }
217     set viewlist
218 } [list "abc" "abc\\200"]
220 test lsort-5.1 "Sort case insensitive" {
221     lsort -nocase {ba aB aa ce}
222 } {aa aB ba ce}
224 testreport