-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter_tests.cpp
More file actions
324 lines (224 loc) · 6.77 KB
/
interpreter_tests.cpp
File metadata and controls
324 lines (224 loc) · 6.77 KB
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
317
318
319
320
321
322
323
324
#include "catch.hpp"
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include "semantic_error.hpp"
#include "interpreter.hpp"
#include "expression.hpp"
Expression run(const std::string & program){
std::istringstream iss(program);
Interpreter interp;
bool ok = interp.parseStream(iss);
if(!ok){
std::cerr << "Failed to parse: " << program << std::endl;
}
REQUIRE(ok == true);
Expression result;
REQUIRE_NOTHROW(result = interp.evaluate());
return result;
}
TEST_CASE( "Test Interpreter parser with expected input", "[interpreter]" ) {
std::string program = "(begin (define r 10) (* pi (* r r)))";
std::istringstream iss(program);
Interpreter interp;
bool ok = interp.parseStream(iss);
REQUIRE(ok == true);
}
TEST_CASE( "Test Interpreter parser with numerical literals", "[interpreter]" ) {
std::vector<std::string> programs = {"(1)", "(+1)", "(+1e+0)", "(1e-0)"};
for(auto program : programs){
std::istringstream iss(program);
Interpreter interp;
bool ok = interp.parseStream(iss);
REQUIRE(ok == true);
}
{
std::istringstream iss("(define x 1abc)");
Interpreter interp;
bool ok = interp.parseStream(iss);
REQUIRE(ok == false);
}
}
TEST_CASE( "Test Interpreter parser with truncated input", "[interpreter]" ) {
{
std::string program = "(f";
std::istringstream iss(program);
Interpreter interp;
bool ok = interp.parseStream(iss);
REQUIRE(ok == false);
}
{
std::string program = "(begin (define r 10) (* pi (* r r";
std::istringstream iss(program);
Interpreter interp;
bool ok = interp.parseStream(iss);
REQUIRE(ok == false);
}
}
TEST_CASE( "Test Interpreter parser with extra input", "[interpreter]" ) {
std::string program = "(begin (define r 10) (* pi (* r r))) )";
std::istringstream iss(program);
Interpreter interp;
bool ok = interp.parseStream(iss);
REQUIRE(ok == false);
}
TEST_CASE( "Test Interpreter parser with single non-keyword", "[interpreter]" ) {
std::string program = "hello";
std::istringstream iss(program);
Interpreter interp;
bool ok = interp.parseStream(iss);
REQUIRE(ok == false);
}
TEST_CASE( "Test Interpreter parser with empty input", "[interpreter]" ) {
std::string program;
std::istringstream iss(program);
Interpreter interp;
bool ok = interp.parseStream(iss);
REQUIRE(ok == false);
}
TEST_CASE( "Test Interpreter parser with empty expression", "[interpreter]" ) {
std::string program = "( )";
std::istringstream iss(program);
Interpreter interp;
bool ok = interp.parseStream(iss);
REQUIRE(ok == false);
}
TEST_CASE( "Test Interpreter parser with bad number string", "[interpreter]" ) {
std::string program = "(1abc)";
std::istringstream iss(program);
Interpreter interp;
bool ok = interp.parseStream(iss);
REQUIRE(ok == false);
}
TEST_CASE( "Test Interpreter parser with incorrect input. Regression Test", "[interpreter]" ) {
std::string program = "(+ 1 2) (+ 3 4)";
std::istringstream iss(program);
Interpreter interp;
bool ok = interp.parseStream(iss);
REQUIRE(ok == false);
}
TEST_CASE( "Test Interpreter result with literal expressions", "[interpreter]" ) {
{ // Number
std::string program = "(4)";
Expression result = run(program);
REQUIRE(result == Expression(4.));
}
{ // Symbol
std::string program = "(pi)";
Expression result = run(program);
REQUIRE(result == Expression(atan2(0, -1)));
}
}
TEST_CASE( "Test Interpreter result with simple procedures (add)", "[interpreter]" ) {
{ // add, binary case
std::string program = "(+ 1 2)";
INFO(program);
Expression result = run(program);
REQUIRE(result == Expression(3.));
}
{ // add, 3-ary case
std::string program = "(+ 1 2 3)";
INFO(program);
Expression result = run(program);
REQUIRE(result == Expression(6.));
}
{ // add, 6-ary case
std::string program = "(+ 1 2 3 4 5 6)";
INFO(program);
Expression result = run(program);
REQUIRE(result == Expression(21.));
}
}
TEST_CASE( "Test Interpreter special forms: begin and define", "[interpreter]" ) {
{
std::string program = "(define answer 42)";
INFO(program);
Expression result = run(program);
REQUIRE(result == Expression(42.));
}
{
std::string program = "(begin (define answer 42)\n(answer))";
INFO(program);
Expression result = run(program);
REQUIRE(result == Expression(42.));
}
{
std::string program = "(begin (define answer (+ 9 11)) (answer))";
INFO(program);
Expression result = run(program);
REQUIRE(result == Expression(20.));
}
{
std::string program = "(begin (define a 1) (define b 1) (+ a b))";
INFO(program);
Expression result = run(program);
REQUIRE(result == Expression(2.));
}
}
TEST_CASE( "Test a medium-sized expression", "[interpreter]" ) {
{
std::string program = "(+ (+ 10 1) (+ 30 (+ 1 1)))";
Expression result = run(program);
REQUIRE(result == Expression(43.));
}
}
TEST_CASE( "Test arithmetic procedures", "[interpreter]" ) {
{
std::vector<std::string> programs = {"(+ 1 -2)",
"(+ -3 1 1)",
"(- 1)",
"(- 1 2)",
"(* 1 -1)",
"(* 1 1 -1)",
"(/ -1 1)",
"(/ 1 -1)"};
for(auto s : programs){
Expression result = run(s);
REQUIRE(result == Expression(-1.));
}
}
}
TEST_CASE( "Test some semantically invalid expresions", "[interpreter]" ) {
std::vector<std::string> programs = {"(@ none)", // so such procedure
"(- 1 1 2)", // too many arguments
"(define begin 1)", // redefine special form
"(define pi 3.14)"}; // redefine builtin symbol
for(auto s : programs){
Interpreter interp;
std::istringstream iss(s);
bool ok = interp.parseStream(iss);
REQUIRE(ok == true);
REQUIRE_THROWS_AS(interp.evaluate(), SemanticError);
}
}
TEST_CASE( "Test for exceptions from semantically incorrect input", "[interpreter]" ) {
std::string input = R"(
(+ 1 a)
)";
Interpreter interp;
std::istringstream iss(input);
bool ok = interp.parseStream(iss);
REQUIRE(ok == true);
REQUIRE_THROWS_AS(interp.evaluate(), SemanticError);
}
TEST_CASE( "Test malformed define", "[interpreter]" ) {
std::string input = R"(
(define a 1 2)
)";
Interpreter interp;
std::istringstream iss(input);
bool ok = interp.parseStream(iss);
REQUIRE(ok == true);
REQUIRE_THROWS_AS(interp.evaluate(), SemanticError);
}
TEST_CASE( "Test using number as procedure", "[interpreter]" ) {
std::string input = R"(
(1 2 3)
)";
Interpreter interp;
std::istringstream iss(input);
bool ok = interp.parseStream(iss);
REQUIRE(ok == true);
REQUIRE_THROWS_AS(interp.evaluate(), SemanticError);
}