|
1 | | -import unittest |
2 | 1 | import math |
3 | | - |
| 2 | +import unittest |
4 | 3 | from math import sqrt |
5 | | -from pygame import Vector2, Vector3, Rect, FRect |
6 | 4 |
|
| 5 | +from pygame import Vector2, Vector3, Rect, FRect |
7 | 6 | from pygame.geometry import Circle |
8 | 7 |
|
9 | 8 |
|
@@ -1149,6 +1148,146 @@ def assert_approx_equal(circle1, circle2, eps=1e-12): |
1149 | 1148 | c.rotate_ip(angle, center) |
1150 | 1149 | assert_approx_equal(c, rotate_circle(c, angle, center)) |
1151 | 1150 |
|
| 1151 | + def test_contains_argtype(self): |
| 1152 | + """Tests if the function correctly handles incorrect types as parameters""" |
| 1153 | + |
| 1154 | + invalid_types = (None, [], "1", (1,), 1, (1, 2, 3)) |
| 1155 | + |
| 1156 | + c = Circle(10, 10, 4) |
| 1157 | + |
| 1158 | + for value in invalid_types: |
| 1159 | + with self.assertRaises(TypeError): |
| 1160 | + c.contains(value) |
| 1161 | + |
| 1162 | + def test_contains_argnum(self): |
| 1163 | + """Tests if the function correctly handles incorrect number of parameters""" |
| 1164 | + c = Circle(10, 10, 4) |
| 1165 | + |
| 1166 | + invalid_args = [(Circle(10, 10, 4), Circle(10, 10, 4))] |
| 1167 | + |
| 1168 | + with self.assertRaises(TypeError): |
| 1169 | + c.contains() |
| 1170 | + |
| 1171 | + for arg in invalid_args: |
| 1172 | + with self.assertRaises(TypeError): |
| 1173 | + c.contains(*arg) |
| 1174 | + |
| 1175 | + def test_contains_return_type(self): |
| 1176 | + """Tests if the function returns the correct type""" |
| 1177 | + c = Circle(10, 10, 4) |
| 1178 | + items = [ |
| 1179 | + Circle(3, 4, 15), |
| 1180 | + (0, 0), |
| 1181 | + Vector2(0, 0), |
| 1182 | + Rect(0, 0, 10, 10), |
| 1183 | + FRect(0, 0, 10, 10), |
| 1184 | + ] |
| 1185 | + |
| 1186 | + for item in items: |
| 1187 | + self.assertIsInstance(c.contains(item), bool) |
| 1188 | + |
| 1189 | + def test_contains_circle(self): |
| 1190 | + """Ensures that the contains method correctly determines if a circle is |
| 1191 | + contained within the circle""" |
| 1192 | + c = Circle(10, 10, 4) |
| 1193 | + c2 = Circle(10, 10, 2) |
| 1194 | + c3 = Circle(100, 100, 5) |
| 1195 | + c4 = Circle(16, 10, 7) |
| 1196 | + |
| 1197 | + # self |
| 1198 | + self.assertTrue(c.contains(c)) |
| 1199 | + |
| 1200 | + # self-like |
| 1201 | + c_s = Circle(c) |
| 1202 | + self.assertTrue(c.contains(c_s)) |
| 1203 | + |
| 1204 | + # contained circle |
| 1205 | + self.assertTrue(c.contains(c2)) |
| 1206 | + |
| 1207 | + # not contained circle |
| 1208 | + self.assertFalse(c.contains(c3)) |
| 1209 | + |
| 1210 | + # intersecting circle |
| 1211 | + self.assertFalse(c.contains(c4)) |
| 1212 | + |
| 1213 | + # bigger circle not contained in smaller circle |
| 1214 | + self.assertFalse(c2.contains(c)) |
| 1215 | + self.assertFalse(c3.contains(c4)) |
| 1216 | + |
| 1217 | + def test_contains_point(self): |
| 1218 | + """Ensures that the contains method correctly determines if a point is |
| 1219 | + contained within the circle""" |
| 1220 | + c = Circle(10, 10, 4) |
| 1221 | + p1 = (10, 10) |
| 1222 | + p2 = (10, 15) |
| 1223 | + p3 = (100, 100) |
| 1224 | + p4 = (c.x + math.sin(math.pi / 4) * c.r, c.y + math.cos(math.pi / 4) * c.r) |
| 1225 | + |
| 1226 | + p1v = Vector2(p1) |
| 1227 | + p2v = Vector2(p2) |
| 1228 | + p3v = Vector2(p3) |
| 1229 | + p4v = Vector2(p4) |
| 1230 | + |
| 1231 | + # contained point |
| 1232 | + self.assertTrue(c.contains(p1)) |
| 1233 | + |
| 1234 | + # not contained point |
| 1235 | + self.assertFalse(c.contains(p2)) |
| 1236 | + self.assertFalse(c.contains(p3)) |
| 1237 | + |
| 1238 | + # on the edge |
| 1239 | + self.assertTrue(c.contains(p4)) |
| 1240 | + |
| 1241 | + # contained point |
| 1242 | + self.assertTrue(c.contains(p1v)) |
| 1243 | + |
| 1244 | + # not contained point |
| 1245 | + self.assertFalse(c.contains(p2v)) |
| 1246 | + self.assertFalse(c.contains(p3v)) |
| 1247 | + |
| 1248 | + # on the edge |
| 1249 | + self.assertTrue(c.contains(p4v)) |
| 1250 | + |
| 1251 | + def test_contains_rect_frect(self): |
| 1252 | + """Ensures that the contains method correctly determines if a rect is |
| 1253 | + contained within the circle""" |
| 1254 | + c = Circle(0, 0, 10) |
| 1255 | + r1 = Rect(0, 0, 3, 3) |
| 1256 | + r2 = Rect(10, 10, 10, 10) |
| 1257 | + r3 = Rect(10, 10, 5, 5) |
| 1258 | + |
| 1259 | + angle = math.pi / 4 |
| 1260 | + x = c.x - math.sin(angle) * c.r |
| 1261 | + y = c.y - math.cos(angle) * c.r |
| 1262 | + rx = c.x + math.sin(angle) * c.r |
| 1263 | + ry = c.y + math.cos(angle) * c.r |
| 1264 | + r_edge = Rect(x, y, rx - x, ry - y) |
| 1265 | + |
| 1266 | + fr1 = FRect(0, 0, 3, 3) |
| 1267 | + fr2 = FRect(10, 10, 10, 10) |
| 1268 | + fr3 = FRect(10, 10, 5, 5) |
| 1269 | + fr_edge = FRect(x, y, rx - x, ry - y) |
| 1270 | + |
| 1271 | + # contained rect |
| 1272 | + self.assertTrue(c.contains(r1)) |
| 1273 | + |
| 1274 | + # not contained rect |
| 1275 | + self.assertFalse(c.contains(r2)) |
| 1276 | + self.assertFalse(c.contains(r3)) |
| 1277 | + |
| 1278 | + # on the edge |
| 1279 | + self.assertTrue(c.contains(r_edge)) |
| 1280 | + |
| 1281 | + # contained rect |
| 1282 | + self.assertTrue(c.contains(fr1)) |
| 1283 | + |
| 1284 | + # not contained rect |
| 1285 | + self.assertFalse(c.contains(fr2)) |
| 1286 | + self.assertFalse(c.contains(fr3)) |
| 1287 | + |
| 1288 | + # on the edge |
| 1289 | + self.assertTrue(c.contains(fr_edge)) |
| 1290 | + |
1152 | 1291 |
|
1153 | 1292 | if __name__ == "__main__": |
1154 | 1293 | unittest.main() |
0 commit comments