test_own_ptr.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (C) 2015-2016 Intel Corporation. All rights reserved.
  3. *
  4. * This file is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <AP_gtest.h>
  18. #include <utility>
  19. #include <AP_HAL/utility/OwnPtr.h>
  20. TEST(OwnPtrTest, SamePointer)
  21. {
  22. int *a = new int{42};
  23. AP_HAL::OwnPtr<int> own(a);
  24. EXPECT_TRUE(own == a);
  25. EXPECT_TRUE(a == own);
  26. EXPECT_FALSE(own != a);
  27. EXPECT_FALSE(a != own);
  28. EXPECT_EQ(a, own.get());
  29. }
  30. TEST(OwnPtrTest, MoveOwnership)
  31. {
  32. int *a = new int{42};
  33. AP_HAL::OwnPtr<int> own(a);
  34. AP_HAL::OwnPtr<int> own2 = std::move(own);
  35. EXPECT_EQ(own2.get(), a);
  36. EXPECT_EQ(own.get(), nullptr);
  37. }
  38. class TestDeleted {
  39. public:
  40. TestDeleted(unsigned int &d) : deleted(d) { }
  41. ~TestDeleted() { deleted++; }
  42. unsigned int &deleted;
  43. };
  44. TEST(OwnPtrTest, DeleteOutOfScope)
  45. {
  46. unsigned int deleted = 0;
  47. {
  48. AP_HAL::OwnPtr<TestDeleted> own(new TestDeleted{deleted});
  49. }
  50. EXPECT_EQ(deleted, 1U);
  51. }
  52. TEST(OwnPtrTest, DeleteOutOfScopeAfterMove)
  53. {
  54. unsigned int deleted = 0;
  55. AP_HAL::OwnPtr<TestDeleted> own;
  56. {
  57. AP_HAL::OwnPtr<TestDeleted> own2(new TestDeleted{deleted});
  58. own = std::move(own2);
  59. }
  60. // own2 is now out of scope, but it has been moved already
  61. EXPECT_EQ(deleted, 0U);
  62. // now remove also 'own'
  63. own.clear();
  64. EXPECT_EQ(deleted, 1U);
  65. }
  66. class TestCall {
  67. public:
  68. int foo() { return 42; }
  69. };
  70. TEST(OwnPtrTest, CallMethod)
  71. {
  72. AP_HAL::OwnPtr<TestCall> own(new TestCall{});
  73. EXPECT_EQ(own->foo(), 42);
  74. EXPECT_EQ((*own).foo(), 42);
  75. }
  76. class TestDestructor {
  77. public:
  78. TestDestructor(AP_HAL::OwnPtr<TestDeleted> v) : _v(std::move(v)) { }
  79. AP_HAL::OwnPtr<TestDeleted> _v;
  80. };
  81. TEST(OwnPtrTest, MoveToConstructor)
  82. {
  83. unsigned int deleted = 0;
  84. AP_HAL::OwnPtr<TestDeleted> own(new TestDeleted{deleted});
  85. {
  86. EXPECT_EQ(0U, deleted);
  87. TestDestructor destructor{std::move(own)};
  88. EXPECT_EQ(0U, deleted);
  89. }
  90. EXPECT_EQ(1U, deleted);
  91. }
  92. static AP_HAL::OwnPtr<TestDeleted> create_test_deleted(unsigned int &deleted)
  93. {
  94. return AP_HAL::OwnPtr<TestDeleted>(new TestDeleted(deleted));
  95. }
  96. TEST(OwnPtrTest, ReturnType)
  97. {
  98. unsigned int deleted = 0;
  99. auto own = create_test_deleted(deleted);
  100. EXPECT_EQ(0U, deleted);
  101. {
  102. auto own2 = create_test_deleted(deleted);
  103. EXPECT_EQ(0U, deleted);
  104. }
  105. EXPECT_EQ(1U, deleted);
  106. own.clear();
  107. EXPECT_EQ(2U, deleted);
  108. }
  109. TEST(OwnPtrTest, ReplacePointer)
  110. {
  111. unsigned int deleted1 = 0;
  112. unsigned int deleted2 = 0;
  113. auto own = create_test_deleted(deleted1);
  114. EXPECT_EQ(0U, deleted1);
  115. {
  116. own = create_test_deleted(deleted2);
  117. EXPECT_EQ(1U, deleted1);
  118. }
  119. EXPECT_EQ(0U, deleted2);
  120. own = nullptr;
  121. EXPECT_EQ(1U, deleted2);
  122. }
  123. TEST(OwnPtrTest, ReplaceWithRawPointer)
  124. {
  125. unsigned int deleted1 = 0;
  126. auto own = create_test_deleted(deleted1);
  127. EXPECT_EQ(0U, deleted1);
  128. {
  129. own = new TestDeleted{deleted1};
  130. EXPECT_EQ(1U, deleted1);
  131. }
  132. }
  133. TEST(OwnPtrTest, Empty)
  134. {
  135. int *a = new int{42};
  136. AP_HAL::OwnPtr<int> own1;
  137. EXPECT_FALSE(own1);
  138. own1 = a;
  139. EXPECT_TRUE((bool) own1);
  140. }
  141. class A {
  142. public:
  143. A(int a) : _a(a) { }
  144. int _a;
  145. };
  146. class B : public A {
  147. public:
  148. B() : A(42) { }
  149. };
  150. TEST(OwnPtrTest, Inheritance)
  151. {
  152. A *a = new A(21);
  153. B *b = new B();
  154. AP_HAL::OwnPtr<A> own_a(a);
  155. AP_HAL::OwnPtr<B> own_b(b);
  156. own_a = std::move(own_b);
  157. EXPECT_EQ(b, own_a.get());
  158. EXPECT_EQ(42, own_a->_a);
  159. }
  160. AP_GTEST_MAIN()