1echo T.builtin: test miscellaneous builtin functions 2 3awk=${awk-../a.out} 4 5$awk 'BEGIN { print index(123, substr(123, 2)) }' >foo1 6echo 2 >foo2 7diff foo1 foo2 || echo 'BAD: T.builtin (index/substr)' 8 9$awk 'BEGIN { 10 pi = 2 * atan2(1, 0) 11 printf("%.5f %.3f %.3f %.5f %.3f\n", 12 pi, sin(pi), cos(pi/2), exp(log(pi)), log(exp(10))) 13}' >foo1 14echo '3.14159 0.000 0.000 3.14159 10.000' >foo2 15diff foo1 foo2 || echo 'BAD: T.builtin (sin/cos)' 16 17$awk 'BEGIN { 18 s = srand(1) # set a real random start 19 for (i = 1; i <= 10; i++) 20 print rand() >"foo1" 21 srand(s) # reset it 22 for (i = 1; i <= 10; i++) 23 print rand() >"foo2" 24}' 25diff foo1 foo2 || echo 'BAD: T.builtin (rand)' 26 27echo 'hello, WORLD!' | 28$awk '{ printf("%s|%s|%s\n", tolower($0), toupper($0), $0)}' >foo1 29echo 'hello, world!|HELLO, WORLD!|hello, WORLD!' >foo2 30diff foo1 foo2 || echo 'BAD: T.builtin (toupper/tolower)' 31 32 33if locale -a | grep -qsi de_DE.UTF-8; then 34 (export LANG=de_DE.UTF-8 && echo 'Dürst' | 35 $awk '{ printf("%s|%s|%s\n", tolower($0), toupper($0), $0)}') >foo1 36 echo 'dürst|DÜRST|Dürst' >foo2 37 diff foo1 foo2 || echo 'BAD: T.builtin (toupper/tolower) for utf-8' 38fi 39 40$awk 'BEGIN { 41 j = 1; sprintf("%d", 99, ++j) # does j get incremented? 42 if (j != 2) 43 print "BAD: T.builtin (printf arg list not evaluated)" 44}' 45 46$awk 'BEGIN { 47 j = 1; substr("", 1, ++j) # does j get incremented? 48 if (j != 2) 49 print "BAD: T.builtin (substr arg list not evaluated)" 50}' 51 52$awk 'BEGIN { 53 j = 1; sub(/1/, ++j, z) # does j get incremented? 54 if (j != 2) 55 print "BAD: T.builtin (sub() arg list not evaluated)" 56}' 57 58$awk 'BEGIN { 59 j = 1; length("zzzz", ++j, ++j) # does j get incremented? 60 if (j != 3) 61 print "BAD: T.builtin (excess length args not evaluated)" 62}' 2>foo 63grep 'too many arg' foo >/dev/null || echo 'T.bad: too many args not caught' 64 65echo 'a 66a b 67a b c' >foo0 68echo '1 692 703' >foo1 71$awk '{ n = split($0, x); print length(x) }' <foo0 >foo2 72diff foo1 foo2 || echo 'BAD: T.builtin length array' 73