-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.sh
executable file
·157 lines (130 loc) · 3.85 KB
/
test.sh
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
#!/bin/bash
#
# Simple test-driver to exercise our compiler.
#
# We compile some simple test-cases and test that the results match what
# we expect.
#
# Compile an expression and compare the result with a fixed value
#
# If the optional third argument is present, and non-empty, then we
# return the full output from the execution. Otherwise just the last
# token.
test_compile() {
input="$1"
result="$2"
full="$3"
#
# Do this the long way round so we have assembly file for
# inspection if/when a test fails.
#
rm -f test.s test || true
go run main.go -- "${input}" > test.s
gcc -static -o ./test test.s
#
# Run the test.
#
out=`./test`
#
# If we're not doing a "full" match we only take the last token
#
if [ "${full}" = "" ]; then
out=$(echo "$out" | awk '{print $NF}')
fi
if [ "${result}" = "${out}" ]; then
echo "Expected output found for '$input' [$result] "
rm test test.s
else
echo "Expected output of '$input' is '$result' - got '${out}' instead"
exit 1
fi
}
# Simple operations
test_compile '1 2 3 4 + + +' 10
test_compile '3 4 5 * *' 60
test_compile '20 10 2 - -' 12
test_compile '20 4 2 / / ' 10
# Division by zero
test_compile '3 4 /' '0.75'
test_compile '3 0 /' 'Attempted division by zero. Aborting' 'full'
test_compile '3 5 5 - /' 'Attempted division by zero. Aborting' 'full'
# Missing arguments upon the stack
test_compile '4 +' 'Insufficient entries on the stack. Aborting' 'full'
test_compile '3 sin -' 'Insufficient entries on the stack. Aborting' 'full'
# Too many arguments on the stack
test_compile '3 3 3 +' 'Too many entries remaining on the stack. Aborting' 'full'
# modulus
test_compile '1 4 %' 1
test_compile '2 4 %' 2
test_compile '3 4 %' 3
test_compile '4 4 %' 0
test_compile '5 4 %' 1
test_compile '6 4 %' 2
test_compile '7 4 %' 3
test_compile '8 4 %' 0
test_compile '9 4 %' 1
test_compile '10 4 %' 2
test_compile '11 4 %' 3
test_compile '12 4 %' 0
# powers of two - the manual-way
test_compile '2 2 *' 4
test_compile '2 2 2 * *' 8
test_compile '2 2 2 2 * * *' 16
test_compile '2 2 2 2 2 * * * *' 32
test_compile '2 2 2 2 2 2 * * * * *' 64
test_compile '2 2 2 2 2 2 2 * * * * * *' 128
test_compile '2 2 2 2 2 2 2 2 * * * * * * *' 256
test_compile '2 2 2 2 2 2 2 2 2 * * * * * * * *' 512
test_compile '2 2 2 2 2 2 2 2 2 2 * * * * * * * * *' 1024
# Add an extreme example of calculating 2^24:
inp="2 2 *"
for i in $(seq 1 22 ) ; do
inp="${inp} 2 *"
done
test_compile "$inp" 1.67772e+07
# powers of two - the simple way
test_compile '2 0 ^' 0
test_compile '2 1 ^' 2
test_compile '2 2 ^' 4
test_compile '2 3 ^' 8
test_compile '2 4 ^' 16
test_compile '2 5 ^' 32
test_compile '2 6 ^' 64
test_compile '2 7 ^' 128
test_compile '2 8 ^' 256
test_compile '2 16 ^' 65536
test_compile '2 30 ^' 1.07374e+09
test_compile '2 300 ^' 'Overflow - value out of range. Aborting' 'full'
# factorials
test_compile '-3 !' 0
test_compile '0 !' 0
test_compile '1 !' 1
test_compile '2 !' 2
test_compile '3 !' 6
test_compile '4 !' 24
test_compile '5 !' 120
test_compile '6 !' 720
test_compile '5 5 + !' 3.6288e+06 # 3628800
test_compile '3 300 !' 'Overflow - value out of range. Aborting' 'full'
# division
test_compile '3 2 /' 1.5
test_compile '5 2 /' 2.5
# abs
test_compile '3 abs' 3
test_compile '3 9 - abs' 6
test_compile '-3 abs' 3
# sqrt
test_compile '9 sqrt' 3
test_compile '81 sqrt sqrt' 3
test_compile '81 sqrt sqrt sqrt' 1.73205
# circles
test_compile '1 sin' 0.841471
test_compile '1 cos' 0.540302
test_compile '1 tan' 1.55741
# swap
test_compile '3 5 -' -2
test_compile '3 5 swap -' 2
# dup
test_compile '3 sqrt dup *' 3
test_compile '3 dup ^' 27
exit 0