michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: // vim:cindent:ts=4:et:sw=4: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsAutoRef.h" michael@0: #include "TestHarness.h" michael@0: michael@0: #define TEST(aCondition, aMsg) \ michael@0: if (!(aCondition)) { fail("TestAutoRef: "#aMsg); exit(1); } michael@0: michael@0: struct TestObjectA { michael@0: public: michael@0: TestObjectA() : mRefCnt(0) { michael@0: } michael@0: michael@0: ~TestObjectA() { michael@0: TEST(mRefCnt == 0, "mRefCnt in destructor"); michael@0: } michael@0: michael@0: public: michael@0: int mRefCnt; michael@0: }; michael@0: michael@0: template <> michael@0: class nsAutoRefTraits : public nsPointerRefTraits michael@0: { michael@0: public: michael@0: static int mTotalRefsCnt; michael@0: michael@0: static void Release(TestObjectA *ptr) { michael@0: ptr->mRefCnt--; michael@0: if (ptr->mRefCnt == 0) { michael@0: delete ptr; michael@0: } michael@0: } michael@0: michael@0: static void AddRef(TestObjectA *ptr) { michael@0: ptr->mRefCnt++; michael@0: } michael@0: }; michael@0: michael@0: int nsAutoRefTraits::mTotalRefsCnt = 0; michael@0: michael@0: int main() michael@0: { michael@0: { michael@0: nsCountedRef a(new TestObjectA()); michael@0: TEST(a->mRefCnt == 1, "nsCountedRef instantiation with valid RawRef"); michael@0: michael@0: nsCountedRef b; michael@0: TEST(b.get() == nullptr, "nsCountedRef instantiation with invalid RawRef"); michael@0: michael@0: a.swap(b); michael@0: TEST(b->mRefCnt, "nsAutoRef::swap() t1"); michael@0: TEST(a.get() == nullptr, "nsAutoRef::swap() t2"); michael@0: } michael@0: michael@0: TEST(true, "All tests pass"); michael@0: return 0; michael@0: }