blob: 728bbf4bf5c95c95643e3bb71ed749dd2620acf8 [file] [log] [blame]
Denis Vlasenko50120da2008-06-05 08:27:26 +00001#!/bin/sh
Denis Vlasenkoa48656b2008-07-18 11:10:51 +00002# Copyright 2008 by Denys Vlasenko
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003# Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko50120da2008-06-05 08:27:26 +00004
Mike Frysingercaa79402009-11-04 18:41:22 -05005. ./testing.sh
Denis Vlasenko50120da2008-06-05 08:27:26 +00006
7# Need this in order to not execute shell builtin
8bb="busybox "
9
10# testing "test name" "command" "expected result" "file input" "stdin"
11
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000012testing "printf produces no further output 1" \
Denis Vlasenko50120da2008-06-05 08:27:26 +000013 "${bb}printf '\c' foo" \
14 "" \
15 "" ""
16
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000017testing "printf produces no further output 2" \
18 "${bb}printf '%s\c' foo bar" \
Denis Vlasenko50120da2008-06-05 08:27:26 +000019 "foo" \
20 "" ""
21
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000022testing "printf repeatedly uses pattern for each argv" \
Ron Yorston1f27fa92018-02-09 09:52:52 +000023 "${bb}printf '%s\n' foo '$HOME'" \
Denis Vlasenko50120da2008-06-05 08:27:26 +000024 "foo\n$HOME\n" \
25 "" ""
26
Denis Vlasenkoa48656b2008-07-18 11:10:51 +000027testing "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
Denis Vlasenko5f116622008-07-18 18:41:55 +000077testing "printf understands %Ld" \
78 "${bb}printf '%Ld\n' -5 2>&1; echo \$?" \
79 "-5\n""0\n" \
80 "" ""
81
Daniel Thau7d06d6e2021-09-02 07:41:08 -040082testing "printf understands %%" \
83 "${bb}printf '%%\n' 2>&1; echo \$?" \
84 "%\n""0\n" \
85 "" ""
86
Bernhard Reutner-Fischer3db4e7f2018-10-19 15:25:41 +020087testing "printf handles positive numbers for %d" \
88 "${bb}printf '%d\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \
89 "3\n"\
90"3\n"\
91"3\n"\
92"3\n""0\n" \
93 "" ""
94
95testing "printf handles positive numbers for %i" \
96 "${bb}printf '%i\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \
97 "3\n"\
98"3\n"\
99"3\n"\
100"3\n""0\n" \
101 "" ""
102
103testing "printf handles positive numbers for %x" \
104 "${bb}printf '%x\n' 42 +42 ' 42' ' +42' 2>&1; echo \$?" \
105 "2a\n"\
106"2a\n"\
107"2a\n"\
108"2a\n""0\n" \
109 "" ""
110
111testing "printf handles positive numbers for %f" \
112 "${bb}printf '%0.3f\n' .42 +.42 ' .42' ' +.42' 2>&1; echo \$?" \
113 "0.420\n"\
114"0.420\n"\
115"0.420\n"\
116"0.420\n""0\n" \
117 "" ""
118
119
Denys Vlasenko24410602009-06-13 23:41:57 +0200120# "FIXED" now to act compatibly
121## We are "more correct" here than bash/coreutils: they happily print -2
122## as if it is a huge unsigned number
123#testing "printf handles %u -N" \
124# "${bb}printf '%u\n' 1 -2 3 2>&1; echo \$?" \
125# "1\n""printf: -2: invalid number\n""0\n""3\n""0\n" \
126# "" ""
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000127
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000128testing "printf handles %d bad_input" \
129 "${bb}printf '%d\n' 1 - 2 bad 3 123bad 4 2>&1; echo \$?" \
Dan Fandricheb2bf5b2010-09-02 18:38:00 -0700130"1\n""printf: invalid number '-'\n""0\n"\
131"2\n""printf: invalid number 'bad'\n""0\n"\
132"3\n""printf: invalid number '123bad'\n""0\n"\
Denys Vlasenkobf4aeed2009-06-18 22:22:04 +0200133"4\n""1\n" \
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000134 "" ""
135
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000136testing "printf aborts on bare %" \
Denis Vlasenkoa48656b2008-07-18 11:10:51 +0000137 "${bb}printf '%' a b c 2>&1; echo \$?" \
138 "printf: %: invalid format\n""1\n" \
139 "" ""
140
141testing "printf aborts on %r" \
142 "${bb}printf '%r' a b c 2>&1; echo \$?" \
143 "printf: %r: invalid format\n""1\n" \
Denis Vlasenko0f683f82008-07-17 09:17:51 +0000144 "" ""
145
Ron Yorston7105e4a2021-12-16 11:19:03 +0000146testing "printf treats leading 0 as flag" \
147 "${bb}printf '%0*d\n' 2 1 2>&1; echo \$?" \
148 "01\n""0\n" \
149 "" ""
150
151testing "printf handles multiple flags" \
152 "${bb}printf '%0 d\n' 2 2>&1; echo \$?" \
153 " 2\n""0\n" \
154 "" ""
155
Denis Vlasenko50120da2008-06-05 08:27:26 +0000156exit $FAILCOUNT