Denis Vlasenko | 50120da | 2008-06-05 08:27:26 +0000 | [diff] [blame] | 1 | #!/bin/sh |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 2 | # Copyright 2008 by Denys Vlasenko |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | # Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 50120da | 2008-06-05 08:27:26 +0000 | [diff] [blame] | 4 | |
Mike Frysinger | caa7940 | 2009-11-04 18:41:22 -0500 | [diff] [blame] | 5 | . ./testing.sh |
Denis Vlasenko | 50120da | 2008-06-05 08:27:26 +0000 | [diff] [blame] | 6 | |
| 7 | # Need this in order to not execute shell builtin |
| 8 | bb="busybox " |
| 9 | |
| 10 | # testing "test name" "command" "expected result" "file input" "stdin" |
| 11 | |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 12 | testing "printf produces no further output 1" \ |
Denis Vlasenko | 50120da | 2008-06-05 08:27:26 +0000 | [diff] [blame] | 13 | "${bb}printf '\c' foo" \ |
| 14 | "" \ |
| 15 | "" "" |
| 16 | |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 17 | testing "printf produces no further output 2" \ |
| 18 | "${bb}printf '%s\c' foo bar" \ |
Denis Vlasenko | 50120da | 2008-06-05 08:27:26 +0000 | [diff] [blame] | 19 | "foo" \ |
| 20 | "" "" |
| 21 | |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 22 | testing "printf repeatedly uses pattern for each argv" \ |
Ron Yorston | 1f27fa9 | 2018-02-09 09:52:52 +0000 | [diff] [blame] | 23 | "${bb}printf '%s\n' foo '$HOME'" \ |
Denis Vlasenko | 50120da | 2008-06-05 08:27:26 +0000 | [diff] [blame] | 24 | "foo\n$HOME\n" \ |
| 25 | "" "" |
| 26 | |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 27 | testing "printf understands %b escaped_string" \ |
| 28 | "${bb}printf '%b' 'a\tb' 'c\\d\n' 2>&1; echo \$?" \ |
| 29 | "a\tbc\\d\n""0\n" \ |
| 30 | "" "" |
| 31 | |
| 32 | testing "printf understands %d '\"x' \"'y\" \"'zTAIL\"" \ |
| 33 | "${bb}printf '%d\n' '\"x' \"'y\" \"'zTAIL\" 2>&1; echo \$?" \ |
| 34 | "120\n""121\n""122\n""0\n" \ |
| 35 | "" "" |
| 36 | |
| 37 | testing "printf understands %s '\"x' \"'y\" \"'zTAIL\"" \ |
| 38 | "${bb}printf '%s\n' '\"x' \"'y\" \"'zTAIL\" 2>&1; echo \$?" \ |
| 39 | "\"x\n""'y\n""'zTAIL\n""0\n" \ |
| 40 | "" "" |
| 41 | |
| 42 | testing "printf understands %23.12f" \ |
| 43 | "${bb}printf '|%23.12f|\n' 5.25 2>&1; echo \$?" \ |
| 44 | "| 5.250000000000|\n""0\n" \ |
| 45 | "" "" |
| 46 | |
| 47 | testing "printf understands %*.*f" \ |
| 48 | "${bb}printf '|%*.*f|\n' 23 12 5.25 2>&1; echo \$?" \ |
| 49 | "| 5.250000000000|\n""0\n" \ |
| 50 | "" "" |
| 51 | |
| 52 | testing "printf understands %*f with negative width" \ |
| 53 | "${bb}printf '|%*f|\n' -23 5.25 2>&1; echo \$?" \ |
| 54 | "|5.250000 |\n""0\n" \ |
| 55 | "" "" |
| 56 | |
| 57 | testing "printf understands %.*f with negative precision" \ |
| 58 | "${bb}printf '|%.*f|\n' -12 5.25 2>&1; echo \$?" \ |
| 59 | "|5.250000|\n""0\n" \ |
| 60 | "" "" |
| 61 | |
| 62 | testing "printf understands %*.*f with negative width/precision" \ |
| 63 | "${bb}printf '|%*.*f|\n' -23 -12 5.25 2>&1; echo \$?" \ |
| 64 | "|5.250000 |\n""0\n" \ |
| 65 | "" "" |
| 66 | |
| 67 | testing "printf understands %zd" \ |
| 68 | "${bb}printf '%zd\n' -5 2>&1; echo \$?" \ |
| 69 | "-5\n""0\n" \ |
| 70 | "" "" |
| 71 | |
| 72 | testing "printf understands %ld" \ |
| 73 | "${bb}printf '%ld\n' -5 2>&1; echo \$?" \ |
| 74 | "-5\n""0\n" \ |
| 75 | "" "" |
| 76 | |
Denis Vlasenko | 5f11662 | 2008-07-18 18:41:55 +0000 | [diff] [blame] | 77 | testing "printf understands %Ld" \ |
| 78 | "${bb}printf '%Ld\n' -5 2>&1; echo \$?" \ |
| 79 | "-5\n""0\n" \ |
| 80 | "" "" |
| 81 | |
Bernhard Reutner-Fischer | 3db4e7f | 2018-10-19 15:25:41 +0200 | [diff] [blame] | 82 | testing "printf handles positive numbers for %d" \ |
| 83 | "${bb}printf '%d\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \ |
| 84 | "3\n"\ |
| 85 | "3\n"\ |
| 86 | "3\n"\ |
| 87 | "3\n""0\n" \ |
| 88 | "" "" |
| 89 | |
| 90 | testing "printf handles positive numbers for %i" \ |
| 91 | "${bb}printf '%i\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \ |
| 92 | "3\n"\ |
| 93 | "3\n"\ |
| 94 | "3\n"\ |
| 95 | "3\n""0\n" \ |
| 96 | "" "" |
| 97 | |
| 98 | testing "printf handles positive numbers for %x" \ |
| 99 | "${bb}printf '%x\n' 42 +42 ' 42' ' +42' 2>&1; echo \$?" \ |
| 100 | "2a\n"\ |
| 101 | "2a\n"\ |
| 102 | "2a\n"\ |
| 103 | "2a\n""0\n" \ |
| 104 | "" "" |
| 105 | |
| 106 | testing "printf handles positive numbers for %f" \ |
| 107 | "${bb}printf '%0.3f\n' .42 +.42 ' .42' ' +.42' 2>&1; echo \$?" \ |
| 108 | "0.420\n"\ |
| 109 | "0.420\n"\ |
| 110 | "0.420\n"\ |
| 111 | "0.420\n""0\n" \ |
| 112 | "" "" |
| 113 | |
| 114 | |
Denys Vlasenko | 2441060 | 2009-06-13 23:41:57 +0200 | [diff] [blame] | 115 | # "FIXED" now to act compatibly |
| 116 | ## We are "more correct" here than bash/coreutils: they happily print -2 |
| 117 | ## as if it is a huge unsigned number |
| 118 | #testing "printf handles %u -N" \ |
| 119 | # "${bb}printf '%u\n' 1 -2 3 2>&1; echo \$?" \ |
| 120 | # "1\n""printf: -2: invalid number\n""0\n""3\n""0\n" \ |
| 121 | # "" "" |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 123 | testing "printf handles %d bad_input" \ |
| 124 | "${bb}printf '%d\n' 1 - 2 bad 3 123bad 4 2>&1; echo \$?" \ |
Dan Fandrich | eb2bf5b | 2010-09-02 18:38:00 -0700 | [diff] [blame] | 125 | "1\n""printf: invalid number '-'\n""0\n"\ |
| 126 | "2\n""printf: invalid number 'bad'\n""0\n"\ |
| 127 | "3\n""printf: invalid number '123bad'\n""0\n"\ |
Denys Vlasenko | bf4aeed | 2009-06-18 22:22:04 +0200 | [diff] [blame] | 128 | "4\n""1\n" \ |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 129 | "" "" |
| 130 | |
Denis Vlasenko | 0f683f8 | 2008-07-17 09:17:51 +0000 | [diff] [blame] | 131 | testing "printf aborts on bare %" \ |
Denis Vlasenko | a48656b | 2008-07-18 11:10:51 +0000 | [diff] [blame] | 132 | "${bb}printf '%' a b c 2>&1; echo \$?" \ |
| 133 | "printf: %: invalid format\n""1\n" \ |
| 134 | "" "" |
| 135 | |
| 136 | testing "printf aborts on %r" \ |
| 137 | "${bb}printf '%r' a b c 2>&1; echo \$?" \ |
| 138 | "printf: %r: invalid format\n""1\n" \ |
Denis Vlasenko | 0f683f8 | 2008-07-17 09:17:51 +0000 | [diff] [blame] | 139 | "" "" |
| 140 | |
Denis Vlasenko | 50120da | 2008-06-05 08:27:26 +0000 | [diff] [blame] | 141 | exit $FAILCOUNT |