test_misc.py :  » Development » Psyco » psyco-dist » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Psyco 
Psyco » psyco dist » test » test_misc.py
import py, sys, psyco
import math


def test_index():
    if sys.version_info < (2, 5):
        py.test.skip("for Python 2.5")

    class X(object):
        def __index__(self):
            return -3

    def f(x):
        return (12, 23, 34, 45)[x]

    res = psyco.proxy(f)(X())
    assert res == 23


def test_index_slice():
    if sys.version_info < (2, 5):
        py.test.skip("for Python 2.5")

    class Observer(object):
        def __getslice__(self, i, j):
            return "slice", i, j
        def __getitem__(self, x):
            raise Exception("in __getitem__")
    class X(object):
        def __index__(self):
            return 5
    class Y(object):
        def __index__(self):
            return -2

    def f(o, x, y):
        return o[x:y]

    res = psyco.proxy(f)(Observer(), X(), Y())
    assert res == ("slice", 5, -2)

def test_index_repeat():
    if sys.version_info < (2, 5):
        py.test.skip("for Python 2.5")

    class X(object):
        def __index__(self):
            return 3

    def f(x):
        return (12, 23) * x

    res = psyco.proxy(f)(X())
    assert res == (12, 23, 12, 23, 12, 23)

def test_slice_overflow():
    class X(object):
        def __len__(self):
            return sys.maxint
        def __getslice__(self, i, j):
            return i, j
    def f(X):
        return X()[-(2**100):2**100]

    res = psyco.proxy(f)(X)
    assert res == f(X)

def test_id():
    def f(x):
        return id(x)
    obj = []
    res = psyco.proxy(f)(obj)
    assert res == id(obj)

def test_math_module():
    import math
    global _operation
    for name in ["acos", "asin", "atan", "ceil", "cos", "cosh", "exp",
                 "fabs", "floor", "sin", "sinh", "sqrt", "tan", "tanh"]:
        _operation = getattr(math, name)

        def tester(a):
            return _operation(a)
        res = psyco.proxy(tester)(0.17)
        assert abs(res - _operation(0.17)) < 1e6

        def tester(a):
            return _operation(*a)
        res = psyco.proxy(tester)((0.71,))
        assert abs(res - _operation(0.71)) < 1e6

    for name in ["atan2", "fmod", "hypot", "pow"]:
        _operation = getattr(math, name)

        def tester(a, b):
            return _operation(a, b)
        res = psyco.proxy(tester)(0.17, 0.081)
        assert abs(res - _operation(0.17, 0.081)) < 1e6

        def tester(a):
            return _operation(*a)
        res = psyco.proxy(tester)((0.71, 0.2643))
        assert abs(res - _operation(0.71, 0.2643)) < 1e6

def test_circular_list():
    def f():
        l = [None]
        l[0] = l
        return l
    lst = psyco.proxy(f)()
    assert type(lst) is list
    assert len(lst) == 1
    assert lst[0] is lst

    def g():
        a = [None]
        b = [None]
        a[0] = b
        b[0] = a
        return b
    lst = psyco.proxy(g)()
    assert type(lst) is list
    assert len(lst) == 1
    assert lst[0] is not lst
    assert type(lst[0]) is list
    assert len(lst[0]) == 1
    assert lst[0][0] is lst

def test_big_dict_constructor():
    def f(x):
        return {1: 2, 3: 4, x: 6, 7: 8, 9: x,
                'a': 'a', 'b': 'c', 'd': 'e', 'f': 'g'}
    assert psyco.proxy(f)(5) == f(5)
    assert psyco.proxy(f)('b') == f('b')

def test_nonfloat_in_math():
    class X(object):
        def __float__(self):
            return -123.45
    def f(x):
        return math.ceil(x), math.floor(x)
    assert psyco.proxy(f)(X()) == (-123.0, -124.0)

def test_power_error_case():
    def f(x, y):
        return x ** y
    py.test.raises(ValueError, f, -4.5, 0.9)
    py.test.raises(ValueError, psyco.proxy(f), -4.5, 0.9)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.