3
0

printf.tests 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 understands %%" \
  65. "${bb}printf '%%\n' 2>&1; echo \$?" \
  66. "%\n""0\n" \
  67. "" ""
  68. testing "printf handles positive numbers for %d" \
  69. "${bb}printf '%d\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \
  70. "3\n"\
  71. "3\n"\
  72. "3\n"\
  73. "3\n""0\n" \
  74. "" ""
  75. testing "printf handles positive numbers for %i" \
  76. "${bb}printf '%i\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \
  77. "3\n"\
  78. "3\n"\
  79. "3\n"\
  80. "3\n""0\n" \
  81. "" ""
  82. testing "printf handles positive numbers for %x" \
  83. "${bb}printf '%x\n' 42 +42 ' 42' ' +42' 2>&1; echo \$?" \
  84. "2a\n"\
  85. "2a\n"\
  86. "2a\n"\
  87. "2a\n""0\n" \
  88. "" ""
  89. testing "printf handles positive numbers for %f" \
  90. "${bb}printf '%0.3f\n' .42 +.42 ' .42' ' +.42' 2>&1; echo \$?" \
  91. "0.420\n"\
  92. "0.420\n"\
  93. "0.420\n"\
  94. "0.420\n""0\n" \
  95. "" ""
  96. # "FIXED" now to act compatibly
  97. ## We are "more correct" here than bash/coreutils: they happily print -2
  98. ## as if it is a huge unsigned number
  99. #testing "printf handles %u -N" \
  100. # "${bb}printf '%u\n' 1 -2 3 2>&1; echo \$?" \
  101. # "1\n""printf: -2: invalid number\n""0\n""3\n""0\n" \
  102. # "" ""
  103. testing "printf handles %d bad_input" \
  104. "${bb}printf '%d\n' 1 - 2 bad 3 123bad 4 2>&1; echo \$?" \
  105. "1\n""printf: invalid number '-'\n""0\n"\
  106. "2\n""printf: invalid number 'bad'\n""0\n"\
  107. "3\n""printf: invalid number '123bad'\n""0\n"\
  108. "4\n""1\n" \
  109. "" ""
  110. testing "printf aborts on bare %" \
  111. "${bb}printf '%' a b c 2>&1; echo \$?" \
  112. "printf: %: invalid format\n""1\n" \
  113. "" ""
  114. testing "printf aborts on %r" \
  115. "${bb}printf '%r' a b c 2>&1; echo \$?" \
  116. "printf: %r: invalid format\n""1\n" \
  117. "" ""
  118. testing "printf treats leading 0 as flag" \
  119. "${bb}printf '%0*d\n' 2 1 2>&1; echo \$?" \
  120. "01\n""0\n" \
  121. "" ""
  122. testing "printf handles multiple flags" \
  123. "${bb}printf '%0 d\n' 2 2>&1; echo \$?" \
  124. " 2\n""0\n" \
  125. "" ""
  126. exit $FAILCOUNT