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