1# test the functions imported from cmath
2
3try:
4    from cmath import *
5except ImportError:
6    print("SKIP")
7    raise SystemExit
8
9# make sure these constants exist in cmath
10print("%.5g" % e)
11print("%.5g" % pi)
12
13test_values_non_zero = []
14base_values = (0.0, 0.5, 1.2345, 10.0)
15for r in base_values:
16    for i in base_values:
17        if r != 0.0 or i != 0.0:
18            test_values_non_zero.append(complex(r, i))
19        if r != 0.0:
20            test_values_non_zero.append(complex(-r, i))
21        if i != 0.0:
22            test_values_non_zero.append(complex(r, -i))
23        if r != 0.0 and i != 0.0:
24            test_values_non_zero.append(complex(-r, -i))
25test_values = [complex(0.0, 0.0)] + test_values_non_zero
26print(test_values)
27
28functions = [
29    ("phase", phase, test_values),
30    ("polar", polar, test_values),
31    (
32        "rect",
33        rect,
34        ((0, 0), (0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, 1), (1, -1), (123.0, -456.0)),
35    ),
36    ("exp", exp, test_values),
37    ("log", log, test_values_non_zero),
38    ("sqrt", sqrt, test_values),
39    ("cos", cos, test_values),
40    ("sin", sin, test_values),
41]
42
43for f_name, f, test_vals in functions:
44    print(f_name)
45    for val in test_vals:
46        if type(val) == tuple:
47            ret = f(*val)
48        else:
49            ret = f(val)
50        if type(ret) == float:
51            print("%.5g" % ret)
52        elif type(ret) == tuple:
53            print("%.5g %.5g" % ret)
54        else:
55            # some test (eg cmath.sqrt(-0.5)) disagree with CPython with tiny real part
56            real = ret.real
57            if abs(real) < 1e-6:
58                real = 0.0
59            print("complex(%.5g, %.5g)" % (real, ret.imag))
60
61# test invalid type passed to cmath function
62try:
63    log([])
64except TypeError:
65    print("TypeError")
66