mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-23 05:35:13 +00:00
3f589379ec
This commit rewrites the examples that look for constructor/destructor calls to do so via static variable tracking rather than output parsing. The added ConstructorStats class provides methods to keep track of constructors and destructors, number of default/copy/move constructors, and number of copy/move assignments. It also provides a mechanism for storing values (e.g. for value construction), and then allows all of this to be checked at the end of a test by getting the statistics for a C++ (or python mapping) class. By not relying on the precise pattern of constructions/destructions, but rather simply ensuring that every construction is matched with a destruction on the same object, we ensure that everything that gets created also gets destroyed as expected. This replaces all of the various "std::cout << whatever" code in constructors/destructors with `print_created(this)`/`print_destroyed(this)`/etc. functions which provide similar output, but now has a unified format across the different examples, including a new ### prefix that makes mixed example output and lifecycle events easier to distinguish. With this change, relaxed mode is no longer needed, which enables testing for proper destruction under MSVC, and under any other compiler that generates code calling extra constructors, or optimizes away any constructors. GCC/clang are used as the baseline for move constructors; the tests are adapted to allow more move constructors to be evoked (but other types are constructors much have matching counts). This commit also disables output buffering of tests, as the buffering sometimes results in C++ output ending up in the middle of python output (or vice versa), depending on the OS/python version.
67 lines
2.9 KiB
Plaintext
67 lines
2.9 KiB
Plaintext
### Vector2 @ 0x11f7830 created [1.000000, 2.000000]
|
|
### Vector2 @ 0x11427c0 created [3.000000, -1.000000]
|
|
v1 = [1.000000, 2.000000]
|
|
v2 = [3.000000, -1.000000]
|
|
### Vector2 @ 0x7ffef6b144b8 created [4.000000, 1.000000]
|
|
### Vector2 @ 0x11f7e90 created via move constructor
|
|
### Vector2 @ 0x7ffef6b144b8 destroyed
|
|
### Vector2 @ 0x11f7e90 destroyed
|
|
v1+v2 = [4.000000, 1.000000]
|
|
### Vector2 @ 0x7ffef6b144b8 created [-2.000000, 3.000000]
|
|
### Vector2 @ 0x11f7e90 created via move constructor
|
|
### Vector2 @ 0x7ffef6b144b8 destroyed
|
|
### Vector2 @ 0x11f7e90 destroyed
|
|
v1-v2 = [-2.000000, 3.000000]
|
|
### Vector2 @ 0x7ffef6b144c8 created [-7.000000, -6.000000]
|
|
### Vector2 @ 0x1115760 created via move constructor
|
|
### Vector2 @ 0x7ffef6b144c8 destroyed
|
|
### Vector2 @ 0x1115760 destroyed
|
|
v1-8 = [-7.000000, -6.000000]
|
|
### Vector2 @ 0x7ffef6b144c8 created [9.000000, 10.000000]
|
|
### Vector2 @ 0x1115760 created via move constructor
|
|
### Vector2 @ 0x7ffef6b144c8 destroyed
|
|
### Vector2 @ 0x1115760 destroyed
|
|
v1+8 = [9.000000, 10.000000]
|
|
### Vector2 @ 0x7ffef6b144b8 created [8.000000, 16.000000]
|
|
### Vector2 @ 0x1115760 created via move constructor
|
|
### Vector2 @ 0x7ffef6b144b8 destroyed
|
|
### Vector2 @ 0x1115760 destroyed
|
|
v1*8 = [8.000000, 16.000000]
|
|
### Vector2 @ 0x7ffef6b144a8 created [0.125000, 0.250000]
|
|
### Vector2 @ 0x112f150 created via move constructor
|
|
### Vector2 @ 0x7ffef6b144a8 destroyed
|
|
### Vector2 @ 0x112f150 destroyed
|
|
v1/8 = [0.125000, 0.250000]
|
|
### Vector2 @ 0x7ffef6b144f8 created [7.000000, 6.000000]
|
|
### Vector2 @ 0x112f1b0 created via move constructor
|
|
### Vector2 @ 0x7ffef6b144f8 destroyed
|
|
### Vector2 @ 0x112f1b0 destroyed
|
|
8-v1 = [7.000000, 6.000000]
|
|
### Vector2 @ 0x7ffef6b144f8 created [9.000000, 10.000000]
|
|
### Vector2 @ 0x112f1b0 created via move constructor
|
|
### Vector2 @ 0x7ffef6b144f8 destroyed
|
|
### Vector2 @ 0x112f1b0 destroyed
|
|
8+v1 = [9.000000, 10.000000]
|
|
### Vector2 @ 0x7ffef6b144e8 created [8.000000, 16.000000]
|
|
### Vector2 @ 0x112f230 created via move constructor
|
|
### Vector2 @ 0x7ffef6b144e8 destroyed
|
|
### Vector2 @ 0x112f230 destroyed
|
|
8*v1 = [8.000000, 16.000000]
|
|
### Vector2 @ 0x7ffef6b144d8 created [8.000000, 4.000000]
|
|
### Vector2 @ 0x11fb360 created via move constructor
|
|
### Vector2 @ 0x7ffef6b144d8 destroyed
|
|
### Vector2 @ 0x11fb360 destroyed
|
|
8/v1 = [8.000000, 4.000000]
|
|
(v1+v2)*2 = [8.000000, 2.000000]
|
|
Instances not destroyed: 2
|
|
### Vector2 @ 0x11f7830 destroyed
|
|
Instances not destroyed: 1
|
|
### Vector2 @ 0x11427c0 destroyed
|
|
Instances not destroyed: 0
|
|
Constructor values: ['[1.000000, 2.000000]', '[3.000000, -1.000000]', '[4.000000, 1.000000]', '[-2.000000, 3.000000]', '[-7.000000, -6.000000]', '[9.000000, 10.000000]', '[8.000000, 16.000000]', '[0.125000, 0.250000]', '[7.000000, 6.000000]', '[9.000000, 10.000000]', '[8.000000, 16.000000]', '[8.000000, 4.000000]']
|
|
Default constructions: 0
|
|
Copy constructions: 0
|
|
Move constructions: True
|
|
Copy assignments: 0
|
|
Move assignments: 0
|