aboutsummaryrefslogtreecommitdiff
path: root/tests/sort.test
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2014-09-20 13:09:14 -0500
committerRob Landley <rob@landley.net>2014-09-20 13:09:14 -0500
commit387edf547eb09b27ca6d49772eb048d729f09cf4 (patch)
tree59d482f33735690cab6d90723393afa1e2c8dce5 /tests/sort.test
parentd3df423a6cde0c6282658ff628574771d3824d71 (diff)
downloadtoybox-387edf547eb09b27ca6d49772eb048d729f09cf4.tar.gz
Move testsuite out of scripts/test into its own top level tests directory, and make ctrl-c kill "make test" more reliably.
Diffstat (limited to 'tests/sort.test')
-rwxr-xr-xtests/sort.test100
1 files changed, 100 insertions, 0 deletions
diff --git a/tests/sort.test b/tests/sort.test
new file mode 100755
index 00000000..7bd413f7
--- /dev/null
+++ b/tests/sort.test
@@ -0,0 +1,100 @@
+#!/bin/bash
+
+# SUSv4 compliant sort tests.
+# Copyright 2005, 2012 by Rob Landley <rob@landley.net>
+
+[ -f testing.sh ] && . testing.sh
+
+# The basic tests. These should work even with the small config.
+
+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 SORT_BIG
+# Longish chunk of data re-used by the next few tests. The expected output
+# varies, but the input (this) is the same.
+
+data="42 1 3 woot
+42 1 010 zoology
+egg 1 2 papyrus
+7 3 42 soup
+999 3 0 algebra
+"
+
+# 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" ""
+
+# The numeric sort orders field 2, ignores field 3 (because numeric sort stops
+# at the whitespace), then the global fallback sort does an alpha sort on
+# the whole string (starting at the beginning of the line).
+
+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" ""
+
+# Numeric sort on field 2 (again, ignore field 3 because it's numeric),
+# then do a _reversed_ alpha sort on the whole line as a tiebreaker.
+
+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" ""
+
+# Reversed numeric sort on field 2 (numeric ignores field 3), then
+# break ties with alpha sort on whole line.
+
+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 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"
+
+# Test case contributed by Joey Hess:
+
+testing "sort key edge case with -t" "sort -n -k4 -t/" \
+"/usr/lib/finish-install.d/1
+/usr/lib/finish-install.d/4
+/usr/lib/prebaseconfig.d/2
+/usr/lib/prebaseconfig.d/6
+" "" "/usr/lib/finish-install.d/1
+/usr/lib/prebaseconfig.d/2
+/usr/lib/finish-install.d/4
+/usr/lib/prebaseconfig.d/6
+"
+
+testing "sort -x" "sort -x" "010\na0\n 0c0\n" "" "a0\n010\n 0c0\n"
+
+optional SORT_FLOAT
+
+# not numbers < NaN < -infinity < numbers < +infinity
+testing "sort -g" "sort -g" \
+ "bork\nNaN\n-inf\n0.4\n1.222\n01.37\n2.1\n+infinity\n" "" \
+ "01.37\n1.222\n2.1\n0.4\nNaN\nbork\n-inf\n+infinity\n"
+
+