Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:57

0001 import pytest
0002 
0003 from pybind11_tests import stl_binders as m
0004 
0005 
0006 def test_vector_int():
0007     v_int = m.VectorInt([0, 0])
0008     assert len(v_int) == 2
0009     assert bool(v_int) is True
0010 
0011     # test construction from a generator
0012     v_int1 = m.VectorInt(x for x in range(5))
0013     assert v_int1 == m.VectorInt([0, 1, 2, 3, 4])
0014 
0015     v_int2 = m.VectorInt([0, 0])
0016     assert v_int == v_int2
0017     v_int2[1] = 1
0018     assert v_int != v_int2
0019 
0020     v_int2.append(2)
0021     v_int2.insert(0, 1)
0022     v_int2.insert(0, 2)
0023     v_int2.insert(0, 3)
0024     v_int2.insert(6, 3)
0025     assert str(v_int2) == "VectorInt[3, 2, 1, 0, 1, 2, 3]"
0026     with pytest.raises(IndexError):
0027         v_int2.insert(8, 4)
0028 
0029     v_int.append(99)
0030     v_int2[2:-2] = v_int
0031     assert v_int2 == m.VectorInt([3, 2, 0, 0, 99, 2, 3])
0032     del v_int2[1:3]
0033     assert v_int2 == m.VectorInt([3, 0, 99, 2, 3])
0034     del v_int2[0]
0035     assert v_int2 == m.VectorInt([0, 99, 2, 3])
0036 
0037     v_int2.extend(m.VectorInt([4, 5]))
0038     assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5])
0039 
0040     v_int2.extend([6, 7])
0041     assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
0042 
0043     # test error handling, and that the vector is unchanged
0044     with pytest.raises(RuntimeError):
0045         v_int2.extend([8, "a"])
0046 
0047     assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
0048 
0049     # test extending from a generator
0050     v_int2.extend(x for x in range(5))
0051     assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4])
0052 
0053     # test negative indexing
0054     assert v_int2[-1] == 4
0055 
0056     # insert with negative index
0057     v_int2.insert(-1, 88)
0058     assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88, 4])
0059 
0060     # delete negative index
0061     del v_int2[-1]
0062     assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88])
0063 
0064     v_int2.clear()
0065     assert len(v_int2) == 0
0066 
0067 
0068 # Older PyPy's failed here, related to the PyPy's buffer protocol.
0069 def test_vector_buffer():
0070     b = bytearray([1, 2, 3, 4])
0071     v = m.VectorUChar(b)
0072     assert v[1] == 2
0073     v[2] = 5
0074     mv = memoryview(v)  # We expose the buffer interface
0075     assert mv[2] == 5
0076     mv[2] = 6
0077     assert v[2] == 6
0078 
0079     mv = memoryview(b)
0080     v = m.VectorUChar(mv[::2])
0081     assert v[1] == 3
0082 
0083     with pytest.raises(RuntimeError) as excinfo:
0084         m.create_undeclstruct()  # Undeclared struct contents, no buffer interface
0085     assert "NumPy type info missing for " in str(excinfo.value)
0086 
0087 
0088 def test_vector_buffer_numpy():
0089     np = pytest.importorskip("numpy")
0090     a = np.array([1, 2, 3, 4], dtype=np.int32)
0091     with pytest.raises(TypeError):
0092         m.VectorInt(a)
0093 
0094     a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.uintc)
0095     v = m.VectorInt(a[0, :])
0096     assert len(v) == 4
0097     assert v[2] == 3
0098     ma = np.asarray(v)
0099     ma[2] = 5
0100     assert v[2] == 5
0101 
0102     v = m.VectorInt(a[:, 1])
0103     assert len(v) == 3
0104     assert v[2] == 10
0105 
0106     v = m.get_vectorstruct()
0107     assert v[0].x == 5
0108     ma = np.asarray(v)
0109     ma[1]["x"] = 99
0110     assert v[1].x == 99
0111 
0112     v = m.VectorStruct(
0113         np.zeros(
0114             3,
0115             dtype=np.dtype(
0116                 [("w", "bool"), ("x", "I"), ("y", "float64"), ("z", "bool")], align=True
0117             ),
0118         )
0119     )
0120     assert len(v) == 3
0121 
0122     b = np.array([1, 2, 3, 4], dtype=np.uint8)
0123     v = m.VectorUChar(b[::2])
0124     assert v[1] == 3
0125 
0126 
0127 def test_vector_bool():
0128     import pybind11_cross_module_tests as cm
0129 
0130     vv_c = cm.VectorBool()
0131     for i in range(10):
0132         vv_c.append(i % 2 == 0)
0133     for i in range(10):
0134         assert vv_c[i] == (i % 2 == 0)
0135     assert str(vv_c) == "VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]"
0136 
0137 
0138 def test_vector_custom():
0139     v_a = m.VectorEl()
0140     v_a.append(m.El(1))
0141     v_a.append(m.El(2))
0142     assert str(v_a) == "VectorEl[El{1}, El{2}]"
0143 
0144     vv_a = m.VectorVectorEl()
0145     vv_a.append(v_a)
0146     vv_b = vv_a[0]
0147     assert str(vv_b) == "VectorEl[El{1}, El{2}]"
0148 
0149 
0150 def test_map_string_double():
0151     mm = m.MapStringDouble()
0152     mm["a"] = 1
0153     mm["b"] = 2.5
0154 
0155     assert list(mm) == ["a", "b"]
0156     assert str(mm) == "MapStringDouble{a: 1, b: 2.5}"
0157     assert "b" in mm
0158     assert "c" not in mm
0159     assert 123 not in mm
0160 
0161     # Check that keys, values, items are views, not merely iterable
0162     keys = mm.keys()
0163     values = mm.values()
0164     items = mm.items()
0165     assert list(keys) == ["a", "b"]
0166     assert len(keys) == 2
0167     assert "a" in keys
0168     assert "c" not in keys
0169     assert 123 not in keys
0170     assert list(items) == [("a", 1), ("b", 2.5)]
0171     assert len(items) == 2
0172     assert ("b", 2.5) in items
0173     assert "hello" not in items
0174     assert ("b", 2.5, None) not in items
0175     assert list(values) == [1, 2.5]
0176     assert len(values) == 2
0177     assert 1 in values
0178     assert 2 not in values
0179     # Check that views update when the map is updated
0180     mm["c"] = -1
0181     assert list(keys) == ["a", "b", "c"]
0182     assert list(values) == [1, 2.5, -1]
0183     assert list(items) == [("a", 1), ("b", 2.5), ("c", -1)]
0184 
0185     um = m.UnorderedMapStringDouble()
0186     um["ua"] = 1.1
0187     um["ub"] = 2.6
0188 
0189     assert sorted(list(um)) == ["ua", "ub"]
0190     assert list(um.keys()) == list(um)
0191     assert sorted(list(um.items())) == [("ua", 1.1), ("ub", 2.6)]
0192     assert list(zip(um.keys(), um.values())) == list(um.items())
0193     assert "UnorderedMapStringDouble" in str(um)
0194 
0195 
0196 def test_map_string_double_const():
0197     mc = m.MapStringDoubleConst()
0198     mc["a"] = 10
0199     mc["b"] = 20.5
0200     assert str(mc) == "MapStringDoubleConst{a: 10, b: 20.5}"
0201 
0202     umc = m.UnorderedMapStringDoubleConst()
0203     umc["a"] = 11
0204     umc["b"] = 21.5
0205 
0206     str(umc)
0207 
0208 
0209 def test_noncopyable_containers():
0210     # std::vector
0211     vnc = m.get_vnc(5)
0212     for i in range(0, 5):
0213         assert vnc[i].value == i + 1
0214 
0215     for i, j in enumerate(vnc, start=1):
0216         assert j.value == i
0217 
0218     # std::deque
0219     dnc = m.get_dnc(5)
0220     for i in range(0, 5):
0221         assert dnc[i].value == i + 1
0222 
0223     i = 1
0224     for j in dnc:
0225         assert j.value == i
0226         i += 1
0227 
0228     # std::map
0229     mnc = m.get_mnc(5)
0230     for i in range(1, 6):
0231         assert mnc[i].value == 10 * i
0232 
0233     vsum = 0
0234     for k, v in mnc.items():
0235         assert v.value == 10 * k
0236         vsum += v.value
0237 
0238     assert vsum == 150
0239 
0240     # std::unordered_map
0241     mnc = m.get_umnc(5)
0242     for i in range(1, 6):
0243         assert mnc[i].value == 10 * i
0244 
0245     vsum = 0
0246     for k, v in mnc.items():
0247         assert v.value == 10 * k
0248         vsum += v.value
0249 
0250     assert vsum == 150
0251 
0252     # nested std::map<std::vector>
0253     nvnc = m.get_nvnc(5)
0254     for i in range(1, 6):
0255         for j in range(0, 5):
0256             assert nvnc[i][j].value == j + 1
0257 
0258     # Note: maps do not have .values()
0259     for _, v in nvnc.items():
0260         for i, j in enumerate(v, start=1):
0261             assert j.value == i
0262 
0263     # nested std::map<std::map>
0264     nmnc = m.get_nmnc(5)
0265     for i in range(1, 6):
0266         for j in range(10, 60, 10):
0267             assert nmnc[i][j].value == 10 * j
0268 
0269     vsum = 0
0270     for _, v_o in nmnc.items():
0271         for k_i, v_i in v_o.items():
0272             assert v_i.value == 10 * k_i
0273             vsum += v_i.value
0274 
0275     assert vsum == 7500
0276 
0277     # nested std::unordered_map<std::unordered_map>
0278     numnc = m.get_numnc(5)
0279     for i in range(1, 6):
0280         for j in range(10, 60, 10):
0281             assert numnc[i][j].value == 10 * j
0282 
0283     vsum = 0
0284     for _, v_o in numnc.items():
0285         for k_i, v_i in v_o.items():
0286             assert v_i.value == 10 * k_i
0287             vsum += v_i.value
0288 
0289     assert vsum == 7500
0290 
0291 
0292 def test_map_delitem():
0293     mm = m.MapStringDouble()
0294     mm["a"] = 1
0295     mm["b"] = 2.5
0296 
0297     assert list(mm) == ["a", "b"]
0298     assert list(mm.items()) == [("a", 1), ("b", 2.5)]
0299     del mm["a"]
0300     assert list(mm) == ["b"]
0301     assert list(mm.items()) == [("b", 2.5)]
0302 
0303     um = m.UnorderedMapStringDouble()
0304     um["ua"] = 1.1
0305     um["ub"] = 2.6
0306 
0307     assert sorted(list(um)) == ["ua", "ub"]
0308     assert sorted(list(um.items())) == [("ua", 1.1), ("ub", 2.6)]
0309     del um["ua"]
0310     assert sorted(list(um)) == ["ub"]
0311     assert sorted(list(um.items())) == [("ub", 2.6)]
0312 
0313 
0314 def test_map_view_types():
0315     map_string_double = m.MapStringDouble()
0316     unordered_map_string_double = m.UnorderedMapStringDouble()
0317     map_string_double_const = m.MapStringDoubleConst()
0318     unordered_map_string_double_const = m.UnorderedMapStringDoubleConst()
0319 
0320     assert map_string_double.keys().__class__.__name__ == "KeysView[str]"
0321     assert map_string_double.values().__class__.__name__ == "ValuesView[float]"
0322     assert map_string_double.items().__class__.__name__ == "ItemsView[str, float]"
0323 
0324     keys_type = type(map_string_double.keys())
0325     assert type(unordered_map_string_double.keys()) is keys_type
0326     assert type(map_string_double_const.keys()) is keys_type
0327     assert type(unordered_map_string_double_const.keys()) is keys_type
0328 
0329     values_type = type(map_string_double.values())
0330     assert type(unordered_map_string_double.values()) is values_type
0331     assert type(map_string_double_const.values()) is values_type
0332     assert type(unordered_map_string_double_const.values()) is values_type
0333 
0334     items_type = type(map_string_double.items())
0335     assert type(unordered_map_string_double.items()) is items_type
0336     assert type(map_string_double_const.items()) is items_type
0337     assert type(unordered_map_string_double_const.items()) is items_type