1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "gtest/gtest.h"
18 
19 #include "chre/util/system/ref_base.h"
20 
21 namespace {
22 
23 class TestBase : public chre::RefBase<TestBase> {
24  public:
~TestBase()25   ~TestBase() {
26     destructorCount++;
27   }
28   static int destructorCount;
29 };
30 
31 int TestBase::destructorCount = 0;
32 
33 class RefBaseTest : public testing::Test {
34  public:
SetUp()35   void SetUp() override {
36     TestBase::destructorCount = 0;
37     mObject = static_cast<TestBase *>(chre::memoryAlloc(sizeof(TestBase)));
38     new (mObject) TestBase();
39   }
40 
41   TestBase *mObject;
42 };
43 
44 }  // namespace
45 
TEST_F(RefBaseTest,DecRef)46 TEST_F(RefBaseTest, DecRef) {
47   mObject->decRef();
48   EXPECT_EQ(1, TestBase::destructorCount);
49 }
50 
TEST_F(RefBaseTest,TwoIncRef)51 TEST_F(RefBaseTest, TwoIncRef) {
52   mObject->incRef();
53 
54   mObject->decRef();
55   EXPECT_EQ(0, TestBase::destructorCount);
56 
57   mObject->decRef();
58   EXPECT_EQ(1, TestBase::destructorCount);
59 }
60