|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 // vim:cindent:ts=4:et:sw=4: |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "nsAutoRef.h" |
|
8 #include "TestHarness.h" |
|
9 |
|
10 #define TEST(aCondition, aMsg) \ |
|
11 if (!(aCondition)) { fail("TestAutoRef: "#aMsg); exit(1); } |
|
12 |
|
13 struct TestObjectA { |
|
14 public: |
|
15 TestObjectA() : mRefCnt(0) { |
|
16 } |
|
17 |
|
18 ~TestObjectA() { |
|
19 TEST(mRefCnt == 0, "mRefCnt in destructor"); |
|
20 } |
|
21 |
|
22 public: |
|
23 int mRefCnt; |
|
24 }; |
|
25 |
|
26 template <> |
|
27 class nsAutoRefTraits<TestObjectA> : public nsPointerRefTraits<TestObjectA> |
|
28 { |
|
29 public: |
|
30 static int mTotalRefsCnt; |
|
31 |
|
32 static void Release(TestObjectA *ptr) { |
|
33 ptr->mRefCnt--; |
|
34 if (ptr->mRefCnt == 0) { |
|
35 delete ptr; |
|
36 } |
|
37 } |
|
38 |
|
39 static void AddRef(TestObjectA *ptr) { |
|
40 ptr->mRefCnt++; |
|
41 } |
|
42 }; |
|
43 |
|
44 int nsAutoRefTraits<TestObjectA>::mTotalRefsCnt = 0; |
|
45 |
|
46 int main() |
|
47 { |
|
48 { |
|
49 nsCountedRef<TestObjectA> a(new TestObjectA()); |
|
50 TEST(a->mRefCnt == 1, "nsCountedRef instantiation with valid RawRef"); |
|
51 |
|
52 nsCountedRef<TestObjectA> b; |
|
53 TEST(b.get() == nullptr, "nsCountedRef instantiation with invalid RawRef"); |
|
54 |
|
55 a.swap(b); |
|
56 TEST(b->mRefCnt, "nsAutoRef::swap() t1"); |
|
57 TEST(a.get() == nullptr, "nsAutoRef::swap() t2"); |
|
58 } |
|
59 |
|
60 TEST(true, "All tests pass"); |
|
61 return 0; |
|
62 } |