Files
comaps/base/base_tests/ref_counted_tests.cpp
Konstantin Pastbin e3e4a1985a Organic Maps sources as of 02.04.2025 (fad26bbf22ac3da75e01e62aa01e5c8e11861005)
To expand with full Organic Maps and Maps.ME commits history run:
  git remote add om-historic [om-historic.git repo url]
  git fetch --tags om-historic
  git replace squashed-history historic-commits
2025-05-08 21:10:51 +07:00

84 lines
1.7 KiB
C++

#include "testing/testing.hpp"
#include "base/ref_counted.hpp"
using namespace base;
namespace
{
struct Resource : public RefCounted
{
explicit Resource(bool & destroyed) : m_destroyed(destroyed) { m_destroyed = false; }
~Resource() override { m_destroyed = true; }
bool & m_destroyed;
};
UNIT_TEST(RefCounted_Smoke)
{
{
RefCountPtr<Resource> p;
}
{
bool destroyed;
{
RefCountPtr<Resource> p(new Resource(destroyed));
TEST_EQUAL(1, p->NumRefs(), ());
TEST(!destroyed, ());
}
TEST(destroyed, ());
}
{
bool destroyed;
{
RefCountPtr<Resource> a(new Resource(destroyed));
TEST_EQUAL(1, a->NumRefs(), ());
TEST(!destroyed, ());
RefCountPtr<Resource> b(a);
TEST(a.Get() == b.Get(), ());
TEST_EQUAL(2, a->NumRefs(), ());
TEST(!destroyed, ());
{
RefCountPtr<Resource> c;
TEST(c.Get() == nullptr, ());
c = b;
TEST(a.Get() == b.Get(), ());
TEST(b.Get() == c.Get(), ());
TEST_EQUAL(3, a->NumRefs(), ());
TEST(!destroyed, ());
}
TEST(a.Get() == b.Get(), ());
TEST_EQUAL(2, a->NumRefs(), ());
TEST(!destroyed, ());
RefCountPtr<Resource> d(std::move(b));
TEST(b.Get() == nullptr, ());
TEST(a.Get() == d.Get(), ());
TEST_EQUAL(2, a->NumRefs(), ());
TEST(!destroyed, ());
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wself-assign-overloaded"
#endif // #ifdef __clang__
a = a;
#ifdef __clang__
#pragma clang diagnostic pop
#endif // #ifdef __clang__
TEST_EQUAL(a.Get(), d.Get(), ());
TEST_EQUAL(2, a->NumRefs(), ());
TEST(!destroyed, ());
}
TEST(destroyed, ());
}
}
} // namespace