-
Notifications
You must be signed in to change notification settings - Fork 0
/
adding_fractions_test.py
117 lines (95 loc) · 3.73 KB
/
adding_fractions_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
import unittest
import math
class Fraction:
def __init__(self, numerator, denominator = 1):
self.numerator = int(numerator)
self.denominator = int(denominator)
def __add__(self, augend):
if augend.denominator == self.denominator:
return Fraction(int(self.numerator) + int(augend.numerator), self.denominator)
else:
lcm = math.lcm(augend.denominator, self.denominator) # lowest common multiple
return Fraction(lcm/self.denominator + lcm / augend.denominator,lcm)
def as_integer(self):
return self.numerator
class AddingFractionsTest(unittest.TestCase):
def testHookUp(self):
assert 0 + 0 == 0
assert 0 + 1 == 1
def test_that_zero_plus_zero_equals_zero(self):
addend = Fraction(0)
augend = Fraction(0)
sum = addend + augend
self.assert_equals(sum.as_integer(), 0)
def test_that_zero_plus_one_equals_one(self):
addend = Fraction(0)
augend = Fraction(1)
sum = addend + augend
self.assert_equals(sum.as_integer(), 1)
def test_that_one_plus_one_equals_two(self):
addend = Fraction(1)
augend = Fraction(1)
sum = addend + augend
self.assert_equals(sum.as_integer(), 2)
def test_that_minus_one_plus_zero_equals_minus_one(self):
addend = Fraction(-1)
augend = Fraction(0)
sum = addend + augend
self.assert_equals(sum.as_integer(), -1)
def test_that_minus_one_plus_one_equals_zero(self):
addend = Fraction(-1)
augend = Fraction(1)
sum = addend + augend
self.assert_equals(sum.as_integer(), 0)
def test_that_minus_one_plus_two_equals_one(self):
addend = Fraction(-1)
augend = Fraction(2)
sum = addend + augend
self.assert_equals(sum.as_integer(), 1)
def test_that_minus_one_plus_minus_one_equals_minus_two(self):
addend = Fraction(-1)
augend = Fraction(-1)
sum = addend + augend
self.assert_equals(sum.as_integer(), -2)
def test_that_1_on_3_plus_1_on_3_equals_2_on_3(self):
addend = Fraction(1,3)
augend = Fraction(1,3)
sum = addend + augend
self.assert_equals(sum.numerator, 2)
self.assert_equals(sum.denominator, 3)
def test_that_2_on_3_plus_2_on_3_equals_4_on_3(self):
addend = Fraction(2,3)
augend = Fraction(2,3)
sum = addend + augend
self.assert_equals(sum.numerator, 4)
self.assert_equals(sum.denominator, 3)
def test_that_2_on_5_plus_1_on_5_equals_3_on_5(self):
addend = Fraction(2, 5)
augend = Fraction(1, 5)
sum = addend + augend
self.assert_equals(sum.numerator, 3)
self.assert_equals(sum.denominator, 5)
def test_that_1_on_4_plus_1_on_3_equals_7_on_12(self):
addend = Fraction(1, 4)
augend = Fraction(1, 3)
sum = addend + augend
self.assert_equals(sum.numerator, 7)
self.assert_equals(sum.denominator, 12)
# This test fails ^^^
# Next step is to add Fractions that do not have the same denominator
def test_that_1_on_3_plus_1_on_5_equals_8_on_15(self):
addend = Fraction(1, 3)
augend = Fraction(1, 5)
sum = addend + augend
self.assert_equals(sum.numerator, 8)
self.assert_equals(sum.denominator, 15)
def test_that_1_on_2_plus_1_on_4_equals_3_on_4(self):
addend = Fraction(1, 2)
augend = Fraction(1, 4)
sum = addend + augend
self.assert_equals(sum.numerator, 3)
self.assert_equals(sum.denominator, 4)
def assert_equals(self, sum, expected):
assert sum == expected, 'Expected ' + str(expected) + ', got ' + str(sum)
if __name__ == "__main__":
unittest.main() # run all tests