~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/jit/timeshifter/test/test_vlist.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from pypy.jit.hintannotator.annotator import HintAnnotatorPolicy
 
2
from pypy.jit.timeshifter.test.test_timeshift import TimeshiftingTests
 
3
from pypy.rlib.objectmodel import hint
 
4
 
 
5
P_OOPSPEC = HintAnnotatorPolicy(novirtualcontainer=True, oopspec=True)
 
6
 
 
7
 
 
8
class TestVList(TimeshiftingTests):
 
9
 
 
10
    def test_vlist(self):
 
11
        def ll_function():
 
12
            lst = []
 
13
            lst.append(12)
 
14
            return lst[0]
 
15
        res = self.timeshift(ll_function, [], [], policy=P_OOPSPEC)
 
16
        assert res == 12
 
17
        self.check_insns({})
 
18
 
 
19
    def test_enter_block(self):
 
20
        def ll_function(flag):
 
21
            lst = []
 
22
            lst.append(flag)
 
23
            lst.append(131)
 
24
            if flag:
 
25
                return lst[0]
 
26
            else:
 
27
                return lst[1]
 
28
        res = self.timeshift(ll_function, [6], [], policy=P_OOPSPEC)
 
29
        assert res == 6
 
30
        self.check_insns({'int_is_true': 1})
 
31
        res = self.timeshift(ll_function, [0], [], policy=P_OOPSPEC)
 
32
        assert res == 131
 
33
        self.check_insns({'int_is_true': 1})
 
34
 
 
35
    def test_merge(self):
 
36
        def ll_function(flag):
 
37
            lst = []
 
38
            if flag:
 
39
                lst.append(flag)
 
40
            else:
 
41
                lst.append(131)
 
42
            return lst[-1]
 
43
        res = self.timeshift(ll_function, [6], [], policy=P_OOPSPEC)
 
44
        assert res == 6
 
45
        self.check_insns({'int_is_true': 1})
 
46
        res = self.timeshift(ll_function, [0], [], policy=P_OOPSPEC)
 
47
        assert res == 131
 
48
        self.check_insns({'int_is_true': 1})
 
49
 
 
50
    def test_replace(self):
 
51
        def ll_function(flag):
 
52
            lst = []
 
53
            if flag:
 
54
                lst.append(12)
 
55
            else:
 
56
                lst.append(131)
 
57
            return lst[-1]
 
58
        res = self.timeshift(ll_function, [6], [], policy=P_OOPSPEC)
 
59
        assert res == 12
 
60
        self.check_insns({'int_is_true': 1})
 
61
        res = self.timeshift(ll_function, [0], [], policy=P_OOPSPEC)
 
62
        assert res == 131
 
63
        self.check_insns({'int_is_true': 1})
 
64
 
 
65
    def test_force(self):
 
66
        def ll_function(n):
 
67
            lst = []
 
68
            lst.append(n)
 
69
            if n:
 
70
                lst.append(12)
 
71
            return lst[-1]
 
72
        res = self.timeshift(ll_function, [6], [], policy=P_OOPSPEC)
 
73
        assert res == 12
 
74
        res = self.timeshift(ll_function, [0], [], policy=P_OOPSPEC)
 
75
        assert res == 0
 
76
 
 
77
    def test_oop_vlist(self):
 
78
        def ll_function():
 
79
            lst = [3, 5]
 
80
            five = lst.pop()        # [3]
 
81
            lst.append(len(lst))    # [3, 1]
 
82
            lst2 = list(lst)
 
83
            three = lst.pop(0)      # [1]
 
84
            lst.insert(0, 8)        # [8, 1]
 
85
            lst.insert(2, 7)        # [8, 1, 7]
 
86
            lst.append(not lst)     # [8, 1, 7, 0]
 
87
            lst.reverse()           # [0, 7, 1, 8]
 
88
            lst3 = lst2 + lst       # [3, 1, 0, 7, 1, 8]
 
