test_complex.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Lib » 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 » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Lib » test » test_complex.py
from test_support import TestFailed
from random import random

# XXX need many, many more tests here.

nerrors = 0

def check_close_real(x, y, eps=1e-12):
    """Return true iff floats x and y "are close\""""
    # put the one with larger magnitude second
    if abs(x) > abs(y):
        x, y = y, x
    if y == 0:
        return abs(x) < eps
    if x == 0:
        return abs(y) < eps
    # check that relative difference < eps
    return abs((x-y)/y) < eps

def check_close(x, y, eps=1e-12):
    """Return true iff complexes x and y "are close\""""
    return check_close_real(x.real, y.real, eps) and \
           check_close_real(x.imag, y.imag, eps)

def test_div(x, y):
    """Compute complex z=x*y, and check that z/x==y and z/y==x."""
    global nerrors
    z = x * y
    if x != 0:
        q = z / x
        if not check_close(q, y):
            nerrors += 1
            print `z`, "/", `x`, "==", `q`, "but expected", `y`
    if y != 0:
        q = z / y
        if not check_close(q, x):
            nerrors += 1
            print `z`, "/", `y`, "==", `q`, "but expected", `x`

simple_real = [float(i) for i in range(-5, 6)]
simple_complex = [complex(x, y) for x in simple_real for y in simple_real]
for x in simple_complex:
    for y in simple_complex:
        test_div(x, y)

# A naive complex division algorithm (such as in 2.0) is very prone to
# nonsense errors for these (overflows and underflows).
test_div(complex(1e200, 1e200), 1+0j)
test_div(complex(1e-200, 1e-200), 1+0j)

# Just for fun.
for i in range(100):
    test_div(complex(random(), random()),
             complex(random(), random()))

try:
    z = 1.0 / (0+0j)
except ZeroDivisionError:
    pass
else:
    nerrors += 1
    raise TestFailed("Division by complex 0 didn't raise ZeroDivisionError")

if nerrors:
    raise TestFailed("%d tests failed" % nerrors)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.