sort: fix key with delimiters breakage
[oweals/busybox.git] / testsuite / sort.tests
1 #!/bin/sh
2
3 # SUSv3 compliant sort tests.
4 # Copyright 2005 by Rob Landley <rob@landley.net>
5 # Licensed under GPLv2, see file LICENSE in this source tree.
6
7 . ./testing.sh
8
9 # The basic tests.  These should work even with the small busybox.
10
11 testing "sort" "sort input" "a\nb\nc\n" "c\na\nb\n" ""
12 testing "sort #2" "sort input" "010\n1\n3\n" "3\n1\n010\n" ""
13 testing "sort stdin" "sort" "a\nb\nc\n" "" "b\na\nc\n"
14 testing "sort numeric" "sort -n input" "1\n3\n010\n" "3\n1\n010\n" ""
15 testing "sort reverse" "sort -r input" "wook\nwalrus\npoint\npabst\naargh\n" \
16         "point\nwook\npabst\naargh\nwalrus\n" ""
17
18 # These tests require the full option set.
19
20 optional FEATURE_SORT_BIG
21 # Longish chunk of data re-used by the next few tests
22
23 data="42        1       3       woot
24 42      1       010     zoology
25 egg     1       2       papyrus
26 7       3       42      soup
27 999     3       0       algebra
28 "
29
30 # testing "description" "command(s)" "result" "infile" "stdin"
31
32 # Sorting with keys
33
34 testing "sort one key" "sort -k4,4 input" \
35 "999    3       0       algebra
36 egg     1       2       papyrus
37 7       3       42      soup
38 42      1       3       woot
39 42      1       010     zoology
40 " "$data" ""
41
42 testing "sort key range with numeric option" "sort -k2,3n input" \
43 "42     1       010     zoology
44 42      1       3       woot
45 egg     1       2       papyrus
46 7       3       42      soup
47 999     3       0       algebra
48 " "$data" ""
49
50 testing "sort key range with numeric option and global reverse" \
51 "sort -k2,3n -r input" \
52 "egg    1       2       papyrus
53 42      1       3       woot
54 42      1       010     zoology
55 999     3       0       algebra
56 7       3       42      soup
57 " "$data" ""
58
59 testing "sort key range with multiple options" "sort -k2,3rn input" \
60 "7      3       42      soup
61 999     3       0       algebra
62 42      1       010     zoology
63 42      1       3       woot
64 egg     1       2       papyrus
65 " "$data" ""
66
67 testing "sort key range with two -k options" "sort -k 2,2n -k 1,1r input" "\
68 d 2
69 b 2
70 c 3
71 " "\
72 c 3
73 b 2
74 d 2
75 " ""
76
77 testing "sort with non-default leading delim 1" "sort -n -k2 -t/ input" "\
78 /a/2
79 /b/1
80 " "\
81 /a/2
82 /b/1
83 " ""
84
85 testing "sort with non-default leading delim 2" "sort -n -k3 -t/ input" "\
86 /b/1
87 /a/2
88 " "\
89 /b/1
90 /a/2
91 " ""
92
93 testing "sort with non-default leading delim 3" "sort -n -k3 -t/ input" "\
94 //a/2
95 //b/1
96 " "\
97 //a/2
98 //b/1
99 " ""
100
101 testing "sort with non-default leading delim 4" "sort -t: -k1,1 input" "\
102 a:b
103 a/a:a
104 " "\
105 a/a:a
106 a:b
107 " ""
108
109 testing "glibc build sort" "sort -t. -k 1,1 -k 2n,2n -k 3 input" "\
110 GLIBC_2.1
111 GLIBC_2.1.1
112 GLIBC_2.2
113 GLIBC_2.2.1
114 GLIBC_2.10
115 GLIBC_2.20
116 GLIBC_2.21
117 " "\
118 GLIBC_2.21
119 GLIBC_2.1.1
120 GLIBC_2.2.1
121 GLIBC_2.2
122 GLIBC_2.20
123 GLIBC_2.10
124 GLIBC_2.1
125 " ""
126
127 testing "glibc build sort unique" "sort -u -t. -k 1,1 -k 2n,2n -k 3 input" "\
128 GLIBC_2.1
129 GLIBC_2.1.1
130 GLIBC_2.2
131 GLIBC_2.2.1
132 GLIBC_2.10
133 GLIBC_2.20
134 GLIBC_2.21
135 " "\
136 GLIBC_2.10
137 GLIBC_2.2.1
138 GLIBC_2.1.1
139 GLIBC_2.20
140 GLIBC_2.2
141 GLIBC_2.1
142 GLIBC_2.21
143 " ""
144
145 testing "sort -u should consider field only when discarding" "sort -u -k2 input" "\
146 a c
147 " "\
148 a c
149 b c
150 " ""
151
152 testing "sort -z outputs NUL terminated lines" "sort -z input" "\
153 one\0three\0two\0\
154 " "\
155 one\0two\0three\0\
156 " ""
157
158 testing "sort key doesn't strip leading blanks, disables fallback global sort" \
159 "sort -n -k2 -t ' '" " a \n 1 \n 2 \n" "" " 2 \n 1 \n a \n"
160
161 testing "sort file in place" \
162 "sort -o input input && cat input" "\
163 111
164 222
165 " "\
166 222
167 111
168 " ""
169
170 # testing "description" "command(s)" "result" "infile" "stdin"
171
172 exit $FAILCOUNT