test_anyptr.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include <catch2/catch.hpp>
  2. #include <libslic3r/libslic3r.h>
  3. #include <libslic3r/AnyPtr.hpp>
  4. #include <test_utils.hpp>
  5. class Foo
  6. {
  7. public:
  8. virtual ~Foo() = default;
  9. virtual void set_foo(int) = 0;
  10. virtual int get_foo() const = 0;
  11. };
  12. class Bar: public Foo
  13. {
  14. int m_i = 0;
  15. public:
  16. virtual void set_foo(int i) { m_i = i; }
  17. virtual int get_foo() const { return m_i; };
  18. };
  19. class BarPlus: public Foo {
  20. int m_i = 0;
  21. public:
  22. virtual void set_foo(int i) { m_i = i + 1; }
  23. virtual int get_foo() const { return m_i; };
  24. };
  25. TEST_CASE("Testing AnyPtr", "[anyptr]") {
  26. using Slic3r::AnyPtr;
  27. SECTION("Construction with various valid arguments using operator=")
  28. {
  29. auto args = std::make_tuple(nullptr,
  30. AnyPtr<Foo>{nullptr},
  31. AnyPtr{static_cast<Foo *>(nullptr)},
  32. AnyPtr{static_cast<Bar *>(nullptr)},
  33. AnyPtr{static_cast<BarPlus *>(nullptr)},
  34. AnyPtr<Foo>{},
  35. AnyPtr<Bar>{},
  36. AnyPtr<BarPlus>{},
  37. static_cast<Foo *>(nullptr),
  38. static_cast<Bar *>(nullptr),
  39. static_cast<BarPlus *>(nullptr));
  40. auto check_ptr = [](auto &ptr) {
  41. REQUIRE(!ptr);
  42. REQUIRE(!ptr.is_owned());
  43. auto shp = ptr.get_shared_cpy();
  44. REQUIRE(!shp);
  45. };
  46. SECTION("operator =") {
  47. Slic3r::for_each_in_tuple([&check_ptr](auto &arg){
  48. AnyPtr<const Foo> ptr = std::move(arg);
  49. check_ptr(ptr);
  50. }, args);
  51. }
  52. SECTION("move construction")
  53. {
  54. Slic3r::for_each_in_tuple([&check_ptr](auto &arg){
  55. AnyPtr<const Foo> ptr{std::move(arg)};
  56. check_ptr(ptr);
  57. }, args);
  58. }
  59. }
  60. GIVEN("A polymorphic base class type Foo") {
  61. WHEN("Creating a subclass on the stack") {
  62. Bar bar;
  63. auto val = random_value(-100, 100);
  64. bar.set_foo(val);
  65. THEN("Storing a raw pointer in an AnyPtr<Foo> should be valid "
  66. "until the object is not destroyed")
  67. {
  68. AnyPtr<Foo> ptr = &bar;
  69. auto val2 = random_value(-100, 100);
  70. ptr->set_foo(val2);
  71. REQUIRE(ptr->get_foo() == val2);
  72. }
  73. THEN("Storing a raw pointer in an AnyPtr<const Foo> should be "
  74. "valid until the object is not destroyed")
  75. {
  76. AnyPtr<const Foo> ptr{&bar};
  77. REQUIRE(ptr->get_foo() == val);
  78. }
  79. }
  80. }
  81. GIVEN("An empty AnyPtr of type Foo")
  82. {
  83. AnyPtr<Foo> ptr;
  84. WHEN("Re-assigning a new unique_ptr of object of type Bar to ptr")
  85. {
  86. auto bar = std::make_unique<Bar>();
  87. auto val = random_value(-100, 100);
  88. bar->set_foo(val);
  89. ptr = std::move(bar);
  90. THEN("the ptr should contain the new object and should own it")
  91. {
  92. REQUIRE(ptr->get_foo() == val);
  93. REQUIRE(ptr.is_owned());
  94. }
  95. }
  96. WHEN("Re-assigning a new unique_ptr of object of type BarPlus to ptr")
  97. {
  98. auto barplus = std::make_unique<BarPlus>();
  99. auto val = random_value(-100, 100);
  100. barplus->set_foo(val);
  101. ptr = std::move(barplus);
  102. THEN("the ptr should contain the new object and should own it")
  103. {
  104. REQUIRE(ptr->get_foo() == val + 1);
  105. REQUIRE(ptr.is_owned());
  106. }
  107. THEN("copying the stored object into a shared_ptr should be invalid")
  108. {
  109. std::shared_ptr<Foo> shptr = ptr.get_shared_cpy();
  110. REQUIRE(!shptr);
  111. }
  112. THEN("copying the stored object into a shared_ptr after calling "
  113. "convert_unique_to_shared should be valid")
  114. {
  115. ptr.convert_unique_to_shared();
  116. std::shared_ptr<Foo> shptr = ptr.get_shared_cpy();
  117. REQUIRE(shptr);
  118. REQUIRE(shptr->get_foo() == val + 1);
  119. }
  120. }
  121. }
  122. GIVEN("A vector of AnyPtr<Foo> pointer to random Bar or BarPlus objects")
  123. {
  124. std::vector<AnyPtr<Foo>> ptrs;
  125. auto N = random_value(size_t(1), size_t(10));
  126. INFO("N = " << N);
  127. std::generate_n(std::back_inserter(ptrs), N, []{
  128. auto v = random_value(0, 1);
  129. std::unique_ptr<Foo> ret;
  130. if (v)
  131. ret = std::make_unique<Bar>();
  132. else
  133. ret = std::make_unique<BarPlus>();
  134. return ret;
  135. });
  136. WHEN("moving the whole array into a vector of AnyPtr<const Foo>")
  137. {
  138. THEN("the move should be valid")
  139. {
  140. std::vector<AnyPtr<const Foo>> constptrs;
  141. std::vector<int> vals;
  142. std::transform(ptrs.begin(), ptrs.end(),
  143. std::back_inserter(vals),
  144. [](auto &p) { return p->get_foo(); });
  145. std::move(ptrs.begin(), ptrs.end(), std::back_inserter(constptrs));
  146. REQUIRE(constptrs.size() == N);
  147. REQUIRE(ptrs.size() == N);
  148. REQUIRE(std::all_of(ptrs.begin(), ptrs.end(), [](auto &p) { return !p; }));
  149. for (size_t i = 0; i < N; ++i) {
  150. REQUIRE(vals[i] == constptrs[i]->get_foo());
  151. }
  152. }
  153. }
  154. }
  155. }