github.com/benhoyt/goawk@v1.8.1/testdata/T.builtin (about) 1 echo T.builtin: test miscellaneous builtin functions 2 3 awk=${awk-../a.out} 4 5 $awk 'BEGIN { print index(123, substr(123, 2)) }' >foo1 6 echo 2 >foo2 7 diff 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 14 echo '3.14159 0.000 0.000 3.14159 10.000' >foo2 15 diff 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 }' 25 diff foo1 foo2 || echo 'BAD: T.builtin (rand)' 26 27 echo 'hello, WORLD!' | 28 $awk '{ printf("%s|%s|%s\n", tolower($0), toupper($0), $0)}' >foo1 29 echo 'hello, world!|HELLO, WORLD!|hello, WORLD!' >foo2 30 diff foo1 foo2 || echo 'BAD: T.builtin (toupper/tolower)' 31 32 $awk 'BEGIN { 33 j = 1; sprintf("%d", 99, ++j) # does j get incremented? 34 if (j != 2) 35 print "BAD: T.builtin (printf arg list not evaluated)" 36 }' 37 38 $awk 'BEGIN { 39 j = 1; substr("", 1, ++j) # does j get incremented? 40 if (j != 2) 41 print "BAD: T.builtin (substr arg list not evaluated)" 42 }' 43 44 $awk 'BEGIN { 45 j = 1; sub(/1/, ++j, z) # does j get incremented? 46 if (j != 2) 47 print "BAD: T.builtin (sub() arg list not evaluated)" 48 }' 49 50 $awk 'BEGIN { 51 j = 1; length("zzzz", ++j, ++j) # does j get incremented? 52 if (j != 3) 53 print "BAD: T.builtin (excess length args not evaluated)" 54 }' 2>foo 55 grep 'too many arg' foo >/dev/null || echo 'T.bad: too many args not caught' 56 57 echo 'a 58 a b 59 a b c' >foo0 60 echo '1 61 2 62 3' >foo1 63 $awk '{ n = split($0, x); print length(x) }' <foo0 >foo2 64 diff foo1 foo2 || echo 'BAD: T.builtin length array'