Print this page
4854 printf(1) doesn't support %b and \c properly
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/test/util-tests/tests/printf/printf_test.ksh
+++ new/usr/src/test/util-tests/tests/printf/printf_test.ksh
1 1 #! /usr/bin/ksh
2 2 #
3 3 #
4 4 # This file and its contents are supplied under the terms of the
5 5 # Common Development and Distribution License ("CDDL"), version 1.0.
6 6 # You may only use this file in accordance with the terms of version
7 7 # 1.0 of the CDDL.
8 8 #
9 9 # A full copy of the text of the CDDL should have accompanied this
10 10 # source. A copy of the CDDL is also available via the Internet at
↓ open down ↓ |
10 lines elided |
↑ open up ↑ |
11 11 # http://www.illumos.org/license/CDDL.
12 12 #
13 13
14 14 #
15 15 # Copyright 2014 Garrett D'Amore <garrett@damore.org>
16 16 #
17 17
18 18 PRINTF=${PRINTF:=/usr/bin/printf}
19 19
20 20 test_start() {
21 - print "TEST STARTING ${1}: ${2}"
21 + print "TEST STARTING ${1}: ${2}"
22 22 }
23 23
24 24 test_pass() {
25 - print "TEST PASS: ${1}"
25 + print "TEST PASS: ${1}"
26 26 }
27 27
28 28 test_fail() {
29 - print "TEST FAIL: ${1}: ${2}"
30 -# exit -1
29 + print "TEST FAIL: ${1}: ${2}"
30 + exit -1
31 31 }
32 32
33 33 checkrv() {
34 34 if [[ $? -ne 0 ]]; then
35 35 test_fail $1 "exit failure"
36 36 fi
37 37 }
38 38
39 39 compare() {
40 40 if [[ "$2" != "$3" ]]; then
41 41 test_fail $1 "compare mismatch, got [$2] expected [$3]"
42 42 fi
43 43 }
44 44
45 45 typeset -A tests=()
46 46
47 47
48 48 typeset -A tests[01]=()
49 49 tests[01][desc]="hexadecimal lowercase"
50 50 tests[01][format]='%04x'
51 51 tests[01][args]="255"
52 52 tests[01][result]="00ff"
53 53
54 54 typeset -A tests[02]=()
55 55 tests[02][desc]="hexadecimal 32-bit"
56 56 tests[02][format]='%08x'
57 57 tests[02][args]='65537'
58 58 tests[02][result]=00010001
59 59
60 60 typeset -A tests[03]=()
61 61 tests[03][desc]="multiple arguments"
62 62 tests[03][format]='%d %s '
63 63 tests[03][args]="1 one 2 two 3 three"
64 64 tests[03][result]='1 one 2 two 3 three '
65 65
66 66 typeset -A tests[04]=()
67 67 tests[04][desc]="variable position parameters"
68 68 tests[04][format]='%2$s %1$d '
69 69 tests[04][args]="1 one 2 two 3 three"
70 70 tests[04][result]='one 1 two 2 three 3 '
71 71
72 72 typeset -A tests[05]=()
73 73 tests[05][desc]="width"
74 74 tests[05][format]='%10s'
75 75 tests[05][args]="abcdef"
76 76 tests[05][result]=' abcdef'
77 77
78 78 typeset -A tests[06]=()
79 79 tests[06][desc]="width and precision"
80 80 tests[06][format]='%10.3s'
81 81 tests[06][args]="abcdef"
82 82 tests[06][result]=' abc'
83 83
84 84 typeset -A tests[07]=()
85 85 tests[07][desc]="variable width and precision"
86 86 tests[07][format]='%*.*s'
87 87 tests[07][args]="10 3 abcdef"
88 88 tests[07][result]=' abc'
89 89
90 90 typeset -A tests[08]=()
91 91 tests[08][desc]="variable position width and precision"
92 92 tests[08][format]='%2$*1$.*3$s'
93 93 tests[08][args]="10 abcdef 3"
94 94 tests[08][result]=' abc'
95 95
96 96 typeset -A tests[09]=()
97 97 tests[09][desc]="multi variable position width and precision"
98 98 tests[09][format]='%2$*1$.*3$s'
99 99 tests[09][args]="10 abcdef 3 5 xyz 1"
100 100 tests[09][result]=' abc x'
101 101
102 102 typeset -A tests[10]=()
103 103 tests[10][desc]="decimal from hex"
104 104 tests[10][format]='%d '
105 105 tests[10][args]="0x1000 0XA"
106 106 tests[10][result]='4096 10 '
107 107
108 108 typeset -A tests[11]=()
109 109 tests[11][desc]="negative dec (64-bit)"
110 110 tests[11][format]='%x'
111 111 tests[11][args]="-1"
112 112 tests[11][result]='ffffffffffffffff'
113 113
114 114 typeset -A tests[12]=()
115 115 tests[12][desc]="float (basic)"
116 116 tests[12][format]='%f'
117 117 tests[12][args]="3.14"
118 118 tests[12][result]='3.140000'
119 119
120 120 typeset -A tests[12]=()
121 121 tests[12][desc]="float precision"
122 122 tests[12][format]='%.2f'
123 123 tests[12][args]="3.14159"
124 124 tests[12][result]='3.14'
125 125
126 126 typeset -A tests[13]=()
127 127 tests[13][desc]="left justify"
128 128 tests[13][format]='%-5d'
129 129 tests[13][args]="45"
130 130 tests[13][result]='45 '
131 131
132 132 typeset -A tests[14]=()
133 133 tests[14][desc]="newlines"
134 134 tests[14][format]='%s\n%s\n%s'
135 135 tests[14][args]="one two three"
↓ open down ↓ |
95 lines elided |
↑ open up ↑ |
136 136 tests[14][result]='one
137 137 two
138 138 three'
139 139
140 140 typeset -A tests[15]=()
141 141 tests[15][desc]="embedded octal escape"
142 142 tests[15][format]='%s\41%s'
143 143 tests[15][args]="one two"
144 144 tests[15][result]='one!two'
145 145
146 -# this is not yet supported
147 -#typeset -A tests[16]=()
148 -#tests[16][desc]="backslash string (%b)"
149 -#tests[16][format]='%b'
150 -#tests[16][args]='\0101\0102\0103'
151 -#tests[16][result]='ABC'
146 +typeset -A tests[16]=()
147 +tests[16][desc]="backslash string (%b)"
148 +tests[16][format]='%b'
149 +tests[16][args]='\0101\0102\0103'
150 +tests[16][result]='ABC'
152 151
153 -# nor is this
154 -#typeset -A tests[17]=()
155 -#tests[17][desc]="backslash c in %b"
156 -#tests[17][format]='%b%s'
157 -#tests[17][args]='\0101\cone two'
158 -#tests[17][result]='A'
152 +typeset -A tests[17]=()
153 +tests[17][desc]="backslash c in %b"
154 +tests[17][format]='%b%s'
155 +tests[17][args]='\0101\cone two'
156 +tests[17][result]='A'
159 157
158 +typeset -A tests[18]=()
159 +tests[18][desc]="backslash octal in format"
160 +tests[18][format]='HI\1120K\0112tabbed\11again'
161 +tests[18][args]=
162 +tests[18][result]='HIJ0K 2tabbed again'
163 +
164 +typeset -A tests[19]=()
165 +tests[19][desc]="backslash octal in %b"
166 +tests[19][format]="%b"
167 +tests[19][args]='HI\0112K\011tabbed'
168 +tests[19][result]='HIJK tabbed'
169 +
170 +typeset -A tests[20]=()
171 +tests[20][desc]="numeric %d and ASCII conversions"
172 +tests[20][format]='%d '
173 +tests[20][args]="3 +3 -3 \"3 \"+ '-"
174 +tests[20][result]='3 3 -3 51 43 45 '
175 +
160 176 #debug=yes
161 177
162 178 for i in "${!tests[@]}"; do
163 179 t=test_$i
164 180 desc=${tests[$i][desc]}
165 181 format=${tests[$i][format]}
166 182 args="${tests[$i][args]}"
167 183 result=${tests[$i][result]}
168 184
169 185 test_start $t "${tests[$i][desc]}"
170 186 [[ -n "$debug" ]] && echo $PRINTF "$format" "${args[@]}"
171 187 comp=$($PRINTF "$format" ${args[@]})
172 188 checkrv $t
173 189 [[ -n "$debug" ]] && echo "got [$comp]"
174 190 good=$result
175 191 compare $t "$comp" "$good"
176 192 test_pass $t
177 193 done
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX