Print this page
4854 printf(1) doesn't support %b and 


  10 # source.  A copy of the CDDL is also available via the Internet at
  11 # http://www.illumos.org/license/CDDL.
  12 #
  13 
  14 #
  15 # Copyright 2014 Garrett D'Amore <garrett@damore.org>
  16 #
  17 
  18 PRINTF=${PRINTF:=/usr/bin/printf}
  19 
  20 test_start() {
  21         print "TEST STARTING ${1}: ${2}"
  22 }
  23 
  24 test_pass() {
  25         print "TEST PASS: ${1}"
  26 }
  27 
  28 test_fail() {
  29         print "TEST FAIL: ${1}: ${2}"
  30 #        exit -1
  31 }
  32 
  33 checkrv() {
  34         if [[ $? -ne 0 ]]; then
  35                 test_fail $1 "exit failure"
  36         fi
  37 }
  38 
  39 compare() {
  40         if [[ "$2" != "$3" ]]; then
  41                 test_fail $1 "compare mismatch, got [$2] expected [$3]"
  42         fi
  43 }
  44 
  45 typeset -A tests=()
  46 
  47 
  48 typeset -A tests[01]=()
  49 tests[01][desc]="hexadecimal lowercase"
  50 tests[01][format]='%04x'


 126 typeset -A tests[13]=()
 127 tests[13][desc]="left justify"
 128 tests[13][format]='%-5d'
 129 tests[13][args]="45"
 130 tests[13][result]='45   '
 131 
 132 typeset -A tests[14]=()
 133 tests[14][desc]="newlines"
 134 tests[14][format]='%s\n%s\n%s'
 135 tests[14][args]="one two three"
 136 tests[14][result]='one
 137 two
 138 three'
 139 
 140 typeset -A tests[15]=()
 141 tests[15][desc]="embedded octal escape"
 142 tests[15][format]='%s\41%s'
 143 tests[15][args]="one two"
 144 tests[15][result]='one!two'
 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'
 152 
 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'
 159 


















 160 #debug=yes
 161 
 162 for i in "${!tests[@]}"; do
 163         t=test_$i
 164         desc=${tests[$i][desc]}
 165         format=${tests[$i][format]}
 166         args="${tests[$i][args]}"
 167         result=${tests[$i][result]}
 168         
 169         test_start $t "${tests[$i][desc]}"
 170         [[ -n "$debug" ]] && echo $PRINTF "$format" "${args[@]}"
 171         comp=$($PRINTF "$format" ${args[@]})
 172         checkrv $t 
 173         [[ -n "$debug" ]] && echo "got [$comp]"
 174         good=$result
 175         compare $t "$comp" "$good"
 176         test_pass $t
 177 done


  10 # source.  A copy of the CDDL is also available via the Internet at
  11 # http://www.illumos.org/license/CDDL.
  12 #
  13 
  14 #
  15 # Copyright 2014 Garrett D'Amore <garrett@damore.org>
  16 #
  17 
  18 PRINTF=${PRINTF:=/usr/bin/printf}
  19 
  20 test_start() {
  21         print "TEST STARTING ${1}: ${2}"
  22 }
  23 
  24 test_pass() {
  25         print "TEST PASS: ${1}"
  26 }
  27 
  28 test_fail() {
  29         print "TEST FAIL: ${1}: ${2}"
  30         exit -1
  31 }
  32 
  33 checkrv() {
  34         if [[ $? -ne 0 ]]; then
  35                 test_fail $1 "exit failure"
  36         fi
  37 }
  38 
  39 compare() {
  40         if [[ "$2" != "$3" ]]; then
  41                 test_fail $1 "compare mismatch, got [$2] expected [$3]"
  42         fi
  43 }
  44 
  45 typeset -A tests=()
  46 
  47 
  48 typeset -A tests[01]=()
  49 tests[01][desc]="hexadecimal lowercase"
  50 tests[01][format]='%04x'


 126 typeset -A tests[13]=()
 127 tests[13][desc]="left justify"
 128 tests[13][format]='%-5d'
 129 tests[13][args]="45"
 130 tests[13][result]='45   '
 131 
 132 typeset -A tests[14]=()
 133 tests[14][desc]="newlines"
 134 tests[14][format]='%s\n%s\n%s'
 135 tests[14][args]="one two three"
 136 tests[14][result]='one
 137 two
 138 three'
 139 
 140 typeset -A tests[15]=()
 141 tests[15][desc]="embedded octal escape"
 142 tests[15][format]='%s\41%s'
 143 tests[15][args]="one two"
 144 tests[15][result]='one!two'
 145 
 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'

 151 
 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'

 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 
 176 #debug=yes
 177 
 178 for i in "${!tests[@]}"; do
 179         t=test_$i
 180         desc=${tests[$i][desc]}
 181         format=${tests[$i][format]}
 182         args="${tests[$i][args]}"
 183         result=${tests[$i][result]}
 184         
 185         test_start $t "${tests[$i][desc]}"
 186         [[ -n "$debug" ]] && echo $PRINTF "$format" "${args[@]}"
 187         comp=$($PRINTF "$format" ${args[@]})
 188         checkrv $t 
 189         [[ -n "$debug" ]] && echo "got [$comp]"
 190         good=$result
 191         compare $t "$comp" "$good"
 192         test_pass $t
 193 done