1#! /usr/bin/ksh93
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright 2020 Joyent, Inc.
16#
17
18AWK=/usr/bin/awk
19WORKDIR=$(mktemp -d /tmp/nawktest.XXXXXX)
20
21SUCCESSES=0
22TOTAL=0
23
24function proctemplate {
25	bash <<-EOF
26	IFS= read -rd '' OUTPUT < $1;
27	printf "%s" "\${OUTPUT//\\\$AWK/\$AWK}";
28	EOF
29}
30
31while [[ $# -gt 0 ]]; do
32	case $1 in
33		-o)
34		AWK=$2
35		shift 2
36		;;
37		*)
38		printf 'Usage: runtests.sh [-o <override awk executable>]\n' >&2
39		exit 1
40		;;
41	esac
42done
43
44# Make path absolute so we can change directories.
45AWK=$(cd $(dirname $AWK); pwd)/$(basename $AWK)
46TOP=$(cd $(dirname $0); pwd)
47
48# Move into $TOP in case we were run from elsewhere.
49cd $TOP
50
51if [[ ! -x $AWK ]]; then
52	printf 'awk executable "%s" is not executable\n' "$AWK" >&2
53	exit 1
54fi
55
56if [[ ! -x /bin/bash ]]; then
57	printf 'executable "/bin/bash" not found\n' >&2
58	exit 1
59fi
60
61if [[ "$(id -u)" == "0" ]]; then
62	printf 'runtests.sh should not be run as root\n' >&2
63	exit 1
64fi
65
66
67export AWK
68export WORKDIR
69export UMEM_DEBUG="default"
70
71mkdir -p $WORKDIR
72
73printf 'Running AWK tests ($AWK="%s")\n' "$AWK"
74
75printf '\n# Examples from "The AWK Programming Environment"\n\n'
76
77for script in examples/awk/p.*; do
78	((TOTAL+=1))
79	printf "$script... "
80	if cmp -s <($AWK -f ${script} data/test.countries 2>&1) ${script/awk/out}; then
81		printf "ok\n"
82		((SUCCESSES+=1))
83	else
84		printf "failed\n"
85	fi
86done
87
88printf '\n# One True AWK Example Programs\n\n'
89
90for script in examples/awk/t.*; do
91	((TOTAL+=1))
92	printf "$script... "
93	if diff <($AWK -f ${script} data/test.data 2>&1) ${script/awk/out}; then
94		printf "ok\n"
95		((SUCCESSES+=1))
96	else
97		printf "failed\n"
98	fi
99done
100
101cd bugs-fixed || exit 1
102for PROG in *.awk; do
103	((TOTAL+=1))
104	export LANG=C
105	printf "$PROG... "
106	$AWK -f $PROG > $WORKDIR/test.temp.out 2>&1 || \
107	    echo EXIT CODE: $? >> $WORKDIR/test.temp.out
108	if diff $WORKDIR/test.temp.out <(proctemplate ${PROG/.awk/.ok}); then
109		printf "ok\n"
110		((SUCCESSES+=1))
111	else
112		printf "failed\n"
113	fi
114done
115cd $TOP
116
117# Run the test programs
118
119printf '\n# One True AWK Test Programs\n\n'
120
121cd tests || exit 1
122for script in ./T.*; do
123	((TOTAL+=1))
124	rm -f $WORKDIR/test.temp*
125	printf "$script... "
126	if $script > /dev/null 2>&1; then
127		printf "ok\n"
128		((SUCCESSES+=1))
129	else
130		printf "failed\n"
131	fi
132done
133cd $TOP
134
135printf '\n# Imported GAWK Test Programs\n\n'
136
137cd gnu || exit 1
138for PROG in *.awk; do
139	((TOTAL+=1))
140	export LANG=C
141	printf "$PROG... "
142	INPUT="${PROG/.awk/.in}"
143	if [[ -f $INPUT ]]; then
144		$AWK -f $PROG < $INPUT > $WORKDIR/test.temp.out 2>&1 || \
145		    echo EXIT CODE: $? >> $WORKDIR/test.temp.out
146	else
147		$AWK -f $PROG > $WORKDIR/test.temp.out 2>&1 || \
148		    echo EXIT CODE: $? >> $WORKDIR/test.temp.out
149	fi
150	if diff $WORKDIR/test.temp.out ${PROG/.awk/.ok}; then
151		printf "ok\n"
152		((SUCCESSES+=1))
153	else
154		printf "failed\n"
155	fi
156done
157
158for script in ./*.sh; do
159	((TOTAL+=1))
160	export LANG=C
161	printf "$script... "
162	$script > $WORKDIR/test.temp.out 2>&1
163	if diff $WORKDIR/test.temp.out ${script/.sh/.ok}; then
164		printf "ok\n"
165		((SUCCESSES+=1))
166	else
167		printf "failed\n"
168	fi
169done
170cd $TOP
171
172printf '\n# Imported GAWK Syntax Tests\n\n'
173
174cd syn || exit 1
175for PROG in *.awk; do
176	((TOTAL+=1))
177	printf "$PROG... "
178	if $AWK -f $PROG /dev/null > /dev/null 2> $WORKDIR/test.temp.out; then
179		printf "failed (should exit nonzero)\n"
180		continue
181	fi
182
183	if diff $WORKDIR/test.temp.out <(proctemplate ${PROG/.awk/.ok}); then
184		printf "ok\n"
185		((SUCCESSES+=1))
186	else
187		printf "failed\n"
188	fi
189done
190cd $TOP
191
192printf '\n\nTOTAL: %d/%d\n' "$SUCCESSES" "$TOTAL"
193
194rm -rf $WORKDIR
195
196if [[ $SUCCESSES != $TOTAL ]]; then
197	exit 1
198fi
199