test__exceptions.py 1.5 KB

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