Shunting yard algorithm (C)/Tests

From LiteratePrograms
Jump to: navigation, search

This script will compile the code in Shunting yard algorithm (C) and run some simple tests on it.

<<tests.sh>>=
#!/bin/sh

oktest() {
	echo -n "Testing \"$1\" (Expected result: $2)... "
	result=`./a.out "$1" 2>stderr.log`
	if test "$?" -ne "0"; then
		echo -n "Failed: "
		cat stderr.log
	elif test "$result" -eq "$2"; then
		echo "Ok"
	else
		echo "Error: Result is: $result"
	fi
}

failtest() {
	echo -n "Testing \"$1\" (Should fail)... "
	result=`./a.out "$1" 2>stderr.log`
	if test "$?" -ne "0"; then
		grep "$2" stderr.log >/dev/null
		if test "$?" -eq "0"; then
			echo "Ok."
		else
			echo -n "Error: Wrong message: "
			cat stderr.log
		fi
	else
		echo "Error: Didn't fail. Result is: $result"
	fi
}

cc -Wall -ansi shunt.c

oktest '1' 1
oktest '1+2' 3 
oktest '1-2' -1
oktest '1*2' 2
oktest '1/2' 0
oktest '1%2' 1
oktest '1- 2' -1
oktest '1 -2' -1
oktest '1 - 2' -1
oktest ' 1 - 2' -1
oktest ' 1 - 2 ' -1
oktest '-1' -1
oktest ' -1' -1
oktest ' - 1' -1
oktest ' - 1 ' -1
oktest '-(1)' -1
oktest '(-1)' -1

failtest 'a' 'Syntax'
failtest '1a' 'Syntax'
failtest 'a1' 'Syntax'
failtest '1a1' 'Syntax'

failtest '1+' 'Number stack'
failtest '+1' 'binary'
failtest '(1+)1' 'binary'
failtest '1(+)1' 'binary'

failtest '1)' 'Operator stack'
failtest '(1' 'Number stack'
Download code
hijacker
hijacker
hijacker
hijacker