]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/printf.tests
Imported from ../bash-3.1.tar.gz.
[thirdparty/bash.git] / tests / printf.tests
CommitLineData
cce855bc 1LC_ALL=C
bb70624e 2LC_NUMERIC=C
cce855bc
JA
3
4# these should output error messages -- the format is required
5printf
6printf --
7
8# these should output nothing
9printf ""
10printf -- ""
11
95732b49
JA
12# in the future this may mean to put the output into VAR, but for
13# now it is an error
14# 2005-03-15 no longer an error
15unset var
16printf -v var "%10d" $RANDOM
17echo ${#var}
18
cce855bc
JA
19# this should expand escape sequences in the format string, nothing else
20printf "\tone\n"
21
22# this should not cut off output after the \c
23printf "one\ctwo\n"
24
25# and unrecognized backslash escapes should have the backslash preserverd
26printf "4\.2\n"
27
28printf "no newline " ; printf "now newline\n"
29
30# %% -> %
31printf "%%\n"
32
bb70624e
JA
33# this was a bug caused by pre-processing the string for backslash escapes
34# before doing the `%' format processing -- all versions before bash-2.04
35printf "\045" ; echo
36printf "\045d\n"
37
cce855bc
JA
38# simple character output
39printf "%c\n" ABCD
40
41# test simple string output
42printf "%s\n" unquoted
43
44# test quoted string output
45printf "%s %q\n" unquoted quoted
46printf "%s%10q\n" unquoted quoted
47
48printf "%q\n" 'this&that'
49
50# make sure the format string is reused to use up arguments
51printf "%d " 1 2 3 4 5; printf "\n"
52
53# make sure that extra format characters get null arguments
54printf "%s %d %d %d\n" onestring
55
56printf "%s %d %u %4.2f\n" onestring
57
58printf -- "--%s %s--\n" 4.2 ''
59printf -- "--%s %s--\n" 4.2
60
61# test %b escapes
62
63# 8 is a non-octal digit, so the `81' should be output
64printf -- "--%b--\n" '\n\081'
65
66printf -- "--%b--\n" '\t\0101'
67printf -- "--%b--\n" '\t\101'
68
69# these should all display `A7'
70echo -e "\1017"
f73dda09 71echo -e "\x417"
cce855bc
JA
72
73printf "%b\n" '\01017'
74printf "%b\n" '\1017'
f73dda09 75printf "%b\n" '\x417'
cce855bc
JA
76
77printf -- "--%b--\n" '\"abcd\"'
78printf -- "--%b--\n" "\'abcd\'"
79
80printf -- "--%b--\n" 'a\\x'
81
82printf -- "--%b--\n" '\x'
83
84Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')
85Z2=$'\a\b\e\f\r\v'
86
87if [ "$Z1" != "$Z2" ]; then
88 echo "whoops: printf %b and $'' differ" >&2
89fi
90unset Z1 Z2
91
92printf -- "--%b--\n" ''
93printf -- "--%b--\n"
94
95# the stuff following the \c should be ignored, as well as the rest
96# of the format string
97printf -- "--%b--\n" '4.2\c5.4\n'; printf "\n"
98
bb70624e
JA
99# unrecognized escape sequences should by displayed unchanged
100printf -- "--%b--\n" '4\.2'
101
102# a bare \ should not be processed as an escape sequence
103printf -- "--%b--\n" '\'
104
b72432fd
JA
105# make sure extra arguments are ignored if the format string doesn't
106# actually use them
107printf "\n" 4.4 BSD
108printf " " 4.4 BSD ; printf "\n"
109
cce855bc
JA
110# make sure that a fieldwidth and precision of `*' are handled right
111printf "%10.8s\n" 4.4BSD
112printf "%*.*s\n" 10 8 4.4BSD
113
114printf "%10.8q\n" 4.4BSD
115printf "%*.*q\n" 10 8 4.4BSD
116
117printf "%6b\n" 4.4BSD
118printf "%*b\n" 6 4.4BSD
119
120# we handle this crap with homemade code in printf.def
121printf "%10b\n" 4.4BSD
122printf -- "--%-10b--\n" 4.4BSD
123printf "%4.2b\n" 4.4BSD
124printf "%.3b\n" 4.4BSD
125printf -- "--%-8b--\n" 4.4BSD
126
127# test numeric conversions -- these four lines should echo identically
128printf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
129printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255
130
131printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
132printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255
133
134printf "%10d\n" 42
135printf "%10d\n" -42
136
137printf "%*d\n" 10 42
138printf "%*d\n" 10 -42
139
140# test some simple floating point formats
141printf "%4.2f\n" 4.2
142printf "%#4.2f\n" 4.2
143printf "%#4.1f\n" 4.2
144
145printf "%*.*f\n" 4 2 4.2
146printf "%#*.*f\n" 4 2 4.2
147printf "%#*.*f\n" 4 1 4.2
148
149printf "%E\n" 4.2
150printf "%e\n" 4.2
151printf "%6.1E\n" 4.2
152printf "%6.1e\n" 4.2
153
154printf "%G\n" 4.2
155printf "%g\n" 4.2
156printf "%6.2G\n" 4.2
157printf "%6.2g\n" 4.2
158
159# test some of the more esoteric features of POSIX.1 printf
160printf "%d\n" "'string'"
161printf "%d\n" '"string"'
162
163printf "%#o\n" "'string'"
164printf "%#o\n" '"string"'
165
166printf "%#x\n" "'string'"
167printf "%#X\n" '"string"'
168
169printf "%6.2f\n" "'string'"
170printf "%6.2f\n" '"string"'
171
172# output from these two lines had better be the same
173printf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
174printf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz
175
176# and these two also
177printf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
178printf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz
179
bb70624e
JA
180# tests for translating \' to ' and \\ to \
181# printf translates \' to ' in the format string...
182printf "\'abcd\'\n"
183
184# but not when the %b format specification is used
185printf "%b\n" \\\'abcd\\\'
186
187# but both translate \\ to \
188printf '\\abcd\\\n'
189printf "%b\n" '\\abcd\\'
190
191# this was reported as a bug in bash-2.03
192# these three lines should all echo `26'
193printf "%d\n" 0x1a
194printf "%d\n" 032
195printf "%d\n" 26
196
cce855bc
JA
197# error messages
198
199# this should be an overflow, but error messages vary between systems
200# printf "%lu\n" 4294967296
201
202# ...but we cannot use this because some systems (SunOS4, for example),
203# happily ignore overflow conditions in strtol(3)
204#printf "%ld\n" 4294967296
205
cce855bc
JA
206printf "%10"
207printf "ab%Mcd\n"
208
209# this caused an infinite loop in older versions of printf
210printf "%y" 0
211
f73dda09 212# these should print a warning and `0', according to POSIX.2
cce855bc
JA
213printf "%d\n" GNU
214printf "%o\n" GNU
f73dda09
JA
215
216# failures in all bash versions through bash-2.05
217printf "%.0s" foo
218printf "%.*s" 0 foo
219
220printf '%.0b-%.0s\n' foo bar
221printf '(%*b)(%*s)\n' -4 foo -4 bar
222
223format='%'`printf '%0100384d' 0`'d\n'
224printf $format 0
225
95732b49
JA
226# failures in all bash versions through bash-3.0 - undercounted characters
227unset vv
228printf " %s %s %s \n%n" ab cd ef vv
229echo "$vv"
230
f73dda09
JA
231# this doesn't work with printf(3) on all systems
232#printf "%'s\n" foo
b80f6443
JA
233
234# test cases from an austin-group list discussion
235# prints ^G as an extension
236printf '%b\n' '\7'
237
238# prints ^G
239printf '%b\n' '\0007'
240
241# prints NUL then 7
242printf '\0007\n'
243
244# prints no more than two hex digits
245printf '\x07e\n'
246
247# additional backslash escapes
248printf '\"\?\n'