test_stl_binders.py revision 12037:d28054ac6ec9
1import pytest
2import sys
3
4with pytest.suppress(ImportError):
5    import numpy as np
6
7
8def test_vector_int():
9    from pybind11_tests import VectorInt
10
11    v_int = VectorInt([0, 0])
12    assert len(v_int) == 2
13    assert bool(v_int) is True
14
15    v_int2 = VectorInt([0, 0])
16    assert v_int == v_int2
17    v_int2[1] = 1
18    assert v_int != v_int2
19
20    v_int2.append(2)
21    v_int2.append(3)
22    v_int2.insert(0, 1)
23    v_int2.insert(0, 2)
24    v_int2.insert(0, 3)
25    assert str(v_int2) == "VectorInt[3, 2, 1, 0, 1, 2, 3]"
26
27    v_int.append(99)
28    v_int2[2:-2] = v_int
29    assert v_int2 == VectorInt([3, 2, 0, 0, 99, 2, 3])
30    del v_int2[1:3]
31    assert v_int2 == VectorInt([3, 0, 99, 2, 3])
32    del v_int2[0]
33    assert v_int2 == VectorInt([0, 99, 2, 3])
34
35
36@pytest.unsupported_on_pypy
37def test_vector_buffer():
38    from pybind11_tests import VectorUChar, create_undeclstruct
39    b = bytearray([1, 2, 3, 4])
40    v = VectorUChar(b)
41    assert v[1] == 2
42    v[2] = 5
43    m = memoryview(v)  # We expose the buffer interface
44    if sys.version_info.major > 2:
45        assert m[2] == 5
46        m[2] = 6
47    else:
48        assert m[2] == '\x05'
49        m[2] = '\x06'
50    assert v[2] == 6
51
52    with pytest.raises(RuntimeError):
53        create_undeclstruct()  # Undeclared struct contents, no buffer interface
54
55
56@pytest.requires_numpy
57def test_vector_buffer_numpy():
58    from pybind11_tests import VectorInt, VectorStruct, get_vectorstruct
59
60    a = np.array([1, 2, 3, 4], dtype=np.int32)
61    with pytest.raises(TypeError):
62        VectorInt(a)
63
64    a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.uintc)
65    v = VectorInt(a[0, :])
66    assert len(v) == 4
67    assert v[2] == 3
68    m = np.asarray(v)
69    m[2] = 5
70    assert v[2] == 5
71
72    v = VectorInt(a[:, 1])
73    assert len(v) == 3
74    assert v[2] == 10
75
76    v = get_vectorstruct()
77    assert v[0].x == 5
78    m = np.asarray(v)
79    m[1]['x'] = 99
80    assert v[1].x == 99
81
82    v = VectorStruct(np.zeros(3, dtype=np.dtype([('w', 'bool'), ('x', 'I'),
83                                                 ('y', 'float64'), ('z', 'bool')], align=True)))
84    assert len(v) == 3
85
86
87def test_vector_custom():
88    from pybind11_tests import El, VectorEl, VectorVectorEl
89
90    v_a = VectorEl()
91    v_a.append(El(1))
92    v_a.append(El(2))
93    assert str(v_a) == "VectorEl[El{1}, El{2}]"
94
95    vv_a = VectorVectorEl()
96    vv_a.append(v_a)
97    vv_b = vv_a[0]
98    assert str(vv_b) == "VectorEl[El{1}, El{2}]"
99
100
101def test_vector_bool():
102    from pybind11_tests import VectorBool
103
104    vv_c = VectorBool()
105    for i in range(10):
106        vv_c.append(i % 2 == 0)
107    for i in range(10):
108        assert vv_c[i] == (i % 2 == 0)
109    assert str(vv_c) == "VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]"
110
111
112def test_map_string_double():
113    from pybind11_tests import MapStringDouble, UnorderedMapStringDouble
114
115    m = MapStringDouble()
116    m['a'] = 1
117    m['b'] = 2.5
118
119    assert list(m) == ['a', 'b']
120    assert list(m.items()) == [('a', 1), ('b', 2.5)]
121    assert str(m) == "MapStringDouble{a: 1, b: 2.5}"
122
123    um = UnorderedMapStringDouble()
124    um['ua'] = 1.1
125    um['ub'] = 2.6
126
127    assert sorted(list(um)) == ['ua', 'ub']
128    assert sorted(list(um.items())) == [('ua', 1.1), ('ub', 2.6)]
129    assert "UnorderedMapStringDouble" in str(um)
130
131
132def test_map_string_double_const():
133    from pybind11_tests import MapStringDoubleConst, UnorderedMapStringDoubleConst
134
135    mc = MapStringDoubleConst()
136    mc['a'] = 10
137    mc['b'] = 20.5
138    assert str(mc) == "MapStringDoubleConst{a: 10, b: 20.5}"
139
140    umc = UnorderedMapStringDoubleConst()
141    umc['a'] = 11
142    umc['b'] = 21.5
143
144    str(umc)
145
146
147def test_noncopyable_vector():
148    from pybind11_tests import get_vnc
149
150    vnc = get_vnc(5)
151    for i in range(0, 5):
152        assert vnc[i].value == i + 1
153
154    for i, j in enumerate(vnc, start=1):
155        assert j.value == i
156
157
158def test_noncopyable_deque():
159    from pybind11_tests import get_dnc
160
161    dnc = get_dnc(5)
162    for i in range(0, 5):
163        assert dnc[i].value == i + 1
164
165    i = 1
166    for j in dnc:
167        assert(j.value == i)
168        i += 1
169
170
171def test_noncopyable_map():
172    from pybind11_tests import get_mnc
173
174    mnc = get_mnc(5)
175    for i in range(1, 6):
176        assert mnc[i].value == 10 * i
177
178    vsum = 0
179    for k, v in mnc.items():
180        assert v.value == 10 * k
181        vsum += v.value
182
183    assert vsum == 150
184
185
186def test_noncopyable_unordered_map():
187    from pybind11_tests import get_umnc
188
189    mnc = get_umnc(5)
190    for i in range(1, 6):
191        assert mnc[i].value == 10 * i
192
193    vsum = 0
194    for k, v in mnc.items():
195        assert v.value == 10 * k
196        vsum += v.value
197
198    assert vsum == 150
199