diff options
Diffstat (limited to 'base/base_tests/ref_counted_tests.cpp')
-rw-r--r-- | base/base_tests/ref_counted_tests.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/base/base_tests/ref_counted_tests.cpp b/base/base_tests/ref_counted_tests.cpp new file mode 100644 index 0000000000..de27a7c01f --- /dev/null +++ b/base/base_tests/ref_counted_tests.cpp @@ -0,0 +1,78 @@ +#include "testing/testing.hpp" + +#include "base/ref_counted.hpp" + +using namespace my; + +namespace +{ +struct Resource : public RefCounted +{ + Resource(bool & destroyed) : m_destroyed(destroyed) {} + ~Resource() override { m_destroyed = true; } + + bool & m_destroyed; +}; + +UNIT_TEST(RefCounted_Smoke) +{ + { + RefCountPtr<Resource> p; + } + + { + bool destroyed = false; + { + RefCountPtr<Resource> p(new Resource(destroyed)); + TEST_EQUAL(1, p->NumRefs(), ()); + TEST(!destroyed, ()); + } + TEST(destroyed, ()); + } + + { + bool destroyed = false; + { + 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(move(b)); + TEST(b.Get() == nullptr, ()); + TEST(a.Get() == d.Get(), ()); + TEST_EQUAL(2, a->NumRefs(), ()); + TEST(!destroyed, ()); + + a = a; + TEST_EQUAL(a.Get(), d.Get(), ()); + TEST_EQUAL(2, a->NumRefs(), ()); + TEST(!destroyed, ()); + + TEST_EQUAL(a.Get(), d.Get(), ()); + TEST_EQUAL(2, a->NumRefs(), ()); + TEST(!destroyed, ()); + } + TEST(destroyed, ()); + } +} +} // namespace |