-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathtest_cfrac.py
89 lines (64 loc) · 2.91 KB
/
test_cfrac.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import math
from fractions import Fraction
import pytest
from cfrac import ComplexFraction
def test_construction():
assert ComplexFraction() == ComplexFraction(0)
assert ComplexFraction() == 0
assert ComplexFraction() == 0.0
assert ComplexFraction(1, 1) == 1 + 1j
assert ComplexFraction.from_complex(1 + 1j) == ComplexFraction(1, 1)
def test_conversions():
assert complex(ComplexFraction(0)) == 0
assert abs(complex(ComplexFraction(1, 2)) - (1 + 2j)) < 1e-6
assert ComplexFraction(1, 2).as_fraction_pair() == (1, 2)
def test_arithmetic():
assert ComplexFraction(1) + ComplexFraction(2) == ComplexFraction(3)
assert ComplexFraction(1, 2) + ComplexFraction(3, 4) == ComplexFraction(4, 6)
assert ComplexFraction(1, 2) - ComplexFraction(3, 4) == ComplexFraction(-2, -2)
assert ComplexFraction(1, 2) * ComplexFraction(3, 4) == ComplexFraction(-5, 10)
assert ComplexFraction(1, 2) / ComplexFraction(3, 4) == ComplexFraction(Fraction(11, 25), Fraction(2, 25))
assert +ComplexFraction(1, 2) == ComplexFraction(1, 2)
assert -ComplexFraction(1, 2) == ComplexFraction(-1, -2)
assert ComplexFraction(1, 2).conjugate() == ComplexFraction(1, -2)
assert ComplexFraction(1, 2).norm_squared() == 5
assert math.isclose(abs(ComplexFraction(1, 2)), math.sqrt(5))
z = ComplexFraction(1)
z += 1j
assert z == ComplexFraction(1, 1)
def test_operator_fallbacks():
assert ComplexFraction(1) + 1 == 2
assert type(ComplexFraction(1) + 1) == ComplexFraction
assert 1 + ComplexFraction(1) == 2
assert type(1 + ComplexFraction(1)) == ComplexFraction
assert abs(ComplexFraction(1) + 1.5 - 2.5) < 1e-6
assert type(ComplexFraction(1) + 1.5) == complex
def test_pow():
assert ComplexFraction(1, 2) ** 0 == 1
assert ComplexFraction(1, 2) ** 5 == ComplexFraction(41, -38)
assert ComplexFraction(1, 2) ** -5 == ComplexFraction(Fraction(41, 3125), Fraction(38, 3125))
assert ComplexFraction(0, 1) ** 1000 == 1
with pytest.raises(ValueError):
ComplexFraction(0) ** 0
def test_hash():
assert hash(ComplexFraction(0)) == hash(0)
assert hash(ComplexFraction(42)) == hash(42)
assert hash(ComplexFraction(Fraction(1, 2))) == hash(Fraction(1, 2))
assert hash(ComplexFraction(0, 1)) == hash(1j)
d = {ComplexFraction(0, 1): "a", ComplexFraction(1, 0): "b", ComplexFraction(1, 1): "c"}
assert d[1j] == "a"
assert d[1] == "b"
assert d[1 + 1j] == "c"
def test_bool():
assert not bool(ComplexFraction(0))
assert bool(ComplexFraction(1))
assert bool(ComplexFraction(0, 1))
def test_comparisons_raise():
with pytest.raises(TypeError):
ComplexFraction() < ComplexFraction()
with pytest.raises(TypeError):
ComplexFraction() <= ComplexFraction()
with pytest.raises(TypeError):
ComplexFraction() > ComplexFraction()
with pytest.raises(TypeError):
ComplexFraction() >= ComplexFraction()