summaryrefslogtreecommitdiff
path: root/test/colm.d/gentests.sh
blob: 9198633bddc1d47c092d4d6d74b5a82a59c1cf2c (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/bash
#

# Test cases contain sections which specify the program, the input and the
# expected output. The first section has no header and is always the colm
# program. The sections afterwards can be in any order.

#
#   colm program
#
###### ARGS #####
#
#   program arguments
#
###### COMP ######
#
#   compilation arguments
#
###### IN #####
#
#   program input
#
###### EXP #####
#
#   expected output
#
###### EXIT ######
#
#   expected exit value
#
###### HOST ######
#
# Host program.
#
###### CALL ######
#
# files containing C functions
#

#######################################

WORKING=working
ERRORS=0

# Make available to to test directories below us that are not part of this
# repository and cannot source one dir up.
export COLM_BIN="@SUBJ_COLM_BIN@"
export COLM_CPPFLAGS="@SUBJ_COLM_CPPFLAGS@"
export COLM_LDFLAGS="@SUBJ_COLM_LDFLAGS@"

# cd `dirname $0`
test -d $WORKING || mkdir $WORKING

function die()
{
	echo
	echo "$@"
	echo
	exit 1
}

function sig_exit()
{
	echo
	exit 1;
}

function echo_cmd()
{
	echo $@
	$@
}

function check_compilation()
{
	if [ $1 != 0 ]; then
		echo "ERROR: $TST cannot be run: compilation error"
		ERRORS=$(( ERRORS + 1 ))
		continue
		return 1
	fi
}

trap sig_exit SIGINT
trap sig_exit SIGQUIT
trap sig_exit SIGTERM

# Parse args.
while getopts vdm opt; do
	case $opt in
		v)
			verbose=true;
		;;
		d)
			diff=true;
		;;
		m)
			VALGRIND="valgrind --leak-check=full --show-reachable=yes "
		;;
	esac
done
shift $(($OPTIND - 1))

# The files to process. If none given then glob all functions and pcap test confs.
if [ $# != 0 ]; then
	TEST_PAT="$*"
else
	TEST_PAT='*.lm'
fi 

function cat_section
{
	local section=$1
	local nth=$2
	local in=$3

	# Print Nth instance of the section
	awk -vsection=$section -vnth=$nth '
		BEGIN {
			if ( section == "LM" ) {
				found = 1
				in_section = 1;
			}
		}

		/#+ *[a-zA-Z]+ *#+/ {
			gsub( "[ #\n]", "", $0 );
			in_section = 0
			if ( $0 == section ) {
				if ( n == nth ) {
					in_section = 1;
					found = 1;
				}
				n += 1
			}
			next;
		}

		in_section {
			print $0;
		}

		END {
			exit( found ? 0 : 1 )
		}
	' $in | awk '
		/--noeol$/ {
			gsub(/--noeol$/,"");
			printf("%s", $0);
			next;
		}
		{ print $0 }
	'
	return ${PIPESTATUS[0]};
}

function section
{
	local section=$1
	local nth=$2
	local in=$3
	local out=$4

	cat_section $section $nth $in > $out

	# Remove the file if no section was found
	[ $? = 0 ] || rm $out
}

function runtests()
{
	for TST in $TEST_PAT; do
		if [ -d $TST ]; then
			cd $TST;
			./runtests
			cd ..
			continue
		fi

		ROOT=${TST/.lm}
		LM=$WORKING/$ROOT.lm
		HOST=$WORKING/$ROOT.host.cc
		CALL=$WORKING/$ROOT.call.c
		SH=$WORKING/$ROOT.sh

		section LM 0 $TST $LM

		BIN=$WORKING/$ROOT
		OUT=$WORKING/$ROOT.out
		DIFF=$WORKING/$ROOT.diff
		LOG=$WORKING/$ROOT.log

		if [ '!' -f $LM ]; then
			echo "ERROR: $TST cannot be run: no LM section"
			ERRORS=$(( ERRORS + 1 ))
			continue
		fi

		section CALL 0 $TST $CALL
		section HOST 0 $TST $HOST

		COLM_ADDS=""
		if test -f $CALL; then
			COLM_ADDS="-a $CALL"
		fi

		COMP=`cat_section COMP 0 $TST`

		echo echo testing $ROOT >> $SH
		echo rm -f $DIFF >> $SH
	
		if test -f $HOST; then
			PARSE=$WORKING/$ROOT.parse
			IF=$WORKING/$ROOT.if

			echo $COLM_BIN $COMP -c -o $PARSE.c -e $IF.h -x $IF.cc $LM >> $SH
			if ! check_compilation $?; then
				continue
			fi

			echo gcc -c $COLM_CPPFLAGS $COLM_LDFLAGS -o $PARSE.o $PARSE.c >> $SH
			echo g++ -I. $COLM_CPPFLAGS $COLM_LDFLAGS -o $WORKING/$ROOT $IF.cc $HOST $PARSE.o -lcolm >> $SH

			if ! check_compilation $?; then
				continue
			fi
		else
			# Compilation.
			echo $COLM_BIN $COMP $COLM_ADDS $LM '&>' $LOG >> $SH
			echo "if [ \$? != 0 ]; then echo \"COMPILATION FAILED (see $LOG)\" >> $DIFF; fi" >> $SH
		fi

		Nth=0
		while true; do
			ARGS=$WORKING/$ROOT-$Nth.args
			IN=$WORKING/$ROOT-$Nth.in
			EXP=$WORKING/$ROOT-$Nth.exp

			section EXP $Nth $TST $EXP

			# Stop when we have no Nth expected output, unless, there were no
			# expected outputs at all. In that case we continue to run with an
			# empty expected output.
			if [ '!' -f $EXP ]; then
				if [ $Nth == 0 ]; then
					echo -n > $EXP
				else
					break;
				fi
			fi

			section ARGS $Nth $TST $ARGS
			section IN $Nth $TST $IN
			EXIT=`cat_section EXIT $Nth $TST`
			if [ -z "$EXIT" ]; then
				EXIT=0
			fi

			cmdargs=""
			if [ -f $ARGS ]; then
				cmdargs=`cat $ARGS`
			fi

			if [ '!' -f $IN ] && [ -f $ROOT.in ]; then
				IN=$ROOT.in;
			fi

			# Execution
			if [ -f $IN ]; then
				echo ${VALGRIND}./$BIN $cmdargs '<' $IN '>' $OUT '2>>' $LOG >> $SH
			else
				echo ${VALGRIND}./$BIN $cmdargs '>' $OUT '2>>' $LOG >> $SH
			fi

			cat <<-EOF >> $SH
			e=\$?
			if [ \$e != "$EXIT" ]; then
				echo "FAILED: exit value error: got: \$e expected: $EXIT"
			fi
			EOF

			# Diff of output
			echo diff -u $EXP $OUT '>>' $DIFF >> $SH
			#if [ $? != 0 ]; then
			#	echo "FAILED: output differs from expected output"
			#	ERRORS=$(( ERRORS + 1 ))
			#	Nth=$((Nth + 1))
			#	if [ "$diff" = true ]; then
			#		echo
			#		cat $DIFF
			#		echo
			#	fi
			#	continue
			#fi

			#echo ok
			Nth=$((Nth + 1))

		done
		echo $SH
	done

#	if [ $ERRORS != 0 ]; then
#		[ $ERRORS != 1 ] && plural="s";
#		echo
#		echo "TESTING FAILED: $ERRORS failure$plural"
#		echo
#		EXIT=1
#	fi
}

rm -f $WORKING/*
runtests 

#exit $EXIT;