test__exceptions.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """
  2. Tests of the ._exceptions module. Primarily for exercising the __str__ methods.
  3. """
  4. import pickle
  5. import numpy as np
  6. _ArrayMemoryError = np.core._exceptions._ArrayMemoryError
  7. _UFuncNoLoopError = np.core._exceptions._UFuncNoLoopError
  8. class TestArrayMemoryError:
  9. def test_pickling(self):
  10. """ Test that _ArrayMemoryError can be pickled """
  11. error = _ArrayMemoryError((1023,), np.dtype(np.uint8))
  12. res = pickle.loads(pickle.dumps(error))
  13. assert res._total_size == error._total_size
  14. def test_str(self):
  15. e = _ArrayMemoryError((1023,), np.dtype(np.uint8))
  16. str(e) # not crashing is enough
  17. # testing these properties is easier than testing the full string repr
  18. def test__size_to_string(self):
  19. """ Test e._size_to_string """
  20. f = _ArrayMemoryError._size_to_string
  21. Ki = 1024
  22. assert f(0) == '0 bytes'
  23. assert f(1) == '1 bytes'
  24. assert f(1023) == '1023 bytes'
  25. assert f(Ki) == '1.00 KiB'
  26. assert f(Ki+1) == '1.00 KiB'
  27. assert f(10*Ki) == '10.0 KiB'
  28. assert f(int(999.4*Ki)) == '999. KiB'
  29. assert f(int(1023.4*Ki)) == '1023. KiB'
  30. assert f(int(1023.5*Ki)) == '1.00 MiB'
  31. assert f(Ki*Ki) == '1.00 MiB'
  32. # 1023.9999 Mib should round to 1 GiB
  33. assert f(int(Ki*Ki*Ki*0.9999)) == '1.00 GiB'
  34. assert f(Ki*Ki*Ki*Ki*Ki*Ki) == '1.00 EiB'
  35. # larger than sys.maxsize, adding larger prefices isn't going to help
  36. # anyway.
  37. assert f(Ki*Ki*Ki*Ki*Ki*Ki*123456) == '123456. EiB'
  38. def test__total_size(self):
  39. """ Test e._total_size """
  40. e = _ArrayMemoryError((1,), np.dtype(np.uint8))
  41. assert e._total_size == 1
  42. e = _ArrayMemoryError((2, 4), np.dtype((np.uint64, 16)))
  43. assert e._total_size == 1024
  44. class TestUFuncNoLoopError:
  45. def test_pickling(self):
  46. """ Test that _UFuncNoLoopError can be pickled """
  47. assert isinstance(pickle.dumps(_UFuncNoLoopError), bytes)