2008-06-05 13:57:26 +05:30
|
|
|
#!/bin/sh
|
2008-07-18 16:40:51 +05:30
|
|
|
# Copyright 2008 by Denys Vlasenko
|
2010-08-16 23:44:46 +05:30
|
|
|
# Licensed under GPLv2, see file LICENSE in this source tree.
|
2008-06-05 13:57:26 +05:30
|
|
|
|
2009-11-05 05:11:22 +05:30
|
|
|
. ./testing.sh
|
2008-06-05 13:57:26 +05:30
|
|
|
|
|
|
|
# Need this in order to not execute shell builtin
|
|
|
|
bb="busybox "
|
|
|
|
|
|
|
|
# testing "test name" "command" "expected result" "file input" "stdin"
|
|
|
|
|
2008-07-18 16:40:51 +05:30
|
|
|
testing "printf produces no further output 1" \
|
2008-06-05 13:57:26 +05:30
|
|
|
"${bb}printf '\c' foo" \
|
|
|
|
"" \
|
|
|
|
"" ""
|
|
|
|
|
2008-07-18 16:40:51 +05:30
|
|
|
testing "printf produces no further output 2" \
|
|
|
|
"${bb}printf '%s\c' foo bar" \
|
2008-06-05 13:57:26 +05:30
|
|
|
"foo" \
|
|
|
|
"" ""
|
|
|
|
|
2008-07-18 16:40:51 +05:30
|
|
|
testing "printf repeatedly uses pattern for each argv" \
|
2018-02-09 15:22:52 +05:30
|
|
|
"${bb}printf '%s\n' foo '$HOME'" \
|
2008-06-05 13:57:26 +05:30
|
|
|
"foo\n$HOME\n" \
|
|
|
|
"" ""
|
|
|
|
|
2008-07-18 16:40:51 +05:30
|
|
|
testing "printf understands %b escaped_string" \
|
|
|
|
"${bb}printf '%b' 'a\tb' 'c\\d\n' 2>&1; echo \$?" \
|
|
|
|
"a\tbc\\d\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf understands %d '\"x' \"'y\" \"'zTAIL\"" \
|
|
|
|
"${bb}printf '%d\n' '\"x' \"'y\" \"'zTAIL\" 2>&1; echo \$?" \
|
|
|
|
"120\n""121\n""122\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf understands %s '\"x' \"'y\" \"'zTAIL\"" \
|
|
|
|
"${bb}printf '%s\n' '\"x' \"'y\" \"'zTAIL\" 2>&1; echo \$?" \
|
|
|
|
"\"x\n""'y\n""'zTAIL\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf understands %23.12f" \
|
|
|
|
"${bb}printf '|%23.12f|\n' 5.25 2>&1; echo \$?" \
|
|
|
|
"| 5.250000000000|\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf understands %*.*f" \
|
|
|
|
"${bb}printf '|%*.*f|\n' 23 12 5.25 2>&1; echo \$?" \
|
|
|
|
"| 5.250000000000|\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf understands %*f with negative width" \
|
|
|
|
"${bb}printf '|%*f|\n' -23 5.25 2>&1; echo \$?" \
|
|
|
|
"|5.250000 |\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf understands %.*f with negative precision" \
|
|
|
|
"${bb}printf '|%.*f|\n' -12 5.25 2>&1; echo \$?" \
|
|
|
|
"|5.250000|\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf understands %*.*f with negative width/precision" \
|
|
|
|
"${bb}printf '|%*.*f|\n' -23 -12 5.25 2>&1; echo \$?" \
|
|
|
|
"|5.250000 |\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf understands %zd" \
|
|
|
|
"${bb}printf '%zd\n' -5 2>&1; echo \$?" \
|
|
|
|
"-5\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf understands %ld" \
|
|
|
|
"${bb}printf '%ld\n' -5 2>&1; echo \$?" \
|
|
|
|
"-5\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
2008-07-19 00:11:55 +05:30
|
|
|
testing "printf understands %Ld" \
|
|
|
|
"${bb}printf '%Ld\n' -5 2>&1; echo \$?" \
|
|
|
|
"-5\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
2018-10-19 18:55:41 +05:30
|
|
|
testing "printf handles positive numbers for %d" \
|
|
|
|
"${bb}printf '%d\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \
|
|
|
|
"3\n"\
|
|
|
|
"3\n"\
|
|
|
|
"3\n"\
|
|
|
|
"3\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf handles positive numbers for %i" \
|
|
|
|
"${bb}printf '%i\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \
|
|
|
|
"3\n"\
|
|
|
|
"3\n"\
|
|
|
|
"3\n"\
|
|
|
|
"3\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf handles positive numbers for %x" \
|
|
|
|
"${bb}printf '%x\n' 42 +42 ' 42' ' +42' 2>&1; echo \$?" \
|
|
|
|
"2a\n"\
|
|
|
|
"2a\n"\
|
|
|
|
"2a\n"\
|
|
|
|
"2a\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf handles positive numbers for %f" \
|
|
|
|
"${bb}printf '%0.3f\n' .42 +.42 ' .42' ' +.42' 2>&1; echo \$?" \
|
|
|
|
"0.420\n"\
|
|
|
|
"0.420\n"\
|
|
|
|
"0.420\n"\
|
|
|
|
"0.420\n""0\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
|
2009-06-14 03:11:57 +05:30
|
|
|
# "FIXED" now to act compatibly
|
|
|
|
## We are "more correct" here than bash/coreutils: they happily print -2
|
|
|
|
## as if it is a huge unsigned number
|
|
|
|
#testing "printf handles %u -N" \
|
|
|
|
# "${bb}printf '%u\n' 1 -2 3 2>&1; echo \$?" \
|
|
|
|
# "1\n""printf: -2: invalid number\n""0\n""3\n""0\n" \
|
|
|
|
# "" ""
|
2008-07-18 16:40:51 +05:30
|
|
|
|
|
|
|
testing "printf handles %d bad_input" \
|
|
|
|
"${bb}printf '%d\n' 1 - 2 bad 3 123bad 4 2>&1; echo \$?" \
|
2010-09-03 07:08:00 +05:30
|
|
|
"1\n""printf: invalid number '-'\n""0\n"\
|
|
|
|
"2\n""printf: invalid number 'bad'\n""0\n"\
|
|
|
|
"3\n""printf: invalid number '123bad'\n""0\n"\
|
2009-06-19 01:52:04 +05:30
|
|
|
"4\n""1\n" \
|
2008-07-18 16:40:51 +05:30
|
|
|
"" ""
|
|
|
|
|
2008-07-17 14:47:51 +05:30
|
|
|
testing "printf aborts on bare %" \
|
2008-07-18 16:40:51 +05:30
|
|
|
"${bb}printf '%' a b c 2>&1; echo \$?" \
|
|
|
|
"printf: %: invalid format\n""1\n" \
|
|
|
|
"" ""
|
|
|
|
|
|
|
|
testing "printf aborts on %r" \
|
|
|
|
"${bb}printf '%r' a b c 2>&1; echo \$?" \
|
|
|
|
"printf: %r: invalid format\n""1\n" \
|
2008-07-17 14:47:51 +05:30
|
|
|
"" ""
|
|
|
|
|
2008-06-05 13:57:26 +05:30
|
|
|
exit $FAILCOUNT
|