forked from GregHilston/github-tutorial-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
135 lines (108 loc) · 5.42 KB
/
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
import unittest
from calculator import Calculator
import math
class TestCalculator(unittest.TestCase):
def setUp(self):
self.calculator = Calculator()
def test_add(self):
"""Tests the add function for every combination of 1, 0 and -1.
May be redundant but checks if communitive property is respected.
"""
# Where x = 1
self.assertEqual(self.calculator.add(1, 1), 2)
self.assertEqual(self.calculator.add(1, 0), 1)
self.assertEqual(self.calculator.add(1, -1), 0)
# Where x = 0
self.assertEqual(self.calculator.add(0, 1), 1)
self.assertEqual(self.calculator.add(0, 0), 0)
self.assertEqual(self.calculator.add(0, -1), -1)
# Where x = -1
self.assertEqual(self.calculator.add(-1, 1), 0)
self.assertEqual(self.calculator.add(-1, 0), -1)
self.assertEqual(self.calculator.add(-1, -1), -2)
def test_subtract(self):
"""Tests the subtract function for every combination of 1, 0 and -1.
May be redundant but checks if communitive property is respected.
"""
# Where x = 1
self.assertEqual(self.calculator.subtract(1, 1), 0)
self.assertEqual(self.calculator.subtract(1, 0), 1)
self.assertEqual(self.calculator.subtract(1, -1), 2)
# Where x = 0
self.assertEqual(self.calculator.subtract(0, 1), -1)
self.assertEqual(self.calculator.subtract(0, 0), 0)
self.assertEqual(self.calculator.subtract(0, -1), 1)
# Where x = -1
self.assertEqual(self.calculator.subtract(-1, 1), -2)
self.assertEqual(self.calculator.subtract(-1, 0), -1)
self.assertEqual(self.calculator.subtract(-1, -1), 0)
def test_multiply(self):
"""Tests the multiply function for every combination of 1, 0 and -1.
May be redundant but checks if communitive property is respected.
"""
# Where x = 1
self.assertEqual(self.calculator.multiply(1, 1), 1)
self.assertEqual(self.calculator.multiply(1, 0), 0)
self.assertEqual(self.calculator.multiply(1, -1), -1)
# Where x = 0
self.assertEqual(self.calculator.multiply(0, 1), 0)
self.assertEqual(self.calculator.multiply(0, 0), 0)
self.assertEqual(self.calculator.multiply(0, -1), 0)
# Where x = -1
self.assertEqual(self.calculator.multiply(-1, 1), -1)
self.assertEqual(self.calculator.multiply(-1, 0), 0)
self.assertEqual(self.calculator.multiply(-1, -1), 1)
def test_divide(self):
"""Tests the divide function for every combination of 1, 0 and -1.
May be redundant but checks if communitive property is respected.
Note: Since our divide function will throw ZeroDivisionErrors when
passing a value of 0 for y, you'll notice we use assertRaises to ensure
that these exceptions are thrown when expected.
"""
# Where x = 1
self.assertEqual(self.calculator.divide(1, 1), 1)
self.assertRaises(ZeroDivisionError, self.calculator.divide, 1, 0)
self.assertEqual(self.calculator.divide(1, -1), -1)
# Where x = 0
self.assertEqual(self.calculator.divide(0, 1), 0)
self.assertRaises(ZeroDivisionError, self.calculator.divide, 0, 0)
self.assertEqual(self.calculator.divide(0, -1), 0)
# Where x = -1
self.assertEqual(self.calculator.divide(-1, 1), -1)
self.assertRaises(ZeroDivisionError, self.calculator.divide, -1, 0)
self.assertEqual(self.calculator.divide(-1, -1), 1)
def test_tan(self):
"""Tests the subtract function for every combination of 1, 0 and -1.
May be redundant but checks if communitive property is respected.
"""
self.assertTrue(math.isclose(self.calculator.tan(0), 0))
self.assertTrue(math.isclose(self.calculator.tan(-3), 0.142546543074))
self.assertTrue(math.isclose(self.calculator.tan(3), -0.142546543074))
self.assertTrue(math.isclose(self.calculator.tan(math.pi/4), 1))
def test_square(self):
"""Tests the square function for every combination of 1, 0 and -1.
May be redundant but checks if communitive property is respected.
"""
# Where x = 1, 0, -1
self.assertEqual(self.calculator.square(1), 1)
self.assertEqual(self.calculator.square(0), 0)
self.assertEqual(self.calculator.square(-1), 1)
def test_log(self):
self.assertTrue(math.isclose(self.calculator.log(2,10), 0.30103, rel_tol=0.05))
self.assertTrue(math.isclose(self.calculator.log(10,10), 1))
self.assertTrue(math.isclose(self.calculator.log(100, 10), 2))
def test_cube(self):
"""Tests the square root function for every combination of 1, 0 and -1.
May be redundant but checks if communitive property is respected.
"""
# Where x = 1, 0, -1
self.assertEqual(self.calculator.cube(1), 1)
self.assertEqual(self.calculator.cube(0), 0)
self.assertEqual(self.calculator.cube(-1), -1)
def test_sin(self):
"""Tests the sin function."""
self.assertTrue(math.isclose(self.calculator.sin(1), 0.8414709848))
self.assertEqual(self.calculator.sin(0), 0)
self.assertTrue(math.isclose(self.calculator.sin(-1), -0.8414709848))
if __name__ == '__main__':
unittest.main()