xref: /aosp_15_r20/external/toybox/tests/sort.test (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1#!/bin/bash
2
3# SUSv4 compliant sort tests.
4# Copyright 2005, 2012 by Rob Landley <[email protected]>
5
6[ -f testing.sh ] && . testing.sh
7
8# The basic tests.  These should work even with the small config.
9
10testing "unknown argument" 'sort --oops 2>/dev/null ; echo $?' "2\n" "" ""
11testing "sort" "sort input" "a\nb\nc\n" "c\na\nb\n" ""
12testing "#2" "sort input" "010\n1\n3\n" "3\n1\n010\n" ""
13testing "stdin" "sort" "a\nb\nc\n" "" "b\na\nc\n"
14testing "numeric" "sort -n input" "1\n3\n010\n" "3\n1\n010\n" ""
15testing "reverse" "sort -r input" "wook\nwalrus\npoint\npabst\naargh\n" \
16	"point\nwook\npabst\naargh\nwalrus\n" ""
17testing "sort -o" "sort input -o output && cat output" "a\nb\nc\n" "c\na\nb\n" ""
18testing "sort -o same" "sort input -o input && cat input" "a\nb\nc\n" "c\na\nb\n" ""
19
20# Longish chunk of data re-used by the next few tests.  The expected output
21# varies, but the input (this) is the same.
22
23data="42	1	3	woot
2442	1	010	zoology
25egg	1	2	papyrus
267	3	42	soup
27999	3	0	algebra
28"
29
30# Sorting with keys
31
32testing "one key" "sort -k4,4 input" \
33"999	3	0	algebra
34egg	1	2	papyrus
357	3	42	soup
3642	1	3	woot
3742	1	010	zoology
38" "$data" ""
39
40# The numeric sort orders field 2, ignores field 3 (because numeric sort stops
41# at the whitespace), then the global fallback sort does an alpha sort on
42# the whole string (starting at the beginning of the line).
43
44testing "key range with numeric option" "sort -k2,3n input" \
45"42	1	010	zoology
4642	1	3	woot
47egg	1	2	papyrus
487	3	42	soup
49999	3	0	algebra
50" "$data" ""
51
52# Numeric sort on field 2 (again, ignore field 3 because it's numeric),
53# then do a _reversed_ alpha sort on the whole line as a tiebreaker.
54
55testing "key range with numeric option and global reverse" \
56"sort -k2,3n -r input" \
57"egg	1	2	papyrus
5842	1	3	woot
5942	1	010	zoology
60999	3	0	algebra
617	3	42	soup
62" "$data" ""
63
64# Reversed numeric sort on field 2 (numeric ignores field 3), then
65# break ties with alpha sort on whole line.
66
67testing "key range with multiple options" "sort -k2,3rn input" \
68"7	3	42	soup
69999	3	0	algebra
7042	1	010	zoology
7142	1	3	woot
72egg	1	2	papyrus
73" "$data" ""
74
75testing "key doesn't strip leading blanks, disables fallback global sort" \
76"sort -n -k2 -t ' '" " a \n 1 \n 2 \n" "" " 2 \n 1 \n a \n"
77
78# Test case contributed by Joey Hess:
79
80testing "key edge case with -t" "sort -n -k4 -t/" \
81"/usr/lib/finish-install.d/1
82/usr/lib/finish-install.d/4
83/usr/lib/prebaseconfig.d/2
84/usr/lib/prebaseconfig.d/6
85" "" "/usr/lib/finish-install.d/1
86/usr/lib/prebaseconfig.d/2
87/usr/lib/finish-install.d/4
88/usr/lib/prebaseconfig.d/6
89"
90
91toyonly testing "-x" "sort -x" "010\na0\n 0c0\n" "" "a0\n010\n 0c0\n"
92
93# Test that -f applies to key or fallback independently
94
95testing "" "sort -k2,2f" "A b b\na B C\na B a\n" "" "a B a\nA b b\na B C\n"
96testing "" "sort -k2,2" "a B C\na B a\nA b b\n" "" "a B a\nA b b\na B C\n"
97testing "" "sort -f -k2,2" "A b b\na B C\na B a\n" "" "a B a\nA b b\na B C\n"
98testing "" "sort -t, -k3n" "3,4,1,2\n4,1,2,3\n1,2,3,4\n2,3,4,1\n" "" \
99  "1,2,3,4\n2,3,4,1\n4,1,2,3\n3,4,1,2\n"
100toyonly testing "-kx" "sort -k1,1x" "3\na\n0c\n" "" "0c\na\n3\n"
101
102# This has irredeemable version skew on the host and no standard defining it.
103# testing "-V" "LANG=c sort -V" \
104#  "toy-2.37.tar.gz\ntoy-3.4.tar.gz\ntoy-3.12.tar.gz\ntoy-4.16-rc2.tar.gz\ntoy-4.16.tar.gz\n" "" \
105#  "toy-3.12.tar.gz\ntoy-2.37.tar.gz\ntoy-3.4.tar.gz\ntoy-4.16-rc2.tar.gz\ntoy-4.16.tar.gz"
106
107testcmd "-c" "-c 2>&1 | grep -o [0-9]*" "3\n" "" "a\nb\na\nc"
108testcmd "-uc" "-uc 2>&1 | grep -o [0-9]*" "3\n" "" "a\nb\nb\nc"
109testcmd "-C 1" "-C || echo yes" "yes\n" "" "one\ntwo\nthree"
110testcmd "-C 2" "-C && echo yes" "yes\n" "" "a\nb\nc\n"
111
112toyonly testcmd 'negative -k' '-k-2,-2 -k-1r' 'a b z\nd e q\nx e a\nb m n\n' \
113  '' 'a b z\nd e q\nb m n\nx e a\n'
114toyonly testcmd 'negative -k2' '-k-2' 'a b z\nx e a\nd e q\nb m n\n' \
115  '' 'a b z\nd e q\nb m n\nx e a\n'
116
117testcmd 'missing key becomes ""' '-k3r' 'm n o\ng h i\na b c\nd e\nj k\n' \
118  '' 'a b c\nd e\ng h i\nj k\nm n o\n'
119toyonly testcmd 'negative straddle' '-k-1r' 'm n o\nj k\ng h i\nd e\na b c\n' \
120  '' 'a b c\nd e\ng h i\nj k\nm n o\n'
121toyonly testcmd 'missing negative' '-k-3r' 'm n o\ng h i\na b c\nd e\nj k\n' \
122  '' 'a b c\nd e\ng h i\nj k\nm n o\n'
123
124optional TOYBOX_FLOAT
125
126# not numbers < NaN < -infinity < numbers < +infinity
127testing "-g" "sort -g" \
128  "bork\nNaN\n-inf\n0.4\n1.222\n01.37\n2.1\n+infinity\n" "" \
129  "01.37\n1.222\n2.1\n0.4\nNaN\nbork\n-inf\n+infinity\n"
130
131# -n without number sorts as leading zero, but fallback is whole string
132testcmd '-n without number sorts as leading zero' '-n' \
133  '-1\n0A\n0D\nC\n1z\n3b\n' '' '1z\n0D\n3b\nC\n-1\n0A\n'
134
135testcmd '-u implies -s' '-uk2,2n' 'zero 1\nthree 2\nfour 3\n' '' \
136   'zero 1\none 1\nfour 3\ntwo 1\nthree 2\nalso 1'
137