From b6744a5c6b956c3325fb48f1fcaf12a56a8bc3e3 Mon Sep 17 00:00:00 2001 From: Joe Wallwork Date: Wed, 12 Jun 2024 13:38:40 +0100 Subject: [PATCH 1/2] Add error check test --- test_maths.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test_maths.py b/test_maths.py index b74db98..5f6a1a7 100644 --- a/test_maths.py +++ b/test_maths.py @@ -17,3 +17,8 @@ def test_equation(a, b, condition): iszero = np.isclose(f(x, y), 0) if (not iszero if condition(x, y) else iszero): raise AssertionError(f"f({x},{y}) {'!=' if condition(x, y) else '=='} 0") + + +def test_single_point(): + with pytest.raises(ValueError): + equation_of_line((0, 0), (0, 0)) From a92866dcb25a653476daf1d1877fc34796d006f1 Mon Sep 17 00:00:00 2001 From: Joe Wallwork Date: Wed, 12 Jun 2024 13:39:46 +0100 Subject: [PATCH 2/2] Raise error if points are identical --- maths.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/maths.py b/maths.py index 6bd8cd5..5ea2e16 100644 --- a/maths.py +++ b/maths.py @@ -1,3 +1,6 @@ +import numpy as np + + def equation_of_line(a, b): """ Determine the equation of the line passing through two points. @@ -6,6 +9,8 @@ def equation_of_line(a, b): :arg b: the second point the line passes through :return: function of two variables defining the line """ + if np.allclose(a, b): + raise ValueError("Cannot determine a unique line through {a} and {b}.") x0, y0 = a x1, y1 = b