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