printf.tests 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/sh
  2. # Copyright 2008 by Denys Vlasenko
  3. # Licensed under GPL v2, see file LICENSE for details.
  4. . ./testing.sh
  5. # Need this in order to not execute shell builtin
  6. bb="busybox "
  7. # testing "test name" "command" "expected result" "file input" "stdin"
  8. testing "printf produces no further output 1" \
  9. "${bb}printf '\c' foo" \
  10. "" \
  11. "" ""
  12. testing "printf produces no further output 2" \
  13. "${bb}printf '%s\c' foo bar" \
  14. "foo" \
  15. "" ""
  16. testing "printf repeatedly uses pattern for each argv" \
  17. "${bb}printf '%s\n' foo \$HOME" \
  18. "foo\n$HOME\n" \
  19. "" ""
  20. testing "printf understands %b escaped_string" \
  21. "${bb}printf '%b' 'a\tb' 'c\\d\n' 2>&1; echo \$?" \
  22. "a\tbc\\d\n""0\n" \
  23. "" ""
  24. testing "printf understands %d '\"x' \"'y\" \"'zTAIL\"" \
  25. "${bb}printf '%d\n' '\"x' \"'y\" \"'zTAIL\" 2>&1; echo \$?" \
  26. "120\n""121\n""122\n""0\n" \
  27. "" ""
  28. testing "printf understands %s '\"x' \"'y\" \"'zTAIL\"" \
  29. "${bb}printf '%s\n' '\"x' \"'y\" \"'zTAIL\" 2>&1; echo \$?" \
  30. "\"x\n""'y\n""'zTAIL\n""0\n" \
  31. "" ""
  32. testing "printf understands %23.12f" \
  33. "${bb}printf '|%23.12f|\n' 5.25 2>&1; echo \$?" \
  34. "| 5.250000000000|\n""0\n" \
  35. "" ""
  36. testing "printf understands %*.*f" \
  37. "${bb}printf '|%*.*f|\n' 23 12 5.25 2>&1; echo \$?" \
  38. "| 5.250000000000|\n""0\n" \
  39. "" ""
  40. testing "printf understands %*f with negative width" \
  41. "${bb}printf '|%*f|\n' -23 5.25 2>&1; echo \$?" \
  42. "|5.250000 |\n""0\n" \
  43. "" ""
  44. testing "printf understands %.*f with negative precision" \
  45. "${bb}printf '|%.*f|\n' -12 5.25 2>&1; echo \$?" \
  46. "|5.250000|\n""0\n" \
  47. "" ""
  48. testing "printf understands %*.*f with negative width/precision" \
  49. "${bb}printf '|%*.*f|\n' -23 -12 5.25 2>&1; echo \$?" \
  50. "|5.250000 |\n""0\n" \
  51. "" ""
  52. testing "printf understands %zd" \
  53. "${bb}printf '%zd\n' -5 2>&1; echo \$?" \
  54. "-5\n""0\n" \
  55. "" ""
  56. testing "printf understands %ld" \
  57. "${bb}printf '%ld\n' -5 2>&1; echo \$?" \
  58. "-5\n""0\n" \
  59. "" ""
  60. testing "printf understands %Ld" \
  61. "${bb}printf '%Ld\n' -5 2>&1; echo \$?" \
  62. "-5\n""0\n" \
  63. "" ""
  64. # "FIXED" now to act compatibly
  65. ## We are "more correct" here than bash/coreutils: they happily print -2
  66. ## as if it is a huge unsigned number
  67. #testing "printf handles %u -N" \
  68. # "${bb}printf '%u\n' 1 -2 3 2>&1; echo \$?" \
  69. # "1\n""printf: -2: invalid number\n""0\n""3\n""0\n" \
  70. # "" ""
  71. testing "printf handles %d bad_input" \
  72. "${bb}printf '%d\n' 1 - 2 bad 3 123bad 4 2>&1; echo \$?" \
  73. "1\n""printf: -: invalid number\n""0\n"\
  74. "2\n""printf: bad: invalid number\n""0\n"\
  75. "3\n""printf: 123bad: invalid number\n""0\n"\
  76. "4\n""1\n" \
  77. "" ""
  78. testing "printf aborts on bare %" \
  79. "${bb}printf '%' a b c 2>&1; echo \$?" \
  80. "printf: %: invalid format\n""1\n" \
  81. "" ""
  82. testing "printf aborts on %r" \
  83. "${bb}printf '%r' a b c 2>&1; echo \$?" \
  84. "printf: %r: invalid format\n""1\n" \
  85. "" ""
  86. exit $FAILCOUNT