1# Tests for the echo, print, printf and pushln builtins
2
3# Tested elsewhere:
4#  Use of print -p to output to coprocess	A01grammar
5#  Prompt expansion with print -P		D01prompt
6#  -l, -r, -R and -n indirectly tested in various places
7
8# Not yet tested:
9#  echo and pushln
10#  print's -b -c -s -z -N options
11
12
13%test
14
15 print -D "${HOME:-~}"
160:replace directory name
17>~
18
19 print -u2 'error message'
200:output to file-descriptor
21?error message
22
23 print -o foo bar Baz
240:argument sorting
25>Baz bar foo
26
27 print -f
281:print -f needs a format specified
29?(eval):print:1: argument expected: -f
30
31 print -Of '%s\n' foo bar baz
320:reverse argument sorting
33>foo
34>baz
35>bar
36
37# some locales force case-insensitive sorting
38 (LC_ALL=C; print -o a B c)
390:case-sensitive argument sorting
40>B a c
41
42 (LC_ALL=C; print -io a B c)
430:case-insensitive argument sorting
44>a B c
45
46 print -m '[0-9]' one 2 three 4 five 6
470:removal of non-matching arguments
48>2 4 6
49
50 printf '%s\n' string
510:test s format specifier
52>string
53
54 printf '%b' '\t\\\n'
550:test b format specifier
56>	\
57
58 printf '%q\n' '=a=b \ c!'
590: test q format specifier
60>\=a=b\ \\\ c!
61
62 printf '%c\n' char
630:test c format specifier
64>c
65
66 printf '%.10e%n\n' 1 count >/dev/null
67 printf '%d\n' $count
680:test n format specifier
69>16
70
71 printf '%5b%n\n' abc count >/dev/null; echo $count
720:check count of width-specified %b
73>5
74
75 printf '%s!%5b!\n' abc
760:ensure width is applied to empty param
77>abc!     !
78
79 printf '%d %d\n' 123.45 678 90.1
800:test d format specifier
81>123 678
82>90 0
83
84 printf '%g %g\n' 123.45 678 90.1
850:test g format specifier
86>123.45 678
87>90.1 0
88
89 print -f 'arg: %b\n' -C2 '\x41' '\x42' '\x43'
900:override -C when -f was given
91>arg: A
92>arg: B
93>arg: C
94
95# Is anyone not using ASCII
96 printf '%d\n' \'A
970:initial quote to get numeric value of character with int
98>65
99
100 printf '%.1E\n' \'B
1010:initial quote to get numeric value of character with double
102>6.6E+01
103
104 printf '%x\n' $(printf '"\xf0')
1050:numeric value of high numbered character
106>f0
107
108 printf '\x25s\n' arg
1090:using \x25 to print a literal % in format
110>%s
111
112 printf '%3c\n' c
1130:width specified in format specifier
114>  c
115
116 printf '%.4s\n' chopped
1170:precision specified in format specifier
118>chop
119
120 printf '%*.*f\n' 6 2 10.2
1210:width/precision specified in arguments
122> 10.20
123
124 printf '%z'
1251:use of invalid directive
126?(eval):printf:1: %z: invalid directive
127
128 printf '%d\n' 3a
1291:bad arithmetic expression
130?(eval):1: bad math expression: operator expected at `a'
131>0
132
133 printf '%12$s' 1 2 3
1341:out of range argument specifier
135?(eval):printf:1: 12: argument specifier out of range
136
137 printf '%2$s\n' 1 2 3
1381:out of range argument specifier on format reuse
139?(eval):printf:1: 2: argument specifier out of range
140>2
141
142 printf '%*0$d'
1431:out of range argument specifier on width
144?(eval):printf:1: 0: argument specifier out of range
145
146 print -m -f 'format - %s.\n' 'z' a b c
1470:format not printed if no arguments left after -m removal
148
149 print -f 'format - %s%b.\n'
1500:format printed despite lack of arguments
151>format - .
152
153 printf 'x%4sx\n'
1540:with no arguments empty string where string needed
155>x    x
156
157 printf '%d\n'
1580:with no arguments, zero used where number needed
159>0
160
161 printf '%s\t%c:%#x%%\n' one a 1 two b 2 three c 3
1620:multiple arguments with format reused
163>one	a:0x1%
164>two	b:0x2%
165>three	c:0x3%
166
167 printf '%d%n' 123 val val val > /dev/null
168 printf '%d\n' val
1690:%n count zeroed on format reuse
170>1
171
172# this may fill spec string with '%0+- #*.*lld\0' - 13 characters
173 printf '%1$0+- #-08.5dx\n' 123
1740:maximal length format specification
175>+00123  x
176
177 printf '%*smorning\n' -5 good
1780:negative width specified
179>good morning
180
181 printf '%.*g\n' -1 .1
1820:negative precision specified
183>0.1
184
185 printf '%2$s %1$d\n' 1 2
1860:specify argument to output explicitly
187>2 1
188
189 printf '%3$.*1$d\n' 4 0 3
1900:specify output and precision arguments explicitly
191>0003
192
193 printf '%2$d%1$d\n' 1 2 3 4
1940:reuse format where arguments are explicitly specified
195>21
196>43
197
198 printf '%1$*2$d' 1 2 3 4 5 6 7 8 9 10; echo .EoL.
1990:reuse of specified arguments 
200> 1   3     5       7         9.EoL.
201
202 echo -n 'Now is the time'; echo .EoL.
2030:testing -n with echo
204>Now is the time.EoL.
205
206 printf '%1$0+.3d\n' 3
2070:flags mixed with specified argument
208>+003
209
210# Test the parsing of the \c escape.
211
212 echo '1 2!\c3 4' a b c d; echo .EoL.
2130:Truncating first echo arg using backslash-c
214>1 2!.EoL.
215
216 echo a b '1 2?\c5 6' c d; echo .EoL.
2170:Truncating third echo arg using backslash-c
218>a b 1 2?.EoL.
219
220 printf '1 2!\c3 4'; echo .EoL.
2210:Truncating printf literal using backslash-c
222>1 2!.EoL.
223
224 printf '%s %b!\c%s %s' 1 2 3 4 5 6 7 8 9; echo .EoL.
2250:Truncating printf format using backslash-c
226>1 2!.EoL.
227
228 printf '%s %b!\c%s %s' '1\c' '2\n\c' 3 4 5 6 7 8 9
2290:Truncating printf early %b arg using backslash-c
230>1\c 2
231
232 printf '%b %b\n' 1 2 3 4 '5\c' 6 7 8 9; echo .EoL.
2330:Truncating printf late %b arg using backslash-c
234>1 2
235>3 4
236>5.EoL.
237
238# The following usage, as stated in the manual, is not recommended and the
239# results are undefined. Tests are here anyway to ensure some form of
240# half-sane behaviour.
241
242 printf '%2$s %s %3$s\n' Morning Good World
2430:mixed style of argument selection
244>Good Morning World
245
246 printf '%*1$.*d\n' 1 2
2470:argument specified for width only
248>00
249
250 print -f '%*.*1$d\n' 1 2 3
2510:argument specified for precision only
252>2
253>000
254
255 printf -- '%s\n' str
2560:initial `--' ignored to satisfy POSIX
257>str
258
259 printf '%'
2601:nothing after % in format specifier
261?(eval):printf:1: %: invalid directive
262
263 printf $'%\0'
2641:explicit null after % in format specifier
265?(eval):printf:1: %: invalid directive
266
267 printf '%b\n' '\0123'
2680:printf handles \0... octal escapes in replacement text
269>S
270
271 print -lO $'a' $'a\0' $'a\0b' $'a\0b\0' $'a\0b\0a' $'a\0b\0b' $'a\0c' |
272 while read -r line; do
273    for (( i = 1; i <= ${#line}; i++ )); do
274      foo=$line[i]
275      printf "%02x" $(( #foo ))
276    done
277    print
278 done
2790:sorting with embedded nulls
280>610063
281>6100620062
282>6100620061
283>61006200
284>610062
285>6100
286>61
287