-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_codegen2.py
More file actions
396 lines (330 loc) · 10.5 KB
/
test_codegen2.py
File metadata and controls
396 lines (330 loc) · 10.5 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import pickle
import re
import keyword
from io import BytesIO
import token
from tokenize import tokenize # , open
from importlib import import_module
import sys
import random
import csv
import copy
import collections
import builtins
import pandas as pd
import warnings
warnings.simplefilter('ignore')
class P(object):
def __init__(self):
self.A = 1
self.B = 0
pattern = re.compile(r'[\(, .\+\-\)]')
def tokenize_pycode(code):
try:
ss = []
tokens = tokenize(BytesIO(code.encode('utf-8')).readline)
for toknum, tokval, _, _, _ in tokens:
if toknum != 62 and tokval != '' and tokval != 'utf-8':
ss.append(tokval)
return ss
except:
return pattern.split(code)
class Missing:
def __init__(self, name=''):
self.msg = name
def __str__(self):
return self.msg
def __repr__(self):
return self.msg
def __getattr__(self, name):
return Missing(f'{self.msg}.{name}')
def __call__(self, *args, **kwargs):
if len(kwargs) == 0:
return Missing(f'{self.msg}{args}')
else:
return Missing(f'{self.msg}{args}{dict(**kwargs)}')
# def __getitem__(self, index):
# return self.msg[index]
MUTABLES = {
type([]),
type({}),
type(set()),
type(bytearray()),
type(pd.DataFrame())
}
def copy_mutable(o):
t = type(o)
if t in MUTABLES:
return copy.copy(o)
return o
def is_assignment(line):
tokens = tokenize_pycode(line)
for token in tokens:
if token == '=':
return True
if token == '(' or token == '[':
break
return False
def is_expression(line):
tokens = tokenize_pycode(line)
if len(tokens) > 0 and keyword.iskeyword(tokens[0]):
return False
for token in tokens:
if token == '=':
return False
if token == '(' or token == '[':
break
return True
def shorten_name(name):
return name[:-1] if name[-1].isdigit() else name
def extract_vars(globals, locals):
for name, value in locals.items():
#name = shorten_name(name)
if name not in dir(builtins):
values = globals.get(name, [])
if value not in values:
values.append(value)
globals[name] = values
def read_vars(filename, evars):
globals = {'print': Missing('print')}
with open(filename) as f:
for line in f.readlines():
line = line.strip()
if line.startswith('import ') or line.startswith('from '):
try:
exec(line, None, globals)
except:
print('Error', line)
if is_assignment(line) and 'sys' not in line:
locals = {}
vars = collections.ChainMap(locals, globals)
try:
exec(line, None, vars)
extract_vars(evars, locals)
for name, value in locals.items():
globals[name] = value
except Exception as e:
builtins.print('Error2', line, e)
VALUE = set(['True', 'False', 'None'])
def iskeyword(s):
if s in VALUE or s in dir(builtins):
return True
return keyword.iskeyword(s)
def extract_names(code):
names = []
try:
tokens = tokenize(BytesIO(code.encode('utf-8')).readline)
prev = ''
for toknum, tokval, _, _, _ in tokens:
#print(toknum, tokval, names, code)
if toknum == token.NAME and not iskeyword(tokval) and prev != '.':
names.append(tokval)
if tokval == '=' and prev in names:
names.pop()
prev = tokval
except Exception as e:
print('FIXME', e, code)
finally:
return names
def match(value1, value2):
s1 = str(value1)
s2 = str(value2)
if 'at 0x' in s1 and 'at 0x' in s2:
s1, _, _ = s1.partition('at 0x')
s2, _, _ = s2.partition('at 0x')
return s1 == s2
def compare_vars(vars, vars2):
if len(vars) != len(vars2):
return False
if '_' in vars and '_' in vars2:
return match(vars['_'], vars2['_'])
for key in vars:
if key not in vars2:
return False
if not match(vars[key], vars2[key]):
return False
return True
def iskeyword(s):
if s in VALUE or s in dir(builtins):
return True
return keyword.iskeyword(s)
def modify_code(code):
if is_expression(code):
return '_ = ' + code
return code
modules = {
'sys': Missing('sys'), 'os': Missing('os'),
# 'plt': Missing('plt'), 'sns': Missing('sns'),
}
ERRLOG = set({})
def dump_emsg():
with open('emsg.txt', 'w') as f:
for emsg in ERRLOG:
print(emsg, file=f)
class TestSuite:
def __init__(self, name_values):
self.name_values = name_values
self.tested = 0
self.syntax_errors = 0
self.failed = 0
self.baddata = 0
self.untested = 0
self.tested_ok = 0
self.missing = 0
def test_code(self, code, code2, check_exact_match=True):
self.tested += 1
code = modify_code(code)
code2 = modify_code(code2)
self.epoch_succ = 0
self.epoch_refok = 0
self.epoch_missing = False
for _ in range(10):
if self.try_test_code(code, code2) == False:
if self.epoch_missing:
self.missing += 1
return
if self.epoch_succ > 3 and self.epoch_refok > 2:
break
if self.epoch_missing:
self.missing += 1
if self.epoch_succ > 3 and self.epoch_refok > 2:
self.tested_ok += 1
return
if self.epoch_refok == 0:
self.baddata += 1
self.untested += 1
print(f'untested({self.epoch_succ})', code)
def try_test_code(self, code, code2):
globals = {
'print': Missing('print'),
'input': Missing('input'),
'list': lambda x: [x],
'open': Missing('open'),
}
locals = {}
globals, globals2, useMissing = self.choose_vars(code, globals)
globals.update(modules)
vars = collections.ChainMap(locals, globals)
refFailed = False
try:
exec(code, None, vars)
self.epoch_refok += 1
except SyntaxError as e:
ERRLOG.add(f'{type(e).__name__}: {e}')
self.untested += 1
return False
except Exception as e:
locals['_'] = e
ERRLOG.add(f'{type(e).__name__}: {e}')
refFailed = True
if useMissing:
self.epoch_missing = True
locals2 = {}
globals2.update(modules)
vars2 = collections.ChainMap(locals2, globals2)
try:
exec(code2, None, vars2)
except SyntaxError:
self.syntax_errors += 1
return False
except Exception as e:
locals2['_'] = e
# if not refFailed:
# print('FAILED', str(e), code2)
if compare_vars(locals, locals2) == False:
self.failed += 1
return False
if useMissing and refFailed:
self.tested_ok += 1
return False
if not refFailed:
self.epoch_succ += 1
return True
def choose_vars(self, code, vars):
names = extract_names(code)
useMissing = False
for name in names:
if name == '_' or name in vars:
continue
if name in self.name_values:
values = self.name_values[name]
random.shuffle(values)
vars[name] = copy_mutable(values[0])
else:
#print('@Missing', name)
useMissing = True
vars[name] = Missing(name)
vars2 = {}
for name, value in vars.items():
vars2[name] = copy_mutable(value)
return vars, vars2, useMissing
def read_tsv(filename, index=2, pred_index=1):
ss = []
try:
with open(filename) as f:
reader = csv.reader(f, delimiter="\t")
for row in reader:
ss.append((row[index], row[pred_index]))
except:
with open(filename) as f:
reader = csv.reader(f, delimiter="\t")
for row in reader:
ss.append((row[1], row[1]))
return ss
# main()
def save_vars(filename, data):
with open(filename, 'wb') as f:
for key, values in data.items():
for value in values:
try:
if isinstance(value, type(sys)):
pickle.dump((0, key, value.__name__), f)
else:
pickle.dump((1, key, value), f)
except Exception as e:
print('PICKLE FAIL', key, value)
def load_vars(filename, vars):
with open(filename, 'rb') as f:
try:
kind, key, value = pickle.load(f)
if kind == 0:
value = import_module(value)
values = vars.get(key, [])
if value not in values:
values.append(value)
vars[key] = values
except EOFError:
pass
def main():
vars = {}
tsvfile = None
for file in sys.argv[1:]:
if file.endswith('.py'):
read_vars(file, vars)
if file.endswith('.tsv'):
tsvfile = file
if file.endswith('.vars'):
load_vars(file, vars)
save_vars('multiese.vars', vars)
suite = TestSuite(vars)
if tsvfile is not None:
ss = read_tsv(tsvfile)
for code, code2 in ss:
suite.test_code(code, code2)
else:
#suite.test_code('n<0', 'n<0')
suite.test_code(
"print(f'\033[32m{s}\033[0m')", "print(f'\033[32m{s}\033[0m')")
print(f'Test Count {suite.tested}')
print(
f' Syntax Error {suite.syntax_errors} {suite.syntax_errors/suite.tested:.5f}')
print(
f' Pass {suite.tested_ok} {suite.tested_ok/suite.tested:.5f} {suite.tested_ok/(suite.tested-suite.untested):.5f}')
print(f' Failed {suite.failed} {suite.failed/suite.tested:.5f} {suite.tested_ok/(suite.failed-suite.untested):.5f}')
print(f' Untested {suite.untested} {suite.untested/suite.tested:.5f}')
print(f' Bad Data {suite.baddata} {suite.baddata/suite.tested:.5f}')
print(
f' Missing {suite.missing} {suite.missing/suite.tested:.5f}')
main()
dump_emsg()
# print(isinstance(sys, type(sys)))