Tizen 2.0 Release
[external/tizen-coreutils.git] / tests / sort / Test.pm
1 # -*-perl-*-
2 # Test "sort".
3
4 # Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
5 # 2006 Free Software Foundation, Inc.
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301, USA.
21
22 package Test;
23 require 5.002;
24 use strict;
25
26 # Tell sort to accept old-style options like `+1'.
27 $Test::env_default = ['_POSIX2_VERSION=199209'];
28
29 my @tv = (
30 #test   options   input   expected-output   expected-return-code
31 #
32 ["n1", '-n', ".01\n0\n", "0\n.01\n", 0],
33 ["n2", '-n', ".02\n.01\n", ".01\n.02\n", 0],
34 ["n3", '-n', ".02\n.00\n", ".00\n.02\n", 0],
35 ["n4", '-n', ".02\n.000\n", ".000\n.02\n", 0],
36 ["n5", '-n', ".021\n.029\n", ".021\n.029\n", 0],
37
38 ["n6", '-n', ".02\n.0*\n", ".0*\n.02\n", 0],
39 ["n7", '-n', ".02\n.*\n", ".*\n.02\n", 0],
40 ["n8a", '-s -n -k1,1', ".0a\n.0b\n", ".0a\n.0b\n", 0],
41 ["n8b", '-s -n -k1,1', ".0b\n.0a\n", ".0b\n.0a\n", 0],
42 ["n9a", '-s -n -k1,1', ".000a\n.000b\n", ".000a\n.000b\n", 0],
43 ["n9b", '-s -n -k1,1', ".000b\n.000a\n", ".000b\n.000a\n", 0],
44 ["n10a", '-s -n -k1,1', ".00a\n.000b\n", ".00a\n.000b\n", 0],
45 ["n10b", '-s -n -k1,1', ".00b\n.000a\n", ".00b\n.000a\n", 0],
46 ["n11a", '-s -n -k1,1', ".01a\n.010\n", ".01a\n.010\n", 0],
47 ["n11b", '-s -n -k1,1', ".010\n.01a\n", ".010\n.01a\n", 0],
48
49 ["01a", '', "A\nB\nC\n", "A\nB\nC\n", 0],
50 #
51 ["02a", '-c', "A\nB\nC\n", '', 0],
52 ["02b", '-c', "A\nC\nB\n", '', 1],
53 ["02c", '-c -k1,1', "a\na b\n", '', 0],
54 ["02d", '-C', "A\nB\nC\n", '', 0],
55 ["02e", '-C', "A\nC\nB\n", '', 1],
56 # This should fail because there are duplicate keys
57 ["02m", '-cu', "A\nA\n", '', 1],
58 ["02n", '-cu', "A\nB\n", '', 0],
59 ["02o", '-cu', "A\nB\nB\n", '', 1],
60 ["02p", '-cu', "B\nA\nB\n", '', 1],
61 #
62 ["03a", '-k1', "B\nA\n", "A\nB\n",  0],
63 ["03b", '-k1,1', "B\nA\n", "A\nB\n",  0],
64 ["03c", '-k1 -k2', "A b\nA a\n", "A a\nA b\n",  0],
65 # Fail with a diagnostic when -k specifies field == 0.
66 ["03d", '-k0', "", "",  2],
67 # Fail with a diagnostic when -k specifies character == 0.
68 ["03e", '-k1.0', "", "",  2],
69 ["03f", '-k1.1,-k0', "", "",  2],
70 # This is ok.
71 ["03g", '-k1.1,1.0', "", "",  0],
72 # This is equivalent to 3f.
73 ["03h", '-k1.1,1', "", "",  0],
74 # This too, is equivalent to 3f.
75 ["03i", '-k1,1', "", "",  0],
76 #
77 ["04a", '-nc', "2\n11\n", "",  0],
78 ["04b", '-n', "11\n2\n", "2\n11\n", 0],
79 ["04c", '-k1n', "11\n2\n", "2\n11\n", 0],
80 ["04d", '-k1', "11\n2\n", "11\n2\n", 0],
81 ["04e", '-k2', "ignored B\nz-ig A\n", "z-ig A\nignored B\n", 0],
82 #
83 ["05a", '-k1,2', "A B\nA A\n", "A A\nA B\n", 0],
84 ["05b", '-k1,2', "A B A\nA A Z\n", "A A Z\nA B A\n", 0],
85 ["05c", '-k1 -k2', "A B A\nA A Z\n", "A A Z\nA B A\n", 0],
86 ["05d", '-k2,2', "A B A\nA A Z\n", "A A Z\nA B A\n", 0],
87 ["05e", '-k2,2', "A B Z\nA A A\n", "A A A\nA B Z\n", 0],
88 ["05f", '-k2,2', "A B A\nA A Z\n", "A A Z\nA B A\n", 0],
89 #
90 ["06a", '-k 1,2', "A B\nA A\n", "A A\nA B\n", 0],
91 ["06b", '-k 1,2', "A B A\nA A Z\n", "A A Z\nA B A\n", 0],
92 ["06c", '-k 1 -k 2', "A B A\nA A Z\n", "A A Z\nA B A\n", 0],
93 ["06d", '-k 2,2', "A B A\nA A Z\n", "A A Z\nA B A\n", 0],
94 ["06e", '-k 2,2', "A B Z\nA A A\n", "A A A\nA B Z\n", 0],
95 ["06f", '-k 2,2', "A B A\nA A Z\n", "A A Z\nA B A\n", 0],
96 #
97 ["07a", '-k 2,3', "9 a b\n7 a a\n", "7 a a\n9 a b\n", 0],
98 ["07b", '-k 2,3', "a a b\nz a a\n", "z a a\na a b\n", 0],
99 ["07c", '-k 2,3', "y k b\nz k a\n", "z k a\ny k b\n", 0],
100 ["07d", '+1 -3', "y k b\nz k a\n", "z k a\ny k b\n", 0],
101 #
102 # report an error for `.' without following char spec
103 ["08a", '-k 2.,3', "", "", 2],
104 # report an error for `,' without following POS2
105 ["08b", '-k 2,', "", "", 2],
106 #
107 # Test new -g option.
108 ["09a", '-g', "1e2\n2e1\n", "2e1\n1e2\n", 0],
109 # Make sure -n works how we expect.
110 ["09b", '-n', "1e2\n2e1\n", "1e2\n2e1\n", 0],
111 ["09c", '-n', "2e1\n1e2\n", "1e2\n2e1\n", 0],
112 ["09d", '-k2g', "a 1e2\nb 2e1\n", "b 2e1\na 1e2\n", 0],
113 #
114 # Bug reported by Roger Peel <R.Peel@ee.surrey.ac.uk>
115 ["10a", '-t : -k 2.2,2.2', ":ba\n:ab\n", ":ba\n:ab\n", 0],
116 # Equivalent to above, but using obsolescent `+pos -pos' option syntax.
117 ["10b", '-t : +1.1 -1.2', ":ba\n:ab\n", ":ba\n:ab\n", 0],
118 #
119 # The same as the preceding two, but with input lines reversed.
120 ["10c", '-t : -k 2.2,2.2', ":ab\n:ba\n", ":ba\n:ab\n", 0],
121 # Equivalent to above, but using obsolescent `+pos -pos' option syntax.
122 ["10d", '-t : +1.1 -1.2', ":ab\n:ba\n", ":ba\n:ab\n", 0],
123 # Try without -t...
124 # But note that we have to count the delimiting space at the beginning
125 # of each field that has it.
126 ["10a0", '-k 2.3,2.3', "z ba\nz ab\n", "z ba\nz ab\n", 0],
127 ["10a1", '-k 1.2,1.2', "ba\nab\n", "ba\nab\n", 0],
128 ["10a2", '-b -k 2.2,2.2', "z ba\nz ab\n", "z ba\nz ab\n", 0],
129 #
130 # An even simpler example demonstrating the bug.
131 ["10e", '-k 1.2,1.2', "ab\nba\n", "ba\nab\n", 0],
132 #
133 # The way sort works on these inputs (10f and 10g) seems wrong to me.
134 # See May 30 ChangeLog entry.  POSIX doesn't seem to say one way or
135 # the other, but that's the way all other sort implementations work.
136 ["10f", '-t : -k 1.3,1.3', ":ab\n:ba\n", ":ba\n:ab\n", 0],
137 ["10g", '-k 1.4,1.4', "a ab\nb ba\n", "b ba\na ab\n", 0],
138 #
139 # Exercise bug re using -b to skip trailing blanks.
140 ["11a", '-t: -k1,1b -k2,2', "a\t:a\na :b\n", "a\t:a\na :b\n", 0],
141 ["11b", '-t: -k1,1b -k2,2', "a :b\na\t:a\n", "a\t:a\na :b\n", 0],
142 ["11c", '-t: -k2,2b -k3,3', "z:a\t:a\na :b\n", "z:a\t:a\na :b\n", 0],
143 # Before 1.22m, the first key comparison reported equality.
144 # With 1.22m, they compare different: "a" sorts before "a\n",
145 # and the second key spec isn't even used.
146 ["11d", '-t: -k2,2b -k3,3', "z:a :b\na\t:a\n", "a\t:a\nz:a :b\n", 0],
147 #
148 # Exercise bug re comparing `-' and integers.
149 ["12a", '-n -t: +1', "a:1\nb:-\n", "b:-\na:1\n", 0],
150 ["12b", '-n -t: +1', "b:-\na:1\n", "b:-\na:1\n", 0],
151 # Try some other (e.g. `X') invalid character.
152 ["12c", '-n -t: +1', "a:1\nb:X\n", "b:X\na:1\n", 0],
153 ["12d", '-n -t: +1', "b:X\na:1\n", "b:X\na:1\n", 0],
154 # From Karl Heuer
155 ["13a", '+0.1n', "axx\nb-1\n", "b-1\naxx\n", 0],
156 ["13b", '+0.1n', "b-1\naxx\n", "b-1\naxx\n", 0],
157 #
158 # From Carl Johnson <carlj@cjlinux.home.org>
159 ["14a", '-d -u', "mal\nmal-\nmala\n", "mal\nmala\n", 0],
160 # Be sure to fix the (translate && ignore) case in keycompare.
161 ["14b", '-f -d -u', "mal\nmal-\nmala\n", "mal\nmala\n", 0],
162 #
163 # Experiment with -i.
164 ["15a", '-i -u', "a\na\1\n", "a\n", 0],
165 ["15b", '-i -u', "a\n\1a\n", "a\n", 0],
166 ["15c", '-i -u', "a\1\na\n", "a\1\n", 0],
167 ["15d", '-i -u', "\1a\na\n", "\1a\n", 0],
168 ["15e", '-i -u', "a\n\1\1\1\1\1a\1\1\1\1\n", "a\n", 0],
169
170 # From Erick Branderhorst -- fixed around 1.19e
171 ["16a", '-f',
172  "éminence\nüberhaupt\n's-Gravenhage\naëroclub\nAag\naagtappels\n",
173  "'s-Gravenhage\nAag\naagtappels\naëroclub\néminence\nüberhaupt\n",
174  0],
175
176 # This provokes a one-byte memory overrun of a malloc'd block for versions
177 # of sort from textutils-1.19p and before.
178 ["17", '-c', "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", "", 0],
179
180 # POSIX says -n no longer implies -b, so here we're comparing ` 9' and `10'.
181 ["18a", '-k1.1,1.2n', " 901\n100\n", " 901\n100\n", 0],
182
183 # Just like above, because the global `-b' has no effect on the
184 # key specifier when a key-specific option (`n' in this case) is used.
185 ["18b", '-b -k1.1,1.2n', " 901\n100\n", " 901\n100\n", 0],
186
187 # Here we're comparing ` 90' and `10', because the `b' on the key-end specifier
188 # makes sort ignore leading blanks when determining that key's *end*.
189 ["18c", '-k1.1,1.2nb', " 901\n100\n", "100\n 901\n", 0],
190
191 # Here we're comparing `9' and `10', because the `b' on the key-start specifier
192 # makes sort ignore leading blanks when determining that key's *start*.
193 ["18d", '-k1.1b,1.2n', " 901\n100\n", " 901\n100\n", 0],
194
195 # This compares `90' and `10', as it ignores leading blanks for both
196 # key start and key end.
197 ["18e", '-nb -k1.1,1.2', " 901\n100\n", "100\n 901\n", 0],
198
199 # This looks odd, but works properly -- 2nd keyspec is never
200 # used because all lines are different.
201 ["19a", '+0 +1nr', "b 2\nb 1\nb 3\n", "b 1\nb 2\nb 3\n", 0],
202
203 # The test *intended* by the author of the above, but using the
204 # more-intuitive POSIX-style -k options.
205 ["19b", '-k1,1 -k2nr', "b 2\nb 1\nb 3\n", "b 3\nb 2\nb 1\n", 0],
206
207 # This test failed when sort-1.22 was compiled on a Next x86 system
208 # without optimization.  Without optimization gcc uses the buggy version
209 # of memcmp in the Next C library.  With optimization, gcc uses its
210 # (working) builtin version.  Test case form William Lewis.
211 ["20a", '',
212  "_________U__free\n_________U__malloc\n_________U__abort\n_________U__memcpy\n_________U__memset\n_________U_dyld_stub_binding_helper\n_________U__malloc\n_________U___iob\n_________U__abort\n_________U__fprintf\n",
213  "_________U___iob\n_________U__abort\n_________U__abort\n_________U__fprintf\n_________U__free\n_________U__malloc\n_________U__malloc\n_________U__memcpy\n_________U__memset\n_________U_dyld_stub_binding_helper\n",
214  0],
215
216 # Demonstrate that folding changes the ordering of e.g. A, a, and _
217 # because while they normally (in the C locale) collate like A, _, a,
218 # when using -f, `a' is compared as if it were `A'.
219 ["21a", '',    "A\na\n_\n", "A\n_\na\n", 0],
220 ["21b", '-f',  "A\na\n_\n", "A\na\n_\n", 0],
221 ["21c", '-f',  "a\nA\n_\n", "A\na\n_\n", 0],
222 ["21d", '-f',  "_\na\nA\n", "A\na\n_\n", 0],
223 ["21e", '-f',  "a\n_\nA\n", "A\na\n_\n", 0],
224 ["21f", '-fs', "A\na\n_\n", "A\na\n_\n", 0],
225 ["21g", '-fu', "a\n_\n", "a\n_\n", 0],
226
227 # This test failed until 1.22f.  From Zvi Har'El.
228 ["22a", '-k 2,2fd -k 1,1r', "3 b\n4 B\n", "4 B\n3 b\n", 0],
229 ["22b", '-k 2,2d  -k 1,1r', "3 b\n4 b\n", "4 b\n3 b\n", 0],
230
231 ["no-file1", 'no-file', {}, '', 2],
232 # This test failed until 1.22f.  Sort didn't give an error.
233 # From Will Edgington.
234 ["o-no-file1", '-o no-such-file no-such-file', {}, '', 2],
235
236 ["create-empty", '-o no/such/file /dev/null', {}, '', 2],
237
238 # From Paul Eggert.  This was fixed in textutils-1.22k.
239 ["neg-nls", '-n', "-1\n-9\n", "-9\n-1\n", 0],
240
241 # From Paul Eggert.  This was fixed in textutils-1.22m.
242 # The bug was visible only when using the internationalized sorting code
243 # (i.e., not when configured with --disable-nls).
244 ["nul-nls", '', "\0b\n\0a\n", "\0a\n\0b\n", 0],
245
246 # Paul Eggert wrote:
247 # I tested the revised `sort' against Solaris `sort', and found a
248 # discrepancy that turns out to be a longstanding bug in GNU sort.
249 # POSIX.2 specifies that a newline is part of the input line, and should
250 # be significant during comparison; but with GNU sort the newline is
251 # insignificant.  Here is an example of the bug:
252 #
253 #       $ od -c t
254 #       0000000  \n  \t  \n
255 #       0000003
256 #       $ sort t | od -c
257 #       0000000  \n  \t  \n
258 #       0000003
259 #
260 # The correct output of the latter command should be
261 #
262 #       0000000  \t  \n  \n
263 #       0000003
264 #
265 # because \t comes before \n in the collating sequence, and the trailing
266 # \n's are part of the input line.
267 ["use-nl", '', "\n\t\n", "\n\t\n", 0],
268
269 # Specifying two -o options should evoke a failure
270 ["o2", '-o x -o y', '', '', 2],
271
272 # Specifying incompatible options should evoke a failure.
273 ["incompat1", '-in', '', '', 2],
274 ["incompat2", '-fR', '', '', 2],
275 ["incompat3", '-dfgiMnR', '', '', 2],
276 ["incompat4", '-c -o /dev/null', '', '', 2],
277 ["incompat5", '-C -o /dev/null', '', '', 2],
278 ["incompat6", '-cC', '', '', 2],
279
280 # -t '\0' is accepted, as of coreutils-5.0.91
281 ['nul-tab', "-k2,2 -t '\\0'", "a\0z\01\nb\0y\02\n", "b\0y\02\na\0z\01\n", 0],
282
283 ["bigfield", '-k 340282366920938463463374607431768211456',
284  "2\n1\n", "1\n2\n", 0],
285 );
286
287 sub test_vector
288 {
289   return @tv;
290 }
291
292 1;