1#!/bin/sh
2# Copyright 2008 by Denys Vlasenko
3# Licensed under GPL v2, see file LICENSE for details.
4
5. ./testing.sh
6
7# Need this in order to not execute shell builtin
8bb="busybox "
9
10# testing "test name" "command" "expected result" "file input" "stdin"
11
12testing "printf produces no further output 1" \
13	"${bb}printf '\c' foo" \
14	"" \
15	"" ""
16
17testing "printf produces no further output 2" \
18	"${bb}printf '%s\c' foo bar" \
19	"foo" \
20	"" ""
21
22testing "printf repeatedly uses pattern for each argv" \
23	"${bb}printf '%s\n' foo \$HOME" \
24	"foo\n$HOME\n" \
25	"" ""
26
27testing "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
32testing "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
37testing "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
42testing "printf understands %23.12f" \
43	"${bb}printf '|%23.12f|\n' 5.25 2>&1; echo \$?" \
44	"|         5.250000000000|\n""0\n" \
45	"" ""
46
47testing "printf understands %*.*f" \
48	"${bb}printf '|%*.*f|\n' 23 12 5.25 2>&1; echo \$?" \
49	"|         5.250000000000|\n""0\n" \
50	"" ""
51
52testing "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
57testing "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
62testing "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
67testing "printf understands %zd" \
68	"${bb}printf '%zd\n' -5 2>&1; echo \$?" \
69	"-5\n""0\n" \
70	"" ""
71
72testing "printf understands %ld" \
73	"${bb}printf '%ld\n' -5 2>&1; echo \$?" \
74	"-5\n""0\n" \
75	"" ""
76
77testing "printf understands %Ld" \
78	"${bb}printf '%Ld\n' -5 2>&1; echo \$?" \
79	"-5\n""0\n" \
80	"" ""
81
82# "FIXED" now to act compatibly
83## We are "more correct" here than bash/coreutils: they happily print -2
84## as if it is a huge unsigned number
85#testing "printf handles %u -N" \
86#	"${bb}printf '%u\n' 1 -2 3 2>&1; echo \$?" \
87#	"1\n""printf: -2: invalid number\n""0\n""3\n""0\n" \
88#	"" ""
89
90testing "printf handles %d bad_input" \
91	"${bb}printf '%d\n' 1 - 2 bad 3 123bad 4 2>&1; echo \$?" \
92"1\n""printf: -: invalid number\n""0\n"\
93"2\n""printf: bad: invalid number\n""0\n"\
94"3\n""printf: 123bad: invalid number\n""0\n"\
95"4\n""1\n" \
96	"" ""
97
98testing "printf aborts on bare %" \
99	"${bb}printf '%' a b c 2>&1; echo \$?" \
100	"printf: %: invalid format\n""1\n" \
101	"" ""
102
103testing "printf aborts on %r" \
104	"${bb}printf '%r' a b c 2>&1; echo \$?" \
105	"printf: %r: invalid format\n""1\n" \
106	"" ""
107
108exit $FAILCOUNT
109