-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
167 lines (145 loc) · 5.63 KB
/
tests.py
File metadata and controls
167 lines (145 loc) · 5.63 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
""" Tests for the cartesian module """
import unittest
import cartesian
class TestPointClass(unittest.TestCase):
""" Tests for the Point class """
def test_create_point(self):
""" Ensures that a point can be created """
point = cartesian.Point()
self.assertIsNotNone(point)
def test_create_point_with_coords(self):
""" Ensures providied coordinates are correctly assigned """
point = cartesian.Point(-1, 2)
self.assertEqual(point.x, -1)
self.assertEqual(point.y, 2)
def test_create_point_default_coords(self):
""" Ensures that the default coordinates are (0, 0) """
point = cartesian.Point()
self.assertEqual(point.x, 0)
self.assertEqual(point.y, 0)
def test_point_repr(self):
""" Ensures that repr() returns the correct result """
point = cartesian.Point(-1, 2)
self.assertEqual(point.__repr__(), "Point(-1, 2)")
self.assertEqual(repr(point), "Point(-1, 2)")
def test_point_str(self):
""" Ensures that str() returns the correct result """
point = cartesian.Point(-1, 2)
self.assertEqual(point.__str__(), "Point at x=-1 and y=2")
self.assertEqual(str(point), "Point at x=-1 and y=2")
def test_point_quadrant_none(self):
""" Ensures that a point lying on an axis has no quadrant """
points = [
cartesian.Point(1, 0),
cartesian.Point(0, 1),
cartesian.Point(-1, 0),
cartesian.Point(0, -1),
cartesian.Point(0, 0)
]
for point in points:
self.assertEqual(point.quadrant(), None)
def test_point_quadrant_first(self):
""" Ensures that the first quadrant is correctly identified """
self.assertEqual(cartesian.Point(1, 1).quadrant(), 1)
def test_point_quadrant_second(self):
""" Ensures that the second quadrant is correctly identified """
self.assertEqual(cartesian.Point(-1, 1).quadrant(), 2)
def test_point_quadrant_third(self):
""" Ensures that the third quadrant is correctly identified """
self.assertEqual(cartesian.Point(-1, -1).quadrant(), 3)
def test_point_quadrant_fourth(self):
""" Ensures that the fourth quadrant is correctly identified """
self.assertEqual(cartesian.Point(1, -1).quadrant(), 4)
def test_point_distance(self):
""" Ensures the distance is correctly calculated """
self.assertEqual(
cartesian.Point(3, 4).distance(cartesian.Point(0, 0)), 5
)
self.assertEqual(
cartesian.Point(1, 2).distance(cartesian.Point(4, 3)), pow(10, 0.5)
)
def test_point_negative(self):
""" Ensures the distance is correctly calculated with negatives """
self.assertEqual(
cartesian.Point(-1, 2).distance(cartesian.Point(4, 3)),
pow(26, 0.5)
)
self.assertEqual(
cartesian.Point(1, -2).distance(cartesian.Point(4, 3)),
pow(34, 0.5)
)
self.assertEqual(
cartesian.Point(-1, -2).distance(cartesian.Point(4, 3)),
pow(50, 0.5)
)
def test_point_distance_default(self):
""" Ensures the distance is correctly calculated between the origin """
# Real distance is 1.41... (the square root of 2)
self.assertEqual(
cartesian.Point(1, 1).distance(), pow(2, 0.5)
)
self.assertEqual(
cartesian.Point(-1, 1).distance(), pow(2, 0.5)
)
self.assertEqual(
cartesian.Point(1, -1).distance(), pow(2, 0.5)
)
self.assertEqual(
cartesian.Point(-1, -1).distance(), pow(2, 0.5)
)
def test_point_distance_none(self):
""" Ensures that a lack of distance returns zero """
self.assertEqual(
cartesian.Point(1, 1).distance(cartesian.Point(1, 1)), 0
)
self.assertEqual(
cartesian.Point(-1, 1).distance(cartesian.Point(-1, 1)), 0
)
self.assertEqual(
cartesian.Point(1, -1).distance(cartesian.Point(1, -1)), 0
)
self.assertEqual(
cartesian.Point(-1, -1).distance(cartesian.Point(-1, -1)), 0
)
def test_point_midpoint_default(self):
""" Ensures the midpoint is correctly calculated between the origin """
self.assertEqual(
cartesian.Point(1, 1).midpoint(), cartesian.Point(0.5, 0.5)
)
self.assertEqual(
cartesian.Point(-1, 1).midpoint(), cartesian.Point(-0.5, 0.5)
)
self.assertEqual(
cartesian.Point(1, -1).midpoint(), cartesian.Point(0.5, -0.5)
)
self.assertEqual(
cartesian.Point(-1, -1).midpoint(), cartesian.Point(-0.5, -0.5)
)
def test_point_eq(self):
""" Ensures the __eq__ dunder method works correctly """
self.assertEqual(
cartesian.Point(1, 1), cartesian.Point(1, 1)
)
self.assertEqual(
cartesian.Point(-1, 1), cartesian.Point(-1, 1)
)
self.assertEqual(
cartesian.Point(1, -1), cartesian.Point(1, -1)
)
self.assertEqual(
cartesian.Point(-1, -1), cartesian.Point(-1, -1)
)
self.assertNotEqual(
cartesian.Point(1, 1), cartesian.Point(-1, -1)
)
self.assertNotEqual(
cartesian.Point(-1, 1), cartesian.Point(1, -1)
)
self.assertNotEqual(
cartesian.Point(1, -1), cartesian.Point(-1, 1)
)
self.assertNotEqual(
cartesian.Point(-1, -1), cartesian.Point(1, 1)
)
if __name__ == '__main__':
unittest.main()