blob: c51a8e475b11b980cca060978fa34d535c6c9082 [file] [log] [blame]
Mike Frysingercaa79402009-11-04 18:41:22 -05001#!/bin/sh
Rob Landley16890752005-09-02 00:41:53 +00002
3# SUSv3 compliant sort tests.
4# Copyright 2005 by Rob Landley <rob@landley.net>
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005# Licensed under GPLv2, see file LICENSE in this source tree.
Rob Landley16890752005-09-02 00:41:53 +00006
Mike Frysingercaa79402009-11-04 18:41:22 -05007. ./testing.sh
Rob Landley16890752005-09-02 00:41:53 +00008
Rob Landley16890752005-09-02 00:41:53 +00009# The basic tests. These should work even with the small busybox.
10
Rob Landley4bb1b042006-03-16 15:20:45 +000011testing "sort" "sort input" "a\nb\nc\n" "c\na\nb\n" ""
12testing "sort #2" "sort input" "010\n1\n3\n" "3\n1\n010\n" ""
13testing "sort stdin" "sort" "a\nb\nc\n" "" "b\na\nc\n"
14testing "sort numeric" "sort -n input" "1\n3\n010\n" "3\n1\n010\n" ""
15testing "sort reverse" "sort -r input" "wook\nwalrus\npoint\npabst\naargh\n" \
Rob Landley16890752005-09-02 00:41:53 +000016 "point\nwook\npabst\naargh\nwalrus\n" ""
17
18# These tests require the full option set.
19
Rob Landley48c61572005-11-07 08:50:53 +000020optional FEATURE_SORT_BIG
Rob Landley16890752005-09-02 00:41:53 +000021# Longish chunk of data re-used by the next few tests
22
23data="42 1 3 woot
2442 1 010 zoology
25egg 1 2 papyrus
267 3 42 soup
27999 3 0 algebra
28"
29
Denys Vlasenkoaa42d132009-12-13 02:09:22 +010030# testing "description" "command(s)" "result" "infile" "stdin"
31
Rob Landley16890752005-09-02 00:41:53 +000032# Sorting with keys
33
Rob Landley4bb1b042006-03-16 15:20:45 +000034testing "sort one key" "sort -k4,4 input" \
Rob Landley16890752005-09-02 00:41:53 +000035"999 3 0 algebra
36egg 1 2 papyrus
377 3 42 soup
3842 1 3 woot
3942 1 010 zoology
40" "$data" ""
41
Rob Landley4bb1b042006-03-16 15:20:45 +000042testing "sort key range with numeric option" "sort -k2,3n input" \
Rob Landley16890752005-09-02 00:41:53 +000043"42 1 010 zoology
4442 1 3 woot
45egg 1 2 papyrus
467 3 42 soup
47999 3 0 algebra
48" "$data" ""
49
Rob Landley16890752005-09-02 00:41:53 +000050testing "sort key range with numeric option and global reverse" \
Rob Landley4bb1b042006-03-16 15:20:45 +000051"sort -k2,3n -r input" \
Rob Landley16890752005-09-02 00:41:53 +000052"egg 1 2 papyrus
5342 1 3 woot
5442 1 010 zoology
55999 3 0 algebra
567 3 42 soup
57" "$data" ""
58
Rob Landley4bb1b042006-03-16 15:20:45 +000059testing "sort key range with multiple options" "sort -k2,3rn input" \
Rob Landley16890752005-09-02 00:41:53 +000060"7 3 42 soup
61999 3 0 algebra
6242 1 010 zoology
6342 1 3 woot
64egg 1 2 papyrus
65" "$data" ""
66
Denis Vlasenko54cf5112007-02-17 18:11:45 +000067testing "sort key range with two -k options" "sort -k 2,2n -k 1,1r input" "\
68d 2
69b 2
70c 3
71" "\
72c 3
73b 2
74d 2
75" ""
76
Denis Vlasenko8336f082007-01-07 00:21:41 +000077testing "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
85testing "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
93testing "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
Denys Vlasenko8aa7cf32015-09-19 22:06:40 +0200101testing "sort with non-default leading delim 4" "sort -t: -k1,1 input" "\
102a:b
103a/a:a
104" "\
105a/a:a
106a:b
107" ""
108
Denys Vlasenko0506e292015-10-19 19:27:26 +0200109testing "sort with ENDCHAR" "sort -t. -k1,1.1 -k2 input" "\
110ab.1
111aa.2
112" "\
113aa.2
114ab.1
115" ""
116
Denys Vlasenko2e86a5c2015-10-19 14:44:51 +0200117testing "glibc build sort" "sort -t. -k 1,1 -k 2n,2n -k 3 input" "\
118GLIBC_2.1
119GLIBC_2.1.1
120GLIBC_2.2
121GLIBC_2.2.1
122GLIBC_2.10
123GLIBC_2.20
124GLIBC_2.21
125" "\
126GLIBC_2.21
127GLIBC_2.1.1
128GLIBC_2.2.1
129GLIBC_2.2
130GLIBC_2.20
131GLIBC_2.10
132GLIBC_2.1
133" ""
134
135testing "glibc build sort unique" "sort -u -t. -k 1,1 -k 2n,2n -k 3 input" "\
136GLIBC_2.1
137GLIBC_2.1.1
138GLIBC_2.2
139GLIBC_2.2.1
140GLIBC_2.10
141GLIBC_2.20
142GLIBC_2.21
143" "\
144GLIBC_2.10
145GLIBC_2.2.1
146GLIBC_2.1.1
147GLIBC_2.20
148GLIBC_2.2
149GLIBC_2.1
150GLIBC_2.21
151" ""
152
Denis Vlasenko8336f082007-01-07 00:21:41 +0000153testing "sort -u should consider field only when discarding" "sort -u -k2 input" "\
154a c
155" "\
156a c
157b c
158" ""
159
Denis Vlasenko3b92eaa2008-02-13 14:30:33 +0000160testing "sort -z outputs NUL terminated lines" "sort -z input" "\
161one\0three\0two\0\
162" "\
163one\0two\0three\0\
164" ""
165
Rob Landley39218932006-06-15 20:50:38 +0000166testing "sort key doesn't strip leading blanks, disables fallback global sort" \
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000167"sort -n -k2 -t ' '" " a \n 1 \n 2 \n" "" " 2 \n 1 \n a \n"
Rob Landley39218932006-06-15 20:50:38 +0000168
Denys Vlasenkoaa42d132009-12-13 02:09:22 +0100169testing "sort file in place" \
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +0200170"sort -o input input && cat input" "\
Denys Vlasenkoaa42d132009-12-13 02:09:22 +0100171111
172222
173" "\
174222
175111
176" ""
177
178# testing "description" "command(s)" "result" "infile" "stdin"
179
Rob Landley16890752005-09-02 00:41:53 +0000180exit $FAILCOUNT