printf.test 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/bash
  2. # Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
  3. # Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>
  4. [ -f testing.sh ] && . testing.sh
  5. #testing "name" "command" "result" "infile" "stdin"
  6. # Disable shell builtin
  7. PRINTF="$(which printf)"
  8. testing "text" "$PRINTF TEXT" "TEXT" "" ""
  9. # TODO: we have to use \x1b rather than \e in the expectations because
  10. # the Mac is stuck on bash 3.2 which doesn't support \e. This can go
  11. # away when we have a usable toysh.
  12. testing "escapes" "$PRINTF 'one\ntwo\n\v\t\r\f\e\b\athree'" \
  13. "one\ntwo\n\v\t\r\f\x1b\b\athree" "" ""
  14. testing "%b escapes" "$PRINTF %b 'one\ntwo\n\v\t\r\f\e\b\athree'" \
  15. "one\ntwo\n\v\t\r\f\x1b\b\athree" "" ""
  16. testing "null" "$PRINTF 'x\0y' | od -An -tx1" ' 78 00 79\n' "" ""
  17. testing "trailing slash" "$PRINTF 'abc\'" 'abc\' "" ""
  18. testing "octal" "$PRINTF ' \1\002\429\045x'" ' \001\002"9%x' "" ""
  19. testing "not octal" "$PRINTF '\9'" '\9' "" ""
  20. testing "hex" "$PRINTF 'A\x1b\x2B\x3Q\xa' | od -An -tx1" \
  21. ' 41 1b 2b 03 51 0a\n' "" ""
  22. testing "%x" "$PRINTF '%x\n' 0x2a" "2a\n" "" ""
  23. testing "%d 42" "$PRINTF %d 42" "42" "" ""
  24. testing "%d 0x2a" "$PRINTF %d 0x2a" "42" "" ""
  25. testing "%d 052" "$PRINTF %d 052" "42" "" ""
  26. testing "%s width precision" \
  27. "$PRINTF '%3s,%.3s,%10s,%10.3s' abcde fghij klmno pqrst" \
  28. "abcde,fgh, klmno, pqr" "" ""
  29. # posix: "The format operand shall be reused as often as necessary to satisfy
  30. # the argument operands."
  31. testing "extra args" "$PRINTF 'abc%s!%ddef\n' X 42 ARG 36" \
  32. "abcX!42def\nabcARG!36def\n" "" ""
  33. testing "'%3c'" "$PRINTF '%3c' x" " x" "" ""
  34. testing "'%-3c'" "$PRINTF '%-3c' x" "x " "" ""
  35. testing "'%+d'" "$PRINTF '%+d' 5" "+5" "" ""
  36. testing "'%5d%4d' 1 21 321 4321 54321" \
  37. "$PRINTF '%5d%4d' 1 21 321 4321 54321" " 1 21 321432154321 0" "" ""
  38. testing "'%c %c' 78 79" "$PRINTF '%c %c' 78 79" "7 7" "" ""
  39. testing "'%d %d' 78 79" "$PRINTF '%d %d' 78 79" "78 79" "" ""
  40. testing "'%f %f' 78 79" "$PRINTF '%f %f' 78 79" \
  41. "78.000000 79.000000" "" ""
  42. testing "'f f' 78 79" "$PRINTF 'f f' 78 79 2>/dev/null" "f f" "" ""
  43. testing "'%i %i' 78 79" "$PRINTF '%i %i' 78 79" "78 79" "" ""
  44. testing "'%o %o' 78 79" "$PRINTF '%o %o' 78 79" "116 117" "" ""
  45. testing "'%u %u' 78 79" "$PRINTF '%u %u' 78 79" "78 79" "" ""
  46. testing "'%u %u' -1 -2" "$PRINTF '%u %u' -1 -2" \
  47. "18446744073709551615 18446744073709551614" "" ""
  48. testing "'%x %X' 78 79" "$PRINTF '%x %X' 78 79" "4e 4F" "" ""
  49. testing "'%g %G' 78 79" "$PRINTF '%g %G' 78 79" "78 79" "" ""
  50. testing "'%s %s' 78 79" "$PRINTF '%s %s' 78 79" "78 79" "" ""
  51. testing "%.s acts like %.0s" "$PRINTF %.s_ 1 2 3 4 5" "_____" "" ""
  52. testing "corner case" "$PRINTF '\\8'" '\8' '' ''
  53. # The posix spec explicitly specifies inconsistent behavior,
  54. # so treating the \0066 in %b like the \0066 not in %b is wrong because posix.
  55. testing "printf posix inconsistency" "$PRINTF '\\0066-%b' '\\0066'" "\x066-6" \
  56. "" ""
  57. testing "printf \x" "$PRINTF 'A\x1b\x2B\x3Q\xa' | od -An -tx1" \
  58. " 41 1b 2b 03 51 0a\n" "" ""
  59. testing "printf \c" "$PRINTF 'one\ctwo'" "one" "" ""
  60. # An extra leading 0 is fine for %b, but not as a direct escape, for some
  61. # reason...
  62. testing "printf octal %b" "$PRINTF '\0007%b' '\0007' | xxd -p" "003707\n" "" ""
  63. # Unlike echo, printf errors out on bad hex.
  64. testcmd "invalid hex 1" "'one\xvdtwo' 2>/dev/null || echo err" "oneerr\n" "" ""
  65. testcmd "invalid hex 2" "'one\xavtwo'" "one\nvtwo" "" ""
  66. # But bad octal is shown as text.
  67. testcmd "invalid oct" "'one\079two'" "one\a9two" "" ""
  68. # extension for ESC
  69. testcmd "%b \e" "'%b' '\\e' | xxd -p" "1b\n" "" ""
  70. testcmd "\e" "'\\e' | xxd -p" "1b\n" "" ""