UniquePointerTests.cpp 952 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "tests/UniquePointerTests.h"
  2. #include "memory/UniquePointer.h"
  3. #include "tests/Test.h"
  4. struct B {
  5. static int instances;
  6. B() {
  7. instances++;
  8. }
  9. ~B() {
  10. instances--;
  11. }
  12. };
  13. int B::instances = 0;
  14. static void testDestroy(Test& test) {
  15. {
  16. UniquePointer<B> p(new B());
  17. test.checkEqual(1, B::instances, "one instance");
  18. }
  19. test.checkEqual(0, B::instances, "instance is destroyed");
  20. }
  21. static void testMoveDestroys(Test& test) {
  22. {
  23. UniquePointer<B> p1(new B());
  24. UniquePointer<B> p2(new B());
  25. test.checkEqual(2, B::instances, "two instances");
  26. p1 = std::move(p2);
  27. test.checkEqual(1, B::instances, "one after move");
  28. }
  29. test.checkEqual(0, B::instances, "everything destroyed correctly after move");
  30. }
  31. void UniquePointerTests::test() {
  32. Test test("UniquePointer");
  33. testDestroy(test);
  34. testMoveDestroys(test);
  35. test.finalize();
  36. }