1#!/bin/bash 2 3if [[ -z "$AWK" || -z "$WORKDIR" ]]; then 4 printf '$AWK and $WORKDIR must be set\n' >&2 5 exit 1 6fi 7 8TEMP0=$WORKDIR/test.temp.0 9TEMP1=$WORKDIR/test.temp.1 10TEMP2=$WORKDIR/test.temp.2 11 12RESULT=0 13 14fail() { 15 echo "$1" >&2 16 RESULT=1 17} 18 19echo T.builtin: test miscellaneous builtin functions 20 21$AWK 'BEGIN { print index(123, substr(123, 2)) }' > $TEMP1 22echo 2 > $TEMP2 23diff $TEMP1 $TEMP2 || fail 'BAD: T.builtin (index/substr)' 24 25$AWK 'BEGIN { 26 pi = 2 * atan2(1, 0) 27 printf("%.5f %.3f %.3f %.5f %.3f\n", 28 pi, sin(pi), cos(pi/2), exp(log(pi)), log(exp(10))) 29}' > $TEMP1 30echo '3.14159 0.000 0.000 3.14159 10.000' > $TEMP2 31diff $TEMP1 $TEMP2 || fail 'BAD: T.builtin (sin/cos)' 32 33$AWK 'BEGIN { 34 s = srand(1) # set a real random start 35 for (i = 1; i <= 10; i++) 36 print rand() >"'$TEMP1'" 37 srand(s) # reset it 38 for (i = 1; i <= 10; i++) 39 print rand() >"'$TEMP2'" 40}' 41diff $TEMP1 $TEMP2 || fail 'BAD: T.builtin (rand)' 42 43echo 'hello, WORLD!' | 44$AWK '{ printf("%s|%s|%s\n", tolower($0), toupper($0), $0)}' > $TEMP1 45echo 'hello, world!|HELLO, WORLD!|hello, WORLD!' > $TEMP2 46diff $TEMP1 $TEMP2 || fail 'BAD: T.builtin (toupper/tolower)' 47 48$AWK 'BEGIN { 49 j = 1; sprintf("%d", 99, ++j) # does j get incremented? 50 if (j != 2) 51 print "BAD: T.builtin (printf arg list not evaluated)" 52}' 53 54$AWK 'BEGIN { 55 j = 1; substr("", 1, ++j) # does j get incremented? 56 if (j != 2) 57 print "BAD: T.builtin (substr arg list not evaluated)" 58}' 59 60$AWK 'BEGIN { 61 j = 1; sub(/1/, ++j, z) # does j get incremented? 62 if (j != 2) 63 print "BAD: T.builtin (sub() arg list not evaluated)" 64}' 65 66$AWK 'BEGIN { 67 j = 1; length("zzzz", ++j, ++j) # does j get incremented? 68 if (j != 3) 69 print "BAD: T.builtin (excess length args not evaluated)" 70}' 2> $TEMP1 71grep 'too many arg' $TEMP1 >/dev/null || fail 'T.bad: too many args not caught' 72 73echo 'a 74a b 75a b c' > $TEMP0 76echo '1 772 783' > $TEMP1 79$AWK '{ n = split($0, x); print length(x) }' < $TEMP0 > $TEMP2 80diff $TEMP1 $TEMP2 || fail 'BAD: T.builtin length array' 81 82exit $RESULT 83