-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
163 lines (125 loc) · 3.07 KB
/
main.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
#include "lexer.h"
#include "parse.h"
#include "runtime.h"
#include "statement.h"
#include "tests/test_runner_p.h"
#include <iostream>
using namespace std;
namespace parse {
void RunOpenLexerTests(TestRunner& tr);
} // namespace parse
namespace ast {
void RunUnitTests(TestRunner& tr);
}
namespace runtime {
void RunObjectHolderTests(TestRunner& tr);
void RunObjectsTests(TestRunner& tr);
} // namespace runtime
void TestParseProgram(TestRunner& tr);
namespace {
void RunMythonProgram(istream& input, ostream& output) {
parse::Lexer lexer(input);
auto program = ParseProgram(lexer);
runtime::SimpleContext context{output};
runtime::Closure closure;
program->Execute(closure, context);
}
void TestSimplePrints() {
istringstream input(R"(
print 57
print 10, 24, -8
print 'hello'
print "world"
print True, False
print
print None
)");
ostringstream output;
RunMythonProgram(input, output);
ASSERT_EQUAL(output.str(), "57\n10 24 -8\nhello\nworld\nTrue False\n\nNone\n");
}
void TestAssignments() {
istringstream input(R"(
x = 57
print x
x = 'C++ black belt'
print x
y = False
x = y
print x
x = None
print x, y
)");
ostringstream output;
RunMythonProgram(input, output);
ASSERT_EQUAL(output.str(), "57\nC++ black belt\nFalse\nNone False\n");
}
void TestArithmetics() {
istringstream input("print 1+2+3+4+5, 1*2*3*4*5, 1-2-3-4-5, 36/4/3, 2*5+10/2");
ostringstream output;
RunMythonProgram(input, output);
ASSERT_EQUAL(output.str(), "15 120 -13 3 15\n");
}
void TestVariablesArePointers() {
istringstream input(R"(
class Counter:
def __init__():
self.value = 0
def add():
self.value = self.value + 1
class Dummy:
def do_add(counter):
counter.add()
x = Counter()
y = x
x.add()
y.add()
print x.value
d = Dummy()
d.do_add(x)
print y.value
)");
ostringstream output;
RunMythonProgram(input, output);
ASSERT_EQUAL(output.str(), "2\n3\n");
}
void TestWithSelf() {
istringstream input(R"(
class X:
def __init__(p):
p.a = self
class XHolder:
def __init__():
dummy = 0
xh = XHolder()
x = X(xh)
print xh.a
)");
ostringstream output;
RunMythonProgram(input, output);
//ASSERT_EQUAL(output.str(), "57\nC++ black belt\nFalse\nNone False\n");
}
void TestAll() {
TestRunner tr;
parse::RunOpenLexerTests(tr);
runtime::RunObjectHolderTests(tr);
runtime::RunObjectsTests(tr);
ast::RunUnitTests(tr);
TestParseProgram(tr);
RUN_TEST(tr, TestSimplePrints);
RUN_TEST(tr, TestAssignments);
RUN_TEST(tr, TestArithmetics);
RUN_TEST(tr, TestVariablesArePointers);
RUN_TEST(tr, TestWithSelf);
}
} // namespace
int main() {
try {
TestAll();
//RunMythonProgram(cin, cout);
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}