sort.test 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/bin/bash
  2. # SUSv4 compliant sort tests.
  3. # Copyright 2005, 2012 by Rob Landley <rob@landley.net>
  4. [ -f testing.sh ] && . testing.sh
  5. # The basic tests. These should work even with the small config.
  6. testing "unknown argument" 'sort --oops 2>/dev/null ; echo $?' "2\n" "" ""
  7. testing "sort" "sort input" "a\nb\nc\n" "c\na\nb\n" ""
  8. testing "#2" "sort input" "010\n1\n3\n" "3\n1\n010\n" ""
  9. testing "stdin" "sort" "a\nb\nc\n" "" "b\na\nc\n"
  10. testing "numeric" "sort -n input" "1\n3\n010\n" "3\n1\n010\n" ""
  11. testing "reverse" "sort -r input" "wook\nwalrus\npoint\npabst\naargh\n" \
  12. "point\nwook\npabst\naargh\nwalrus\n" ""
  13. testing "sort -o" "sort input -o output && cat output" "a\nb\nc\n" "c\na\nb\n" ""
  14. testing "sort -o same" "sort input -o input && cat input" "a\nb\nc\n" "c\na\nb\n" ""
  15. # Longish chunk of data re-used by the next few tests. The expected output
  16. # varies, but the input (this) is the same.
  17. data="42 1 3 woot
  18. 42 1 010 zoology
  19. egg 1 2 papyrus
  20. 7 3 42 soup
  21. 999 3 0 algebra
  22. "
  23. # Sorting with keys
  24. testing "one key" "sort -k4,4 input" \
  25. "999 3 0 algebra
  26. egg 1 2 papyrus
  27. 7 3 42 soup
  28. 42 1 3 woot
  29. 42 1 010 zoology
  30. " "$data" ""
  31. # The numeric sort orders field 2, ignores field 3 (because numeric sort stops
  32. # at the whitespace), then the global fallback sort does an alpha sort on
  33. # the whole string (starting at the beginning of the line).
  34. testing "key range with numeric option" "sort -k2,3n input" \
  35. "42 1 010 zoology
  36. 42 1 3 woot
  37. egg 1 2 papyrus
  38. 7 3 42 soup
  39. 999 3 0 algebra
  40. " "$data" ""
  41. # Numeric sort on field 2 (again, ignore field 3 because it's numeric),
  42. # then do a _reversed_ alpha sort on the whole line as a tiebreaker.
  43. testing "key range with numeric option and global reverse" \
  44. "sort -k2,3n -r input" \
  45. "egg 1 2 papyrus
  46. 42 1 3 woot
  47. 42 1 010 zoology
  48. 999 3 0 algebra
  49. 7 3 42 soup
  50. " "$data" ""
  51. # Reversed numeric sort on field 2 (numeric ignores field 3), then
  52. # break ties with alpha sort on whole line.
  53. testing "key range with multiple options" "sort -k2,3rn input" \
  54. "7 3 42 soup
  55. 999 3 0 algebra
  56. 42 1 010 zoology
  57. 42 1 3 woot
  58. egg 1 2 papyrus
  59. " "$data" ""
  60. testing "key doesn't strip leading blanks, disables fallback global sort" \
  61. "sort -n -k2 -t ' '" " a \n 1 \n 2 \n" "" " 2 \n 1 \n a \n"
  62. # Test case contributed by Joey Hess:
  63. testing "key edge case with -t" "sort -n -k4 -t/" \
  64. "/usr/lib/finish-install.d/1
  65. /usr/lib/finish-install.d/4
  66. /usr/lib/prebaseconfig.d/2
  67. /usr/lib/prebaseconfig.d/6
  68. " "" "/usr/lib/finish-install.d/1
  69. /usr/lib/prebaseconfig.d/2
  70. /usr/lib/finish-install.d/4
  71. /usr/lib/prebaseconfig.d/6
  72. "
  73. toyonly testing "-x" "sort -x" "010\na0\n 0c0\n" "" "a0\n010\n 0c0\n"
  74. # Test that -f applies to key or fallback independently
  75. testing "" "sort -k2,2f" "A b b\na B C\na B a\n" "" "a B a\nA b b\na B C\n"
  76. testing "" "sort -k2,2" "a B C\na B a\nA b b\n" "" "a B a\nA b b\na B C\n"
  77. testing "" "sort -f -k2,2" "A b b\na B C\na B a\n" "" "a B a\nA b b\na B C\n"
  78. testing "" "sort -t, -k3n" "3,4,1,2\n4,1,2,3\n1,2,3,4\n2,3,4,1\n" "" \
  79. "1,2,3,4\n2,3,4,1\n4,1,2,3\n3,4,1,2\n"
  80. toyonly testing "-kx" "sort -k1,1x" "3\na\n0c\n" "" "0c\na\n3\n"
  81. # This has irredeemable version skew on the host and no standard defining it.
  82. # testing "-V" "LANG=c sort -V" \
  83. # "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" "" \
  84. # "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"
  85. testcmd "-c" "-c 2>&1 | grep -o [0-9]*" "3\n" "" "a\nb\na\nc"
  86. testcmd "-uc" "-uc 2>&1 | grep -o [0-9]*" "3\n" "" "a\nb\nb\nc"
  87. testcmd "-C 1" "-C || echo yes" "yes\n" "" "one\ntwo\nthree"
  88. testcmd "-C 2" "-C && echo yes" "yes\n" "" "a\nb\nc\n"
  89. optional SORT_FLOAT
  90. # not numbers < NaN < -infinity < numbers < +infinity
  91. testing "-g" "sort -g" \
  92. "bork\nNaN\n-inf\n0.4\n1.222\n01.37\n2.1\n+infinity\n" "" \
  93. "01.37\n1.222\n2.1\n0.4\nNaN\nbork\n-inf\n+infinity\n"
  94. # -n without number sorts as leading zero, but fallback is whole string
  95. testcmd '-n without number sorts as leading zero' '-n' \
  96. '-1\n0A\n0D\nC\n1z\n3b\n' '' '1z\n0D\n3b\nC\n-1\n0A\n'
  97. testcmd '-u implies -s' '-uk2,2n' 'zero 1\nthree 2\nfour 3\n' '' \
  98. 'zero 1\none 1\nfour 3\ntwo 1\nthree 2\nalso 1'