summaryrefslogtreecommitdiff
path: root/usr/src/test/util-tests/tests/awk/runtests.sh
blob: fd94d3585ab6028b592868cf6e7a2f80872160d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#! /usr/bin/ksh93
#
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2020 Joyent, Inc.
#

AWK=/usr/bin/awk
WORKDIR=$(mktemp -d /tmp/nawktest.XXXXXX)

SUCCESSES=0
TOTAL=0

function proctemplate {
	bash <<-EOF
	IFS= read -rd '' OUTPUT < $1;
	printf "%s" "\${OUTPUT//\\\$AWK/\$AWK}";
	EOF
}

while [[ $# -gt 0 ]]; do
	case $1 in
		-o)
		AWK=$2
		shift 2
		;;
		*)
		printf 'Usage: runtests.sh [-o <override awk executable>]\n' >&2
		exit 1
		;;
	esac
done

# Make path absolute so we can change directories.
AWK=$(cd $(dirname $AWK); pwd)/$(basename $AWK)
TOP=$(cd $(dirname $0); pwd)

# Move into $TOP in case we were run from elsewhere.
cd $TOP

if [[ ! -x $AWK ]]; then
	printf 'awk executable "%s" is not executable\n' "$AWK" >&2
	exit 1
fi

if [[ ! -x /bin/bash ]]; then
	printf 'executable "/bin/bash" not found\n' >&2
	exit 1
fi

if [[ "$(id -u)" == "0" ]]; then
	printf 'runtests.sh should not be run as root\n' >&2
	exit 1
fi


export AWK
export WORKDIR
export UMEM_DEBUG="default"

mkdir -p $WORKDIR

printf 'Running AWK tests ($AWK="%s")\n' "$AWK"

printf '\n# Examples from "The AWK Programming Environment"\n\n'

for script in examples/awk/p.*; do
	((TOTAL+=1))
	printf "$script... "
	if cmp -s <($AWK -f ${script} data/test.countries 2>&1) ${script/awk/out}; then
		printf "ok\n"
		((SUCCESSES+=1))
	else
		printf "failed\n"
	fi
done

printf '\n# One True AWK Example Programs\n\n'

for script in examples/awk/t.*; do
	((TOTAL+=1))
	printf "$script... "
	if diff <($AWK -f ${script} data/test.data 2>&1) ${script/awk/out}; then
		printf "ok\n"
		((SUCCESSES+=1))
	else
		printf "failed\n"
	fi
done

cd bugs-fixed || exit 1
for PROG in *.awk; do
	((TOTAL+=1))
	export LANG=C
	printf "$PROG... "
	$AWK -f $PROG > $WORKDIR/test.temp.out 2>&1 || \
	    echo EXIT CODE: $? >> $WORKDIR/test.temp.out
	if diff $WORKDIR/test.temp.out <(proctemplate ${PROG/.awk/.ok}); then
		printf "ok\n"
		((SUCCESSES+=1))
	else
		printf "failed\n"
	fi
done
cd $TOP

# Run the test programs

printf '\n# One True AWK Test Programs\n\n'

cd tests || exit 1
for script in ./T.*; do
	((TOTAL+=1))
	rm -f $WORKDIR/test.temp*
	printf "$script... "
	if $script > /dev/null 2>&1; then
		printf "ok\n"
		((SUCCESSES+=1))
	else
		printf "failed\n"
	fi
done
cd $TOP

printf '\n# Imported GAWK Test Programs\n\n'

cd gnu || exit 1
for PROG in *.awk; do
	((TOTAL+=1))
	export LANG=C
	printf "$PROG... "
	INPUT="${PROG/.awk/.in}"
	if [[ -f $INPUT ]]; then
		$AWK -f $PROG < $INPUT > $WORKDIR/test.temp.out 2>&1 || \
		    echo EXIT CODE: $? >> $WORKDIR/test.temp.out
	else
		$AWK -f $PROG > $WORKDIR/test.temp.out 2>&1 || \
		    echo EXIT CODE: $? >> $WORKDIR/test.temp.out
	fi
	if diff $WORKDIR/test.temp.out ${PROG/.awk/.ok}; then
		printf "ok\n"
		((SUCCESSES+=1))
	else
		printf "failed\n"
	fi
done

for script in ./*.sh; do
	((TOTAL+=1))
	export LANG=C
	printf "$script... "
	$script > $WORKDIR/test.temp.out 2>&1
	if diff $WORKDIR/test.temp.out ${script/.sh/.ok}; then
		printf "ok\n"
		((SUCCESSES+=1))
	else
		printf "failed\n"
	fi
done
cd $TOP

printf '\n# Imported GAWK Syntax Tests\n\n'

cd syn || exit 1
for PROG in *.awk; do
	((TOTAL+=1))
	printf "$PROG... "
	if $AWK -f $PROG /dev/null > /dev/null 2> $WORKDIR/test.temp.out; then
		printf "failed (should exit nonzero)\n"
		continue
	fi

	if diff $WORKDIR/test.temp.out <(proctemplate ${PROG/.awk/.ok}); then
		printf "ok\n"
		((SUCCESSES+=1))
	else
		printf "failed\n"
	fi
done
cd $TOP

printf '\n\nTOTAL: %d/%d\n' "$SUCCESSES" "$TOTAL"

rm -rf $WORKDIR

if [[ $SUCCESSES != $TOTAL ]]; then
	exit 1
fi