diff options
Diffstat (limited to 'silx/utils/test')
-rw-r--r-- | silx/utils/test/__init__.py | 6 | ||||
-rw-r--r-- | silx/utils/test/test_debug.py | 99 | ||||
-rw-r--r-- | silx/utils/test/test_number.py | 181 |
3 files changed, 285 insertions, 1 deletions
diff --git a/silx/utils/test/__init__.py b/silx/utils/test/__init__.py index 14ce38d..f16cbdc 100644 --- a/silx/utils/test/__init__.py +++ b/silx/utils/test/__init__.py @@ -24,7 +24,7 @@ # ###########################################################################*/ __authors__ = ["T. Vincent", "P. Knobel"] __license__ = "MIT" -__date__ = "02/10/2017" +__date__ = "24/05/2018" import unittest @@ -34,6 +34,8 @@ from . import test_array_like from . import test_launcher from . import test_deprecation from . import test_proxy +from . import test_debug +from . import test_number def suite(): @@ -44,4 +46,6 @@ def suite(): test_suite.addTest(test_launcher.suite()) test_suite.addTest(test_deprecation.suite()) test_suite.addTest(test_proxy.suite()) + test_suite.addTest(test_debug.suite()) + test_suite.addTest(test_number.suite()) return test_suite diff --git a/silx/utils/test/test_debug.py b/silx/utils/test/test_debug.py new file mode 100644 index 0000000..da08960 --- /dev/null +++ b/silx/utils/test/test_debug.py @@ -0,0 +1,99 @@ +# coding: utf-8 +# /*########################################################################## +# +# Copyright (c) 2016 European Synchrotron Radiation Facility +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# ###########################################################################*/ +"""Tests for debug module""" + +__authors__ = ["V. Valls"] +__license__ = "MIT" +__date__ = "27/02/2018" + + +import unittest +from silx.utils import debug +from silx.utils import testutils + + +@debug.log_all_methods +class _Foobar(object): + + def a(self): + return None + + def b(self): + return self.a() + + def random_args(self, *args, **kwargs): + return args, kwargs + + def named_args(self, a, b): + return a + 1, b + 1 + + +class TestDebug(unittest.TestCase): + """Tests for debug module.""" + + def logB(self): + """ + Can be used to check the log output using: + `./run_tests.py silx.utils.test.test_debug.TestDebug.logB -v` + """ + print() + test = _Foobar() + test.b() + + @testutils.test_logging(debug.debug_logger.name, warning=2) + def testMethod(self): + test = _Foobar() + test.a() + + @testutils.test_logging(debug.debug_logger.name, warning=4) + def testInterleavedMethod(self): + test = _Foobar() + test.b() + + @testutils.test_logging(debug.debug_logger.name, warning=2) + def testNamedArgument(self): + # Arguments arre still provided to the patched method + test = _Foobar() + result = test.named_args(10, 11) + self.assertEqual(result, (11, 12)) + + @testutils.test_logging(debug.debug_logger.name, warning=2) + def testRandomArguments(self): + # Arguments arre still provided to the patched method + test = _Foobar() + result = test.random_args("foo", 50, a=10, b=100) + self.assertEqual(result[0], ("foo", 50)) + self.assertEqual(result[1], {"a": 10, "b": 100}) + + +def suite(): + test_suite = unittest.TestSuite() + loadTests = unittest.defaultTestLoader.loadTestsFromTestCase + test_suite.addTest(loadTests(TestDebug)) + return test_suite + + +if __name__ == '__main__': + unittest.main(defaultTest='suite') diff --git a/silx/utils/test/test_number.py b/silx/utils/test/test_number.py new file mode 100644 index 0000000..c900b32 --- /dev/null +++ b/silx/utils/test/test_number.py @@ -0,0 +1,181 @@ +# coding: utf-8 +# /*########################################################################## +# Copyright (C) 2016-2018 European Synchrotron Radiation Facility +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# ############################################################################*/ +"""Tests for silx.uitls.number module""" + +__authors__ = ["V. Valls"] +__license__ = "MIT" +__date__ = "05/06/2018" + +import logging +import numpy +import unittest +import pkg_resources +from silx.utils import number +from silx.utils import testutils + +_logger = logging.getLogger(__name__) + + +class TestConversionTypes(testutils.ParametricTestCase): + + def testEmptyFail(self): + self.assertRaises(ValueError, number.min_numerical_convertible_type, "") + + def testStringFail(self): + self.assertRaises(ValueError, number.min_numerical_convertible_type, "a") + + def testInteger(self): + dtype = number.min_numerical_convertible_type("1456") + self.assertTrue(numpy.issubdtype(dtype, numpy.unsignedinteger)) + + def testTrailledInteger(self): + dtype = number.min_numerical_convertible_type(" \t\n\r1456\t\n\r") + self.assertTrue(numpy.issubdtype(dtype, numpy.unsignedinteger)) + + def testPositiveInteger(self): + dtype = number.min_numerical_convertible_type("+1456") + self.assertTrue(numpy.issubdtype(dtype, numpy.unsignedinteger)) + + def testNegativeInteger(self): + dtype = number.min_numerical_convertible_type("-1456") + self.assertTrue(numpy.issubdtype(dtype, numpy.signedinteger)) + + def testIntegerExponential(self): + dtype = number.min_numerical_convertible_type("14e10") + self.assertTrue(numpy.issubdtype(dtype, numpy.floating)) + + def testIntegerPositiveExponential(self): + dtype = number.min_numerical_convertible_type("14e+10") + self.assertTrue(numpy.issubdtype(dtype, numpy.floating)) + + def testIntegerNegativeExponential(self): + dtype = number.min_numerical_convertible_type("14e-10") + self.assertTrue(numpy.issubdtype(dtype, numpy.floating)) + + def testNumberDecimal(self): + dtype = number.min_numerical_convertible_type("14.5") + self.assertTrue(numpy.issubdtype(dtype, numpy.floating)) + + def testPositiveNumberDecimal(self): + dtype = number.min_numerical_convertible_type("+14.5") + self.assertTrue(numpy.issubdtype(dtype, numpy.floating)) + + def testNegativeNumberDecimal(self): + dtype = number.min_numerical_convertible_type("-14.5") + self.assertTrue(numpy.issubdtype(dtype, numpy.floating)) + + def testDecimal(self): + dtype = number.min_numerical_convertible_type(".50") + self.assertTrue(numpy.issubdtype(dtype, numpy.floating)) + + def testPositiveDecimal(self): + dtype = number.min_numerical_convertible_type("+.5") + self.assertTrue(numpy.issubdtype(dtype, numpy.floating)) + + def testNegativeDecimal(self): + dtype = number.min_numerical_convertible_type("-.5") + self.assertTrue(numpy.issubdtype(dtype, numpy.floating)) + + def testMantissa16(self): + dtype = number.min_numerical_convertible_type("1.50") + self.assertEqual(dtype, numpy.float16) + + def testMantissa32(self): + dtype = number.min_numerical_convertible_type("1400.50") + self.assertEqual(dtype, numpy.float32) + + def testMantissa64(self): + dtype = number.min_numerical_convertible_type("10000.000010") + self.assertEqual(dtype, numpy.float64) + + def testMantissa80(self): + self.skipIfFloat80NotSupported() + dtype = number.min_numerical_convertible_type("1000000000.00001013") + + if pkg_resources.parse_version(numpy.version.version) <= pkg_resources.parse_version("1.10.4"): + # numpy 1.8.2 -> Debian 8 + # Checking a float128 precision with numpy 1.8.2 using abs(diff) is not working. + # It looks like the difference is done using float64 (diff == 0.0) + expected = (numpy.longdouble, numpy.float64) + else: + expected = (numpy.longdouble, ) + self.assertIn(dtype, expected) + + def testExponent32(self): + dtype = number.min_numerical_convertible_type("14.0e30") + self.assertEqual(dtype, numpy.float32) + + def testExponent64(self): + dtype = number.min_numerical_convertible_type("14.0e300") + self.assertEqual(dtype, numpy.float64) + + def testExponent80(self): + self.skipIfFloat80NotSupported() + dtype = number.min_numerical_convertible_type("14.0e3000") + self.assertEqual(dtype, numpy.longdouble) + + def testFloat32ToString(self): + value = str(numpy.float32(numpy.pi)) + dtype = number.min_numerical_convertible_type(value) + self.assertIn(dtype, (numpy.float32, numpy.float64)) + + def skipIfFloat80NotSupported(self): + if number.is_longdouble_64bits(): + self.skipTest("float-80bits not supported") + + def testLosePrecisionUsingFloat80(self): + self.skipIfFloat80NotSupported() + if pkg_resources.parse_version(numpy.version.version) <= pkg_resources.parse_version("1.10.4"): + self.skipTest("numpy > 1.10.4 expected") + value = "1000000000.00001013332" + func = testutils.test_logging(number._logger.name, warning=1) + func = func(number.min_numerical_convertible_type) + dtype = func(value) + self.assertIn(dtype, (numpy.longdouble, )) + + def testMillisecondEpochTime(self): + datetimes = ['1465803236.495412', + '1465803236.999362', + '1465803237.504311', + '1465803238.009261', + '1465803238.512211', + '1465803239.016160', + '1465803239.520110', + '1465803240.026059', + '1465803240.529009'] + for datetime in datetimes: + with self.subTest(datetime=datetime): + dtype = number.min_numerical_convertible_type(datetime) + self.assertEqual(dtype, numpy.float64) + + +def suite(): + loadTests = unittest.defaultTestLoader.loadTestsFromTestCase + test_suite = unittest.TestSuite() + test_suite.addTest(loadTests(TestConversionTypes)) + return test_suite + + +if __name__ == '__main__': + unittest.main(defaultTest="suite") |