1
0

stl.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "test.h"
  2. #include "pybind11/stl.h"
  3. namespace {
  4. int constructor_calls = 0;
  5. int destructor_calls = 0;
  6. struct Point {
  7. int x;
  8. int y;
  9. Point() : x(0), y(0) { constructor_calls++; }
  10. Point(int x, int y) : x(x), y(y) { constructor_calls++; }
  11. Point(const Point& p) : x(p.x), y(p.y) { constructor_calls++; }
  12. Point(Point&& p) noexcept : x(p.x), y(p.y) { constructor_calls++; }
  13. Point& operator= (const Point& p) {
  14. x = p.x;
  15. y = p.y;
  16. return *this;
  17. }
  18. ~Point() { destructor_calls++; }
  19. bool operator== (const Point& p) const { return x == p.x && y == p.y; }
  20. };
  21. } // namespace
  22. TEST_F(PYBIND11_TEST, vector_bool) {
  23. std::vector<bool> v = {true, false, true};
  24. py::object obj = py::cast(v);
  25. EXPECT_EVAL_EQ("[True, False, True]", obj);
  26. std::vector<bool> v2 = obj.cast<std::vector<bool>>();
  27. EXPECT_EQ(v, v2);
  28. }
  29. TEST_F(PYBIND11_TEST, list_like) {
  30. py::class_<Point>(py::module::__main__(), "Point")
  31. .def(py::init<int, int>())
  32. .def_readwrite("x", &Point::x)
  33. .def_readwrite("y", &Point::y)
  34. .def("__eq__", &Point::operator==);
  35. // array
  36. {
  37. std::array<Point, 2> a = {Point(1, 2), Point(3, 4)};
  38. py::object obj = py::eval("[Point(1, 2), Point(3, 4)]");
  39. EXPECT_EVAL_EQ("[Point(1, 2), Point(3, 4)]", obj);
  40. std::array<Point, 2> a2 = obj.cast<std::array<Point, 2>>();
  41. EXPECT_EQ(a, a2);
  42. }
  43. // vector
  44. {
  45. std::vector<Point> v = {Point(1, 2), Point(3, 4)};
  46. py::object obj = py::cast(v);
  47. EXPECT_EVAL_EQ("[Point(1, 2), Point(3, 4)]", obj);
  48. std::vector<Point> v2 = obj.cast<std::vector<Point>>();
  49. EXPECT_EQ(v, v2);
  50. }
  51. // list
  52. {
  53. std::list<Point> l = {Point(1, 2), Point(3, 4)};
  54. py::object obj = py::cast(l);
  55. EXPECT_EVAL_EQ("[Point(1, 2), Point(3, 4)]", obj);
  56. std::list<Point> l2 = obj.cast<std::list<Point>>();
  57. EXPECT_EQ(l, l2);
  58. }
  59. // deque
  60. {
  61. std::deque<Point> d = {Point(1, 2), Point(3, 4)};
  62. py::object obj = py::cast(d);
  63. EXPECT_EVAL_EQ("[Point(1, 2), Point(3, 4)]", obj);
  64. std::deque<Point> d2 = obj.cast<std::deque<Point>>();
  65. EXPECT_EQ(d, d2);
  66. }
  67. }
  68. TEST_F(PYBIND11_TEST, dict_like) {
  69. py::class_<Point>(py::module::__main__(), "Point")
  70. .def(py::init<int, int>())
  71. .def_readwrite("x", &Point::x)
  72. .def_readwrite("y", &Point::y)
  73. .def("__eq__", &Point::operator==);
  74. // map
  75. {
  76. std::map<std::string, Point> m = {
  77. {"a", Point(1, 2)},
  78. {"b", Point(3, 4)}
  79. };
  80. py::object obj = py::cast(m);
  81. EXPECT_EVAL_EQ("{'a': Point(1, 2), 'b': Point(3, 4)}", obj);
  82. std::map<std::string, Point> m2 = obj.cast<std::map<std::string, Point>>();
  83. EXPECT_EQ(m, m2);
  84. }
  85. // unordered_map
  86. {
  87. std::unordered_map<std::string, Point> m = {
  88. {"a", Point(1, 2)},
  89. {"b", Point(3, 4)}
  90. };
  91. py::object obj = py::cast(m);
  92. EXPECT_EVAL_EQ("{'a': Point(1, 2), 'b': Point(3, 4)}", obj);
  93. std::unordered_map<std::string, Point> m2 =
  94. obj.cast<std::unordered_map<std::string, Point>>();
  95. EXPECT_EQ(m, m2);
  96. }
  97. }