blob: 389b9fd7c2ed1723bf62b4c31278ca14e23a84d9 [file] [log] [blame]
Rob Landleyf901f242008-06-22 04:18:39 -05001#!/bin/bash
2
3# SUSv3 compliant sort tests.
4# Copyright 2005 by Rob Landley <rob@landley.net>
5# Licensed under GPL v2, see file LICENSE for details.
6
7. testing.sh
8
9# The basic tests. These should work even with the small config.
10
11testing "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" \
16 "point\nwook\npabst\naargh\nwalrus\n" ""
17
18# These tests require the full option set.
19
20optional SORT_BIG
21# Longish chunk of data re-used by the next few tests. The expected output
22# varies, but the input (this) is the same.
23
24data="42 1 3 woot
2542 1 010 zoology
26egg 1 2 papyrus
277 3 42 soup
28999 3 0 algebra
29"
30
31# Sorting with keys
32
33testing "sort one key" "sort -k4,4 input" \
34"999 3 0 algebra
35egg 1 2 papyrus
367 3 42 soup
3742 1 3 woot
3842 1 010 zoology
39" "$data" ""
40
41# The numeric sort orders field 2, ignores field 3 (because numeric sort stops
42# at the whitespace), then the global fallback sort does an alpha sort on
43# the whole string (starting at the beginning of the line).
44
45testing "sort key range with numeric option" "sort -k2,3n input" \
46"42 1 010 zoology
4742 1 3 woot
48egg 1 2 papyrus
497 3 42 soup
50999 3 0 algebra
51" "$data" ""
52
53# Numeric sort on field 2 (again, ignore field 3 because it's numeric),
54# then do a _reversed_ alpha sort on the whole line as a tiebreaker.
55
56testing "sort key range with numeric option and global reverse" \
57"sort -k2,3n -r input" \
58"egg 1 2 papyrus
5942 1 3 woot
6042 1 010 zoology
61999 3 0 algebra
627 3 42 soup
63" "$data" ""
64
65# Reversed numeric sort on field 2 (numeric ignores field 3), then
66# break ties with alpha sort on whole line.
67
68testing "sort key range with multiple options" "sort -k2,3rn input" \
69"7 3 42 soup
70999 3 0 algebra
7142 1 010 zoology
7242 1 3 woot
73egg 1 2 papyrus
74" "$data" ""
75
76testing "sort key doesn't strip leading blanks, disables fallback global sort" \
77"sort -n -k2 -t ' '" " a \n 1 \n 2 \n" "" " 2 \n 1 \n a \n"
78
79# Test case contributed by Joey Hess:
80
81testing "sort key edge case with -t" "sort -n -k4 -t/" \
82"/usr/lib/finish-install.d/1
83/usr/lib/finish-install.d/4
84/usr/lib/prebaseconfig.d/2
85/usr/lib/prebaseconfig.d/6
86" "" "/usr/lib/finish-install.d/1
87/usr/lib/prebaseconfig.d/2
88/usr/lib/finish-install.d/4
89/usr/lib/prebaseconfig.d/6
90"
91
92exit $FAILCOUNT