You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.7 KiB

8 years ago
  1. import pytest
  2. from pybind11_tests import Matrix, ConstructorStats
  3. pytestmark = pytest.requires_numpy
  4. with pytest.suppress(ImportError):
  5. import numpy as np
  6. def test_from_python():
  7. with pytest.raises(RuntimeError) as excinfo:
  8. Matrix(np.array([1, 2, 3])) # trying to assign a 1D array
  9. assert str(excinfo.value) == "Incompatible buffer format!"
  10. m3 = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
  11. m4 = Matrix(m3)
  12. for i in range(m4.rows()):
  13. for j in range(m4.cols()):
  14. assert m3[i, j] == m4[i, j]
  15. cstats = ConstructorStats.get(Matrix)
  16. assert cstats.alive() == 1
  17. del m3, m4
  18. assert cstats.alive() == 0
  19. assert cstats.values() == ["2x3 matrix"]
  20. assert cstats.copy_constructions == 0
  21. # assert cstats.move_constructions >= 0 # Don't invoke any
  22. assert cstats.copy_assignments == 0
  23. assert cstats.move_assignments == 0
  24. # PyPy: Memory leak in the "np.array(m, copy=False)" call
  25. # https://bitbucket.org/pypy/pypy/issues/2444
  26. @pytest.unsupported_on_pypy
  27. def test_to_python():
  28. m = Matrix(5, 5)
  29. assert m[2, 3] == 0
  30. m[2, 3] = 4
  31. assert m[2, 3] == 4
  32. m2 = np.array(m, copy=False)
  33. assert m2.shape == (5, 5)
  34. assert abs(m2).sum() == 4
  35. assert m2[2, 3] == 4
  36. m2[2, 3] = 5
  37. assert m2[2, 3] == 5
  38. cstats = ConstructorStats.get(Matrix)
  39. assert cstats.alive() == 1
  40. del m
  41. pytest.gc_collect()
  42. assert cstats.alive() == 1
  43. del m2 # holds an m reference
  44. pytest.gc_collect()
  45. assert cstats.alive() == 0
  46. assert cstats.values() == ["5x5 matrix"]
  47. assert cstats.copy_constructions == 0
  48. # assert cstats.move_constructions >= 0 # Don't invoke any
  49. assert cstats.copy_assignments == 0
  50. assert cstats.move_assignments == 0