OSDN Git Service

am fa01b4ca: Merge changes Ie1705960,I8b94351f,I661540a9,Id2dbdcab,I1223578c, ...
[android-x86/external-toybox.git] / tests / sort.test
1 #!/bin/bash
2
3 # SUSv4 compliant sort tests.
4 # Copyright 2005, 2012 by Rob Landley <rob@landley.net>
5
6 [ -f testing.sh ] && . testing.sh
7
8 # The basic tests.  These should work even with the small config.
9
10 testing "sort" "sort input" "a\nb\nc\n" "c\na\nb\n" ""
11 testing "sort #2" "sort input" "010\n1\n3\n" "3\n1\n010\n" ""
12 testing "sort stdin" "sort" "a\nb\nc\n" "" "b\na\nc\n"
13 testing "sort numeric" "sort -n input" "1\n3\n010\n" "3\n1\n010\n" ""
14 testing "sort reverse" "sort -r input" "wook\nwalrus\npoint\npabst\naargh\n" \
15         "point\nwook\npabst\naargh\nwalrus\n" ""
16
17 # These tests require the full option set.
18
19 optional SORT_BIG
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
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 # Sorting with keys
31
32 testing "sort one key" "sort -k4,4 input" \
33 "999    3       0       algebra
34 egg     1       2       papyrus
35 7       3       42      soup
36 42      1       3       woot
37 42      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
44 testing "sort key range with numeric option" "sort -k2,3n input" \
45 "42     1       010     zoology
46 42      1       3       woot
47 egg     1       2       papyrus
48 7       3       42      soup
49 999     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
55 testing "sort key range with numeric option and global reverse" \
56 "sort -k2,3n -r input" \
57 "egg    1       2       papyrus
58 42      1       3       woot
59 42      1       010     zoology
60 999     3       0       algebra
61 7       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
67 testing "sort key range with multiple options" "sort -k2,3rn input" \
68 "7      3       42      soup
69 999     3       0       algebra
70 42      1       010     zoology
71 42      1       3       woot
72 egg     1       2       papyrus
73 " "$data" ""
74
75 testing "sort 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
80 testing "sort 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
91 testing "sort -x" "sort -x" "010\na0\n 0c0\n" "" "a0\n010\n 0c0\n"
92
93 optional SORT_FLOAT
94
95 # not numbers < NaN < -infinity < numbers < +infinity
96 testing "sort -g" "sort -g" \
97   "bork\nNaN\n-inf\n0.4\n1.222\n01.37\n2.1\n+infinity\n" "" \
98   "01.37\n1.222\n2.1\n0.4\nNaN\nbork\n-inf\n+infinity\n"
99
100