File: randomized.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (134 lines) | stat: -rw-r--r-- 3,524 bytes parent folder | download | duplicates (9)
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
#!/usr/bin/python

import random, inspect, os

class RandomCode(object):

    maxifdepth = 10
    maxopdepth = 20

    def __init__(self):
        self.vars = set()

    def sample(self, population):
        return random.sample(population, 1)[0]

    def chose(self, *args):
        return self.sample(args)()

    def expression(self):
        if len(self.vars) == 0:
            return self.constant()
        elif self.depth() > self.maxopdepth:
            return self.chose(self.variable, self.constant)
        else:
            return self.chose(self.variable, self.opperation, self.constant)
        
    def variable(self):
        return self.sample(self.vars)

    def opperation(self):
        return self.expression() + ' ' + self.sample(self.opperators) + \
               ' ' + self.expression()

    def test(self):
        tst = self.sample(self.tests)
        if tst:
            return self.expression() + ' ' + tst + \
                   ' ' + self.expression()
        else:
            return self.expression()

    def constant(self):
        return str(self.sample(self.constants))

    def depth(self):
        return len(inspect.getouterframes(inspect.currentframe()))
        
    def statement(self):
        if self.depth() > self.maxifdepth:
            return self.assignment()
        else:
            return self.chose(self.assignment, self.ifstatement)

    def assignment(self):
        v = self.sample(self.varnames)
        s = v + ' = ' + self.expression() + '\n'
        self.vars.add(v)
        return s

    def indent(self, s):
        lines = s.split('\n')
        lines = ['    ' + l for l in lines[:-1]]
        return '\n'.join(lines) + '\n'
    
    def ifstatement(self):
        return 'if ' + self.test() + ':\n' + self.indent(self.block(5))

    def block(self, n):
        s = ''
        for i in range(random.randrange(1,n)):
            s += self.statement()
        return s

    def whileloop(self):
        self.vars.add('i')
        return 'i = 0\nwhile i < 10:\n' + \
               self.indent(self.block(5) + 'i += 1\n')

    def setupvars(self):
        return ', '.join(self.vars) + ' = ' + \
               ', '.join('0' * len(self.vars)) + '\n'

    def return_statement(self):
        return 'return (' + ', '.join(self.vars) + ')\n'
        

class IntBounds(RandomCode):
    opperators = ('+', '-', '*', '/', '>>', '<<')
    tests = ('<', '>', '<=', '>=', '==', '!=', None)
    constants = range(-3,4) 
    varnames = 'abcd'

    def function(self, name='f'):
        body = self.block(3) + self.whileloop() + self.return_statement()
        body = self.setupvars() + body
        return 'def %s():\n' % name + self.indent(body)


def run(python, code):
    (s,r,e) = os.popen3(python)
    s.write(code)
    s.close()
    res = r.read()
    err = e.read()
    r.close()
    return res, err

fcnt = 0
while True:
    code = '''
try: # make the file runnable by CPython
    import pypyjit
    pypyjit.set_param(threshold=3)
except ImportError:
    pass

%s
print f()
''' % IntBounds().function('f')

    r1,e1 = run('/usr/bin/python', code)
    r2,e2 = run('../../../translator/goal/pypy-c', code)
    if r1 != r2:
        rapport = '******************** FAILED ******************\n' + \
                  code + "\n" + \
                  'cpython: %s %s\n' % (r1, e1) + \
                  'pypy: %s %s\n' % (r2, e2)
        fcnt += 1
        f = open('failures/%d' % fcnt, "w")
        f.write(rapport)
        f.close()
        print rapport