test_getlimits.py 5.2 KB

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