summaryrefslogtreecommitdiff
path: root/testsuite/sort.tests
blob: ff33e21b4decc2408a6f404b515120da240f05a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/sh

# SUSv3 compliant sort tests.
# Copyright 2005 by Rob Landley <rob@landley.net>
# Licensed under GPLv2, see file LICENSE in this source tree.

. ./testing.sh

# The basic tests.  These should work even with the small busybox.

testing "sort" "sort input" "a\nb\nc\n" "c\na\nb\n" ""
testing "sort #2" "sort input" "010\n1\n3\n" "3\n1\n010\n" ""
testing "sort stdin" "sort" "a\nb\nc\n" "" "b\na\nc\n"
testing "sort numeric" "sort -n input" "1\n3\n010\n" "3\n1\n010\n" ""
testing "sort reverse" "sort -r input" "wook\nwalrus\npoint\npabst\naargh\n" \
	"point\nwook\npabst\naargh\nwalrus\n" ""

# These tests require the full option set.

optional FEATURE_SORT_BIG
# Longish chunk of data re-used by the next few tests

data="42	1	3	woot
42	1	010	zoology
egg	1	2	papyrus
7	3	42	soup
999	3	0	algebra
"

# testing "description" "command(s)" "result" "infile" "stdin"

# Sorting with keys

testing "sort one key" "sort -k4,4 input" \
"999	3	0	algebra
egg	1	2	papyrus
7	3	42	soup
42	1	3	woot
42	1	010	zoology
" "$data" ""

testing "sort key range with numeric option" "sort -k2,3n input" \
"42	1	010	zoology
42	1	3	woot
egg	1	2	papyrus
7	3	42	soup
999	3	0	algebra
" "$data" ""

testing "sort key range with numeric option and global reverse" \
"sort -k2,3n -r input" \
"egg	1	2	papyrus
42	1	3	woot
42	1	010	zoology
999	3	0	algebra
7	3	42	soup
" "$data" ""

testing "sort key range with multiple options" "sort -k2,3rn input" \
"7	3	42	soup
999	3	0	algebra
42	1	010	zoology
42	1	3	woot
egg	1	2	papyrus
" "$data" ""

testing "sort key range with two -k options" "sort -k 2,2n -k 1,1r input" "\
d 2
b 2
c 3
" "\
c 3
b 2
d 2
" ""

testing "sort with non-default leading delim 1" "sort -n -k2 -t/ input" "\
/a/2
/b/1
" "\
/a/2
/b/1
" ""

testing "sort with non-default leading delim 2" "sort -n -k3 -t/ input" "\
/b/1
/a/2
" "\
/b/1
/a/2
" ""

testing "sort with non-default leading delim 3" "sort -n -k3 -t/ input" "\
//a/2
//b/1
" "\
//a/2
//b/1
" ""

testing "sort with non-default leading delim 4" "sort -t: -k1,1 input" "\
a:b
a/a:a
" "\
a/a:a
a:b
" ""

testing "sort with ENDCHAR" "sort -t. -k1,1.1 -k2 input" "\
ab.1
aa.2
" "\
aa.2
ab.1
" ""

testing "glibc build sort" "sort -t. -k 1,1 -k 2n,2n -k 3 input" "\
GLIBC_2.1
GLIBC_2.1.1
GLIBC_2.2
GLIBC_2.2.1
GLIBC_2.10
GLIBC_2.20
GLIBC_2.21
" "\
GLIBC_2.21
GLIBC_2.1.1
GLIBC_2.2.1
GLIBC_2.2
GLIBC_2.20
GLIBC_2.10
GLIBC_2.1
" ""

testing "glibc build sort unique" "sort -u -t. -k 1,1 -k 2n,2n -k 3 input" "\
GLIBC_2.1
GLIBC_2.1.1
GLIBC_2.2
GLIBC_2.2.1
GLIBC_2.10
GLIBC_2.20
GLIBC_2.21
" "\
GLIBC_2.10
GLIBC_2.2.1
GLIBC_2.1.1
GLIBC_2.20
GLIBC_2.2
GLIBC_2.1
GLIBC_2.21
" ""

testing "sort -u should consider field only when discarding" "sort -u -k2 input" "\
a c
" "\
a c
b c
" ""

testing "sort -z outputs NUL terminated lines" "sort -z input" "\
one\0three\0two\0\
" "\
one\0two\0three\0\
" ""

testing "sort key doesn't strip leading blanks, disables fallback global sort" \
"sort -n -k2 -t ' '" " a \n 1 \n 2 \n" "" " 2 \n 1 \n a \n"

testing "sort file in place" \
"sort -o input input && cat input" "\
111
222
" "\
222
111
" ""

testing "sort -sr (stable and reverse) does NOT reverse 'stable' ordering" \
"sort -k2 -r -s input" "\
b 2
d 2
a 1
c 1
" "\
a 1
b 2
c 1
d 2
" ""

testing "sort -h" \
"sort -h input" "\
3e
4m
5y
1023
1024
1025
3000
2K
3k
1M
2E
1Y
" "\
1Y
5y
1M
2E
3k
3e
2K
4m
1023
1025
3000
1024
" ""

# testing "description" "command(s)" "result" "infile" "stdin"

exit $FAILCOUNT