diff options
author | Hernan Grecco <hernan.grecco@gmail.com> | 2023-04-24 11:19:37 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-24 11:19:37 -0300 |
commit | 3acc6e0667d8e6c21a908f56e11ec357ac07dad7 (patch) | |
tree | 98481703fe78ab4791aa9143258f4ade85eda02d /pint/testsuite | |
parent | 781307d0781f5bc8a7a7088cad333ff9bd508531 (diff) | |
parent | e0a8c221bfe7c83f5d6fcbe2e847724a41cf61a6 (diff) | |
download | pint-3acc6e0667d8e6c21a908f56e11ec357ac07dad7.tar.gz |
Merge branch 'master' into fix-trailing-zeros-decimal
Diffstat (limited to 'pint/testsuite')
-rw-r--r-- | pint/testsuite/baseline/test_basic_plot.png | bin | 17483 -> 17415 bytes | |||
-rw-r--r-- | pint/testsuite/baseline/test_plot_with_non_default_format.png | bin | 0 -> 16617 bytes | |||
-rw-r--r-- | pint/testsuite/baseline/test_plot_with_set_units.png | bin | 18145 -> 18176 bytes | |||
-rw-r--r-- | pint/testsuite/test_dask.py | 2 | ||||
-rw-r--r-- | pint/testsuite/test_matplotlib.py | 18 | ||||
-rw-r--r-- | pint/testsuite/test_numpy.py | 32 | ||||
-rw-r--r-- | pint/testsuite/test_pint_eval.py | 74 |
7 files changed, 122 insertions, 4 deletions
diff --git a/pint/testsuite/baseline/test_basic_plot.png b/pint/testsuite/baseline/test_basic_plot.png Binary files differindex 63be609..b0c4d18 100644 --- a/pint/testsuite/baseline/test_basic_plot.png +++ b/pint/testsuite/baseline/test_basic_plot.png diff --git a/pint/testsuite/baseline/test_plot_with_non_default_format.png b/pint/testsuite/baseline/test_plot_with_non_default_format.png Binary files differnew file mode 100644 index 0000000..1cb5b18 --- /dev/null +++ b/pint/testsuite/baseline/test_plot_with_non_default_format.png diff --git a/pint/testsuite/baseline/test_plot_with_set_units.png b/pint/testsuite/baseline/test_plot_with_set_units.png Binary files differindex 5fd3ce0..a59924c 100644 --- a/pint/testsuite/baseline/test_plot_with_set_units.png +++ b/pint/testsuite/baseline/test_plot_with_set_units.png diff --git a/pint/testsuite/test_dask.py b/pint/testsuite/test_dask.py index 69c80fe..f4dee6a 100644 --- a/pint/testsuite/test_dask.py +++ b/pint/testsuite/test_dask.py @@ -149,6 +149,8 @@ def test_compute_persist_equivalent(local_registry, dask_array, numpy_array): assert np.all(res_compute == res_persist) assert res_compute.units == res_persist.units == units_ + assert type(res_compute) == local_registry.Quantity + assert type(res_persist) == local_registry.Quantity @pytest.mark.parametrize("method", ["compute", "persist", "visualize"]) diff --git a/pint/testsuite/test_matplotlib.py b/pint/testsuite/test_matplotlib.py index 25f3172..0735721 100644 --- a/pint/testsuite/test_matplotlib.py +++ b/pint/testsuite/test_matplotlib.py @@ -46,3 +46,21 @@ def test_plot_with_set_units(local_registry): ax.axvline(120 * local_registry.minutes, color="tab:green") return fig + + +@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True) +def test_plot_with_non_default_format(local_registry): + local_registry.mpl_formatter = "{:~P}" + + y = np.linspace(0, 30) * local_registry.miles + x = np.linspace(0, 5) * local_registry.hours + + fig, ax = plt.subplots() + ax.yaxis.set_units(local_registry.inches) + ax.xaxis.set_units(local_registry.seconds) + + ax.plot(x, y, "tab:blue") + ax.axhline(26400 * local_registry.feet, color="tab:red") + ax.axvline(120 * local_registry.minutes, color="tab:green") + + return fig diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index 83448ce..f0f95bc 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -806,7 +806,7 @@ class TestNumpyUnclassified(TestNumpyMethods): np.around(1.0275 * self.ureg.m, decimals=2), 1.03 * self.ureg.m ) helpers.assert_quantity_equal( - np.round_(1.0275 * self.ureg.m, decimals=2), 1.03 * self.ureg.m + np.round(1.0275 * self.ureg.m, decimals=2), 1.03 * self.ureg.m ) def test_trace(self): @@ -1050,7 +1050,7 @@ class TestNumpyUnclassified(TestNumpyMethods): np.isclose(self.q, q2), np.array([[False, True], [True, False]]) ) self.assertNDArrayEqual( - np.isclose(self.q, q2, atol=1e-5, rtol=1e-7), + np.isclose(self.q, q2, atol=1e-5 * self.ureg.mm, rtol=1e-7), np.array([[False, True], [True, False]]), ) @@ -1222,6 +1222,24 @@ class TestNumpyUnclassified(TestNumpyMethods): np.array([[1, 0, 2], [3, 0, 4]]) * self.ureg.m, ) + @helpers.requires_array_function_protocol() + def test_delete(self): + q = self.Q_(np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]), "m") + helpers.assert_quantity_equal( + np.delete(q, 1, axis=0), + np.array([[1, 2, 3, 4], [9, 10, 11, 12]]) * self.ureg.m, + ) + + helpers.assert_quantity_equal( + np.delete(q, np.s_[::2], 1), + np.array([[2, 4], [6, 8], [10, 12]]) * self.ureg.m, + ) + + helpers.assert_quantity_equal( + np.delete(q, [1, 3, 5], None), + np.array([1, 3, 5, 7, 8, 9, 10, 11, 12]) * self.ureg.m, + ) + def test_ndarray_downcast(self): with pytest.warns(UnitStrippedWarning): np.asarray(self.q) @@ -1355,6 +1373,16 @@ class TestNumpyUnclassified(TestNumpyMethods): assert not np.allclose( [1e10, 1e-8] * self.ureg.m, [1.00001e10, 1e-9] * self.ureg.mm ) + assert np.allclose( + [1e10, 1e-8] * self.ureg.m, + [1.00001e10, 1e-9] * self.ureg.m, + atol=1e-8 * self.ureg.m, + ) + + with pytest.raises(DimensionalityError): + assert np.allclose( + [1e10, 1e-8] * self.ureg.m, [1.00001e10, 1e-9] * self.ureg.m, atol=1e-8 + ) @helpers.requires_array_function_protocol() def test_intersect1d(self): diff --git a/pint/testsuite/test_pint_eval.py b/pint/testsuite/test_pint_eval.py index bed8105..b5b94f0 100644 --- a/pint/testsuite/test_pint_eval.py +++ b/pint/testsuite/test_pint_eval.py @@ -2,10 +2,13 @@ import pytest from pint.compat import tokenizer from pint.pint_eval import build_eval_tree +from pint.util import string_preprocessor class TestPintEval: - def _test_one(self, input_text, parsed): + def _test_one(self, input_text, parsed, preprocess=False): + if preprocess: + input_text = string_preprocessor(input_text) assert build_eval_tree(tokenizer(input_text)).to_string() == parsed @pytest.mark.parametrize( @@ -13,6 +16,7 @@ class TestPintEval: ( ("3", "3"), ("1 + 2", "(1 + 2)"), + ("1 - 2", "(1 - 2)"), ("2 * 3 + 4", "((2 * 3) + 4)"), # order of operations ("2 * (3 + 4)", "(2 * (3 + 4))"), # parentheses ( @@ -71,4 +75,70 @@ class TestPintEval: ), ) def test_build_eval_tree(self, input_text, parsed): - self._test_one(input_text, parsed) + self._test_one(input_text, parsed, preprocess=False) + + @pytest.mark.parametrize( + ("input_text", "parsed"), + ( + ("3", "3"), + ("1 + 2", "(1 + 2)"), + ("1 - 2", "(1 - 2)"), + ("2 * 3 + 4", "((2 * 3) + 4)"), # order of operations + ("2 * (3 + 4)", "(2 * (3 + 4))"), # parentheses + ( + "1 + 2 * 3 ** (4 + 3 / 5)", + "(1 + (2 * (3 ** (4 + (3 / 5)))))", + ), # more order of operations + ( + "1 * ((3 + 4) * 5)", + "(1 * ((3 + 4) * 5))", + ), # nested parentheses at beginning + ("1 * (5 * (3 + 4))", "(1 * (5 * (3 + 4)))"), # nested parentheses at end + ( + "1 * (5 * (3 + 4) / 6)", + "(1 * ((5 * (3 + 4)) / 6))", + ), # nested parentheses in middle + ("-1", "(- 1)"), # unary + ("3 * -1", "(3 * (- 1))"), # unary + ("3 * --1", "(3 * (- (- 1)))"), # double unary + ("3 * -(2 + 4)", "(3 * (- (2 + 4)))"), # parenthetical unary + ("3 * -((2 + 4))", "(3 * (- (2 + 4)))"), # parenthetical unary + # implicit op + ("3 4", "(3 * 4)"), + # implicit op, then parentheses + ("3 (2 + 4)", "(3 * (2 + 4))"), + # parentheses, then implicit + ("(3 ** 4 ) 5", "((3 ** 4) * 5)"), + # implicit op, then exponentiation + ("3 4 ** 5", "(3 * (4 ** 5))"), + # implicit op, then addition + ("3 4 + 5", "((3 * 4) + 5)"), + # power followed by implicit + ("3 ** 4 5", "((3 ** 4) * 5)"), + # implicit with parentheses + ("3 (4 ** 5)", "(3 * (4 ** 5))"), + # exponent with e + ("3e-1", "3e-1"), + # multiple units with exponents + ("kg ** 1 * s ** 2", "((kg ** 1) * (s ** 2))"), + # multiple units with neg exponents + ("kg ** -1 * s ** -2", "((kg ** (- 1)) * (s ** (- 2)))"), + # multiple units with neg exponents + ("kg^-1 * s^-2", "((kg ** (- 1)) * (s ** (- 2)))"), + # multiple units with neg exponents, implicit op + ("kg^-1 s^-2", "((kg ** (- 1)) * (s ** (- 2)))"), + # nested power + ("2 ^ 3 ^ 2", "(2 ** (3 ** 2))"), + # nested power + ("gram * second / meter ** 2", "((gram * second) / (meter ** 2))"), + # nested power + ("gram / meter ** 2 / second", "((gram / (meter ** 2)) / second)"), + # units should behave like numbers, so we don't need a bunch of extra tests for them + # implicit op, then addition + ("3 kg + 5", "((3 * kg) + 5)"), + ("(5 % 2) m", "((5 % 2) * m)"), # mod operator + ("(5 // 2) m", "((5 // 2) * m)"), # floordiv operator + ), + ) + def test_preprocessed_eval_tree(self, input_text, parsed): + self._test_one(input_text, parsed, preprocess=True) |