printf.test 3.3 KB

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