89
            del lst3[1]             # [3, 0, 7, 1, 8]
 
90
            seven = lst3.pop(2)     # [3, 0, 1, 8]
 
91
            lst3[0] = 9             # [9, 0, 1, 8]
 
92
            nine = lst3.pop(-4)     # [0, 1, 8]
 
93
            return (len(lst3) * 10000000 +
 
94
                    lst3[0]   *  1000000 +
 
95
                    lst3[1]   *   100000 +
 
96
                    lst3[-1]  *    10000 +
 
97
                    five      *     1000 +
 
98
                    three     *      100 +
 
99
                    seven     *       10 +
 
100
                    nine      *        1)
 
101
        assert ll_function() == 30185379
 
102
        res = self.timeshift(ll_function, [], [], policy=P_OOPSPEC)
 
103
        assert res == 30185379
 
104
        self.check_insns({})
 
105
 
 
106
    def test_alloc_and_set(self):
 
107
        def ll_function():
 
108
            lst = [0] * 9
 
109
            return len(lst)
 
110
        res = self.timeshift(ll_function, [], [], policy=P_OOPSPEC)
 
111
        assert res == 9
 
112
        self.check_insns({})
 
113
        
 
114
    def test_lists_deepfreeze(self):
 
115
        l1 = [1,2,3,4,5]
 
116
        l2 = [6,7,8,9,10]
 
117
        def getlist(n):
 
118
            if n:
 
119
                return l1
 
120
            else:
 
121
                return l2
 
122
        def ll_function(n, i):
 
123
            l = getlist(n)
 
124
            l = hint(l, deepfreeze=True)
 
125
            res = l[i]
 
126
            res = hint(res, variable=True)
 
127
            return res
 
128
        
 
129
        res = self.timeshift(ll_function, [3, 4], [0, 1], policy=P_OOPSPEC)
 
130
        assert res == 5
 
131
        self.check_insns({})
 
132
 
 
133
    def test_frozen_list(self):
 
134
        lst = [5, 7, 9]
 
135
        def ll_function(x):
 
136
            mylist = hint(lst, deepfreeze=True)
 
137
            z = mylist[x]
 
138
            hint(z, concrete=True)
 
139
            return z
 
140
 
 
141
        res = self.timeshift(ll_function, [1], policy=P_OOPSPEC)
 
142
        assert res == 7
 
143
        self.check_insns({})
 
144
 
 
145
    def test_frozen_list_indexerror(self):
 
146
        lst = [5, 7, 9]
 
147
        def ll_function(x):
 
148
            mylist = hint(lst, deepfreeze=True)
 
149
            try:
 
150
                z = mylist[x]
 
151
            except IndexError:
 
152
                return -42
 
153
            hint(z, concrete=True)
 
154
            return z
 
155
 
 
156
        res = self.timeshift(ll_function, [4], policy=P_OOPSPEC)
 
157
        assert res == -42
 
158
        self.check_insns({})
 
159
 
 
160
    def test_bogus_index_while_compiling(self):
 
161
        class Y:
 
162
            pass
 
163
 
 
164
        def g(lst, y, n):
 
165
            lst = hint(lst, deepfreeze=True)
 
166
            if y.flag:
 
167
                return lst[n]
 
168
            else:
 
169
                return -7
 
170
 
 
171
        y = Y()
 
172
        lst1 = [3, 4, 5]
 
173
        lst2 = [6, 2]
 
174
 
 
175
        def h(i):
 
176
            if i == 1: return lst1
 
177
            elif i == 2: return lst2
 
178
            else: return []
 
179
 
 
180
        def f(n):
 
181
            y.flag = n < 3
 
182
            g(h(1), y, n)
 
183
            y.flag = n < 2
 
184
            return g(h(2), y, n)
 
185
 
 
186
        res = self.timeshift(f, [2], [0], policy=P_OOPSPEC)
 
187
        assert res == -7