test_getlimits.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """ Test functions for limits module.
  2. """
  3. import numpy as np
  4. from numpy.core import finfo, iinfo
  5. from numpy import half, single, double, longdouble
  6. from numpy.testing import assert_equal, assert_, assert_raises
  7. from numpy.core.getlimits import _discovered_machar, _float_ma
  8. ##################################################
  9. class TestPythonFloat:
  10. def test_singleton(self):
  11. ftype = finfo(float)
  12. ftype2 = finfo(float)
  13. assert_equal(id(ftype), id(ftype2))
  14. class TestHalf:
  15. def test_singleton(self):
  16. ftype = finfo(half)
  17. ftype2 = finfo(half)
  18. assert_equal(id(ftype), id(ftype2))
  19. class TestSingle:
  20. def test_singleton(self):
  21. ftype = finfo(single)
  22. ftype2 = finfo(single)
  23. assert_equal(id(ftype), id(ftype2))
  24. class TestDouble:
  25. def test_singleton(self):
  26. ftype = finfo(double)
  27. ftype2 = finfo(double)
  28. assert_equal(id(ftype), id(ftype2))
  29. class TestLongdouble:
  30. def test_singleton(self):
  31. ftype = finfo(longdouble)
  32. ftype2 = finfo(longdouble)
  33. assert_equal(id(ftype), id(ftype2))
  34. class TestFinfo:
  35. def test_basic(self):
  36. dts = list(zip(['f2', 'f4', 'f8', 'c8', 'c16'],
  37. [np.float16, np.float32, np.float64, np.complex64,
  38. np.complex128]))
  39. for dt1, dt2 in dts:
  40. for attr in ('bits', 'eps', 'epsneg', 'iexp', 'machar', 'machep',
  41. 'max', 'maxexp', 'min', 'minexp', 'negep', 'nexp',
  42. 'nmant', 'precision', 'resolution', 'tiny'):
  43. assert_equal(getattr(finfo(dt1), attr),
  44. getattr(finfo(dt2), attr), attr)
  45. assert_raises(ValueError, finfo, 'i4')
  46. class TestIinfo:
  47. def test_basic(self):
  48. dts = list(zip(['i1', 'i2', 'i4', 'i8',
  49. 'u1', 'u2', 'u4', 'u8'],
  50. [np.int8, np.int16, np.int32, np.int64,
  51. np.uint8, np.uint16, np.uint32, np.uint64]))
  52. for dt1, dt2 in dts:
  53. for attr in ('bits', 'min', 'max'):
  54. assert_equal(getattr(iinfo(dt1), attr),
  55. getattr(iinfo(dt2), attr), attr)
  56. assert_raises(ValueError, iinfo, 'f4')
  57. def test_unsigned_max(self):
  58. types = np.sctypes['uint']
  59. for T in types:
  60. assert_equal(iinfo(T).max, T(-1))
  61. class TestRepr:
  62. def test_iinfo_repr(self):
  63. expected = "iinfo(min=-32768, max=32767, dtype=int16)"
  64. assert_equal(repr(np.iinfo(np.int16)), expected)
  65. def test_finfo_repr(self):
  66. expected = "finfo(resolution=1e-06, min=-3.4028235e+38," + \
  67. " max=3.4028235e+38, dtype=float32)"
  68. assert_equal(repr(np.finfo(np.float32)), expected)
  69. def test_instances():
  70. iinfo(10)
  71. finfo(3.0)
  72. def assert_ma_equal(discovered, ma_like):
  73. # Check MachAr-like objects same as calculated MachAr instances
  74. for key, value in discovered.__dict__.items():
  75. assert_equal(value, getattr(ma_like, key))
  76. if hasattr(value, 'shape'):
  77. assert_equal(value.shape, getattr(ma_like, key).shape)
  78. assert_equal(value.dtype, getattr(ma_like, key).dtype)
  79. def test_known_types():
  80. # Test we are correctly compiling parameters for known types
  81. for ftype, ma_like in ((np.float16, _float_ma[16]),
  82. (np.float32, _float_ma[32]),
  83. (np.float64, _float_ma[64])):
  84. assert_ma_equal(_discovered_machar(ftype), ma_like)
  85. # Suppress warning for broken discovery of double double on PPC
  86. with np.errstate(all='ignore'):
  87. ld_ma = _discovered_machar(np.longdouble)
  88. bytes = np.dtype(np.longdouble).itemsize
  89. if (ld_ma.it, ld_ma.maxexp) == (63, 16384) and bytes in (12, 16):
  90. # 80-bit extended precision
  91. assert_ma_equal(ld_ma, _float_ma[80])
  92. elif (ld_ma.it, ld_ma.maxexp) == (112, 16384) and bytes == 16:
  93. # IEE 754 128-bit
  94. assert_ma_equal(ld_ma, _float_ma[128])
  95. def test_plausible_finfo():
  96. # Assert that finfo returns reasonable results for all types
  97. for ftype in np.sctypes['float'] + np.sctypes['complex']:
  98. info = np.finfo(ftype)
  99. assert_(info.nmant > 1)
  100. assert_(info.minexp < -1)
  101. assert_(info.maxexp > 1)