1 #! /usr/bin/ksh
   2 #
   3 #
   4 # This file and its contents are supplied under the terms of the
   5 # Common Development and Distribution License ("CDDL"), version 1.0.
   6 # You may only use this file in accordance with the terms of version
   7 # 1.0 of the CDDL.
   8 #
   9 # A full copy of the text of the CDDL should have accompanied this
  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'
  51 tests[01][args]="255"
  52 tests[01][result]="00ff"
  53 
  54 typeset -A tests[02]=()
  55 tests[02][desc]="hexadecimal 32-bit" 
  56 tests[02][format]='%08x'
  57 tests[02][args]='65537'
  58 tests[02][result]=00010001
  59 
  60 typeset -A tests[03]=()
  61 tests[03][desc]="multiple arguments"
  62 tests[03][format]='%d %s '
  63 tests[03][args]="1 one 2 two 3 three"
  64 tests[03][result]='1 one 2 two 3 three '
  65 
  66 typeset -A tests[04]=()
  67 tests[04][desc]="variable position parameters"
  68 tests[04][format]='%2$s %1$d '
  69 tests[04][args]="1 one 2 two 3 three"
  70 tests[04][result]='one 1 two 2 three 3 '
  71 
  72 typeset -A tests[05]=()
  73 tests[05][desc]="width"
  74 tests[05][format]='%10s'
  75 tests[05][args]="abcdef"
  76 tests[05][result]='    abcdef'
  77 
  78 typeset -A tests[06]=()
  79 tests[06][desc]="width and precision"
  80 tests[06][format]='%10.3s'
  81 tests[06][args]="abcdef"
  82 tests[06][result]='       abc'
  83 
  84 typeset -A tests[07]=()
  85 tests[07][desc]="variable width and precision"
  86 tests[07][format]='%*.*s'
  87 tests[07][args]="10 3 abcdef"
  88 tests[07][result]='       abc'
  89 
  90 typeset -A tests[08]=()
  91 tests[08][desc]="variable position width and precision"
  92 tests[08][format]='%2$*1$.*3$s'
  93 tests[08][args]="10 abcdef 3"
  94 tests[08][result]='       abc'
  95 
  96 typeset -A tests[09]=()
  97 tests[09][desc]="multi variable position width and precision"
  98 tests[09][format]='%2$*1$.*3$s'
  99 tests[09][args]="10 abcdef 3 5 xyz 1"
 100 tests[09][result]='       abc    x'
 101 
 102 typeset -A tests[10]=()
 103 tests[10][desc]="decimal from hex"
 104 tests[10][format]='%d '
 105 tests[10][args]="0x1000 0XA"
 106 tests[10][result]='4096 10 '
 107 
 108 typeset -A tests[11]=()
 109 tests[11][desc]="negative dec (64-bit)"
 110 tests[11][format]='%x'
 111 tests[11][args]="-1"
 112 tests[11][result]='ffffffffffffffff'
 113 
 114 typeset -A tests[12]=()
 115 tests[12][desc]="float (basic)"
 116 tests[12][format]='%f'
 117 tests[12][args]="3.14"
 118 tests[12][result]='3.140000'
 119 
 120 typeset -A tests[12]=()
 121 tests[12][desc]="float precision"
 122 tests[12][format]='%.2f'
 123 tests[12][args]="3.14159"
 124 tests[12][result]='3.14'
 125 
 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