forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_circle.py
More file actions
242 lines (142 loc) · 3.51 KB
/
Copy pathtest_circle.py
File metadata and controls
242 lines (142 loc) · 3.51 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python
from circle import Circle, Sphere
import pytest
from math import pi
import random
def test_init():
Circle(3)
def test_radius():
c = Circle(3)
assert c.radius == 3
def test_no_radius():
with pytest.raises(TypeError):
c = Circle()
def test_set_radius():
c = Circle(3)
c.radius = 5
assert c.radius == 5
def test_diam():
c = Circle(3)
assert c.diameter == 6
def test_radius_change():
c = Circle(3)
c.radius = 4
assert c.diameter == 8
def test_set_diameter():
c = Circle(4)
c.diameter = 10
assert c.radius == 5
assert c.diameter == 10
def test_set_diameter_float():
c = Circle(4)
c.diameter = 11
assert c.radius == 5.5
assert c.diameter == 11
def test_area():
c = Circle(2)
assert c.area == pi*4
def test_set_area():
c = Circle(2)
with pytest.raises(AttributeError):
c.area = 30
def test_from_diameter():
c = Circle.from_diameter(4)
assert isinstance(c, Circle)
assert c.radius == 2
assert c.diameter == 4
def test_repr():
c = Circle(6.0)
assert repr(c) == 'Circle(6.0)'
def test_str():
c = Circle(3.0)
assert str(c) == 'Circle with radius: 3'
def test_addition():
c1 = Circle(2)
c2 = Circle(3)
c3 = c1 + c2
assert c3.radius == 5
def test_multiplication():
c1 = Circle(2)
c3 = c1 * 4
assert c3.radius == 8
def test_equal():
c1 = Circle(3)
c2 = Circle(3.0)
assert c1 == c2
assert c1 <= c2
assert c1 >= c2
def test_not_equal():
c1 = Circle(2.9)
c2 = Circle(3.0)
assert c1 != c2
def test_greater():
c1 = Circle(2)
c2 = Circle(3)
assert c2 > c1
assert c2 >= c1
def test_less():
c1 = Circle(2)
c2 = Circle(3)
assert c1 < c2
assert c1 <= c2
def test_reverse_multiply():
c = Circle(3)
c2 = 3 * c
assert c2.radius == 9.0
def test_plus_equal():
c = Circle(3)
c2 = c
c += Circle(2)
assert c.radius == 5
assert c is c2
assert c2.radius == 5
def test_times_equal():
c = Circle(3)
c2 = c
c *= 2
assert c.radius == 6
assert c is c2
assert c2.radius == 6
def test_sort():
a_list = [Circle(20), Circle(10), Circle(15), Circle(5)]
a_list.sort()
assert a_list[0] == Circle(5)
assert a_list[3] == Circle(20)
assert a_list[0] < a_list[1] < a_list[2] < a_list[3]
#############################
# Tests for the Sphere Object
#############################
def test_sphere_vol():
s = Sphere(4)
print(s.volume())
assert s.volume() == 268.082573106329
def test_sphere_change_radius():
s = Sphere.from_diameter(8)
assert s.radius == 4
s.radius = 3
assert s.diameter == 6
def test_sphere_diameter():
s = Sphere.from_diameter(8)
# note that the classmethod got properly inherited
assert type(s) == Sphere
print(s.volume())
assert s.volume() == 268.082573106329
def test_sphere_area():
s = Sphere(4)
with pytest.raises(NotImplementedError):
s.area()
def test_sphere_repr():
s = Sphere(12)
assert repr(s) == "Sphere(12)"
assert eval(repr(s)) == s
def test_sphere_str():
s = Sphere(12)
assert str(s) == "Sphere with radius: 12"
assert eval(repr(s)) == s
def test_sphere_sort():
list_o_spheres = [Sphere(random.randint(1, 20)) for i in range(10)]
print(list_o_spheres)
list_o_spheres.sort()
print(list_o_spheres)
for s1, s2 in zip(list_o_spheres, list_o_spheres[1:]):
assert s1 <= s2