value_ptr.t.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // copy_ptr.t.h
  3. // pointers
  4. //
  5. // Created by Sam Jaffe on 1/5/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include <stdexcept>
  10. #include "pointer/value_ptr.hpp"
  11. struct copy_me {};
  12. struct copy_me_throw {};
  13. namespace detail {
  14. template<> class value_ptr_copy<copy_me_throw> {
  15. protected:
  16. copy_me_throw * Copy(copy_me_throw const * p) const {
  17. if (p == nullptr) { throw std::runtime_error{"NULL"}; }
  18. return new copy_me_throw{*p};
  19. }
  20. };
  21. }
  22. class copy_ptr_TestSuite : public CxxTest::TestSuite {
  23. private:
  24. class base {
  25. public:
  26. virtual ~base() {}
  27. virtual base* clone() const = 0;
  28. virtual int id() const = 0;
  29. };
  30. class derived_0 : public base {
  31. public:
  32. static const constexpr int ID = 0;
  33. virtual base * clone() const override { return new derived_0; }
  34. virtual int id() const override { return ID; }
  35. };
  36. class derived_1 : public base {
  37. public:
  38. static const constexpr int ID = 1;
  39. virtual base * clone() const override { return new derived_1; }
  40. virtual int id() const override { return ID; }
  41. };
  42. public:
  43. void test_copy_new_obj() {
  44. value_ptr<copy_me> c1 { new copy_me };
  45. value_ptr<copy_me> c2 { c1 };
  46. TS_ASSERT_DIFFERS( c1.get(), c2.get() );
  47. }
  48. void test_copy_with_nullptr() {
  49. value_ptr<copy_me> c1 { nullptr };
  50. value_ptr<copy_me> c2 { c1 };
  51. TS_ASSERT_EQUALS( c1.get(), nullptr );
  52. TS_ASSERT_EQUALS( c1.get(), c2.get() );
  53. }
  54. void test_copy_custom_func() {
  55. using ptr_t = value_ptr<copy_me_throw>;
  56. ptr_t c1 { new copy_me_throw };
  57. ptr_t c2 { c1 };
  58. TS_ASSERT_DIFFERS( c1.get(), c2.get() );
  59. }
  60. void test_copy_custom_func_with_nullptr() {
  61. using ptr_t = value_ptr<copy_me_throw>;
  62. ptr_t c1 { nullptr };
  63. TS_ASSERT_THROWS( ptr_t{ c1 }, std::runtime_error );
  64. }
  65. void test_copy_object_is_deep_equals() {
  66. using vec_t = std::vector<int>;
  67. vec_t my_vec = { 1, 3, 5, 3, 6, 1, 2, -1, 0 };
  68. value_ptr<vec_t> c1{ new vec_t{ my_vec } };
  69. TS_ASSERT_EQUALS( *c1, my_vec );
  70. value_ptr<vec_t> c2{ c1 };
  71. TS_ASSERT_EQUALS( *c2, *c1 );
  72. }
  73. void test_clone_polymorpic_object() {
  74. using ptr_t = value_ptr<base>;
  75. ptr_t c0 { new derived_0 };
  76. TS_ASSERT_EQUALS( ptr_t( c0 )->id(), derived_0::ID);
  77. ptr_t c1 { new derived_1 };
  78. TS_ASSERT_EQUALS( ptr_t( c1 )->id(), derived_1::ID);
  79. }
  80. void test_clone_polymorphic_with_nullptr() {
  81. using ptr_t = value_ptr<base>;
  82. ptr_t c1 { nullptr };
  83. ptr_t c2 { c1 };
  84. TS_ASSERT_EQUALS( c1.get(), nullptr );
  85. TS_ASSERT_EQUALS( c1.get(), c2.get() );
  86. }
  87. void test_does_own() const {
  88. bool has_delete{false};
  89. struct test_t {
  90. ~test_t() { _r = true; }
  91. bool & _r;
  92. };
  93. test_t * test_struct = new test_t{has_delete};
  94. TS_ASSERT_THROWS_NOTHING(value_ptr<test_t>{ std::move(test_struct) });
  95. TS_ASSERT_EQUALS(has_delete, true);
  96. }
  97. void test_does_own_temporary() const {
  98. bool has_delete{false};
  99. struct test_t {
  100. ~test_t() { _r = true; }
  101. bool & _r;
  102. };
  103. TS_ASSERT_THROWS_NOTHING(value_ptr<test_t>{ new test_t{has_delete} });
  104. TS_ASSERT_EQUALS(has_delete, true);
  105. }
  106. void test_does_copy_on_copy() const {
  107. int deleted{0};
  108. struct test_t {
  109. ~test_t() { ++(*_r); }
  110. int * _r;
  111. };
  112. {
  113. std::unique_ptr<test_t> test_struct{new test_t{&deleted}};
  114. TS_ASSERT_THROWS_NOTHING(value_ptr<test_t>::copy_of( test_struct.get() ));
  115. TS_ASSERT_EQUALS(deleted, 1);
  116. }
  117. TS_ASSERT_EQUALS(deleted, 2);
  118. }
  119. };