]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/printf.tests
d21eb4e7c120fe4e5f85b0f2bc4758b0b7979c0d
[thirdparty/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 that a fieldwidth and precision of `*' are handled right
87 printf "%10.8s\n" 4.4BSD
88 printf "%*.*s\n" 10 8 4.4BSD
89
90 printf "%10.8q\n" 4.4BSD
91 printf "%*.*q\n" 10 8 4.4BSD
92
93 printf "%6b\n" 4.4BSD
94 printf "%*b\n" 6 4.4BSD
95
96 # we handle this crap with homemade code in printf.def
97 printf "%10b\n" 4.4BSD
98 printf -- "--%-10b--\n" 4.4BSD
99 printf "%4.2b\n" 4.4BSD
100 printf "%.3b\n" 4.4BSD
101 printf -- "--%-8b--\n" 4.4BSD
102
103 # test numeric conversions -- these four lines should echo identically
104 printf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
105 printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255
106
107 printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
108 printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255
109
110 printf "%10d\n" 42
111 printf "%10d\n" -42
112
113 printf "%*d\n" 10 42
114 printf "%*d\n" 10 -42
115
116 # test some simple floating point formats
117 printf "%4.2f\n" 4.2
118 printf "%#4.2f\n" 4.2
119 printf "%#4.1f\n" 4.2
120
121 printf "%*.*f\n" 4 2 4.2
122 printf "%#*.*f\n" 4 2 4.2
123 printf "%#*.*f\n" 4 1 4.2
124
125 printf "%E\n" 4.2
126 printf "%e\n" 4.2
127 printf "%6.1E\n" 4.2
128 printf "%6.1e\n" 4.2
129
130 printf "%G\n" 4.2
131 printf "%g\n" 4.2
132 printf "%6.2G\n" 4.2
133 printf "%6.2g\n" 4.2
134
135 # test some of the more esoteric features of POSIX.1 printf
136 printf "%d\n" "'string'"
137 printf "%d\n" '"string"'
138
139 printf "%#o\n" "'string'"
140 printf "%#o\n" '"string"'
141
142 printf "%#x\n" "'string'"
143 printf "%#X\n" '"string"'
144
145 printf "%6.2f\n" "'string'"
146 printf "%6.2f\n" '"string"'
147
148 # output from these two lines had better be the same
149 printf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
150 printf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz
151
152 # and these two also
153 printf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
154 printf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz
155
156 # error messages
157
158 # this should be an overflow, but error messages vary between systems
159 # printf "%lu\n" 4294967296
160
161 # ...but we cannot use this because some systems (SunOS4, for example),
162 # happily ignore overflow conditions in strtol(3)
163 #printf "%ld\n" 4294967296
164
165 # in the future this may mean to put the output into VAR, but for
166 # now it is an error
167 printf -v var "%10d" $RANDOM
168
169 printf "%10"
170 printf "ab%Mcd\n"
171
172 # this caused an infinite loop in older versions of printf
173 printf "%y" 0
174
175 printf "%d\n" GNU
176 printf "%o\n" GNU