From fc15fcda35ed86b26070c82f1aed684849e51317 Mon Sep 17 00:00:00 2001 From: Thomas Nipen Date: Mon, 11 Mar 2024 11:29:59 +0100 Subject: [PATCH] Use invalid_argument exception for invalid extrapolation policy Instead of runtime_error --- src/api/curve.cpp | 2 +- tests/test_apply_curve.py | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/api/curve.cpp b/src/api/curve.cpp index 481dd4fb..c7309ee0 100644 --- a/src/api/curve.cpp +++ b/src/api/curve.cpp @@ -63,7 +63,7 @@ float gridpp::apply_curve(float input, const vec& curve_ref, const vec& curve_fc slope = dObs / dFcst; } else { - throw std::runtime_error("Unknown extrapolation policy"); + throw std::invalid_argument("Unknown extrapolation policy"); } output = nearestObs + slope * (input - nearestFcst); } diff --git a/tests/test_apply_curve.py b/tests/test_apply_curve.py index ad780f23..839f6809 100644 --- a/tests/test_apply_curve.py +++ b/tests/test_apply_curve.py @@ -56,10 +56,18 @@ def test_3d(self): def test_all_extrapolation_policies(self): """Check that all policies work""" + curve_fcst = np.random.rand(3, 2, 4) + curve_ref = np.random.rand(3, 2, 4) + field = np.random.rand(3, 2) for policy in [gridpp.OneToOne, gridpp.Zero, gridpp.NearestSlope, gridpp.MeanSlope, gridpp.Unchanged]: - curve_fcst = np.random.rand(3, 2, 4) - curve_ref = np.random.rand(3, 2, 4) - field = np.random.rand(3, 2) + gridpp.apply_curve(field, curve_ref, curve_fcst, policy, policy) + + def test_invalid_extrapolation_policy(self): + policy = -1 + curve_fcst = np.random.rand(3, 2, 4) + curve_ref = np.random.rand(3, 2, 4) + field = np.random.rand(3, 2) + with self.assertRaises(ValueError) as e: gridpp.apply_curve(field, curve_ref, curve_fcst, policy, policy)