/src/mozilla-central/xpcom/tests/gtest/TestAutoRef.cpp
Line | Count | Source (jump to first uncovered line) |
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 "gtest/gtest.h" |
9 | | |
10 | | struct TestObjectA { |
11 | | public: |
12 | 0 | TestObjectA() : mRefCnt(0) { |
13 | 0 | } |
14 | | |
15 | 0 | ~TestObjectA() { |
16 | 0 | EXPECT_EQ(mRefCnt, 0); |
17 | 0 | } |
18 | | |
19 | | public: |
20 | | int mRefCnt; |
21 | | }; |
22 | | |
23 | | template <> |
24 | | class nsAutoRefTraits<TestObjectA> : public nsPointerRefTraits<TestObjectA> |
25 | | { |
26 | | public: |
27 | | static int mTotalRefsCnt; |
28 | | |
29 | 0 | static void Release(TestObjectA *ptr) { |
30 | 0 | ptr->mRefCnt--; |
31 | 0 | if (ptr->mRefCnt == 0) { |
32 | 0 | delete ptr; |
33 | 0 | } |
34 | 0 | } |
35 | | |
36 | 0 | static void AddRef(TestObjectA *ptr) { |
37 | 0 | ptr->mRefCnt++; |
38 | 0 | } |
39 | | }; |
40 | | |
41 | | int nsAutoRefTraits<TestObjectA>::mTotalRefsCnt = 0; |
42 | | |
43 | | TEST(AutoRef, Assignment) |
44 | 0 | { |
45 | 0 | { |
46 | 0 | nsCountedRef<TestObjectA> a(new TestObjectA()); |
47 | 0 | ASSERT_EQ(a->mRefCnt, 1); |
48 | 0 |
|
49 | 0 | nsCountedRef<TestObjectA> b; |
50 | 0 | ASSERT_EQ(b.get(), nullptr); |
51 | 0 |
|
52 | 0 | a.swap(b); |
53 | 0 | ASSERT_EQ(b->mRefCnt, 1); |
54 | 0 | ASSERT_EQ(a.get(), nullptr); |
55 | 0 | } |
56 | 0 | } |