Debian Codegraph @

pypy/module/pypyjit/test/randomized.py

raw symbols SCIP
1 #!/usr/bin/python
2
3 import random, inspect, os
4
5 class RandomCode(object):
6
7 maxifdepth = 10
8 maxopdepth = 20
9
10 def __init__(self):
11 self.vars = set()
12
13 def sample(self, population):
14 return random.sample(population, 1)[0]
15
16 def chose(self, *args):
17 return self.sample(args)()
18
19 def expression(self):
20 if len(self.vars) == 0:
21 return self.constant()
22 elif self.depth() > self.maxopdepth:
23 return self.chose(self.variable, self.constant)
24 else:
25 return self.chose(self.variable, self.opperation, self.constant)
26
27 def variable(self):
28 return self.sample(self.vars)
29
30 def opperation(self):
31 return self.expression() + ' ' + self.sample(self.opperators) + \
32 ' ' + self.expression()
33
34 def test(self):
35 tst = self.sample(self.tests)
36 if tst:
37 return self.expression() + ' ' + tst + \
38 ' ' + self.expression()
39 else:
40 return self.expression()
41
42 def constant(self):
43 return str(self.sample(self.constants))
44
45 def depth(self):
46 return len(inspect.getouterframes(inspect.currentframe()))
47
48 def statement(self):
49 if self.depth() > self.maxifdepth:
50 return self.assignment()
51 else:
52 return self.chose(self.assignment, self.ifstatement)
53
54 def assignment(self):
55 v = self.sample(self.varnames)
56 s = v + ' = ' + self.expression() + '\n'
57 self.vars.add(v)
58 return s
59
60 def indent(self, s):
61 lines = s.split('\n')
62 lines = [' ' + l for l in lines[:-1]]
63 return '\n'.join(lines) + '\n'
64
65 def ifstatement(self):
66 return 'if ' + self.test() + ':\n' + self.indent(self.block(5))
67
68 def block(self, n):
69 s = ''
70 for i in range(random.randrange(1,n)):
71 s += self.statement()
72 return s
73
74 def whileloop(self):
75 self.vars.add('i')
76 return 'i = 0\nwhile i < 10:\n' + \
77 self.indent(self.block(5) + 'i += 1\n')
78
79 def setupvars(self):
80 return ', '.join(self.vars) + ' = ' + \
81 ', '.join('0' * len(self.vars)) + '\n'
82
83 def return_statement(self):
84 return 'return (' + ', '.join(self.vars) + ')\n'
85
86
87 class IntBounds(RandomCode):
88 opperators = ('+', '-', '*', '/', '>>', '<<')
89 tests = ('<', '>', '<=', '>=', '==', '!=', None)
90 constants = range(-3,4)
91 varnames = 'abcd'
92
93 def function(self, name='f'):
94 body = self.block(3) + self.whileloop() + self.return_statement()
95 body = self.setupvars() + body
96 return 'def %s():\n' % name + self.indent(body)
97
98
99 def run(python, code):
100 (s,r,e) = os.popen3(python)
101 s.write(code)
102 s.close()
103 res = r.read()
104 err = e.read()
105 r.close()
106 return res, err
107
108 fcnt = 0
109 while True:
110 code = '''
111 try: # make the file runnable by CPython
112 import pypyjit
113 pypyjit.set_param(threshold=3)
114 except ImportError:
115 pass
116
117 %s
118 print f()
119 ''' % IntBounds().function('f')
120
121 r1,e1 = run('/usr/bin/python', code)
122 r2,e2 = run('../../../translator/goal/pypy-c', code)
123 if r1 != r2:
124 rapport = '******************** FAILED ******************\n' + \
125 code + "\n" + \
126 'cpython: %s %s\n' % (r1, e1) + \
127 'pypy: %s %s\n' % (r2, e2)
128 fcnt += 1
129 f = open('failures/%d' % fcnt, "w")
130 f.write(rapport)
131 f.close()
132 print rapport
133
134