-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.cpp
258 lines (201 loc) · 5.04 KB
/
parse_test.cpp
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
#include "lexer.h"
#include "parse.h"
#include "statement.h"
#include "test_runner_p.h"
using namespace std;
namespace parse {
unique_ptr<ast::Statement> ParseProgramFromString(const string& program) {
istringstream is(program);
parse::Lexer lexer(is);
return ParseProgram(lexer);
}
void TestSimpleProgram() {
const string program = R"(
x = 4
y = 5
z = "hello, "
n = "world"
print x + y, z + n
)"s;
runtime::DummyContext context;
runtime::Closure closure;
auto tree = ParseProgramFromString(program);
tree->Execute(closure, context);
ASSERT_EQUAL(context.output.str(), "9 hello, world\n"s);
}
void TestProgramWithClasses() {
const string program = R"(
program_name = "Classes test"
class Empty:
def __init__():
x = 0
class Point:
def __init__(x, y):
self.x = x
self.y = y
def SetX(value):
self.x = value
def SetY(value):
self.y = value
def __str__():
return '(' + str(self.x) + '; ' + str(self.y) + ')'
origin = Empty()
origin = Point(0, 0)
far_far_away = Point(10000, 50000)
print program_name, origin, far_far_away, origin.SetX(1)
)"s;
runtime::DummyContext context;
runtime::Closure closure;
auto tree = ParseProgramFromString(program);
tree->Execute(closure, context);
ASSERT_EQUAL(context.output.str(), "Classes test (0; 0) (10000; 50000) None\n"s);
}
void TestProgramWithIf() {
const string program = R"(
x = 4
y = 5
if x > y:
print "x > y"
else:
print "x <= y"
if x > 0:
if y < 0:
print "y < 0"
else:
else:
print "y >= 0"
print 'x <= 0'
)"s;
runtime::DummyContext context;
runtime::Closure closure;
auto tree = ParseProgramFromString(program);
tree->Execute(closure, context);
ASSERT_EQUAL(context.output.str(), "x <= y\ny >= 0\n"s);
}
void TestReturnFromIf() {
const string program = R"(
class Abs:
def calc(n):
if n > 0:
return n
else:
return -n
x = Abs()
print x.calc(2)
)"s;
runtime::DummyContext context;
runtime::Closure closure;
auto tree = ParseProgramFromString(program);
tree->Execute(closure, context);
ASSERT_EQUAL(context.output.str(), "2\n"s);
}
void TestRecursion() {
const string program = R"(
class ArithmeticProgression:
def calc(n):
self.result = 0
self.calc_impl(n)
def calc_impl(n):
value = n
if value > 0:
self.result = self.result + value
self.calc_impl(value - 1)
x = ArithmeticProgression()
x.calc(10)
print x.result
)"s;
runtime::DummyContext context;
runtime::Closure closure;
auto tree = ParseProgramFromString(program);
tree->Execute(closure, context);
ASSERT_EQUAL(context.output.str(), "55\n"s);
}
void TestRecursion2() {
const string program = R"(
class GCD:
def __init__():
self.call_count = 0
def calc(a, b):
self.call_count = self.call_count + 1
if a < b:
return self.calc(b, a)
if b == 0:
return a
return self.calc(a - b, b)
x = GCD()
print x.calc(510510, 18629977)
print x.calc(22, 17)
print x.call_count
)"s;
runtime::DummyContext context;
runtime::Closure closure;
auto tree = ParseProgramFromString(program);
tree->Execute(closure, context);
ASSERT_EQUAL(context.output.str(), "17\n1\n115\n"s);
}
void TestComplexLogicalExpression() {
const string program = R"(
a = 1
b = 2
c = 3
ok = a + b > c and a + c > b and b + c > a
print ok
)"s;
runtime::DummyContext context;
runtime::Closure closure;
auto tree = ParseProgramFromString(program);
tree->Execute(closure, context);
ASSERT_EQUAL(context.output.str(), "False\n"s);
}
void TestClassicalPolymorphism() {
const string program = R"(
class Shape:
def __str__():
return "Shape"
class Rect(Shape):
def __init__(w, h):
self.w = w
self.h = h
def __str__():
return "Rect(" + str(self.w) + 'x' + str(self.h) + ')'
class Circle(Shape):
def __init__(r):
self.r = r
def __str__():
return 'Circle(' + str(self.r) + ')'
class Triangle(Shape):
def __init__(a, b, c):
self.ok = a + b > c and a + c > b and b + c > a
if (self.ok):
self.a = a
self.b = b
self.c = c
def __str__():
if self.ok:
return 'Triangle(' + str(self.a) + ', ' + str(self.b) + ', ' + str(self.c) + ')'
else:
return 'Wrong triangle'
r = Rect(10, 20)
c = Circle(52)
t1 = Triangle(3, 4, 5)
t2 = Triangle(125, 1, 2)
print r, c, t1, t2
)"s;
runtime::DummyContext context;
runtime::Closure closure;
auto tree = ParseProgramFromString(program);
tree->Execute(closure, context);
ASSERT_EQUAL(context.output.str(),
"Rect(10x20) Circle(52) Triangle(3, 4, 5) Wrong triangle\n"s);
}
} // namespace parse
void TestParseProgram(TestRunner& tr) {
RUN_TEST(tr, parse::TestSimpleProgram);
RUN_TEST(tr, parse::TestProgramWithClasses);
RUN_TEST(tr, parse::TestProgramWithIf);
RUN_TEST(tr, parse::TestReturnFromIf);
RUN_TEST(tr, parse::TestRecursion);
RUN_TEST(tr, parse::TestRecursion2);
RUN_TEST(tr, parse::TestComplexLogicalExpression);
RUN_TEST(tr, parse::TestClassicalPolymorphism);
}