Skip to content

Commit

Permalink
Now accepting degenerate lines, added some more __init__ tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
itzpr3d4t0r committed Oct 6, 2024
1 parent 7c5310f commit 502cc89
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 75 deletions.
38 changes: 18 additions & 20 deletions src_c/line.c
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -53,75 +51,75 @@ 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)) {
length = PySequence_Length(obj);
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);
Expand All @@ -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;
Expand Down
89 changes: 34 additions & 55 deletions test/geometry_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 502cc89

Please sign in to comment.