logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git

split.sh (1550B)


  1. #!/bin/sh
  2. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. WD="$(dirname "$0")/../"
  5. WD="$(realpath "$WD")"
  6. target="$WD/cmd/split"
  7. t()
  8. {
  9. count=$((count+1))
  10. name="$1"
  11. shift
  12. if "$@"; then
  13. printf 'ok %s - %s\n' "$count" "$name"
  14. else
  15. printf '# Command failed: %s\n' "$*"
  16. printf 'not ok %s - %s\n' "$count" "$name"
  17. err=1
  18. fi
  19. }
  20. t_end()
  21. {
  22. rm split_test_*
  23. cd "${oldpwd?}"
  24. rm -r "${tempdir?}"
  25. if [ $count -ne $plans ]
  26. then
  27. printf 'error: Ran %d instead of the planned %d tests\n' "$count" "$plans" >&2
  28. err=1
  29. fi
  30. exit $err
  31. }
  32. check_splits_b32()
  33. {
  34. for i in split_test_b32_*
  35. do
  36. set -- $(wc -c "$i")
  37. if [ "$1" != "32" ]
  38. then
  39. printf "# Expected 32 bytes but '%s' is %s bytes\n" "$i" "$size"
  40. return 1
  41. fi
  42. done
  43. }
  44. check_splits_l10()
  45. {
  46. for i in split_test_l10_*
  47. do
  48. set -- $(wc -l "$i")
  49. if [ "$1" != "10" ]
  50. then
  51. printf "# Expected 10 lines but '%s' has %s lines\n" "$i" "$size"
  52. return 1
  53. fi
  54. done
  55. }
  56. err=0
  57. plans=6
  58. count=0
  59. printf '1..%d\n' "$plans"
  60. oldpwd="$PWD"
  61. tempdir="$(mktemp -d)"
  62. cd "$tempdir" || exit 1
  63. trap t_end EXIT
  64. ###
  65. t split_b32 "$target" -b 32 "$WD/test-cmd/inputs/all_bytes" split_test_b32_
  66. t check_splits check_splits_b32
  67. cat split_test_b32_* > split_test_b32
  68. t compare cmp split_test_b32 "$WD/test-cmd/inputs/all_bytes"
  69. ###
  70. seq 1 100 > test_100
  71. t split_l10 "$target" -l 10 test_100 split_test_l10_
  72. t check_splits check_splits_l10
  73. cat split_test_l10_* > split_test_l10
  74. t compare cmp split_test_l10 test_100