From e765b71c6aced0d5e90a85663dc40090ed5122cc Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Wed, 22 Jan 2020 14:25:07 -0600 Subject: [PATCH] [numerics] Add unit tests for tabulated Func1 --- interfaces/cython/cantera/test/test_func1.py | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/interfaces/cython/cantera/test/test_func1.py b/interfaces/cython/cantera/test/test_func1.py index b8672d45d42..e725a22073c 100644 --- a/interfaces/cython/cantera/test/test_func1.py +++ b/interfaces/cython/cantera/test/test_func1.py @@ -70,3 +70,44 @@ def test_uncopyable(self): f = ct.Func1(np.sin) with self.assertRaises(NotImplementedError): copy.copy(f) + + def test_tabulated1(self): + arr = np.array([[0, 2], [1, 1], [2, 0]]) + time = arr[:, 0] + fval = arr[:, 1] + fcn = ct.Func1(time, fval) + for t, f in zip(time, fval): + self.assertNear(f, fcn(t)) + + def test_tabulated2(self): + time = [0, 1, 2] + fval = [2, 1, 0] + fcn = ct.Func1(time, fval) + for t, f in zip(time, fval): + self.assertNear(f, fcn(t)) + + def test_tabulated3(self): + time = np.array([0, 1, 2]) + fval = np.array([2, 1, 0]) + fcn = ct.Func1(time, fval) + tt = .5*(time[1:] + time[:-1]) + ff = .5*(fval[1:] + fval[:-1]) + for t, f in zip(tt, ff): + self.assertNear(f, fcn(t)) + + def test_tabulated4(self): + time = 0, 1, 2, + fval = 2, 1, 0, + fcn = ct.Func1(time, fval) + self.assertNear(fcn(-1), fval[0]) + self.assertNear(fcn(3), fval[-1]) + + def test_failures(self): + with self.assertRaisesRegex(ValueError, 'Invalid number of arguments'): + ct.Func1(1, 2, 3) + with self.assertRaisesRegex(ValueError, 'Invalid dimensions'): + ct.Func1(np.zeros((3, 3))) + with self.assertRaisesRegex(ct.CanteraError, 'do not match'): + ct.Func1(range(2), range(3)) + with self.assertRaisesRegex(ct.CanteraError, 'must not be empty'): + ct.Func1([], [])