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