-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_test.py
More file actions
51 lines (39 loc) · 1.33 KB
/
run_test.py
File metadata and controls
51 lines (39 loc) · 1.33 KB
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
import unittest
from fraction import Fraction
class TestFraction(unittest.TestCase):
def test_str(self):
val = Fraction(1, 2)
self.assertEqual('1/2', str(val))
def test_sanitization(self):
val = Fraction(3, -6)
self.assertEqual(Fraction(-1, 2).__dict__, val.__dict__)
def test_add(self):
a = Fraction(1, 3)
b = Fraction(3, 4)
self.assertEqual(Fraction(13, 12), a + b)
def test_sub(self):
a = Fraction(1, 3)
b = Fraction(1, 4)
self.assertEqual(Fraction(1, 12), a - b)
def test_mul(self):
a = Fraction(1, 3)
b = Fraction(3, 5)
self.assertEqual(Fraction(1, 5), a * b)
def test_truediv(self):
a = Fraction(1, 3)
b = Fraction(3, 4)
self.assertEqual(Fraction(4, 9), a / b)
def test_decimal(self):
frac = Fraction(1, 2)
if isinstance(Fraction.decimal, property):
self.assertEqual(0.5, frac.decimal)
else:
self.assertEqual(0.5, frac.decimal())
def test_zero_denumerator(self):
with self.assertRaises(ValueError):
Fraction(1, 0)
def test_non_integer(self):
with self.assertRaises((ValueError, TypeError)):
Fraction(1.5, 2)
with self.assertRaises((ValueError, TypeError)):
Fraction(1, 2.5)