From 6e5b3b44dc84f79fb1c7dec7022e81146b2a908d Mon Sep 17 00:00:00 2001 From: Jonathan Rocher Date: Wed, 17 Apr 2019 15:48:20 -0500 Subject: [PATCH 1/6] Add assertion and test functions to compare 2 UnitScalars or 2 UnitArrays. --- scimath/units/compare_units.py | 72 ++++++++++++++ scimath/units/testing/__init__.py | 0 scimath/units/testing/assertion_utils.py | 40 ++++++++ .../testing/tests/test_assertion_utils.py | 25 +++++ scimath/units/tests/test_compare_units.py | 96 +++++++++++++++++++ 5 files changed, 233 insertions(+) create mode 100644 scimath/units/compare_units.py create mode 100644 scimath/units/testing/__init__.py create mode 100644 scimath/units/testing/assertion_utils.py create mode 100644 scimath/units/testing/tests/test_assertion_utils.py create mode 100644 scimath/units/tests/test_compare_units.py diff --git a/scimath/units/compare_units.py b/scimath/units/compare_units.py new file mode 100644 index 0000000..33ccf05 --- /dev/null +++ b/scimath/units/compare_units.py @@ -0,0 +1,72 @@ +""" Utilities around unit conversion and unit management. +""" +from six import string_types +import numpy as np + +from scimath.units.api import convert, dimensionless, UnitArray, UnitScalar +from scimath.units.unit import InvalidConversion + + +def unit_scalars_almost_equal(x1, x2, eps=1e-9): + """ Returns whether 2 UnitScalars are almost equal. + + Parameters + ---------- + x1 : UnitScalar + First unit scalar to compare. + + x2 : UnitScalar + Second unit scalar to compare. + + eps : float + Absolute precision of the comparison. + """ + if not isinstance(x1, UnitScalar): + msg = "x1 is supposed to be a UnitScalar but a {} was passed." + msg = msg.format(type(x1)) + raise ValueError(msg) + + if not isinstance(x2, UnitScalar): + msg = "x2 is supposed to be a UnitScalar but a {} was passed." + msg = msg.format(type(x2)) + raise ValueError(msg) + + a1 = float(x1) + try: + a2 = convert(float(x2), from_unit=x2.units, to_unit=x1.units) + except InvalidConversion: + return False + return np.abs(a1 - a2) < eps + + +def unit_arrays_almost_equal(uarr1, uarr2, eps=1e-9): + """ Returns whether 2 UnitArrays are almost equal. + + Parameters + ---------- + uarr1 : UnitArray + First unit array to compare. + + uarr2 : UnitArray + Second unit array to compare. + + eps : float + Absolute precision of the comparison. + """ + if not isinstance(uarr1, UnitArray): + msg = "uarr1 is supposed to be a UnitArray but a {} was passed." + msg = msg.format(type(uarr1)) + raise ValueError(msg) + + if not isinstance(uarr2, UnitArray): + msg = "uarr2 is supposed to be a UnitArray but a {} was passed." + msg = msg.format(type(uarr2)) + raise ValueError(msg) + + a1 = np.array(uarr1) + try: + a2 = convert(np.array(uarr2), from_unit=uarr2.units, + to_unit=uarr1.units) + except InvalidConversion: + return False + return np.all(np.abs(a1 - a2) < eps) diff --git a/scimath/units/testing/__init__.py b/scimath/units/testing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scimath/units/testing/assertion_utils.py b/scimath/units/testing/assertion_utils.py new file mode 100644 index 0000000..993ed92 --- /dev/null +++ b/scimath/units/testing/assertion_utils.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +""" Utilities providing assertions to support unit tests involving UnitScalars +and UnitArrays. +""" +from nose.tools import assert_false, assert_true + +from scimath.units.compare_units import unit_arrays_almost_equal, \ + unit_scalars_almost_equal + + +def assert_unit_scalar_almost_equal(val1, val2, eps=1.e-9, msg=None): + if msg is None: + msg = "{} and {} are not almost equal with precision {}" + msg = msg.format(val1, val2, eps) + + assert_true(unit_scalars_almost_equal(val1, val2, eps=eps), msg=msg) + + +def assert_unit_scalar_not_almost_equal(val1, val2, eps=1.e-9, msg=None): + if msg is None: + msg = "{} and {} unexpectedly almost equal with precision {}" + msg = msg.format(val1, val2, eps) + + assert_false(unit_scalars_almost_equal(val1, val2, eps=eps), msg=msg) + + +def assert_unit_array_almost_equal(uarr1, uarr2, eps=1e-9, msg=None): + if msg is None: + msg = "{} and {} are not almost equal with precision {}" + msg = msg.format(uarr1, uarr2, eps) + + assert_true(unit_arrays_almost_equal(uarr1, uarr2, eps=eps), msg=msg) + + +def assert_unit_array_not_almost_equal(uarr1, uarr2, eps=1e-9, msg=None): + if msg is None: + msg = "{} and {} are almost equal with precision {}" + msg = msg.format(uarr1, uarr2, eps) + + assert_false(unit_arrays_almost_equal(uarr1, uarr2, eps=eps), msg=msg) diff --git a/scimath/units/testing/tests/test_assertion_utils.py b/scimath/units/testing/tests/test_assertion_utils.py new file mode 100644 index 0000000..57b0306 --- /dev/null +++ b/scimath/units/testing/tests/test_assertion_utils.py @@ -0,0 +1,25 @@ +from unittest import TestCase + +from scimath.units.api import UnitArray, UnitScalar +from scimath.units.testing.assertion_utils import \ + assert_unit_array_almost_equal, assert_unit_scalar_almost_equal + + +class TestAssertUnitScalarEqual(TestCase): + def test_same_unit_scalar(self): + assert_unit_scalar_almost_equal(UnitScalar(1, units="s"), + UnitScalar(1, units="s")) + + def test_equivalent_unit_scalar(self): + assert_unit_scalar_almost_equal(UnitScalar(1, units="m"), + UnitScalar(100, units="cm")) + + +class TestAssertUnitArrayEqual(TestCase): + def test_same_unit_array(self): + assert_unit_array_almost_equal(UnitArray([1, 2], units="s"), + UnitArray([1, 2], units="s")) + + def test_equivalent_unit_array(self): + assert_unit_array_almost_equal(UnitArray([1, 2], units="m"), + UnitArray([100, 200], units="cm")) diff --git a/scimath/units/tests/test_compare_units.py b/scimath/units/tests/test_compare_units.py new file mode 100644 index 0000000..12142cd --- /dev/null +++ b/scimath/units/tests/test_compare_units.py @@ -0,0 +1,96 @@ +from unittest import TestCase + +from scimath.units.api import dimensionless, UnitArray, UnitScalar +from scimath.units.length import meter + +from scimath.units.compare_units import unit_arrays_almost_equal, \ + unit_scalars_almost_equal + + +class TestUnitScalarAlmostEqual(TestCase): + def test_values_identical(self): + val1 = UnitScalar(1., units="m") + self.assertTrue(unit_scalars_almost_equal(val1, val1)) + + def test_wrong_type1(self): + val1 = 1 + val2 = UnitScalar(1., units="m") + with self.assertRaises(ValueError): + unit_scalars_almost_equal(val1, val2) + + def test_wrong_type2(self): + val1 = UnitScalar(1., units="m") + val2 = 1 + with self.assertRaises(ValueError): + unit_scalars_almost_equal(val1, val2) + + def test_values_identical_in_diff_units(self): + val1 = UnitScalar(1., units="m") + val2 = UnitScalar(100., units="cm") + self.assertTrue(unit_scalars_almost_equal(val1, val2)) + + def test_dimensionless(self): + val1 = UnitScalar(1., units=dimensionless) + val2 = UnitScalar(1., units="cm") + self.assertFalse(unit_scalars_almost_equal(val1, val2)) + + def test_2_dimensionless(self): + val1 = UnitScalar(1., units=dimensionless) + val2 = UnitScalar(1., units="BLAH") + val3 = UnitScalar(100., units="BLAH") + self.assertTrue(unit_scalars_almost_equal(val1, val1)) + self.assertTrue(unit_scalars_almost_equal(val1, val2)) + self.assertFalse(unit_scalars_almost_equal(val1, val3)) + + def test_values_close_enough(self): + val1 = UnitScalar(1., units="m") + val2 = val1 + UnitScalar(1.e-5, units="m") + self.assertFalse(unit_scalars_almost_equal(val1, val2)) + self.assertTrue(unit_scalars_almost_equal(val1, val2, eps=1e-4)) + + +class TestUnitArraysAlmostEqual(TestCase): + def test_wrong_type1(self): + val1 = 1 + val2 = UnitScalar(1., units="m") + with self.assertRaises(ValueError): + unit_scalars_almost_equal(val1, val2) + + def test_wrong_type2(self): + val1 = UnitScalar(1., units="m") + val2 = 1 + with self.assertRaises(ValueError): + unit_scalars_almost_equal(val1, val2) + + def test_values_identical(self): + val1 = UnitArray([1., 2.], units="m") + self.assertTrue(unit_arrays_almost_equal(val1, val1)) + + def test_values_identical_in_diff_units(self): + val1 = UnitArray([1., 2.], units="m") + val2 = UnitArray([100., 200.], units="cm") + self.assertTrue(unit_arrays_almost_equal(val1, val2)) + + def test_dimensionless(self): + val1 = UnitArray([1.], units=dimensionless) + val2 = UnitArray([1.], units="cm") + self.assertFalse(unit_scalars_almost_equal(val1, val2)) + + def test_2_dimensionless(self): + val1 = UnitArray([1.], units=dimensionless) + val2 = UnitArray([1.], units="BLAH") + val3 = UnitArray([100.], units="BLAH") + self.assertTrue(unit_scalars_almost_equal(val1, val1)) + self.assertTrue(unit_scalars_almost_equal(val1, val2)) + self.assertFalse(unit_scalars_almost_equal(val1, val3)) + + def test_values_close_enough(self): + val1 = UnitArray([1., 2.], units="m") + val2 = val1 + UnitArray([1.e-5, 1.e-6], units="m") + self.assertFalse(unit_arrays_almost_equal(val1, val2)) + self.assertTrue(unit_arrays_almost_equal(val1, val2, eps=1e-4)) + + def test_values_not_close_enough(self): + val1 = UnitArray([1., 2.], units="m") + val3 = val1 + UnitArray([1.e-2, 1.e-6], units="m") + self.assertFalse(unit_arrays_almost_equal(val1, val3, eps=1e-4)) From 9279ca7b79a70bce51ab2da4f68cb5978a50fb6d Mon Sep 17 00:00:00 2001 From: Jonathan Rocher Date: Wed, 17 Apr 2019 15:52:09 -0500 Subject: [PATCH 2/6] Flake8 --- scimath/units/compare_units.py | 5 ++--- scimath/units/tests/test_compare_units.py | 2 -- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/scimath/units/compare_units.py b/scimath/units/compare_units.py index 33ccf05..c039d88 100644 --- a/scimath/units/compare_units.py +++ b/scimath/units/compare_units.py @@ -1,9 +1,8 @@ -""" Utilities around unit conversion and unit management. +""" Utilities around unit comparisons. """ -from six import string_types import numpy as np -from scimath.units.api import convert, dimensionless, UnitArray, UnitScalar +from scimath.units.api import convert, UnitArray, UnitScalar from scimath.units.unit import InvalidConversion diff --git a/scimath/units/tests/test_compare_units.py b/scimath/units/tests/test_compare_units.py index 12142cd..f4c36ce 100644 --- a/scimath/units/tests/test_compare_units.py +++ b/scimath/units/tests/test_compare_units.py @@ -1,8 +1,6 @@ from unittest import TestCase from scimath.units.api import dimensionless, UnitArray, UnitScalar -from scimath.units.length import meter - from scimath.units.compare_units import unit_arrays_almost_equal, \ unit_scalars_almost_equal From a6093410107187675faf83571517125e07d9d26e Mon Sep 17 00:00:00 2001 From: Jonathan Rocher Date: Wed, 17 Apr 2019 15:58:39 -0500 Subject: [PATCH 3/6] Clean up some tests. --- scimath/units/tests/test_compare_units.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/scimath/units/tests/test_compare_units.py b/scimath/units/tests/test_compare_units.py index f4c36ce..94c4d44 100644 --- a/scimath/units/tests/test_compare_units.py +++ b/scimath/units/tests/test_compare_units.py @@ -50,15 +50,15 @@ def test_values_close_enough(self): class TestUnitArraysAlmostEqual(TestCase): def test_wrong_type1(self): val1 = 1 - val2 = UnitScalar(1., units="m") + val2 = UnitArray([1.], units="m") with self.assertRaises(ValueError): - unit_scalars_almost_equal(val1, val2) + unit_arrays_almost_equal(val1, val2) def test_wrong_type2(self): - val1 = UnitScalar(1., units="m") + val1 = UnitArray([1.], units="m") val2 = 1 with self.assertRaises(ValueError): - unit_scalars_almost_equal(val1, val2) + unit_arrays_almost_equal(val1, val2) def test_values_identical(self): val1 = UnitArray([1., 2.], units="m") @@ -72,15 +72,15 @@ def test_values_identical_in_diff_units(self): def test_dimensionless(self): val1 = UnitArray([1.], units=dimensionless) val2 = UnitArray([1.], units="cm") - self.assertFalse(unit_scalars_almost_equal(val1, val2)) + self.assertFalse(unit_arrays_almost_equal(val1, val2)) def test_2_dimensionless(self): val1 = UnitArray([1.], units=dimensionless) val2 = UnitArray([1.], units="BLAH") val3 = UnitArray([100.], units="BLAH") - self.assertTrue(unit_scalars_almost_equal(val1, val1)) - self.assertTrue(unit_scalars_almost_equal(val1, val2)) - self.assertFalse(unit_scalars_almost_equal(val1, val3)) + self.assertTrue(unit_arrays_almost_equal(val1, val1)) + self.assertTrue(unit_arrays_almost_equal(val1, val2)) + self.assertFalse(unit_arrays_almost_equal(val1, val3)) def test_values_close_enough(self): val1 = UnitArray([1., 2.], units="m") From 3692c4f7b87883748cd05fb077bcda9a70f7f246 Mon Sep 17 00:00:00 2001 From: Jonathan Rocher Date: Sun, 28 Apr 2019 17:15:56 -0500 Subject: [PATCH 4/6] Simplify the folder structure and import path. --- scimath/units/{testing => }/assertion_utils.py | 0 scimath/units/{testing => }/tests/test_assertion_utils.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename scimath/units/{testing => }/assertion_utils.py (100%) rename scimath/units/{testing => }/tests/test_assertion_utils.py (100%) diff --git a/scimath/units/testing/assertion_utils.py b/scimath/units/assertion_utils.py similarity index 100% rename from scimath/units/testing/assertion_utils.py rename to scimath/units/assertion_utils.py diff --git a/scimath/units/testing/tests/test_assertion_utils.py b/scimath/units/tests/test_assertion_utils.py similarity index 100% rename from scimath/units/testing/tests/test_assertion_utils.py rename to scimath/units/tests/test_assertion_utils.py From 640b0cb5fa12dfdbed2264b4045c5a1207a17564 Mon Sep 17 00:00:00 2001 From: Jonathan Rocher Date: Sun, 28 Apr 2019 17:41:45 -0500 Subject: [PATCH 5/6] Change precision argument meaning from absolute to relative. Add a shape check. --- scimath/units/assertion_utils.py | 16 +++++------ scimath/units/compare_units.py | 28 ++++++++++++------ scimath/units/tests/test_compare_units.py | 35 ++++++++++++++++++----- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/scimath/units/assertion_utils.py b/scimath/units/assertion_utils.py index 993ed92..1be4073 100644 --- a/scimath/units/assertion_utils.py +++ b/scimath/units/assertion_utils.py @@ -8,33 +8,33 @@ unit_scalars_almost_equal -def assert_unit_scalar_almost_equal(val1, val2, eps=1.e-9, msg=None): +def assert_unit_scalar_almost_equal(val1, val2, rtol=1.e-9, msg=None): if msg is None: msg = "{} and {} are not almost equal with precision {}" msg = msg.format(val1, val2, eps) - assert_true(unit_scalars_almost_equal(val1, val2, eps=eps), msg=msg) + assert_true(unit_scalars_almost_equal(val1, val2, rtol=rtol), msg=msg) -def assert_unit_scalar_not_almost_equal(val1, val2, eps=1.e-9, msg=None): +def assert_unit_scalar_not_almost_equal(val1, val2, rtol=1.e-9, msg=None): if msg is None: msg = "{} and {} unexpectedly almost equal with precision {}" msg = msg.format(val1, val2, eps) - assert_false(unit_scalars_almost_equal(val1, val2, eps=eps), msg=msg) + assert_false(unit_scalars_almost_equal(val1, val2, rtol=rtol), msg=msg) -def assert_unit_array_almost_equal(uarr1, uarr2, eps=1e-9, msg=None): +def assert_unit_array_almost_equal(uarr1, uarr2, rtol=1e-9, msg=None): if msg is None: msg = "{} and {} are not almost equal with precision {}" msg = msg.format(uarr1, uarr2, eps) - assert_true(unit_arrays_almost_equal(uarr1, uarr2, eps=eps), msg=msg) + assert_true(unit_arrays_almost_equal(uarr1, uarr2, rtol=rtol), msg=msg) -def assert_unit_array_not_almost_equal(uarr1, uarr2, eps=1e-9, msg=None): +def assert_unit_array_not_almost_equal(uarr1, uarr2, rtol=1e-9, msg=None): if msg is None: msg = "{} and {} are almost equal with precision {}" msg = msg.format(uarr1, uarr2, eps) - assert_false(unit_arrays_almost_equal(uarr1, uarr2, eps=eps), msg=msg) + assert_false(unit_arrays_almost_equal(uarr1, uarr2, rtol=rtol), msg=msg) diff --git a/scimath/units/compare_units.py b/scimath/units/compare_units.py index c039d88..094bc09 100644 --- a/scimath/units/compare_units.py +++ b/scimath/units/compare_units.py @@ -6,9 +6,12 @@ from scimath.units.unit import InvalidConversion -def unit_scalars_almost_equal(x1, x2, eps=1e-9): +def unit_scalars_almost_equal(x1, x2, rtol=1e-9): """ Returns whether 2 UnitScalars are almost equal. + More precisely, what is tested is whether abs(a1-a2) < rtol*abs(a2), where + a1=float(x1) and a2=float(x2) after conversion to x1's units. + Parameters ---------- x1 : UnitScalar @@ -17,8 +20,8 @@ def unit_scalars_almost_equal(x1, x2, eps=1e-9): x2 : UnitScalar Second unit scalar to compare. - eps : float - Absolute precision of the comparison. + rtol : float + Relative precision of the comparison. """ if not isinstance(x1, UnitScalar): msg = "x1 is supposed to be a UnitScalar but a {} was passed." @@ -35,11 +38,14 @@ def unit_scalars_almost_equal(x1, x2, eps=1e-9): a2 = convert(float(x2), from_unit=x2.units, to_unit=x1.units) except InvalidConversion: return False - return np.abs(a1 - a2) < eps + return np.abs(a1 - a2) < np.abs(rtol * a2) + +def unit_arrays_almost_equal(uarr1, uarr2, rtol=1e-9): + """ Returns whether 2 UnitArrays are almost equal (must be the same shape). -def unit_arrays_almost_equal(uarr1, uarr2, eps=1e-9): - """ Returns whether 2 UnitArrays are almost equal. + More precisely, what is tested is whether abs(a1-a2) < rtol*abs(a2) for all + values in the arrays, once uarr2 has been converted to uarr1's units. Parameters ---------- @@ -49,8 +55,8 @@ def unit_arrays_almost_equal(uarr1, uarr2, eps=1e-9): uarr2 : UnitArray Second unit array to compare. - eps : float - Absolute precision of the comparison. + rtol : float + Relative precision of the comparison. """ if not isinstance(uarr1, UnitArray): msg = "uarr1 is supposed to be a UnitArray but a {} was passed." @@ -62,10 +68,14 @@ def unit_arrays_almost_equal(uarr1, uarr2, eps=1e-9): msg = msg.format(type(uarr2)) raise ValueError(msg) + if uarr1.shape != uarr2.shape: + return False + a1 = np.array(uarr1) try: a2 = convert(np.array(uarr2), from_unit=uarr2.units, to_unit=uarr1.units) except InvalidConversion: return False - return np.all(np.abs(a1 - a2) < eps) + + return np.all(np.abs(a1 - a2) < np.abs(rtol * a2)) diff --git a/scimath/units/tests/test_compare_units.py b/scimath/units/tests/test_compare_units.py index 94c4d44..3784315 100644 --- a/scimath/units/tests/test_compare_units.py +++ b/scimath/units/tests/test_compare_units.py @@ -10,18 +10,26 @@ def test_values_identical(self): val1 = UnitScalar(1., units="m") self.assertTrue(unit_scalars_almost_equal(val1, val1)) - def test_wrong_type1(self): + def test_wrong_arg_type1(self): val1 = 1 val2 = UnitScalar(1., units="m") with self.assertRaises(ValueError): unit_scalars_almost_equal(val1, val2) - def test_wrong_type2(self): + def test_wrong_arg_type2(self): val1 = UnitScalar(1., units="m") val2 = 1 with self.assertRaises(ValueError): unit_scalars_almost_equal(val1, val2) + def test_values_not_close(self): + val1 = UnitScalar(1., units="m") + val2 = UnitScalar(1.1, units="m") + self.assertFalse(unit_scalars_almost_equal(val1, val2)) + + val2 = UnitScalar(1.00001, units="m") + self.assertFalse(unit_scalars_almost_equal(val1, val2)) + def test_values_identical_in_diff_units(self): val1 = UnitScalar(1., units="m") val2 = UnitScalar(100., units="cm") @@ -44,22 +52,35 @@ def test_values_close_enough(self): val1 = UnitScalar(1., units="m") val2 = val1 + UnitScalar(1.e-5, units="m") self.assertFalse(unit_scalars_almost_equal(val1, val2)) - self.assertTrue(unit_scalars_almost_equal(val1, val2, eps=1e-4)) + self.assertTrue(unit_scalars_almost_equal(val1, val2, rtol=1e-4)) class TestUnitArraysAlmostEqual(TestCase): - def test_wrong_type1(self): + def test_wrong_argument_type1(self): val1 = 1 val2 = UnitArray([1.], units="m") with self.assertRaises(ValueError): unit_arrays_almost_equal(val1, val2) - def test_wrong_type2(self): + def test_wrong_argument_type2(self): val1 = UnitArray([1.], units="m") val2 = 1 with self.assertRaises(ValueError): unit_arrays_almost_equal(val1, val2) + def test_different_shape(self): + val1 = UnitArray([1.], units="m") + val2 = UnitArray([1., 2.], units="m") + self.assertFalse(unit_arrays_almost_equal(val1, val2)) + + def test_not_close_default_rtol(self): + val1 = UnitArray([1., 2.], units="m") + val2 = UnitArray([1., 2.1], units="m") + self.assertFalse(unit_arrays_almost_equal(val1, val2)) + + val2 = UnitArray([1., 2.000001], units="m") + self.assertFalse(unit_arrays_almost_equal(val1, val2)) + def test_values_identical(self): val1 = UnitArray([1., 2.], units="m") self.assertTrue(unit_arrays_almost_equal(val1, val1)) @@ -86,9 +107,9 @@ def test_values_close_enough(self): val1 = UnitArray([1., 2.], units="m") val2 = val1 + UnitArray([1.e-5, 1.e-6], units="m") self.assertFalse(unit_arrays_almost_equal(val1, val2)) - self.assertTrue(unit_arrays_almost_equal(val1, val2, eps=1e-4)) + self.assertTrue(unit_arrays_almost_equal(val1, val2, rtol=1e-4)) def test_values_not_close_enough(self): val1 = UnitArray([1., 2.], units="m") val3 = val1 + UnitArray([1.e-2, 1.e-6], units="m") - self.assertFalse(unit_arrays_almost_equal(val1, val3, eps=1e-4)) + self.assertFalse(unit_arrays_almost_equal(val1, val3, rtol=1e-4)) From fbbc66e694d6492d3d09513ba2e862be9ddbde75 Mon Sep 17 00:00:00 2001 From: Jonathan Rocher Date: Sun, 28 Apr 2019 17:54:03 -0500 Subject: [PATCH 6/6] One more conversion eps -> rtol, and update test module. --- scimath/units/assertion_utils.py | 8 ++--- scimath/units/tests/test_assertion_utils.py | 35 +++++++++++++++++++-- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/scimath/units/assertion_utils.py b/scimath/units/assertion_utils.py index 1be4073..89f7e67 100644 --- a/scimath/units/assertion_utils.py +++ b/scimath/units/assertion_utils.py @@ -11,7 +11,7 @@ def assert_unit_scalar_almost_equal(val1, val2, rtol=1.e-9, msg=None): if msg is None: msg = "{} and {} are not almost equal with precision {}" - msg = msg.format(val1, val2, eps) + msg = msg.format(val1, val2, rtol) assert_true(unit_scalars_almost_equal(val1, val2, rtol=rtol), msg=msg) @@ -19,7 +19,7 @@ def assert_unit_scalar_almost_equal(val1, val2, rtol=1.e-9, msg=None): def assert_unit_scalar_not_almost_equal(val1, val2, rtol=1.e-9, msg=None): if msg is None: msg = "{} and {} unexpectedly almost equal with precision {}" - msg = msg.format(val1, val2, eps) + msg = msg.format(val1, val2, rtol) assert_false(unit_scalars_almost_equal(val1, val2, rtol=rtol), msg=msg) @@ -27,7 +27,7 @@ def assert_unit_scalar_not_almost_equal(val1, val2, rtol=1.e-9, msg=None): def assert_unit_array_almost_equal(uarr1, uarr2, rtol=1e-9, msg=None): if msg is None: msg = "{} and {} are not almost equal with precision {}" - msg = msg.format(uarr1, uarr2, eps) + msg = msg.format(uarr1, uarr2, rtol) assert_true(unit_arrays_almost_equal(uarr1, uarr2, rtol=rtol), msg=msg) @@ -35,6 +35,6 @@ def assert_unit_array_almost_equal(uarr1, uarr2, rtol=1e-9, msg=None): def assert_unit_array_not_almost_equal(uarr1, uarr2, rtol=1e-9, msg=None): if msg is None: msg = "{} and {} are almost equal with precision {}" - msg = msg.format(uarr1, uarr2, eps) + msg = msg.format(uarr1, uarr2, rtol) assert_false(unit_arrays_almost_equal(uarr1, uarr2, rtol=rtol), msg=msg) diff --git a/scimath/units/tests/test_assertion_utils.py b/scimath/units/tests/test_assertion_utils.py index 57b0306..47401a8 100644 --- a/scimath/units/tests/test_assertion_utils.py +++ b/scimath/units/tests/test_assertion_utils.py @@ -1,8 +1,8 @@ from unittest import TestCase from scimath.units.api import UnitArray, UnitScalar -from scimath.units.testing.assertion_utils import \ - assert_unit_array_almost_equal, assert_unit_scalar_almost_equal +from scimath.units.assertion_utils import assert_unit_array_almost_equal, \ + assert_unit_scalar_almost_equal class TestAssertUnitScalarEqual(TestCase): @@ -14,6 +14,21 @@ def test_equivalent_unit_scalar(self): assert_unit_scalar_almost_equal(UnitScalar(1, units="m"), UnitScalar(100, units="cm")) + def test_not_close(self): + with self.assertRaises(AssertionError): + assert_unit_scalar_almost_equal(UnitScalar(1, units="m"), + UnitScalar(1.1, units="m")) + + def test_not_close_custom_msg(self): + a1 = UnitScalar(1, units="m") + a2 = UnitScalar(1.1, units="m") + with self.assertRaises(AssertionError): + assert_unit_scalar_almost_equal(a1, a2, rtol=1e-2, msg="BLAH") + + def test_unit_scalar_non_default_rtol(self): + assert_unit_scalar_almost_equal(UnitScalar(1, units="m"), + UnitScalar(1.01, units="m"), rtol=1e-1) + class TestAssertUnitArrayEqual(TestCase): def test_same_unit_array(self): @@ -23,3 +38,19 @@ def test_same_unit_array(self): def test_equivalent_unit_array(self): assert_unit_array_almost_equal(UnitArray([1, 2], units="m"), UnitArray([100, 200], units="cm")) + + def test_not_close(self): + a1 = UnitArray([1.01, 2], units="s") + a2 = UnitArray([1, 2], units="s") + with self.assertRaises(AssertionError): + assert_unit_array_almost_equal(a1, a2) + + def test_not_close_custom_msg(self): + a1 = UnitArray([1.01, 2], units="s") + a2 = UnitArray([1, 2], units="s") + with self.assertRaises(AssertionError): + assert_unit_array_almost_equal(a1, a2, msg="BLAH") + + def test_unit_scalar_non_default_rtol(self): + assert_unit_array_almost_equal(UnitScalar(1, units="m"), + UnitScalar(1.01, units="m"), rtol=1e-1)