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

Function test_move_support

tests/test_virtual_functions.py:200–242  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

198 not hasattr(m, "NCVirt"), reason="NCVirt does not work on Intel/PGI/NVCC compilers"
199)
200def test_move_support():
201 class NCVirtExt(m.NCVirt):
202 def get_noncopyable(self, a, b):
203 # Constructs and returns a new instance:
204 return m.NonCopyable(a * a, b * b)
205
206 def get_movable(self, a, b):
207 # Return a referenced copy
208 self.movable = m.Movable(a, b)
209 return self.movable
210
211 class NCVirtExt2(m.NCVirt):
212 def get_noncopyable(self, a, b):
213 # Keep a reference: this is going to throw an exception
214 self.nc = m.NonCopyable(a, b)
215 return self.nc
216
217 def get_movable(self, a, b):
218 # Return a new instance without storing it
219 return m.Movable(a, b)
220
221 ncv1 = NCVirtExt()
222 assert ncv1.print_nc(2, 3) == "36"
223 assert ncv1.print_movable(4, 5) == "9"
224 ncv2 = NCVirtExt2()
225 assert ncv2.print_movable(7, 7) == "14"
226 # Don't check the exception message here because it differs under debug/non-debug mode
227 with pytest.raises(RuntimeError):
228 ncv2.print_nc(9, 9)
229
230 nc_stats = ConstructorStats.get(m.NonCopyable)
231 mv_stats = ConstructorStats.get(m.Movable)
232 assert nc_stats.alive() == 1
233 assert mv_stats.alive() == 1
234 del ncv1, ncv2
235 assert nc_stats.alive() == 0
236 assert mv_stats.alive() == 0
237 assert nc_stats.values() == ["4", "9", "9", "9"]
238 assert mv_stats.values() == ["4", "5", "7", "7"]
239 assert nc_stats.copy_constructions == 0
240 assert mv_stats.copy_constructions == 1
241 assert nc_stats.move_constructions >= 0
242 assert mv_stats.move_constructions >= 0
243
244
245def test_dispatch_issue(msg):

Callers

nothing calls this directly

Calls 5

NCVirtExtClass · 0.85
NCVirtExt2Class · 0.85
aliveMethod · 0.80
valuesMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected