UniquePointerTests.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "../Tests.hpp"
  2. #include "core/utils/UniquePointer.hpp"
  3. struct B final {
  4. static int instances;
  5. bool b = false;
  6. B() {
  7. instances++;
  8. }
  9. ~B() {
  10. instances--;
  11. }
  12. void test() {
  13. b = true;
  14. }
  15. };
  16. int B::instances = 0;
  17. template class Core::UniquePointer<B>;
  18. using UniqueB = Core::UniquePointer<B>;
  19. static void testDestroy() {
  20. {
  21. UniqueB p(new B());
  22. CORE_TEST_EQUAL(1, B::instances);
  23. }
  24. CORE_TEST_EQUAL(0, B::instances);
  25. }
  26. static void testMoveConstructDestroys() {
  27. UniqueB p1(new B());
  28. CORE_TEST_EQUAL(1, B::instances);
  29. UniqueB p2(Core::move(p1));
  30. CORE_TEST_EQUAL(1, B::instances);
  31. p2 = nullptr;
  32. CORE_TEST_EQUAL(0, B::instances);
  33. }
  34. static void testMoveDestroys() {
  35. {
  36. UniqueB p1(new B());
  37. UniqueB p2(new B());
  38. CORE_TEST_EQUAL(2, B::instances);
  39. p1 = Core::move(p2);
  40. CORE_TEST_EQUAL(1, B::instances);
  41. }
  42. CORE_TEST_EQUAL(0, B::instances);
  43. }
  44. static void testEmpty() {
  45. UniqueB p;
  46. CORE_TEST_TRUE(p == nullptr);
  47. CORE_TEST_TRUE(static_cast<const UniqueB&>(p) == nullptr);
  48. }
  49. static void testCall() {
  50. UniqueB p(new B());
  51. CORE_TEST_FALSE(p->b);
  52. p->test();
  53. CORE_TEST_TRUE(p->b);
  54. CORE_TEST_TRUE(static_cast<const UniqueB&>(p)->b);
  55. }
  56. void Core::testUniquePointer() {
  57. testDestroy();
  58. testMoveConstructDestroys();
  59. testMoveDestroys();
  60. testEmpty();
  61. testCall();
  62. }