generated from ISP19/unittesting-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fraction_test.py
142 lines (125 loc) · 5.56 KB
/
fraction_test.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import math
import time
import unittest
from fraction import Fraction
class FractionTest(unittest.TestCase):
"""Test the methods and constructor of the Fraction class. """
def test_init(self):
self.assertEqual(0, Fraction(0))
self.assertEqual(88, Fraction(88))
self.assertEqual(10, Fraction(10, 1))
self.assertEqual(112, Fraction(112, 1))
self.assertTrue(math.isnan(Fraction(0, 0)))
self.assertEqual(math.inf, Fraction(1, 0))
self.assertEqual(-math.inf, Fraction(-1, 0))
with self.assertRaises(ValueError):
Fraction(8, 0)
Fraction(-9, 0)
Fraction(11, -0)
with self.assertRaises(ValueError):
Fraction(0.1, 0.8)
with self.assertRaises(ValueError):
Fraction(3.14, 3.14)
def test_str(self):
f = Fraction(3, -1)
self.assertEqual("-3", f.__str__())
f = Fraction(0, 5)
self.assertEqual("0", f.__str__())
f = Fraction(0, 100)
self.assertEqual("0", f.__str__())
f = Fraction(60, 90)
self.assertEqual("2/3", f.__str__())
f = Fraction(1500, 60)
self.assertEqual("25", f.__str__())
f = Fraction(1500, 90)
self.assertEqual("50/3", f.__str__())
f = Fraction(-80, 20)
self.assertEqual("-4", f.__str__())
f = Fraction(36, -60)
self.assertEqual("-3/5", f.__str__())
# Constructor should provide default denominator = 1
f = Fraction(99)
self.assertEqual("99", f.__str__())
f = Fraction(10, 1)
self.assertEqual("10", f.__str__())
f = Fraction(0, 0)
self.assertEqual("nan", f.__str__())
f = Fraction(1, 0)
self.assertEqual("inf", f.__str__())
f = Fraction(-1, 0)
self.assertEqual("-inf", f.__str__())
def test_add(self):
self.assertTrue(Fraction(3, 4) == Fraction(1, 12) + Fraction(2, 3))
self.assertTrue(Fraction(-1, 16) == Fraction(1, 16) + Fraction(-2, 16))
self.assertTrue(Fraction(-8, 9) == Fraction(-7, 9) + Fraction(-1, 9))
self.assertTrue(Fraction(-8, 28) == Fraction(7, -28) + Fraction(1, -28))
self.assertTrue(math.inf == Fraction(10, 112) + Fraction(1, 0))
self.assertTrue(-math.inf == Fraction(10, 112) + Fraction(-1, 0))
def test_sub(self):
self.assertTrue(Fraction(-1, 16) == Fraction(1, 16) - Fraction(2, 16))
self.assertTrue(Fraction(-8, 9) == Fraction(-7, 9) - Fraction(1, 9))
self.assertTrue(Fraction(-8, 28) == Fraction(7, -28) - Fraction(1, 28))
self.assertTrue(Fraction(10, 112) == Fraction(10, 112) - Fraction(0))
self.assertTrue(Fraction(10, 112) == Fraction(10, 112) - 0)
def test_mul(self):
self.assertEqual(Fraction(0), Fraction(-5, 9) * Fraction(0, 6))
self.assertEqual(Fraction(5, 6), Fraction(1, 6)*Fraction(5, 1))
self.assertEqual(Fraction(-5, 49), Fraction(1, 7) * Fraction(-5, 7))
self.assertEqual(Fraction(20, 54), Fraction(-5, 9) * Fraction(-4, 6))
self.assertEqual(-math.inf, Fraction(1, 0) * Fraction(-1, 0))
self.assertEqual(math.inf, Fraction(-1, 0) * Fraction(-1, 0))
def test_neg(self):
self.assertEqual(Fraction(-7, 8), -Fraction(7, 8))
self.assertEqual(Fraction(10, -16), -Fraction(10, 16))
self.assertEqual(Fraction(9, 10), -Fraction(-9, 10))
self.assertEqual(Fraction(10, 112), -Fraction(-10, 112))
self.assertEqual(-math.inf, -Fraction(1, 0))
self.assertEqual(math.inf, -Fraction(-1, 0))
# Double negation
self.assertEqual(math.inf, -(-Fraction(1, 0)))
def test_lt(self):
self.assertTrue(Fraction(-1, 2) > Fraction(-2, 3))
self.assertTrue(Fraction(8, 8) < Fraction(9, 8))
self.assertTrue(Fraction(-112, 112) < 0)
self.assertTrue(Fraction(-10, 10) < Fraction(1))
self.assertTrue(Fraction(-15, 16) < Fraction(15, 16))
self.assertTrue(Fraction(27, 28) < Fraction(28, 29))
def test_gt(self):
self.assertTrue(Fraction(1, 2) > Fraction(1, 4))
self.assertTrue(Fraction(4, 5) > Fraction(3, 4))
self.assertTrue(Fraction(-4, 5) > Fraction(-5, 6))
self.assertTrue(Fraction(5, 9) > Fraction(1, 3))
self.assertTrue(Fraction(10, 112) > Fraction(-10, 112))
self.assertTrue(Fraction(10, 112) > Fraction(10, -112))
self.assertTrue(Fraction(8) > Fraction(-8))
self.assertTrue(Fraction(3, 2) > 0)
def test_from_str(self):
self.assertEqual(Fraction(1, 8), Fraction.from_str("1/8"))
self.assertEqual(1, Fraction.from_str("112/112"))
self.assertEqual(math.inf, Fraction.from_str("1/0"))
self.assertEqual(-math.inf, Fraction.from_str("-1/0"))
self.assertTrue(math.isnan(Fraction.from_str("0/0")))
with self.assertRaises(ValueError):
Fraction.from_str("112")
def test_eq(self):
one = Fraction(1)
inf = Fraction(1, 0)
f = Fraction(1, 2)
g = Fraction(-40, -80)
h = Fraction(10000, 20001) # not quite 1/2
i = Fraction(0, 3)
j = Fraction(0, 6)
k = Fraction(10, 10)
self.assertTrue(inf == math.inf)
self.assertTrue(inf.__eq__(math.inf))
self.assertTrue(f == g)
self.assertTrue(f.__eq__(g)) # same thing
self.assertFalse(f == h)
self.assertFalse(f.__eq__(h))
self.assertTrue(i == j)
self.assertTrue(i.__eq__(j))
self.assertTrue(one == k)
self.assertTrue(one.__eq__(k))
self.assertTrue(one == 1)
if __name__ == "__main__":
unittest.main(verbosity=2)