test_utils.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from __future__ import division, absolute_import, print_function
  2. import sys
  3. import pytest
  4. from numpy.core import arange
  5. from numpy.testing import assert_, assert_equal, assert_raises_regex
  6. from numpy.lib import deprecate
  7. import numpy.lib.utils as utils
  8. if sys.version_info[0] >= 3:
  9. from io import StringIO
  10. else:
  11. from StringIO import StringIO
  12. @pytest.mark.skipif(sys.flags.optimize == 2, reason="Python running -OO")
  13. def test_lookfor():
  14. out = StringIO()
  15. utils.lookfor('eigenvalue', module='numpy', output=out,
  16. import_modules=False)
  17. out = out.getvalue()
  18. assert_('numpy.linalg.eig' in out)
  19. @deprecate
  20. def old_func(self, x):
  21. return x
  22. @deprecate(message="Rather use new_func2")
  23. def old_func2(self, x):
  24. return x
  25. def old_func3(self, x):
  26. return x
  27. new_func3 = deprecate(old_func3, old_name="old_func3", new_name="new_func3")
  28. def test_deprecate_decorator():
  29. assert_('deprecated' in old_func.__doc__)
  30. def test_deprecate_decorator_message():
  31. assert_('Rather use new_func2' in old_func2.__doc__)
  32. def test_deprecate_fn():
  33. assert_('old_func3' in new_func3.__doc__)
  34. assert_('new_func3' in new_func3.__doc__)
  35. def test_safe_eval_nameconstant():
  36. # Test if safe_eval supports Python 3.4 _ast.NameConstant
  37. utils.safe_eval('None')
  38. class TestByteBounds(object):
  39. def test_byte_bounds(self):
  40. # pointer difference matches size * itemsize
  41. # due to contiguity
  42. a = arange(12).reshape(3, 4)
  43. low, high = utils.byte_bounds(a)
  44. assert_equal(high - low, a.size * a.itemsize)
  45. def test_unusual_order_positive_stride(self):
  46. a = arange(12).reshape(3, 4)
  47. b = a.T
  48. low, high = utils.byte_bounds(b)
  49. assert_equal(high - low, b.size * b.itemsize)
  50. def test_unusual_order_negative_stride(self):
  51. a = arange(12).reshape(3, 4)
  52. b = a.T[::-1]
  53. low, high = utils.byte_bounds(b)
  54. assert_equal(high - low, b.size * b.itemsize)
  55. def test_strided(self):
  56. a = arange(12)
  57. b = a[::2]
  58. low, high = utils.byte_bounds(b)
  59. # the largest pointer address is lost (even numbers only in the
  60. # stride), and compensate addresses for striding by 2
  61. assert_equal(high - low, b.size * 2 * b.itemsize - b.itemsize)
  62. def test_assert_raises_regex_context_manager():
  63. with assert_raises_regex(ValueError, 'no deprecation warning'):
  64. raise ValueError('no deprecation warning')