summaryrefslogtreecommitdiff
path: root/tests/printf.tests
blob: d21eb4e7c120fe4e5f85b0f2bc4758b0b7979c0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
LC_ALL=C

# these should output error messages -- the format is required
printf
printf --

# these should output nothing
printf ""
printf -- ""

# this should expand escape sequences in the format string, nothing else
printf "\tone\n"

# this should not cut off output after the \c
printf "one\ctwo\n"

# and unrecognized backslash escapes should have the backslash preserverd
printf "4\.2\n"

printf "no newline " ; printf "now newline\n"

# %% -> %
printf "%%\n"

# simple character output
printf "%c\n" ABCD

# test simple string output
printf "%s\n" unquoted

# test quoted string output
printf "%s %q\n" unquoted quoted
printf "%s%10q\n" unquoted quoted

printf "%q\n" 'this&that'

# make sure the format string is reused to use up arguments
printf "%d " 1 2 3 4 5; printf "\n"

# make sure that extra format characters get null arguments
printf "%s %d %d %d\n" onestring

printf "%s %d %u %4.2f\n" onestring

printf -- "--%s %s--\n" 4.2 ''
printf -- "--%s %s--\n" 4.2

# test %b escapes

# 8 is a non-octal digit, so the `81' should be output
printf -- "--%b--\n" '\n\081'

printf -- "--%b--\n" '\t\0101'
printf -- "--%b--\n" '\t\101'

# these should all display `A7'
echo -e "\1017"
echo -e "\x0417"

printf "%b\n" '\01017'
printf "%b\n" '\1017'
printf "%b\n" '\x0417'

printf -- "--%b--\n" '\"abcd\"'
printf -- "--%b--\n" "\'abcd\'"

printf -- "--%b--\n" 'a\\x'

printf -- "--%b--\n" '\x'

Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')
Z2=$'\a\b\e\f\r\v'

if [ "$Z1" != "$Z2" ]; then
	echo "whoops: printf %b and $'' differ" >&2
fi
unset Z1 Z2

printf -- "--%b--\n" ''
printf -- "--%b--\n"

# the stuff following the \c should be ignored, as well as the rest
# of the format string
printf -- "--%b--\n" '4.2\c5.4\n'; printf "\n"

# make sure that a fieldwidth and precision of `*' are handled right
printf "%10.8s\n" 4.4BSD
printf "%*.*s\n" 10 8 4.4BSD

printf "%10.8q\n" 4.4BSD
printf "%*.*q\n" 10 8 4.4BSD

printf "%6b\n" 4.4BSD
printf "%*b\n" 6 4.4BSD

# we handle this crap with homemade code in printf.def
printf "%10b\n" 4.4BSD
printf -- "--%-10b--\n" 4.4BSD
printf "%4.2b\n" 4.4BSD
printf "%.3b\n" 4.4BSD
printf -- "--%-8b--\n" 4.4BSD

# test numeric conversions -- these four lines should echo identically
printf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255

printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255

printf "%10d\n" 42
printf "%10d\n" -42

printf "%*d\n" 10 42
printf "%*d\n" 10 -42

# test some simple floating point formats
printf "%4.2f\n" 4.2
printf "%#4.2f\n" 4.2
printf "%#4.1f\n" 4.2

printf "%*.*f\n" 4 2 4.2
printf "%#*.*f\n" 4 2 4.2
printf "%#*.*f\n" 4 1 4.2

printf "%E\n" 4.2
printf "%e\n" 4.2
printf "%6.1E\n" 4.2
printf "%6.1e\n" 4.2

printf "%G\n" 4.2
printf "%g\n" 4.2
printf "%6.2G\n" 4.2
printf "%6.2g\n" 4.2

# test some of the more esoteric features of POSIX.1 printf
printf "%d\n" "'string'"
printf "%d\n" '"string"'

printf "%#o\n" "'string'"
printf "%#o\n" '"string"'

printf "%#x\n" "'string'"
printf "%#X\n" '"string"'

printf "%6.2f\n" "'string'"
printf "%6.2f\n" '"string"'

# output from these two lines had better be the same
printf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
printf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz

# and these two also
printf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
printf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz

# error messages

# this should be an overflow, but error messages vary between systems
# printf "%lu\n" 4294967296

# ...but we cannot use this because some systems (SunOS4, for example),
# happily ignore overflow conditions in strtol(3)
#printf "%ld\n" 4294967296

# in the future this may mean to put the output into VAR, but for
# now it is an error
printf -v var "%10d" $RANDOM

printf "%10"
printf "ab%Mcd\n"

# this caused an infinite loop in older versions of printf
printf "%y" 0

printf "%d\n" GNU
printf "%o\n" GNU