MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_vector_int

Function test_vector_int

tests/test_stl_binders.py:8–67  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

6
7
8def test_vector_int():
9 v_int = m.VectorInt([0, 0])
10 assert len(v_int) == 2
11 assert bool(v_int) is True
12
13 # test construction from a generator
14 v_int1 = m.VectorInt(x for x in range(5))
15 assert v_int1 == m.VectorInt([0, 1, 2, 3, 4])
16
17 v_int2 = m.VectorInt([0, 0])
18 assert v_int == v_int2
19 v_int2[1] = 1
20 assert v_int != v_int2
21
22 v_int2.append(2)
23 v_int2.insert(0, 1)
24 v_int2.insert(0, 2)
25 v_int2.insert(0, 3)
26 v_int2.insert(6, 3)
27 assert str(v_int2) == "VectorInt[3, 2, 1, 0, 1, 2, 3]"
28 with pytest.raises(IndexError):
29 v_int2.insert(8, 4)
30
31 v_int.append(99)
32 v_int2[2:-2] = v_int
33 assert v_int2 == m.VectorInt([3, 2, 0, 0, 99, 2, 3])
34 del v_int2[1:3]
35 assert v_int2 == m.VectorInt([3, 0, 99, 2, 3])
36 del v_int2[0]
37 assert v_int2 == m.VectorInt([0, 99, 2, 3])
38
39 v_int2.extend(m.VectorInt([4, 5]))
40 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5])
41
42 v_int2.extend([6, 7])
43 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
44
45 # test error handling, and that the vector is unchanged
46 with pytest.raises(RuntimeError):
47 v_int2.extend([8, "a"])
48
49 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7])
50
51 # test extending from a generator
52 v_int2.extend(x for x in range(5))
53 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4])
54
55 # test negative indexing
56 assert v_int2[-1] == 4
57
58 # insert with negative index
59 v_int2.insert(-1, 88)
60 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88, 4])
61
62 # delete negative index
63 del v_int2[-1]
64 assert v_int2 == m.VectorInt([0, 99, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 88])
65

Callers

nothing calls this directly

Calls 5

lenFunction · 0.85
strClass · 0.85
insertMethod · 0.80
appendMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected