printf.tests 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/bin/sh
  2. # Copyright 2008 by Denys Vlasenko
  3. # Licensed under GPLv2, see file LICENSE in this source tree.
  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. testing "printf handles positive numbers for %d" \
  65. "${bb}printf '%d\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \
  66. "3\n"\
  67. "3\n"\
  68. "3\n"\
  69. "3\n""0\n" \
  70. "" ""
  71. testing "printf handles positive numbers for %i" \
  72. "${bb}printf '%i\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \
  73. "3\n"\
  74. "3\n"\
  75. "3\n"\
  76. "3\n""0\n" \
  77. "" ""
  78. testing "printf handles positive numbers for %x" \
  79. "${bb}printf '%x\n' 42 +42 ' 42' ' +42' 2>&1; echo \$?" \
  80. "2a\n"\
  81. "2a\n"\
  82. "2a\n"\
  83. "2a\n""0\n" \
  84. "" ""
  85. testing "printf handles positive numbers for %f" \
  86. "${bb}printf '%0.3f\n' .42 +.42 ' .42' ' +.42' 2>&1; echo \$?" \
  87. "0.420\n"\
  88. "0.420\n"\
  89. "0.420\n"\
  90. "0.420\n""0\n" \
  91. "" ""
  92. # "FIXED" now to act compatibly
  93. ## We are "more correct" here than bash/coreutils: they happily print -2
  94. ## as if it is a huge unsigned number
  95. #testing "printf handles %u -N" \
  96. # "${bb}printf '%u\n' 1 -2 3 2>&1; echo \$?" \
  97. # "1\n""printf: -2: invalid number\n""0\n""3\n""0\n" \
  98. # "" ""
  99. testing "printf handles %d bad_input" \
  100. "${bb}printf '%d\n' 1 - 2 bad 3 123bad 4 2>&1; echo \$?" \
  101. "1\n""printf: invalid number '-'\n""0\n"\
  102. "2\n""printf: invalid number 'bad'\n""0\n"\
  103. "3\n""printf: invalid number '123bad'\n""0\n"\
  104. "4\n""1\n" \
  105. "" ""
  106. testing "printf aborts on bare %" \
  107. "${bb}printf '%' a b c 2>&1; echo \$?" \
  108. "printf: %: invalid format\n""1\n" \
  109. "" ""
  110. testing "printf aborts on %r" \
  111. "${bb}printf '%r' a b c 2>&1; echo \$?" \
  112. "printf: %r: invalid format\n""1\n" \
  113. "" ""
  114. exit $FAILCOUNT