test_buffers.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import io
3 import struct
4 
5 import pytest
6 
7 import env # noqa: F401
8 
9 from pybind11_tests import buffers as m
10 from pybind11_tests import ConstructorStats
11 
12 np = pytest.importorskip("numpy")
13 
14 
16  with pytest.raises(RuntimeError) as excinfo:
17  m.Matrix(np.array([1, 2, 3])) # trying to assign a 1D array
18  assert str(excinfo.value) == "Incompatible buffer format!"
19 
20  m3 = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
21  m4 = m.Matrix(m3)
22 
23  for i in range(m4.rows()):
24  for j in range(m4.cols()):
25  assert m3[i, j] == m4[i, j]
26 
27  cstats = ConstructorStats.get(m.Matrix)
28  assert cstats.alive() == 1
29  del m3, m4
30  assert cstats.alive() == 0
31  assert cstats.values() == ["2x3 matrix"]
32  assert cstats.copy_constructions == 0
33  # assert cstats.move_constructions >= 0 # Don't invoke any
34  assert cstats.copy_assignments == 0
35  assert cstats.move_assignments == 0
36 
37 
38 # https://foss.heptapod.net/pypy/pypy/-/issues/2444
40  mat = m.Matrix(5, 4)
41  assert memoryview(mat).shape == (5, 4)
42 
43  assert mat[2, 3] == 0
44  mat[2, 3] = 4.0
45  mat[3, 2] = 7.0
46  assert mat[2, 3] == 4
47  assert mat[3, 2] == 7
48  assert struct.unpack_from('f', mat, (3 * 4 + 2) * 4) == (7, )
49  assert struct.unpack_from('f', mat, (2 * 4 + 3) * 4) == (4, )
50 
51  mat2 = np.array(mat, copy=False)
52  assert mat2.shape == (5, 4)
53  assert abs(mat2).sum() == 11
54  assert mat2[2, 3] == 4 and mat2[3, 2] == 7
55  mat2[2, 3] = 5
56  assert mat2[2, 3] == 5
57 
58  cstats = ConstructorStats.get(m.Matrix)
59  assert cstats.alive() == 1
60  del mat
61  pytest.gc_collect()
62  assert cstats.alive() == 1
63  del mat2 # holds a mat reference
64  pytest.gc_collect()
65  assert cstats.alive() == 0
66  assert cstats.values() == ["5x4 matrix"]
67  assert cstats.copy_constructions == 0
68  # assert cstats.move_constructions >= 0 # Don't invoke any
69  assert cstats.copy_assignments == 0
70  assert cstats.move_assignments == 0
71 
72 
74  """SquareMatrix is derived from Matrix and inherits the buffer protocol"""
75 
76  matrix = m.SquareMatrix(5)
77  assert memoryview(matrix).shape == (5, 5)
78  assert np.asarray(matrix).shape == (5, 5)
79 
80 
82  for cls in [m.Buffer, m.ConstBuffer, m.DerivedBuffer]:
83  buf = cls()
84  buf.value = 0x12345678
85  value = struct.unpack('i', bytearray(buf))[0]
86  assert value == 0x12345678
87 
88 
90  buf = m.BufferReadOnly(0x64)
91  view = memoryview(buf)
92  assert view[0] == b'd' if env.PY2 else 0x64
93  assert view.readonly
94 
95 
97  buf = m.BufferReadOnlySelect()
98 
99  memoryview(buf)[0] = b'd' if env.PY2 else 0x64
100  assert buf.value == 0x64
101 
102  io.BytesIO(b'A').readinto(buf)
103  assert buf.value == ord(b'A')
104 
105  buf.readonly = True
106  with pytest.raises(TypeError):
107  memoryview(buf)[0] = b'\0' if env.PY2 else 0
108  with pytest.raises(TypeError):
109  io.BytesIO(b'1').readinto(buf)
def test_pointer_to_member_fn()
Definition: test_buffers.py:81
static ConstructorStats & get(std::type_index type)
Definition: pytypes.h:928
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
Definition: mpreal.h:2381
def test_from_python()
Definition: test_buffers.py:15
def test_readonly_buffer()
Definition: test_buffers.py:89
def test_inherited_protocol()
Definition: test_buffers.py:73
def test_to_python()
Definition: test_buffers.py:39
#define abs(x)
Definition: datatypes.h:17
def test_selective_readonly_buffer()
Definition: test_buffers.py:96


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:03