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

« back to all changes in this revision

Viewing changes to pypy/translator/js/test/test_class.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 __future__ import division
 
2
import py
 
3
 
 
4
from pypy.translator.js.test.runtest import compile_function
 
5
from pypy.translator.llvm.test import llvmsnippet
 
6
 
 
7
class TestClass(object):
 
8
    def test_classsimple(self):
 
9
        f = compile_function(llvmsnippet.class_simple, [])
 
10
        assert f() == 14
 
11
 
 
12
    def test_classsimple1(self):
 
13
        f = compile_function(llvmsnippet.class_simple1, [int])
 
14
        assert f(2) == 10
 
15
 
 
16
    def test_id_int(self):
 
17
        #py.test.skip("Full list support not implemented")
 
18
        f = compile_function(llvmsnippet.id_int, [int])
 
19
        for i in range(1, 20):
 
20
            assert f(i) == i
 
21
 
 
22
    def test_classsimple2(self):
 
23
        f = compile_function(llvmsnippet.class_simple2, [int])
 
24
        assert f(2) == 10
 
25
 
 
26
    def test_inherit1(self):
 
27
        f = compile_function(llvmsnippet.class_inherit1, [])
 
28
        assert f() == 11
 
29
 
 
30
    def test_inherit2(self):
 
31
        #py.test.skip("Inheritance not implemented")
 
32
        #py.test.skip("issue 'null' for Ptr's? or recurse into Ptr.TO?) see: opwriter.py")
 
33
        f = compile_function(llvmsnippet.class_inherit2, [])
 
34
        assert f() == 1
 
35
 
 
36
    def test_method_of_base_class(self):
 
37
        #py.test.skip("Inheritance not implemented")
 
38
        #py.test.skip("issue 'null' for Ptr's? or recurse into Ptr.TO?) see: opwriter.py")
 
39
        f = compile_function(llvmsnippet.method_of_base_class, [])
 
40
        assert f() == 14
 
41
 
 
42
    def test_attribute_from_base_class(self):
 
43
        #py.test.skip("Inheritance not implemented")
 
44
        f = compile_function(llvmsnippet.attribute_from_base_class, [])
 
45
        assert f() == 4
 
46
 
 
47
    def test_direct_call_of_virtual_method(self):
 
48
        #py.test.skip("Inheritance not implemented")
 
49
        #py.test.skip("issue 'null' for Ptr's? or recurse into Ptr.TO?) see: opwriter.py")
 
50
        f = compile_function(llvmsnippet.direct_call_of_virtual_method, [])
 
51
        assert f() == 14
 
52
 
 
53
    def test_flow_type(self):
 
54
        #py.test.skip("isinstanceof not implemented")
 
55
        f = compile_function(llvmsnippet.flow_type, [])
 
56
        assert f() == 16
 
57
 
 
58
    def test_merge_class(self):
 
59
        #py.test.skip("Inheritance not implemented")
 
60
        f = compile_function(llvmsnippet.merge_classes, [bool])
 
61
        assert f(True) == 1
 
62
        assert f(False) == 2
 
63
 
 
64
    def test_attribute_instance(self):
 
65
        #py.test.skip("Inheritance not implemented")
 
66
        f = compile_function(llvmsnippet.attribute_instance, [bool])
 
67
        assert f(True) == 1
 
68
        assert f(False) == 2
 
69
 
 
70
    def test_global_instance(self): #issue we restart every test with a fresh set of globals
 
71
        f = compile_function(llvmsnippet.global_instance, [int])
 
72
        assert f(-1) == llvmsnippet.global_instance(-1)
 
73
        for i in range(20):
 
74
            x = f(i)
 
75
            y = llvmsnippet.global_instance(i)
 
76
            assert x == y
 
77
 
 
78
    def test_getset(self):
 
79
        #py.test.skip("Basic arguments of const objects not implemented")
 
80
        f = compile_function(llvmsnippet.testgetset, [int])
 
81
        assert f(15) == 25
 
82
 
 
83
    def test_call_degrading_func(self):
 
84
        #py.test.skip("isinstanceof not implemented")
 
85
        f = compile_function(llvmsnippet.call_degrading_func, [bool])
 
86
        assert f(True) == llvmsnippet.call_degrading_func(True)
 
87
        assert f(False) == llvmsnippet.call_degrading_func(False)
 
88
    
 
89
    def test_circular_classdef(self):
 
90
        #py.test.skip("Problems with constant names")
 
91
        #py.test.skip("Inheritance not implemented")
 
92
        f = compile_function(llvmsnippet.circular_classdef, [])
 
93
        assert f() == 10
 
94
 
 
95
class A(object):
 
96
    def __init__(self, a):
 
97
        self.a = a
 
98
        if a is None:
 
99
            self.b = 0
 
100
        else:
 
101
            self.b = a.b + 3
 
102
 
 
103
a = A(None)
 
104
 
 
105
def test_circular_class():
 
106
    def circular_class():
 
107
        b = A(a)
 
108
        return b.b
 
109
    
 
110
    fn = compile_function(circular_class, [])
 
111
    assert fn() == 3
 
112
    
 
113
INIT_VAL = 0
 
114
 
 
115
class B(object):
 
116
    def __init__(self):
 
117
        self.a = [INIT_VAL] * 30
 
118
 
 
119
b = B()
 
120
 
 
121
def test_init_list():
 
122
    def init_list(i):
 
123
        if i == 8:
 
124
            b.a = [1,1,1]
 
125
        return b.a[2]
 
126
    
 
127
    fn = compile_function(init_list, [int])
 
128
    assert fn(3) == INIT_VAL
 
129
    assert fn(8) == 1
 
130
 
 
131
class C(object):
 
132
    pass
 
133
 
 
134
def test_instance_str():
 
135
    def instance_str():
 
136
        return str(C())
 
137
    
 
138
    fn = compile_function(instance_str, [])
 
139
    assert fn() == '<pypy_translator_js_test_test_class_C instance>'
 
140
 
 
141
def test_instance_ret():
 
142
    def instance_ret():
 
143
        return str(C())
 
144
    
 
145
    fn = compile_function(instance_ret, [])
 
146
    assert fn() == '<pypy_translator_js_test_test_class_C instance>'