xpcom/tests/TestAutoRef.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestAutoRef.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,62 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +// vim:cindent:ts=4:et:sw=4:
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsAutoRef.h"
    1.11 +#include "TestHarness.h"
    1.12 +
    1.13 +#define TEST(aCondition, aMsg) \
    1.14 +  if (!(aCondition)) { fail("TestAutoRef: "#aMsg); exit(1); }
    1.15 +
    1.16 +struct TestObjectA {
    1.17 +public:
    1.18 +  TestObjectA() : mRefCnt(0) {
    1.19 +  }
    1.20 +
    1.21 +  ~TestObjectA() {
    1.22 +    TEST(mRefCnt == 0, "mRefCnt in destructor");
    1.23 +  }
    1.24 +
    1.25 +public:
    1.26 +  int mRefCnt;
    1.27 +};
    1.28 +
    1.29 +template <>
    1.30 +class nsAutoRefTraits<TestObjectA> : public nsPointerRefTraits<TestObjectA>
    1.31 +{
    1.32 +public:
    1.33 +  static int mTotalRefsCnt;
    1.34 +
    1.35 +  static void Release(TestObjectA *ptr) {
    1.36 +    ptr->mRefCnt--;
    1.37 +    if (ptr->mRefCnt == 0) {
    1.38 +      delete ptr;
    1.39 +    }
    1.40 +  }
    1.41 +
    1.42 +  static void AddRef(TestObjectA *ptr) {
    1.43 +    ptr->mRefCnt++;
    1.44 +  }
    1.45 +};
    1.46 +
    1.47 +int nsAutoRefTraits<TestObjectA>::mTotalRefsCnt = 0;
    1.48 +
    1.49 +int main()
    1.50 +{
    1.51 +  {
    1.52 +    nsCountedRef<TestObjectA> a(new TestObjectA());
    1.53 +    TEST(a->mRefCnt == 1, "nsCountedRef instantiation with valid RawRef");
    1.54 +
    1.55 +    nsCountedRef<TestObjectA> b;
    1.56 +    TEST(b.get() == nullptr, "nsCountedRef instantiation with invalid RawRef");
    1.57 +
    1.58 +    a.swap(b);
    1.59 +    TEST(b->mRefCnt, "nsAutoRef::swap() t1");
    1.60 +    TEST(a.get() == nullptr, "nsAutoRef::swap() t2");
    1.61 +  }
    1.62 +
    1.63 +  TEST(true, "All tests pass");
    1.64 +  return 0;
    1.65 +}

mercurial