Download code

Jump to: navigation, search

Back to Shunting_yard_algorithm_(C)/Tests

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

tests.sh

 1 #!/bin/sh
 2 # The authors of this work have released all rights to it and placed it
 3 # in the public domain under the Creative Commons CC0 1.0 waiver
 4 # (http://creativecommons.org/publicdomain/zero/1.0/).
 5 # 
 6 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 7 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 8 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 9 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
10 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
11 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
12 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 # 
14 # Retrieved from: http://en.literateprograms.org/Shunting_yard_algorithm_(C)/Tests?oldid=12064
15 
16 
17 oktest() {
18 	echo -n "Testing \"$1\" (Expected result: $2)... "
19 	result=`./a.out "$1" 2>stderr.log`
20 	if test "$?" -ne "0"; then
21 		echo -n "Failed: "
22 		cat stderr.log
23 	elif test "$result" -eq "$2"; then
24 		echo "Ok"
25 	else
26 		echo "Error: Result is: $result"
27 	fi
28 }
29 
30 failtest() {
31 	echo -n "Testing \"$1\" (Should fail)... "
32 	result=`./a.out "$1" 2>stderr.log`
33 	if test "$?" -ne "0"; then
34 		grep "$2" stderr.log >/dev/null
35 		if test "$?" -eq "0"; then
36 			echo "Ok."
37 		else
38 			echo -n "Error: Wrong message: "
39 			cat stderr.log
40 		fi
41 	else
42 		echo "Error: Didn't fail. Result is: $result"
43 	fi
44 }
45 
46 cc -Wall -ansi shunt.c
47 
48 oktest '1' 1
49 oktest '1+2' 3 
50 oktest '1-2' -1
51 oktest '1*2' 2
52 oktest '1/2' 0
53 oktest '1%2' 1
54 oktest '1- 2' -1
55 oktest '1 -2' -1
56 oktest '1 - 2' -1
57 oktest ' 1 - 2' -1
58 oktest ' 1 - 2 ' -1
59 oktest '-1' -1
60 oktest ' -1' -1
61 oktest ' - 1' -1
62 oktest ' - 1 ' -1
63 oktest '-(1)' -1
64 oktest '(-1)' -1
65 
66 failtest 'a' 'Syntax'
67 failtest '1a' 'Syntax'
68 failtest 'a1' 'Syntax'
69 failtest '1a1' 'Syntax'
70 
71 failtest '1+' 'Number stack'
72 failtest '+1' 'binary'
73 failtest '(1+)1' 'binary'
74 failtest '1(+)1' 'binary'
75 
76 failtest '1)' 'Operator stack'
77 failtest '(1' 'Number stack'
78 


hijacker
hijacker
hijacker
hijacker