1 #!/bin/sh 2 3 echo T.expr: tests of miscellaneous expressions 4 5 awk=${awk-../a.out} 6 7 $awk ' 8 BEGIN { 9 FS = "\t" 10 awk = "../a.out" 11 } 12 NF == 0 || $1 ~ /^#/ { 13 next 14 } 15 $1 ~ /try/ { # new test 16 nt++ 17 sub(/try /, "") 18 prog = $0 19 printf("%3d %s\n", nt, prog) 20 prog = sprintf("%s -F\"\\t\" '"'"'%s'"'"'", awk, prog) 21 # print "prog is", prog 22 nt2 = 0 23 while (getline > 0) { 24 if (NF == 0) # blank line terminates a sequence 25 break 26 input = $1 27 for (i = 2; i < NF; i++) # input data 28 input = input "\t" $i 29 test = sprintf("./echo '"'"'%s'"'"' | %s >foo1; ", 30 input, prog) 31 if ($NF == "\"\"") 32 output = ">foo2;" 33 else 34 output = sprintf("./echo '"'"'%s'"'"' >foo2; ", $NF) 35 gsub(/\\t/, "\t", output) 36 gsub(/\\n/, "\n", output) 37 run = sprintf("cmp foo1 foo2 || echo test %d.%d failed", 38 nt, ++nt2) 39 # print "input is", input 40 # print "test is", test 41 # print "output is", output 42 # print "run is", run 43 system(test output run) 44 } 45 tt += nt2 46 } 47 END { print tt, "tests" } 48 ' <<\!!!! 49 # General format: 50 # try program as rest of line 51 # $1 $2 $3 output1 (\t for tab, \n for newline, 52 # $1 $2 $3 output2 ("" for null) 53 # ... terminated by blank line 54 55 # try another program... 56 57 try { print ($1 == 1) ? "yes" : "no" } 58 1 yes 59 1.0 yes 60 1E0 yes 61 0.1E1 yes 62 10E-1 yes 63 01 yes 64 10 no 65 10E-2 no 66 67 try $1 > 0 68 1 1 69 2 2 70 0 "" 71 -1 "" 72 1e0 1e0 73 0e1 "" 74 -2e64 "" 75 3.1e4 3.1e4 76 77 try { print NF } 78 0 79 x 1 80 x y 2 81 y 2 82 x 2 83 84 try { print NF, $NF } 85 0 86 x 1 x 87 x y 2 y 88 x yy zzz 3 zzz 89 90 # this horror prints $($2+1) 91 try { i=1; print ($++$++i) } 92 1 1 93 1 2 3 3 94 abc abc 95 96 # concatenate $1 and ++$2; print new $1 and concatenated value 97 try { x = $1++++$2; print $1, x } 98 1 3 2 14 99 100 # do we get the precedence of ! right? 101 try $1 !$2 102 0 0 0\t0 103 0 1 0\t1 104 1 0 1\t0 105 1 1 1\t1 106 107 # another ava special 108 try { print ($1~/abc/ !$2) } 109 0 0 01 110 0 1 00 111 abc 0 11 112 xabcd 1 10 113 114 try { print !$1 + $2 } 115 1 3 3 116 0 3 4 117 -1 3 3 118 119 # aside: !$1 = $2 is now a syntax error 120 121 # the definition of "number" changes with isnumber. 122 # 2e100 is ok according to strtod. 123 # try 1 124 125 try { print ($1 == $2) } 126 0 0 1 127 0 1 0 128 0 00 1 129 0 "" 0 130 +0 -0 1 131 1 1.0 1 132 1 1e0 1 133 2e10 2.00e10 1 134 2e10 2e+10 1 135 2e-10 2e-10 1 136 2e10 2e-10 0 137 2e10 20e9 1 138 2e100 2.000e100 1 139 2e1000 2.0e1000 0 140 141 # this one (3 & 4) may "fail" if a negative 0 is printed as -0, 142 # but i think this might be a type-coercion problem. 143 144 try { print $1, +$1, -$1, - -$1 } 145 1 1 1 -1 1 146 -1 -1 -1 1 -1 147 0 0 0 0 0 148 x x 0 0 0 149 150 try { printf("a%*sb\n", $1, $2) } 151 1 x axb 152 2 x a xb 153 3 x a xb 154 155 try { printf("a%-*sb\n", $1, $2) } 156 1 x axb 157 2 x ax b 158 3 x ax b 159 160 try { printf("a%*.*sb\n", $1, $2, "hello") } 161 1 1 ahb 162 2 1 a hb 163 3 1 a hb 164 165 try { printf("a%-*.*sb\n", $1, $2, "hello") } 166 1 1 ahb 167 2 1 ah b 168 3 1 ah b 169 170 try { printf("%d %ld %lld %zd %jd %hd %hhd\n", $1, $1, $1, $1, $1, $1, $1) } 171 1 1 1 1 1 1 1 1 172 10 10 10 10 10 10 10 10 173 10000 10000 10000 10000 10000 10000 10000 10000 174 175 try { printf("%x %lx %llx %zx %jx %hx %hhx\n", $1, $1, $1, $1, $1, $1, $1) } 176 1 1 1 1 1 1 1 1 177 10 a a a a a a a 178 10000 2710 2710 2710 2710 2710 2710 2710 179 180 try { if ($1 ~ $2) print 1; else print 0 } 181 a \141 1 182 a \142 0 183 a \x61 1 184 a \x061 0 185 a \x62 0 186 0 \060 1 187 0 \60 1 188 0 \0060 0 189 Z \x5a 1 190 Z \x5A 1 191 192 try { print $1 ~ $2 } 193 a \141 1 194 a \142 0 195 a \x61 1 196 a \x061 0 197 a \x62 0 198 0 \060 1 199 0 \60 1 200 0 \0060 0 201 Z \x5a 1 202 Z \x5A 1 203 204 try { print $1 || $2 } 205 0 206 1 1 207 0 0 0 208 1 0 1 209 0 1 1 210 1 1 1 211 a b 1 212 213 try { print $1 && $2 } 214 0 215 1 0 216 0 0 0 217 1 0 0 218 0 1 0 219 1 1 1 220 a b 1 221 222 try { $1 = $2; $1 = $1; print $1 } 223 abc def def 224 abc def ghi def 225 226 # $f++ => ($f)++ 227 try { f = 1; $f++; print f, $f } 228 11 22 33 1 12 229 230 # $f[1]++ => ($f[1])++ 231 try { f[1]=1; f[2]=2; print $f[1], $f[1]++, $f[2], f[1], f[2] } 232 111 222 333 111 111 222 2 2 233 234 235 !!!! 236