1#!/bin/sh 2 3if [[ -z "$AWK" || -z "$WORKDIR" ]]; then 4 printf '$AWK and $WORKDIR must be set\n' >&2 5 exit 1 6fi 7 8TEMP1=$WORKDIR/test.temp.1 9TEMP2=$WORKDIR/test.temp.2 10 11echo T.expr: tests of miscellaneous expressions 12 13$AWK ' 14BEGIN { 15 FS = "\t" 16 awk = ENVIRON["AWK"] 17} 18NF == 0 || $1 ~ /^#/ { 19 next 20} 21$1 ~ /try/ { # new test 22 nt++ 23 sub(/try /, "") 24 prog = $0 25 printf("%3d %s\n", nt, prog) 26 prog = sprintf("%s -F\"\\t\" '"'"'%s'"'"'", awk, prog) 27 # print "prog is", prog 28 nt2 = 0 29 while (getline > 0) { 30 if (NF == 0) # blank line terminates a sequence 31 break 32 input = $1 33 for (i = 2; i < NF; i++) # input data 34 input = input "\t" $i 35 test = sprintf("printf %%s\\\\n '"'"'%s'"'"' | %s > '$TEMP1'; ", 36 input, prog) 37 gsub(/\\t/, "\t", $NF) 38 gsub(/\\n/, "\n", $NF) 39 if ($NF == "\"\"") 40 output = "> '$TEMP2';" 41 else 42 output = sprintf("printf %%s\\\\n '"'"'%s'"'"' > '$TEMP2'; ", $NF) 43 run = sprintf("diff '$TEMP1' '$TEMP2'") 44 msg = sprintf("test %d.%d failed", nt, ++nt2); 45 # print "input is", input 46 # print "test is", test 47 # print "output is", output 48 # print "run is", run 49 tcode = system(test output run) 50 if (tcode > 0) { 51 print msg 52 ecode = tcode 53 } 54 } 55 tt += nt2 56} 57END { 58 print tt, "tests" 59 exit ecode 60} 61' <<\!!!! 62# General format: 63# try program as rest of line 64# $1 $2 $3 output1 (\t for tab, \n for newline, 65# $1 $2 $3 output2 ("" for null) 66# ... terminated by blank line 67 68# try another program... 69 70try { print ($1 == 1) ? "yes" : "no" } 711 yes 721.0 yes 731E0 yes 740.1E1 yes 7510E-1 yes 7601 yes 7710 no 7810E-2 no 79 80try $1 > 0 811 1 822 2 830 "" 84-1 "" 851e0 1e0 860e1 "" 87-2e64 "" 883.1e4 3.1e4 89 90try { print NF } 91 0 92x 1 93x y 2 94 y 2 95x 2 96 97try { print NF, $NF } 98 0 99x 1 x 100x y 2 y 101x yy zzz 3 zzz 102 103# this horror prints $($2+1) 104try { i=1; print ($++$++i) } 1051 1 1061 2 3 3 107abc abc 108 109# concatenate $1 and ++$2; print new $1 and concatenated value 110try { x = $1++++$2; print $1, x } 1111 3 2 14 112 113# do we get the precedence of ! right? 114try $1 !$2 1150 0 0\t0 1160 1 0\t1 1171 0 1\t0 1181 1 1\t1 119 120# another ava special 121try { print ($1~/abc/ !$2) } 1220 0 01 1230 1 00 124abc 0 11 125xabcd 1 10 126 127try { print !$1 + $2 } 1281 3 3 1290 3 4 130-1 3 3 131 132# aside: !$1 = $2 is now a syntax error 133 134# the definition of "number" changes with isnumber. 135# 2e100 is ok according to strtod. 136# try 1 137 138try { print ($1 == $2) } 1390 0 1 1400 1 0 1410 00 1 1420 "" 0 143+0 -0 1 1441 1.0 1 1451 1e0 1 1462e10 2.00e10 1 1472e10 2e+10 1 1482e-10 2e-10 1 1492e10 2e-10 0 1502e10 20e9 1 1512e100 2.000e100 1 1522e1000 2.0e1000 0 153 154# this one (3 & 4) may "fail" if a negative 0 is printed as -0, 155# but i think this might be a type-coercion problem. 156 157try { print $1, +$1, -$1, - -$1 } 1581 1 1 -1 1 159-1 -1 -1 1 -1 1600 0 0 0 0 161x x 0 0 0 162 163try { printf("a%*sb\n", $1, $2) } 1641 x axb 1652 x a xb 1663 x a xb 167 168try { printf("a%-*sb\n", $1, $2) } 1691 x axb 1702 x ax b 1713 x ax b 172 173try { printf("a%*.*sb\n", $1, $2, "hello") } 1741 1 ahb 1752 1 a hb 1763 1 a hb 177 178try { printf("a%-*.*sb\n", $1, $2, "hello") } 1791 1 ahb 1802 1 ah b 1813 1 ah b 182 183try { printf("%d %ld\n", $1, $1) } 1841 1 1 18510 10 10 18610000 10000 10000 187 188try { printf("%x %lx\n", $1, $1) } 1891 1 1 19010 a a 19110000 2710 2710 192 193try { if ($1 ~ $2) print 1; else print 0 } 194a \141 1 195a \142 0 196a \x61 1 197a \x061 0 198a \x62 0 1990 \060 1 2000 \60 1 2010 \0060 0 202Z \x5a 1 203Z \x5A 1 204 205try { print $1 ~ $2 } 206a \141 1 207a \142 0 208a \x61 1 209a \x061 0 210a \x62 0 2110 \060 1 2120 \60 1 2130 \0060 0 214Z \x5a 1 215Z \x5A 1 216 217try { print $1 || $2 } 218 0 2191 1 2200 0 0 2211 0 1 2220 1 1 2231 1 1 224a b 1 225 226try { print $1 && $2 } 227 0 2281 0 2290 0 0 2301 0 0 2310 1 0 2321 1 1 233a b 1 234 235try { $1 = $2; $1 = $1; print $1 } 236abc def def 237abc def ghi def 238 239# $f++ => ($f)++ 240try { f = 1; $f++; print f, $f } 24111 22 33 1 12 242 243# $f[1]++ => ($f[1])++ 244try { f[1]=1; f[2]=2; print $f[1], $f[1]++, $f[2], f[1], f[2] } 245111 222 333 111 111 222 2 2 246 247 248!!!! 249