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

Function test_sequence

tests/test_sequences_and_iterators.py:120–181  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

118
119
120def test_sequence():
121 cstats = ConstructorStats.get(m.Sequence)
122
123 s = m.Sequence(5)
124 if not env.GRAALPY:
125 assert cstats.values() == ["of size", "5"]
126
127 assert "Sequence" in repr(s)
128 assert len(s) == 5
129 assert s[0] == 0
130 assert s[3] == 0
131 assert 12.34 not in s
132 s[0], s[3] = 12.34, 56.78
133 assert 12.34 in s
134 assert s[0] == approx(12.34, rel=1e-05)
135 assert s[3] == approx(56.78, rel=1e-05)
136
137 rev = reversed(s)
138 if not env.GRAALPY:
139 assert cstats.values() == ["of size", "5"]
140
141 rev2 = s[::-1]
142 if not env.GRAALPY:
143 assert cstats.values() == ["of size", "5"]
144
145 it = iter(m.Sequence(0))
146 for _ in range(3): # __next__ must continue to raise StopIteration
147 with pytest.raises(StopIteration):
148 next(it)
149 if not env.GRAALPY:
150 assert cstats.values() == ["of size", "0"]
151
152 expected = [0, 56.78, 0, 0, 12.34]
153 assert rev == approx(expected, rel=1e-05)
154 assert rev2 == approx(expected, rel=1e-05)
155 assert rev == rev2
156
157 rev[0::2] = m.Sequence([2.0, 2.0, 2.0])
158 if not env.GRAALPY:
159 assert cstats.values() == ["of size", "3", "from std::vector"]
160
161 assert rev == approx([2, 56.78, 2, 0, 2], rel=1e-05)
162
163 if env.GRAALPY:
164 pytest.skip("ConstructorStats is incompatible with GraalPy.")
165
166 assert cstats.alive() == 4
167 del it
168 assert cstats.alive() == 3
169 del s
170 assert cstats.alive() == 2
171 del rev
172 assert cstats.alive() == 1
173 del rev2
174 assert cstats.alive() == 0
175
176 assert cstats.values() == []
177 assert cstats.default_constructions == 0

Callers

nothing calls this directly

Calls 7

reprFunction · 0.85
lenFunction · 0.85
iterFunction · 0.85
SequenceMethod · 0.80
valuesMethod · 0.80
aliveMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected