Imported from ../bash-2.03.tar.gz.
[platform/upstream/bash.git] / tests / printf.tests
1 LC_ALL=C
2
3 # these should output error messages -- the format is required
4 printf
5 printf --
6
7 # these should output nothing
8 printf ""
9 printf -- ""
10
11 # this should expand escape sequences in the format string, nothing else
12 printf "\tone\n"
13
14 # this should not cut off output after the \c
15 printf "one\ctwo\n"
16
17 # and unrecognized backslash escapes should have the backslash preserverd
18 printf "4\.2\n"
19
20 printf "no newline " ; printf "now newline\n"
21
22 # %% -> %
23 printf "%%\n"
24
25 # simple character output
26 printf "%c\n" ABCD
27
28 # test simple string output
29 printf "%s\n" unquoted
30
31 # test quoted string output
32 printf "%s %q\n" unquoted quoted
33 printf "%s%10q\n" unquoted quoted
34
35 printf "%q\n" 'this&that'
36
37 # make sure the format string is reused to use up arguments
38 printf "%d " 1 2 3 4 5; printf "\n"
39
40 # make sure that extra format characters get null arguments
41 printf "%s %d %d %d\n" onestring
42
43 printf "%s %d %u %4.2f\n" onestring
44
45 printf -- "--%s %s--\n" 4.2 ''
46 printf -- "--%s %s--\n" 4.2
47
48 # test %b escapes
49
50 # 8 is a non-octal digit, so the `81' should be output
51 printf -- "--%b--\n" '\n\081'
52
53 printf -- "--%b--\n" '\t\0101'
54 printf -- "--%b--\n" '\t\101'
55
56 # these should all display `A7'
57 echo -e "\1017"
58 echo -e "\x0417"
59
60 printf "%b\n" '\01017'
61 printf "%b\n" '\1017'
62 printf "%b\n" '\x0417'
63
64 printf -- "--%b--\n" '\"abcd\"'
65 printf -- "--%b--\n" "\'abcd\'"
66
67 printf -- "--%b--\n" 'a\\x'
68
69 printf -- "--%b--\n" '\x'
70
71 Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')
72 Z2=$'\a\b\e\f\r\v'
73
74 if [ "$Z1" != "$Z2" ]; then
75         echo "whoops: printf %b and $'' differ" >&2
76 fi
77 unset Z1 Z2
78
79 printf -- "--%b--\n" ''
80 printf -- "--%b--\n"
81
82 # the stuff following the \c should be ignored, as well as the rest
83 # of the format string
84 printf -- "--%b--\n" '4.2\c5.4\n'; printf "\n"
85
86 # make sure extra arguments are ignored if the format string doesn't
87 # actually use them
88 printf "\n" 4.4 BSD
89 printf " " 4.4 BSD ; printf "\n"
90
91 # make sure that a fieldwidth and precision of `*' are handled right
92 printf "%10.8s\n" 4.4BSD
93 printf "%*.*s\n" 10 8 4.4BSD
94
95 printf "%10.8q\n" 4.4BSD
96 printf "%*.*q\n" 10 8 4.4BSD
97
98 printf "%6b\n" 4.4BSD
99 printf "%*b\n" 6 4.4BSD
100
101 # we handle this crap with homemade code in printf.def
102 printf "%10b\n" 4.4BSD
103 printf -- "--%-10b--\n" 4.4BSD
104 printf "%4.2b\n" 4.4BSD
105 printf "%.3b\n" 4.4BSD
106 printf -- "--%-8b--\n" 4.4BSD
107
108 # test numeric conversions -- these four lines should echo identically
109 printf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
110 printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255
111
112 printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
113 printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255
114
115 printf "%10d\n" 42
116 printf "%10d\n" -42
117
118 printf "%*d\n" 10 42
119 printf "%*d\n" 10 -42
120
121 # test some simple floating point formats
122 printf "%4.2f\n" 4.2
123 printf "%#4.2f\n" 4.2
124 printf "%#4.1f\n" 4.2
125
126 printf "%*.*f\n" 4 2 4.2
127 printf "%#*.*f\n" 4 2 4.2
128 printf "%#*.*f\n" 4 1 4.2
129
130 printf "%E\n" 4.2
131 printf "%e\n" 4.2
132 printf "%6.1E\n" 4.2
133 printf "%6.1e\n" 4.2
134
135 printf "%G\n" 4.2
136 printf "%g\n" 4.2
137 printf "%6.2G\n" 4.2
138 printf "%6.2g\n" 4.2
139
140 # test some of the more esoteric features of POSIX.1 printf
141 printf "%d\n" "'string'"
142 printf "%d\n" '"string"'
143
144 printf "%#o\n" "'string'"
145 printf "%#o\n" '"string"'
146
147 printf "%#x\n" "'string'"
148 printf "%#X\n" '"string"'
149
150 printf "%6.2f\n" "'string'"
151 printf "%6.2f\n" '"string"'
152
153 # output from these two lines had better be the same
154 printf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
155 printf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz
156
157 # and these two also
158 printf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
159 printf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz
160
161 # error messages
162
163 # this should be an overflow, but error messages vary between systems
164 # printf "%lu\n" 4294967296
165
166 # ...but we cannot use this because some systems (SunOS4, for example),
167 # happily ignore overflow conditions in strtol(3)
168 #printf "%ld\n" 4294967296
169
170 # in the future this may mean to put the output into VAR, but for
171 # now it is an error
172 printf -v var "%10d" $RANDOM
173
174 printf "%10"
175 printf "ab%Mcd\n"
176
177 # this caused an infinite loop in older versions of printf
178 printf "%y" 0
179
180 printf "%d\n" GNU
181 printf "%o\n" GNU