-
Notifications
You must be signed in to change notification settings - Fork 43
/
test_cpp.py
270 lines (204 loc) · 6.08 KB
/
test_cpp.py
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
import unittest
import textwrap
from pseudo import generate
from pseudo.pseudo_tree import Node
import suite as suite
#v
class TestCpp(unittest.TestCase, metaclass=suite.TestLanguage): # dark magic bitches
_language = 'cpp'
_import = '#include'
_parse_import = lambda self, line: line[10:-1]
def gen(self, custom_exceptions, ast):
imports, source = self.gen_with_imports(custom_exceptions, ast)
z = source.strip()
if z[-1] == ';':
return z[:-1]
else:
return z
def gen_special(self, source):
lines = source.lstrip().split('\n')
main_index = lines.index('int main() {')
main = '\n'.join([line[4:] for line in lines[main_index + 1:-1]]).strip()
l = 0
imports = set()
while len(lines) > l and (not lines[l] or lines[l].startswith(self._import)):
if lines[l]:
imports.add(lines[l][9:-1])
l += 1
if len(lines) > l + 1 and lines[l + 1] == 'using namespace std;':
l += 2
definitions = '\n'.join(lines[l:main_index]).lstrip()
if not definitions.strip():
return imports, main
else:
# input(definitions)
# input(definitions + '\n'.join(lines[main_index:]))
return imports, definitions + '\n' + '\n'.join(lines[main_index:])
# make declarative style great again
# expected c++ translation for each example in suite:
int_ = '42'
float_ = '42.42'
string = '"la"'
null = 'NULL'
dictionary = (
{'iostream', 'unordered_map', 'string'},
'unordered_map<string, int>{{"la": 0}};'
)
list_ = (
{'iostream', 'vector', 'string'},
'{"la"};'
)
local = 'egg'
typename = 'Egg'
instance_variable = 'this->egg'
attr = 'e.egg'
local_assignment = 'egg = ham'
instance_assignment = 'this.egg = ham'
attr_assignment = 'T.egg = ham'
call = 'map(x)'
method_call = 'e->filter(42)'
standard_call = [
'cout << 42 << endl',
({'iostream', 'string'}, textwrap.dedent('''\
string _dummy;
cin >> _dummy;''')),
({'math'}, 'log(ham);'),
({'iostream', 'fstream', 'string'}, textwrap.dedent('''\
ifstream ifs("f.py");
string source((istreambuf_iterator<char>(ifs)), (istreambuf_iterator<char>()));'''))
]
standard_method_call = [
'l.size()',
'"l".substr(0, 2)'
]
binary_op = 'ham + egg'
unary_op = '-a'
comparison = 'egg > ham'
if_statement = (
{'iostream', 'vector'},
textwrap.dedent('''\
if (egg == ham) {
vector<string> _sliced(l.begin(), l.begin() + 2);
} else if (egg == ham) {
cout << 4.2 << endl;
} else {
z;
}''')
)
for_statement = [
textwrap.dedent('''\
for(auto a: sequence) {
log(a);
}'''),
textwrap.dedent('''\
for(int j = 0; j != 42; j += 2) {
analyze(j);
}'''),
textwrap.dedent('''\
for(int j = 0; j < z.size(); j ++) {
auto k = z[j];
analyze(j, k);
}'''),
textwrap.dedent('''\
for(auto& _item : z) {
auto j = _item.first;
auto k = _item.second;
analyze(k, j);
}'''),
textwrap.dedent('''\
for(int _index = 0; _index < z.size(); _index ++) {
auto k = z[_index];
auto l = zz[_index];
a(k, l);
}''')
]
while_statement = textwrap.dedent('''\
while (f() >= 42) {
int b = g();
}''')
function_definition = textwrap.dedent('''\
int weird(int z) {
int fixed = fix(z);
return fixed;
}
int main() {
}''')
class_with_method_definition = (
{'iostream', 'vector', 'string'},
textwrap.dedent('''\
class A {
private int ast;
vector<string> parse(string source) {
this->ast = 0;
return {source};
}
}
int main() {
}
'''))
anonymous_function = [
'[](string source) { return ves(source.length()); }',
textwrap.dedent('''\
[](string source) {
cout << source << endl;
return ves(source.length());
}''')
]
class_statement = [textwrap.dedent('''\
class A : B {
public int a;
public:
A(int a) {
this->a = a;
}
int parse() {
return 42;
}''')]
this = 'this'
class_constructor = textwrap.dedent('''\
class A {
private int a;
private int b;
A(int a, int b) {
this->a = a;
this->b = b;
}
}
int main() {
}''')
try_statement = [
({'iostream', 'stdexcept', 'exception'},
textwrap.dedent('''\
try {
a();
h(-4);
}
catch (exception& e) {
cout << e.what() << endl;
}''')),
({'iostream', 'stdexcept', 'exception'},
textwrap.dedent('''\
class NeptunError : runtime_error {
}
int main() {
try {
a();
h(-4);
}
catch (NeptunError& e) {
cout << e.what() << endl;
}
}
'''))
]
index = '"la"[2]'
cpp_new_instance = 'smart_ptr<Z> z = new Z()'
cpp_pointer_method_call = 'z->rave(0)'
throw_statement = ({'iostream', 'stdexcept', 'exception', 'string'},
textwrap.dedent('''\
class NeptunError : runtime_error {
}
int main() {
throw NeptunError("no tea");
}
'''))