UniquePointerTests.cpp 1.5 KB

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