test_irrptr.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Minetest
  3. Copyright (C) 2018 numzero, Lobachevskiy Vitaliy <numzer0@yandex.ru>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "test.h"
  17. #include "exceptions.h"
  18. #include "irr_ptr.h"
  19. class TestIrrPtr : public TestBase
  20. {
  21. public:
  22. TestIrrPtr() { TestManager::registerTestModule(this); }
  23. const char *getName() { return "TestIrrPtr"; }
  24. void runTests(IGameDef *gamedef);
  25. void testRefCounting();
  26. void testSelfAssignment();
  27. void testNullHandling();
  28. };
  29. static TestIrrPtr g_test_instance;
  30. void TestIrrPtr::runTests(IGameDef *gamedef)
  31. {
  32. TEST(testRefCounting);
  33. TEST(testSelfAssignment);
  34. TEST(testNullHandling);
  35. }
  36. ////////////////////////////////////////////////////////////////////////////////
  37. #define UASSERT_REFERENCE_COUNT(object, value, info) \
  38. UTEST((object)->getReferenceCount() == value, \
  39. info "Reference count is %d instead of " #value, \
  40. (object)->getReferenceCount())
  41. void TestIrrPtr::testRefCounting()
  42. {
  43. IReferenceCounted *obj = new IReferenceCounted(); // RC=1
  44. obj->grab();
  45. UASSERT_REFERENCE_COUNT(obj, 2, "Pre-condition failed: ");
  46. {
  47. irr_ptr<IReferenceCounted> p1{obj}; // move semantics
  48. UASSERT(p1.get() == obj);
  49. UASSERT_REFERENCE_COUNT(obj, 2, );
  50. irr_ptr<IReferenceCounted> p2{p1}; // copy ctor
  51. UASSERT(p1.get() == obj);
  52. UASSERT(p2.get() == obj);
  53. UASSERT_REFERENCE_COUNT(obj, 3, );
  54. irr_ptr<IReferenceCounted> p3{std::move(p1)}; // move ctor
  55. UASSERT(p1.get() == nullptr);
  56. UASSERT(p3.get() == obj);
  57. UASSERT_REFERENCE_COUNT(obj, 3, );
  58. p1 = std::move(p2); // move assignment
  59. UASSERT(p1.get() == obj);
  60. UASSERT(p2.get() == nullptr);
  61. UASSERT_REFERENCE_COUNT(obj, 3, );
  62. p2 = p3; // copy assignment
  63. UASSERT(p2.get() == obj);
  64. UASSERT(p3.get() == obj);
  65. UASSERT_REFERENCE_COUNT(obj, 4, );
  66. p1.release();
  67. UASSERT(p1.get() == nullptr);
  68. UASSERT_REFERENCE_COUNT(obj, 4, );
  69. }
  70. UASSERT_REFERENCE_COUNT(obj, 2, );
  71. obj->drop();
  72. UTEST(obj->drop(), "Dropping failed: reference count is %d",
  73. obj->getReferenceCount());
  74. }
  75. void TestIrrPtr::testSelfAssignment()
  76. {
  77. irr_ptr<IReferenceCounted> p1{new IReferenceCounted()};
  78. UASSERT(p1);
  79. UASSERT_REFERENCE_COUNT(p1, 1, );
  80. p1 = p1;
  81. UASSERT(p1);
  82. UASSERT_REFERENCE_COUNT(p1, 1, );
  83. p1 = std::move(p1);
  84. UASSERT(p1);
  85. UASSERT_REFERENCE_COUNT(p1, 1, );
  86. }
  87. void TestIrrPtr::testNullHandling()
  88. {
  89. // In the case of an error, it will probably crash with SEGV.
  90. // Nevertheless, UASSERTs are used to catch possible corner cases.
  91. irr_ptr<IReferenceCounted> p1{new IReferenceCounted()};
  92. UASSERT(p1);
  93. irr_ptr<IReferenceCounted> p2;
  94. UASSERT(!p2);
  95. irr_ptr<IReferenceCounted> p3{p2};
  96. UASSERT(!p2);
  97. UASSERT(!p3);
  98. irr_ptr<IReferenceCounted> p4{std::move(p2)};
  99. UASSERT(!p2);
  100. UASSERT(!p4);
  101. p2 = p2;
  102. UASSERT(!p2);
  103. p2 = std::move(p2);
  104. UASSERT(!p2);
  105. p3 = p2;
  106. UASSERT(!p2);
  107. UASSERT(!p3);
  108. p3 = std::move(p2);
  109. UASSERT(!p2);
  110. UASSERT(!p3);
  111. }