From 74b0f41e1533692abd9e96b08c136240597019f1 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Fri, 3 Feb 2012 16:25:11 -0500 Subject: [PATCH] ENH: Add tests for full coverage. The extra dtype tests are not exhaustive, but, instead, were added to test specific code paths in `_convert`. --- skimage/util/tests/test_dtype.py | 35 +++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/skimage/util/tests/test_dtype.py b/skimage/util/tests/test_dtype.py index 4932a480..dabfe044 100644 --- a/skimage/util/tests/test_dtype.py +++ b/skimage/util/tests/test_dtype.py @@ -1,7 +1,9 @@ import numpy as np -from numpy.testing import assert_equal +from numpy.testing import assert_equal, assert_raises from skimage import img_as_int, img_as_float, \ img_as_uint, img_as_ubyte +from skimage.util.dtype import _convert + dtype_range = {np.uint8: (0, 255), np.uint16: (0, 65535), @@ -34,6 +36,37 @@ def test_range(): "From %s to %s" % (np.dtype(dtype), np.dtype(dt)), \ y, omin, omax + +def test_range_extra_dtypes(): + """Test code paths that are not skipped by `test_range`""" + + # Add non-standard data types that are allowed by the `_convert` function. + dtype_range_extra = dtype_range.copy() + dtype_range_extra.update({np.int32: (-2147483648, 2147483647), + np.uint32: (0, 4294967295)}) + + dtype_pairs = [(np.uint8, np.uint32), + (np.int8, np.uint32), + (np.int8, np.int32), + (np.int32, np.int8), + (np.float64, np.float32), + (np.int32, np.float32)] + + for dtype_in, dt in dtype_pairs: + imin, imax = dtype_range_extra[dtype_in] + x = np.linspace(imin, imax, 10).astype(dtype_in) + y = _convert(x, dt) + omin, omax = dtype_range_extra[dt] + yield _verify_range, \ + "From %s to %s" % (np.dtype(dtype_in), np.dtype(dt)), \ + y, omin, omax + + +def test_unsupported_dtype(): + x = np.arange(10).astype(np.uint64) + assert_raises(ValueError, img_as_int, x) + + if __name__ == '__main__': np.testing.run_module_suite()