From 502cc8914cce20def1fe3719582ae3268462fdb7 Mon Sep 17 00:00:00 2001 From: itzpr3d4t0r <103119829+itzpr3d4t0r@users.noreply.github.com> Date: Sun, 6 Oct 2024 11:57:36 +0200 Subject: [PATCH] Now accepting degenerate lines, added some more __init__ tests. --- src_c/line.c | 38 +++++++++--------- test/geometry_test.py | 89 +++++++++++++++++-------------------------- 2 files changed, 52 insertions(+), 75 deletions(-) diff --git a/src_c/line.c b/src_c/line.c index 98f0d6d82f..407a56499e 100644 --- a/src_c/line.c +++ b/src_c/line.c @@ -1,8 +1,6 @@ #include "doc/geometry_doc.h" #include "geometry_common.h" -#define IS_LINE_VALID(line) (line->xa != line->xb || line->ya != line->yb) - static PyObject * _pg_line_subtype_new4(PyTypeObject *type, double xa, double ya, double xb, double yb) @@ -53,28 +51,28 @@ pgLine_FromObject(PyObject *obj, pgLineBase *out) PyObject **farray = PySequence_Fast_ITEMS(obj); if (length == 4) { - if (!pg_DoubleFromObj(farray[0], &(out->xa)) || - !pg_DoubleFromObj(farray[1], &(out->ya)) || - !pg_DoubleFromObj(farray[2], &(out->xb)) || - !pg_DoubleFromObj(farray[3], &(out->yb))) { + if (!pg_DoubleFromObj(farray[0], &out->xa) || + !pg_DoubleFromObj(farray[1], &out->ya) || + !pg_DoubleFromObj(farray[2], &out->xb) || + !pg_DoubleFromObj(farray[3], &out->yb)) { return 0; } - return IS_LINE_VALID(out); + return 1; } else if (length == 2) { - if (!pg_TwoDoublesFromObj(farray[0], &(out->xa), &(out->ya)) || - !pg_TwoDoublesFromObj(farray[1], &(out->xb), &(out->yb))) { + if (!pg_TwoDoublesFromObj(farray[0], &out->xa, &out->ya) || + !pg_TwoDoublesFromObj(farray[1], &out->xb, &out->yb)) { PyErr_Clear(); return 0; } - return IS_LINE_VALID(out); + return 1; } else if (length == 1) /*looks like an arg?*/ { if (PyUnicode_Check(farray[0]) || !pgLine_FromObject(farray[0], out)) { return 0; } - return IS_LINE_VALID(out); + return 1; } } if (PySequence_Check(obj)) { @@ -82,46 +80,46 @@ pgLine_FromObject(PyObject *obj, pgLineBase *out) if (length == 4) { PyObject *tmp; tmp = PySequence_GetItem(obj, 0); - if (!pg_DoubleFromObj(tmp, &(out->xa))) { + if (!pg_DoubleFromObj(tmp, &out->xa)) { Py_DECREF(tmp); return 0; } Py_DECREF(tmp); tmp = PySequence_GetItem(obj, 1); - if (!pg_DoubleFromObj(tmp, &(out->ya))) { + if (!pg_DoubleFromObj(tmp, &out->ya)) { Py_DECREF(tmp); return 0; } Py_DECREF(tmp); tmp = PySequence_GetItem(obj, 2); - if (!pg_DoubleFromObj(tmp, &(out->xb))) { + if (!pg_DoubleFromObj(tmp, &out->xb)) { Py_DECREF(tmp); return 0; } Py_DECREF(tmp); tmp = PySequence_GetItem(obj, 3); - if (!pg_DoubleFromObj(tmp, &(out->yb))) { + if (!pg_DoubleFromObj(tmp, &out->yb)) { Py_DECREF(tmp); return 0; } Py_DECREF(tmp); - return IS_LINE_VALID(out); + return 1; } else if (length == 2) { PyObject *tmp; tmp = PySequence_GetItem(obj, 0); - if (!pg_TwoDoublesFromObj(tmp, &(out->xa), &(out->ya))) { + if (!pg_TwoDoublesFromObj(tmp, &out->xa, &out->ya)) { Py_DECREF(tmp); return 0; } Py_DECREF(tmp); tmp = PySequence_GetItem(obj, 1); - if (!pg_TwoDoublesFromObj(tmp, &(out->xb), &(out->yb))) { + if (!pg_TwoDoublesFromObj(tmp, &out->xb, &out->yb)) { Py_DECREF(tmp); return 0; } Py_DECREF(tmp); - return IS_LINE_VALID(out); + return 1; } else if (PyTuple_Check(obj) && length == 1) /*looks like an arg?*/ { PyObject *sub = PySequence_GetItem(obj, 0); @@ -130,7 +128,7 @@ pgLine_FromObject(PyObject *obj, pgLineBase *out) return 0; } Py_DECREF(sub); - return IS_LINE_VALID(out); + return 1; } else { return 0; diff --git a/test/geometry_test.py b/test/geometry_test.py index 0ff8f97a48..cc0ae5084b 100644 --- a/test/geometry_test.py +++ b/test/geometry_test.py @@ -1693,6 +1693,40 @@ def testConstruction_invalid_type(self): with self.assertRaises(TypeError): Line(0, 1, 2, value) + # Test xa + for value in invalid_types: + with self.assertRaises(TypeError): + Line((value, 0), (1, 2)) + # Test ya + for value in invalid_types: + with self.assertRaises(TypeError): + Line((0, value), (1, 2)) + # Test xb + for value in invalid_types: + with self.assertRaises(TypeError): + Line((0, 0), (value, 2)) + # Test yb + for value in invalid_types: + with self.assertRaises(TypeError): + Line((0, 1), (2, value)) + + # Test xa + for value in invalid_types: + with self.assertRaises(TypeError): + Line(((value, 0), (1, 2))) + # Test ya + for value in invalid_types: + with self.assertRaises(TypeError): + Line(((0, value), (1, 2))) + # Test xb + for value in invalid_types: + with self.assertRaises(TypeError): + Line(((0, 0), (value, 2))) + # Test yb + for value in invalid_types: + with self.assertRaises(TypeError): + Line(((0, 1), (2, value))) + def testConstruction_invalid_arguments_number(self): """Checks whether passing the wrong number of arguments to the constructor raises the appropriate errors @@ -1777,61 +1811,6 @@ def testConstruction_class_with_line_function(self): self.assertEqual(line.xb, 3.3) self.assertEqual(line.yb, 4.4) - def testConstruction_degenerate(self): - """Ensures that you can't create degenerate lines (lines with zero length)""" - - # 4 args - with self.assertRaises(TypeError): - Line(1.0, 2.0, 1.0, 2.0) - with self.assertRaises(TypeError): - Line(1, 2, 1, 2) - - # 1 list arg 4 - with self.assertRaises(TypeError): - Line([1, 2, 1, 2]) - with self.assertRaises(TypeError): - Line([1.0, 2.0, 1.0, 2.0]) - - # 1 tuple arg 4 - with self.assertRaises(TypeError): - Line((1, 2, 1, 2)) - with self.assertRaises(TypeError): - Line((1.0, 2.0, 1.0, 2.0)) - - # two tuple args - with self.assertRaises(TypeError): - Line((1, 2), (1, 2)) - with self.assertRaises(TypeError): - Line((1.0, 2.0), (1.0, 2.0)) - - # two list args - with self.assertRaises(TypeError): - Line([1, 2], [1, 2]) - with self.assertRaises(TypeError): - Line([1.0, 2.0], [1.0, 2.0]) - - # one list two tuple args - with self.assertRaises(TypeError): - Line([1, 2], (1, 2)) - with self.assertRaises(TypeError): - Line((1, 2), [1, 2]) - with self.assertRaises(TypeError): - Line([1.0, 2.0], (1.0, 2.0)) - with self.assertRaises(TypeError): - Line((1.0, 2.0), [1.0, 2.0]) - - # one list two sub-tuples arg - with self.assertRaises(TypeError): - Line([(1, 2), (1, 2)]) - with self.assertRaises(TypeError): - Line([(1.0, 2.0), (1.0, 2.0)]) - - # one tuple two sub-lists arg - with self.assertRaises(TypeError): - Line(([1, 2], [1, 2])) - with self.assertRaises(TypeError): - Line(([1.0, 2.0], [1.0, 2.0])) - def test_attrib_x1(self): """a full test for the xa attribute""" expected_x1 = 10.0