class.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "test.h"
  2. namespace {
  3. struct Point {
  4. int x;
  5. int y;
  6. private:
  7. int z;
  8. public:
  9. inline static int constructor_calls = 0;
  10. inline static int copy_constructor_calls = 0;
  11. inline static int move_constructor_calls = 0;
  12. inline static int destructor_calls = 0;
  13. Point() : x(0), y(0), z(0) { constructor_calls++; }
  14. Point(int x, int y, int z) : x(x), y(y), z(z) { constructor_calls++; }
  15. Point(const Point& p) : x(p.x), y(p.y), z(p.z) {
  16. copy_constructor_calls++;
  17. constructor_calls++;
  18. }
  19. Point(Point&& p) noexcept : x(p.x), y(p.y), z(p.z) {
  20. move_constructor_calls++;
  21. constructor_calls++;
  22. }
  23. Point& operator= (const Point& p) {
  24. x = p.x;
  25. y = p.y;
  26. z = p.z;
  27. return *this;
  28. }
  29. ~Point() { destructor_calls++; }
  30. int& get_z() { return z; }
  31. void set_z(int z) { this->z = z; }
  32. std::string stringfy() const {
  33. return "(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z) + ")";
  34. }
  35. };
  36. struct Line {
  37. Point start;
  38. Point end;
  39. };
  40. TEST_F(PYBIND11_TEST, class) {
  41. py::module m = py::module::import("__main__");
  42. py::class_<Point>(m, "Point")
  43. .def(py::init<>())
  44. .def(py::init<int, int, int>())
  45. .def_readwrite("x", &Point::x)
  46. .def_readwrite("y", &Point::y)
  47. .def_property("z", &Point::get_z, &Point::set_z)
  48. .def("stringfy", &Point::stringfy);
  49. py::exec(R"(
  50. p = Point()
  51. assert p.stringfy() == '(0, 0, 0)'
  52. p = Point(1, 2, 3)
  53. assert p.x == 1
  54. assert p.y == 2
  55. assert p.z == 3
  56. assert p.stringfy() == '(1, 2, 3)'
  57. p.x = 10
  58. p.y = 20
  59. p.z = 30
  60. assert p.stringfy() == '(10, 20, 30)'
  61. )");
  62. py::class_<Line> line(m, "Line");
  63. line // not bind constructor
  64. .def_readwrite("start", &Line::start)
  65. .def_readwrite("end", &Line::end);
  66. // bind constructor
  67. line.def(py::init<>());
  68. py::exec(R"(
  69. l = Line()
  70. l.start = Point(1, 2, 3)
  71. l.end = Point(4, 5, 6)
  72. p = l.start
  73. assert l.start.stringfy() == '(1, 2, 3)'
  74. assert l.end.stringfy() == '(4, 5, 6)'
  75. )");
  76. py::finalize(true);
  77. EXPECT_EQ(Point::constructor_calls, Point::destructor_calls);
  78. EXPECT_EQ(Point::copy_constructor_calls, 0);
  79. EXPECT_EQ(Point::move_constructor_calls, 0);
  80. }
  81. TEST_F(PYBIND11_TEST, inheritance) {
  82. static int constructor_calls = 0;
  83. struct Point {
  84. int x;
  85. int y;
  86. Point() : x(0), y(0) { constructor_calls++; }
  87. Point(int x, int y) : x(x), y(y) { constructor_calls++; }
  88. };
  89. struct Point3D : Point {
  90. int z;
  91. Point3D() : Point(), z(0) { constructor_calls++; }
  92. Point3D(int x, int y, int z) : Point(x, y), z(z) { constructor_calls++; }
  93. };
  94. py::module m = py::module::import("__main__");
  95. py::class_<Point>(m, "Point")
  96. .def(py::init<>())
  97. .def(py::init<int, int>())
  98. .def_readwrite("x", &Point::x)
  99. .def_readwrite("y", &Point::y);
  100. py::class_<Point3D, Point>(m, "Point3D")
  101. .def(py::init<>())
  102. .def(py::init<int, int, int>())
  103. .def_readwrite("z", &Point3D::z);
  104. py::exec(R"(
  105. p = Point3D()
  106. assert type(p) == Point3D
  107. assert p.x == 0
  108. assert p.y == 0
  109. assert p.z == 0
  110. p = Point3D(1, 2, 3)
  111. assert p.x == 1
  112. assert p.y == 2
  113. assert p.z == 3
  114. p.x = 10
  115. p.y = 20
  116. p.z = 30
  117. assert p.x == 10
  118. assert p.y == 20
  119. assert p.z == 30
  120. )");
  121. py::finalize(true);
  122. EXPECT_EQ(constructor_calls, 4);
  123. }
  124. TEST_F(PYBIND11_TEST, dynamic_attr) {
  125. py::module m = py::module::import("__main__");
  126. struct Point {
  127. int x;
  128. int y;
  129. Point(int x, int y) : x(x), y(y) {}
  130. };
  131. py::class_<Point>(m, "Point", py::dynamic_attr())
  132. .def(py::init<int, int>())
  133. .def_readwrite("x", &Point::x)
  134. .def_readwrite("y", &Point::y);
  135. py::object p = py::eval("Point(1, 2)");
  136. EXPECT_EQ(p.attr("x").cast<int>(), 1);
  137. EXPECT_EQ(p.attr("y").cast<int>(), 2);
  138. p.attr("z") = py::int_(3);
  139. EXPECT_EQ(p.attr("z").cast<int>(), 3);
  140. }
  141. TEST_F(PYBIND11_TEST, enum) {
  142. enum class Color { RED, Yellow, GREEN, BLUE };
  143. py::module m = py::module::import("__main__");
  144. py::enum_<Color> color(m, "Color");
  145. color.value("RED", Color::RED)
  146. .value("Yellow", Color::Yellow)
  147. .value("GREEN", Color::GREEN)
  148. .value("BLUE", Color::BLUE);
  149. EXPECT_EVAL_EQ("Color.RED", Color::RED);
  150. EXPECT_EVAL_EQ("Color.Yellow", Color::Yellow);
  151. EXPECT_EVAL_EQ("Color.GREEN", Color::GREEN);
  152. EXPECT_EVAL_EQ("Color.BLUE", Color::BLUE);
  153. EXPECT_EVAL_EQ("Color(0)", Color::RED);
  154. EXPECT_EVAL_EQ("Color(1)", Color::Yellow);
  155. EXPECT_EVAL_EQ("Color(2)", Color::GREEN);
  156. EXPECT_EVAL_EQ("Color(3)", Color::BLUE);
  157. EXPECT_EXEC_EQ("Color(0)", "Color.RED");
  158. EXPECT_EXEC_EQ("Color(1)", "Color.Yellow");
  159. EXPECT_EXEC_EQ("Color(2)", "Color.GREEN");
  160. EXPECT_EXEC_EQ("Color(3)", "Color.BLUE");
  161. EXPECT_EVAL_EQ("Color.RED.value", static_cast<int>(Color::RED));
  162. EXPECT_EVAL_EQ("Color.Yellow.value", static_cast<int>(Color::Yellow));
  163. EXPECT_EVAL_EQ("Color.GREEN.value", static_cast<int>(Color::GREEN));
  164. EXPECT_EVAL_EQ("Color.BLUE.value", static_cast<int>(Color::BLUE));
  165. color.export_values();
  166. EXPECT_EVAL_EQ("RED", Color::RED);
  167. EXPECT_EVAL_EQ("Yellow", Color::Yellow);
  168. EXPECT_EVAL_EQ("GREEN", Color::GREEN);
  169. EXPECT_EVAL_EQ("BLUE", Color::BLUE);
  170. }
  171. } // namespace