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